]> code.delx.au - gnu-emacs/blob - src/xdisp.c
*** empty log message ***
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24
25 Redisplay.
26
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
31
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
39
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
49
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
63 |
64 expose_window (asynchronous) |
65 |
66 X expose events -----+
67
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
72
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
82
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
88
89
90 Direct operations.
91
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
96
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
103
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
109
110
111 Desired matrices.
112
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
119
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
126
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "keymap.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "coding.h"
189 #include "process.h"
190 #include "region-cache.h"
191 #include "fontset.h"
192 #include "blockinput.h"
193
194 #ifdef HAVE_X_WINDOWS
195 #include "xterm.h"
196 #endif
197 #ifdef WINDOWSNT
198 #include "w32term.h"
199 #endif
200 #ifdef MAC_OS
201 #include "macterm.h"
202 #endif
203
204 #ifndef FRAME_X_OUTPUT
205 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
206 #endif
207
208 #define INFINITY 10000000
209
210 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
211 || defined (USE_GTK)
212 extern void set_frame_menubar P_ ((struct frame *f, int, int));
213 extern int pending_menu_activation;
214 #endif
215
216 extern int interrupt_input;
217 extern int command_loop_level;
218
219 extern Lisp_Object do_mouse_tracking;
220
221 extern int minibuffer_auto_raise;
222 extern Lisp_Object Vminibuffer_list;
223
224 extern Lisp_Object Qface;
225 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
226
227 extern Lisp_Object Voverriding_local_map;
228 extern Lisp_Object Voverriding_local_map_menu_flag;
229 extern Lisp_Object Qmenu_item;
230 extern Lisp_Object Qwhen;
231 extern Lisp_Object Qhelp_echo;
232
233 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
234 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
235 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
236 Lisp_Object Qinhibit_point_motion_hooks;
237 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
238 Lisp_Object Qfontified;
239 Lisp_Object Qgrow_only;
240 Lisp_Object Qinhibit_eval_during_redisplay;
241 Lisp_Object Qbuffer_position, Qposition, Qobject;
242
243 /* Cursor shapes */
244 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
245
246 /* Pointer shapes */
247 Lisp_Object Qarrow, Qhand, Qtext;
248
249 Lisp_Object Qrisky_local_variable;
250
251 /* Holds the list (error). */
252 Lisp_Object list_of_error;
253
254 /* Functions called to fontify regions of text. */
255
256 Lisp_Object Vfontification_functions;
257 Lisp_Object Qfontification_functions;
258
259 /* Non-zero means automatically select any window when the mouse
260 cursor moves into it. */
261 int mouse_autoselect_window;
262
263 /* Non-zero means draw tool bar buttons raised when the mouse moves
264 over them. */
265
266 int auto_raise_tool_bar_buttons_p;
267
268 /* Non-zero means to reposition window if cursor line is only partially visible. */
269
270 int make_cursor_line_fully_visible_p;
271
272 /* Margin below tool bar in pixels. 0 or nil means no margin.
273 If value is `internal-border-width' or `border-width',
274 the corresponding frame parameter is used. */
275
276 Lisp_Object Vtool_bar_border;
277
278 /* Margin around tool bar buttons in pixels. */
279
280 Lisp_Object Vtool_bar_button_margin;
281
282 /* Thickness of shadow to draw around tool bar buttons. */
283
284 EMACS_INT tool_bar_button_relief;
285
286 /* Non-zero means automatically resize tool-bars so that all tool-bar
287 items are visible, and no blank lines remain. */
288
289 int auto_resize_tool_bars_p;
290
291 /* Non-zero means draw block and hollow cursor as wide as the glyph
292 under it. For example, if a block cursor is over a tab, it will be
293 drawn as wide as that tab on the display. */
294
295 int x_stretch_cursor_p;
296
297 /* Non-nil means don't actually do any redisplay. */
298
299 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
300
301 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
302
303 int inhibit_eval_during_redisplay;
304
305 /* Names of text properties relevant for redisplay. */
306
307 Lisp_Object Qdisplay;
308 extern Lisp_Object Qface, Qinvisible, Qwidth;
309
310 /* Symbols used in text property values. */
311
312 Lisp_Object Vdisplay_pixels_per_inch;
313 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
314 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
315 Lisp_Object Qslice;
316 Lisp_Object Qcenter;
317 Lisp_Object Qmargin, Qpointer;
318 Lisp_Object Qline_height;
319 extern Lisp_Object Qheight;
320 extern Lisp_Object QCwidth, QCheight, QCascent;
321 extern Lisp_Object Qscroll_bar;
322 extern Lisp_Object Qcursor;
323
324 /* Non-nil means highlight trailing whitespace. */
325
326 Lisp_Object Vshow_trailing_whitespace;
327
328 /* Non-nil means escape non-break space and hyphens. */
329
330 Lisp_Object Vnobreak_char_display;
331
332 #ifdef HAVE_WINDOW_SYSTEM
333 extern Lisp_Object Voverflow_newline_into_fringe;
334
335 /* Test if overflow newline into fringe. Called with iterator IT
336 at or past right window margin, and with IT->current_x set. */
337
338 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
339 (!NILP (Voverflow_newline_into_fringe) \
340 && FRAME_WINDOW_P (it->f) \
341 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
342 && it->current_x == it->last_visible_x)
343
344 #endif /* HAVE_WINDOW_SYSTEM */
345
346 /* Non-nil means show the text cursor in void text areas
347 i.e. in blank areas after eol and eob. This used to be
348 the default in 21.3. */
349
350 Lisp_Object Vvoid_text_area_pointer;
351
352 /* Name of the face used to highlight trailing whitespace. */
353
354 Lisp_Object Qtrailing_whitespace;
355
356 /* Name and number of the face used to highlight escape glyphs. */
357
358 Lisp_Object Qescape_glyph;
359
360 /* Name and number of the face used to highlight non-breaking spaces. */
361
362 Lisp_Object Qnobreak_space;
363
364 /* The symbol `image' which is the car of the lists used to represent
365 images in Lisp. */
366
367 Lisp_Object Qimage;
368
369 /* The image map types. */
370 Lisp_Object QCmap, QCpointer;
371 Lisp_Object Qrect, Qcircle, Qpoly;
372
373 /* Non-zero means print newline to stdout before next mini-buffer
374 message. */
375
376 int noninteractive_need_newline;
377
378 /* Non-zero means print newline to message log before next message. */
379
380 static int message_log_need_newline;
381
382 /* Three markers that message_dolog uses.
383 It could allocate them itself, but that causes trouble
384 in handling memory-full errors. */
385 static Lisp_Object message_dolog_marker1;
386 static Lisp_Object message_dolog_marker2;
387 static Lisp_Object message_dolog_marker3;
388 \f
389 /* The buffer position of the first character appearing entirely or
390 partially on the line of the selected window which contains the
391 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
392 redisplay optimization in redisplay_internal. */
393
394 static struct text_pos this_line_start_pos;
395
396 /* Number of characters past the end of the line above, including the
397 terminating newline. */
398
399 static struct text_pos this_line_end_pos;
400
401 /* The vertical positions and the height of this line. */
402
403 static int this_line_vpos;
404 static int this_line_y;
405 static int this_line_pixel_height;
406
407 /* X position at which this display line starts. Usually zero;
408 negative if first character is partially visible. */
409
410 static int this_line_start_x;
411
412 /* Buffer that this_line_.* variables are referring to. */
413
414 static struct buffer *this_line_buffer;
415
416 /* Nonzero means truncate lines in all windows less wide than the
417 frame. */
418
419 int truncate_partial_width_windows;
420
421 /* A flag to control how to display unibyte 8-bit character. */
422
423 int unibyte_display_via_language_environment;
424
425 /* Nonzero means we have more than one non-mini-buffer-only frame.
426 Not guaranteed to be accurate except while parsing
427 frame-title-format. */
428
429 int multiple_frames;
430
431 Lisp_Object Vglobal_mode_string;
432
433
434 /* List of variables (symbols) which hold markers for overlay arrows.
435 The symbols on this list are examined during redisplay to determine
436 where to display overlay arrows. */
437
438 Lisp_Object Voverlay_arrow_variable_list;
439
440 /* Marker for where to display an arrow on top of the buffer text. */
441
442 Lisp_Object Voverlay_arrow_position;
443
444 /* String to display for the arrow. Only used on terminal frames. */
445
446 Lisp_Object Voverlay_arrow_string;
447
448 /* Values of those variables at last redisplay are stored as
449 properties on `overlay-arrow-position' symbol. However, if
450 Voverlay_arrow_position is a marker, last-arrow-position is its
451 numerical position. */
452
453 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
454
455 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
456 properties on a symbol in overlay-arrow-variable-list. */
457
458 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
459
460 /* Like mode-line-format, but for the title bar on a visible frame. */
461
462 Lisp_Object Vframe_title_format;
463
464 /* Like mode-line-format, but for the title bar on an iconified frame. */
465
466 Lisp_Object Vicon_title_format;
467
468 /* List of functions to call when a window's size changes. These
469 functions get one arg, a frame on which one or more windows' sizes
470 have changed. */
471
472 static Lisp_Object Vwindow_size_change_functions;
473
474 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
475
476 /* Nonzero if an overlay arrow has been displayed in this window. */
477
478 static int overlay_arrow_seen;
479
480 /* Nonzero means highlight the region even in nonselected windows. */
481
482 int highlight_nonselected_windows;
483
484 /* If cursor motion alone moves point off frame, try scrolling this
485 many lines up or down if that will bring it back. */
486
487 static EMACS_INT scroll_step;
488
489 /* Nonzero means scroll just far enough to bring point back on the
490 screen, when appropriate. */
491
492 static EMACS_INT scroll_conservatively;
493
494 /* Recenter the window whenever point gets within this many lines of
495 the top or bottom of the window. This value is translated into a
496 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
497 that there is really a fixed pixel height scroll margin. */
498
499 EMACS_INT scroll_margin;
500
501 /* Number of windows showing the buffer of the selected window (or
502 another buffer with the same base buffer). keyboard.c refers to
503 this. */
504
505 int buffer_shared;
506
507 /* Vector containing glyphs for an ellipsis `...'. */
508
509 static Lisp_Object default_invis_vector[3];
510
511 /* Zero means display the mode-line/header-line/menu-bar in the default face
512 (this slightly odd definition is for compatibility with previous versions
513 of emacs), non-zero means display them using their respective faces.
514
515 This variable is deprecated. */
516
517 int mode_line_inverse_video;
518
519 /* Prompt to display in front of the mini-buffer contents. */
520
521 Lisp_Object minibuf_prompt;
522
523 /* Width of current mini-buffer prompt. Only set after display_line
524 of the line that contains the prompt. */
525
526 int minibuf_prompt_width;
527
528 /* This is the window where the echo area message was displayed. It
529 is always a mini-buffer window, but it may not be the same window
530 currently active as a mini-buffer. */
531
532 Lisp_Object echo_area_window;
533
534 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
535 pushes the current message and the value of
536 message_enable_multibyte on the stack, the function restore_message
537 pops the stack and displays MESSAGE again. */
538
539 Lisp_Object Vmessage_stack;
540
541 /* Nonzero means multibyte characters were enabled when the echo area
542 message was specified. */
543
544 int message_enable_multibyte;
545
546 /* Nonzero if we should redraw the mode lines on the next redisplay. */
547
548 int update_mode_lines;
549
550 /* Nonzero if window sizes or contents have changed since last
551 redisplay that finished. */
552
553 int windows_or_buffers_changed;
554
555 /* Nonzero means a frame's cursor type has been changed. */
556
557 int cursor_type_changed;
558
559 /* Nonzero after display_mode_line if %l was used and it displayed a
560 line number. */
561
562 int line_number_displayed;
563
564 /* Maximum buffer size for which to display line numbers. */
565
566 Lisp_Object Vline_number_display_limit;
567
568 /* Line width to consider when repositioning for line number display. */
569
570 static EMACS_INT line_number_display_limit_width;
571
572 /* Number of lines to keep in the message log buffer. t means
573 infinite. nil means don't log at all. */
574
575 Lisp_Object Vmessage_log_max;
576
577 /* The name of the *Messages* buffer, a string. */
578
579 static Lisp_Object Vmessages_buffer_name;
580
581 /* Index 0 is the buffer that holds the current (desired) echo area message,
582 or nil if none is desired right now.
583
584 Index 1 is the buffer that holds the previously displayed echo area message,
585 or nil to indicate no message. This is normally what's on the screen now.
586
587 These two can point to the same buffer. That happens when the last
588 message output by the user (or made by echoing) has been displayed. */
589
590 Lisp_Object echo_area_buffer[2];
591
592 /* Permanent pointers to the two buffers that are used for echo area
593 purposes. Once the two buffers are made, and their pointers are
594 placed here, these two slots remain unchanged unless those buffers
595 need to be created afresh. */
596
597 static Lisp_Object echo_buffer[2];
598
599 /* A vector saved used in with_area_buffer to reduce consing. */
600
601 static Lisp_Object Vwith_echo_area_save_vector;
602
603 /* Non-zero means display_echo_area should display the last echo area
604 message again. Set by redisplay_preserve_echo_area. */
605
606 static int display_last_displayed_message_p;
607
608 /* Nonzero if echo area is being used by print; zero if being used by
609 message. */
610
611 int message_buf_print;
612
613 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
614
615 Lisp_Object Qinhibit_menubar_update;
616 int inhibit_menubar_update;
617
618 /* Maximum height for resizing mini-windows. Either a float
619 specifying a fraction of the available height, or an integer
620 specifying a number of lines. */
621
622 Lisp_Object Vmax_mini_window_height;
623
624 /* Non-zero means messages should be displayed with truncated
625 lines instead of being continued. */
626
627 int message_truncate_lines;
628 Lisp_Object Qmessage_truncate_lines;
629
630 /* Set to 1 in clear_message to make redisplay_internal aware
631 of an emptied echo area. */
632
633 static int message_cleared_p;
634
635 /* How to blink the default frame cursor off. */
636 Lisp_Object Vblink_cursor_alist;
637
638 /* A scratch glyph row with contents used for generating truncation
639 glyphs. Also used in direct_output_for_insert. */
640
641 #define MAX_SCRATCH_GLYPHS 100
642 struct glyph_row scratch_glyph_row;
643 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
644
645 /* Ascent and height of the last line processed by move_it_to. */
646
647 static int last_max_ascent, last_height;
648
649 /* Non-zero if there's a help-echo in the echo area. */
650
651 int help_echo_showing_p;
652
653 /* If >= 0, computed, exact values of mode-line and header-line height
654 to use in the macros CURRENT_MODE_LINE_HEIGHT and
655 CURRENT_HEADER_LINE_HEIGHT. */
656
657 int current_mode_line_height, current_header_line_height;
658
659 /* The maximum distance to look ahead for text properties. Values
660 that are too small let us call compute_char_face and similar
661 functions too often which is expensive. Values that are too large
662 let us call compute_char_face and alike too often because we
663 might not be interested in text properties that far away. */
664
665 #define TEXT_PROP_DISTANCE_LIMIT 100
666
667 #if GLYPH_DEBUG
668
669 /* Variables to turn off display optimizations from Lisp. */
670
671 int inhibit_try_window_id, inhibit_try_window_reusing;
672 int inhibit_try_cursor_movement;
673
674 /* Non-zero means print traces of redisplay if compiled with
675 GLYPH_DEBUG != 0. */
676
677 int trace_redisplay_p;
678
679 #endif /* GLYPH_DEBUG */
680
681 #ifdef DEBUG_TRACE_MOVE
682 /* Non-zero means trace with TRACE_MOVE to stderr. */
683 int trace_move;
684
685 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
686 #else
687 #define TRACE_MOVE(x) (void) 0
688 #endif
689
690 /* Non-zero means automatically scroll windows horizontally to make
691 point visible. */
692
693 int automatic_hscrolling_p;
694
695 /* How close to the margin can point get before the window is scrolled
696 horizontally. */
697 EMACS_INT hscroll_margin;
698
699 /* How much to scroll horizontally when point is inside the above margin. */
700 Lisp_Object Vhscroll_step;
701
702 /* The variable `resize-mini-windows'. If nil, don't resize
703 mini-windows. If t, always resize them to fit the text they
704 display. If `grow-only', let mini-windows grow only until they
705 become empty. */
706
707 Lisp_Object Vresize_mini_windows;
708
709 /* Buffer being redisplayed -- for redisplay_window_error. */
710
711 struct buffer *displayed_buffer;
712
713 /* Value returned from text property handlers (see below). */
714
715 enum prop_handled
716 {
717 HANDLED_NORMALLY,
718 HANDLED_RECOMPUTE_PROPS,
719 HANDLED_OVERLAY_STRING_CONSUMED,
720 HANDLED_RETURN
721 };
722
723 /* A description of text properties that redisplay is interested
724 in. */
725
726 struct props
727 {
728 /* The name of the property. */
729 Lisp_Object *name;
730
731 /* A unique index for the property. */
732 enum prop_idx idx;
733
734 /* A handler function called to set up iterator IT from the property
735 at IT's current position. Value is used to steer handle_stop. */
736 enum prop_handled (*handler) P_ ((struct it *it));
737 };
738
739 static enum prop_handled handle_face_prop P_ ((struct it *));
740 static enum prop_handled handle_invisible_prop P_ ((struct it *));
741 static enum prop_handled handle_display_prop P_ ((struct it *));
742 static enum prop_handled handle_composition_prop P_ ((struct it *));
743 static enum prop_handled handle_overlay_change P_ ((struct it *));
744 static enum prop_handled handle_fontified_prop P_ ((struct it *));
745
746 /* Properties handled by iterators. */
747
748 static struct props it_props[] =
749 {
750 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
751 /* Handle `face' before `display' because some sub-properties of
752 `display' need to know the face. */
753 {&Qface, FACE_PROP_IDX, handle_face_prop},
754 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
755 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
756 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
757 {NULL, 0, NULL}
758 };
759
760 /* Value is the position described by X. If X is a marker, value is
761 the marker_position of X. Otherwise, value is X. */
762
763 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
764
765 /* Enumeration returned by some move_it_.* functions internally. */
766
767 enum move_it_result
768 {
769 /* Not used. Undefined value. */
770 MOVE_UNDEFINED,
771
772 /* Move ended at the requested buffer position or ZV. */
773 MOVE_POS_MATCH_OR_ZV,
774
775 /* Move ended at the requested X pixel position. */
776 MOVE_X_REACHED,
777
778 /* Move within a line ended at the end of a line that must be
779 continued. */
780 MOVE_LINE_CONTINUED,
781
782 /* Move within a line ended at the end of a line that would
783 be displayed truncated. */
784 MOVE_LINE_TRUNCATED,
785
786 /* Move within a line ended at a line end. */
787 MOVE_NEWLINE_OR_CR
788 };
789
790 /* This counter is used to clear the face cache every once in a while
791 in redisplay_internal. It is incremented for each redisplay.
792 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
793 cleared. */
794
795 #define CLEAR_FACE_CACHE_COUNT 500
796 static int clear_face_cache_count;
797
798 /* Similarly for the image cache. */
799
800 #ifdef HAVE_WINDOW_SYSTEM
801 #define CLEAR_IMAGE_CACHE_COUNT 101
802 static int clear_image_cache_count;
803 #endif
804
805 /* Record the previous terminal frame we displayed. */
806
807 static struct frame *previous_terminal_frame;
808
809 /* Non-zero while redisplay_internal is in progress. */
810
811 int redisplaying_p;
812
813 /* Non-zero means don't free realized faces. Bound while freeing
814 realized faces is dangerous because glyph matrices might still
815 reference them. */
816
817 int inhibit_free_realized_faces;
818 Lisp_Object Qinhibit_free_realized_faces;
819
820 /* If a string, XTread_socket generates an event to display that string.
821 (The display is done in read_char.) */
822
823 Lisp_Object help_echo_string;
824 Lisp_Object help_echo_window;
825 Lisp_Object help_echo_object;
826 int help_echo_pos;
827
828 /* Temporary variable for XTread_socket. */
829
830 Lisp_Object previous_help_echo_string;
831
832 /* Null glyph slice */
833
834 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
835
836 \f
837 /* Function prototypes. */
838
839 static void setup_for_ellipsis P_ ((struct it *, int));
840 static void mark_window_display_accurate_1 P_ ((struct window *, int));
841 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
842 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
843 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
844 static int redisplay_mode_lines P_ ((Lisp_Object, int));
845 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
846
847 #if 0
848 static int invisible_text_between_p P_ ((struct it *, int, int));
849 #endif
850
851 static void pint2str P_ ((char *, int, int));
852 static void pint2hrstr P_ ((char *, int, int));
853 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
854 struct text_pos));
855 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
856 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
857 static void store_mode_line_noprop_char P_ ((char));
858 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
859 static void x_consider_frame_title P_ ((Lisp_Object));
860 static void handle_stop P_ ((struct it *));
861 static int tool_bar_lines_needed P_ ((struct frame *, int *));
862 static int single_display_spec_intangible_p P_ ((Lisp_Object));
863 static void ensure_echo_area_buffers P_ ((void));
864 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
865 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
866 static int with_echo_area_buffer P_ ((struct window *, int,
867 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
868 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
869 static void clear_garbaged_frames P_ ((void));
870 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
871 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
872 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
873 static int display_echo_area P_ ((struct window *));
874 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
875 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
876 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
877 static int string_char_and_length P_ ((const unsigned char *, int, int *));
878 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
879 struct text_pos));
880 static int compute_window_start_on_continuation_line P_ ((struct window *));
881 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
882 static void insert_left_trunc_glyphs P_ ((struct it *));
883 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
884 Lisp_Object));
885 static void extend_face_to_end_of_line P_ ((struct it *));
886 static int append_space_for_newline P_ ((struct it *, int));
887 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
888 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
889 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
890 static int trailing_whitespace_p P_ ((int));
891 static int message_log_check_duplicate P_ ((int, int, int, int));
892 static void push_it P_ ((struct it *));
893 static void pop_it P_ ((struct it *));
894 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
895 static void select_frame_for_redisplay P_ ((Lisp_Object));
896 static void redisplay_internal P_ ((int));
897 static int echo_area_display P_ ((int));
898 static void redisplay_windows P_ ((Lisp_Object));
899 static void redisplay_window P_ ((Lisp_Object, int));
900 static Lisp_Object redisplay_window_error ();
901 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
902 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
903 static void update_menu_bar P_ ((struct frame *, int));
904 static int try_window_reusing_current_matrix P_ ((struct window *));
905 static int try_window_id P_ ((struct window *));
906 static int display_line P_ ((struct it *));
907 static int display_mode_lines P_ ((struct window *));
908 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
909 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
910 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
911 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
912 static void display_menu_bar P_ ((struct window *));
913 static int display_count_lines P_ ((int, int, int, int, int *));
914 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
915 int, int, struct it *, int, int, int, int));
916 static void compute_line_metrics P_ ((struct it *));
917 static void run_redisplay_end_trigger_hook P_ ((struct it *));
918 static int get_overlay_strings P_ ((struct it *, int));
919 static int get_overlay_strings_1 P_ ((struct it *, int, int));
920 static void next_overlay_string P_ ((struct it *));
921 static void reseat P_ ((struct it *, struct text_pos, int));
922 static void reseat_1 P_ ((struct it *, struct text_pos, int));
923 static void back_to_previous_visible_line_start P_ ((struct it *));
924 void reseat_at_previous_visible_line_start P_ ((struct it *));
925 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
926 static int next_element_from_ellipsis P_ ((struct it *));
927 static int next_element_from_display_vector P_ ((struct it *));
928 static int next_element_from_string P_ ((struct it *));
929 static int next_element_from_c_string P_ ((struct it *));
930 static int next_element_from_buffer P_ ((struct it *));
931 static int next_element_from_composition P_ ((struct it *));
932 static int next_element_from_image P_ ((struct it *));
933 static int next_element_from_stretch P_ ((struct it *));
934 static void load_overlay_strings P_ ((struct it *, int));
935 static int init_from_display_pos P_ ((struct it *, struct window *,
936 struct display_pos *));
937 static void reseat_to_string P_ ((struct it *, unsigned char *,
938 Lisp_Object, int, int, int, int));
939 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
940 int, int, int));
941 void move_it_vertically_backward P_ ((struct it *, int));
942 static void init_to_row_start P_ ((struct it *, struct window *,
943 struct glyph_row *));
944 static int init_to_row_end P_ ((struct it *, struct window *,
945 struct glyph_row *));
946 static void back_to_previous_line_start P_ ((struct it *));
947 static int forward_to_next_line_start P_ ((struct it *, int *));
948 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
949 Lisp_Object, int));
950 static struct text_pos string_pos P_ ((int, Lisp_Object));
951 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
952 static int number_of_chars P_ ((unsigned char *, int));
953 static void compute_stop_pos P_ ((struct it *));
954 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
955 Lisp_Object));
956 static int face_before_or_after_it_pos P_ ((struct it *, int));
957 static int next_overlay_change P_ ((int));
958 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
959 Lisp_Object, struct text_pos *,
960 int));
961 static int underlying_face_id P_ ((struct it *));
962 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
963 struct window *));
964
965 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
966 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
967
968 #ifdef HAVE_WINDOW_SYSTEM
969
970 static void update_tool_bar P_ ((struct frame *, int));
971 static void build_desired_tool_bar_string P_ ((struct frame *f));
972 static int redisplay_tool_bar P_ ((struct frame *));
973 static void display_tool_bar_line P_ ((struct it *, int));
974 static void notice_overwritten_cursor P_ ((struct window *,
975 enum glyph_row_area,
976 int, int, int, int));
977
978
979
980 #endif /* HAVE_WINDOW_SYSTEM */
981
982 \f
983 /***********************************************************************
984 Window display dimensions
985 ***********************************************************************/
986
987 /* Return the bottom boundary y-position for text lines in window W.
988 This is the first y position at which a line cannot start.
989 It is relative to the top of the window.
990
991 This is the height of W minus the height of a mode line, if any. */
992
993 INLINE int
994 window_text_bottom_y (w)
995 struct window *w;
996 {
997 int height = WINDOW_TOTAL_HEIGHT (w);
998
999 if (WINDOW_WANTS_MODELINE_P (w))
1000 height -= CURRENT_MODE_LINE_HEIGHT (w);
1001 return height;
1002 }
1003
1004 /* Return the pixel width of display area AREA of window W. AREA < 0
1005 means return the total width of W, not including fringes to
1006 the left and right of the window. */
1007
1008 INLINE int
1009 window_box_width (w, area)
1010 struct window *w;
1011 int area;
1012 {
1013 int cols = XFASTINT (w->total_cols);
1014 int pixels = 0;
1015
1016 if (!w->pseudo_window_p)
1017 {
1018 cols -= WINDOW_SCROLL_BAR_COLS (w);
1019
1020 if (area == TEXT_AREA)
1021 {
1022 if (INTEGERP (w->left_margin_cols))
1023 cols -= XFASTINT (w->left_margin_cols);
1024 if (INTEGERP (w->right_margin_cols))
1025 cols -= XFASTINT (w->right_margin_cols);
1026 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1027 }
1028 else if (area == LEFT_MARGIN_AREA)
1029 {
1030 cols = (INTEGERP (w->left_margin_cols)
1031 ? XFASTINT (w->left_margin_cols) : 0);
1032 pixels = 0;
1033 }
1034 else if (area == RIGHT_MARGIN_AREA)
1035 {
1036 cols = (INTEGERP (w->right_margin_cols)
1037 ? XFASTINT (w->right_margin_cols) : 0);
1038 pixels = 0;
1039 }
1040 }
1041
1042 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1043 }
1044
1045
1046 /* Return the pixel height of the display area of window W, not
1047 including mode lines of W, if any. */
1048
1049 INLINE int
1050 window_box_height (w)
1051 struct window *w;
1052 {
1053 struct frame *f = XFRAME (w->frame);
1054 int height = WINDOW_TOTAL_HEIGHT (w);
1055
1056 xassert (height >= 0);
1057
1058 /* Note: the code below that determines the mode-line/header-line
1059 height is essentially the same as that contained in the macro
1060 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1061 the appropriate glyph row has its `mode_line_p' flag set,
1062 and if it doesn't, uses estimate_mode_line_height instead. */
1063
1064 if (WINDOW_WANTS_MODELINE_P (w))
1065 {
1066 struct glyph_row *ml_row
1067 = (w->current_matrix && w->current_matrix->rows
1068 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1069 : 0);
1070 if (ml_row && ml_row->mode_line_p)
1071 height -= ml_row->height;
1072 else
1073 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1074 }
1075
1076 if (WINDOW_WANTS_HEADER_LINE_P (w))
1077 {
1078 struct glyph_row *hl_row
1079 = (w->current_matrix && w->current_matrix->rows
1080 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1081 : 0);
1082 if (hl_row && hl_row->mode_line_p)
1083 height -= hl_row->height;
1084 else
1085 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1086 }
1087
1088 /* With a very small font and a mode-line that's taller than
1089 default, we might end up with a negative height. */
1090 return max (0, height);
1091 }
1092
1093 /* Return the window-relative coordinate of the left edge of display
1094 area AREA of window W. AREA < 0 means return the left edge of the
1095 whole window, to the right of the left fringe of W. */
1096
1097 INLINE int
1098 window_box_left_offset (w, area)
1099 struct window *w;
1100 int area;
1101 {
1102 int x;
1103
1104 if (w->pseudo_window_p)
1105 return 0;
1106
1107 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1108
1109 if (area == TEXT_AREA)
1110 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1111 + window_box_width (w, LEFT_MARGIN_AREA));
1112 else if (area == RIGHT_MARGIN_AREA)
1113 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1114 + window_box_width (w, LEFT_MARGIN_AREA)
1115 + window_box_width (w, TEXT_AREA)
1116 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1117 ? 0
1118 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1119 else if (area == LEFT_MARGIN_AREA
1120 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1121 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1122
1123 return x;
1124 }
1125
1126
1127 /* Return the window-relative coordinate of the right edge of display
1128 area AREA of window W. AREA < 0 means return the left edge of the
1129 whole window, to the left of the right fringe of W. */
1130
1131 INLINE int
1132 window_box_right_offset (w, area)
1133 struct window *w;
1134 int area;
1135 {
1136 return window_box_left_offset (w, area) + window_box_width (w, area);
1137 }
1138
1139 /* Return the frame-relative coordinate of the left edge of display
1140 area AREA of window W. AREA < 0 means return the left edge of the
1141 whole window, to the right of the left fringe of W. */
1142
1143 INLINE int
1144 window_box_left (w, area)
1145 struct window *w;
1146 int area;
1147 {
1148 struct frame *f = XFRAME (w->frame);
1149 int x;
1150
1151 if (w->pseudo_window_p)
1152 return FRAME_INTERNAL_BORDER_WIDTH (f);
1153
1154 x = (WINDOW_LEFT_EDGE_X (w)
1155 + window_box_left_offset (w, area));
1156
1157 return x;
1158 }
1159
1160
1161 /* Return the frame-relative coordinate of the right edge of display
1162 area AREA of window W. AREA < 0 means return the left edge of the
1163 whole window, to the left of the right fringe of W. */
1164
1165 INLINE int
1166 window_box_right (w, area)
1167 struct window *w;
1168 int area;
1169 {
1170 return window_box_left (w, area) + window_box_width (w, area);
1171 }
1172
1173 /* Get the bounding box of the display area AREA of window W, without
1174 mode lines, in frame-relative coordinates. AREA < 0 means the
1175 whole window, not including the left and right fringes of
1176 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1177 coordinates of the upper-left corner of the box. Return in
1178 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1179
1180 INLINE void
1181 window_box (w, area, box_x, box_y, box_width, box_height)
1182 struct window *w;
1183 int area;
1184 int *box_x, *box_y, *box_width, *box_height;
1185 {
1186 if (box_width)
1187 *box_width = window_box_width (w, area);
1188 if (box_height)
1189 *box_height = window_box_height (w);
1190 if (box_x)
1191 *box_x = window_box_left (w, area);
1192 if (box_y)
1193 {
1194 *box_y = WINDOW_TOP_EDGE_Y (w);
1195 if (WINDOW_WANTS_HEADER_LINE_P (w))
1196 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1197 }
1198 }
1199
1200
1201 /* Get the bounding box of the display area AREA of window W, without
1202 mode lines. AREA < 0 means the whole window, not including the
1203 left and right fringe of the window. Return in *TOP_LEFT_X
1204 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1205 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1206 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1207 box. */
1208
1209 INLINE void
1210 window_box_edges (w, area, top_left_x, top_left_y,
1211 bottom_right_x, bottom_right_y)
1212 struct window *w;
1213 int area;
1214 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1215 {
1216 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1217 bottom_right_y);
1218 *bottom_right_x += *top_left_x;
1219 *bottom_right_y += *top_left_y;
1220 }
1221
1222
1223 \f
1224 /***********************************************************************
1225 Utilities
1226 ***********************************************************************/
1227
1228 /* Return the bottom y-position of the line the iterator IT is in.
1229 This can modify IT's settings. */
1230
1231 int
1232 line_bottom_y (it)
1233 struct it *it;
1234 {
1235 int line_height = it->max_ascent + it->max_descent;
1236 int line_top_y = it->current_y;
1237
1238 if (line_height == 0)
1239 {
1240 if (last_height)
1241 line_height = last_height;
1242 else if (IT_CHARPOS (*it) < ZV)
1243 {
1244 move_it_by_lines (it, 1, 1);
1245 line_height = (it->max_ascent || it->max_descent
1246 ? it->max_ascent + it->max_descent
1247 : last_height);
1248 }
1249 else
1250 {
1251 struct glyph_row *row = it->glyph_row;
1252
1253 /* Use the default character height. */
1254 it->glyph_row = NULL;
1255 it->what = IT_CHARACTER;
1256 it->c = ' ';
1257 it->len = 1;
1258 PRODUCE_GLYPHS (it);
1259 line_height = it->ascent + it->descent;
1260 it->glyph_row = row;
1261 }
1262 }
1263
1264 return line_top_y + line_height;
1265 }
1266
1267
1268 /* Return 1 if position CHARPOS is visible in window W.
1269 If visible, set *X and *Y to pixel coordinates of top left corner.
1270 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1271 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1272 and header-lines heights. */
1273
1274 int
1275 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1276 struct window *w;
1277 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1278 {
1279 struct it it;
1280 struct text_pos top;
1281 int visible_p = 0;
1282 struct buffer *old_buffer = NULL;
1283
1284 if (noninteractive)
1285 return visible_p;
1286
1287 if (XBUFFER (w->buffer) != current_buffer)
1288 {
1289 old_buffer = current_buffer;
1290 set_buffer_internal_1 (XBUFFER (w->buffer));
1291 }
1292
1293 SET_TEXT_POS_FROM_MARKER (top, w->start);
1294
1295 /* Compute exact mode line heights, if requested. */
1296 if (exact_mode_line_heights_p)
1297 {
1298 if (WINDOW_WANTS_MODELINE_P (w))
1299 current_mode_line_height
1300 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1301 current_buffer->mode_line_format);
1302
1303 if (WINDOW_WANTS_HEADER_LINE_P (w))
1304 current_header_line_height
1305 = display_mode_line (w, HEADER_LINE_FACE_ID,
1306 current_buffer->header_line_format);
1307 }
1308
1309 start_display (&it, w, top);
1310 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1311 MOVE_TO_POS | MOVE_TO_Y);
1312
1313 /* Note that we may overshoot because of invisible text. */
1314 if (IT_CHARPOS (it) >= charpos)
1315 {
1316 int top_x = it.current_x;
1317 int top_y = it.current_y;
1318 int bottom_y = (last_height = 0, line_bottom_y (&it));
1319 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1320
1321 if (top_y < window_top_y)
1322 visible_p = bottom_y > window_top_y;
1323 else if (top_y < it.last_visible_y)
1324 visible_p = 1;
1325 if (visible_p)
1326 {
1327 *x = top_x;
1328 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1329 *rtop = max (0, window_top_y - top_y);
1330 *rbot = max (0, bottom_y - it.last_visible_y);
1331 }
1332 }
1333 else
1334 {
1335 struct it it2;
1336
1337 it2 = it;
1338 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1339 move_it_by_lines (&it, 1, 0);
1340 if (charpos < IT_CHARPOS (it))
1341 {
1342 visible_p = 1;
1343 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1344 *x = it2.current_x;
1345 *y = it2.current_y + it2.max_ascent - it2.ascent;
1346 *rtop = max (0, -it2.current_y);
1347 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1348 - it.last_visible_y));
1349 }
1350 }
1351
1352 if (old_buffer)
1353 set_buffer_internal_1 (old_buffer);
1354
1355 current_header_line_height = current_mode_line_height = -1;
1356
1357 if (visible_p && XFASTINT (w->hscroll) > 0)
1358 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1359
1360 return visible_p;
1361 }
1362
1363
1364 /* Return the next character from STR which is MAXLEN bytes long.
1365 Return in *LEN the length of the character. This is like
1366 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1367 we find one, we return a `?', but with the length of the invalid
1368 character. */
1369
1370 static INLINE int
1371 string_char_and_length (str, maxlen, len)
1372 const unsigned char *str;
1373 int maxlen, *len;
1374 {
1375 int c;
1376
1377 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1378 if (!CHAR_VALID_P (c, 1))
1379 /* We may not change the length here because other places in Emacs
1380 don't use this function, i.e. they silently accept invalid
1381 characters. */
1382 c = '?';
1383
1384 return c;
1385 }
1386
1387
1388
1389 /* Given a position POS containing a valid character and byte position
1390 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1391
1392 static struct text_pos
1393 string_pos_nchars_ahead (pos, string, nchars)
1394 struct text_pos pos;
1395 Lisp_Object string;
1396 int nchars;
1397 {
1398 xassert (STRINGP (string) && nchars >= 0);
1399
1400 if (STRING_MULTIBYTE (string))
1401 {
1402 int rest = SBYTES (string) - BYTEPOS (pos);
1403 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1404 int len;
1405
1406 while (nchars--)
1407 {
1408 string_char_and_length (p, rest, &len);
1409 p += len, rest -= len;
1410 xassert (rest >= 0);
1411 CHARPOS (pos) += 1;
1412 BYTEPOS (pos) += len;
1413 }
1414 }
1415 else
1416 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1417
1418 return pos;
1419 }
1420
1421
1422 /* Value is the text position, i.e. character and byte position,
1423 for character position CHARPOS in STRING. */
1424
1425 static INLINE struct text_pos
1426 string_pos (charpos, string)
1427 int charpos;
1428 Lisp_Object string;
1429 {
1430 struct text_pos pos;
1431 xassert (STRINGP (string));
1432 xassert (charpos >= 0);
1433 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1434 return pos;
1435 }
1436
1437
1438 /* Value is a text position, i.e. character and byte position, for
1439 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1440 means recognize multibyte characters. */
1441
1442 static struct text_pos
1443 c_string_pos (charpos, s, multibyte_p)
1444 int charpos;
1445 unsigned char *s;
1446 int multibyte_p;
1447 {
1448 struct text_pos pos;
1449
1450 xassert (s != NULL);
1451 xassert (charpos >= 0);
1452
1453 if (multibyte_p)
1454 {
1455 int rest = strlen (s), len;
1456
1457 SET_TEXT_POS (pos, 0, 0);
1458 while (charpos--)
1459 {
1460 string_char_and_length (s, rest, &len);
1461 s += len, rest -= len;
1462 xassert (rest >= 0);
1463 CHARPOS (pos) += 1;
1464 BYTEPOS (pos) += len;
1465 }
1466 }
1467 else
1468 SET_TEXT_POS (pos, charpos, charpos);
1469
1470 return pos;
1471 }
1472
1473
1474 /* Value is the number of characters in C string S. MULTIBYTE_P
1475 non-zero means recognize multibyte characters. */
1476
1477 static int
1478 number_of_chars (s, multibyte_p)
1479 unsigned char *s;
1480 int multibyte_p;
1481 {
1482 int nchars;
1483
1484 if (multibyte_p)
1485 {
1486 int rest = strlen (s), len;
1487 unsigned char *p = (unsigned char *) s;
1488
1489 for (nchars = 0; rest > 0; ++nchars)
1490 {
1491 string_char_and_length (p, rest, &len);
1492 rest -= len, p += len;
1493 }
1494 }
1495 else
1496 nchars = strlen (s);
1497
1498 return nchars;
1499 }
1500
1501
1502 /* Compute byte position NEWPOS->bytepos corresponding to
1503 NEWPOS->charpos. POS is a known position in string STRING.
1504 NEWPOS->charpos must be >= POS.charpos. */
1505
1506 static void
1507 compute_string_pos (newpos, pos, string)
1508 struct text_pos *newpos, pos;
1509 Lisp_Object string;
1510 {
1511 xassert (STRINGP (string));
1512 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1513
1514 if (STRING_MULTIBYTE (string))
1515 *newpos = string_pos_nchars_ahead (pos, string,
1516 CHARPOS (*newpos) - CHARPOS (pos));
1517 else
1518 BYTEPOS (*newpos) = CHARPOS (*newpos);
1519 }
1520
1521 /* EXPORT:
1522 Return an estimation of the pixel height of mode or top lines on
1523 frame F. FACE_ID specifies what line's height to estimate. */
1524
1525 int
1526 estimate_mode_line_height (f, face_id)
1527 struct frame *f;
1528 enum face_id face_id;
1529 {
1530 #ifdef HAVE_WINDOW_SYSTEM
1531 if (FRAME_WINDOW_P (f))
1532 {
1533 int height = FONT_HEIGHT (FRAME_FONT (f));
1534
1535 /* This function is called so early when Emacs starts that the face
1536 cache and mode line face are not yet initialized. */
1537 if (FRAME_FACE_CACHE (f))
1538 {
1539 struct face *face = FACE_FROM_ID (f, face_id);
1540 if (face)
1541 {
1542 if (face->font)
1543 height = FONT_HEIGHT (face->font);
1544 if (face->box_line_width > 0)
1545 height += 2 * face->box_line_width;
1546 }
1547 }
1548
1549 return height;
1550 }
1551 #endif
1552
1553 return 1;
1554 }
1555
1556 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1557 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1558 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1559 not force the value into range. */
1560
1561 void
1562 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1563 FRAME_PTR f;
1564 register int pix_x, pix_y;
1565 int *x, *y;
1566 NativeRectangle *bounds;
1567 int noclip;
1568 {
1569
1570 #ifdef HAVE_WINDOW_SYSTEM
1571 if (FRAME_WINDOW_P (f))
1572 {
1573 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1574 even for negative values. */
1575 if (pix_x < 0)
1576 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1577 if (pix_y < 0)
1578 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1579
1580 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1581 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1582
1583 if (bounds)
1584 STORE_NATIVE_RECT (*bounds,
1585 FRAME_COL_TO_PIXEL_X (f, pix_x),
1586 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1587 FRAME_COLUMN_WIDTH (f) - 1,
1588 FRAME_LINE_HEIGHT (f) - 1);
1589
1590 if (!noclip)
1591 {
1592 if (pix_x < 0)
1593 pix_x = 0;
1594 else if (pix_x > FRAME_TOTAL_COLS (f))
1595 pix_x = FRAME_TOTAL_COLS (f);
1596
1597 if (pix_y < 0)
1598 pix_y = 0;
1599 else if (pix_y > FRAME_LINES (f))
1600 pix_y = FRAME_LINES (f);
1601 }
1602 }
1603 #endif
1604
1605 *x = pix_x;
1606 *y = pix_y;
1607 }
1608
1609
1610 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1611 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1612 can't tell the positions because W's display is not up to date,
1613 return 0. */
1614
1615 int
1616 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1617 struct window *w;
1618 int hpos, vpos;
1619 int *frame_x, *frame_y;
1620 {
1621 #ifdef HAVE_WINDOW_SYSTEM
1622 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1623 {
1624 int success_p;
1625
1626 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1627 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1628
1629 if (display_completed)
1630 {
1631 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1632 struct glyph *glyph = row->glyphs[TEXT_AREA];
1633 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1634
1635 hpos = row->x;
1636 vpos = row->y;
1637 while (glyph < end)
1638 {
1639 hpos += glyph->pixel_width;
1640 ++glyph;
1641 }
1642
1643 /* If first glyph is partially visible, its first visible position is still 0. */
1644 if (hpos < 0)
1645 hpos = 0;
1646
1647 success_p = 1;
1648 }
1649 else
1650 {
1651 hpos = vpos = 0;
1652 success_p = 0;
1653 }
1654
1655 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1656 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1657 return success_p;
1658 }
1659 #endif
1660
1661 *frame_x = hpos;
1662 *frame_y = vpos;
1663 return 1;
1664 }
1665
1666
1667 #ifdef HAVE_WINDOW_SYSTEM
1668
1669 /* Find the glyph under window-relative coordinates X/Y in window W.
1670 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1671 strings. Return in *HPOS and *VPOS the row and column number of
1672 the glyph found. Return in *AREA the glyph area containing X.
1673 Value is a pointer to the glyph found or null if X/Y is not on
1674 text, or we can't tell because W's current matrix is not up to
1675 date. */
1676
1677 static struct glyph *
1678 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1679 struct window *w;
1680 int x, y;
1681 int *hpos, *vpos, *dx, *dy, *area;
1682 {
1683 struct glyph *glyph, *end;
1684 struct glyph_row *row = NULL;
1685 int x0, i;
1686
1687 /* Find row containing Y. Give up if some row is not enabled. */
1688 for (i = 0; i < w->current_matrix->nrows; ++i)
1689 {
1690 row = MATRIX_ROW (w->current_matrix, i);
1691 if (!row->enabled_p)
1692 return NULL;
1693 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1694 break;
1695 }
1696
1697 *vpos = i;
1698 *hpos = 0;
1699
1700 /* Give up if Y is not in the window. */
1701 if (i == w->current_matrix->nrows)
1702 return NULL;
1703
1704 /* Get the glyph area containing X. */
1705 if (w->pseudo_window_p)
1706 {
1707 *area = TEXT_AREA;
1708 x0 = 0;
1709 }
1710 else
1711 {
1712 if (x < window_box_left_offset (w, TEXT_AREA))
1713 {
1714 *area = LEFT_MARGIN_AREA;
1715 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1716 }
1717 else if (x < window_box_right_offset (w, TEXT_AREA))
1718 {
1719 *area = TEXT_AREA;
1720 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1721 }
1722 else
1723 {
1724 *area = RIGHT_MARGIN_AREA;
1725 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1726 }
1727 }
1728
1729 /* Find glyph containing X. */
1730 glyph = row->glyphs[*area];
1731 end = glyph + row->used[*area];
1732 x -= x0;
1733 while (glyph < end && x >= glyph->pixel_width)
1734 {
1735 x -= glyph->pixel_width;
1736 ++glyph;
1737 }
1738
1739 if (glyph == end)
1740 return NULL;
1741
1742 if (dx)
1743 {
1744 *dx = x;
1745 *dy = y - (row->y + row->ascent - glyph->ascent);
1746 }
1747
1748 *hpos = glyph - row->glyphs[*area];
1749 return glyph;
1750 }
1751
1752
1753 /* EXPORT:
1754 Convert frame-relative x/y to coordinates relative to window W.
1755 Takes pseudo-windows into account. */
1756
1757 void
1758 frame_to_window_pixel_xy (w, x, y)
1759 struct window *w;
1760 int *x, *y;
1761 {
1762 if (w->pseudo_window_p)
1763 {
1764 /* A pseudo-window is always full-width, and starts at the
1765 left edge of the frame, plus a frame border. */
1766 struct frame *f = XFRAME (w->frame);
1767 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1768 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1769 }
1770 else
1771 {
1772 *x -= WINDOW_LEFT_EDGE_X (w);
1773 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1774 }
1775 }
1776
1777 /* EXPORT:
1778 Return in RECTS[] at most N clipping rectangles for glyph string S.
1779 Return the number of stored rectangles. */
1780
1781 int
1782 get_glyph_string_clip_rects (s, rects, n)
1783 struct glyph_string *s;
1784 NativeRectangle *rects;
1785 int n;
1786 {
1787 XRectangle r;
1788
1789 if (n <= 0)
1790 return 0;
1791
1792 if (s->row->full_width_p)
1793 {
1794 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1795 r.x = WINDOW_LEFT_EDGE_X (s->w);
1796 r.width = WINDOW_TOTAL_WIDTH (s->w);
1797
1798 /* Unless displaying a mode or menu bar line, which are always
1799 fully visible, clip to the visible part of the row. */
1800 if (s->w->pseudo_window_p)
1801 r.height = s->row->visible_height;
1802 else
1803 r.height = s->height;
1804 }
1805 else
1806 {
1807 /* This is a text line that may be partially visible. */
1808 r.x = window_box_left (s->w, s->area);
1809 r.width = window_box_width (s->w, s->area);
1810 r.height = s->row->visible_height;
1811 }
1812
1813 if (s->clip_head)
1814 if (r.x < s->clip_head->x)
1815 {
1816 if (r.width >= s->clip_head->x - r.x)
1817 r.width -= s->clip_head->x - r.x;
1818 else
1819 r.width = 0;
1820 r.x = s->clip_head->x;
1821 }
1822 if (s->clip_tail)
1823 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1824 {
1825 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1826 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1827 else
1828 r.width = 0;
1829 }
1830
1831 /* If S draws overlapping rows, it's sufficient to use the top and
1832 bottom of the window for clipping because this glyph string
1833 intentionally draws over other lines. */
1834 if (s->for_overlaps)
1835 {
1836 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1837 r.height = window_text_bottom_y (s->w) - r.y;
1838
1839 /* Alas, the above simple strategy does not work for the
1840 environments with anti-aliased text: if the same text is
1841 drawn onto the same place multiple times, it gets thicker.
1842 If the overlap we are processing is for the erased cursor, we
1843 take the intersection with the rectagle of the cursor. */
1844 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1845 {
1846 XRectangle rc, r_save = r;
1847
1848 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1849 rc.y = s->w->phys_cursor.y;
1850 rc.width = s->w->phys_cursor_width;
1851 rc.height = s->w->phys_cursor_height;
1852
1853 x_intersect_rectangles (&r_save, &rc, &r);
1854 }
1855 }
1856 else
1857 {
1858 /* Don't use S->y for clipping because it doesn't take partially
1859 visible lines into account. For example, it can be negative for
1860 partially visible lines at the top of a window. */
1861 if (!s->row->full_width_p
1862 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1863 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1864 else
1865 r.y = max (0, s->row->y);
1866
1867 /* If drawing a tool-bar window, draw it over the internal border
1868 at the top of the window. */
1869 if (WINDOWP (s->f->tool_bar_window)
1870 && s->w == XWINDOW (s->f->tool_bar_window))
1871 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1872 }
1873
1874 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1875
1876 /* If drawing the cursor, don't let glyph draw outside its
1877 advertised boundaries. Cleartype does this under some circumstances. */
1878 if (s->hl == DRAW_CURSOR)
1879 {
1880 struct glyph *glyph = s->first_glyph;
1881 int height, max_y;
1882
1883 if (s->x > r.x)
1884 {
1885 r.width -= s->x - r.x;
1886 r.x = s->x;
1887 }
1888 r.width = min (r.width, glyph->pixel_width);
1889
1890 /* If r.y is below window bottom, ensure that we still see a cursor. */
1891 height = min (glyph->ascent + glyph->descent,
1892 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1893 max_y = window_text_bottom_y (s->w) - height;
1894 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1895 if (s->ybase - glyph->ascent > max_y)
1896 {
1897 r.y = max_y;
1898 r.height = height;
1899 }
1900 else
1901 {
1902 /* Don't draw cursor glyph taller than our actual glyph. */
1903 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1904 if (height < r.height)
1905 {
1906 max_y = r.y + r.height;
1907 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1908 r.height = min (max_y - r.y, height);
1909 }
1910 }
1911 }
1912
1913 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1914 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1915 {
1916 #ifdef CONVERT_FROM_XRECT
1917 CONVERT_FROM_XRECT (r, *rects);
1918 #else
1919 *rects = r;
1920 #endif
1921 return 1;
1922 }
1923 else
1924 {
1925 /* If we are processing overlapping and allowed to return
1926 multiple clipping rectangles, we exclude the row of the glyph
1927 string from the clipping rectangle. This is to avoid drawing
1928 the same text on the environment with anti-aliasing. */
1929 #ifdef CONVERT_FROM_XRECT
1930 XRectangle rs[2];
1931 #else
1932 XRectangle *rs = rects;
1933 #endif
1934 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1935
1936 if (s->for_overlaps & OVERLAPS_PRED)
1937 {
1938 rs[i] = r;
1939 if (r.y + r.height > row_y)
1940 {
1941 if (r.y < row_y)
1942 rs[i].height = row_y - r.y;
1943 else
1944 rs[i].height = 0;
1945 }
1946 i++;
1947 }
1948 if (s->for_overlaps & OVERLAPS_SUCC)
1949 {
1950 rs[i] = r;
1951 if (r.y < row_y + s->row->visible_height)
1952 {
1953 if (r.y + r.height > row_y + s->row->visible_height)
1954 {
1955 rs[i].y = row_y + s->row->visible_height;
1956 rs[i].height = r.y + r.height - rs[i].y;
1957 }
1958 else
1959 rs[i].height = 0;
1960 }
1961 i++;
1962 }
1963
1964 n = i;
1965 #ifdef CONVERT_FROM_XRECT
1966 for (i = 0; i < n; i++)
1967 CONVERT_FROM_XRECT (rs[i], rects[i]);
1968 #endif
1969 return n;
1970 }
1971 }
1972
1973 /* EXPORT:
1974 Return in *NR the clipping rectangle for glyph string S. */
1975
1976 void
1977 get_glyph_string_clip_rect (s, nr)
1978 struct glyph_string *s;
1979 NativeRectangle *nr;
1980 {
1981 get_glyph_string_clip_rects (s, nr, 1);
1982 }
1983
1984
1985 /* EXPORT:
1986 Return the position and height of the phys cursor in window W.
1987 Set w->phys_cursor_width to width of phys cursor.
1988 */
1989
1990 void
1991 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
1992 struct window *w;
1993 struct glyph_row *row;
1994 struct glyph *glyph;
1995 int *xp, *yp, *heightp;
1996 {
1997 struct frame *f = XFRAME (WINDOW_FRAME (w));
1998 int x, y, wd, h, h0, y0;
1999
2000 /* Compute the width of the rectangle to draw. If on a stretch
2001 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2002 rectangle as wide as the glyph, but use a canonical character
2003 width instead. */
2004 wd = glyph->pixel_width - 1;
2005 #ifdef HAVE_NTGUI
2006 wd++; /* Why? */
2007 #endif
2008
2009 x = w->phys_cursor.x;
2010 if (x < 0)
2011 {
2012 wd += x;
2013 x = 0;
2014 }
2015
2016 if (glyph->type == STRETCH_GLYPH
2017 && !x_stretch_cursor_p)
2018 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2019 w->phys_cursor_width = wd;
2020
2021 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2022
2023 /* If y is below window bottom, ensure that we still see a cursor. */
2024 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2025
2026 h = max (h0, glyph->ascent + glyph->descent);
2027 h0 = min (h0, glyph->ascent + glyph->descent);
2028
2029 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2030 if (y < y0)
2031 {
2032 h = max (h - (y0 - y) + 1, h0);
2033 y = y0 - 1;
2034 }
2035 else
2036 {
2037 y0 = window_text_bottom_y (w) - h0;
2038 if (y > y0)
2039 {
2040 h += y - y0;
2041 y = y0;
2042 }
2043 }
2044
2045 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2046 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2047 *heightp = h;
2048 }
2049
2050 /*
2051 * Remember which glyph the mouse is over.
2052 */
2053
2054 void
2055 remember_mouse_glyph (f, gx, gy, rect)
2056 struct frame *f;
2057 int gx, gy;
2058 NativeRectangle *rect;
2059 {
2060 Lisp_Object window;
2061 struct window *w;
2062 struct glyph_row *r, *gr, *end_row;
2063 enum window_part part;
2064 enum glyph_row_area area;
2065 int x, y, width, height;
2066
2067 /* Try to determine frame pixel position and size of the glyph under
2068 frame pixel coordinates X/Y on frame F. */
2069
2070 window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0);
2071 if (NILP (window))
2072 {
2073 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2074 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2075 goto virtual_glyph;
2076 }
2077
2078 w = XWINDOW (window);
2079 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2080 height = WINDOW_FRAME_LINE_HEIGHT (w);
2081
2082 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2083 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2084
2085 if (w->pseudo_window_p)
2086 {
2087 area = TEXT_AREA;
2088 part = ON_MODE_LINE; /* Don't adjust margin. */
2089 goto text_glyph;
2090 }
2091
2092 switch (part)
2093 {
2094 case ON_LEFT_MARGIN:
2095 area = LEFT_MARGIN_AREA;
2096 goto text_glyph;
2097
2098 case ON_RIGHT_MARGIN:
2099 area = RIGHT_MARGIN_AREA;
2100 goto text_glyph;
2101
2102 case ON_HEADER_LINE:
2103 case ON_MODE_LINE:
2104 gr = (part == ON_HEADER_LINE
2105 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2106 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2107 gy = gr->y;
2108 area = TEXT_AREA;
2109 goto text_glyph_row_found;
2110
2111 case ON_TEXT:
2112 area = TEXT_AREA;
2113
2114 text_glyph:
2115 gr = 0; gy = 0;
2116 for (; r <= end_row && r->enabled_p; ++r)
2117 if (r->y + r->height > y)
2118 {
2119 gr = r; gy = r->y;
2120 break;
2121 }
2122
2123 text_glyph_row_found:
2124 if (gr && gy <= y)
2125 {
2126 struct glyph *g = gr->glyphs[area];
2127 struct glyph *end = g + gr->used[area];
2128
2129 height = gr->height;
2130 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2131 if (gx + g->pixel_width > x)
2132 break;
2133
2134 if (g < end)
2135 {
2136 if (g->type == IMAGE_GLYPH)
2137 {
2138 /* Don't remember when mouse is over image, as
2139 image may have hot-spots. */
2140 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2141 return;
2142 }
2143 width = g->pixel_width;
2144 }
2145 else
2146 {
2147 /* Use nominal char spacing at end of line. */
2148 x -= gx;
2149 gx += (x / width) * width;
2150 }
2151
2152 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2153 gx += window_box_left_offset (w, area);
2154 }
2155 else
2156 {
2157 /* Use nominal line height at end of window. */
2158 gx = (x / width) * width;
2159 y -= gy;
2160 gy += (y / height) * height;
2161 }
2162 break;
2163
2164 case ON_LEFT_FRINGE:
2165 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2166 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2167 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2168 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2169 goto row_glyph;
2170
2171 case ON_RIGHT_FRINGE:
2172 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2173 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2174 : window_box_right_offset (w, TEXT_AREA));
2175 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2176 goto row_glyph;
2177
2178 case ON_SCROLL_BAR:
2179 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2180 ? 0
2181 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2182 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2183 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2184 : 0)));
2185 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2186
2187 row_glyph:
2188 gr = 0, gy = 0;
2189 for (; r <= end_row && r->enabled_p; ++r)
2190 if (r->y + r->height > y)
2191 {
2192 gr = r; gy = r->y;
2193 break;
2194 }
2195
2196 if (gr && gy <= y)
2197 height = gr->height;
2198 else
2199 {
2200 /* Use nominal line height at end of window. */
2201 y -= gy;
2202 gy += (y / height) * height;
2203 }
2204 break;
2205
2206 default:
2207 ;
2208 virtual_glyph:
2209 /* If there is no glyph under the mouse, then we divide the screen
2210 into a grid of the smallest glyph in the frame, and use that
2211 as our "glyph". */
2212
2213 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2214 round down even for negative values. */
2215 if (gx < 0)
2216 gx -= width - 1;
2217 if (gy < 0)
2218 gy -= height - 1;
2219
2220 gx = (gx / width) * width;
2221 gy = (gy / height) * height;
2222
2223 goto store_rect;
2224 }
2225
2226 gx += WINDOW_LEFT_EDGE_X (w);
2227 gy += WINDOW_TOP_EDGE_Y (w);
2228
2229 store_rect:
2230 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2231
2232 /* Visible feedback for debugging. */
2233 #if 0
2234 #if HAVE_X_WINDOWS
2235 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2236 f->output_data.x->normal_gc,
2237 gx, gy, width, height);
2238 #endif
2239 #endif
2240 }
2241
2242
2243 #endif /* HAVE_WINDOW_SYSTEM */
2244
2245 \f
2246 /***********************************************************************
2247 Lisp form evaluation
2248 ***********************************************************************/
2249
2250 /* Error handler for safe_eval and safe_call. */
2251
2252 static Lisp_Object
2253 safe_eval_handler (arg)
2254 Lisp_Object arg;
2255 {
2256 add_to_log ("Error during redisplay: %s", arg, Qnil);
2257 return Qnil;
2258 }
2259
2260
2261 /* Evaluate SEXPR and return the result, or nil if something went
2262 wrong. Prevent redisplay during the evaluation. */
2263
2264 Lisp_Object
2265 safe_eval (sexpr)
2266 Lisp_Object sexpr;
2267 {
2268 Lisp_Object val;
2269
2270 if (inhibit_eval_during_redisplay)
2271 val = Qnil;
2272 else
2273 {
2274 int count = SPECPDL_INDEX ();
2275 struct gcpro gcpro1;
2276
2277 GCPRO1 (sexpr);
2278 specbind (Qinhibit_redisplay, Qt);
2279 /* Use Qt to ensure debugger does not run,
2280 so there is no possibility of wanting to redisplay. */
2281 val = internal_condition_case_1 (Feval, sexpr, Qt,
2282 safe_eval_handler);
2283 UNGCPRO;
2284 val = unbind_to (count, val);
2285 }
2286
2287 return val;
2288 }
2289
2290
2291 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2292 Return the result, or nil if something went wrong. Prevent
2293 redisplay during the evaluation. */
2294
2295 Lisp_Object
2296 safe_call (nargs, args)
2297 int nargs;
2298 Lisp_Object *args;
2299 {
2300 Lisp_Object val;
2301
2302 if (inhibit_eval_during_redisplay)
2303 val = Qnil;
2304 else
2305 {
2306 int count = SPECPDL_INDEX ();
2307 struct gcpro gcpro1;
2308
2309 GCPRO1 (args[0]);
2310 gcpro1.nvars = nargs;
2311 specbind (Qinhibit_redisplay, Qt);
2312 /* Use Qt to ensure debugger does not run,
2313 so there is no possibility of wanting to redisplay. */
2314 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2315 safe_eval_handler);
2316 UNGCPRO;
2317 val = unbind_to (count, val);
2318 }
2319
2320 return val;
2321 }
2322
2323
2324 /* Call function FN with one argument ARG.
2325 Return the result, or nil if something went wrong. */
2326
2327 Lisp_Object
2328 safe_call1 (fn, arg)
2329 Lisp_Object fn, arg;
2330 {
2331 Lisp_Object args[2];
2332 args[0] = fn;
2333 args[1] = arg;
2334 return safe_call (2, args);
2335 }
2336
2337
2338 \f
2339 /***********************************************************************
2340 Debugging
2341 ***********************************************************************/
2342
2343 #if 0
2344
2345 /* Define CHECK_IT to perform sanity checks on iterators.
2346 This is for debugging. It is too slow to do unconditionally. */
2347
2348 static void
2349 check_it (it)
2350 struct it *it;
2351 {
2352 if (it->method == GET_FROM_STRING)
2353 {
2354 xassert (STRINGP (it->string));
2355 xassert (IT_STRING_CHARPOS (*it) >= 0);
2356 }
2357 else
2358 {
2359 xassert (IT_STRING_CHARPOS (*it) < 0);
2360 if (it->method == GET_FROM_BUFFER)
2361 {
2362 /* Check that character and byte positions agree. */
2363 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2364 }
2365 }
2366
2367 if (it->dpvec)
2368 xassert (it->current.dpvec_index >= 0);
2369 else
2370 xassert (it->current.dpvec_index < 0);
2371 }
2372
2373 #define CHECK_IT(IT) check_it ((IT))
2374
2375 #else /* not 0 */
2376
2377 #define CHECK_IT(IT) (void) 0
2378
2379 #endif /* not 0 */
2380
2381
2382 #if GLYPH_DEBUG
2383
2384 /* Check that the window end of window W is what we expect it
2385 to be---the last row in the current matrix displaying text. */
2386
2387 static void
2388 check_window_end (w)
2389 struct window *w;
2390 {
2391 if (!MINI_WINDOW_P (w)
2392 && !NILP (w->window_end_valid))
2393 {
2394 struct glyph_row *row;
2395 xassert ((row = MATRIX_ROW (w->current_matrix,
2396 XFASTINT (w->window_end_vpos)),
2397 !row->enabled_p
2398 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2399 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2400 }
2401 }
2402
2403 #define CHECK_WINDOW_END(W) check_window_end ((W))
2404
2405 #else /* not GLYPH_DEBUG */
2406
2407 #define CHECK_WINDOW_END(W) (void) 0
2408
2409 #endif /* not GLYPH_DEBUG */
2410
2411
2412 \f
2413 /***********************************************************************
2414 Iterator initialization
2415 ***********************************************************************/
2416
2417 /* Initialize IT for displaying current_buffer in window W, starting
2418 at character position CHARPOS. CHARPOS < 0 means that no buffer
2419 position is specified which is useful when the iterator is assigned
2420 a position later. BYTEPOS is the byte position corresponding to
2421 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2422
2423 If ROW is not null, calls to produce_glyphs with IT as parameter
2424 will produce glyphs in that row.
2425
2426 BASE_FACE_ID is the id of a base face to use. It must be one of
2427 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2428 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2429 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2430
2431 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2432 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2433 will be initialized to use the corresponding mode line glyph row of
2434 the desired matrix of W. */
2435
2436 void
2437 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2438 struct it *it;
2439 struct window *w;
2440 int charpos, bytepos;
2441 struct glyph_row *row;
2442 enum face_id base_face_id;
2443 {
2444 int highlight_region_p;
2445
2446 /* Some precondition checks. */
2447 xassert (w != NULL && it != NULL);
2448 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2449 && charpos <= ZV));
2450
2451 /* If face attributes have been changed since the last redisplay,
2452 free realized faces now because they depend on face definitions
2453 that might have changed. Don't free faces while there might be
2454 desired matrices pending which reference these faces. */
2455 if (face_change_count && !inhibit_free_realized_faces)
2456 {
2457 face_change_count = 0;
2458 free_all_realized_faces (Qnil);
2459 }
2460
2461 /* Use one of the mode line rows of W's desired matrix if
2462 appropriate. */
2463 if (row == NULL)
2464 {
2465 if (base_face_id == MODE_LINE_FACE_ID
2466 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2467 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2468 else if (base_face_id == HEADER_LINE_FACE_ID)
2469 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2470 }
2471
2472 /* Clear IT. */
2473 bzero (it, sizeof *it);
2474 it->current.overlay_string_index = -1;
2475 it->current.dpvec_index = -1;
2476 it->base_face_id = base_face_id;
2477 it->string = Qnil;
2478 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2479
2480 /* The window in which we iterate over current_buffer: */
2481 XSETWINDOW (it->window, w);
2482 it->w = w;
2483 it->f = XFRAME (w->frame);
2484
2485 /* Extra space between lines (on window systems only). */
2486 if (base_face_id == DEFAULT_FACE_ID
2487 && FRAME_WINDOW_P (it->f))
2488 {
2489 if (NATNUMP (current_buffer->extra_line_spacing))
2490 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2491 else if (FLOATP (current_buffer->extra_line_spacing))
2492 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2493 * FRAME_LINE_HEIGHT (it->f));
2494 else if (it->f->extra_line_spacing > 0)
2495 it->extra_line_spacing = it->f->extra_line_spacing;
2496 it->max_extra_line_spacing = 0;
2497 }
2498
2499 /* If realized faces have been removed, e.g. because of face
2500 attribute changes of named faces, recompute them. When running
2501 in batch mode, the face cache of Vterminal_frame is null. If
2502 we happen to get called, make a dummy face cache. */
2503 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2504 init_frame_faces (it->f);
2505 if (FRAME_FACE_CACHE (it->f)->used == 0)
2506 recompute_basic_faces (it->f);
2507
2508 /* Current value of the `slice', `space-width', and 'height' properties. */
2509 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2510 it->space_width = Qnil;
2511 it->font_height = Qnil;
2512 it->override_ascent = -1;
2513
2514 /* Are control characters displayed as `^C'? */
2515 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2516
2517 /* -1 means everything between a CR and the following line end
2518 is invisible. >0 means lines indented more than this value are
2519 invisible. */
2520 it->selective = (INTEGERP (current_buffer->selective_display)
2521 ? XFASTINT (current_buffer->selective_display)
2522 : (!NILP (current_buffer->selective_display)
2523 ? -1 : 0));
2524 it->selective_display_ellipsis_p
2525 = !NILP (current_buffer->selective_display_ellipses);
2526
2527 /* Display table to use. */
2528 it->dp = window_display_table (w);
2529
2530 /* Are multibyte characters enabled in current_buffer? */
2531 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2532
2533 /* Non-zero if we should highlight the region. */
2534 highlight_region_p
2535 = (!NILP (Vtransient_mark_mode)
2536 && !NILP (current_buffer->mark_active)
2537 && XMARKER (current_buffer->mark)->buffer != 0);
2538
2539 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2540 start and end of a visible region in window IT->w. Set both to
2541 -1 to indicate no region. */
2542 if (highlight_region_p
2543 /* Maybe highlight only in selected window. */
2544 && (/* Either show region everywhere. */
2545 highlight_nonselected_windows
2546 /* Or show region in the selected window. */
2547 || w == XWINDOW (selected_window)
2548 /* Or show the region if we are in the mini-buffer and W is
2549 the window the mini-buffer refers to. */
2550 || (MINI_WINDOW_P (XWINDOW (selected_window))
2551 && WINDOWP (minibuf_selected_window)
2552 && w == XWINDOW (minibuf_selected_window))))
2553 {
2554 int charpos = marker_position (current_buffer->mark);
2555 it->region_beg_charpos = min (PT, charpos);
2556 it->region_end_charpos = max (PT, charpos);
2557 }
2558 else
2559 it->region_beg_charpos = it->region_end_charpos = -1;
2560
2561 /* Get the position at which the redisplay_end_trigger hook should
2562 be run, if it is to be run at all. */
2563 if (MARKERP (w->redisplay_end_trigger)
2564 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2565 it->redisplay_end_trigger_charpos
2566 = marker_position (w->redisplay_end_trigger);
2567 else if (INTEGERP (w->redisplay_end_trigger))
2568 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2569
2570 /* Correct bogus values of tab_width. */
2571 it->tab_width = XINT (current_buffer->tab_width);
2572 if (it->tab_width <= 0 || it->tab_width > 1000)
2573 it->tab_width = 8;
2574
2575 /* Are lines in the display truncated? */
2576 it->truncate_lines_p
2577 = (base_face_id != DEFAULT_FACE_ID
2578 || XINT (it->w->hscroll)
2579 || (truncate_partial_width_windows
2580 && !WINDOW_FULL_WIDTH_P (it->w))
2581 || !NILP (current_buffer->truncate_lines));
2582
2583 /* Get dimensions of truncation and continuation glyphs. These are
2584 displayed as fringe bitmaps under X, so we don't need them for such
2585 frames. */
2586 if (!FRAME_WINDOW_P (it->f))
2587 {
2588 if (it->truncate_lines_p)
2589 {
2590 /* We will need the truncation glyph. */
2591 xassert (it->glyph_row == NULL);
2592 produce_special_glyphs (it, IT_TRUNCATION);
2593 it->truncation_pixel_width = it->pixel_width;
2594 }
2595 else
2596 {
2597 /* We will need the continuation glyph. */
2598 xassert (it->glyph_row == NULL);
2599 produce_special_glyphs (it, IT_CONTINUATION);
2600 it->continuation_pixel_width = it->pixel_width;
2601 }
2602
2603 /* Reset these values to zero because the produce_special_glyphs
2604 above has changed them. */
2605 it->pixel_width = it->ascent = it->descent = 0;
2606 it->phys_ascent = it->phys_descent = 0;
2607 }
2608
2609 /* Set this after getting the dimensions of truncation and
2610 continuation glyphs, so that we don't produce glyphs when calling
2611 produce_special_glyphs, above. */
2612 it->glyph_row = row;
2613 it->area = TEXT_AREA;
2614
2615 /* Get the dimensions of the display area. The display area
2616 consists of the visible window area plus a horizontally scrolled
2617 part to the left of the window. All x-values are relative to the
2618 start of this total display area. */
2619 if (base_face_id != DEFAULT_FACE_ID)
2620 {
2621 /* Mode lines, menu bar in terminal frames. */
2622 it->first_visible_x = 0;
2623 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2624 }
2625 else
2626 {
2627 it->first_visible_x
2628 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2629 it->last_visible_x = (it->first_visible_x
2630 + window_box_width (w, TEXT_AREA));
2631
2632 /* If we truncate lines, leave room for the truncator glyph(s) at
2633 the right margin. Otherwise, leave room for the continuation
2634 glyph(s). Truncation and continuation glyphs are not inserted
2635 for window-based redisplay. */
2636 if (!FRAME_WINDOW_P (it->f))
2637 {
2638 if (it->truncate_lines_p)
2639 it->last_visible_x -= it->truncation_pixel_width;
2640 else
2641 it->last_visible_x -= it->continuation_pixel_width;
2642 }
2643
2644 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2645 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2646 }
2647
2648 /* Leave room for a border glyph. */
2649 if (!FRAME_WINDOW_P (it->f)
2650 && !WINDOW_RIGHTMOST_P (it->w))
2651 it->last_visible_x -= 1;
2652
2653 it->last_visible_y = window_text_bottom_y (w);
2654
2655 /* For mode lines and alike, arrange for the first glyph having a
2656 left box line if the face specifies a box. */
2657 if (base_face_id != DEFAULT_FACE_ID)
2658 {
2659 struct face *face;
2660
2661 it->face_id = base_face_id;
2662
2663 /* If we have a boxed mode line, make the first character appear
2664 with a left box line. */
2665 face = FACE_FROM_ID (it->f, base_face_id);
2666 if (face->box != FACE_NO_BOX)
2667 it->start_of_box_run_p = 1;
2668 }
2669
2670 /* If a buffer position was specified, set the iterator there,
2671 getting overlays and face properties from that position. */
2672 if (charpos >= BUF_BEG (current_buffer))
2673 {
2674 it->end_charpos = ZV;
2675 it->face_id = -1;
2676 IT_CHARPOS (*it) = charpos;
2677
2678 /* Compute byte position if not specified. */
2679 if (bytepos < charpos)
2680 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2681 else
2682 IT_BYTEPOS (*it) = bytepos;
2683
2684 it->start = it->current;
2685
2686 /* Compute faces etc. */
2687 reseat (it, it->current.pos, 1);
2688 }
2689
2690 CHECK_IT (it);
2691 }
2692
2693
2694 /* Initialize IT for the display of window W with window start POS. */
2695
2696 void
2697 start_display (it, w, pos)
2698 struct it *it;
2699 struct window *w;
2700 struct text_pos pos;
2701 {
2702 struct glyph_row *row;
2703 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2704
2705 row = w->desired_matrix->rows + first_vpos;
2706 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2707 it->first_vpos = first_vpos;
2708
2709 /* Don't reseat to previous visible line start if current start
2710 position is in a string or image. */
2711 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2712 {
2713 int start_at_line_beg_p;
2714 int first_y = it->current_y;
2715
2716 /* If window start is not at a line start, skip forward to POS to
2717 get the correct continuation lines width. */
2718 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2719 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2720 if (!start_at_line_beg_p)
2721 {
2722 int new_x;
2723
2724 reseat_at_previous_visible_line_start (it);
2725 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2726
2727 new_x = it->current_x + it->pixel_width;
2728
2729 /* If lines are continued, this line may end in the middle
2730 of a multi-glyph character (e.g. a control character
2731 displayed as \003, or in the middle of an overlay
2732 string). In this case move_it_to above will not have
2733 taken us to the start of the continuation line but to the
2734 end of the continued line. */
2735 if (it->current_x > 0
2736 && !it->truncate_lines_p /* Lines are continued. */
2737 && (/* And glyph doesn't fit on the line. */
2738 new_x > it->last_visible_x
2739 /* Or it fits exactly and we're on a window
2740 system frame. */
2741 || (new_x == it->last_visible_x
2742 && FRAME_WINDOW_P (it->f))))
2743 {
2744 if (it->current.dpvec_index >= 0
2745 || it->current.overlay_string_index >= 0)
2746 {
2747 set_iterator_to_next (it, 1);
2748 move_it_in_display_line_to (it, -1, -1, 0);
2749 }
2750
2751 it->continuation_lines_width += it->current_x;
2752 }
2753
2754 /* We're starting a new display line, not affected by the
2755 height of the continued line, so clear the appropriate
2756 fields in the iterator structure. */
2757 it->max_ascent = it->max_descent = 0;
2758 it->max_phys_ascent = it->max_phys_descent = 0;
2759
2760 it->current_y = first_y;
2761 it->vpos = 0;
2762 it->current_x = it->hpos = 0;
2763 }
2764 }
2765
2766 #if 0 /* Don't assert the following because start_display is sometimes
2767 called intentionally with a window start that is not at a
2768 line start. Please leave this code in as a comment. */
2769
2770 /* Window start should be on a line start, now. */
2771 xassert (it->continuation_lines_width
2772 || IT_CHARPOS (it) == BEGV
2773 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2774 #endif /* 0 */
2775 }
2776
2777
2778 /* Return 1 if POS is a position in ellipses displayed for invisible
2779 text. W is the window we display, for text property lookup. */
2780
2781 static int
2782 in_ellipses_for_invisible_text_p (pos, w)
2783 struct display_pos *pos;
2784 struct window *w;
2785 {
2786 Lisp_Object prop, window;
2787 int ellipses_p = 0;
2788 int charpos = CHARPOS (pos->pos);
2789
2790 /* If POS specifies a position in a display vector, this might
2791 be for an ellipsis displayed for invisible text. We won't
2792 get the iterator set up for delivering that ellipsis unless
2793 we make sure that it gets aware of the invisible text. */
2794 if (pos->dpvec_index >= 0
2795 && pos->overlay_string_index < 0
2796 && CHARPOS (pos->string_pos) < 0
2797 && charpos > BEGV
2798 && (XSETWINDOW (window, w),
2799 prop = Fget_char_property (make_number (charpos),
2800 Qinvisible, window),
2801 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2802 {
2803 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2804 window);
2805 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2806 }
2807
2808 return ellipses_p;
2809 }
2810
2811
2812 /* Initialize IT for stepping through current_buffer in window W,
2813 starting at position POS that includes overlay string and display
2814 vector/ control character translation position information. Value
2815 is zero if there are overlay strings with newlines at POS. */
2816
2817 static int
2818 init_from_display_pos (it, w, pos)
2819 struct it *it;
2820 struct window *w;
2821 struct display_pos *pos;
2822 {
2823 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2824 int i, overlay_strings_with_newlines = 0;
2825
2826 /* If POS specifies a position in a display vector, this might
2827 be for an ellipsis displayed for invisible text. We won't
2828 get the iterator set up for delivering that ellipsis unless
2829 we make sure that it gets aware of the invisible text. */
2830 if (in_ellipses_for_invisible_text_p (pos, w))
2831 {
2832 --charpos;
2833 bytepos = 0;
2834 }
2835
2836 /* Keep in mind: the call to reseat in init_iterator skips invisible
2837 text, so we might end up at a position different from POS. This
2838 is only a problem when POS is a row start after a newline and an
2839 overlay starts there with an after-string, and the overlay has an
2840 invisible property. Since we don't skip invisible text in
2841 display_line and elsewhere immediately after consuming the
2842 newline before the row start, such a POS will not be in a string,
2843 but the call to init_iterator below will move us to the
2844 after-string. */
2845 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2846
2847 /* This only scans the current chunk -- it should scan all chunks.
2848 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2849 to 16 in 22.1 to make this a lesser problem. */
2850 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2851 {
2852 const char *s = SDATA (it->overlay_strings[i]);
2853 const char *e = s + SBYTES (it->overlay_strings[i]);
2854
2855 while (s < e && *s != '\n')
2856 ++s;
2857
2858 if (s < e)
2859 {
2860 overlay_strings_with_newlines = 1;
2861 break;
2862 }
2863 }
2864
2865 /* If position is within an overlay string, set up IT to the right
2866 overlay string. */
2867 if (pos->overlay_string_index >= 0)
2868 {
2869 int relative_index;
2870
2871 /* If the first overlay string happens to have a `display'
2872 property for an image, the iterator will be set up for that
2873 image, and we have to undo that setup first before we can
2874 correct the overlay string index. */
2875 if (it->method == GET_FROM_IMAGE)
2876 pop_it (it);
2877
2878 /* We already have the first chunk of overlay strings in
2879 IT->overlay_strings. Load more until the one for
2880 pos->overlay_string_index is in IT->overlay_strings. */
2881 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2882 {
2883 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2884 it->current.overlay_string_index = 0;
2885 while (n--)
2886 {
2887 load_overlay_strings (it, 0);
2888 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2889 }
2890 }
2891
2892 it->current.overlay_string_index = pos->overlay_string_index;
2893 relative_index = (it->current.overlay_string_index
2894 % OVERLAY_STRING_CHUNK_SIZE);
2895 it->string = it->overlay_strings[relative_index];
2896 xassert (STRINGP (it->string));
2897 it->current.string_pos = pos->string_pos;
2898 it->method = GET_FROM_STRING;
2899 }
2900
2901 #if 0 /* This is bogus because POS not having an overlay string
2902 position does not mean it's after the string. Example: A
2903 line starting with a before-string and initialization of IT
2904 to the previous row's end position. */
2905 else if (it->current.overlay_string_index >= 0)
2906 {
2907 /* If POS says we're already after an overlay string ending at
2908 POS, make sure to pop the iterator because it will be in
2909 front of that overlay string. When POS is ZV, we've thereby
2910 also ``processed'' overlay strings at ZV. */
2911 while (it->sp)
2912 pop_it (it);
2913 xassert (it->current.overlay_string_index == -1);
2914 xassert (it->method == GET_FROM_BUFFER);
2915 if (CHARPOS (pos->pos) == ZV)
2916 it->overlay_strings_at_end_processed_p = 1;
2917 }
2918 #endif /* 0 */
2919
2920 if (CHARPOS (pos->string_pos) >= 0)
2921 {
2922 /* Recorded position is not in an overlay string, but in another
2923 string. This can only be a string from a `display' property.
2924 IT should already be filled with that string. */
2925 it->current.string_pos = pos->string_pos;
2926 xassert (STRINGP (it->string));
2927 }
2928
2929 /* Restore position in display vector translations, control
2930 character translations or ellipses. */
2931 if (pos->dpvec_index >= 0)
2932 {
2933 if (it->dpvec == NULL)
2934 get_next_display_element (it);
2935 xassert (it->dpvec && it->current.dpvec_index == 0);
2936 it->current.dpvec_index = pos->dpvec_index;
2937 }
2938
2939 CHECK_IT (it);
2940 return !overlay_strings_with_newlines;
2941 }
2942
2943
2944 /* Initialize IT for stepping through current_buffer in window W
2945 starting at ROW->start. */
2946
2947 static void
2948 init_to_row_start (it, w, row)
2949 struct it *it;
2950 struct window *w;
2951 struct glyph_row *row;
2952 {
2953 init_from_display_pos (it, w, &row->start);
2954 it->start = row->start;
2955 it->continuation_lines_width = row->continuation_lines_width;
2956 CHECK_IT (it);
2957 }
2958
2959
2960 /* Initialize IT for stepping through current_buffer in window W
2961 starting in the line following ROW, i.e. starting at ROW->end.
2962 Value is zero if there are overlay strings with newlines at ROW's
2963 end position. */
2964
2965 static int
2966 init_to_row_end (it, w, row)
2967 struct it *it;
2968 struct window *w;
2969 struct glyph_row *row;
2970 {
2971 int success = 0;
2972
2973 if (init_from_display_pos (it, w, &row->end))
2974 {
2975 if (row->continued_p)
2976 it->continuation_lines_width
2977 = row->continuation_lines_width + row->pixel_width;
2978 CHECK_IT (it);
2979 success = 1;
2980 }
2981
2982 return success;
2983 }
2984
2985
2986
2987 \f
2988 /***********************************************************************
2989 Text properties
2990 ***********************************************************************/
2991
2992 /* Called when IT reaches IT->stop_charpos. Handle text property and
2993 overlay changes. Set IT->stop_charpos to the next position where
2994 to stop. */
2995
2996 static void
2997 handle_stop (it)
2998 struct it *it;
2999 {
3000 enum prop_handled handled;
3001 int handle_overlay_change_p;
3002 struct props *p;
3003
3004 it->dpvec = NULL;
3005 it->current.dpvec_index = -1;
3006 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3007 it->ignore_overlay_strings_at_pos_p = 0;
3008
3009 /* Use face of preceding text for ellipsis (if invisible) */
3010 if (it->selective_display_ellipsis_p)
3011 it->saved_face_id = it->face_id;
3012
3013 do
3014 {
3015 handled = HANDLED_NORMALLY;
3016
3017 /* Call text property handlers. */
3018 for (p = it_props; p->handler; ++p)
3019 {
3020 handled = p->handler (it);
3021
3022 if (handled == HANDLED_RECOMPUTE_PROPS)
3023 break;
3024 else if (handled == HANDLED_RETURN)
3025 {
3026 /* We still want to show before and after strings from
3027 overlays even if the actual buffer text is replaced. */
3028 if (!handle_overlay_change_p || it->sp > 1)
3029 return;
3030 if (!get_overlay_strings_1 (it, 0, 0))
3031 return;
3032 it->string_from_display_prop_p = 0;
3033 handle_overlay_change_p = 0;
3034 handled = HANDLED_RECOMPUTE_PROPS;
3035 break;
3036 }
3037 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3038 handle_overlay_change_p = 0;
3039 }
3040
3041 if (handled != HANDLED_RECOMPUTE_PROPS)
3042 {
3043 /* Don't check for overlay strings below when set to deliver
3044 characters from a display vector. */
3045 if (it->method == GET_FROM_DISPLAY_VECTOR)
3046 handle_overlay_change_p = 0;
3047
3048 /* Handle overlay changes. */
3049 if (handle_overlay_change_p)
3050 handled = handle_overlay_change (it);
3051
3052 /* Determine where to stop next. */
3053 if (handled == HANDLED_NORMALLY)
3054 compute_stop_pos (it);
3055 }
3056 }
3057 while (handled == HANDLED_RECOMPUTE_PROPS);
3058 }
3059
3060
3061 /* Compute IT->stop_charpos from text property and overlay change
3062 information for IT's current position. */
3063
3064 static void
3065 compute_stop_pos (it)
3066 struct it *it;
3067 {
3068 register INTERVAL iv, next_iv;
3069 Lisp_Object object, limit, position;
3070
3071 /* If nowhere else, stop at the end. */
3072 it->stop_charpos = it->end_charpos;
3073
3074 if (STRINGP (it->string))
3075 {
3076 /* Strings are usually short, so don't limit the search for
3077 properties. */
3078 object = it->string;
3079 limit = Qnil;
3080 position = make_number (IT_STRING_CHARPOS (*it));
3081 }
3082 else
3083 {
3084 int charpos;
3085
3086 /* If next overlay change is in front of the current stop pos
3087 (which is IT->end_charpos), stop there. Note: value of
3088 next_overlay_change is point-max if no overlay change
3089 follows. */
3090 charpos = next_overlay_change (IT_CHARPOS (*it));
3091 if (charpos < it->stop_charpos)
3092 it->stop_charpos = charpos;
3093
3094 /* If showing the region, we have to stop at the region
3095 start or end because the face might change there. */
3096 if (it->region_beg_charpos > 0)
3097 {
3098 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3099 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3100 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3101 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3102 }
3103
3104 /* Set up variables for computing the stop position from text
3105 property changes. */
3106 XSETBUFFER (object, current_buffer);
3107 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3108 position = make_number (IT_CHARPOS (*it));
3109
3110 }
3111
3112 /* Get the interval containing IT's position. Value is a null
3113 interval if there isn't such an interval. */
3114 iv = validate_interval_range (object, &position, &position, 0);
3115 if (!NULL_INTERVAL_P (iv))
3116 {
3117 Lisp_Object values_here[LAST_PROP_IDX];
3118 struct props *p;
3119
3120 /* Get properties here. */
3121 for (p = it_props; p->handler; ++p)
3122 values_here[p->idx] = textget (iv->plist, *p->name);
3123
3124 /* Look for an interval following iv that has different
3125 properties. */
3126 for (next_iv = next_interval (iv);
3127 (!NULL_INTERVAL_P (next_iv)
3128 && (NILP (limit)
3129 || XFASTINT (limit) > next_iv->position));
3130 next_iv = next_interval (next_iv))
3131 {
3132 for (p = it_props; p->handler; ++p)
3133 {
3134 Lisp_Object new_value;
3135
3136 new_value = textget (next_iv->plist, *p->name);
3137 if (!EQ (values_here[p->idx], new_value))
3138 break;
3139 }
3140
3141 if (p->handler)
3142 break;
3143 }
3144
3145 if (!NULL_INTERVAL_P (next_iv))
3146 {
3147 if (INTEGERP (limit)
3148 && next_iv->position >= XFASTINT (limit))
3149 /* No text property change up to limit. */
3150 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3151 else
3152 /* Text properties change in next_iv. */
3153 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3154 }
3155 }
3156
3157 xassert (STRINGP (it->string)
3158 || (it->stop_charpos >= BEGV
3159 && it->stop_charpos >= IT_CHARPOS (*it)));
3160 }
3161
3162
3163 /* Return the position of the next overlay change after POS in
3164 current_buffer. Value is point-max if no overlay change
3165 follows. This is like `next-overlay-change' but doesn't use
3166 xmalloc. */
3167
3168 static int
3169 next_overlay_change (pos)
3170 int pos;
3171 {
3172 int noverlays;
3173 int endpos;
3174 Lisp_Object *overlays;
3175 int i;
3176
3177 /* Get all overlays at the given position. */
3178 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3179
3180 /* If any of these overlays ends before endpos,
3181 use its ending point instead. */
3182 for (i = 0; i < noverlays; ++i)
3183 {
3184 Lisp_Object oend;
3185 int oendpos;
3186
3187 oend = OVERLAY_END (overlays[i]);
3188 oendpos = OVERLAY_POSITION (oend);
3189 endpos = min (endpos, oendpos);
3190 }
3191
3192 return endpos;
3193 }
3194
3195
3196 \f
3197 /***********************************************************************
3198 Fontification
3199 ***********************************************************************/
3200
3201 /* Handle changes in the `fontified' property of the current buffer by
3202 calling hook functions from Qfontification_functions to fontify
3203 regions of text. */
3204
3205 static enum prop_handled
3206 handle_fontified_prop (it)
3207 struct it *it;
3208 {
3209 Lisp_Object prop, pos;
3210 enum prop_handled handled = HANDLED_NORMALLY;
3211
3212 if (!NILP (Vmemory_full))
3213 return handled;
3214
3215 /* Get the value of the `fontified' property at IT's current buffer
3216 position. (The `fontified' property doesn't have a special
3217 meaning in strings.) If the value is nil, call functions from
3218 Qfontification_functions. */
3219 if (!STRINGP (it->string)
3220 && it->s == NULL
3221 && !NILP (Vfontification_functions)
3222 && !NILP (Vrun_hooks)
3223 && (pos = make_number (IT_CHARPOS (*it)),
3224 prop = Fget_char_property (pos, Qfontified, Qnil),
3225 NILP (prop)))
3226 {
3227 int count = SPECPDL_INDEX ();
3228 Lisp_Object val;
3229
3230 val = Vfontification_functions;
3231 specbind (Qfontification_functions, Qnil);
3232
3233 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3234 safe_call1 (val, pos);
3235 else
3236 {
3237 Lisp_Object globals, fn;
3238 struct gcpro gcpro1, gcpro2;
3239
3240 globals = Qnil;
3241 GCPRO2 (val, globals);
3242
3243 for (; CONSP (val); val = XCDR (val))
3244 {
3245 fn = XCAR (val);
3246
3247 if (EQ (fn, Qt))
3248 {
3249 /* A value of t indicates this hook has a local
3250 binding; it means to run the global binding too.
3251 In a global value, t should not occur. If it
3252 does, we must ignore it to avoid an endless
3253 loop. */
3254 for (globals = Fdefault_value (Qfontification_functions);
3255 CONSP (globals);
3256 globals = XCDR (globals))
3257 {
3258 fn = XCAR (globals);
3259 if (!EQ (fn, Qt))
3260 safe_call1 (fn, pos);
3261 }
3262 }
3263 else
3264 safe_call1 (fn, pos);
3265 }
3266
3267 UNGCPRO;
3268 }
3269
3270 unbind_to (count, Qnil);
3271
3272 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3273 something. This avoids an endless loop if they failed to
3274 fontify the text for which reason ever. */
3275 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3276 handled = HANDLED_RECOMPUTE_PROPS;
3277 }
3278
3279 return handled;
3280 }
3281
3282
3283 \f
3284 /***********************************************************************
3285 Faces
3286 ***********************************************************************/
3287
3288 /* Set up iterator IT from face properties at its current position.
3289 Called from handle_stop. */
3290
3291 static enum prop_handled
3292 handle_face_prop (it)
3293 struct it *it;
3294 {
3295 int new_face_id, next_stop;
3296
3297 if (!STRINGP (it->string))
3298 {
3299 new_face_id
3300 = face_at_buffer_position (it->w,
3301 IT_CHARPOS (*it),
3302 it->region_beg_charpos,
3303 it->region_end_charpos,
3304 &next_stop,
3305 (IT_CHARPOS (*it)
3306 + TEXT_PROP_DISTANCE_LIMIT),
3307 0);
3308
3309 /* Is this a start of a run of characters with box face?
3310 Caveat: this can be called for a freshly initialized
3311 iterator; face_id is -1 in this case. We know that the new
3312 face will not change until limit, i.e. if the new face has a
3313 box, all characters up to limit will have one. But, as
3314 usual, we don't know whether limit is really the end. */
3315 if (new_face_id != it->face_id)
3316 {
3317 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3318
3319 /* If new face has a box but old face has not, this is
3320 the start of a run of characters with box, i.e. it has
3321 a shadow on the left side. The value of face_id of the
3322 iterator will be -1 if this is the initial call that gets
3323 the face. In this case, we have to look in front of IT's
3324 position and see whether there is a face != new_face_id. */
3325 it->start_of_box_run_p
3326 = (new_face->box != FACE_NO_BOX
3327 && (it->face_id >= 0
3328 || IT_CHARPOS (*it) == BEG
3329 || new_face_id != face_before_it_pos (it)));
3330 it->face_box_p = new_face->box != FACE_NO_BOX;
3331 }
3332 }
3333 else
3334 {
3335 int base_face_id, bufpos;
3336
3337 if (it->current.overlay_string_index >= 0)
3338 bufpos = IT_CHARPOS (*it);
3339 else
3340 bufpos = 0;
3341
3342 /* For strings from a buffer, i.e. overlay strings or strings
3343 from a `display' property, use the face at IT's current
3344 buffer position as the base face to merge with, so that
3345 overlay strings appear in the same face as surrounding
3346 text, unless they specify their own faces. */
3347 base_face_id = underlying_face_id (it);
3348
3349 new_face_id = face_at_string_position (it->w,
3350 it->string,
3351 IT_STRING_CHARPOS (*it),
3352 bufpos,
3353 it->region_beg_charpos,
3354 it->region_end_charpos,
3355 &next_stop,
3356 base_face_id, 0);
3357
3358 #if 0 /* This shouldn't be neccessary. Let's check it. */
3359 /* If IT is used to display a mode line we would really like to
3360 use the mode line face instead of the frame's default face. */
3361 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3362 && new_face_id == DEFAULT_FACE_ID)
3363 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3364 #endif
3365
3366 /* Is this a start of a run of characters with box? Caveat:
3367 this can be called for a freshly allocated iterator; face_id
3368 is -1 is this case. We know that the new face will not
3369 change until the next check pos, i.e. if the new face has a
3370 box, all characters up to that position will have a
3371 box. But, as usual, we don't know whether that position
3372 is really the end. */
3373 if (new_face_id != it->face_id)
3374 {
3375 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3376 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3377
3378 /* If new face has a box but old face hasn't, this is the
3379 start of a run of characters with box, i.e. it has a
3380 shadow on the left side. */
3381 it->start_of_box_run_p
3382 = new_face->box && (old_face == NULL || !old_face->box);
3383 it->face_box_p = new_face->box != FACE_NO_BOX;
3384 }
3385 }
3386
3387 it->face_id = new_face_id;
3388 return HANDLED_NORMALLY;
3389 }
3390
3391
3392 /* Return the ID of the face ``underlying'' IT's current position,
3393 which is in a string. If the iterator is associated with a
3394 buffer, return the face at IT's current buffer position.
3395 Otherwise, use the iterator's base_face_id. */
3396
3397 static int
3398 underlying_face_id (it)
3399 struct it *it;
3400 {
3401 int face_id = it->base_face_id, i;
3402
3403 xassert (STRINGP (it->string));
3404
3405 for (i = it->sp - 1; i >= 0; --i)
3406 if (NILP (it->stack[i].string))
3407 face_id = it->stack[i].face_id;
3408
3409 return face_id;
3410 }
3411
3412
3413 /* Compute the face one character before or after the current position
3414 of IT. BEFORE_P non-zero means get the face in front of IT's
3415 position. Value is the id of the face. */
3416
3417 static int
3418 face_before_or_after_it_pos (it, before_p)
3419 struct it *it;
3420 int before_p;
3421 {
3422 int face_id, limit;
3423 int next_check_charpos;
3424 struct text_pos pos;
3425
3426 xassert (it->s == NULL);
3427
3428 if (STRINGP (it->string))
3429 {
3430 int bufpos, base_face_id;
3431
3432 /* No face change past the end of the string (for the case
3433 we are padding with spaces). No face change before the
3434 string start. */
3435 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3436 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3437 return it->face_id;
3438
3439 /* Set pos to the position before or after IT's current position. */
3440 if (before_p)
3441 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3442 else
3443 /* For composition, we must check the character after the
3444 composition. */
3445 pos = (it->what == IT_COMPOSITION
3446 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3447 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3448
3449 if (it->current.overlay_string_index >= 0)
3450 bufpos = IT_CHARPOS (*it);
3451 else
3452 bufpos = 0;
3453
3454 base_face_id = underlying_face_id (it);
3455
3456 /* Get the face for ASCII, or unibyte. */
3457 face_id = face_at_string_position (it->w,
3458 it->string,
3459 CHARPOS (pos),
3460 bufpos,
3461 it->region_beg_charpos,
3462 it->region_end_charpos,
3463 &next_check_charpos,
3464 base_face_id, 0);
3465
3466 /* Correct the face for charsets different from ASCII. Do it
3467 for the multibyte case only. The face returned above is
3468 suitable for unibyte text if IT->string is unibyte. */
3469 if (STRING_MULTIBYTE (it->string))
3470 {
3471 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3472 int rest = SBYTES (it->string) - BYTEPOS (pos);
3473 int c, len;
3474 struct face *face = FACE_FROM_ID (it->f, face_id);
3475
3476 c = string_char_and_length (p, rest, &len);
3477 face_id = FACE_FOR_CHAR (it->f, face, c);
3478 }
3479 }
3480 else
3481 {
3482 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3483 || (IT_CHARPOS (*it) <= BEGV && before_p))
3484 return it->face_id;
3485
3486 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3487 pos = it->current.pos;
3488
3489 if (before_p)
3490 DEC_TEXT_POS (pos, it->multibyte_p);
3491 else
3492 {
3493 if (it->what == IT_COMPOSITION)
3494 /* For composition, we must check the position after the
3495 composition. */
3496 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3497 else
3498 INC_TEXT_POS (pos, it->multibyte_p);
3499 }
3500
3501 /* Determine face for CHARSET_ASCII, or unibyte. */
3502 face_id = face_at_buffer_position (it->w,
3503 CHARPOS (pos),
3504 it->region_beg_charpos,
3505 it->region_end_charpos,
3506 &next_check_charpos,
3507 limit, 0);
3508
3509 /* Correct the face for charsets different from ASCII. Do it
3510 for the multibyte case only. The face returned above is
3511 suitable for unibyte text if current_buffer is unibyte. */
3512 if (it->multibyte_p)
3513 {
3514 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3515 struct face *face = FACE_FROM_ID (it->f, face_id);
3516 face_id = FACE_FOR_CHAR (it->f, face, c);
3517 }
3518 }
3519
3520 return face_id;
3521 }
3522
3523
3524 \f
3525 /***********************************************************************
3526 Invisible text
3527 ***********************************************************************/
3528
3529 /* Set up iterator IT from invisible properties at its current
3530 position. Called from handle_stop. */
3531
3532 static enum prop_handled
3533 handle_invisible_prop (it)
3534 struct it *it;
3535 {
3536 enum prop_handled handled = HANDLED_NORMALLY;
3537
3538 if (STRINGP (it->string))
3539 {
3540 extern Lisp_Object Qinvisible;
3541 Lisp_Object prop, end_charpos, limit, charpos;
3542
3543 /* Get the value of the invisible text property at the
3544 current position. Value will be nil if there is no such
3545 property. */
3546 charpos = make_number (IT_STRING_CHARPOS (*it));
3547 prop = Fget_text_property (charpos, Qinvisible, it->string);
3548
3549 if (!NILP (prop)
3550 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3551 {
3552 handled = HANDLED_RECOMPUTE_PROPS;
3553
3554 /* Get the position at which the next change of the
3555 invisible text property can be found in IT->string.
3556 Value will be nil if the property value is the same for
3557 all the rest of IT->string. */
3558 XSETINT (limit, SCHARS (it->string));
3559 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3560 it->string, limit);
3561
3562 /* Text at current position is invisible. The next
3563 change in the property is at position end_charpos.
3564 Move IT's current position to that position. */
3565 if (INTEGERP (end_charpos)
3566 && XFASTINT (end_charpos) < XFASTINT (limit))
3567 {
3568 struct text_pos old;
3569 old = it->current.string_pos;
3570 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3571 compute_string_pos (&it->current.string_pos, old, it->string);
3572 }
3573 else
3574 {
3575 /* The rest of the string is invisible. If this is an
3576 overlay string, proceed with the next overlay string
3577 or whatever comes and return a character from there. */
3578 if (it->current.overlay_string_index >= 0)
3579 {
3580 next_overlay_string (it);
3581 /* Don't check for overlay strings when we just
3582 finished processing them. */
3583 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3584 }
3585 else
3586 {
3587 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3588 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3589 }
3590 }
3591 }
3592 }
3593 else
3594 {
3595 int invis_p, newpos, next_stop, start_charpos;
3596 Lisp_Object pos, prop, overlay;
3597
3598 /* First of all, is there invisible text at this position? */
3599 start_charpos = IT_CHARPOS (*it);
3600 pos = make_number (IT_CHARPOS (*it));
3601 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3602 &overlay);
3603 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3604
3605 /* If we are on invisible text, skip over it. */
3606 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3607 {
3608 /* Record whether we have to display an ellipsis for the
3609 invisible text. */
3610 int display_ellipsis_p = invis_p == 2;
3611
3612 handled = HANDLED_RECOMPUTE_PROPS;
3613
3614 /* Loop skipping over invisible text. The loop is left at
3615 ZV or with IT on the first char being visible again. */
3616 do
3617 {
3618 /* Try to skip some invisible text. Return value is the
3619 position reached which can be equal to IT's position
3620 if there is nothing invisible here. This skips both
3621 over invisible text properties and overlays with
3622 invisible property. */
3623 newpos = skip_invisible (IT_CHARPOS (*it),
3624 &next_stop, ZV, it->window);
3625
3626 /* If we skipped nothing at all we weren't at invisible
3627 text in the first place. If everything to the end of
3628 the buffer was skipped, end the loop. */
3629 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3630 invis_p = 0;
3631 else
3632 {
3633 /* We skipped some characters but not necessarily
3634 all there are. Check if we ended up on visible
3635 text. Fget_char_property returns the property of
3636 the char before the given position, i.e. if we
3637 get invis_p = 0, this means that the char at
3638 newpos is visible. */
3639 pos = make_number (newpos);
3640 prop = Fget_char_property (pos, Qinvisible, it->window);
3641 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3642 }
3643
3644 /* If we ended up on invisible text, proceed to
3645 skip starting with next_stop. */
3646 if (invis_p)
3647 IT_CHARPOS (*it) = next_stop;
3648
3649 /* If there are adjacent invisible texts, don't lose the
3650 second one's ellipsis. */
3651 if (invis_p == 2)
3652 display_ellipsis_p = 1;
3653 }
3654 while (invis_p);
3655
3656 /* The position newpos is now either ZV or on visible text. */
3657 IT_CHARPOS (*it) = newpos;
3658 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3659
3660 /* If there are before-strings at the start of invisible
3661 text, and the text is invisible because of a text
3662 property, arrange to show before-strings because 20.x did
3663 it that way. (If the text is invisible because of an
3664 overlay property instead of a text property, this is
3665 already handled in the overlay code.) */
3666 if (NILP (overlay)
3667 && get_overlay_strings (it, start_charpos))
3668 {
3669 handled = HANDLED_RECOMPUTE_PROPS;
3670 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3671 }
3672 else if (display_ellipsis_p)
3673 {
3674 /* Make sure that the glyphs of the ellipsis will get
3675 correct `charpos' values. If we would not update
3676 it->position here, the glyphs would belong to the
3677 last visible character _before_ the invisible
3678 text, which confuses `set_cursor_from_row'.
3679
3680 We use the last invisible position instead of the
3681 first because this way the cursor is always drawn on
3682 the first "." of the ellipsis, whenever PT is inside
3683 the invisible text. Otherwise the cursor would be
3684 placed _after_ the ellipsis when the point is after the
3685 first invisible character. */
3686 if (!STRINGP (it->object))
3687 {
3688 it->position.charpos = IT_CHARPOS (*it) - 1;
3689 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3690 }
3691 setup_for_ellipsis (it, 0);
3692 }
3693 }
3694 }
3695
3696 return handled;
3697 }
3698
3699
3700 /* Make iterator IT return `...' next.
3701 Replaces LEN characters from buffer. */
3702
3703 static void
3704 setup_for_ellipsis (it, len)
3705 struct it *it;
3706 int len;
3707 {
3708 /* Use the display table definition for `...'. Invalid glyphs
3709 will be handled by the method returning elements from dpvec. */
3710 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3711 {
3712 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3713 it->dpvec = v->contents;
3714 it->dpend = v->contents + v->size;
3715 }
3716 else
3717 {
3718 /* Default `...'. */
3719 it->dpvec = default_invis_vector;
3720 it->dpend = default_invis_vector + 3;
3721 }
3722
3723 it->dpvec_char_len = len;
3724 it->current.dpvec_index = 0;
3725 it->dpvec_face_id = -1;
3726
3727 /* Remember the current face id in case glyphs specify faces.
3728 IT's face is restored in set_iterator_to_next.
3729 saved_face_id was set to preceding char's face in handle_stop. */
3730 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3731 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3732
3733 it->method = GET_FROM_DISPLAY_VECTOR;
3734 it->ellipsis_p = 1;
3735 }
3736
3737
3738 \f
3739 /***********************************************************************
3740 'display' property
3741 ***********************************************************************/
3742
3743 /* Set up iterator IT from `display' property at its current position.
3744 Called from handle_stop.
3745 We return HANDLED_RETURN if some part of the display property
3746 overrides the display of the buffer text itself.
3747 Otherwise we return HANDLED_NORMALLY. */
3748
3749 static enum prop_handled
3750 handle_display_prop (it)
3751 struct it *it;
3752 {
3753 Lisp_Object prop, object;
3754 struct text_pos *position;
3755 /* Nonzero if some property replaces the display of the text itself. */
3756 int display_replaced_p = 0;
3757
3758 if (STRINGP (it->string))
3759 {
3760 object = it->string;
3761 position = &it->current.string_pos;
3762 }
3763 else
3764 {
3765 XSETWINDOW (object, it->w);
3766 position = &it->current.pos;
3767 }
3768
3769 /* Reset those iterator values set from display property values. */
3770 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3771 it->space_width = Qnil;
3772 it->font_height = Qnil;
3773 it->voffset = 0;
3774
3775 /* We don't support recursive `display' properties, i.e. string
3776 values that have a string `display' property, that have a string
3777 `display' property etc. */
3778 if (!it->string_from_display_prop_p)
3779 it->area = TEXT_AREA;
3780
3781 prop = Fget_char_property (make_number (position->charpos),
3782 Qdisplay, object);
3783 if (NILP (prop))
3784 return HANDLED_NORMALLY;
3785
3786 if (!STRINGP (it->string))
3787 object = it->w->buffer;
3788
3789 if (CONSP (prop)
3790 /* Simple properties. */
3791 && !EQ (XCAR (prop), Qimage)
3792 && !EQ (XCAR (prop), Qspace)
3793 && !EQ (XCAR (prop), Qwhen)
3794 && !EQ (XCAR (prop), Qslice)
3795 && !EQ (XCAR (prop), Qspace_width)
3796 && !EQ (XCAR (prop), Qheight)
3797 && !EQ (XCAR (prop), Qraise)
3798 /* Marginal area specifications. */
3799 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3800 && !EQ (XCAR (prop), Qleft_fringe)
3801 && !EQ (XCAR (prop), Qright_fringe)
3802 && !NILP (XCAR (prop)))
3803 {
3804 for (; CONSP (prop); prop = XCDR (prop))
3805 {
3806 if (handle_single_display_spec (it, XCAR (prop), object,
3807 position, display_replaced_p))
3808 display_replaced_p = 1;
3809 }
3810 }
3811 else if (VECTORP (prop))
3812 {
3813 int i;
3814 for (i = 0; i < ASIZE (prop); ++i)
3815 if (handle_single_display_spec (it, AREF (prop, i), object,
3816 position, display_replaced_p))
3817 display_replaced_p = 1;
3818 }
3819 else
3820 {
3821 int ret = handle_single_display_spec (it, prop, object, position, 0);
3822 if (ret < 0) /* Replaced by "", i.e. nothing. */
3823 return HANDLED_RECOMPUTE_PROPS;
3824 if (ret)
3825 display_replaced_p = 1;
3826 }
3827
3828 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3829 }
3830
3831
3832 /* Value is the position of the end of the `display' property starting
3833 at START_POS in OBJECT. */
3834
3835 static struct text_pos
3836 display_prop_end (it, object, start_pos)
3837 struct it *it;
3838 Lisp_Object object;
3839 struct text_pos start_pos;
3840 {
3841 Lisp_Object end;
3842 struct text_pos end_pos;
3843
3844 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3845 Qdisplay, object, Qnil);
3846 CHARPOS (end_pos) = XFASTINT (end);
3847 if (STRINGP (object))
3848 compute_string_pos (&end_pos, start_pos, it->string);
3849 else
3850 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3851
3852 return end_pos;
3853 }
3854
3855
3856 /* Set up IT from a single `display' specification PROP. OBJECT
3857 is the object in which the `display' property was found. *POSITION
3858 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3859 means that we previously saw a display specification which already
3860 replaced text display with something else, for example an image;
3861 we ignore such properties after the first one has been processed.
3862
3863 If PROP is a `space' or `image' specification, and in some other
3864 cases too, set *POSITION to the position where the `display'
3865 property ends.
3866
3867 Value is non-zero if something was found which replaces the display
3868 of buffer or string text. Specifically, the value is -1 if that
3869 "something" is "nothing". */
3870
3871 static int
3872 handle_single_display_spec (it, spec, object, position,
3873 display_replaced_before_p)
3874 struct it *it;
3875 Lisp_Object spec;
3876 Lisp_Object object;
3877 struct text_pos *position;
3878 int display_replaced_before_p;
3879 {
3880 Lisp_Object form;
3881 Lisp_Object location, value;
3882 struct text_pos start_pos;
3883 int valid_p;
3884
3885 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3886 If the result is non-nil, use VALUE instead of SPEC. */
3887 form = Qt;
3888 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3889 {
3890 spec = XCDR (spec);
3891 if (!CONSP (spec))
3892 return 0;
3893 form = XCAR (spec);
3894 spec = XCDR (spec);
3895 }
3896
3897 if (!NILP (form) && !EQ (form, Qt))
3898 {
3899 int count = SPECPDL_INDEX ();
3900 struct gcpro gcpro1;
3901
3902 /* Bind `object' to the object having the `display' property, a
3903 buffer or string. Bind `position' to the position in the
3904 object where the property was found, and `buffer-position'
3905 to the current position in the buffer. */
3906 specbind (Qobject, object);
3907 specbind (Qposition, make_number (CHARPOS (*position)));
3908 specbind (Qbuffer_position,
3909 make_number (STRINGP (object)
3910 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3911 GCPRO1 (form);
3912 form = safe_eval (form);
3913 UNGCPRO;
3914 unbind_to (count, Qnil);
3915 }
3916
3917 if (NILP (form))
3918 return 0;
3919
3920 /* Handle `(height HEIGHT)' specifications. */
3921 if (CONSP (spec)
3922 && EQ (XCAR (spec), Qheight)
3923 && CONSP (XCDR (spec)))
3924 {
3925 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3926 return 0;
3927
3928 it->font_height = XCAR (XCDR (spec));
3929 if (!NILP (it->font_height))
3930 {
3931 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3932 int new_height = -1;
3933
3934 if (CONSP (it->font_height)
3935 && (EQ (XCAR (it->font_height), Qplus)
3936 || EQ (XCAR (it->font_height), Qminus))
3937 && CONSP (XCDR (it->font_height))
3938 && INTEGERP (XCAR (XCDR (it->font_height))))
3939 {
3940 /* `(+ N)' or `(- N)' where N is an integer. */
3941 int steps = XINT (XCAR (XCDR (it->font_height)));
3942 if (EQ (XCAR (it->font_height), Qplus))
3943 steps = - steps;
3944 it->face_id = smaller_face (it->f, it->face_id, steps);
3945 }
3946 else if (FUNCTIONP (it->font_height))
3947 {
3948 /* Call function with current height as argument.
3949 Value is the new height. */
3950 Lisp_Object height;
3951 height = safe_call1 (it->font_height,
3952 face->lface[LFACE_HEIGHT_INDEX]);
3953 if (NUMBERP (height))
3954 new_height = XFLOATINT (height);
3955 }
3956 else if (NUMBERP (it->font_height))
3957 {
3958 /* Value is a multiple of the canonical char height. */
3959 struct face *face;
3960
3961 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3962 new_height = (XFLOATINT (it->font_height)
3963 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3964 }
3965 else
3966 {
3967 /* Evaluate IT->font_height with `height' bound to the
3968 current specified height to get the new height. */
3969 int count = SPECPDL_INDEX ();
3970
3971 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3972 value = safe_eval (it->font_height);
3973 unbind_to (count, Qnil);
3974
3975 if (NUMBERP (value))
3976 new_height = XFLOATINT (value);
3977 }
3978
3979 if (new_height > 0)
3980 it->face_id = face_with_height (it->f, it->face_id, new_height);
3981 }
3982
3983 return 0;
3984 }
3985
3986 /* Handle `(space_width WIDTH)'. */
3987 if (CONSP (spec)
3988 && EQ (XCAR (spec), Qspace_width)
3989 && CONSP (XCDR (spec)))
3990 {
3991 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3992 return 0;
3993
3994 value = XCAR (XCDR (spec));
3995 if (NUMBERP (value) && XFLOATINT (value) > 0)
3996 it->space_width = value;
3997
3998 return 0;
3999 }
4000
4001 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4002 if (CONSP (spec)
4003 && EQ (XCAR (spec), Qslice))
4004 {
4005 Lisp_Object tem;
4006
4007 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4008 return 0;
4009
4010 if (tem = XCDR (spec), CONSP (tem))
4011 {
4012 it->slice.x = XCAR (tem);
4013 if (tem = XCDR (tem), CONSP (tem))
4014 {
4015 it->slice.y = XCAR (tem);
4016 if (tem = XCDR (tem), CONSP (tem))
4017 {
4018 it->slice.width = XCAR (tem);
4019 if (tem = XCDR (tem), CONSP (tem))
4020 it->slice.height = XCAR (tem);
4021 }
4022 }
4023 }
4024
4025 return 0;
4026 }
4027
4028 /* Handle `(raise FACTOR)'. */
4029 if (CONSP (spec)
4030 && EQ (XCAR (spec), Qraise)
4031 && CONSP (XCDR (spec)))
4032 {
4033 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4034 return 0;
4035
4036 #ifdef HAVE_WINDOW_SYSTEM
4037 value = XCAR (XCDR (spec));
4038 if (NUMBERP (value))
4039 {
4040 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4041 it->voffset = - (XFLOATINT (value)
4042 * (FONT_HEIGHT (face->font)));
4043 }
4044 #endif /* HAVE_WINDOW_SYSTEM */
4045
4046 return 0;
4047 }
4048
4049 /* Don't handle the other kinds of display specifications
4050 inside a string that we got from a `display' property. */
4051 if (it->string_from_display_prop_p)
4052 return 0;
4053
4054 /* Characters having this form of property are not displayed, so
4055 we have to find the end of the property. */
4056 start_pos = *position;
4057 *position = display_prop_end (it, object, start_pos);
4058 value = Qnil;
4059
4060 /* Stop the scan at that end position--we assume that all
4061 text properties change there. */
4062 it->stop_charpos = position->charpos;
4063
4064 /* Handle `(left-fringe BITMAP [FACE])'
4065 and `(right-fringe BITMAP [FACE])'. */
4066 if (CONSP (spec)
4067 && (EQ (XCAR (spec), Qleft_fringe)
4068 || EQ (XCAR (spec), Qright_fringe))
4069 && CONSP (XCDR (spec)))
4070 {
4071 int face_id = DEFAULT_FACE_ID;
4072 int fringe_bitmap;
4073
4074 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4075 /* If we return here, POSITION has been advanced
4076 across the text with this property. */
4077 return 0;
4078
4079 #ifdef HAVE_WINDOW_SYSTEM
4080 value = XCAR (XCDR (spec));
4081 if (!SYMBOLP (value)
4082 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4083 /* If we return here, POSITION has been advanced
4084 across the text with this property. */
4085 return 0;
4086
4087 if (CONSP (XCDR (XCDR (spec))))
4088 {
4089 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4090 int face_id2 = lookup_derived_face (it->f, face_name,
4091 'A', FRINGE_FACE_ID, 0);
4092 if (face_id2 >= 0)
4093 face_id = face_id2;
4094 }
4095
4096 /* Save current settings of IT so that we can restore them
4097 when we are finished with the glyph property value. */
4098
4099 push_it (it);
4100
4101 it->area = TEXT_AREA;
4102 it->what = IT_IMAGE;
4103 it->image_id = -1; /* no image */
4104 it->position = start_pos;
4105 it->object = NILP (object) ? it->w->buffer : object;
4106 it->method = GET_FROM_IMAGE;
4107 it->face_id = face_id;
4108
4109 /* Say that we haven't consumed the characters with
4110 `display' property yet. The call to pop_it in
4111 set_iterator_to_next will clean this up. */
4112 *position = start_pos;
4113
4114 if (EQ (XCAR (spec), Qleft_fringe))
4115 {
4116 it->left_user_fringe_bitmap = fringe_bitmap;
4117 it->left_user_fringe_face_id = face_id;
4118 }
4119 else
4120 {
4121 it->right_user_fringe_bitmap = fringe_bitmap;
4122 it->right_user_fringe_face_id = face_id;
4123 }
4124 #endif /* HAVE_WINDOW_SYSTEM */
4125 return 1;
4126 }
4127
4128 /* Prepare to handle `((margin left-margin) ...)',
4129 `((margin right-margin) ...)' and `((margin nil) ...)'
4130 prefixes for display specifications. */
4131 location = Qunbound;
4132 if (CONSP (spec) && CONSP (XCAR (spec)))
4133 {
4134 Lisp_Object tem;
4135
4136 value = XCDR (spec);
4137 if (CONSP (value))
4138 value = XCAR (value);
4139
4140 tem = XCAR (spec);
4141 if (EQ (XCAR (tem), Qmargin)
4142 && (tem = XCDR (tem),
4143 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4144 (NILP (tem)
4145 || EQ (tem, Qleft_margin)
4146 || EQ (tem, Qright_margin))))
4147 location = tem;
4148 }
4149
4150 if (EQ (location, Qunbound))
4151 {
4152 location = Qnil;
4153 value = spec;
4154 }
4155
4156 /* After this point, VALUE is the property after any
4157 margin prefix has been stripped. It must be a string,
4158 an image specification, or `(space ...)'.
4159
4160 LOCATION specifies where to display: `left-margin',
4161 `right-margin' or nil. */
4162
4163 valid_p = (STRINGP (value)
4164 #ifdef HAVE_WINDOW_SYSTEM
4165 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4166 #endif /* not HAVE_WINDOW_SYSTEM */
4167 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4168
4169 if (valid_p && !display_replaced_before_p)
4170 {
4171 /* Save current settings of IT so that we can restore them
4172 when we are finished with the glyph property value. */
4173 push_it (it);
4174
4175 if (NILP (location))
4176 it->area = TEXT_AREA;
4177 else if (EQ (location, Qleft_margin))
4178 it->area = LEFT_MARGIN_AREA;
4179 else
4180 it->area = RIGHT_MARGIN_AREA;
4181
4182 if (STRINGP (value))
4183 {
4184 if (SCHARS (value) == 0)
4185 {
4186 pop_it (it);
4187 return -1; /* Replaced by "", i.e. nothing. */
4188 }
4189 it->string = value;
4190 it->multibyte_p = STRING_MULTIBYTE (it->string);
4191 it->current.overlay_string_index = -1;
4192 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4193 it->end_charpos = it->string_nchars = SCHARS (it->string);
4194 it->method = GET_FROM_STRING;
4195 it->stop_charpos = 0;
4196 it->string_from_display_prop_p = 1;
4197 /* Say that we haven't consumed the characters with
4198 `display' property yet. The call to pop_it in
4199 set_iterator_to_next will clean this up. */
4200 *position = start_pos;
4201 }
4202 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4203 {
4204 it->method = GET_FROM_STRETCH;
4205 it->object = value;
4206 *position = it->position = start_pos;
4207 }
4208 #ifdef HAVE_WINDOW_SYSTEM
4209 else
4210 {
4211 it->what = IT_IMAGE;
4212 it->image_id = lookup_image (it->f, value);
4213 it->position = start_pos;
4214 it->object = NILP (object) ? it->w->buffer : object;
4215 it->method = GET_FROM_IMAGE;
4216
4217 /* Say that we haven't consumed the characters with
4218 `display' property yet. The call to pop_it in
4219 set_iterator_to_next will clean this up. */
4220 *position = start_pos;
4221 }
4222 #endif /* HAVE_WINDOW_SYSTEM */
4223
4224 return 1;
4225 }
4226
4227 /* Invalid property or property not supported. Restore
4228 POSITION to what it was before. */
4229 *position = start_pos;
4230 return 0;
4231 }
4232
4233
4234 /* Check if SPEC is a display specification value whose text should be
4235 treated as intangible. */
4236
4237 static int
4238 single_display_spec_intangible_p (prop)
4239 Lisp_Object prop;
4240 {
4241 /* Skip over `when FORM'. */
4242 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4243 {
4244 prop = XCDR (prop);
4245 if (!CONSP (prop))
4246 return 0;
4247 prop = XCDR (prop);
4248 }
4249
4250 if (STRINGP (prop))
4251 return 1;
4252
4253 if (!CONSP (prop))
4254 return 0;
4255
4256 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4257 we don't need to treat text as intangible. */
4258 if (EQ (XCAR (prop), Qmargin))
4259 {
4260 prop = XCDR (prop);
4261 if (!CONSP (prop))
4262 return 0;
4263
4264 prop = XCDR (prop);
4265 if (!CONSP (prop)
4266 || EQ (XCAR (prop), Qleft_margin)
4267 || EQ (XCAR (prop), Qright_margin))
4268 return 0;
4269 }
4270
4271 return (CONSP (prop)
4272 && (EQ (XCAR (prop), Qimage)
4273 || EQ (XCAR (prop), Qspace)));
4274 }
4275
4276
4277 /* Check if PROP is a display property value whose text should be
4278 treated as intangible. */
4279
4280 int
4281 display_prop_intangible_p (prop)
4282 Lisp_Object prop;
4283 {
4284 if (CONSP (prop)
4285 && CONSP (XCAR (prop))
4286 && !EQ (Qmargin, XCAR (XCAR (prop))))
4287 {
4288 /* A list of sub-properties. */
4289 while (CONSP (prop))
4290 {
4291 if (single_display_spec_intangible_p (XCAR (prop)))
4292 return 1;
4293 prop = XCDR (prop);
4294 }
4295 }
4296 else if (VECTORP (prop))
4297 {
4298 /* A vector of sub-properties. */
4299 int i;
4300 for (i = 0; i < ASIZE (prop); ++i)
4301 if (single_display_spec_intangible_p (AREF (prop, i)))
4302 return 1;
4303 }
4304 else
4305 return single_display_spec_intangible_p (prop);
4306
4307 return 0;
4308 }
4309
4310
4311 /* Return 1 if PROP is a display sub-property value containing STRING. */
4312
4313 static int
4314 single_display_spec_string_p (prop, string)
4315 Lisp_Object prop, string;
4316 {
4317 if (EQ (string, prop))
4318 return 1;
4319
4320 /* Skip over `when FORM'. */
4321 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4322 {
4323 prop = XCDR (prop);
4324 if (!CONSP (prop))
4325 return 0;
4326 prop = XCDR (prop);
4327 }
4328
4329 if (CONSP (prop))
4330 /* Skip over `margin LOCATION'. */
4331 if (EQ (XCAR (prop), Qmargin))
4332 {
4333 prop = XCDR (prop);
4334 if (!CONSP (prop))
4335 return 0;
4336
4337 prop = XCDR (prop);
4338 if (!CONSP (prop))
4339 return 0;
4340 }
4341
4342 return CONSP (prop) && EQ (XCAR (prop), string);
4343 }
4344
4345
4346 /* Return 1 if STRING appears in the `display' property PROP. */
4347
4348 static int
4349 display_prop_string_p (prop, string)
4350 Lisp_Object prop, string;
4351 {
4352 if (CONSP (prop)
4353 && CONSP (XCAR (prop))
4354 && !EQ (Qmargin, XCAR (XCAR (prop))))
4355 {
4356 /* A list of sub-properties. */
4357 while (CONSP (prop))
4358 {
4359 if (single_display_spec_string_p (XCAR (prop), string))
4360 return 1;
4361 prop = XCDR (prop);
4362 }
4363 }
4364 else if (VECTORP (prop))
4365 {
4366 /* A vector of sub-properties. */
4367 int i;
4368 for (i = 0; i < ASIZE (prop); ++i)
4369 if (single_display_spec_string_p (AREF (prop, i), string))
4370 return 1;
4371 }
4372 else
4373 return single_display_spec_string_p (prop, string);
4374
4375 return 0;
4376 }
4377
4378
4379 /* Determine from which buffer position in W's buffer STRING comes
4380 from. AROUND_CHARPOS is an approximate position where it could
4381 be from. Value is the buffer position or 0 if it couldn't be
4382 determined.
4383
4384 W's buffer must be current.
4385
4386 This function is necessary because we don't record buffer positions
4387 in glyphs generated from strings (to keep struct glyph small).
4388 This function may only use code that doesn't eval because it is
4389 called asynchronously from note_mouse_highlight. */
4390
4391 int
4392 string_buffer_position (w, string, around_charpos)
4393 struct window *w;
4394 Lisp_Object string;
4395 int around_charpos;
4396 {
4397 Lisp_Object limit, prop, pos;
4398 const int MAX_DISTANCE = 1000;
4399 int found = 0;
4400
4401 pos = make_number (around_charpos);
4402 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4403 while (!found && !EQ (pos, limit))
4404 {
4405 prop = Fget_char_property (pos, Qdisplay, Qnil);
4406 if (!NILP (prop) && display_prop_string_p (prop, string))
4407 found = 1;
4408 else
4409 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4410 }
4411
4412 if (!found)
4413 {
4414 pos = make_number (around_charpos);
4415 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4416 while (!found && !EQ (pos, limit))
4417 {
4418 prop = Fget_char_property (pos, Qdisplay, Qnil);
4419 if (!NILP (prop) && display_prop_string_p (prop, string))
4420 found = 1;
4421 else
4422 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4423 limit);
4424 }
4425 }
4426
4427 return found ? XINT (pos) : 0;
4428 }
4429
4430
4431 \f
4432 /***********************************************************************
4433 `composition' property
4434 ***********************************************************************/
4435
4436 /* Set up iterator IT from `composition' property at its current
4437 position. Called from handle_stop. */
4438
4439 static enum prop_handled
4440 handle_composition_prop (it)
4441 struct it *it;
4442 {
4443 Lisp_Object prop, string;
4444 int pos, pos_byte, end;
4445 enum prop_handled handled = HANDLED_NORMALLY;
4446
4447 if (STRINGP (it->string))
4448 {
4449 pos = IT_STRING_CHARPOS (*it);
4450 pos_byte = IT_STRING_BYTEPOS (*it);
4451 string = it->string;
4452 }
4453 else
4454 {
4455 pos = IT_CHARPOS (*it);
4456 pos_byte = IT_BYTEPOS (*it);
4457 string = Qnil;
4458 }
4459
4460 /* If there's a valid composition and point is not inside of the
4461 composition (in the case that the composition is from the current
4462 buffer), draw a glyph composed from the composition components. */
4463 if (find_composition (pos, -1, &pos, &end, &prop, string)
4464 && COMPOSITION_VALID_P (pos, end, prop)
4465 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4466 {
4467 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4468
4469 if (id >= 0)
4470 {
4471 struct composition *cmp = composition_table[id];
4472
4473 if (cmp->glyph_len == 0)
4474 {
4475 /* No glyph. */
4476 if (STRINGP (it->string))
4477 {
4478 IT_STRING_CHARPOS (*it) = end;
4479 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4480 end);
4481 }
4482 else
4483 {
4484 IT_CHARPOS (*it) = end;
4485 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4486 }
4487 return HANDLED_RECOMPUTE_PROPS;
4488 }
4489 it->method = GET_FROM_COMPOSITION;
4490 it->cmp_id = id;
4491 it->cmp_len = COMPOSITION_LENGTH (prop);
4492 /* For a terminal, draw only the first character of the
4493 components. */
4494 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4495 it->len = (STRINGP (it->string)
4496 ? string_char_to_byte (it->string, end)
4497 : CHAR_TO_BYTE (end)) - pos_byte;
4498 it->stop_charpos = end;
4499 handled = HANDLED_RETURN;
4500 }
4501 }
4502
4503 return handled;
4504 }
4505
4506
4507 \f
4508 /***********************************************************************
4509 Overlay strings
4510 ***********************************************************************/
4511
4512 /* The following structure is used to record overlay strings for
4513 later sorting in load_overlay_strings. */
4514
4515 struct overlay_entry
4516 {
4517 Lisp_Object overlay;
4518 Lisp_Object string;
4519 int priority;
4520 int after_string_p;
4521 };
4522
4523
4524 /* Set up iterator IT from overlay strings at its current position.
4525 Called from handle_stop. */
4526
4527 static enum prop_handled
4528 handle_overlay_change (it)
4529 struct it *it;
4530 {
4531 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4532 return HANDLED_RECOMPUTE_PROPS;
4533 else
4534 return HANDLED_NORMALLY;
4535 }
4536
4537
4538 /* Set up the next overlay string for delivery by IT, if there is an
4539 overlay string to deliver. Called by set_iterator_to_next when the
4540 end of the current overlay string is reached. If there are more
4541 overlay strings to display, IT->string and
4542 IT->current.overlay_string_index are set appropriately here.
4543 Otherwise IT->string is set to nil. */
4544
4545 static void
4546 next_overlay_string (it)
4547 struct it *it;
4548 {
4549 ++it->current.overlay_string_index;
4550 if (it->current.overlay_string_index == it->n_overlay_strings)
4551 {
4552 /* No more overlay strings. Restore IT's settings to what
4553 they were before overlay strings were processed, and
4554 continue to deliver from current_buffer. */
4555 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4556
4557 pop_it (it);
4558 xassert (it->sp > 0
4559 || it->method == GET_FROM_COMPOSITION
4560 || (NILP (it->string)
4561 && it->method == GET_FROM_BUFFER
4562 && it->stop_charpos >= BEGV
4563 && it->stop_charpos <= it->end_charpos));
4564 it->current.overlay_string_index = -1;
4565 it->n_overlay_strings = 0;
4566
4567 /* If we're at the end of the buffer, record that we have
4568 processed the overlay strings there already, so that
4569 next_element_from_buffer doesn't try it again. */
4570 if (IT_CHARPOS (*it) >= it->end_charpos)
4571 it->overlay_strings_at_end_processed_p = 1;
4572
4573 /* If we have to display `...' for invisible text, set
4574 the iterator up for that. */
4575 if (display_ellipsis_p)
4576 setup_for_ellipsis (it, 0);
4577 }
4578 else
4579 {
4580 /* There are more overlay strings to process. If
4581 IT->current.overlay_string_index has advanced to a position
4582 where we must load IT->overlay_strings with more strings, do
4583 it. */
4584 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4585
4586 if (it->current.overlay_string_index && i == 0)
4587 load_overlay_strings (it, 0);
4588
4589 /* Initialize IT to deliver display elements from the overlay
4590 string. */
4591 it->string = it->overlay_strings[i];
4592 it->multibyte_p = STRING_MULTIBYTE (it->string);
4593 SET_TEXT_POS (it->current.string_pos, 0, 0);
4594 it->method = GET_FROM_STRING;
4595 it->stop_charpos = 0;
4596 }
4597
4598 CHECK_IT (it);
4599 }
4600
4601
4602 /* Compare two overlay_entry structures E1 and E2. Used as a
4603 comparison function for qsort in load_overlay_strings. Overlay
4604 strings for the same position are sorted so that
4605
4606 1. All after-strings come in front of before-strings, except
4607 when they come from the same overlay.
4608
4609 2. Within after-strings, strings are sorted so that overlay strings
4610 from overlays with higher priorities come first.
4611
4612 2. Within before-strings, strings are sorted so that overlay
4613 strings from overlays with higher priorities come last.
4614
4615 Value is analogous to strcmp. */
4616
4617
4618 static int
4619 compare_overlay_entries (e1, e2)
4620 void *e1, *e2;
4621 {
4622 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4623 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4624 int result;
4625
4626 if (entry1->after_string_p != entry2->after_string_p)
4627 {
4628 /* Let after-strings appear in front of before-strings if
4629 they come from different overlays. */
4630 if (EQ (entry1->overlay, entry2->overlay))
4631 result = entry1->after_string_p ? 1 : -1;
4632 else
4633 result = entry1->after_string_p ? -1 : 1;
4634 }
4635 else if (entry1->after_string_p)
4636 /* After-strings sorted in order of decreasing priority. */
4637 result = entry2->priority - entry1->priority;
4638 else
4639 /* Before-strings sorted in order of increasing priority. */
4640 result = entry1->priority - entry2->priority;
4641
4642 return result;
4643 }
4644
4645
4646 /* Load the vector IT->overlay_strings with overlay strings from IT's
4647 current buffer position, or from CHARPOS if that is > 0. Set
4648 IT->n_overlays to the total number of overlay strings found.
4649
4650 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4651 a time. On entry into load_overlay_strings,
4652 IT->current.overlay_string_index gives the number of overlay
4653 strings that have already been loaded by previous calls to this
4654 function.
4655
4656 IT->add_overlay_start contains an additional overlay start
4657 position to consider for taking overlay strings from, if non-zero.
4658 This position comes into play when the overlay has an `invisible'
4659 property, and both before and after-strings. When we've skipped to
4660 the end of the overlay, because of its `invisible' property, we
4661 nevertheless want its before-string to appear.
4662 IT->add_overlay_start will contain the overlay start position
4663 in this case.
4664
4665 Overlay strings are sorted so that after-string strings come in
4666 front of before-string strings. Within before and after-strings,
4667 strings are sorted by overlay priority. See also function
4668 compare_overlay_entries. */
4669
4670 static void
4671 load_overlay_strings (it, charpos)
4672 struct it *it;
4673 int charpos;
4674 {
4675 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4676 Lisp_Object overlay, window, str, invisible;
4677 struct Lisp_Overlay *ov;
4678 int start, end;
4679 int size = 20;
4680 int n = 0, i, j, invis_p;
4681 struct overlay_entry *entries
4682 = (struct overlay_entry *) alloca (size * sizeof *entries);
4683
4684 if (charpos <= 0)
4685 charpos = IT_CHARPOS (*it);
4686
4687 /* Append the overlay string STRING of overlay OVERLAY to vector
4688 `entries' which has size `size' and currently contains `n'
4689 elements. AFTER_P non-zero means STRING is an after-string of
4690 OVERLAY. */
4691 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4692 do \
4693 { \
4694 Lisp_Object priority; \
4695 \
4696 if (n == size) \
4697 { \
4698 int new_size = 2 * size; \
4699 struct overlay_entry *old = entries; \
4700 entries = \
4701 (struct overlay_entry *) alloca (new_size \
4702 * sizeof *entries); \
4703 bcopy (old, entries, size * sizeof *entries); \
4704 size = new_size; \
4705 } \
4706 \
4707 entries[n].string = (STRING); \
4708 entries[n].overlay = (OVERLAY); \
4709 priority = Foverlay_get ((OVERLAY), Qpriority); \
4710 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4711 entries[n].after_string_p = (AFTER_P); \
4712 ++n; \
4713 } \
4714 while (0)
4715
4716 /* Process overlay before the overlay center. */
4717 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4718 {
4719 XSETMISC (overlay, ov);
4720 xassert (OVERLAYP (overlay));
4721 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4722 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4723
4724 if (end < charpos)
4725 break;
4726
4727 /* Skip this overlay if it doesn't start or end at IT's current
4728 position. */
4729 if (end != charpos && start != charpos)
4730 continue;
4731
4732 /* Skip this overlay if it doesn't apply to IT->w. */
4733 window = Foverlay_get (overlay, Qwindow);
4734 if (WINDOWP (window) && XWINDOW (window) != it->w)
4735 continue;
4736
4737 /* If the text ``under'' the overlay is invisible, both before-
4738 and after-strings from this overlay are visible; start and
4739 end position are indistinguishable. */
4740 invisible = Foverlay_get (overlay, Qinvisible);
4741 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4742
4743 /* If overlay has a non-empty before-string, record it. */
4744 if ((start == charpos || (end == charpos && invis_p))
4745 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4746 && SCHARS (str))
4747 RECORD_OVERLAY_STRING (overlay, str, 0);
4748
4749 /* If overlay has a non-empty after-string, record it. */
4750 if ((end == charpos || (start == charpos && invis_p))
4751 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4752 && SCHARS (str))
4753 RECORD_OVERLAY_STRING (overlay, str, 1);
4754 }
4755
4756 /* Process overlays after the overlay center. */
4757 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4758 {
4759 XSETMISC (overlay, ov);
4760 xassert (OVERLAYP (overlay));
4761 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4762 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4763
4764 if (start > charpos)
4765 break;
4766
4767 /* Skip this overlay if it doesn't start or end at IT's current
4768 position. */
4769 if (end != charpos && start != charpos)
4770 continue;
4771
4772 /* Skip this overlay if it doesn't apply to IT->w. */
4773 window = Foverlay_get (overlay, Qwindow);
4774 if (WINDOWP (window) && XWINDOW (window) != it->w)
4775 continue;
4776
4777 /* If the text ``under'' the overlay is invisible, it has a zero
4778 dimension, and both before- and after-strings apply. */
4779 invisible = Foverlay_get (overlay, Qinvisible);
4780 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4781
4782 /* If overlay has a non-empty before-string, record it. */
4783 if ((start == charpos || (end == charpos && invis_p))
4784 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4785 && SCHARS (str))
4786 RECORD_OVERLAY_STRING (overlay, str, 0);
4787
4788 /* If overlay has a non-empty after-string, record it. */
4789 if ((end == charpos || (start == charpos && invis_p))
4790 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4791 && SCHARS (str))
4792 RECORD_OVERLAY_STRING (overlay, str, 1);
4793 }
4794
4795 #undef RECORD_OVERLAY_STRING
4796
4797 /* Sort entries. */
4798 if (n > 1)
4799 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4800
4801 /* Record the total number of strings to process. */
4802 it->n_overlay_strings = n;
4803
4804 /* IT->current.overlay_string_index is the number of overlay strings
4805 that have already been consumed by IT. Copy some of the
4806 remaining overlay strings to IT->overlay_strings. */
4807 i = 0;
4808 j = it->current.overlay_string_index;
4809 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4810 it->overlay_strings[i++] = entries[j++].string;
4811
4812 CHECK_IT (it);
4813 }
4814
4815
4816 /* Get the first chunk of overlay strings at IT's current buffer
4817 position, or at CHARPOS if that is > 0. Value is non-zero if at
4818 least one overlay string was found. */
4819
4820 static int
4821 get_overlay_strings_1 (it, charpos, compute_stop_p)
4822 struct it *it;
4823 int charpos;
4824 {
4825 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4826 process. This fills IT->overlay_strings with strings, and sets
4827 IT->n_overlay_strings to the total number of strings to process.
4828 IT->pos.overlay_string_index has to be set temporarily to zero
4829 because load_overlay_strings needs this; it must be set to -1
4830 when no overlay strings are found because a zero value would
4831 indicate a position in the first overlay string. */
4832 it->current.overlay_string_index = 0;
4833 load_overlay_strings (it, charpos);
4834
4835 /* If we found overlay strings, set up IT to deliver display
4836 elements from the first one. Otherwise set up IT to deliver
4837 from current_buffer. */
4838 if (it->n_overlay_strings)
4839 {
4840 /* Make sure we know settings in current_buffer, so that we can
4841 restore meaningful values when we're done with the overlay
4842 strings. */
4843 if (compute_stop_p)
4844 compute_stop_pos (it);
4845 xassert (it->face_id >= 0);
4846
4847 /* Save IT's settings. They are restored after all overlay
4848 strings have been processed. */
4849 xassert (!compute_stop_p || it->sp == 0);
4850 push_it (it);
4851
4852 /* Set up IT to deliver display elements from the first overlay
4853 string. */
4854 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4855 it->string = it->overlay_strings[0];
4856 it->stop_charpos = 0;
4857 xassert (STRINGP (it->string));
4858 it->end_charpos = SCHARS (it->string);
4859 it->multibyte_p = STRING_MULTIBYTE (it->string);
4860 it->method = GET_FROM_STRING;
4861 return 1;
4862 }
4863
4864 it->current.overlay_string_index = -1;
4865 return 0;
4866 }
4867
4868 static int
4869 get_overlay_strings (it, charpos)
4870 struct it *it;
4871 int charpos;
4872 {
4873 it->string = Qnil;
4874 it->method = GET_FROM_BUFFER;
4875
4876 (void) get_overlay_strings_1 (it, charpos, 1);
4877
4878 CHECK_IT (it);
4879
4880 /* Value is non-zero if we found at least one overlay string. */
4881 return STRINGP (it->string);
4882 }
4883
4884
4885 \f
4886 /***********************************************************************
4887 Saving and restoring state
4888 ***********************************************************************/
4889
4890 /* Save current settings of IT on IT->stack. Called, for example,
4891 before setting up IT for an overlay string, to be able to restore
4892 IT's settings to what they were after the overlay string has been
4893 processed. */
4894
4895 static void
4896 push_it (it)
4897 struct it *it;
4898 {
4899 struct iterator_stack_entry *p;
4900
4901 xassert (it->sp < IT_STACK_SIZE);
4902 p = it->stack + it->sp;
4903
4904 p->stop_charpos = it->stop_charpos;
4905 xassert (it->face_id >= 0);
4906 p->face_id = it->face_id;
4907 p->string = it->string;
4908 p->method = it->method;
4909 switch (p->method)
4910 {
4911 case GET_FROM_IMAGE:
4912 p->u.image.object = it->object;
4913 p->u.image.image_id = it->image_id;
4914 p->u.image.slice = it->slice;
4915 break;
4916 case GET_FROM_COMPOSITION:
4917 p->u.comp.object = it->object;
4918 p->u.comp.c = it->c;
4919 p->u.comp.len = it->len;
4920 p->u.comp.cmp_id = it->cmp_id;
4921 p->u.comp.cmp_len = it->cmp_len;
4922 break;
4923 case GET_FROM_STRETCH:
4924 p->u.stretch.object = it->object;
4925 break;
4926 }
4927 p->pos = it->current;
4928 p->end_charpos = it->end_charpos;
4929 p->string_nchars = it->string_nchars;
4930 p->area = it->area;
4931 p->multibyte_p = it->multibyte_p;
4932 p->space_width = it->space_width;
4933 p->font_height = it->font_height;
4934 p->voffset = it->voffset;
4935 p->string_from_display_prop_p = it->string_from_display_prop_p;
4936 p->display_ellipsis_p = 0;
4937 ++it->sp;
4938 }
4939
4940
4941 /* Restore IT's settings from IT->stack. Called, for example, when no
4942 more overlay strings must be processed, and we return to delivering
4943 display elements from a buffer, or when the end of a string from a
4944 `display' property is reached and we return to delivering display
4945 elements from an overlay string, or from a buffer. */
4946
4947 static void
4948 pop_it (it)
4949 struct it *it;
4950 {
4951 struct iterator_stack_entry *p;
4952
4953 xassert (it->sp > 0);
4954 --it->sp;
4955 p = it->stack + it->sp;
4956 it->stop_charpos = p->stop_charpos;
4957 it->face_id = p->face_id;
4958 it->current = p->pos;
4959 it->string = p->string;
4960 if (NILP (it->string))
4961 SET_TEXT_POS (it->current.string_pos, -1, -1);
4962 it->method = p->method;
4963 switch (it->method)
4964 {
4965 case GET_FROM_IMAGE:
4966 it->image_id = p->u.image.image_id;
4967 it->object = p->u.image.object;
4968 it->slice = p->u.image.slice;
4969 break;
4970 case GET_FROM_COMPOSITION:
4971 it->object = p->u.comp.object;
4972 it->c = p->u.comp.c;
4973 it->len = p->u.comp.len;
4974 it->cmp_id = p->u.comp.cmp_id;
4975 it->cmp_len = p->u.comp.cmp_len;
4976 break;
4977 case GET_FROM_STRETCH:
4978 it->object = p->u.comp.object;
4979 break;
4980 }
4981 it->end_charpos = p->end_charpos;
4982 it->string_nchars = p->string_nchars;
4983 it->area = p->area;
4984 it->multibyte_p = p->multibyte_p;
4985 it->space_width = p->space_width;
4986 it->font_height = p->font_height;
4987 it->voffset = p->voffset;
4988 it->string_from_display_prop_p = p->string_from_display_prop_p;
4989 }
4990
4991
4992 \f
4993 /***********************************************************************
4994 Moving over lines
4995 ***********************************************************************/
4996
4997 /* Set IT's current position to the previous line start. */
4998
4999 static void
5000 back_to_previous_line_start (it)
5001 struct it *it;
5002 {
5003 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5004 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5005 }
5006
5007
5008 /* Move IT to the next line start.
5009
5010 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5011 we skipped over part of the text (as opposed to moving the iterator
5012 continuously over the text). Otherwise, don't change the value
5013 of *SKIPPED_P.
5014
5015 Newlines may come from buffer text, overlay strings, or strings
5016 displayed via the `display' property. That's the reason we can't
5017 simply use find_next_newline_no_quit.
5018
5019 Note that this function may not skip over invisible text that is so
5020 because of text properties and immediately follows a newline. If
5021 it would, function reseat_at_next_visible_line_start, when called
5022 from set_iterator_to_next, would effectively make invisible
5023 characters following a newline part of the wrong glyph row, which
5024 leads to wrong cursor motion. */
5025
5026 static int
5027 forward_to_next_line_start (it, skipped_p)
5028 struct it *it;
5029 int *skipped_p;
5030 {
5031 int old_selective, newline_found_p, n;
5032 const int MAX_NEWLINE_DISTANCE = 500;
5033
5034 /* If already on a newline, just consume it to avoid unintended
5035 skipping over invisible text below. */
5036 if (it->what == IT_CHARACTER
5037 && it->c == '\n'
5038 && CHARPOS (it->position) == IT_CHARPOS (*it))
5039 {
5040 set_iterator_to_next (it, 0);
5041 it->c = 0;
5042 return 1;
5043 }
5044
5045 /* Don't handle selective display in the following. It's (a)
5046 unnecessary because it's done by the caller, and (b) leads to an
5047 infinite recursion because next_element_from_ellipsis indirectly
5048 calls this function. */
5049 old_selective = it->selective;
5050 it->selective = 0;
5051
5052 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5053 from buffer text. */
5054 for (n = newline_found_p = 0;
5055 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5056 n += STRINGP (it->string) ? 0 : 1)
5057 {
5058 if (!get_next_display_element (it))
5059 return 0;
5060 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5061 set_iterator_to_next (it, 0);
5062 }
5063
5064 /* If we didn't find a newline near enough, see if we can use a
5065 short-cut. */
5066 if (!newline_found_p)
5067 {
5068 int start = IT_CHARPOS (*it);
5069 int limit = find_next_newline_no_quit (start, 1);
5070 Lisp_Object pos;
5071
5072 xassert (!STRINGP (it->string));
5073
5074 /* If there isn't any `display' property in sight, and no
5075 overlays, we can just use the position of the newline in
5076 buffer text. */
5077 if (it->stop_charpos >= limit
5078 || ((pos = Fnext_single_property_change (make_number (start),
5079 Qdisplay,
5080 Qnil, make_number (limit)),
5081 NILP (pos))
5082 && next_overlay_change (start) == ZV))
5083 {
5084 IT_CHARPOS (*it) = limit;
5085 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5086 *skipped_p = newline_found_p = 1;
5087 }
5088 else
5089 {
5090 while (get_next_display_element (it)
5091 && !newline_found_p)
5092 {
5093 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5094 set_iterator_to_next (it, 0);
5095 }
5096 }
5097 }
5098
5099 it->selective = old_selective;
5100 return newline_found_p;
5101 }
5102
5103
5104 /* Set IT's current position to the previous visible line start. Skip
5105 invisible text that is so either due to text properties or due to
5106 selective display. Caution: this does not change IT->current_x and
5107 IT->hpos. */
5108
5109 static void
5110 back_to_previous_visible_line_start (it)
5111 struct it *it;
5112 {
5113 while (IT_CHARPOS (*it) > BEGV)
5114 {
5115 back_to_previous_line_start (it);
5116 if (IT_CHARPOS (*it) <= BEGV)
5117 break;
5118
5119 /* If selective > 0, then lines indented more than that values
5120 are invisible. */
5121 if (it->selective > 0
5122 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5123 (double) it->selective)) /* iftc */
5124 continue;
5125
5126 /* Check the newline before point for invisibility. */
5127 {
5128 Lisp_Object prop;
5129 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5130 Qinvisible, it->window);
5131 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5132 continue;
5133 }
5134
5135 if (IT_CHARPOS (*it) <= BEGV)
5136 break;
5137
5138 {
5139 struct it it2;
5140 int pos;
5141 int beg, end;
5142 Lisp_Object val, overlay;
5143
5144 /* If newline is part of a composition, continue from start of composition */
5145 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5146 && beg < IT_CHARPOS (*it))
5147 goto replaced;
5148
5149 /* If newline is replaced by a display property, find start of overlay
5150 or interval and continue search from that point. */
5151 it2 = *it;
5152 pos = --IT_CHARPOS (it2);
5153 --IT_BYTEPOS (it2);
5154 it2.sp = 0;
5155 if (handle_display_prop (&it2) == HANDLED_RETURN
5156 && !NILP (val = get_char_property_and_overlay
5157 (make_number (pos), Qdisplay, Qnil, &overlay))
5158 && (OVERLAYP (overlay)
5159 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5160 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5161 goto replaced;
5162
5163 /* Newline is not replaced by anything -- so we are done. */
5164 break;
5165
5166 replaced:
5167 if (beg < BEGV)
5168 beg = BEGV;
5169 IT_CHARPOS (*it) = beg;
5170 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5171 }
5172 }
5173
5174 it->continuation_lines_width = 0;
5175
5176 xassert (IT_CHARPOS (*it) >= BEGV);
5177 xassert (IT_CHARPOS (*it) == BEGV
5178 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5179 CHECK_IT (it);
5180 }
5181
5182
5183 /* Reseat iterator IT at the previous visible line start. Skip
5184 invisible text that is so either due to text properties or due to
5185 selective display. At the end, update IT's overlay information,
5186 face information etc. */
5187
5188 void
5189 reseat_at_previous_visible_line_start (it)
5190 struct it *it;
5191 {
5192 back_to_previous_visible_line_start (it);
5193 reseat (it, it->current.pos, 1);
5194 CHECK_IT (it);
5195 }
5196
5197
5198 /* Reseat iterator IT on the next visible line start in the current
5199 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5200 preceding the line start. Skip over invisible text that is so
5201 because of selective display. Compute faces, overlays etc at the
5202 new position. Note that this function does not skip over text that
5203 is invisible because of text properties. */
5204
5205 static void
5206 reseat_at_next_visible_line_start (it, on_newline_p)
5207 struct it *it;
5208 int on_newline_p;
5209 {
5210 int newline_found_p, skipped_p = 0;
5211
5212 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5213
5214 /* Skip over lines that are invisible because they are indented
5215 more than the value of IT->selective. */
5216 if (it->selective > 0)
5217 while (IT_CHARPOS (*it) < ZV
5218 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5219 (double) it->selective)) /* iftc */
5220 {
5221 xassert (IT_BYTEPOS (*it) == BEGV
5222 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5223 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5224 }
5225
5226 /* Position on the newline if that's what's requested. */
5227 if (on_newline_p && newline_found_p)
5228 {
5229 if (STRINGP (it->string))
5230 {
5231 if (IT_STRING_CHARPOS (*it) > 0)
5232 {
5233 --IT_STRING_CHARPOS (*it);
5234 --IT_STRING_BYTEPOS (*it);
5235 }
5236 }
5237 else if (IT_CHARPOS (*it) > BEGV)
5238 {
5239 --IT_CHARPOS (*it);
5240 --IT_BYTEPOS (*it);
5241 reseat (it, it->current.pos, 0);
5242 }
5243 }
5244 else if (skipped_p)
5245 reseat (it, it->current.pos, 0);
5246
5247 CHECK_IT (it);
5248 }
5249
5250
5251 \f
5252 /***********************************************************************
5253 Changing an iterator's position
5254 ***********************************************************************/
5255
5256 /* Change IT's current position to POS in current_buffer. If FORCE_P
5257 is non-zero, always check for text properties at the new position.
5258 Otherwise, text properties are only looked up if POS >=
5259 IT->check_charpos of a property. */
5260
5261 static void
5262 reseat (it, pos, force_p)
5263 struct it *it;
5264 struct text_pos pos;
5265 int force_p;
5266 {
5267 int original_pos = IT_CHARPOS (*it);
5268
5269 reseat_1 (it, pos, 0);
5270
5271 /* Determine where to check text properties. Avoid doing it
5272 where possible because text property lookup is very expensive. */
5273 if (force_p
5274 || CHARPOS (pos) > it->stop_charpos
5275 || CHARPOS (pos) < original_pos)
5276 handle_stop (it);
5277
5278 CHECK_IT (it);
5279 }
5280
5281
5282 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5283 IT->stop_pos to POS, also. */
5284
5285 static void
5286 reseat_1 (it, pos, set_stop_p)
5287 struct it *it;
5288 struct text_pos pos;
5289 int set_stop_p;
5290 {
5291 /* Don't call this function when scanning a C string. */
5292 xassert (it->s == NULL);
5293
5294 /* POS must be a reasonable value. */
5295 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5296
5297 it->current.pos = it->position = pos;
5298 XSETBUFFER (it->object, current_buffer);
5299 it->end_charpos = ZV;
5300 it->dpvec = NULL;
5301 it->current.dpvec_index = -1;
5302 it->current.overlay_string_index = -1;
5303 IT_STRING_CHARPOS (*it) = -1;
5304 IT_STRING_BYTEPOS (*it) = -1;
5305 it->string = Qnil;
5306 it->method = GET_FROM_BUFFER;
5307 it->object = it->w->buffer;
5308 it->area = TEXT_AREA;
5309 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5310 it->sp = 0;
5311 it->string_from_display_prop_p = 0;
5312 it->face_before_selective_p = 0;
5313
5314 if (set_stop_p)
5315 it->stop_charpos = CHARPOS (pos);
5316 }
5317
5318
5319 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5320 If S is non-null, it is a C string to iterate over. Otherwise,
5321 STRING gives a Lisp string to iterate over.
5322
5323 If PRECISION > 0, don't return more then PRECISION number of
5324 characters from the string.
5325
5326 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5327 characters have been returned. FIELD_WIDTH < 0 means an infinite
5328 field width.
5329
5330 MULTIBYTE = 0 means disable processing of multibyte characters,
5331 MULTIBYTE > 0 means enable it,
5332 MULTIBYTE < 0 means use IT->multibyte_p.
5333
5334 IT must be initialized via a prior call to init_iterator before
5335 calling this function. */
5336
5337 static void
5338 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5339 struct it *it;
5340 unsigned char *s;
5341 Lisp_Object string;
5342 int charpos;
5343 int precision, field_width, multibyte;
5344 {
5345 /* No region in strings. */
5346 it->region_beg_charpos = it->region_end_charpos = -1;
5347
5348 /* No text property checks performed by default, but see below. */
5349 it->stop_charpos = -1;
5350
5351 /* Set iterator position and end position. */
5352 bzero (&it->current, sizeof it->current);
5353 it->current.overlay_string_index = -1;
5354 it->current.dpvec_index = -1;
5355 xassert (charpos >= 0);
5356
5357 /* If STRING is specified, use its multibyteness, otherwise use the
5358 setting of MULTIBYTE, if specified. */
5359 if (multibyte >= 0)
5360 it->multibyte_p = multibyte > 0;
5361
5362 if (s == NULL)
5363 {
5364 xassert (STRINGP (string));
5365 it->string = string;
5366 it->s = NULL;
5367 it->end_charpos = it->string_nchars = SCHARS (string);
5368 it->method = GET_FROM_STRING;
5369 it->current.string_pos = string_pos (charpos, string);
5370 }
5371 else
5372 {
5373 it->s = s;
5374 it->string = Qnil;
5375
5376 /* Note that we use IT->current.pos, not it->current.string_pos,
5377 for displaying C strings. */
5378 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5379 if (it->multibyte_p)
5380 {
5381 it->current.pos = c_string_pos (charpos, s, 1);
5382 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5383 }
5384 else
5385 {
5386 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5387 it->end_charpos = it->string_nchars = strlen (s);
5388 }
5389
5390 it->method = GET_FROM_C_STRING;
5391 }
5392
5393 /* PRECISION > 0 means don't return more than PRECISION characters
5394 from the string. */
5395 if (precision > 0 && it->end_charpos - charpos > precision)
5396 it->end_charpos = it->string_nchars = charpos + precision;
5397
5398 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5399 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5400 FIELD_WIDTH < 0 means infinite field width. This is useful for
5401 padding with `-' at the end of a mode line. */
5402 if (field_width < 0)
5403 field_width = INFINITY;
5404 if (field_width > it->end_charpos - charpos)
5405 it->end_charpos = charpos + field_width;
5406
5407 /* Use the standard display table for displaying strings. */
5408 if (DISP_TABLE_P (Vstandard_display_table))
5409 it->dp = XCHAR_TABLE (Vstandard_display_table);
5410
5411 it->stop_charpos = charpos;
5412 CHECK_IT (it);
5413 }
5414
5415
5416 \f
5417 /***********************************************************************
5418 Iteration
5419 ***********************************************************************/
5420
5421 /* Map enum it_method value to corresponding next_element_from_* function. */
5422
5423 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5424 {
5425 next_element_from_buffer,
5426 next_element_from_display_vector,
5427 next_element_from_composition,
5428 next_element_from_string,
5429 next_element_from_c_string,
5430 next_element_from_image,
5431 next_element_from_stretch
5432 };
5433
5434
5435 /* Load IT's display element fields with information about the next
5436 display element from the current position of IT. Value is zero if
5437 end of buffer (or C string) is reached. */
5438
5439 static struct frame *last_escape_glyph_frame = NULL;
5440 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5441 static int last_escape_glyph_merged_face_id = 0;
5442
5443 int
5444 get_next_display_element (it)
5445 struct it *it;
5446 {
5447 /* Non-zero means that we found a display element. Zero means that
5448 we hit the end of what we iterate over. Performance note: the
5449 function pointer `method' used here turns out to be faster than
5450 using a sequence of if-statements. */
5451 int success_p;
5452
5453 get_next:
5454 success_p = (*get_next_element[it->method]) (it);
5455
5456 if (it->what == IT_CHARACTER)
5457 {
5458 /* Map via display table or translate control characters.
5459 IT->c, IT->len etc. have been set to the next character by
5460 the function call above. If we have a display table, and it
5461 contains an entry for IT->c, translate it. Don't do this if
5462 IT->c itself comes from a display table, otherwise we could
5463 end up in an infinite recursion. (An alternative could be to
5464 count the recursion depth of this function and signal an
5465 error when a certain maximum depth is reached.) Is it worth
5466 it? */
5467 if (success_p && it->dpvec == NULL)
5468 {
5469 Lisp_Object dv;
5470
5471 if (it->dp
5472 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5473 VECTORP (dv)))
5474 {
5475 struct Lisp_Vector *v = XVECTOR (dv);
5476
5477 /* Return the first character from the display table
5478 entry, if not empty. If empty, don't display the
5479 current character. */
5480 if (v->size)
5481 {
5482 it->dpvec_char_len = it->len;
5483 it->dpvec = v->contents;
5484 it->dpend = v->contents + v->size;
5485 it->current.dpvec_index = 0;
5486 it->dpvec_face_id = -1;
5487 it->saved_face_id = it->face_id;
5488 it->method = GET_FROM_DISPLAY_VECTOR;
5489 it->ellipsis_p = 0;
5490 }
5491 else
5492 {
5493 set_iterator_to_next (it, 0);
5494 }
5495 goto get_next;
5496 }
5497
5498 /* Translate control characters into `\003' or `^C' form.
5499 Control characters coming from a display table entry are
5500 currently not translated because we use IT->dpvec to hold
5501 the translation. This could easily be changed but I
5502 don't believe that it is worth doing.
5503
5504 If it->multibyte_p is nonzero, eight-bit characters and
5505 non-printable multibyte characters are also translated to
5506 octal form.
5507
5508 If it->multibyte_p is zero, eight-bit characters that
5509 don't have corresponding multibyte char code are also
5510 translated to octal form. */
5511 else if ((it->c < ' '
5512 && (it->area != TEXT_AREA
5513 /* In mode line, treat \n like other crl chars. */
5514 || (it->c != '\t'
5515 && it->glyph_row && it->glyph_row->mode_line_p)
5516 || (it->c != '\n' && it->c != '\t')))
5517 || (it->multibyte_p
5518 ? ((it->c >= 127
5519 && it->len == 1)
5520 || !CHAR_PRINTABLE_P (it->c)
5521 || (!NILP (Vnobreak_char_display)
5522 && (it->c == 0x8a0 || it->c == 0x8ad
5523 || it->c == 0x920 || it->c == 0x92d
5524 || it->c == 0xe20 || it->c == 0xe2d
5525 || it->c == 0xf20 || it->c == 0xf2d)))
5526 : (it->c >= 127
5527 && (!unibyte_display_via_language_environment
5528 || it->c == unibyte_char_to_multibyte (it->c)))))
5529 {
5530 /* IT->c is a control character which must be displayed
5531 either as '\003' or as `^C' where the '\\' and '^'
5532 can be defined in the display table. Fill
5533 IT->ctl_chars with glyphs for what we have to
5534 display. Then, set IT->dpvec to these glyphs. */
5535 GLYPH g;
5536 int ctl_len;
5537 int face_id, lface_id = 0 ;
5538 GLYPH escape_glyph;
5539
5540 /* Handle control characters with ^. */
5541
5542 if (it->c < 128 && it->ctl_arrow_p)
5543 {
5544 g = '^'; /* default glyph for Control */
5545 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5546 if (it->dp
5547 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5548 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5549 {
5550 g = XINT (DISP_CTRL_GLYPH (it->dp));
5551 lface_id = FAST_GLYPH_FACE (g);
5552 }
5553 if (lface_id)
5554 {
5555 g = FAST_GLYPH_CHAR (g);
5556 face_id = merge_faces (it->f, Qt, lface_id,
5557 it->face_id);
5558 }
5559 else if (it->f == last_escape_glyph_frame
5560 && it->face_id == last_escape_glyph_face_id)
5561 {
5562 face_id = last_escape_glyph_merged_face_id;
5563 }
5564 else
5565 {
5566 /* Merge the escape-glyph face into the current face. */
5567 face_id = merge_faces (it->f, Qescape_glyph, 0,
5568 it->face_id);
5569 last_escape_glyph_frame = it->f;
5570 last_escape_glyph_face_id = it->face_id;
5571 last_escape_glyph_merged_face_id = face_id;
5572 }
5573
5574 XSETINT (it->ctl_chars[0], g);
5575 g = it->c ^ 0100;
5576 XSETINT (it->ctl_chars[1], g);
5577 ctl_len = 2;
5578 goto display_control;
5579 }
5580
5581 /* Handle non-break space in the mode where it only gets
5582 highlighting. */
5583
5584 if (EQ (Vnobreak_char_display, Qt)
5585 && (it->c == 0x8a0 || it->c == 0x920
5586 || it->c == 0xe20 || it->c == 0xf20))
5587 {
5588 /* Merge the no-break-space face into the current face. */
5589 face_id = merge_faces (it->f, Qnobreak_space, 0,
5590 it->face_id);
5591
5592 g = it->c = ' ';
5593 XSETINT (it->ctl_chars[0], g);
5594 ctl_len = 1;
5595 goto display_control;
5596 }
5597
5598 /* Handle sequences that start with the "escape glyph". */
5599
5600 /* the default escape glyph is \. */
5601 escape_glyph = '\\';
5602
5603 if (it->dp
5604 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5605 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5606 {
5607 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5608 lface_id = FAST_GLYPH_FACE (escape_glyph);
5609 }
5610 if (lface_id)
5611 {
5612 /* The display table specified a face.
5613 Merge it into face_id and also into escape_glyph. */
5614 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5615 face_id = merge_faces (it->f, Qt, lface_id,
5616 it->face_id);
5617 }
5618 else if (it->f == last_escape_glyph_frame
5619 && it->face_id == last_escape_glyph_face_id)
5620 {
5621 face_id = last_escape_glyph_merged_face_id;
5622 }
5623 else
5624 {
5625 /* Merge the escape-glyph face into the current face. */
5626 face_id = merge_faces (it->f, Qescape_glyph, 0,
5627 it->face_id);
5628 last_escape_glyph_frame = it->f;
5629 last_escape_glyph_face_id = it->face_id;
5630 last_escape_glyph_merged_face_id = face_id;
5631 }
5632
5633 /* Handle soft hyphens in the mode where they only get
5634 highlighting. */
5635
5636 if (EQ (Vnobreak_char_display, Qt)
5637 && (it->c == 0x8ad || it->c == 0x92d
5638 || it->c == 0xe2d || it->c == 0xf2d))
5639 {
5640 g = it->c = '-';
5641 XSETINT (it->ctl_chars[0], g);
5642 ctl_len = 1;
5643 goto display_control;
5644 }
5645
5646 /* Handle non-break space and soft hyphen
5647 with the escape glyph. */
5648
5649 if (it->c == 0x8a0 || it->c == 0x8ad
5650 || it->c == 0x920 || it->c == 0x92d
5651 || it->c == 0xe20 || it->c == 0xe2d
5652 || it->c == 0xf20 || it->c == 0xf2d)
5653 {
5654 XSETINT (it->ctl_chars[0], escape_glyph);
5655 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5656 XSETINT (it->ctl_chars[1], g);
5657 ctl_len = 2;
5658 goto display_control;
5659 }
5660
5661 {
5662 unsigned char str[MAX_MULTIBYTE_LENGTH];
5663 int len;
5664 int i;
5665
5666 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5667 if (SINGLE_BYTE_CHAR_P (it->c))
5668 str[0] = it->c, len = 1;
5669 else
5670 {
5671 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5672 if (len < 0)
5673 {
5674 /* It's an invalid character, which shouldn't
5675 happen actually, but due to bugs it may
5676 happen. Let's print the char as is, there's
5677 not much meaningful we can do with it. */
5678 str[0] = it->c;
5679 str[1] = it->c >> 8;
5680 str[2] = it->c >> 16;
5681 str[3] = it->c >> 24;
5682 len = 4;
5683 }
5684 }
5685
5686 for (i = 0; i < len; i++)
5687 {
5688 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5689 /* Insert three more glyphs into IT->ctl_chars for
5690 the octal display of the character. */
5691 g = ((str[i] >> 6) & 7) + '0';
5692 XSETINT (it->ctl_chars[i * 4 + 1], g);
5693 g = ((str[i] >> 3) & 7) + '0';
5694 XSETINT (it->ctl_chars[i * 4 + 2], g);
5695 g = (str[i] & 7) + '0';
5696 XSETINT (it->ctl_chars[i * 4 + 3], g);
5697 }
5698 ctl_len = len * 4;
5699 }
5700
5701 display_control:
5702 /* Set up IT->dpvec and return first character from it. */
5703 it->dpvec_char_len = it->len;
5704 it->dpvec = it->ctl_chars;
5705 it->dpend = it->dpvec + ctl_len;
5706 it->current.dpvec_index = 0;
5707 it->dpvec_face_id = face_id;
5708 it->saved_face_id = it->face_id;
5709 it->method = GET_FROM_DISPLAY_VECTOR;
5710 it->ellipsis_p = 0;
5711 goto get_next;
5712 }
5713 }
5714
5715 /* Adjust face id for a multibyte character. There are no
5716 multibyte character in unibyte text. */
5717 if (it->multibyte_p
5718 && success_p
5719 && FRAME_WINDOW_P (it->f))
5720 {
5721 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5722 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5723 }
5724 }
5725
5726 /* Is this character the last one of a run of characters with
5727 box? If yes, set IT->end_of_box_run_p to 1. */
5728 if (it->face_box_p
5729 && it->s == NULL)
5730 {
5731 int face_id;
5732 struct face *face;
5733
5734 it->end_of_box_run_p
5735 = ((face_id = face_after_it_pos (it),
5736 face_id != it->face_id)
5737 && (face = FACE_FROM_ID (it->f, face_id),
5738 face->box == FACE_NO_BOX));
5739 }
5740
5741 /* Value is 0 if end of buffer or string reached. */
5742 return success_p;
5743 }
5744
5745
5746 /* Move IT to the next display element.
5747
5748 RESEAT_P non-zero means if called on a newline in buffer text,
5749 skip to the next visible line start.
5750
5751 Functions get_next_display_element and set_iterator_to_next are
5752 separate because I find this arrangement easier to handle than a
5753 get_next_display_element function that also increments IT's
5754 position. The way it is we can first look at an iterator's current
5755 display element, decide whether it fits on a line, and if it does,
5756 increment the iterator position. The other way around we probably
5757 would either need a flag indicating whether the iterator has to be
5758 incremented the next time, or we would have to implement a
5759 decrement position function which would not be easy to write. */
5760
5761 void
5762 set_iterator_to_next (it, reseat_p)
5763 struct it *it;
5764 int reseat_p;
5765 {
5766 /* Reset flags indicating start and end of a sequence of characters
5767 with box. Reset them at the start of this function because
5768 moving the iterator to a new position might set them. */
5769 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5770
5771 switch (it->method)
5772 {
5773 case GET_FROM_BUFFER:
5774 /* The current display element of IT is a character from
5775 current_buffer. Advance in the buffer, and maybe skip over
5776 invisible lines that are so because of selective display. */
5777 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5778 reseat_at_next_visible_line_start (it, 0);
5779 else
5780 {
5781 xassert (it->len != 0);
5782 IT_BYTEPOS (*it) += it->len;
5783 IT_CHARPOS (*it) += 1;
5784 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5785 }
5786 break;
5787
5788 case GET_FROM_COMPOSITION:
5789 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5790 if (STRINGP (it->string))
5791 {
5792 IT_STRING_BYTEPOS (*it) += it->len;
5793 IT_STRING_CHARPOS (*it) += it->cmp_len;
5794 it->method = GET_FROM_STRING;
5795 it->object = it->string;
5796 goto consider_string_end;
5797 }
5798 else
5799 {
5800 IT_BYTEPOS (*it) += it->len;
5801 IT_CHARPOS (*it) += it->cmp_len;
5802 it->method = GET_FROM_BUFFER;
5803 it->object = it->w->buffer;
5804 }
5805 break;
5806
5807 case GET_FROM_C_STRING:
5808 /* Current display element of IT is from a C string. */
5809 IT_BYTEPOS (*it) += it->len;
5810 IT_CHARPOS (*it) += 1;
5811 break;
5812
5813 case GET_FROM_DISPLAY_VECTOR:
5814 /* Current display element of IT is from a display table entry.
5815 Advance in the display table definition. Reset it to null if
5816 end reached, and continue with characters from buffers/
5817 strings. */
5818 ++it->current.dpvec_index;
5819
5820 /* Restore face of the iterator to what they were before the
5821 display vector entry (these entries may contain faces). */
5822 it->face_id = it->saved_face_id;
5823
5824 if (it->dpvec + it->current.dpvec_index == it->dpend)
5825 {
5826 int recheck_faces = it->ellipsis_p;
5827
5828 if (it->s)
5829 it->method = GET_FROM_C_STRING;
5830 else if (STRINGP (it->string))
5831 it->method = GET_FROM_STRING;
5832 else
5833 {
5834 it->method = GET_FROM_BUFFER;
5835 it->object = it->w->buffer;
5836 }
5837
5838 it->dpvec = NULL;
5839 it->current.dpvec_index = -1;
5840
5841 /* Skip over characters which were displayed via IT->dpvec. */
5842 if (it->dpvec_char_len < 0)
5843 reseat_at_next_visible_line_start (it, 1);
5844 else if (it->dpvec_char_len > 0)
5845 {
5846 if (it->method == GET_FROM_STRING
5847 && it->n_overlay_strings > 0)
5848 it->ignore_overlay_strings_at_pos_p = 1;
5849 it->len = it->dpvec_char_len;
5850 set_iterator_to_next (it, reseat_p);
5851 }
5852
5853 /* Maybe recheck faces after display vector */
5854 if (recheck_faces)
5855 it->stop_charpos = IT_CHARPOS (*it);
5856 }
5857 break;
5858
5859 case GET_FROM_STRING:
5860 /* Current display element is a character from a Lisp string. */
5861 xassert (it->s == NULL && STRINGP (it->string));
5862 IT_STRING_BYTEPOS (*it) += it->len;
5863 IT_STRING_CHARPOS (*it) += 1;
5864
5865 consider_string_end:
5866
5867 if (it->current.overlay_string_index >= 0)
5868 {
5869 /* IT->string is an overlay string. Advance to the
5870 next, if there is one. */
5871 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5872 next_overlay_string (it);
5873 }
5874 else
5875 {
5876 /* IT->string is not an overlay string. If we reached
5877 its end, and there is something on IT->stack, proceed
5878 with what is on the stack. This can be either another
5879 string, this time an overlay string, or a buffer. */
5880 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5881 && it->sp > 0)
5882 {
5883 pop_it (it);
5884 if (it->method == GET_FROM_STRING)
5885 goto consider_string_end;
5886 }
5887 }
5888 break;
5889
5890 case GET_FROM_IMAGE:
5891 case GET_FROM_STRETCH:
5892 /* The position etc with which we have to proceed are on
5893 the stack. The position may be at the end of a string,
5894 if the `display' property takes up the whole string. */
5895 xassert (it->sp > 0);
5896 pop_it (it);
5897 if (it->method == GET_FROM_STRING)
5898 goto consider_string_end;
5899 break;
5900
5901 default:
5902 /* There are no other methods defined, so this should be a bug. */
5903 abort ();
5904 }
5905
5906 xassert (it->method != GET_FROM_STRING
5907 || (STRINGP (it->string)
5908 && IT_STRING_CHARPOS (*it) >= 0));
5909 }
5910
5911 /* Load IT's display element fields with information about the next
5912 display element which comes from a display table entry or from the
5913 result of translating a control character to one of the forms `^C'
5914 or `\003'.
5915
5916 IT->dpvec holds the glyphs to return as characters.
5917 IT->saved_face_id holds the face id before the display vector--
5918 it is restored into IT->face_idin set_iterator_to_next. */
5919
5920 static int
5921 next_element_from_display_vector (it)
5922 struct it *it;
5923 {
5924 /* Precondition. */
5925 xassert (it->dpvec && it->current.dpvec_index >= 0);
5926
5927 it->face_id = it->saved_face_id;
5928
5929 if (INTEGERP (*it->dpvec)
5930 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5931 {
5932 GLYPH g;
5933
5934 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5935 it->c = FAST_GLYPH_CHAR (g);
5936 it->len = CHAR_BYTES (it->c);
5937
5938 /* The entry may contain a face id to use. Such a face id is
5939 the id of a Lisp face, not a realized face. A face id of
5940 zero means no face is specified. */
5941 if (it->dpvec_face_id >= 0)
5942 it->face_id = it->dpvec_face_id;
5943 else
5944 {
5945 int lface_id = FAST_GLYPH_FACE (g);
5946 if (lface_id > 0)
5947 it->face_id = merge_faces (it->f, Qt, lface_id,
5948 it->saved_face_id);
5949 }
5950 }
5951 else
5952 /* Display table entry is invalid. Return a space. */
5953 it->c = ' ', it->len = 1;
5954
5955 /* Don't change position and object of the iterator here. They are
5956 still the values of the character that had this display table
5957 entry or was translated, and that's what we want. */
5958 it->what = IT_CHARACTER;
5959 return 1;
5960 }
5961
5962
5963 /* Load IT with the next display element from Lisp string IT->string.
5964 IT->current.string_pos is the current position within the string.
5965 If IT->current.overlay_string_index >= 0, the Lisp string is an
5966 overlay string. */
5967
5968 static int
5969 next_element_from_string (it)
5970 struct it *it;
5971 {
5972 struct text_pos position;
5973
5974 xassert (STRINGP (it->string));
5975 xassert (IT_STRING_CHARPOS (*it) >= 0);
5976 position = it->current.string_pos;
5977
5978 /* Time to check for invisible text? */
5979 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5980 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5981 {
5982 handle_stop (it);
5983
5984 /* Since a handler may have changed IT->method, we must
5985 recurse here. */
5986 return get_next_display_element (it);
5987 }
5988
5989 if (it->current.overlay_string_index >= 0)
5990 {
5991 /* Get the next character from an overlay string. In overlay
5992 strings, There is no field width or padding with spaces to
5993 do. */
5994 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5995 {
5996 it->what = IT_EOB;
5997 return 0;
5998 }
5999 else if (STRING_MULTIBYTE (it->string))
6000 {
6001 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6002 const unsigned char *s = (SDATA (it->string)
6003 + IT_STRING_BYTEPOS (*it));
6004 it->c = string_char_and_length (s, remaining, &it->len);
6005 }
6006 else
6007 {
6008 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6009 it->len = 1;
6010 }
6011 }
6012 else
6013 {
6014 /* Get the next character from a Lisp string that is not an
6015 overlay string. Such strings come from the mode line, for
6016 example. We may have to pad with spaces, or truncate the
6017 string. See also next_element_from_c_string. */
6018 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6019 {
6020 it->what = IT_EOB;
6021 return 0;
6022 }
6023 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6024 {
6025 /* Pad with spaces. */
6026 it->c = ' ', it->len = 1;
6027 CHARPOS (position) = BYTEPOS (position) = -1;
6028 }
6029 else if (STRING_MULTIBYTE (it->string))
6030 {
6031 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6032 const unsigned char *s = (SDATA (it->string)
6033 + IT_STRING_BYTEPOS (*it));
6034 it->c = string_char_and_length (s, maxlen, &it->len);
6035 }
6036 else
6037 {
6038 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6039 it->len = 1;
6040 }
6041 }
6042
6043 /* Record what we have and where it came from. Note that we store a
6044 buffer position in IT->position although it could arguably be a
6045 string position. */
6046 it->what = IT_CHARACTER;
6047 it->object = it->string;
6048 it->position = position;
6049 return 1;
6050 }
6051
6052
6053 /* Load IT with next display element from C string IT->s.
6054 IT->string_nchars is the maximum number of characters to return
6055 from the string. IT->end_charpos may be greater than
6056 IT->string_nchars when this function is called, in which case we
6057 may have to return padding spaces. Value is zero if end of string
6058 reached, including padding spaces. */
6059
6060 static int
6061 next_element_from_c_string (it)
6062 struct it *it;
6063 {
6064 int success_p = 1;
6065
6066 xassert (it->s);
6067 it->what = IT_CHARACTER;
6068 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6069 it->object = Qnil;
6070
6071 /* IT's position can be greater IT->string_nchars in case a field
6072 width or precision has been specified when the iterator was
6073 initialized. */
6074 if (IT_CHARPOS (*it) >= it->end_charpos)
6075 {
6076 /* End of the game. */
6077 it->what = IT_EOB;
6078 success_p = 0;
6079 }
6080 else if (IT_CHARPOS (*it) >= it->string_nchars)
6081 {
6082 /* Pad with spaces. */
6083 it->c = ' ', it->len = 1;
6084 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6085 }
6086 else if (it->multibyte_p)
6087 {
6088 /* Implementation note: The calls to strlen apparently aren't a
6089 performance problem because there is no noticeable performance
6090 difference between Emacs running in unibyte or multibyte mode. */
6091 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6092 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6093 maxlen, &it->len);
6094 }
6095 else
6096 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6097
6098 return success_p;
6099 }
6100
6101
6102 /* Set up IT to return characters from an ellipsis, if appropriate.
6103 The definition of the ellipsis glyphs may come from a display table
6104 entry. This function Fills IT with the first glyph from the
6105 ellipsis if an ellipsis is to be displayed. */
6106
6107 static int
6108 next_element_from_ellipsis (it)
6109 struct it *it;
6110 {
6111 if (it->selective_display_ellipsis_p)
6112 setup_for_ellipsis (it, it->len);
6113 else
6114 {
6115 /* The face at the current position may be different from the
6116 face we find after the invisible text. Remember what it
6117 was in IT->saved_face_id, and signal that it's there by
6118 setting face_before_selective_p. */
6119 it->saved_face_id = it->face_id;
6120 it->method = GET_FROM_BUFFER;
6121 it->object = it->w->buffer;
6122 reseat_at_next_visible_line_start (it, 1);
6123 it->face_before_selective_p = 1;
6124 }
6125
6126 return get_next_display_element (it);
6127 }
6128
6129
6130 /* Deliver an image display element. The iterator IT is already
6131 filled with image information (done in handle_display_prop). Value
6132 is always 1. */
6133
6134
6135 static int
6136 next_element_from_image (it)
6137 struct it *it;
6138 {
6139 it->what = IT_IMAGE;
6140 return 1;
6141 }
6142
6143
6144 /* Fill iterator IT with next display element from a stretch glyph
6145 property. IT->object is the value of the text property. Value is
6146 always 1. */
6147
6148 static int
6149 next_element_from_stretch (it)
6150 struct it *it;
6151 {
6152 it->what = IT_STRETCH;
6153 return 1;
6154 }
6155
6156
6157 /* Load IT with the next display element from current_buffer. Value
6158 is zero if end of buffer reached. IT->stop_charpos is the next
6159 position at which to stop and check for text properties or buffer
6160 end. */
6161
6162 static int
6163 next_element_from_buffer (it)
6164 struct it *it;
6165 {
6166 int success_p = 1;
6167
6168 /* Check this assumption, otherwise, we would never enter the
6169 if-statement, below. */
6170 xassert (IT_CHARPOS (*it) >= BEGV
6171 && IT_CHARPOS (*it) <= it->stop_charpos);
6172
6173 if (IT_CHARPOS (*it) >= it->stop_charpos)
6174 {
6175 if (IT_CHARPOS (*it) >= it->end_charpos)
6176 {
6177 int overlay_strings_follow_p;
6178
6179 /* End of the game, except when overlay strings follow that
6180 haven't been returned yet. */
6181 if (it->overlay_strings_at_end_processed_p)
6182 overlay_strings_follow_p = 0;
6183 else
6184 {
6185 it->overlay_strings_at_end_processed_p = 1;
6186 overlay_strings_follow_p = get_overlay_strings (it, 0);
6187 }
6188
6189 if (overlay_strings_follow_p)
6190 success_p = get_next_display_element (it);
6191 else
6192 {
6193 it->what = IT_EOB;
6194 it->position = it->current.pos;
6195 success_p = 0;
6196 }
6197 }
6198 else
6199 {
6200 handle_stop (it);
6201 return get_next_display_element (it);
6202 }
6203 }
6204 else
6205 {
6206 /* No face changes, overlays etc. in sight, so just return a
6207 character from current_buffer. */
6208 unsigned char *p;
6209
6210 /* Maybe run the redisplay end trigger hook. Performance note:
6211 This doesn't seem to cost measurable time. */
6212 if (it->redisplay_end_trigger_charpos
6213 && it->glyph_row
6214 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6215 run_redisplay_end_trigger_hook (it);
6216
6217 /* Get the next character, maybe multibyte. */
6218 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6219 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6220 {
6221 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6222 - IT_BYTEPOS (*it));
6223 it->c = string_char_and_length (p, maxlen, &it->len);
6224 }
6225 else
6226 it->c = *p, it->len = 1;
6227
6228 /* Record what we have and where it came from. */
6229 it->what = IT_CHARACTER;;
6230 it->object = it->w->buffer;
6231 it->position = it->current.pos;
6232
6233 /* Normally we return the character found above, except when we
6234 really want to return an ellipsis for selective display. */
6235 if (it->selective)
6236 {
6237 if (it->c == '\n')
6238 {
6239 /* A value of selective > 0 means hide lines indented more
6240 than that number of columns. */
6241 if (it->selective > 0
6242 && IT_CHARPOS (*it) + 1 < ZV
6243 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6244 IT_BYTEPOS (*it) + 1,
6245 (double) it->selective)) /* iftc */
6246 {
6247 success_p = next_element_from_ellipsis (it);
6248 it->dpvec_char_len = -1;
6249 }
6250 }
6251 else if (it->c == '\r' && it->selective == -1)
6252 {
6253 /* A value of selective == -1 means that everything from the
6254 CR to the end of the line is invisible, with maybe an
6255 ellipsis displayed for it. */
6256 success_p = next_element_from_ellipsis (it);
6257 it->dpvec_char_len = -1;
6258 }
6259 }
6260 }
6261
6262 /* Value is zero if end of buffer reached. */
6263 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6264 return success_p;
6265 }
6266
6267
6268 /* Run the redisplay end trigger hook for IT. */
6269
6270 static void
6271 run_redisplay_end_trigger_hook (it)
6272 struct it *it;
6273 {
6274 Lisp_Object args[3];
6275
6276 /* IT->glyph_row should be non-null, i.e. we should be actually
6277 displaying something, or otherwise we should not run the hook. */
6278 xassert (it->glyph_row);
6279
6280 /* Set up hook arguments. */
6281 args[0] = Qredisplay_end_trigger_functions;
6282 args[1] = it->window;
6283 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6284 it->redisplay_end_trigger_charpos = 0;
6285
6286 /* Since we are *trying* to run these functions, don't try to run
6287 them again, even if they get an error. */
6288 it->w->redisplay_end_trigger = Qnil;
6289 Frun_hook_with_args (3, args);
6290
6291 /* Notice if it changed the face of the character we are on. */
6292 handle_face_prop (it);
6293 }
6294
6295
6296 /* Deliver a composition display element. The iterator IT is already
6297 filled with composition information (done in
6298 handle_composition_prop). Value is always 1. */
6299
6300 static int
6301 next_element_from_composition (it)
6302 struct it *it;
6303 {
6304 it->what = IT_COMPOSITION;
6305 it->position = (STRINGP (it->string)
6306 ? it->current.string_pos
6307 : it->current.pos);
6308 if (STRINGP (it->string))
6309 it->object = it->string;
6310 else
6311 it->object = it->w->buffer;
6312 return 1;
6313 }
6314
6315
6316 \f
6317 /***********************************************************************
6318 Moving an iterator without producing glyphs
6319 ***********************************************************************/
6320
6321 /* Check if iterator is at a position corresponding to a valid buffer
6322 position after some move_it_ call. */
6323
6324 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6325 ((it)->method == GET_FROM_STRING \
6326 ? IT_STRING_CHARPOS (*it) == 0 \
6327 : 1)
6328
6329
6330 /* Move iterator IT to a specified buffer or X position within one
6331 line on the display without producing glyphs.
6332
6333 OP should be a bit mask including some or all of these bits:
6334 MOVE_TO_X: Stop on reaching x-position TO_X.
6335 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6336 Regardless of OP's value, stop in reaching the end of the display line.
6337
6338 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6339 This means, in particular, that TO_X includes window's horizontal
6340 scroll amount.
6341
6342 The return value has several possible values that
6343 say what condition caused the scan to stop:
6344
6345 MOVE_POS_MATCH_OR_ZV
6346 - when TO_POS or ZV was reached.
6347
6348 MOVE_X_REACHED
6349 -when TO_X was reached before TO_POS or ZV were reached.
6350
6351 MOVE_LINE_CONTINUED
6352 - when we reached the end of the display area and the line must
6353 be continued.
6354
6355 MOVE_LINE_TRUNCATED
6356 - when we reached the end of the display area and the line is
6357 truncated.
6358
6359 MOVE_NEWLINE_OR_CR
6360 - when we stopped at a line end, i.e. a newline or a CR and selective
6361 display is on. */
6362
6363 static enum move_it_result
6364 move_it_in_display_line_to (it, to_charpos, to_x, op)
6365 struct it *it;
6366 int to_charpos, to_x, op;
6367 {
6368 enum move_it_result result = MOVE_UNDEFINED;
6369 struct glyph_row *saved_glyph_row;
6370
6371 /* Don't produce glyphs in produce_glyphs. */
6372 saved_glyph_row = it->glyph_row;
6373 it->glyph_row = NULL;
6374
6375 #define BUFFER_POS_REACHED_P() \
6376 ((op & MOVE_TO_POS) != 0 \
6377 && BUFFERP (it->object) \
6378 && IT_CHARPOS (*it) >= to_charpos \
6379 && (it->method == GET_FROM_BUFFER \
6380 || (it->method == GET_FROM_DISPLAY_VECTOR \
6381 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6382
6383
6384 while (1)
6385 {
6386 int x, i, ascent = 0, descent = 0;
6387
6388 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6389 if ((op & MOVE_TO_POS) != 0
6390 && BUFFERP (it->object)
6391 && it->method == GET_FROM_BUFFER
6392 && IT_CHARPOS (*it) > to_charpos)
6393 {
6394 result = MOVE_POS_MATCH_OR_ZV;
6395 break;
6396 }
6397
6398 /* Stop when ZV reached.
6399 We used to stop here when TO_CHARPOS reached as well, but that is
6400 too soon if this glyph does not fit on this line. So we handle it
6401 explicitly below. */
6402 if (!get_next_display_element (it)
6403 || (it->truncate_lines_p
6404 && BUFFER_POS_REACHED_P ()))
6405 {
6406 result = MOVE_POS_MATCH_OR_ZV;
6407 break;
6408 }
6409
6410 /* The call to produce_glyphs will get the metrics of the
6411 display element IT is loaded with. We record in x the
6412 x-position before this display element in case it does not
6413 fit on the line. */
6414 x = it->current_x;
6415
6416 /* Remember the line height so far in case the next element doesn't
6417 fit on the line. */
6418 if (!it->truncate_lines_p)
6419 {
6420 ascent = it->max_ascent;
6421 descent = it->max_descent;
6422 }
6423
6424 PRODUCE_GLYPHS (it);
6425
6426 if (it->area != TEXT_AREA)
6427 {
6428 set_iterator_to_next (it, 1);
6429 continue;
6430 }
6431
6432 /* The number of glyphs we get back in IT->nglyphs will normally
6433 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6434 character on a terminal frame, or (iii) a line end. For the
6435 second case, IT->nglyphs - 1 padding glyphs will be present
6436 (on X frames, there is only one glyph produced for a
6437 composite character.
6438
6439 The behavior implemented below means, for continuation lines,
6440 that as many spaces of a TAB as fit on the current line are
6441 displayed there. For terminal frames, as many glyphs of a
6442 multi-glyph character are displayed in the current line, too.
6443 This is what the old redisplay code did, and we keep it that
6444 way. Under X, the whole shape of a complex character must
6445 fit on the line or it will be completely displayed in the
6446 next line.
6447
6448 Note that both for tabs and padding glyphs, all glyphs have
6449 the same width. */
6450 if (it->nglyphs)
6451 {
6452 /* More than one glyph or glyph doesn't fit on line. All
6453 glyphs have the same width. */
6454 int single_glyph_width = it->pixel_width / it->nglyphs;
6455 int new_x;
6456 int x_before_this_char = x;
6457 int hpos_before_this_char = it->hpos;
6458
6459 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6460 {
6461 new_x = x + single_glyph_width;
6462
6463 /* We want to leave anything reaching TO_X to the caller. */
6464 if ((op & MOVE_TO_X) && new_x > to_x)
6465 {
6466 if (BUFFER_POS_REACHED_P ())
6467 goto buffer_pos_reached;
6468 it->current_x = x;
6469 result = MOVE_X_REACHED;
6470 break;
6471 }
6472 else if (/* Lines are continued. */
6473 !it->truncate_lines_p
6474 && (/* And glyph doesn't fit on the line. */
6475 new_x > it->last_visible_x
6476 /* Or it fits exactly and we're on a window
6477 system frame. */
6478 || (new_x == it->last_visible_x
6479 && FRAME_WINDOW_P (it->f))))
6480 {
6481 if (/* IT->hpos == 0 means the very first glyph
6482 doesn't fit on the line, e.g. a wide image. */
6483 it->hpos == 0
6484 || (new_x == it->last_visible_x
6485 && FRAME_WINDOW_P (it->f)))
6486 {
6487 ++it->hpos;
6488 it->current_x = new_x;
6489
6490 /* The character's last glyph just barely fits
6491 in this row. */
6492 if (i == it->nglyphs - 1)
6493 {
6494 /* If this is the destination position,
6495 return a position *before* it in this row,
6496 now that we know it fits in this row. */
6497 if (BUFFER_POS_REACHED_P ())
6498 {
6499 it->hpos = hpos_before_this_char;
6500 it->current_x = x_before_this_char;
6501 result = MOVE_POS_MATCH_OR_ZV;
6502 break;
6503 }
6504
6505 set_iterator_to_next (it, 1);
6506 #ifdef HAVE_WINDOW_SYSTEM
6507 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6508 {
6509 if (!get_next_display_element (it))
6510 {
6511 result = MOVE_POS_MATCH_OR_ZV;
6512 break;
6513 }
6514 if (BUFFER_POS_REACHED_P ())
6515 {
6516 if (ITERATOR_AT_END_OF_LINE_P (it))
6517 result = MOVE_POS_MATCH_OR_ZV;
6518 else
6519 result = MOVE_LINE_CONTINUED;
6520 break;
6521 }
6522 if (ITERATOR_AT_END_OF_LINE_P (it))
6523 {
6524 result = MOVE_NEWLINE_OR_CR;
6525 break;
6526 }
6527 }
6528 #endif /* HAVE_WINDOW_SYSTEM */
6529 }
6530 }
6531 else
6532 {
6533 it->current_x = x;
6534 it->max_ascent = ascent;
6535 it->max_descent = descent;
6536 }
6537
6538 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6539 IT_CHARPOS (*it)));
6540 result = MOVE_LINE_CONTINUED;
6541 break;
6542 }
6543 else if (BUFFER_POS_REACHED_P ())
6544 goto buffer_pos_reached;
6545 else if (new_x > it->first_visible_x)
6546 {
6547 /* Glyph is visible. Increment number of glyphs that
6548 would be displayed. */
6549 ++it->hpos;
6550 }
6551 else
6552 {
6553 /* Glyph is completely off the left margin of the display
6554 area. Nothing to do. */
6555 }
6556 }
6557
6558 if (result != MOVE_UNDEFINED)
6559 break;
6560 }
6561 else if (BUFFER_POS_REACHED_P ())
6562 {
6563 buffer_pos_reached:
6564 it->current_x = x;
6565 it->max_ascent = ascent;
6566 it->max_descent = descent;
6567 result = MOVE_POS_MATCH_OR_ZV;
6568 break;
6569 }
6570 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6571 {
6572 /* Stop when TO_X specified and reached. This check is
6573 necessary here because of lines consisting of a line end,
6574 only. The line end will not produce any glyphs and we
6575 would never get MOVE_X_REACHED. */
6576 xassert (it->nglyphs == 0);
6577 result = MOVE_X_REACHED;
6578 break;
6579 }
6580
6581 /* Is this a line end? If yes, we're done. */
6582 if (ITERATOR_AT_END_OF_LINE_P (it))
6583 {
6584 result = MOVE_NEWLINE_OR_CR;
6585 break;
6586 }
6587
6588 /* The current display element has been consumed. Advance
6589 to the next. */
6590 set_iterator_to_next (it, 1);
6591
6592 /* Stop if lines are truncated and IT's current x-position is
6593 past the right edge of the window now. */
6594 if (it->truncate_lines_p
6595 && it->current_x >= it->last_visible_x)
6596 {
6597 #ifdef HAVE_WINDOW_SYSTEM
6598 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6599 {
6600 if (!get_next_display_element (it)
6601 || BUFFER_POS_REACHED_P ())
6602 {
6603 result = MOVE_POS_MATCH_OR_ZV;
6604 break;
6605 }
6606 if (ITERATOR_AT_END_OF_LINE_P (it))
6607 {
6608 result = MOVE_NEWLINE_OR_CR;
6609 break;
6610 }
6611 }
6612 #endif /* HAVE_WINDOW_SYSTEM */
6613 result = MOVE_LINE_TRUNCATED;
6614 break;
6615 }
6616 }
6617
6618 #undef BUFFER_POS_REACHED_P
6619
6620 /* Restore the iterator settings altered at the beginning of this
6621 function. */
6622 it->glyph_row = saved_glyph_row;
6623 return result;
6624 }
6625
6626
6627 /* Move IT forward until it satisfies one or more of the criteria in
6628 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6629
6630 OP is a bit-mask that specifies where to stop, and in particular,
6631 which of those four position arguments makes a difference. See the
6632 description of enum move_operation_enum.
6633
6634 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6635 screen line, this function will set IT to the next position >
6636 TO_CHARPOS. */
6637
6638 void
6639 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6640 struct it *it;
6641 int to_charpos, to_x, to_y, to_vpos;
6642 int op;
6643 {
6644 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6645 int line_height;
6646 int reached = 0;
6647
6648 for (;;)
6649 {
6650 if (op & MOVE_TO_VPOS)
6651 {
6652 /* If no TO_CHARPOS and no TO_X specified, stop at the
6653 start of the line TO_VPOS. */
6654 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6655 {
6656 if (it->vpos == to_vpos)
6657 {
6658 reached = 1;
6659 break;
6660 }
6661 else
6662 skip = move_it_in_display_line_to (it, -1, -1, 0);
6663 }
6664 else
6665 {
6666 /* TO_VPOS >= 0 means stop at TO_X in the line at
6667 TO_VPOS, or at TO_POS, whichever comes first. */
6668 if (it->vpos == to_vpos)
6669 {
6670 reached = 2;
6671 break;
6672 }
6673
6674 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6675
6676 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6677 {
6678 reached = 3;
6679 break;
6680 }
6681 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6682 {
6683 /* We have reached TO_X but not in the line we want. */
6684 skip = move_it_in_display_line_to (it, to_charpos,
6685 -1, MOVE_TO_POS);
6686 if (skip == MOVE_POS_MATCH_OR_ZV)
6687 {
6688 reached = 4;
6689 break;
6690 }
6691 }
6692 }
6693 }
6694 else if (op & MOVE_TO_Y)
6695 {
6696 struct it it_backup;
6697
6698 /* TO_Y specified means stop at TO_X in the line containing
6699 TO_Y---or at TO_CHARPOS if this is reached first. The
6700 problem is that we can't really tell whether the line
6701 contains TO_Y before we have completely scanned it, and
6702 this may skip past TO_X. What we do is to first scan to
6703 TO_X.
6704
6705 If TO_X is not specified, use a TO_X of zero. The reason
6706 is to make the outcome of this function more predictable.
6707 If we didn't use TO_X == 0, we would stop at the end of
6708 the line which is probably not what a caller would expect
6709 to happen. */
6710 skip = move_it_in_display_line_to (it, to_charpos,
6711 ((op & MOVE_TO_X)
6712 ? to_x : 0),
6713 (MOVE_TO_X
6714 | (op & MOVE_TO_POS)));
6715
6716 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6717 if (skip == MOVE_POS_MATCH_OR_ZV)
6718 {
6719 reached = 5;
6720 break;
6721 }
6722
6723 /* If TO_X was reached, we would like to know whether TO_Y
6724 is in the line. This can only be said if we know the
6725 total line height which requires us to scan the rest of
6726 the line. */
6727 if (skip == MOVE_X_REACHED)
6728 {
6729 it_backup = *it;
6730 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6731 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6732 op & MOVE_TO_POS);
6733 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6734 }
6735
6736 /* Now, decide whether TO_Y is in this line. */
6737 line_height = it->max_ascent + it->max_descent;
6738 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6739
6740 if (to_y >= it->current_y
6741 && to_y < it->current_y + line_height)
6742 {
6743 if (skip == MOVE_X_REACHED)
6744 /* If TO_Y is in this line and TO_X was reached above,
6745 we scanned too far. We have to restore IT's settings
6746 to the ones before skipping. */
6747 *it = it_backup;
6748 reached = 6;
6749 }
6750 else if (skip == MOVE_X_REACHED)
6751 {
6752 skip = skip2;
6753 if (skip == MOVE_POS_MATCH_OR_ZV)
6754 reached = 7;
6755 }
6756
6757 if (reached)
6758 break;
6759 }
6760 else
6761 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6762
6763 switch (skip)
6764 {
6765 case MOVE_POS_MATCH_OR_ZV:
6766 reached = 8;
6767 goto out;
6768
6769 case MOVE_NEWLINE_OR_CR:
6770 set_iterator_to_next (it, 1);
6771 it->continuation_lines_width = 0;
6772 break;
6773
6774 case MOVE_LINE_TRUNCATED:
6775 it->continuation_lines_width = 0;
6776 reseat_at_next_visible_line_start (it, 0);
6777 if ((op & MOVE_TO_POS) != 0
6778 && IT_CHARPOS (*it) > to_charpos)
6779 {
6780 reached = 9;
6781 goto out;
6782 }
6783 break;
6784
6785 case MOVE_LINE_CONTINUED:
6786 it->continuation_lines_width += it->current_x;
6787 break;
6788
6789 default:
6790 abort ();
6791 }
6792
6793 /* Reset/increment for the next run. */
6794 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6795 it->current_x = it->hpos = 0;
6796 it->current_y += it->max_ascent + it->max_descent;
6797 ++it->vpos;
6798 last_height = it->max_ascent + it->max_descent;
6799 last_max_ascent = it->max_ascent;
6800 it->max_ascent = it->max_descent = 0;
6801 }
6802
6803 out:
6804
6805 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6806 }
6807
6808
6809 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6810
6811 If DY > 0, move IT backward at least that many pixels. DY = 0
6812 means move IT backward to the preceding line start or BEGV. This
6813 function may move over more than DY pixels if IT->current_y - DY
6814 ends up in the middle of a line; in this case IT->current_y will be
6815 set to the top of the line moved to. */
6816
6817 void
6818 move_it_vertically_backward (it, dy)
6819 struct it *it;
6820 int dy;
6821 {
6822 int nlines, h;
6823 struct it it2, it3;
6824 int start_pos;
6825
6826 move_further_back:
6827 xassert (dy >= 0);
6828
6829 start_pos = IT_CHARPOS (*it);
6830
6831 /* Estimate how many newlines we must move back. */
6832 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6833
6834 /* Set the iterator's position that many lines back. */
6835 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6836 back_to_previous_visible_line_start (it);
6837
6838 /* Reseat the iterator here. When moving backward, we don't want
6839 reseat to skip forward over invisible text, set up the iterator
6840 to deliver from overlay strings at the new position etc. So,
6841 use reseat_1 here. */
6842 reseat_1 (it, it->current.pos, 1);
6843
6844 /* We are now surely at a line start. */
6845 it->current_x = it->hpos = 0;
6846 it->continuation_lines_width = 0;
6847
6848 /* Move forward and see what y-distance we moved. First move to the
6849 start of the next line so that we get its height. We need this
6850 height to be able to tell whether we reached the specified
6851 y-distance. */
6852 it2 = *it;
6853 it2.max_ascent = it2.max_descent = 0;
6854 do
6855 {
6856 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6857 MOVE_TO_POS | MOVE_TO_VPOS);
6858 }
6859 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6860 xassert (IT_CHARPOS (*it) >= BEGV);
6861 it3 = it2;
6862
6863 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6864 xassert (IT_CHARPOS (*it) >= BEGV);
6865 /* H is the actual vertical distance from the position in *IT
6866 and the starting position. */
6867 h = it2.current_y - it->current_y;
6868 /* NLINES is the distance in number of lines. */
6869 nlines = it2.vpos - it->vpos;
6870
6871 /* Correct IT's y and vpos position
6872 so that they are relative to the starting point. */
6873 it->vpos -= nlines;
6874 it->current_y -= h;
6875
6876 if (dy == 0)
6877 {
6878 /* DY == 0 means move to the start of the screen line. The
6879 value of nlines is > 0 if continuation lines were involved. */
6880 if (nlines > 0)
6881 move_it_by_lines (it, nlines, 1);
6882 #if 0
6883 /* I think this assert is bogus if buffer contains
6884 invisible text or images. KFS. */
6885 xassert (IT_CHARPOS (*it) <= start_pos);
6886 #endif
6887 }
6888 else
6889 {
6890 /* The y-position we try to reach, relative to *IT.
6891 Note that H has been subtracted in front of the if-statement. */
6892 int target_y = it->current_y + h - dy;
6893 int y0 = it3.current_y;
6894 int y1 = line_bottom_y (&it3);
6895 int line_height = y1 - y0;
6896
6897 /* If we did not reach target_y, try to move further backward if
6898 we can. If we moved too far backward, try to move forward. */
6899 if (target_y < it->current_y
6900 /* This is heuristic. In a window that's 3 lines high, with
6901 a line height of 13 pixels each, recentering with point
6902 on the bottom line will try to move -39/2 = 19 pixels
6903 backward. Try to avoid moving into the first line. */
6904 && (it->current_y - target_y
6905 > min (window_box_height (it->w), line_height * 2 / 3))
6906 && IT_CHARPOS (*it) > BEGV)
6907 {
6908 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6909 target_y - it->current_y));
6910 dy = it->current_y - target_y;
6911 goto move_further_back;
6912 }
6913 else if (target_y >= it->current_y + line_height
6914 && IT_CHARPOS (*it) < ZV)
6915 {
6916 /* Should move forward by at least one line, maybe more.
6917
6918 Note: Calling move_it_by_lines can be expensive on
6919 terminal frames, where compute_motion is used (via
6920 vmotion) to do the job, when there are very long lines
6921 and truncate-lines is nil. That's the reason for
6922 treating terminal frames specially here. */
6923
6924 if (!FRAME_WINDOW_P (it->f))
6925 move_it_vertically (it, target_y - (it->current_y + line_height));
6926 else
6927 {
6928 do
6929 {
6930 move_it_by_lines (it, 1, 1);
6931 }
6932 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6933 }
6934
6935 #if 0
6936 /* I think this assert is bogus if buffer contains
6937 invisible text or images. KFS. */
6938 xassert (IT_CHARPOS (*it) >= BEGV);
6939 #endif
6940 }
6941 }
6942 }
6943
6944
6945 /* Move IT by a specified amount of pixel lines DY. DY negative means
6946 move backwards. DY = 0 means move to start of screen line. At the
6947 end, IT will be on the start of a screen line. */
6948
6949 void
6950 move_it_vertically (it, dy)
6951 struct it *it;
6952 int dy;
6953 {
6954 if (dy <= 0)
6955 move_it_vertically_backward (it, -dy);
6956 else
6957 {
6958 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6959 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6960 MOVE_TO_POS | MOVE_TO_Y);
6961 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6962
6963 /* If buffer ends in ZV without a newline, move to the start of
6964 the line to satisfy the post-condition. */
6965 if (IT_CHARPOS (*it) == ZV
6966 && ZV > BEGV
6967 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6968 move_it_by_lines (it, 0, 0);
6969 }
6970 }
6971
6972
6973 /* Move iterator IT past the end of the text line it is in. */
6974
6975 void
6976 move_it_past_eol (it)
6977 struct it *it;
6978 {
6979 enum move_it_result rc;
6980
6981 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6982 if (rc == MOVE_NEWLINE_OR_CR)
6983 set_iterator_to_next (it, 0);
6984 }
6985
6986
6987 #if 0 /* Currently not used. */
6988
6989 /* Return non-zero if some text between buffer positions START_CHARPOS
6990 and END_CHARPOS is invisible. IT->window is the window for text
6991 property lookup. */
6992
6993 static int
6994 invisible_text_between_p (it, start_charpos, end_charpos)
6995 struct it *it;
6996 int start_charpos, end_charpos;
6997 {
6998 Lisp_Object prop, limit;
6999 int invisible_found_p;
7000
7001 xassert (it != NULL && start_charpos <= end_charpos);
7002
7003 /* Is text at START invisible? */
7004 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7005 it->window);
7006 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7007 invisible_found_p = 1;
7008 else
7009 {
7010 limit = Fnext_single_char_property_change (make_number (start_charpos),
7011 Qinvisible, Qnil,
7012 make_number (end_charpos));
7013 invisible_found_p = XFASTINT (limit) < end_charpos;
7014 }
7015
7016 return invisible_found_p;
7017 }
7018
7019 #endif /* 0 */
7020
7021
7022 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7023 negative means move up. DVPOS == 0 means move to the start of the
7024 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7025 NEED_Y_P is zero, IT->current_y will be left unchanged.
7026
7027 Further optimization ideas: If we would know that IT->f doesn't use
7028 a face with proportional font, we could be faster for
7029 truncate-lines nil. */
7030
7031 void
7032 move_it_by_lines (it, dvpos, need_y_p)
7033 struct it *it;
7034 int dvpos, need_y_p;
7035 {
7036 struct position pos;
7037
7038 if (!FRAME_WINDOW_P (it->f))
7039 {
7040 struct text_pos textpos;
7041
7042 /* We can use vmotion on frames without proportional fonts. */
7043 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7044 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7045 reseat (it, textpos, 1);
7046 it->vpos += pos.vpos;
7047 it->current_y += pos.vpos;
7048 }
7049 else if (dvpos == 0)
7050 {
7051 /* DVPOS == 0 means move to the start of the screen line. */
7052 move_it_vertically_backward (it, 0);
7053 xassert (it->current_x == 0 && it->hpos == 0);
7054 /* Let next call to line_bottom_y calculate real line height */
7055 last_height = 0;
7056 }
7057 else if (dvpos > 0)
7058 {
7059 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7060 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7061 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7062 }
7063 else
7064 {
7065 struct it it2;
7066 int start_charpos, i;
7067
7068 /* Start at the beginning of the screen line containing IT's
7069 position. This may actually move vertically backwards,
7070 in case of overlays, so adjust dvpos accordingly. */
7071 dvpos += it->vpos;
7072 move_it_vertically_backward (it, 0);
7073 dvpos -= it->vpos;
7074
7075 /* Go back -DVPOS visible lines and reseat the iterator there. */
7076 start_charpos = IT_CHARPOS (*it);
7077 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7078 back_to_previous_visible_line_start (it);
7079 reseat (it, it->current.pos, 1);
7080
7081 /* Move further back if we end up in a string or an image. */
7082 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7083 {
7084 /* First try to move to start of display line. */
7085 dvpos += it->vpos;
7086 move_it_vertically_backward (it, 0);
7087 dvpos -= it->vpos;
7088 if (IT_POS_VALID_AFTER_MOVE_P (it))
7089 break;
7090 /* If start of line is still in string or image,
7091 move further back. */
7092 back_to_previous_visible_line_start (it);
7093 reseat (it, it->current.pos, 1);
7094 dvpos--;
7095 }
7096
7097 it->current_x = it->hpos = 0;
7098
7099 /* Above call may have moved too far if continuation lines
7100 are involved. Scan forward and see if it did. */
7101 it2 = *it;
7102 it2.vpos = it2.current_y = 0;
7103 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7104 it->vpos -= it2.vpos;
7105 it->current_y -= it2.current_y;
7106 it->current_x = it->hpos = 0;
7107
7108 /* If we moved too far back, move IT some lines forward. */
7109 if (it2.vpos > -dvpos)
7110 {
7111 int delta = it2.vpos + dvpos;
7112 it2 = *it;
7113 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7114 /* Move back again if we got too far ahead. */
7115 if (IT_CHARPOS (*it) >= start_charpos)
7116 *it = it2;
7117 }
7118 }
7119 }
7120
7121 /* Return 1 if IT points into the middle of a display vector. */
7122
7123 int
7124 in_display_vector_p (it)
7125 struct it *it;
7126 {
7127 return (it->method == GET_FROM_DISPLAY_VECTOR
7128 && it->current.dpvec_index > 0
7129 && it->dpvec + it->current.dpvec_index != it->dpend);
7130 }
7131
7132 \f
7133 /***********************************************************************
7134 Messages
7135 ***********************************************************************/
7136
7137
7138 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7139 to *Messages*. */
7140
7141 void
7142 add_to_log (format, arg1, arg2)
7143 char *format;
7144 Lisp_Object arg1, arg2;
7145 {
7146 Lisp_Object args[3];
7147 Lisp_Object msg, fmt;
7148 char *buffer;
7149 int len;
7150 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7151 USE_SAFE_ALLOCA;
7152
7153 /* Do nothing if called asynchronously. Inserting text into
7154 a buffer may call after-change-functions and alike and
7155 that would means running Lisp asynchronously. */
7156 if (handling_signal)
7157 return;
7158
7159 fmt = msg = Qnil;
7160 GCPRO4 (fmt, msg, arg1, arg2);
7161
7162 args[0] = fmt = build_string (format);
7163 args[1] = arg1;
7164 args[2] = arg2;
7165 msg = Fformat (3, args);
7166
7167 len = SBYTES (msg) + 1;
7168 SAFE_ALLOCA (buffer, char *, len);
7169 bcopy (SDATA (msg), buffer, len);
7170
7171 message_dolog (buffer, len - 1, 1, 0);
7172 SAFE_FREE ();
7173
7174 UNGCPRO;
7175 }
7176
7177
7178 /* Output a newline in the *Messages* buffer if "needs" one. */
7179
7180 void
7181 message_log_maybe_newline ()
7182 {
7183 if (message_log_need_newline)
7184 message_dolog ("", 0, 1, 0);
7185 }
7186
7187
7188 /* Add a string M of length NBYTES to the message log, optionally
7189 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7190 nonzero, means interpret the contents of M as multibyte. This
7191 function calls low-level routines in order to bypass text property
7192 hooks, etc. which might not be safe to run.
7193
7194 This may GC (insert may run before/after change hooks),
7195 so the buffer M must NOT point to a Lisp string. */
7196
7197 void
7198 message_dolog (m, nbytes, nlflag, multibyte)
7199 const char *m;
7200 int nbytes, nlflag, multibyte;
7201 {
7202 if (!NILP (Vmemory_full))
7203 return;
7204
7205 if (!NILP (Vmessage_log_max))
7206 {
7207 struct buffer *oldbuf;
7208 Lisp_Object oldpoint, oldbegv, oldzv;
7209 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7210 int point_at_end = 0;
7211 int zv_at_end = 0;
7212 Lisp_Object old_deactivate_mark, tem;
7213 struct gcpro gcpro1;
7214
7215 old_deactivate_mark = Vdeactivate_mark;
7216 oldbuf = current_buffer;
7217 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7218 current_buffer->undo_list = Qt;
7219
7220 oldpoint = message_dolog_marker1;
7221 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7222 oldbegv = message_dolog_marker2;
7223 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7224 oldzv = message_dolog_marker3;
7225 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7226 GCPRO1 (old_deactivate_mark);
7227
7228 if (PT == Z)
7229 point_at_end = 1;
7230 if (ZV == Z)
7231 zv_at_end = 1;
7232
7233 BEGV = BEG;
7234 BEGV_BYTE = BEG_BYTE;
7235 ZV = Z;
7236 ZV_BYTE = Z_BYTE;
7237 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7238
7239 /* Insert the string--maybe converting multibyte to single byte
7240 or vice versa, so that all the text fits the buffer. */
7241 if (multibyte
7242 && NILP (current_buffer->enable_multibyte_characters))
7243 {
7244 int i, c, char_bytes;
7245 unsigned char work[1];
7246
7247 /* Convert a multibyte string to single-byte
7248 for the *Message* buffer. */
7249 for (i = 0; i < nbytes; i += char_bytes)
7250 {
7251 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7252 work[0] = (SINGLE_BYTE_CHAR_P (c)
7253 ? c
7254 : multibyte_char_to_unibyte (c, Qnil));
7255 insert_1_both (work, 1, 1, 1, 0, 0);
7256 }
7257 }
7258 else if (! multibyte
7259 && ! NILP (current_buffer->enable_multibyte_characters))
7260 {
7261 int i, c, char_bytes;
7262 unsigned char *msg = (unsigned char *) m;
7263 unsigned char str[MAX_MULTIBYTE_LENGTH];
7264 /* Convert a single-byte string to multibyte
7265 for the *Message* buffer. */
7266 for (i = 0; i < nbytes; i++)
7267 {
7268 c = unibyte_char_to_multibyte (msg[i]);
7269 char_bytes = CHAR_STRING (c, str);
7270 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7271 }
7272 }
7273 else if (nbytes)
7274 insert_1 (m, nbytes, 1, 0, 0);
7275
7276 if (nlflag)
7277 {
7278 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7279 insert_1 ("\n", 1, 1, 0, 0);
7280
7281 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7282 this_bol = PT;
7283 this_bol_byte = PT_BYTE;
7284
7285 /* See if this line duplicates the previous one.
7286 If so, combine duplicates. */
7287 if (this_bol > BEG)
7288 {
7289 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7290 prev_bol = PT;
7291 prev_bol_byte = PT_BYTE;
7292
7293 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7294 this_bol, this_bol_byte);
7295 if (dup)
7296 {
7297 del_range_both (prev_bol, prev_bol_byte,
7298 this_bol, this_bol_byte, 0);
7299 if (dup > 1)
7300 {
7301 char dupstr[40];
7302 int duplen;
7303
7304 /* If you change this format, don't forget to also
7305 change message_log_check_duplicate. */
7306 sprintf (dupstr, " [%d times]", dup);
7307 duplen = strlen (dupstr);
7308 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7309 insert_1 (dupstr, duplen, 1, 0, 1);
7310 }
7311 }
7312 }
7313
7314 /* If we have more than the desired maximum number of lines
7315 in the *Messages* buffer now, delete the oldest ones.
7316 This is safe because we don't have undo in this buffer. */
7317
7318 if (NATNUMP (Vmessage_log_max))
7319 {
7320 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7321 -XFASTINT (Vmessage_log_max) - 1, 0);
7322 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7323 }
7324 }
7325 BEGV = XMARKER (oldbegv)->charpos;
7326 BEGV_BYTE = marker_byte_position (oldbegv);
7327
7328 if (zv_at_end)
7329 {
7330 ZV = Z;
7331 ZV_BYTE = Z_BYTE;
7332 }
7333 else
7334 {
7335 ZV = XMARKER (oldzv)->charpos;
7336 ZV_BYTE = marker_byte_position (oldzv);
7337 }
7338
7339 if (point_at_end)
7340 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7341 else
7342 /* We can't do Fgoto_char (oldpoint) because it will run some
7343 Lisp code. */
7344 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7345 XMARKER (oldpoint)->bytepos);
7346
7347 UNGCPRO;
7348 unchain_marker (XMARKER (oldpoint));
7349 unchain_marker (XMARKER (oldbegv));
7350 unchain_marker (XMARKER (oldzv));
7351
7352 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7353 set_buffer_internal (oldbuf);
7354 if (NILP (tem))
7355 windows_or_buffers_changed = old_windows_or_buffers_changed;
7356 message_log_need_newline = !nlflag;
7357 Vdeactivate_mark = old_deactivate_mark;
7358 }
7359 }
7360
7361
7362 /* We are at the end of the buffer after just having inserted a newline.
7363 (Note: We depend on the fact we won't be crossing the gap.)
7364 Check to see if the most recent message looks a lot like the previous one.
7365 Return 0 if different, 1 if the new one should just replace it, or a
7366 value N > 1 if we should also append " [N times]". */
7367
7368 static int
7369 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7370 int prev_bol, this_bol;
7371 int prev_bol_byte, this_bol_byte;
7372 {
7373 int i;
7374 int len = Z_BYTE - 1 - this_bol_byte;
7375 int seen_dots = 0;
7376 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7377 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7378
7379 for (i = 0; i < len; i++)
7380 {
7381 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7382 seen_dots = 1;
7383 if (p1[i] != p2[i])
7384 return seen_dots;
7385 }
7386 p1 += len;
7387 if (*p1 == '\n')
7388 return 2;
7389 if (*p1++ == ' ' && *p1++ == '[')
7390 {
7391 int n = 0;
7392 while (*p1 >= '0' && *p1 <= '9')
7393 n = n * 10 + *p1++ - '0';
7394 if (strncmp (p1, " times]\n", 8) == 0)
7395 return n+1;
7396 }
7397 return 0;
7398 }
7399 \f
7400
7401 /* Display an echo area message M with a specified length of NBYTES
7402 bytes. The string may include null characters. If M is 0, clear
7403 out any existing message, and let the mini-buffer text show
7404 through.
7405
7406 This may GC, so the buffer M must NOT point to a Lisp string. */
7407
7408 void
7409 message2 (m, nbytes, multibyte)
7410 const char *m;
7411 int nbytes;
7412 int multibyte;
7413 {
7414 /* First flush out any partial line written with print. */
7415 message_log_maybe_newline ();
7416 if (m)
7417 message_dolog (m, nbytes, 1, multibyte);
7418 message2_nolog (m, nbytes, multibyte);
7419 }
7420
7421
7422 /* The non-logging counterpart of message2. */
7423
7424 void
7425 message2_nolog (m, nbytes, multibyte)
7426 const char *m;
7427 int nbytes, multibyte;
7428 {
7429 struct frame *sf = SELECTED_FRAME ();
7430 message_enable_multibyte = multibyte;
7431
7432 if (noninteractive)
7433 {
7434 if (noninteractive_need_newline)
7435 putc ('\n', stderr);
7436 noninteractive_need_newline = 0;
7437 if (m)
7438 fwrite (m, nbytes, 1, stderr);
7439 if (cursor_in_echo_area == 0)
7440 fprintf (stderr, "\n");
7441 fflush (stderr);
7442 }
7443 /* A null message buffer means that the frame hasn't really been
7444 initialized yet. Error messages get reported properly by
7445 cmd_error, so this must be just an informative message; toss it. */
7446 else if (INTERACTIVE
7447 && sf->glyphs_initialized_p
7448 && FRAME_MESSAGE_BUF (sf))
7449 {
7450 Lisp_Object mini_window;
7451 struct frame *f;
7452
7453 /* Get the frame containing the mini-buffer
7454 that the selected frame is using. */
7455 mini_window = FRAME_MINIBUF_WINDOW (sf);
7456 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7457
7458 FRAME_SAMPLE_VISIBILITY (f);
7459 if (FRAME_VISIBLE_P (sf)
7460 && ! FRAME_VISIBLE_P (f))
7461 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7462
7463 if (m)
7464 {
7465 set_message (m, Qnil, nbytes, multibyte);
7466 if (minibuffer_auto_raise)
7467 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7468 }
7469 else
7470 clear_message (1, 1);
7471
7472 do_pending_window_change (0);
7473 echo_area_display (1);
7474 do_pending_window_change (0);
7475 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7476 (*frame_up_to_date_hook) (f);
7477 }
7478 }
7479
7480
7481 /* Display an echo area message M with a specified length of NBYTES
7482 bytes. The string may include null characters. If M is not a
7483 string, clear out any existing message, and let the mini-buffer
7484 text show through.
7485
7486 This function cancels echoing. */
7487
7488 void
7489 message3 (m, nbytes, multibyte)
7490 Lisp_Object m;
7491 int nbytes;
7492 int multibyte;
7493 {
7494 struct gcpro gcpro1;
7495
7496 GCPRO1 (m);
7497 clear_message (1,1);
7498 cancel_echoing ();
7499
7500 /* First flush out any partial line written with print. */
7501 message_log_maybe_newline ();
7502 if (STRINGP (m))
7503 {
7504 char *buffer;
7505 USE_SAFE_ALLOCA;
7506
7507 SAFE_ALLOCA (buffer, char *, nbytes);
7508 bcopy (SDATA (m), buffer, nbytes);
7509 message_dolog (buffer, nbytes, 1, multibyte);
7510 SAFE_FREE ();
7511 }
7512 message3_nolog (m, nbytes, multibyte);
7513
7514 UNGCPRO;
7515 }
7516
7517
7518 /* The non-logging version of message3.
7519 This does not cancel echoing, because it is used for echoing.
7520 Perhaps we need to make a separate function for echoing
7521 and make this cancel echoing. */
7522
7523 void
7524 message3_nolog (m, nbytes, multibyte)
7525 Lisp_Object m;
7526 int nbytes, multibyte;
7527 {
7528 struct frame *sf = SELECTED_FRAME ();
7529 message_enable_multibyte = multibyte;
7530
7531 if (noninteractive)
7532 {
7533 if (noninteractive_need_newline)
7534 putc ('\n', stderr);
7535 noninteractive_need_newline = 0;
7536 if (STRINGP (m))
7537 fwrite (SDATA (m), nbytes, 1, stderr);
7538 if (cursor_in_echo_area == 0)
7539 fprintf (stderr, "\n");
7540 fflush (stderr);
7541 }
7542 /* A null message buffer means that the frame hasn't really been
7543 initialized yet. Error messages get reported properly by
7544 cmd_error, so this must be just an informative message; toss it. */
7545 else if (INTERACTIVE
7546 && sf->glyphs_initialized_p
7547 && FRAME_MESSAGE_BUF (sf))
7548 {
7549 Lisp_Object mini_window;
7550 Lisp_Object frame;
7551 struct frame *f;
7552
7553 /* Get the frame containing the mini-buffer
7554 that the selected frame is using. */
7555 mini_window = FRAME_MINIBUF_WINDOW (sf);
7556 frame = XWINDOW (mini_window)->frame;
7557 f = XFRAME (frame);
7558
7559 FRAME_SAMPLE_VISIBILITY (f);
7560 if (FRAME_VISIBLE_P (sf)
7561 && !FRAME_VISIBLE_P (f))
7562 Fmake_frame_visible (frame);
7563
7564 if (STRINGP (m) && SCHARS (m) > 0)
7565 {
7566 set_message (NULL, m, nbytes, multibyte);
7567 if (minibuffer_auto_raise)
7568 Fraise_frame (frame);
7569 /* Assume we are not echoing.
7570 (If we are, echo_now will override this.) */
7571 echo_message_buffer = Qnil;
7572 }
7573 else
7574 clear_message (1, 1);
7575
7576 do_pending_window_change (0);
7577 echo_area_display (1);
7578 do_pending_window_change (0);
7579 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7580 (*frame_up_to_date_hook) (f);
7581 }
7582 }
7583
7584
7585 /* Display a null-terminated echo area message M. If M is 0, clear
7586 out any existing message, and let the mini-buffer text show through.
7587
7588 The buffer M must continue to exist until after the echo area gets
7589 cleared or some other message gets displayed there. Do not pass
7590 text that is stored in a Lisp string. Do not pass text in a buffer
7591 that was alloca'd. */
7592
7593 void
7594 message1 (m)
7595 char *m;
7596 {
7597 message2 (m, (m ? strlen (m) : 0), 0);
7598 }
7599
7600
7601 /* The non-logging counterpart of message1. */
7602
7603 void
7604 message1_nolog (m)
7605 char *m;
7606 {
7607 message2_nolog (m, (m ? strlen (m) : 0), 0);
7608 }
7609
7610 /* Display a message M which contains a single %s
7611 which gets replaced with STRING. */
7612
7613 void
7614 message_with_string (m, string, log)
7615 char *m;
7616 Lisp_Object string;
7617 int log;
7618 {
7619 CHECK_STRING (string);
7620
7621 if (noninteractive)
7622 {
7623 if (m)
7624 {
7625 if (noninteractive_need_newline)
7626 putc ('\n', stderr);
7627 noninteractive_need_newline = 0;
7628 fprintf (stderr, m, SDATA (string));
7629 if (cursor_in_echo_area == 0)
7630 fprintf (stderr, "\n");
7631 fflush (stderr);
7632 }
7633 }
7634 else if (INTERACTIVE)
7635 {
7636 /* The frame whose minibuffer we're going to display the message on.
7637 It may be larger than the selected frame, so we need
7638 to use its buffer, not the selected frame's buffer. */
7639 Lisp_Object mini_window;
7640 struct frame *f, *sf = SELECTED_FRAME ();
7641
7642 /* Get the frame containing the minibuffer
7643 that the selected frame is using. */
7644 mini_window = FRAME_MINIBUF_WINDOW (sf);
7645 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7646
7647 /* A null message buffer means that the frame hasn't really been
7648 initialized yet. Error messages get reported properly by
7649 cmd_error, so this must be just an informative message; toss it. */
7650 if (FRAME_MESSAGE_BUF (f))
7651 {
7652 Lisp_Object args[2], message;
7653 struct gcpro gcpro1, gcpro2;
7654
7655 args[0] = build_string (m);
7656 args[1] = message = string;
7657 GCPRO2 (args[0], message);
7658 gcpro1.nvars = 2;
7659
7660 message = Fformat (2, args);
7661
7662 if (log)
7663 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7664 else
7665 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7666
7667 UNGCPRO;
7668
7669 /* Print should start at the beginning of the message
7670 buffer next time. */
7671 message_buf_print = 0;
7672 }
7673 }
7674 }
7675
7676
7677 /* Dump an informative message to the minibuf. If M is 0, clear out
7678 any existing message, and let the mini-buffer text show through. */
7679
7680 /* VARARGS 1 */
7681 void
7682 message (m, a1, a2, a3)
7683 char *m;
7684 EMACS_INT a1, a2, a3;
7685 {
7686 if (noninteractive)
7687 {
7688 if (m)
7689 {
7690 if (noninteractive_need_newline)
7691 putc ('\n', stderr);
7692 noninteractive_need_newline = 0;
7693 fprintf (stderr, m, a1, a2, a3);
7694 if (cursor_in_echo_area == 0)
7695 fprintf (stderr, "\n");
7696 fflush (stderr);
7697 }
7698 }
7699 else if (INTERACTIVE)
7700 {
7701 /* The frame whose mini-buffer we're going to display the message
7702 on. It may be larger than the selected frame, so we need to
7703 use its buffer, not the selected frame's buffer. */
7704 Lisp_Object mini_window;
7705 struct frame *f, *sf = SELECTED_FRAME ();
7706
7707 /* Get the frame containing the mini-buffer
7708 that the selected frame is using. */
7709 mini_window = FRAME_MINIBUF_WINDOW (sf);
7710 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7711
7712 /* A null message buffer means that the frame hasn't really been
7713 initialized yet. Error messages get reported properly by
7714 cmd_error, so this must be just an informative message; toss
7715 it. */
7716 if (FRAME_MESSAGE_BUF (f))
7717 {
7718 if (m)
7719 {
7720 int len;
7721 #ifdef NO_ARG_ARRAY
7722 char *a[3];
7723 a[0] = (char *) a1;
7724 a[1] = (char *) a2;
7725 a[2] = (char *) a3;
7726
7727 len = doprnt (FRAME_MESSAGE_BUF (f),
7728 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7729 #else
7730 len = doprnt (FRAME_MESSAGE_BUF (f),
7731 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7732 (char **) &a1);
7733 #endif /* NO_ARG_ARRAY */
7734
7735 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7736 }
7737 else
7738 message1 (0);
7739
7740 /* Print should start at the beginning of the message
7741 buffer next time. */
7742 message_buf_print = 0;
7743 }
7744 }
7745 }
7746
7747
7748 /* The non-logging version of message. */
7749
7750 void
7751 message_nolog (m, a1, a2, a3)
7752 char *m;
7753 EMACS_INT a1, a2, a3;
7754 {
7755 Lisp_Object old_log_max;
7756 old_log_max = Vmessage_log_max;
7757 Vmessage_log_max = Qnil;
7758 message (m, a1, a2, a3);
7759 Vmessage_log_max = old_log_max;
7760 }
7761
7762
7763 /* Display the current message in the current mini-buffer. This is
7764 only called from error handlers in process.c, and is not time
7765 critical. */
7766
7767 void
7768 update_echo_area ()
7769 {
7770 if (!NILP (echo_area_buffer[0]))
7771 {
7772 Lisp_Object string;
7773 string = Fcurrent_message ();
7774 message3 (string, SBYTES (string),
7775 !NILP (current_buffer->enable_multibyte_characters));
7776 }
7777 }
7778
7779
7780 /* Make sure echo area buffers in `echo_buffers' are live.
7781 If they aren't, make new ones. */
7782
7783 static void
7784 ensure_echo_area_buffers ()
7785 {
7786 int i;
7787
7788 for (i = 0; i < 2; ++i)
7789 if (!BUFFERP (echo_buffer[i])
7790 || NILP (XBUFFER (echo_buffer[i])->name))
7791 {
7792 char name[30];
7793 Lisp_Object old_buffer;
7794 int j;
7795
7796 old_buffer = echo_buffer[i];
7797 sprintf (name, " *Echo Area %d*", i);
7798 echo_buffer[i] = Fget_buffer_create (build_string (name));
7799 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7800
7801 for (j = 0; j < 2; ++j)
7802 if (EQ (old_buffer, echo_area_buffer[j]))
7803 echo_area_buffer[j] = echo_buffer[i];
7804 }
7805 }
7806
7807
7808 /* Call FN with args A1..A4 with either the current or last displayed
7809 echo_area_buffer as current buffer.
7810
7811 WHICH zero means use the current message buffer
7812 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7813 from echo_buffer[] and clear it.
7814
7815 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7816 suitable buffer from echo_buffer[] and clear it.
7817
7818 Value is what FN returns. */
7819
7820 static int
7821 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7822 struct window *w;
7823 int which;
7824 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7825 EMACS_INT a1;
7826 Lisp_Object a2;
7827 EMACS_INT a3, a4;
7828 {
7829 Lisp_Object buffer;
7830 int this_one, the_other, clear_buffer_p, rc;
7831 int count = SPECPDL_INDEX ();
7832
7833 /* If buffers aren't live, make new ones. */
7834 ensure_echo_area_buffers ();
7835
7836 clear_buffer_p = 0;
7837
7838 if (which == 0)
7839 this_one = 0, the_other = 1;
7840 else if (which > 0)
7841 this_one = 1, the_other = 0;
7842
7843 /* Choose a suitable buffer from echo_buffer[] is we don't
7844 have one. */
7845 if (NILP (echo_area_buffer[this_one]))
7846 {
7847 echo_area_buffer[this_one]
7848 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7849 ? echo_buffer[the_other]
7850 : echo_buffer[this_one]);
7851 clear_buffer_p = 1;
7852 }
7853
7854 buffer = echo_area_buffer[this_one];
7855
7856 /* Don't get confused by reusing the buffer used for echoing
7857 for a different purpose. */
7858 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7859 cancel_echoing ();
7860
7861 record_unwind_protect (unwind_with_echo_area_buffer,
7862 with_echo_area_buffer_unwind_data (w));
7863
7864 /* Make the echo area buffer current. Note that for display
7865 purposes, it is not necessary that the displayed window's buffer
7866 == current_buffer, except for text property lookup. So, let's
7867 only set that buffer temporarily here without doing a full
7868 Fset_window_buffer. We must also change w->pointm, though,
7869 because otherwise an assertions in unshow_buffer fails, and Emacs
7870 aborts. */
7871 set_buffer_internal_1 (XBUFFER (buffer));
7872 if (w)
7873 {
7874 w->buffer = buffer;
7875 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7876 }
7877
7878 current_buffer->undo_list = Qt;
7879 current_buffer->read_only = Qnil;
7880 specbind (Qinhibit_read_only, Qt);
7881 specbind (Qinhibit_modification_hooks, Qt);
7882
7883 if (clear_buffer_p && Z > BEG)
7884 del_range (BEG, Z);
7885
7886 xassert (BEGV >= BEG);
7887 xassert (ZV <= Z && ZV >= BEGV);
7888
7889 rc = fn (a1, a2, a3, a4);
7890
7891 xassert (BEGV >= BEG);
7892 xassert (ZV <= Z && ZV >= BEGV);
7893
7894 unbind_to (count, Qnil);
7895 return rc;
7896 }
7897
7898
7899 /* Save state that should be preserved around the call to the function
7900 FN called in with_echo_area_buffer. */
7901
7902 static Lisp_Object
7903 with_echo_area_buffer_unwind_data (w)
7904 struct window *w;
7905 {
7906 int i = 0;
7907 Lisp_Object vector;
7908
7909 /* Reduce consing by keeping one vector in
7910 Vwith_echo_area_save_vector. */
7911 vector = Vwith_echo_area_save_vector;
7912 Vwith_echo_area_save_vector = Qnil;
7913
7914 if (NILP (vector))
7915 vector = Fmake_vector (make_number (7), Qnil);
7916
7917 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7918 AREF (vector, i) = Vdeactivate_mark, ++i;
7919 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7920
7921 if (w)
7922 {
7923 XSETWINDOW (AREF (vector, i), w); ++i;
7924 AREF (vector, i) = w->buffer; ++i;
7925 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7926 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7927 }
7928 else
7929 {
7930 int end = i + 4;
7931 for (; i < end; ++i)
7932 AREF (vector, i) = Qnil;
7933 }
7934
7935 xassert (i == ASIZE (vector));
7936 return vector;
7937 }
7938
7939
7940 /* Restore global state from VECTOR which was created by
7941 with_echo_area_buffer_unwind_data. */
7942
7943 static Lisp_Object
7944 unwind_with_echo_area_buffer (vector)
7945 Lisp_Object vector;
7946 {
7947 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7948 Vdeactivate_mark = AREF (vector, 1);
7949 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7950
7951 if (WINDOWP (AREF (vector, 3)))
7952 {
7953 struct window *w;
7954 Lisp_Object buffer, charpos, bytepos;
7955
7956 w = XWINDOW (AREF (vector, 3));
7957 buffer = AREF (vector, 4);
7958 charpos = AREF (vector, 5);
7959 bytepos = AREF (vector, 6);
7960
7961 w->buffer = buffer;
7962 set_marker_both (w->pointm, buffer,
7963 XFASTINT (charpos), XFASTINT (bytepos));
7964 }
7965
7966 Vwith_echo_area_save_vector = vector;
7967 return Qnil;
7968 }
7969
7970
7971 /* Set up the echo area for use by print functions. MULTIBYTE_P
7972 non-zero means we will print multibyte. */
7973
7974 void
7975 setup_echo_area_for_printing (multibyte_p)
7976 int multibyte_p;
7977 {
7978 /* If we can't find an echo area any more, exit. */
7979 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7980 Fkill_emacs (Qnil);
7981
7982 ensure_echo_area_buffers ();
7983
7984 if (!message_buf_print)
7985 {
7986 /* A message has been output since the last time we printed.
7987 Choose a fresh echo area buffer. */
7988 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7989 echo_area_buffer[0] = echo_buffer[1];
7990 else
7991 echo_area_buffer[0] = echo_buffer[0];
7992
7993 /* Switch to that buffer and clear it. */
7994 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7995 current_buffer->truncate_lines = Qnil;
7996
7997 if (Z > BEG)
7998 {
7999 int count = SPECPDL_INDEX ();
8000 specbind (Qinhibit_read_only, Qt);
8001 /* Note that undo recording is always disabled. */
8002 del_range (BEG, Z);
8003 unbind_to (count, Qnil);
8004 }
8005 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8006
8007 /* Set up the buffer for the multibyteness we need. */
8008 if (multibyte_p
8009 != !NILP (current_buffer->enable_multibyte_characters))
8010 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8011
8012 /* Raise the frame containing the echo area. */
8013 if (minibuffer_auto_raise)
8014 {
8015 struct frame *sf = SELECTED_FRAME ();
8016 Lisp_Object mini_window;
8017 mini_window = FRAME_MINIBUF_WINDOW (sf);
8018 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8019 }
8020
8021 message_log_maybe_newline ();
8022 message_buf_print = 1;
8023 }
8024 else
8025 {
8026 if (NILP (echo_area_buffer[0]))
8027 {
8028 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8029 echo_area_buffer[0] = echo_buffer[1];
8030 else
8031 echo_area_buffer[0] = echo_buffer[0];
8032 }
8033
8034 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8035 {
8036 /* Someone switched buffers between print requests. */
8037 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8038 current_buffer->truncate_lines = Qnil;
8039 }
8040 }
8041 }
8042
8043
8044 /* Display an echo area message in window W. Value is non-zero if W's
8045 height is changed. If display_last_displayed_message_p is
8046 non-zero, display the message that was last displayed, otherwise
8047 display the current message. */
8048
8049 static int
8050 display_echo_area (w)
8051 struct window *w;
8052 {
8053 int i, no_message_p, window_height_changed_p, count;
8054
8055 /* Temporarily disable garbage collections while displaying the echo
8056 area. This is done because a GC can print a message itself.
8057 That message would modify the echo area buffer's contents while a
8058 redisplay of the buffer is going on, and seriously confuse
8059 redisplay. */
8060 count = inhibit_garbage_collection ();
8061
8062 /* If there is no message, we must call display_echo_area_1
8063 nevertheless because it resizes the window. But we will have to
8064 reset the echo_area_buffer in question to nil at the end because
8065 with_echo_area_buffer will sets it to an empty buffer. */
8066 i = display_last_displayed_message_p ? 1 : 0;
8067 no_message_p = NILP (echo_area_buffer[i]);
8068
8069 window_height_changed_p
8070 = with_echo_area_buffer (w, display_last_displayed_message_p,
8071 display_echo_area_1,
8072 (EMACS_INT) w, Qnil, 0, 0);
8073
8074 if (no_message_p)
8075 echo_area_buffer[i] = Qnil;
8076
8077 unbind_to (count, Qnil);
8078 return window_height_changed_p;
8079 }
8080
8081
8082 /* Helper for display_echo_area. Display the current buffer which
8083 contains the current echo area message in window W, a mini-window,
8084 a pointer to which is passed in A1. A2..A4 are currently not used.
8085 Change the height of W so that all of the message is displayed.
8086 Value is non-zero if height of W was changed. */
8087
8088 static int
8089 display_echo_area_1 (a1, a2, a3, a4)
8090 EMACS_INT a1;
8091 Lisp_Object a2;
8092 EMACS_INT a3, a4;
8093 {
8094 struct window *w = (struct window *) a1;
8095 Lisp_Object window;
8096 struct text_pos start;
8097 int window_height_changed_p = 0;
8098
8099 /* Do this before displaying, so that we have a large enough glyph
8100 matrix for the display. If we can't get enough space for the
8101 whole text, display the last N lines. That works by setting w->start. */
8102 window_height_changed_p = resize_mini_window (w, 0);
8103
8104 /* Use the starting position chosen by resize_mini_window. */
8105 SET_TEXT_POS_FROM_MARKER (start, w->start);
8106
8107 /* Display. */
8108 clear_glyph_matrix (w->desired_matrix);
8109 XSETWINDOW (window, w);
8110 try_window (window, start, 0);
8111
8112 return window_height_changed_p;
8113 }
8114
8115
8116 /* Resize the echo area window to exactly the size needed for the
8117 currently displayed message, if there is one. If a mini-buffer
8118 is active, don't shrink it. */
8119
8120 void
8121 resize_echo_area_exactly ()
8122 {
8123 if (BUFFERP (echo_area_buffer[0])
8124 && WINDOWP (echo_area_window))
8125 {
8126 struct window *w = XWINDOW (echo_area_window);
8127 int resized_p;
8128 Lisp_Object resize_exactly;
8129
8130 if (minibuf_level == 0)
8131 resize_exactly = Qt;
8132 else
8133 resize_exactly = Qnil;
8134
8135 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8136 (EMACS_INT) w, resize_exactly, 0, 0);
8137 if (resized_p)
8138 {
8139 ++windows_or_buffers_changed;
8140 ++update_mode_lines;
8141 redisplay_internal (0);
8142 }
8143 }
8144 }
8145
8146
8147 /* Callback function for with_echo_area_buffer, when used from
8148 resize_echo_area_exactly. A1 contains a pointer to the window to
8149 resize, EXACTLY non-nil means resize the mini-window exactly to the
8150 size of the text displayed. A3 and A4 are not used. Value is what
8151 resize_mini_window returns. */
8152
8153 static int
8154 resize_mini_window_1 (a1, exactly, a3, a4)
8155 EMACS_INT a1;
8156 Lisp_Object exactly;
8157 EMACS_INT a3, a4;
8158 {
8159 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8160 }
8161
8162
8163 /* Resize mini-window W to fit the size of its contents. EXACT:P
8164 means size the window exactly to the size needed. Otherwise, it's
8165 only enlarged until W's buffer is empty.
8166
8167 Set W->start to the right place to begin display. If the whole
8168 contents fit, start at the beginning. Otherwise, start so as
8169 to make the end of the contents appear. This is particularly
8170 important for y-or-n-p, but seems desirable generally.
8171
8172 Value is non-zero if the window height has been changed. */
8173
8174 int
8175 resize_mini_window (w, exact_p)
8176 struct window *w;
8177 int exact_p;
8178 {
8179 struct frame *f = XFRAME (w->frame);
8180 int window_height_changed_p = 0;
8181
8182 xassert (MINI_WINDOW_P (w));
8183
8184 /* By default, start display at the beginning. */
8185 set_marker_both (w->start, w->buffer,
8186 BUF_BEGV (XBUFFER (w->buffer)),
8187 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8188
8189 /* Don't resize windows while redisplaying a window; it would
8190 confuse redisplay functions when the size of the window they are
8191 displaying changes from under them. Such a resizing can happen,
8192 for instance, when which-func prints a long message while
8193 we are running fontification-functions. We're running these
8194 functions with safe_call which binds inhibit-redisplay to t. */
8195 if (!NILP (Vinhibit_redisplay))
8196 return 0;
8197
8198 /* Nil means don't try to resize. */
8199 if (NILP (Vresize_mini_windows)
8200 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8201 return 0;
8202
8203 if (!FRAME_MINIBUF_ONLY_P (f))
8204 {
8205 struct it it;
8206 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8207 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8208 int height, max_height;
8209 int unit = FRAME_LINE_HEIGHT (f);
8210 struct text_pos start;
8211 struct buffer *old_current_buffer = NULL;
8212
8213 if (current_buffer != XBUFFER (w->buffer))
8214 {
8215 old_current_buffer = current_buffer;
8216 set_buffer_internal (XBUFFER (w->buffer));
8217 }
8218
8219 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8220
8221 /* Compute the max. number of lines specified by the user. */
8222 if (FLOATP (Vmax_mini_window_height))
8223 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8224 else if (INTEGERP (Vmax_mini_window_height))
8225 max_height = XINT (Vmax_mini_window_height);
8226 else
8227 max_height = total_height / 4;
8228
8229 /* Correct that max. height if it's bogus. */
8230 max_height = max (1, max_height);
8231 max_height = min (total_height, max_height);
8232
8233 /* Find out the height of the text in the window. */
8234 if (it.truncate_lines_p)
8235 height = 1;
8236 else
8237 {
8238 last_height = 0;
8239 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8240 if (it.max_ascent == 0 && it.max_descent == 0)
8241 height = it.current_y + last_height;
8242 else
8243 height = it.current_y + it.max_ascent + it.max_descent;
8244 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8245 height = (height + unit - 1) / unit;
8246 }
8247
8248 /* Compute a suitable window start. */
8249 if (height > max_height)
8250 {
8251 height = max_height;
8252 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8253 move_it_vertically_backward (&it, (height - 1) * unit);
8254 start = it.current.pos;
8255 }
8256 else
8257 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8258 SET_MARKER_FROM_TEXT_POS (w->start, start);
8259
8260 if (EQ (Vresize_mini_windows, Qgrow_only))
8261 {
8262 /* Let it grow only, until we display an empty message, in which
8263 case the window shrinks again. */
8264 if (height > WINDOW_TOTAL_LINES (w))
8265 {
8266 int old_height = WINDOW_TOTAL_LINES (w);
8267 freeze_window_starts (f, 1);
8268 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8269 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8270 }
8271 else if (height < WINDOW_TOTAL_LINES (w)
8272 && (exact_p || BEGV == ZV))
8273 {
8274 int old_height = WINDOW_TOTAL_LINES (w);
8275 freeze_window_starts (f, 0);
8276 shrink_mini_window (w);
8277 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8278 }
8279 }
8280 else
8281 {
8282 /* Always resize to exact size needed. */
8283 if (height > WINDOW_TOTAL_LINES (w))
8284 {
8285 int old_height = WINDOW_TOTAL_LINES (w);
8286 freeze_window_starts (f, 1);
8287 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8288 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8289 }
8290 else if (height < WINDOW_TOTAL_LINES (w))
8291 {
8292 int old_height = WINDOW_TOTAL_LINES (w);
8293 freeze_window_starts (f, 0);
8294 shrink_mini_window (w);
8295
8296 if (height)
8297 {
8298 freeze_window_starts (f, 1);
8299 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8300 }
8301
8302 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8303 }
8304 }
8305
8306 if (old_current_buffer)
8307 set_buffer_internal (old_current_buffer);
8308 }
8309
8310 return window_height_changed_p;
8311 }
8312
8313
8314 /* Value is the current message, a string, or nil if there is no
8315 current message. */
8316
8317 Lisp_Object
8318 current_message ()
8319 {
8320 Lisp_Object msg;
8321
8322 if (NILP (echo_area_buffer[0]))
8323 msg = Qnil;
8324 else
8325 {
8326 with_echo_area_buffer (0, 0, current_message_1,
8327 (EMACS_INT) &msg, Qnil, 0, 0);
8328 if (NILP (msg))
8329 echo_area_buffer[0] = Qnil;
8330 }
8331
8332 return msg;
8333 }
8334
8335
8336 static int
8337 current_message_1 (a1, a2, a3, a4)
8338 EMACS_INT a1;
8339 Lisp_Object a2;
8340 EMACS_INT a3, a4;
8341 {
8342 Lisp_Object *msg = (Lisp_Object *) a1;
8343
8344 if (Z > BEG)
8345 *msg = make_buffer_string (BEG, Z, 1);
8346 else
8347 *msg = Qnil;
8348 return 0;
8349 }
8350
8351
8352 /* Push the current message on Vmessage_stack for later restauration
8353 by restore_message. Value is non-zero if the current message isn't
8354 empty. This is a relatively infrequent operation, so it's not
8355 worth optimizing. */
8356
8357 int
8358 push_message ()
8359 {
8360 Lisp_Object msg;
8361 msg = current_message ();
8362 Vmessage_stack = Fcons (msg, Vmessage_stack);
8363 return STRINGP (msg);
8364 }
8365
8366
8367 /* Restore message display from the top of Vmessage_stack. */
8368
8369 void
8370 restore_message ()
8371 {
8372 Lisp_Object msg;
8373
8374 xassert (CONSP (Vmessage_stack));
8375 msg = XCAR (Vmessage_stack);
8376 if (STRINGP (msg))
8377 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8378 else
8379 message3_nolog (msg, 0, 0);
8380 }
8381
8382
8383 /* Handler for record_unwind_protect calling pop_message. */
8384
8385 Lisp_Object
8386 pop_message_unwind (dummy)
8387 Lisp_Object dummy;
8388 {
8389 pop_message ();
8390 return Qnil;
8391 }
8392
8393 /* Pop the top-most entry off Vmessage_stack. */
8394
8395 void
8396 pop_message ()
8397 {
8398 xassert (CONSP (Vmessage_stack));
8399 Vmessage_stack = XCDR (Vmessage_stack);
8400 }
8401
8402
8403 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8404 exits. If the stack is not empty, we have a missing pop_message
8405 somewhere. */
8406
8407 void
8408 check_message_stack ()
8409 {
8410 if (!NILP (Vmessage_stack))
8411 abort ();
8412 }
8413
8414
8415 /* Truncate to NCHARS what will be displayed in the echo area the next
8416 time we display it---but don't redisplay it now. */
8417
8418 void
8419 truncate_echo_area (nchars)
8420 int nchars;
8421 {
8422 if (nchars == 0)
8423 echo_area_buffer[0] = Qnil;
8424 /* A null message buffer means that the frame hasn't really been
8425 initialized yet. Error messages get reported properly by
8426 cmd_error, so this must be just an informative message; toss it. */
8427 else if (!noninteractive
8428 && INTERACTIVE
8429 && !NILP (echo_area_buffer[0]))
8430 {
8431 struct frame *sf = SELECTED_FRAME ();
8432 if (FRAME_MESSAGE_BUF (sf))
8433 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8434 }
8435 }
8436
8437
8438 /* Helper function for truncate_echo_area. Truncate the current
8439 message to at most NCHARS characters. */
8440
8441 static int
8442 truncate_message_1 (nchars, a2, a3, a4)
8443 EMACS_INT nchars;
8444 Lisp_Object a2;
8445 EMACS_INT a3, a4;
8446 {
8447 if (BEG + nchars < Z)
8448 del_range (BEG + nchars, Z);
8449 if (Z == BEG)
8450 echo_area_buffer[0] = Qnil;
8451 return 0;
8452 }
8453
8454
8455 /* Set the current message to a substring of S or STRING.
8456
8457 If STRING is a Lisp string, set the message to the first NBYTES
8458 bytes from STRING. NBYTES zero means use the whole string. If
8459 STRING is multibyte, the message will be displayed multibyte.
8460
8461 If S is not null, set the message to the first LEN bytes of S. LEN
8462 zero means use the whole string. MULTIBYTE_P non-zero means S is
8463 multibyte. Display the message multibyte in that case.
8464
8465 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8466 to t before calling set_message_1 (which calls insert).
8467 */
8468
8469 void
8470 set_message (s, string, nbytes, multibyte_p)
8471 const char *s;
8472 Lisp_Object string;
8473 int nbytes, multibyte_p;
8474 {
8475 message_enable_multibyte
8476 = ((s && multibyte_p)
8477 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8478
8479 with_echo_area_buffer (0, 0, set_message_1,
8480 (EMACS_INT) s, string, nbytes, multibyte_p);
8481 message_buf_print = 0;
8482 help_echo_showing_p = 0;
8483 }
8484
8485
8486 /* Helper function for set_message. Arguments have the same meaning
8487 as there, with A1 corresponding to S and A2 corresponding to STRING
8488 This function is called with the echo area buffer being
8489 current. */
8490
8491 static int
8492 set_message_1 (a1, a2, nbytes, multibyte_p)
8493 EMACS_INT a1;
8494 Lisp_Object a2;
8495 EMACS_INT nbytes, multibyte_p;
8496 {
8497 const char *s = (const char *) a1;
8498 Lisp_Object string = a2;
8499
8500 /* Change multibyteness of the echo buffer appropriately. */
8501 if (message_enable_multibyte
8502 != !NILP (current_buffer->enable_multibyte_characters))
8503 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8504
8505 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8506
8507 /* Insert new message at BEG. */
8508 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8509 Ferase_buffer ();
8510
8511 if (STRINGP (string))
8512 {
8513 int nchars;
8514
8515 if (nbytes == 0)
8516 nbytes = SBYTES (string);
8517 nchars = string_byte_to_char (string, nbytes);
8518
8519 /* This function takes care of single/multibyte conversion. We
8520 just have to ensure that the echo area buffer has the right
8521 setting of enable_multibyte_characters. */
8522 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8523 }
8524 else if (s)
8525 {
8526 if (nbytes == 0)
8527 nbytes = strlen (s);
8528
8529 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8530 {
8531 /* Convert from multi-byte to single-byte. */
8532 int i, c, n;
8533 unsigned char work[1];
8534
8535 /* Convert a multibyte string to single-byte. */
8536 for (i = 0; i < nbytes; i += n)
8537 {
8538 c = string_char_and_length (s + i, nbytes - i, &n);
8539 work[0] = (SINGLE_BYTE_CHAR_P (c)
8540 ? c
8541 : multibyte_char_to_unibyte (c, Qnil));
8542 insert_1_both (work, 1, 1, 1, 0, 0);
8543 }
8544 }
8545 else if (!multibyte_p
8546 && !NILP (current_buffer->enable_multibyte_characters))
8547 {
8548 /* Convert from single-byte to multi-byte. */
8549 int i, c, n;
8550 const unsigned char *msg = (const unsigned char *) s;
8551 unsigned char str[MAX_MULTIBYTE_LENGTH];
8552
8553 /* Convert a single-byte string to multibyte. */
8554 for (i = 0; i < nbytes; i++)
8555 {
8556 c = unibyte_char_to_multibyte (msg[i]);
8557 n = CHAR_STRING (c, str);
8558 insert_1_both (str, 1, n, 1, 0, 0);
8559 }
8560 }
8561 else
8562 insert_1 (s, nbytes, 1, 0, 0);
8563 }
8564
8565 return 0;
8566 }
8567
8568
8569 /* Clear messages. CURRENT_P non-zero means clear the current
8570 message. LAST_DISPLAYED_P non-zero means clear the message
8571 last displayed. */
8572
8573 void
8574 clear_message (current_p, last_displayed_p)
8575 int current_p, last_displayed_p;
8576 {
8577 if (current_p)
8578 {
8579 echo_area_buffer[0] = Qnil;
8580 message_cleared_p = 1;
8581 }
8582
8583 if (last_displayed_p)
8584 echo_area_buffer[1] = Qnil;
8585
8586 message_buf_print = 0;
8587 }
8588
8589 /* Clear garbaged frames.
8590
8591 This function is used where the old redisplay called
8592 redraw_garbaged_frames which in turn called redraw_frame which in
8593 turn called clear_frame. The call to clear_frame was a source of
8594 flickering. I believe a clear_frame is not necessary. It should
8595 suffice in the new redisplay to invalidate all current matrices,
8596 and ensure a complete redisplay of all windows. */
8597
8598 static void
8599 clear_garbaged_frames ()
8600 {
8601 if (frame_garbaged)
8602 {
8603 Lisp_Object tail, frame;
8604 int changed_count = 0;
8605
8606 FOR_EACH_FRAME (tail, frame)
8607 {
8608 struct frame *f = XFRAME (frame);
8609
8610 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8611 {
8612 if (f->resized_p)
8613 {
8614 Fredraw_frame (frame);
8615 f->force_flush_display_p = 1;
8616 }
8617 clear_current_matrices (f);
8618 changed_count++;
8619 f->garbaged = 0;
8620 f->resized_p = 0;
8621 }
8622 }
8623
8624 frame_garbaged = 0;
8625 if (changed_count)
8626 ++windows_or_buffers_changed;
8627 }
8628 }
8629
8630
8631 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8632 is non-zero update selected_frame. Value is non-zero if the
8633 mini-windows height has been changed. */
8634
8635 static int
8636 echo_area_display (update_frame_p)
8637 int update_frame_p;
8638 {
8639 Lisp_Object mini_window;
8640 struct window *w;
8641 struct frame *f;
8642 int window_height_changed_p = 0;
8643 struct frame *sf = SELECTED_FRAME ();
8644
8645 mini_window = FRAME_MINIBUF_WINDOW (sf);
8646 w = XWINDOW (mini_window);
8647 f = XFRAME (WINDOW_FRAME (w));
8648
8649 /* Don't display if frame is invisible or not yet initialized. */
8650 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8651 return 0;
8652
8653 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8654 #ifndef MAC_OS8
8655 #ifdef HAVE_WINDOW_SYSTEM
8656 /* When Emacs starts, selected_frame may be a visible terminal
8657 frame, even if we run under a window system. If we let this
8658 through, a message would be displayed on the terminal. */
8659 if (EQ (selected_frame, Vterminal_frame)
8660 && !NILP (Vwindow_system))
8661 return 0;
8662 #endif /* HAVE_WINDOW_SYSTEM */
8663 #endif
8664
8665 /* Redraw garbaged frames. */
8666 if (frame_garbaged)
8667 clear_garbaged_frames ();
8668
8669 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8670 {
8671 echo_area_window = mini_window;
8672 window_height_changed_p = display_echo_area (w);
8673 w->must_be_updated_p = 1;
8674
8675 /* Update the display, unless called from redisplay_internal.
8676 Also don't update the screen during redisplay itself. The
8677 update will happen at the end of redisplay, and an update
8678 here could cause confusion. */
8679 if (update_frame_p && !redisplaying_p)
8680 {
8681 int n = 0;
8682
8683 /* If the display update has been interrupted by pending
8684 input, update mode lines in the frame. Due to the
8685 pending input, it might have been that redisplay hasn't
8686 been called, so that mode lines above the echo area are
8687 garbaged. This looks odd, so we prevent it here. */
8688 if (!display_completed)
8689 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8690
8691 if (window_height_changed_p
8692 /* Don't do this if Emacs is shutting down. Redisplay
8693 needs to run hooks. */
8694 && !NILP (Vrun_hooks))
8695 {
8696 /* Must update other windows. Likewise as in other
8697 cases, don't let this update be interrupted by
8698 pending input. */
8699 int count = SPECPDL_INDEX ();
8700 specbind (Qredisplay_dont_pause, Qt);
8701 windows_or_buffers_changed = 1;
8702 redisplay_internal (0);
8703 unbind_to (count, Qnil);
8704 }
8705 else if (FRAME_WINDOW_P (f) && n == 0)
8706 {
8707 /* Window configuration is the same as before.
8708 Can do with a display update of the echo area,
8709 unless we displayed some mode lines. */
8710 update_single_window (w, 1);
8711 rif->flush_display (f);
8712 }
8713 else
8714 update_frame (f, 1, 1);
8715
8716 /* If cursor is in the echo area, make sure that the next
8717 redisplay displays the minibuffer, so that the cursor will
8718 be replaced with what the minibuffer wants. */
8719 if (cursor_in_echo_area)
8720 ++windows_or_buffers_changed;
8721 }
8722 }
8723 else if (!EQ (mini_window, selected_window))
8724 windows_or_buffers_changed++;
8725
8726 /* The current message is now also the last one displayed. */
8727 echo_area_buffer[1] = echo_area_buffer[0];
8728
8729 /* Prevent redisplay optimization in redisplay_internal by resetting
8730 this_line_start_pos. This is done because the mini-buffer now
8731 displays the message instead of its buffer text. */
8732 if (EQ (mini_window, selected_window))
8733 CHARPOS (this_line_start_pos) = 0;
8734
8735 return window_height_changed_p;
8736 }
8737
8738
8739 \f
8740 /***********************************************************************
8741 Mode Lines and Frame Titles
8742 ***********************************************************************/
8743
8744 /* A buffer for constructing non-propertized mode-line strings and
8745 frame titles in it; allocated from the heap in init_xdisp and
8746 resized as needed in store_mode_line_noprop_char. */
8747
8748 static char *mode_line_noprop_buf;
8749
8750 /* The buffer's end, and a current output position in it. */
8751
8752 static char *mode_line_noprop_buf_end;
8753 static char *mode_line_noprop_ptr;
8754
8755 #define MODE_LINE_NOPROP_LEN(start) \
8756 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8757
8758 static enum {
8759 MODE_LINE_DISPLAY = 0,
8760 MODE_LINE_TITLE,
8761 MODE_LINE_NOPROP,
8762 MODE_LINE_STRING
8763 } mode_line_target;
8764
8765 /* Alist that caches the results of :propertize.
8766 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8767 static Lisp_Object mode_line_proptrans_alist;
8768
8769 /* List of strings making up the mode-line. */
8770 static Lisp_Object mode_line_string_list;
8771
8772 /* Base face property when building propertized mode line string. */
8773 static Lisp_Object mode_line_string_face;
8774 static Lisp_Object mode_line_string_face_prop;
8775
8776
8777 /* Unwind data for mode line strings */
8778
8779 static Lisp_Object Vmode_line_unwind_vector;
8780
8781 static Lisp_Object
8782 format_mode_line_unwind_data (obuf, save_proptrans)
8783 struct buffer *obuf;
8784 {
8785 Lisp_Object vector;
8786
8787 /* Reduce consing by keeping one vector in
8788 Vwith_echo_area_save_vector. */
8789 vector = Vmode_line_unwind_vector;
8790 Vmode_line_unwind_vector = Qnil;
8791
8792 if (NILP (vector))
8793 vector = Fmake_vector (make_number (7), Qnil);
8794
8795 AREF (vector, 0) = make_number (mode_line_target);
8796 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8797 AREF (vector, 2) = mode_line_string_list;
8798 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8799 AREF (vector, 4) = mode_line_string_face;
8800 AREF (vector, 5) = mode_line_string_face_prop;
8801
8802 if (obuf)
8803 XSETBUFFER (AREF (vector, 6), obuf);
8804 else
8805 AREF (vector, 6) = Qnil;
8806
8807 return vector;
8808 }
8809
8810 static Lisp_Object
8811 unwind_format_mode_line (vector)
8812 Lisp_Object vector;
8813 {
8814 mode_line_target = XINT (AREF (vector, 0));
8815 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8816 mode_line_string_list = AREF (vector, 2);
8817 if (! EQ (AREF (vector, 3), Qt))
8818 mode_line_proptrans_alist = AREF (vector, 3);
8819 mode_line_string_face = AREF (vector, 4);
8820 mode_line_string_face_prop = AREF (vector, 5);
8821
8822 if (!NILP (AREF (vector, 6)))
8823 {
8824 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8825 AREF (vector, 6) = Qnil;
8826 }
8827
8828 Vmode_line_unwind_vector = vector;
8829 return Qnil;
8830 }
8831
8832
8833 /* Store a single character C for the frame title in mode_line_noprop_buf.
8834 Re-allocate mode_line_noprop_buf if necessary. */
8835
8836 static void
8837 #ifdef PROTOTYPES
8838 store_mode_line_noprop_char (char c)
8839 #else
8840 store_mode_line_noprop_char (c)
8841 char c;
8842 #endif
8843 {
8844 /* If output position has reached the end of the allocated buffer,
8845 double the buffer's size. */
8846 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8847 {
8848 int len = MODE_LINE_NOPROP_LEN (0);
8849 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8850 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8851 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8852 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8853 }
8854
8855 *mode_line_noprop_ptr++ = c;
8856 }
8857
8858
8859 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8860 mode_line_noprop_ptr. STR is the string to store. Do not copy
8861 characters that yield more columns than PRECISION; PRECISION <= 0
8862 means copy the whole string. Pad with spaces until FIELD_WIDTH
8863 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8864 pad. Called from display_mode_element when it is used to build a
8865 frame title. */
8866
8867 static int
8868 store_mode_line_noprop (str, field_width, precision)
8869 const unsigned char *str;
8870 int field_width, precision;
8871 {
8872 int n = 0;
8873 int dummy, nbytes;
8874
8875 /* Copy at most PRECISION chars from STR. */
8876 nbytes = strlen (str);
8877 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8878 while (nbytes--)
8879 store_mode_line_noprop_char (*str++);
8880
8881 /* Fill up with spaces until FIELD_WIDTH reached. */
8882 while (field_width > 0
8883 && n < field_width)
8884 {
8885 store_mode_line_noprop_char (' ');
8886 ++n;
8887 }
8888
8889 return n;
8890 }
8891
8892 /***********************************************************************
8893 Frame Titles
8894 ***********************************************************************/
8895
8896 #ifdef HAVE_WINDOW_SYSTEM
8897
8898 /* Set the title of FRAME, if it has changed. The title format is
8899 Vicon_title_format if FRAME is iconified, otherwise it is
8900 frame_title_format. */
8901
8902 static void
8903 x_consider_frame_title (frame)
8904 Lisp_Object frame;
8905 {
8906 struct frame *f = XFRAME (frame);
8907
8908 if (FRAME_WINDOW_P (f)
8909 || FRAME_MINIBUF_ONLY_P (f)
8910 || f->explicit_name)
8911 {
8912 /* Do we have more than one visible frame on this X display? */
8913 Lisp_Object tail;
8914 Lisp_Object fmt;
8915 int title_start;
8916 char *title;
8917 int len;
8918 struct it it;
8919 int count = SPECPDL_INDEX ();
8920
8921 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8922 {
8923 Lisp_Object other_frame = XCAR (tail);
8924 struct frame *tf = XFRAME (other_frame);
8925
8926 if (tf != f
8927 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8928 && !FRAME_MINIBUF_ONLY_P (tf)
8929 && !EQ (other_frame, tip_frame)
8930 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8931 break;
8932 }
8933
8934 /* Set global variable indicating that multiple frames exist. */
8935 multiple_frames = CONSP (tail);
8936
8937 /* Switch to the buffer of selected window of the frame. Set up
8938 mode_line_target so that display_mode_element will output into
8939 mode_line_noprop_buf; then display the title. */
8940 record_unwind_protect (unwind_format_mode_line,
8941 format_mode_line_unwind_data (current_buffer, 0));
8942
8943 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8944 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8945
8946 mode_line_target = MODE_LINE_TITLE;
8947 title_start = MODE_LINE_NOPROP_LEN (0);
8948 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8949 NULL, DEFAULT_FACE_ID);
8950 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8951 len = MODE_LINE_NOPROP_LEN (title_start);
8952 title = mode_line_noprop_buf + title_start;
8953 unbind_to (count, Qnil);
8954
8955 /* Set the title only if it's changed. This avoids consing in
8956 the common case where it hasn't. (If it turns out that we've
8957 already wasted too much time by walking through the list with
8958 display_mode_element, then we might need to optimize at a
8959 higher level than this.) */
8960 if (! STRINGP (f->name)
8961 || SBYTES (f->name) != len
8962 || bcmp (title, SDATA (f->name), len) != 0)
8963 x_implicitly_set_name (f, make_string (title, len), Qnil);
8964 }
8965 }
8966
8967 #endif /* not HAVE_WINDOW_SYSTEM */
8968
8969
8970
8971 \f
8972 /***********************************************************************
8973 Menu Bars
8974 ***********************************************************************/
8975
8976
8977 /* Prepare for redisplay by updating menu-bar item lists when
8978 appropriate. This can call eval. */
8979
8980 void
8981 prepare_menu_bars ()
8982 {
8983 int all_windows;
8984 struct gcpro gcpro1, gcpro2;
8985 struct frame *f;
8986 Lisp_Object tooltip_frame;
8987
8988 #ifdef HAVE_WINDOW_SYSTEM
8989 tooltip_frame = tip_frame;
8990 #else
8991 tooltip_frame = Qnil;
8992 #endif
8993
8994 /* Update all frame titles based on their buffer names, etc. We do
8995 this before the menu bars so that the buffer-menu will show the
8996 up-to-date frame titles. */
8997 #ifdef HAVE_WINDOW_SYSTEM
8998 if (windows_or_buffers_changed || update_mode_lines)
8999 {
9000 Lisp_Object tail, frame;
9001
9002 FOR_EACH_FRAME (tail, frame)
9003 {
9004 f = XFRAME (frame);
9005 if (!EQ (frame, tooltip_frame)
9006 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9007 x_consider_frame_title (frame);
9008 }
9009 }
9010 #endif /* HAVE_WINDOW_SYSTEM */
9011
9012 /* Update the menu bar item lists, if appropriate. This has to be
9013 done before any actual redisplay or generation of display lines. */
9014 all_windows = (update_mode_lines
9015 || buffer_shared > 1
9016 || windows_or_buffers_changed);
9017 if (all_windows)
9018 {
9019 Lisp_Object tail, frame;
9020 int count = SPECPDL_INDEX ();
9021
9022 record_unwind_save_match_data ();
9023
9024 FOR_EACH_FRAME (tail, frame)
9025 {
9026 f = XFRAME (frame);
9027
9028 /* Ignore tooltip frame. */
9029 if (EQ (frame, tooltip_frame))
9030 continue;
9031
9032 /* If a window on this frame changed size, report that to
9033 the user and clear the size-change flag. */
9034 if (FRAME_WINDOW_SIZES_CHANGED (f))
9035 {
9036 Lisp_Object functions;
9037
9038 /* Clear flag first in case we get an error below. */
9039 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9040 functions = Vwindow_size_change_functions;
9041 GCPRO2 (tail, functions);
9042
9043 while (CONSP (functions))
9044 {
9045 call1 (XCAR (functions), frame);
9046 functions = XCDR (functions);
9047 }
9048 UNGCPRO;
9049 }
9050
9051 GCPRO1 (tail);
9052 update_menu_bar (f, 0);
9053 #ifdef HAVE_WINDOW_SYSTEM
9054 update_tool_bar (f, 0);
9055 #ifdef MAC_OS
9056 mac_update_title_bar (f, 0);
9057 #endif
9058 #endif
9059 UNGCPRO;
9060 }
9061
9062 unbind_to (count, Qnil);
9063 }
9064 else
9065 {
9066 struct frame *sf = SELECTED_FRAME ();
9067 update_menu_bar (sf, 1);
9068 #ifdef HAVE_WINDOW_SYSTEM
9069 update_tool_bar (sf, 1);
9070 #ifdef MAC_OS
9071 mac_update_title_bar (sf, 1);
9072 #endif
9073 #endif
9074 }
9075
9076 /* Motif needs this. See comment in xmenu.c. Turn it off when
9077 pending_menu_activation is not defined. */
9078 #ifdef USE_X_TOOLKIT
9079 pending_menu_activation = 0;
9080 #endif
9081 }
9082
9083
9084 /* Update the menu bar item list for frame F. This has to be done
9085 before we start to fill in any display lines, because it can call
9086 eval.
9087
9088 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
9089
9090 static void
9091 update_menu_bar (f, save_match_data)
9092 struct frame *f;
9093 int save_match_data;
9094 {
9095 Lisp_Object window;
9096 register struct window *w;
9097
9098 /* If called recursively during a menu update, do nothing. This can
9099 happen when, for instance, an activate-menubar-hook causes a
9100 redisplay. */
9101 if (inhibit_menubar_update)
9102 return;
9103
9104 window = FRAME_SELECTED_WINDOW (f);
9105 w = XWINDOW (window);
9106
9107 #if 0 /* The if statement below this if statement used to include the
9108 condition !NILP (w->update_mode_line), rather than using
9109 update_mode_lines directly, and this if statement may have
9110 been added to make that condition work. Now the if
9111 statement below matches its comment, this isn't needed. */
9112 if (update_mode_lines)
9113 w->update_mode_line = Qt;
9114 #endif
9115
9116 if (FRAME_WINDOW_P (f)
9117 ?
9118 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9119 || defined (USE_GTK)
9120 FRAME_EXTERNAL_MENU_BAR (f)
9121 #else
9122 FRAME_MENU_BAR_LINES (f) > 0
9123 #endif
9124 : FRAME_MENU_BAR_LINES (f) > 0)
9125 {
9126 /* If the user has switched buffers or windows, we need to
9127 recompute to reflect the new bindings. But we'll
9128 recompute when update_mode_lines is set too; that means
9129 that people can use force-mode-line-update to request
9130 that the menu bar be recomputed. The adverse effect on
9131 the rest of the redisplay algorithm is about the same as
9132 windows_or_buffers_changed anyway. */
9133 if (windows_or_buffers_changed
9134 /* This used to test w->update_mode_line, but we believe
9135 there is no need to recompute the menu in that case. */
9136 || update_mode_lines
9137 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9138 < BUF_MODIFF (XBUFFER (w->buffer)))
9139 != !NILP (w->last_had_star))
9140 || ((!NILP (Vtransient_mark_mode)
9141 && !NILP (XBUFFER (w->buffer)->mark_active))
9142 != !NILP (w->region_showing)))
9143 {
9144 struct buffer *prev = current_buffer;
9145 int count = SPECPDL_INDEX ();
9146
9147 specbind (Qinhibit_menubar_update, Qt);
9148
9149 set_buffer_internal_1 (XBUFFER (w->buffer));
9150 if (save_match_data)
9151 record_unwind_save_match_data ();
9152 if (NILP (Voverriding_local_map_menu_flag))
9153 {
9154 specbind (Qoverriding_terminal_local_map, Qnil);
9155 specbind (Qoverriding_local_map, Qnil);
9156 }
9157
9158 /* Run the Lucid hook. */
9159 safe_run_hooks (Qactivate_menubar_hook);
9160
9161 /* If it has changed current-menubar from previous value,
9162 really recompute the menu-bar from the value. */
9163 if (! NILP (Vlucid_menu_bar_dirty_flag))
9164 call0 (Qrecompute_lucid_menubar);
9165
9166 safe_run_hooks (Qmenu_bar_update_hook);
9167 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9168
9169 /* Redisplay the menu bar in case we changed it. */
9170 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9171 || defined (USE_GTK)
9172 if (FRAME_WINDOW_P (f))
9173 {
9174 #ifdef MAC_OS
9175 /* All frames on Mac OS share the same menubar. So only
9176 the selected frame should be allowed to set it. */
9177 if (f == SELECTED_FRAME ())
9178 #endif
9179 set_frame_menubar (f, 0, 0);
9180 }
9181 else
9182 /* On a terminal screen, the menu bar is an ordinary screen
9183 line, and this makes it get updated. */
9184 w->update_mode_line = Qt;
9185 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9186 /* In the non-toolkit version, the menu bar is an ordinary screen
9187 line, and this makes it get updated. */
9188 w->update_mode_line = Qt;
9189 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9190
9191 unbind_to (count, Qnil);
9192 set_buffer_internal_1 (prev);
9193 }
9194 }
9195 }
9196
9197
9198 \f
9199 /***********************************************************************
9200 Output Cursor
9201 ***********************************************************************/
9202
9203 #ifdef HAVE_WINDOW_SYSTEM
9204
9205 /* EXPORT:
9206 Nominal cursor position -- where to draw output.
9207 HPOS and VPOS are window relative glyph matrix coordinates.
9208 X and Y are window relative pixel coordinates. */
9209
9210 struct cursor_pos output_cursor;
9211
9212
9213 /* EXPORT:
9214 Set the global variable output_cursor to CURSOR. All cursor
9215 positions are relative to updated_window. */
9216
9217 void
9218 set_output_cursor (cursor)
9219 struct cursor_pos *cursor;
9220 {
9221 output_cursor.hpos = cursor->hpos;
9222 output_cursor.vpos = cursor->vpos;
9223 output_cursor.x = cursor->x;
9224 output_cursor.y = cursor->y;
9225 }
9226
9227
9228 /* EXPORT for RIF:
9229 Set a nominal cursor position.
9230
9231 HPOS and VPOS are column/row positions in a window glyph matrix. X
9232 and Y are window text area relative pixel positions.
9233
9234 If this is done during an update, updated_window will contain the
9235 window that is being updated and the position is the future output
9236 cursor position for that window. If updated_window is null, use
9237 selected_window and display the cursor at the given position. */
9238
9239 void
9240 x_cursor_to (vpos, hpos, y, x)
9241 int vpos, hpos, y, x;
9242 {
9243 struct window *w;
9244
9245 /* If updated_window is not set, work on selected_window. */
9246 if (updated_window)
9247 w = updated_window;
9248 else
9249 w = XWINDOW (selected_window);
9250
9251 /* Set the output cursor. */
9252 output_cursor.hpos = hpos;
9253 output_cursor.vpos = vpos;
9254 output_cursor.x = x;
9255 output_cursor.y = y;
9256
9257 /* If not called as part of an update, really display the cursor.
9258 This will also set the cursor position of W. */
9259 if (updated_window == NULL)
9260 {
9261 BLOCK_INPUT;
9262 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9263 if (rif->flush_display_optional)
9264 rif->flush_display_optional (SELECTED_FRAME ());
9265 UNBLOCK_INPUT;
9266 }
9267 }
9268
9269 #endif /* HAVE_WINDOW_SYSTEM */
9270
9271 \f
9272 /***********************************************************************
9273 Tool-bars
9274 ***********************************************************************/
9275
9276 #ifdef HAVE_WINDOW_SYSTEM
9277
9278 /* Where the mouse was last time we reported a mouse event. */
9279
9280 FRAME_PTR last_mouse_frame;
9281
9282 /* Tool-bar item index of the item on which a mouse button was pressed
9283 or -1. */
9284
9285 int last_tool_bar_item;
9286
9287
9288 /* Update the tool-bar item list for frame F. This has to be done
9289 before we start to fill in any display lines. Called from
9290 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9291 and restore it here. */
9292
9293 static void
9294 update_tool_bar (f, save_match_data)
9295 struct frame *f;
9296 int save_match_data;
9297 {
9298 #ifdef USE_GTK
9299 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9300 #else
9301 int do_update = WINDOWP (f->tool_bar_window)
9302 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9303 #endif
9304
9305 if (do_update)
9306 {
9307 Lisp_Object window;
9308 struct window *w;
9309
9310 window = FRAME_SELECTED_WINDOW (f);
9311 w = XWINDOW (window);
9312
9313 /* If the user has switched buffers or windows, we need to
9314 recompute to reflect the new bindings. But we'll
9315 recompute when update_mode_lines is set too; that means
9316 that people can use force-mode-line-update to request
9317 that the menu bar be recomputed. The adverse effect on
9318 the rest of the redisplay algorithm is about the same as
9319 windows_or_buffers_changed anyway. */
9320 if (windows_or_buffers_changed
9321 || !NILP (w->update_mode_line)
9322 || update_mode_lines
9323 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9324 < BUF_MODIFF (XBUFFER (w->buffer)))
9325 != !NILP (w->last_had_star))
9326 || ((!NILP (Vtransient_mark_mode)
9327 && !NILP (XBUFFER (w->buffer)->mark_active))
9328 != !NILP (w->region_showing)))
9329 {
9330 struct buffer *prev = current_buffer;
9331 int count = SPECPDL_INDEX ();
9332 Lisp_Object new_tool_bar;
9333 int new_n_tool_bar;
9334 struct gcpro gcpro1;
9335
9336 /* Set current_buffer to the buffer of the selected
9337 window of the frame, so that we get the right local
9338 keymaps. */
9339 set_buffer_internal_1 (XBUFFER (w->buffer));
9340
9341 /* Save match data, if we must. */
9342 if (save_match_data)
9343 record_unwind_save_match_data ();
9344
9345 /* Make sure that we don't accidentally use bogus keymaps. */
9346 if (NILP (Voverriding_local_map_menu_flag))
9347 {
9348 specbind (Qoverriding_terminal_local_map, Qnil);
9349 specbind (Qoverriding_local_map, Qnil);
9350 }
9351
9352 GCPRO1 (new_tool_bar);
9353
9354 /* Build desired tool-bar items from keymaps. */
9355 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9356 &new_n_tool_bar);
9357
9358 /* Redisplay the tool-bar if we changed it. */
9359 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9360 {
9361 /* Redisplay that happens asynchronously due to an expose event
9362 may access f->tool_bar_items. Make sure we update both
9363 variables within BLOCK_INPUT so no such event interrupts. */
9364 BLOCK_INPUT;
9365 f->tool_bar_items = new_tool_bar;
9366 f->n_tool_bar_items = new_n_tool_bar;
9367 w->update_mode_line = Qt;
9368 UNBLOCK_INPUT;
9369 }
9370
9371 UNGCPRO;
9372
9373 unbind_to (count, Qnil);
9374 set_buffer_internal_1 (prev);
9375 }
9376 }
9377 }
9378
9379
9380 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9381 F's desired tool-bar contents. F->tool_bar_items must have
9382 been set up previously by calling prepare_menu_bars. */
9383
9384 static void
9385 build_desired_tool_bar_string (f)
9386 struct frame *f;
9387 {
9388 int i, size, size_needed;
9389 struct gcpro gcpro1, gcpro2, gcpro3;
9390 Lisp_Object image, plist, props;
9391
9392 image = plist = props = Qnil;
9393 GCPRO3 (image, plist, props);
9394
9395 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9396 Otherwise, make a new string. */
9397
9398 /* The size of the string we might be able to reuse. */
9399 size = (STRINGP (f->desired_tool_bar_string)
9400 ? SCHARS (f->desired_tool_bar_string)
9401 : 0);
9402
9403 /* We need one space in the string for each image. */
9404 size_needed = f->n_tool_bar_items;
9405
9406 /* Reuse f->desired_tool_bar_string, if possible. */
9407 if (size < size_needed || NILP (f->desired_tool_bar_string))
9408 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9409 make_number (' '));
9410 else
9411 {
9412 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9413 Fremove_text_properties (make_number (0), make_number (size),
9414 props, f->desired_tool_bar_string);
9415 }
9416
9417 /* Put a `display' property on the string for the images to display,
9418 put a `menu_item' property on tool-bar items with a value that
9419 is the index of the item in F's tool-bar item vector. */
9420 for (i = 0; i < f->n_tool_bar_items; ++i)
9421 {
9422 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9423
9424 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9425 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9426 int hmargin, vmargin, relief, idx, end;
9427 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9428
9429 /* If image is a vector, choose the image according to the
9430 button state. */
9431 image = PROP (TOOL_BAR_ITEM_IMAGES);
9432 if (VECTORP (image))
9433 {
9434 if (enabled_p)
9435 idx = (selected_p
9436 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9437 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9438 else
9439 idx = (selected_p
9440 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9441 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9442
9443 xassert (ASIZE (image) >= idx);
9444 image = AREF (image, idx);
9445 }
9446 else
9447 idx = -1;
9448
9449 /* Ignore invalid image specifications. */
9450 if (!valid_image_p (image))
9451 continue;
9452
9453 /* Display the tool-bar button pressed, or depressed. */
9454 plist = Fcopy_sequence (XCDR (image));
9455
9456 /* Compute margin and relief to draw. */
9457 relief = (tool_bar_button_relief >= 0
9458 ? tool_bar_button_relief
9459 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9460 hmargin = vmargin = relief;
9461
9462 if (INTEGERP (Vtool_bar_button_margin)
9463 && XINT (Vtool_bar_button_margin) > 0)
9464 {
9465 hmargin += XFASTINT (Vtool_bar_button_margin);
9466 vmargin += XFASTINT (Vtool_bar_button_margin);
9467 }
9468 else if (CONSP (Vtool_bar_button_margin))
9469 {
9470 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9471 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9472 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9473
9474 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9475 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9476 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9477 }
9478
9479 if (auto_raise_tool_bar_buttons_p)
9480 {
9481 /* Add a `:relief' property to the image spec if the item is
9482 selected. */
9483 if (selected_p)
9484 {
9485 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9486 hmargin -= relief;
9487 vmargin -= relief;
9488 }
9489 }
9490 else
9491 {
9492 /* If image is selected, display it pressed, i.e. with a
9493 negative relief. If it's not selected, display it with a
9494 raised relief. */
9495 plist = Fplist_put (plist, QCrelief,
9496 (selected_p
9497 ? make_number (-relief)
9498 : make_number (relief)));
9499 hmargin -= relief;
9500 vmargin -= relief;
9501 }
9502
9503 /* Put a margin around the image. */
9504 if (hmargin || vmargin)
9505 {
9506 if (hmargin == vmargin)
9507 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9508 else
9509 plist = Fplist_put (plist, QCmargin,
9510 Fcons (make_number (hmargin),
9511 make_number (vmargin)));
9512 }
9513
9514 /* If button is not enabled, and we don't have special images
9515 for the disabled state, make the image appear disabled by
9516 applying an appropriate algorithm to it. */
9517 if (!enabled_p && idx < 0)
9518 plist = Fplist_put (plist, QCconversion, Qdisabled);
9519
9520 /* Put a `display' text property on the string for the image to
9521 display. Put a `menu-item' property on the string that gives
9522 the start of this item's properties in the tool-bar items
9523 vector. */
9524 image = Fcons (Qimage, plist);
9525 props = list4 (Qdisplay, image,
9526 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9527
9528 /* Let the last image hide all remaining spaces in the tool bar
9529 string. The string can be longer than needed when we reuse a
9530 previous string. */
9531 if (i + 1 == f->n_tool_bar_items)
9532 end = SCHARS (f->desired_tool_bar_string);
9533 else
9534 end = i + 1;
9535 Fadd_text_properties (make_number (i), make_number (end),
9536 props, f->desired_tool_bar_string);
9537 #undef PROP
9538 }
9539
9540 UNGCPRO;
9541 }
9542
9543
9544 /* Display one line of the tool-bar of frame IT->f.
9545
9546 HEIGHT specifies the desired height of the tool-bar line.
9547 If the actual height of the glyph row is less than HEIGHT, the
9548 row's height is increased to HEIGHT, and the icons are centered
9549 vertically in the new height.
9550
9551 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9552 count a final empty row in case the tool-bar width exactly matches
9553 the window width.
9554 */
9555
9556 static void
9557 display_tool_bar_line (it, height)
9558 struct it *it;
9559 int height;
9560 {
9561 struct glyph_row *row = it->glyph_row;
9562 int max_x = it->last_visible_x;
9563 struct glyph *last;
9564
9565 prepare_desired_row (row);
9566 row->y = it->current_y;
9567
9568 /* Note that this isn't made use of if the face hasn't a box,
9569 so there's no need to check the face here. */
9570 it->start_of_box_run_p = 1;
9571
9572 while (it->current_x < max_x)
9573 {
9574 int x, n_glyphs_before, i, nglyphs;
9575 struct it it_before;
9576
9577 /* Get the next display element. */
9578 if (!get_next_display_element (it))
9579 {
9580 /* Don't count empty row if we are counting needed tool-bar lines. */
9581 if (height < 0 && !it->hpos)
9582 return;
9583 break;
9584 }
9585
9586 /* Produce glyphs. */
9587 n_glyphs_before = row->used[TEXT_AREA];
9588 it_before = *it;
9589
9590 PRODUCE_GLYPHS (it);
9591
9592 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9593 i = 0;
9594 x = it_before.current_x;
9595 while (i < nglyphs)
9596 {
9597 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9598
9599 if (x + glyph->pixel_width > max_x)
9600 {
9601 /* Glyph doesn't fit on line. Backtrack. */
9602 row->used[TEXT_AREA] = n_glyphs_before;
9603 *it = it_before;
9604 goto out;
9605 }
9606
9607 ++it->hpos;
9608 x += glyph->pixel_width;
9609 ++i;
9610 }
9611
9612 /* Stop at line ends. */
9613 if (ITERATOR_AT_END_OF_LINE_P (it))
9614 break;
9615
9616 set_iterator_to_next (it, 1);
9617 }
9618
9619 out:;
9620
9621 row->displays_text_p = row->used[TEXT_AREA] != 0;
9622 /* Use default face for the border below the tool bar. */
9623 if (!row->displays_text_p)
9624 it->face_id = DEFAULT_FACE_ID;
9625 extend_face_to_end_of_line (it);
9626 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9627 last->right_box_line_p = 1;
9628 if (last == row->glyphs[TEXT_AREA])
9629 last->left_box_line_p = 1;
9630
9631 /* Make line the desired height and center it vertically. */
9632 if ((height -= it->max_ascent + it->max_descent) > 0)
9633 {
9634 /* Don't add more than one line height. */
9635 height %= FRAME_LINE_HEIGHT (it->f);
9636 it->max_ascent += height / 2;
9637 it->max_descent += (height + 1) / 2;
9638 }
9639
9640 compute_line_metrics (it);
9641
9642 /* If line is empty, make it occupy the rest of the tool-bar. */
9643 if (!row->displays_text_p)
9644 {
9645 row->height = row->phys_height = it->last_visible_y - row->y;
9646 row->ascent = row->phys_ascent = 0;
9647 row->extra_line_spacing = 0;
9648 }
9649
9650 row->full_width_p = 1;
9651 row->continued_p = 0;
9652 row->truncated_on_left_p = 0;
9653 row->truncated_on_right_p = 0;
9654
9655 it->current_x = it->hpos = 0;
9656 it->current_y += row->height;
9657 ++it->vpos;
9658 ++it->glyph_row;
9659 }
9660
9661
9662 /* Value is the number of screen lines needed to make all tool-bar
9663 items of frame F visible. The number of actual rows needed is
9664 returned in *N_ROWS if non-NULL. */
9665
9666 static int
9667 tool_bar_lines_needed (f, n_rows)
9668 struct frame *f;
9669 int *n_rows;
9670 {
9671 struct window *w = XWINDOW (f->tool_bar_window);
9672 struct it it;
9673 struct glyph_row *temp_row = w->desired_matrix->rows;
9674
9675 /* Initialize an iterator for iteration over
9676 F->desired_tool_bar_string in the tool-bar window of frame F. */
9677 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9678 it.first_visible_x = 0;
9679 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9680 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9681
9682 while (!ITERATOR_AT_END_P (&it))
9683 {
9684 clear_glyph_row (temp_row);
9685 it.glyph_row = temp_row;
9686 display_tool_bar_line (&it, -1);
9687 }
9688 clear_glyph_row (temp_row);
9689
9690 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9691 if (n_rows)
9692 *n_rows = it.vpos > 0 ? it.vpos : -1;
9693
9694 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9695 }
9696
9697
9698 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9699 0, 1, 0,
9700 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9701 (frame)
9702 Lisp_Object frame;
9703 {
9704 struct frame *f;
9705 struct window *w;
9706 int nlines = 0;
9707
9708 if (NILP (frame))
9709 frame = selected_frame;
9710 else
9711 CHECK_FRAME (frame);
9712 f = XFRAME (frame);
9713
9714 if (WINDOWP (f->tool_bar_window)
9715 || (w = XWINDOW (f->tool_bar_window),
9716 WINDOW_TOTAL_LINES (w) > 0))
9717 {
9718 update_tool_bar (f, 1);
9719 if (f->n_tool_bar_items)
9720 {
9721 build_desired_tool_bar_string (f);
9722 nlines = tool_bar_lines_needed (f, NULL);
9723 }
9724 }
9725
9726 return make_number (nlines);
9727 }
9728
9729
9730 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9731 height should be changed. */
9732
9733 static int
9734 redisplay_tool_bar (f)
9735 struct frame *f;
9736 {
9737 struct window *w;
9738 struct it it;
9739 struct glyph_row *row;
9740 int change_height_p = 0;
9741
9742 #ifdef USE_GTK
9743 if (FRAME_EXTERNAL_TOOL_BAR (f))
9744 update_frame_tool_bar (f);
9745 return 0;
9746 #endif
9747
9748 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9749 do anything. This means you must start with tool-bar-lines
9750 non-zero to get the auto-sizing effect. Or in other words, you
9751 can turn off tool-bars by specifying tool-bar-lines zero. */
9752 if (!WINDOWP (f->tool_bar_window)
9753 || (w = XWINDOW (f->tool_bar_window),
9754 WINDOW_TOTAL_LINES (w) == 0))
9755 return 0;
9756
9757 /* Set up an iterator for the tool-bar window. */
9758 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9759 it.first_visible_x = 0;
9760 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9761 row = it.glyph_row;
9762
9763 /* Build a string that represents the contents of the tool-bar. */
9764 build_desired_tool_bar_string (f);
9765 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9766
9767 if (f->n_tool_bar_rows == 0)
9768 {
9769 int nlines;
9770
9771 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9772 nlines != WINDOW_TOTAL_LINES (w)))
9773 {
9774 extern Lisp_Object Qtool_bar_lines;
9775 Lisp_Object frame;
9776 int old_height = WINDOW_TOTAL_LINES (w);
9777
9778 XSETFRAME (frame, f);
9779 clear_glyph_matrix (w->desired_matrix);
9780 Fmodify_frame_parameters (frame,
9781 Fcons (Fcons (Qtool_bar_lines,
9782 make_number (nlines)),
9783 Qnil));
9784 if (WINDOW_TOTAL_LINES (w) != old_height)
9785 {
9786 fonts_changed_p = 1;
9787 return 1;
9788 }
9789 }
9790 }
9791
9792 /* Display as many lines as needed to display all tool-bar items. */
9793
9794 if (f->n_tool_bar_rows > 0)
9795 {
9796 int border, rows, height, extra;
9797
9798 if (INTEGERP (Vtool_bar_border))
9799 border = XINT (Vtool_bar_border);
9800 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9801 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9802 else if (EQ (Vtool_bar_border, Qborder_width))
9803 border = f->border_width;
9804 else
9805 border = 0;
9806 if (border < 0)
9807 border = 0;
9808
9809 rows = f->n_tool_bar_rows;
9810 height = max (1, (it.last_visible_y - border) / rows);
9811 extra = it.last_visible_y - border - height * rows;
9812
9813 while (it.current_y < it.last_visible_y)
9814 {
9815 int h = 0;
9816 if (extra > 0 && rows-- > 0)
9817 {
9818 h = (extra + rows - 1) / rows;
9819 extra -= h;
9820 }
9821 display_tool_bar_line (&it, height + h);
9822 }
9823 }
9824 else
9825 {
9826 while (it.current_y < it.last_visible_y)
9827 display_tool_bar_line (&it, 0);
9828 }
9829
9830 /* It doesn't make much sense to try scrolling in the tool-bar
9831 window, so don't do it. */
9832 w->desired_matrix->no_scrolling_p = 1;
9833 w->must_be_updated_p = 1;
9834
9835 if (auto_resize_tool_bars_p)
9836 {
9837 int nlines;
9838
9839 /* If we couldn't display everything, change the tool-bar's
9840 height. */
9841 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9842 change_height_p = 1;
9843
9844 /* If there are blank lines at the end, except for a partially
9845 visible blank line at the end that is smaller than
9846 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9847 row = it.glyph_row - 1;
9848 if (!row->displays_text_p
9849 && row->height >= FRAME_LINE_HEIGHT (f))
9850 change_height_p = 1;
9851
9852 /* If row displays tool-bar items, but is partially visible,
9853 change the tool-bar's height. */
9854 if (row->displays_text_p
9855 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9856 change_height_p = 1;
9857
9858 /* Resize windows as needed by changing the `tool-bar-lines'
9859 frame parameter. */
9860 if (change_height_p
9861 && (nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9862 nlines != WINDOW_TOTAL_LINES (w)))
9863 {
9864 extern Lisp_Object Qtool_bar_lines;
9865 Lisp_Object frame;
9866 int old_height = WINDOW_TOTAL_LINES (w);
9867
9868 XSETFRAME (frame, f);
9869 clear_glyph_matrix (w->desired_matrix);
9870 Fmodify_frame_parameters (frame,
9871 Fcons (Fcons (Qtool_bar_lines,
9872 make_number (nlines)),
9873 Qnil));
9874 if (WINDOW_TOTAL_LINES (w) != old_height)
9875 fonts_changed_p = 1;
9876 }
9877 }
9878
9879 return change_height_p;
9880 }
9881
9882
9883 /* Get information about the tool-bar item which is displayed in GLYPH
9884 on frame F. Return in *PROP_IDX the index where tool-bar item
9885 properties start in F->tool_bar_items. Value is zero if
9886 GLYPH doesn't display a tool-bar item. */
9887
9888 static int
9889 tool_bar_item_info (f, glyph, prop_idx)
9890 struct frame *f;
9891 struct glyph *glyph;
9892 int *prop_idx;
9893 {
9894 Lisp_Object prop;
9895 int success_p;
9896 int charpos;
9897
9898 /* This function can be called asynchronously, which means we must
9899 exclude any possibility that Fget_text_property signals an
9900 error. */
9901 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9902 charpos = max (0, charpos);
9903
9904 /* Get the text property `menu-item' at pos. The value of that
9905 property is the start index of this item's properties in
9906 F->tool_bar_items. */
9907 prop = Fget_text_property (make_number (charpos),
9908 Qmenu_item, f->current_tool_bar_string);
9909 if (INTEGERP (prop))
9910 {
9911 *prop_idx = XINT (prop);
9912 success_p = 1;
9913 }
9914 else
9915 success_p = 0;
9916
9917 return success_p;
9918 }
9919
9920 \f
9921 /* Get information about the tool-bar item at position X/Y on frame F.
9922 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9923 the current matrix of the tool-bar window of F, or NULL if not
9924 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9925 item in F->tool_bar_items. Value is
9926
9927 -1 if X/Y is not on a tool-bar item
9928 0 if X/Y is on the same item that was highlighted before.
9929 1 otherwise. */
9930
9931 static int
9932 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9933 struct frame *f;
9934 int x, y;
9935 struct glyph **glyph;
9936 int *hpos, *vpos, *prop_idx;
9937 {
9938 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9939 struct window *w = XWINDOW (f->tool_bar_window);
9940 int area;
9941
9942 /* Find the glyph under X/Y. */
9943 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9944 if (*glyph == NULL)
9945 return -1;
9946
9947 /* Get the start of this tool-bar item's properties in
9948 f->tool_bar_items. */
9949 if (!tool_bar_item_info (f, *glyph, prop_idx))
9950 return -1;
9951
9952 /* Is mouse on the highlighted item? */
9953 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9954 && *vpos >= dpyinfo->mouse_face_beg_row
9955 && *vpos <= dpyinfo->mouse_face_end_row
9956 && (*vpos > dpyinfo->mouse_face_beg_row
9957 || *hpos >= dpyinfo->mouse_face_beg_col)
9958 && (*vpos < dpyinfo->mouse_face_end_row
9959 || *hpos < dpyinfo->mouse_face_end_col
9960 || dpyinfo->mouse_face_past_end))
9961 return 0;
9962
9963 return 1;
9964 }
9965
9966
9967 /* EXPORT:
9968 Handle mouse button event on the tool-bar of frame F, at
9969 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9970 0 for button release. MODIFIERS is event modifiers for button
9971 release. */
9972
9973 void
9974 handle_tool_bar_click (f, x, y, down_p, modifiers)
9975 struct frame *f;
9976 int x, y, down_p;
9977 unsigned int modifiers;
9978 {
9979 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9980 struct window *w = XWINDOW (f->tool_bar_window);
9981 int hpos, vpos, prop_idx;
9982 struct glyph *glyph;
9983 Lisp_Object enabled_p;
9984
9985 /* If not on the highlighted tool-bar item, return. */
9986 frame_to_window_pixel_xy (w, &x, &y);
9987 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9988 return;
9989
9990 /* If item is disabled, do nothing. */
9991 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9992 if (NILP (enabled_p))
9993 return;
9994
9995 if (down_p)
9996 {
9997 /* Show item in pressed state. */
9998 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9999 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10000 last_tool_bar_item = prop_idx;
10001 }
10002 else
10003 {
10004 Lisp_Object key, frame;
10005 struct input_event event;
10006 EVENT_INIT (event);
10007
10008 /* Show item in released state. */
10009 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10010 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10011
10012 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10013
10014 XSETFRAME (frame, f);
10015 event.kind = TOOL_BAR_EVENT;
10016 event.frame_or_window = frame;
10017 event.arg = frame;
10018 kbd_buffer_store_event (&event);
10019
10020 event.kind = TOOL_BAR_EVENT;
10021 event.frame_or_window = frame;
10022 event.arg = key;
10023 event.modifiers = modifiers;
10024 kbd_buffer_store_event (&event);
10025 last_tool_bar_item = -1;
10026 }
10027 }
10028
10029
10030 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10031 tool-bar window-relative coordinates X/Y. Called from
10032 note_mouse_highlight. */
10033
10034 static void
10035 note_tool_bar_highlight (f, x, y)
10036 struct frame *f;
10037 int x, y;
10038 {
10039 Lisp_Object window = f->tool_bar_window;
10040 struct window *w = XWINDOW (window);
10041 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10042 int hpos, vpos;
10043 struct glyph *glyph;
10044 struct glyph_row *row;
10045 int i;
10046 Lisp_Object enabled_p;
10047 int prop_idx;
10048 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10049 int mouse_down_p, rc;
10050
10051 /* Function note_mouse_highlight is called with negative x(y
10052 values when mouse moves outside of the frame. */
10053 if (x <= 0 || y <= 0)
10054 {
10055 clear_mouse_face (dpyinfo);
10056 return;
10057 }
10058
10059 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10060 if (rc < 0)
10061 {
10062 /* Not on tool-bar item. */
10063 clear_mouse_face (dpyinfo);
10064 return;
10065 }
10066 else if (rc == 0)
10067 /* On same tool-bar item as before. */
10068 goto set_help_echo;
10069
10070 clear_mouse_face (dpyinfo);
10071
10072 /* Mouse is down, but on different tool-bar item? */
10073 mouse_down_p = (dpyinfo->grabbed
10074 && f == last_mouse_frame
10075 && FRAME_LIVE_P (f));
10076 if (mouse_down_p
10077 && last_tool_bar_item != prop_idx)
10078 return;
10079
10080 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10081 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10082
10083 /* If tool-bar item is not enabled, don't highlight it. */
10084 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10085 if (!NILP (enabled_p))
10086 {
10087 /* Compute the x-position of the glyph. In front and past the
10088 image is a space. We include this in the highlighted area. */
10089 row = MATRIX_ROW (w->current_matrix, vpos);
10090 for (i = x = 0; i < hpos; ++i)
10091 x += row->glyphs[TEXT_AREA][i].pixel_width;
10092
10093 /* Record this as the current active region. */
10094 dpyinfo->mouse_face_beg_col = hpos;
10095 dpyinfo->mouse_face_beg_row = vpos;
10096 dpyinfo->mouse_face_beg_x = x;
10097 dpyinfo->mouse_face_beg_y = row->y;
10098 dpyinfo->mouse_face_past_end = 0;
10099
10100 dpyinfo->mouse_face_end_col = hpos + 1;
10101 dpyinfo->mouse_face_end_row = vpos;
10102 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10103 dpyinfo->mouse_face_end_y = row->y;
10104 dpyinfo->mouse_face_window = window;
10105 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10106
10107 /* Display it as active. */
10108 show_mouse_face (dpyinfo, draw);
10109 dpyinfo->mouse_face_image_state = draw;
10110 }
10111
10112 set_help_echo:
10113
10114 /* Set help_echo_string to a help string to display for this tool-bar item.
10115 XTread_socket does the rest. */
10116 help_echo_object = help_echo_window = Qnil;
10117 help_echo_pos = -1;
10118 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10119 if (NILP (help_echo_string))
10120 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10121 }
10122
10123 #endif /* HAVE_WINDOW_SYSTEM */
10124
10125
10126 \f
10127 /************************************************************************
10128 Horizontal scrolling
10129 ************************************************************************/
10130
10131 static int hscroll_window_tree P_ ((Lisp_Object));
10132 static int hscroll_windows P_ ((Lisp_Object));
10133
10134 /* For all leaf windows in the window tree rooted at WINDOW, set their
10135 hscroll value so that PT is (i) visible in the window, and (ii) so
10136 that it is not within a certain margin at the window's left and
10137 right border. Value is non-zero if any window's hscroll has been
10138 changed. */
10139
10140 static int
10141 hscroll_window_tree (window)
10142 Lisp_Object window;
10143 {
10144 int hscrolled_p = 0;
10145 int hscroll_relative_p = FLOATP (Vhscroll_step);
10146 int hscroll_step_abs = 0;
10147 double hscroll_step_rel = 0;
10148
10149 if (hscroll_relative_p)
10150 {
10151 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10152 if (hscroll_step_rel < 0)
10153 {
10154 hscroll_relative_p = 0;
10155 hscroll_step_abs = 0;
10156 }
10157 }
10158 else if (INTEGERP (Vhscroll_step))
10159 {
10160 hscroll_step_abs = XINT (Vhscroll_step);
10161 if (hscroll_step_abs < 0)
10162 hscroll_step_abs = 0;
10163 }
10164 else
10165 hscroll_step_abs = 0;
10166
10167 while (WINDOWP (window))
10168 {
10169 struct window *w = XWINDOW (window);
10170
10171 if (WINDOWP (w->hchild))
10172 hscrolled_p |= hscroll_window_tree (w->hchild);
10173 else if (WINDOWP (w->vchild))
10174 hscrolled_p |= hscroll_window_tree (w->vchild);
10175 else if (w->cursor.vpos >= 0)
10176 {
10177 int h_margin;
10178 int text_area_width;
10179 struct glyph_row *current_cursor_row
10180 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10181 struct glyph_row *desired_cursor_row
10182 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10183 struct glyph_row *cursor_row
10184 = (desired_cursor_row->enabled_p
10185 ? desired_cursor_row
10186 : current_cursor_row);
10187
10188 text_area_width = window_box_width (w, TEXT_AREA);
10189
10190 /* Scroll when cursor is inside this scroll margin. */
10191 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10192
10193 if ((XFASTINT (w->hscroll)
10194 && w->cursor.x <= h_margin)
10195 || (cursor_row->enabled_p
10196 && cursor_row->truncated_on_right_p
10197 && (w->cursor.x >= text_area_width - h_margin)))
10198 {
10199 struct it it;
10200 int hscroll;
10201 struct buffer *saved_current_buffer;
10202 int pt;
10203 int wanted_x;
10204
10205 /* Find point in a display of infinite width. */
10206 saved_current_buffer = current_buffer;
10207 current_buffer = XBUFFER (w->buffer);
10208
10209 if (w == XWINDOW (selected_window))
10210 pt = BUF_PT (current_buffer);
10211 else
10212 {
10213 pt = marker_position (w->pointm);
10214 pt = max (BEGV, pt);
10215 pt = min (ZV, pt);
10216 }
10217
10218 /* Move iterator to pt starting at cursor_row->start in
10219 a line with infinite width. */
10220 init_to_row_start (&it, w, cursor_row);
10221 it.last_visible_x = INFINITY;
10222 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10223 current_buffer = saved_current_buffer;
10224
10225 /* Position cursor in window. */
10226 if (!hscroll_relative_p && hscroll_step_abs == 0)
10227 hscroll = max (0, (it.current_x
10228 - (ITERATOR_AT_END_OF_LINE_P (&it)
10229 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10230 : (text_area_width / 2))))
10231 / FRAME_COLUMN_WIDTH (it.f);
10232 else if (w->cursor.x >= text_area_width - h_margin)
10233 {
10234 if (hscroll_relative_p)
10235 wanted_x = text_area_width * (1 - hscroll_step_rel)
10236 - h_margin;
10237 else
10238 wanted_x = text_area_width
10239 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10240 - h_margin;
10241 hscroll
10242 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10243 }
10244 else
10245 {
10246 if (hscroll_relative_p)
10247 wanted_x = text_area_width * hscroll_step_rel
10248 + h_margin;
10249 else
10250 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10251 + h_margin;
10252 hscroll
10253 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10254 }
10255 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10256
10257 /* Don't call Fset_window_hscroll if value hasn't
10258 changed because it will prevent redisplay
10259 optimizations. */
10260 if (XFASTINT (w->hscroll) != hscroll)
10261 {
10262 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10263 w->hscroll = make_number (hscroll);
10264 hscrolled_p = 1;
10265 }
10266 }
10267 }
10268
10269 window = w->next;
10270 }
10271
10272 /* Value is non-zero if hscroll of any leaf window has been changed. */
10273 return hscrolled_p;
10274 }
10275
10276
10277 /* Set hscroll so that cursor is visible and not inside horizontal
10278 scroll margins for all windows in the tree rooted at WINDOW. See
10279 also hscroll_window_tree above. Value is non-zero if any window's
10280 hscroll has been changed. If it has, desired matrices on the frame
10281 of WINDOW are cleared. */
10282
10283 static int
10284 hscroll_windows (window)
10285 Lisp_Object window;
10286 {
10287 int hscrolled_p;
10288
10289 if (automatic_hscrolling_p)
10290 {
10291 hscrolled_p = hscroll_window_tree (window);
10292 if (hscrolled_p)
10293 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10294 }
10295 else
10296 hscrolled_p = 0;
10297 return hscrolled_p;
10298 }
10299
10300
10301 \f
10302 /************************************************************************
10303 Redisplay
10304 ************************************************************************/
10305
10306 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10307 to a non-zero value. This is sometimes handy to have in a debugger
10308 session. */
10309
10310 #if GLYPH_DEBUG
10311
10312 /* First and last unchanged row for try_window_id. */
10313
10314 int debug_first_unchanged_at_end_vpos;
10315 int debug_last_unchanged_at_beg_vpos;
10316
10317 /* Delta vpos and y. */
10318
10319 int debug_dvpos, debug_dy;
10320
10321 /* Delta in characters and bytes for try_window_id. */
10322
10323 int debug_delta, debug_delta_bytes;
10324
10325 /* Values of window_end_pos and window_end_vpos at the end of
10326 try_window_id. */
10327
10328 EMACS_INT debug_end_pos, debug_end_vpos;
10329
10330 /* Append a string to W->desired_matrix->method. FMT is a printf
10331 format string. A1...A9 are a supplement for a variable-length
10332 argument list. If trace_redisplay_p is non-zero also printf the
10333 resulting string to stderr. */
10334
10335 static void
10336 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10337 struct window *w;
10338 char *fmt;
10339 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10340 {
10341 char buffer[512];
10342 char *method = w->desired_matrix->method;
10343 int len = strlen (method);
10344 int size = sizeof w->desired_matrix->method;
10345 int remaining = size - len - 1;
10346
10347 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10348 if (len && remaining)
10349 {
10350 method[len] = '|';
10351 --remaining, ++len;
10352 }
10353
10354 strncpy (method + len, buffer, remaining);
10355
10356 if (trace_redisplay_p)
10357 fprintf (stderr, "%p (%s): %s\n",
10358 w,
10359 ((BUFFERP (w->buffer)
10360 && STRINGP (XBUFFER (w->buffer)->name))
10361 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10362 : "no buffer"),
10363 buffer);
10364 }
10365
10366 #endif /* GLYPH_DEBUG */
10367
10368
10369 /* Value is non-zero if all changes in window W, which displays
10370 current_buffer, are in the text between START and END. START is a
10371 buffer position, END is given as a distance from Z. Used in
10372 redisplay_internal for display optimization. */
10373
10374 static INLINE int
10375 text_outside_line_unchanged_p (w, start, end)
10376 struct window *w;
10377 int start, end;
10378 {
10379 int unchanged_p = 1;
10380
10381 /* If text or overlays have changed, see where. */
10382 if (XFASTINT (w->last_modified) < MODIFF
10383 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10384 {
10385 /* Gap in the line? */
10386 if (GPT < start || Z - GPT < end)
10387 unchanged_p = 0;
10388
10389 /* Changes start in front of the line, or end after it? */
10390 if (unchanged_p
10391 && (BEG_UNCHANGED < start - 1
10392 || END_UNCHANGED < end))
10393 unchanged_p = 0;
10394
10395 /* If selective display, can't optimize if changes start at the
10396 beginning of the line. */
10397 if (unchanged_p
10398 && INTEGERP (current_buffer->selective_display)
10399 && XINT (current_buffer->selective_display) > 0
10400 && (BEG_UNCHANGED < start || GPT <= start))
10401 unchanged_p = 0;
10402
10403 /* If there are overlays at the start or end of the line, these
10404 may have overlay strings with newlines in them. A change at
10405 START, for instance, may actually concern the display of such
10406 overlay strings as well, and they are displayed on different
10407 lines. So, quickly rule out this case. (For the future, it
10408 might be desirable to implement something more telling than
10409 just BEG/END_UNCHANGED.) */
10410 if (unchanged_p)
10411 {
10412 if (BEG + BEG_UNCHANGED == start
10413 && overlay_touches_p (start))
10414 unchanged_p = 0;
10415 if (END_UNCHANGED == end
10416 && overlay_touches_p (Z - end))
10417 unchanged_p = 0;
10418 }
10419 }
10420
10421 return unchanged_p;
10422 }
10423
10424
10425 /* Do a frame update, taking possible shortcuts into account. This is
10426 the main external entry point for redisplay.
10427
10428 If the last redisplay displayed an echo area message and that message
10429 is no longer requested, we clear the echo area or bring back the
10430 mini-buffer if that is in use. */
10431
10432 void
10433 redisplay ()
10434 {
10435 redisplay_internal (0);
10436 }
10437
10438
10439 static Lisp_Object
10440 overlay_arrow_string_or_property (var)
10441 Lisp_Object var;
10442 {
10443 Lisp_Object val;
10444
10445 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10446 return val;
10447
10448 return Voverlay_arrow_string;
10449 }
10450
10451 /* Return 1 if there are any overlay-arrows in current_buffer. */
10452 static int
10453 overlay_arrow_in_current_buffer_p ()
10454 {
10455 Lisp_Object vlist;
10456
10457 for (vlist = Voverlay_arrow_variable_list;
10458 CONSP (vlist);
10459 vlist = XCDR (vlist))
10460 {
10461 Lisp_Object var = XCAR (vlist);
10462 Lisp_Object val;
10463
10464 if (!SYMBOLP (var))
10465 continue;
10466 val = find_symbol_value (var);
10467 if (MARKERP (val)
10468 && current_buffer == XMARKER (val)->buffer)
10469 return 1;
10470 }
10471 return 0;
10472 }
10473
10474
10475 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10476 has changed. */
10477
10478 static int
10479 overlay_arrows_changed_p ()
10480 {
10481 Lisp_Object vlist;
10482
10483 for (vlist = Voverlay_arrow_variable_list;
10484 CONSP (vlist);
10485 vlist = XCDR (vlist))
10486 {
10487 Lisp_Object var = XCAR (vlist);
10488 Lisp_Object val, pstr;
10489
10490 if (!SYMBOLP (var))
10491 continue;
10492 val = find_symbol_value (var);
10493 if (!MARKERP (val))
10494 continue;
10495 if (! EQ (COERCE_MARKER (val),
10496 Fget (var, Qlast_arrow_position))
10497 || ! (pstr = overlay_arrow_string_or_property (var),
10498 EQ (pstr, Fget (var, Qlast_arrow_string))))
10499 return 1;
10500 }
10501 return 0;
10502 }
10503
10504 /* Mark overlay arrows to be updated on next redisplay. */
10505
10506 static void
10507 update_overlay_arrows (up_to_date)
10508 int up_to_date;
10509 {
10510 Lisp_Object vlist;
10511
10512 for (vlist = Voverlay_arrow_variable_list;
10513 CONSP (vlist);
10514 vlist = XCDR (vlist))
10515 {
10516 Lisp_Object var = XCAR (vlist);
10517
10518 if (!SYMBOLP (var))
10519 continue;
10520
10521 if (up_to_date > 0)
10522 {
10523 Lisp_Object val = find_symbol_value (var);
10524 Fput (var, Qlast_arrow_position,
10525 COERCE_MARKER (val));
10526 Fput (var, Qlast_arrow_string,
10527 overlay_arrow_string_or_property (var));
10528 }
10529 else if (up_to_date < 0
10530 || !NILP (Fget (var, Qlast_arrow_position)))
10531 {
10532 Fput (var, Qlast_arrow_position, Qt);
10533 Fput (var, Qlast_arrow_string, Qt);
10534 }
10535 }
10536 }
10537
10538
10539 /* Return overlay arrow string to display at row.
10540 Return integer (bitmap number) for arrow bitmap in left fringe.
10541 Return nil if no overlay arrow. */
10542
10543 static Lisp_Object
10544 overlay_arrow_at_row (it, row)
10545 struct it *it;
10546 struct glyph_row *row;
10547 {
10548 Lisp_Object vlist;
10549
10550 for (vlist = Voverlay_arrow_variable_list;
10551 CONSP (vlist);
10552 vlist = XCDR (vlist))
10553 {
10554 Lisp_Object var = XCAR (vlist);
10555 Lisp_Object val;
10556
10557 if (!SYMBOLP (var))
10558 continue;
10559
10560 val = find_symbol_value (var);
10561
10562 if (MARKERP (val)
10563 && current_buffer == XMARKER (val)->buffer
10564 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10565 {
10566 if (FRAME_WINDOW_P (it->f)
10567 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10568 {
10569 #ifdef HAVE_WINDOW_SYSTEM
10570 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10571 {
10572 int fringe_bitmap;
10573 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10574 return make_number (fringe_bitmap);
10575 }
10576 #endif
10577 return make_number (-1); /* Use default arrow bitmap */
10578 }
10579 return overlay_arrow_string_or_property (var);
10580 }
10581 }
10582
10583 return Qnil;
10584 }
10585
10586 /* Return 1 if point moved out of or into a composition. Otherwise
10587 return 0. PREV_BUF and PREV_PT are the last point buffer and
10588 position. BUF and PT are the current point buffer and position. */
10589
10590 int
10591 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10592 struct buffer *prev_buf, *buf;
10593 int prev_pt, pt;
10594 {
10595 int start, end;
10596 Lisp_Object prop;
10597 Lisp_Object buffer;
10598
10599 XSETBUFFER (buffer, buf);
10600 /* Check a composition at the last point if point moved within the
10601 same buffer. */
10602 if (prev_buf == buf)
10603 {
10604 if (prev_pt == pt)
10605 /* Point didn't move. */
10606 return 0;
10607
10608 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10609 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10610 && COMPOSITION_VALID_P (start, end, prop)
10611 && start < prev_pt && end > prev_pt)
10612 /* The last point was within the composition. Return 1 iff
10613 point moved out of the composition. */
10614 return (pt <= start || pt >= end);
10615 }
10616
10617 /* Check a composition at the current point. */
10618 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10619 && find_composition (pt, -1, &start, &end, &prop, buffer)
10620 && COMPOSITION_VALID_P (start, end, prop)
10621 && start < pt && end > pt);
10622 }
10623
10624
10625 /* Reconsider the setting of B->clip_changed which is displayed
10626 in window W. */
10627
10628 static INLINE void
10629 reconsider_clip_changes (w, b)
10630 struct window *w;
10631 struct buffer *b;
10632 {
10633 if (b->clip_changed
10634 && !NILP (w->window_end_valid)
10635 && w->current_matrix->buffer == b
10636 && w->current_matrix->zv == BUF_ZV (b)
10637 && w->current_matrix->begv == BUF_BEGV (b))
10638 b->clip_changed = 0;
10639
10640 /* If display wasn't paused, and W is not a tool bar window, see if
10641 point has been moved into or out of a composition. In that case,
10642 we set b->clip_changed to 1 to force updating the screen. If
10643 b->clip_changed has already been set to 1, we can skip this
10644 check. */
10645 if (!b->clip_changed
10646 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10647 {
10648 int pt;
10649
10650 if (w == XWINDOW (selected_window))
10651 pt = BUF_PT (current_buffer);
10652 else
10653 pt = marker_position (w->pointm);
10654
10655 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10656 || pt != XINT (w->last_point))
10657 && check_point_in_composition (w->current_matrix->buffer,
10658 XINT (w->last_point),
10659 XBUFFER (w->buffer), pt))
10660 b->clip_changed = 1;
10661 }
10662 }
10663 \f
10664
10665 /* Select FRAME to forward the values of frame-local variables into C
10666 variables so that the redisplay routines can access those values
10667 directly. */
10668
10669 static void
10670 select_frame_for_redisplay (frame)
10671 Lisp_Object frame;
10672 {
10673 Lisp_Object tail, sym, val;
10674 Lisp_Object old = selected_frame;
10675
10676 selected_frame = frame;
10677
10678 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10679 if (CONSP (XCAR (tail))
10680 && (sym = XCAR (XCAR (tail)),
10681 SYMBOLP (sym))
10682 && (sym = indirect_variable (sym),
10683 val = SYMBOL_VALUE (sym),
10684 (BUFFER_LOCAL_VALUEP (val)
10685 || SOME_BUFFER_LOCAL_VALUEP (val)))
10686 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10687 /* Use find_symbol_value rather than Fsymbol_value
10688 to avoid an error if it is void. */
10689 find_symbol_value (sym);
10690
10691 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10692 if (CONSP (XCAR (tail))
10693 && (sym = XCAR (XCAR (tail)),
10694 SYMBOLP (sym))
10695 && (sym = indirect_variable (sym),
10696 val = SYMBOL_VALUE (sym),
10697 (BUFFER_LOCAL_VALUEP (val)
10698 || SOME_BUFFER_LOCAL_VALUEP (val)))
10699 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10700 find_symbol_value (sym);
10701 }
10702
10703
10704 #define STOP_POLLING \
10705 do { if (! polling_stopped_here) stop_polling (); \
10706 polling_stopped_here = 1; } while (0)
10707
10708 #define RESUME_POLLING \
10709 do { if (polling_stopped_here) start_polling (); \
10710 polling_stopped_here = 0; } while (0)
10711
10712
10713 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10714 response to any user action; therefore, we should preserve the echo
10715 area. (Actually, our caller does that job.) Perhaps in the future
10716 avoid recentering windows if it is not necessary; currently that
10717 causes some problems. */
10718
10719 static void
10720 redisplay_internal (preserve_echo_area)
10721 int preserve_echo_area;
10722 {
10723 struct window *w = XWINDOW (selected_window);
10724 struct frame *f = XFRAME (w->frame);
10725 int pause;
10726 int must_finish = 0;
10727 struct text_pos tlbufpos, tlendpos;
10728 int number_of_visible_frames;
10729 int count;
10730 struct frame *sf = SELECTED_FRAME ();
10731 int polling_stopped_here = 0;
10732
10733 /* Non-zero means redisplay has to consider all windows on all
10734 frames. Zero means, only selected_window is considered. */
10735 int consider_all_windows_p;
10736
10737 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10738
10739 /* No redisplay if running in batch mode or frame is not yet fully
10740 initialized, or redisplay is explicitly turned off by setting
10741 Vinhibit_redisplay. */
10742 if (noninteractive
10743 || !NILP (Vinhibit_redisplay)
10744 || !f->glyphs_initialized_p)
10745 return;
10746
10747 /* The flag redisplay_performed_directly_p is set by
10748 direct_output_for_insert when it already did the whole screen
10749 update necessary. */
10750 if (redisplay_performed_directly_p)
10751 {
10752 redisplay_performed_directly_p = 0;
10753 if (!hscroll_windows (selected_window))
10754 return;
10755 }
10756
10757 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10758 if (popup_activated ())
10759 return;
10760 #endif
10761
10762 /* I don't think this happens but let's be paranoid. */
10763 if (redisplaying_p)
10764 return;
10765
10766 /* Record a function that resets redisplaying_p to its old value
10767 when we leave this function. */
10768 count = SPECPDL_INDEX ();
10769 record_unwind_protect (unwind_redisplay,
10770 Fcons (make_number (redisplaying_p), selected_frame));
10771 ++redisplaying_p;
10772 specbind (Qinhibit_free_realized_faces, Qnil);
10773
10774 {
10775 Lisp_Object tail, frame;
10776
10777 FOR_EACH_FRAME (tail, frame)
10778 {
10779 struct frame *f = XFRAME (frame);
10780 f->already_hscrolled_p = 0;
10781 }
10782 }
10783
10784 retry:
10785 pause = 0;
10786 reconsider_clip_changes (w, current_buffer);
10787 last_escape_glyph_frame = NULL;
10788 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10789
10790 /* If new fonts have been loaded that make a glyph matrix adjustment
10791 necessary, do it. */
10792 if (fonts_changed_p)
10793 {
10794 adjust_glyphs (NULL);
10795 ++windows_or_buffers_changed;
10796 fonts_changed_p = 0;
10797 }
10798
10799 /* If face_change_count is non-zero, init_iterator will free all
10800 realized faces, which includes the faces referenced from current
10801 matrices. So, we can't reuse current matrices in this case. */
10802 if (face_change_count)
10803 ++windows_or_buffers_changed;
10804
10805 if (! FRAME_WINDOW_P (sf)
10806 && previous_terminal_frame != sf)
10807 {
10808 /* Since frames on an ASCII terminal share the same display
10809 area, displaying a different frame means redisplay the whole
10810 thing. */
10811 windows_or_buffers_changed++;
10812 SET_FRAME_GARBAGED (sf);
10813 XSETFRAME (Vterminal_frame, sf);
10814 }
10815 previous_terminal_frame = sf;
10816
10817 /* Set the visible flags for all frames. Do this before checking
10818 for resized or garbaged frames; they want to know if their frames
10819 are visible. See the comment in frame.h for
10820 FRAME_SAMPLE_VISIBILITY. */
10821 {
10822 Lisp_Object tail, frame;
10823
10824 number_of_visible_frames = 0;
10825
10826 FOR_EACH_FRAME (tail, frame)
10827 {
10828 struct frame *f = XFRAME (frame);
10829
10830 FRAME_SAMPLE_VISIBILITY (f);
10831 if (FRAME_VISIBLE_P (f))
10832 ++number_of_visible_frames;
10833 clear_desired_matrices (f);
10834 }
10835 }
10836
10837 /* Notice any pending interrupt request to change frame size. */
10838 do_pending_window_change (1);
10839
10840 /* Clear frames marked as garbaged. */
10841 if (frame_garbaged)
10842 clear_garbaged_frames ();
10843
10844 /* Build menubar and tool-bar items. */
10845 if (NILP (Vmemory_full))
10846 prepare_menu_bars ();
10847
10848 if (windows_or_buffers_changed)
10849 update_mode_lines++;
10850
10851 /* Detect case that we need to write or remove a star in the mode line. */
10852 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10853 {
10854 w->update_mode_line = Qt;
10855 if (buffer_shared > 1)
10856 update_mode_lines++;
10857 }
10858
10859 /* If %c is in the mode line, update it if needed. */
10860 if (!NILP (w->column_number_displayed)
10861 /* This alternative quickly identifies a common case
10862 where no change is needed. */
10863 && !(PT == XFASTINT (w->last_point)
10864 && XFASTINT (w->last_modified) >= MODIFF
10865 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10866 && (XFASTINT (w->column_number_displayed)
10867 != (int) current_column ())) /* iftc */
10868 w->update_mode_line = Qt;
10869
10870 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10871
10872 /* The variable buffer_shared is set in redisplay_window and
10873 indicates that we redisplay a buffer in different windows. See
10874 there. */
10875 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10876 || cursor_type_changed);
10877
10878 /* If specs for an arrow have changed, do thorough redisplay
10879 to ensure we remove any arrow that should no longer exist. */
10880 if (overlay_arrows_changed_p ())
10881 consider_all_windows_p = windows_or_buffers_changed = 1;
10882
10883 /* Normally the message* functions will have already displayed and
10884 updated the echo area, but the frame may have been trashed, or
10885 the update may have been preempted, so display the echo area
10886 again here. Checking message_cleared_p captures the case that
10887 the echo area should be cleared. */
10888 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10889 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10890 || (message_cleared_p
10891 && minibuf_level == 0
10892 /* If the mini-window is currently selected, this means the
10893 echo-area doesn't show through. */
10894 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10895 {
10896 int window_height_changed_p = echo_area_display (0);
10897 must_finish = 1;
10898
10899 /* If we don't display the current message, don't clear the
10900 message_cleared_p flag, because, if we did, we wouldn't clear
10901 the echo area in the next redisplay which doesn't preserve
10902 the echo area. */
10903 if (!display_last_displayed_message_p)
10904 message_cleared_p = 0;
10905
10906 if (fonts_changed_p)
10907 goto retry;
10908 else if (window_height_changed_p)
10909 {
10910 consider_all_windows_p = 1;
10911 ++update_mode_lines;
10912 ++windows_or_buffers_changed;
10913
10914 /* If window configuration was changed, frames may have been
10915 marked garbaged. Clear them or we will experience
10916 surprises wrt scrolling. */
10917 if (frame_garbaged)
10918 clear_garbaged_frames ();
10919 }
10920 }
10921 else if (EQ (selected_window, minibuf_window)
10922 && (current_buffer->clip_changed
10923 || XFASTINT (w->last_modified) < MODIFF
10924 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10925 && resize_mini_window (w, 0))
10926 {
10927 /* Resized active mini-window to fit the size of what it is
10928 showing if its contents might have changed. */
10929 must_finish = 1;
10930 consider_all_windows_p = 1;
10931 ++windows_or_buffers_changed;
10932 ++update_mode_lines;
10933
10934 /* If window configuration was changed, frames may have been
10935 marked garbaged. Clear them or we will experience
10936 surprises wrt scrolling. */
10937 if (frame_garbaged)
10938 clear_garbaged_frames ();
10939 }
10940
10941
10942 /* If showing the region, and mark has changed, we must redisplay
10943 the whole window. The assignment to this_line_start_pos prevents
10944 the optimization directly below this if-statement. */
10945 if (((!NILP (Vtransient_mark_mode)
10946 && !NILP (XBUFFER (w->buffer)->mark_active))
10947 != !NILP (w->region_showing))
10948 || (!NILP (w->region_showing)
10949 && !EQ (w->region_showing,
10950 Fmarker_position (XBUFFER (w->buffer)->mark))))
10951 CHARPOS (this_line_start_pos) = 0;
10952
10953 /* Optimize the case that only the line containing the cursor in the
10954 selected window has changed. Variables starting with this_ are
10955 set in display_line and record information about the line
10956 containing the cursor. */
10957 tlbufpos = this_line_start_pos;
10958 tlendpos = this_line_end_pos;
10959 if (!consider_all_windows_p
10960 && CHARPOS (tlbufpos) > 0
10961 && NILP (w->update_mode_line)
10962 && !current_buffer->clip_changed
10963 && !current_buffer->prevent_redisplay_optimizations_p
10964 && FRAME_VISIBLE_P (XFRAME (w->frame))
10965 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10966 /* Make sure recorded data applies to current buffer, etc. */
10967 && this_line_buffer == current_buffer
10968 && current_buffer == XBUFFER (w->buffer)
10969 && NILP (w->force_start)
10970 && NILP (w->optional_new_start)
10971 /* Point must be on the line that we have info recorded about. */
10972 && PT >= CHARPOS (tlbufpos)
10973 && PT <= Z - CHARPOS (tlendpos)
10974 /* All text outside that line, including its final newline,
10975 must be unchanged */
10976 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10977 CHARPOS (tlendpos)))
10978 {
10979 if (CHARPOS (tlbufpos) > BEGV
10980 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10981 && (CHARPOS (tlbufpos) == ZV
10982 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10983 /* Former continuation line has disappeared by becoming empty */
10984 goto cancel;
10985 else if (XFASTINT (w->last_modified) < MODIFF
10986 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10987 || MINI_WINDOW_P (w))
10988 {
10989 /* We have to handle the case of continuation around a
10990 wide-column character (See the comment in indent.c around
10991 line 885).
10992
10993 For instance, in the following case:
10994
10995 -------- Insert --------
10996 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10997 J_I_ ==> J_I_ `^^' are cursors.
10998 ^^ ^^
10999 -------- --------
11000
11001 As we have to redraw the line above, we should goto cancel. */
11002
11003 struct it it;
11004 int line_height_before = this_line_pixel_height;
11005
11006 /* Note that start_display will handle the case that the
11007 line starting at tlbufpos is a continuation lines. */
11008 start_display (&it, w, tlbufpos);
11009
11010 /* Implementation note: It this still necessary? */
11011 if (it.current_x != this_line_start_x)
11012 goto cancel;
11013
11014 TRACE ((stderr, "trying display optimization 1\n"));
11015 w->cursor.vpos = -1;
11016 overlay_arrow_seen = 0;
11017 it.vpos = this_line_vpos;
11018 it.current_y = this_line_y;
11019 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11020 display_line (&it);
11021
11022 /* If line contains point, is not continued,
11023 and ends at same distance from eob as before, we win */
11024 if (w->cursor.vpos >= 0
11025 /* Line is not continued, otherwise this_line_start_pos
11026 would have been set to 0 in display_line. */
11027 && CHARPOS (this_line_start_pos)
11028 /* Line ends as before. */
11029 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11030 /* Line has same height as before. Otherwise other lines
11031 would have to be shifted up or down. */
11032 && this_line_pixel_height == line_height_before)
11033 {
11034 /* If this is not the window's last line, we must adjust
11035 the charstarts of the lines below. */
11036 if (it.current_y < it.last_visible_y)
11037 {
11038 struct glyph_row *row
11039 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11040 int delta, delta_bytes;
11041
11042 if (Z - CHARPOS (tlendpos) == ZV)
11043 {
11044 /* This line ends at end of (accessible part of)
11045 buffer. There is no newline to count. */
11046 delta = (Z
11047 - CHARPOS (tlendpos)
11048 - MATRIX_ROW_START_CHARPOS (row));
11049 delta_bytes = (Z_BYTE
11050 - BYTEPOS (tlendpos)
11051 - MATRIX_ROW_START_BYTEPOS (row));
11052 }
11053 else
11054 {
11055 /* This line ends in a newline. Must take
11056 account of the newline and the rest of the
11057 text that follows. */
11058 delta = (Z
11059 - CHARPOS (tlendpos)
11060 - MATRIX_ROW_START_CHARPOS (row));
11061 delta_bytes = (Z_BYTE
11062 - BYTEPOS (tlendpos)
11063 - MATRIX_ROW_START_BYTEPOS (row));
11064 }
11065
11066 increment_matrix_positions (w->current_matrix,
11067 this_line_vpos + 1,
11068 w->current_matrix->nrows,
11069 delta, delta_bytes);
11070 }
11071
11072 /* If this row displays text now but previously didn't,
11073 or vice versa, w->window_end_vpos may have to be
11074 adjusted. */
11075 if ((it.glyph_row - 1)->displays_text_p)
11076 {
11077 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11078 XSETINT (w->window_end_vpos, this_line_vpos);
11079 }
11080 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11081 && this_line_vpos > 0)
11082 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11083 w->window_end_valid = Qnil;
11084
11085 /* Update hint: No need to try to scroll in update_window. */
11086 w->desired_matrix->no_scrolling_p = 1;
11087
11088 #if GLYPH_DEBUG
11089 *w->desired_matrix->method = 0;
11090 debug_method_add (w, "optimization 1");
11091 #endif
11092 #ifdef HAVE_WINDOW_SYSTEM
11093 update_window_fringes (w, 0);
11094 #endif
11095 goto update;
11096 }
11097 else
11098 goto cancel;
11099 }
11100 else if (/* Cursor position hasn't changed. */
11101 PT == XFASTINT (w->last_point)
11102 /* Make sure the cursor was last displayed
11103 in this window. Otherwise we have to reposition it. */
11104 && 0 <= w->cursor.vpos
11105 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11106 {
11107 if (!must_finish)
11108 {
11109 do_pending_window_change (1);
11110
11111 /* We used to always goto end_of_redisplay here, but this
11112 isn't enough if we have a blinking cursor. */
11113 if (w->cursor_off_p == w->last_cursor_off_p)
11114 goto end_of_redisplay;
11115 }
11116 goto update;
11117 }
11118 /* If highlighting the region, or if the cursor is in the echo area,
11119 then we can't just move the cursor. */
11120 else if (! (!NILP (Vtransient_mark_mode)
11121 && !NILP (current_buffer->mark_active))
11122 && (EQ (selected_window, current_buffer->last_selected_window)
11123 || highlight_nonselected_windows)
11124 && NILP (w->region_showing)
11125 && NILP (Vshow_trailing_whitespace)
11126 && !cursor_in_echo_area)
11127 {
11128 struct it it;
11129 struct glyph_row *row;
11130
11131 /* Skip from tlbufpos to PT and see where it is. Note that
11132 PT may be in invisible text. If so, we will end at the
11133 next visible position. */
11134 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11135 NULL, DEFAULT_FACE_ID);
11136 it.current_x = this_line_start_x;
11137 it.current_y = this_line_y;
11138 it.vpos = this_line_vpos;
11139
11140 /* The call to move_it_to stops in front of PT, but
11141 moves over before-strings. */
11142 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11143
11144 if (it.vpos == this_line_vpos
11145 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11146 row->enabled_p))
11147 {
11148 xassert (this_line_vpos == it.vpos);
11149 xassert (this_line_y == it.current_y);
11150 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11151 #if GLYPH_DEBUG
11152 *w->desired_matrix->method = 0;
11153 debug_method_add (w, "optimization 3");
11154 #endif
11155 goto update;
11156 }
11157 else
11158 goto cancel;
11159 }
11160
11161 cancel:
11162 /* Text changed drastically or point moved off of line. */
11163 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11164 }
11165
11166 CHARPOS (this_line_start_pos) = 0;
11167 consider_all_windows_p |= buffer_shared > 1;
11168 ++clear_face_cache_count;
11169 #ifdef HAVE_WINDOW_SYSTEM
11170 ++clear_image_cache_count;
11171 #endif
11172
11173 /* Build desired matrices, and update the display. If
11174 consider_all_windows_p is non-zero, do it for all windows on all
11175 frames. Otherwise do it for selected_window, only. */
11176
11177 if (consider_all_windows_p)
11178 {
11179 Lisp_Object tail, frame;
11180
11181 FOR_EACH_FRAME (tail, frame)
11182 XFRAME (frame)->updated_p = 0;
11183
11184 /* Recompute # windows showing selected buffer. This will be
11185 incremented each time such a window is displayed. */
11186 buffer_shared = 0;
11187
11188 FOR_EACH_FRAME (tail, frame)
11189 {
11190 struct frame *f = XFRAME (frame);
11191
11192 if (FRAME_WINDOW_P (f) || f == sf)
11193 {
11194 if (! EQ (frame, selected_frame))
11195 /* Select the frame, for the sake of frame-local
11196 variables. */
11197 select_frame_for_redisplay (frame);
11198
11199 /* Mark all the scroll bars to be removed; we'll redeem
11200 the ones we want when we redisplay their windows. */
11201 if (condemn_scroll_bars_hook)
11202 condemn_scroll_bars_hook (f);
11203
11204 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11205 redisplay_windows (FRAME_ROOT_WINDOW (f));
11206
11207 /* Any scroll bars which redisplay_windows should have
11208 nuked should now go away. */
11209 if (judge_scroll_bars_hook)
11210 judge_scroll_bars_hook (f);
11211
11212 /* If fonts changed, display again. */
11213 /* ??? rms: I suspect it is a mistake to jump all the way
11214 back to retry here. It should just retry this frame. */
11215 if (fonts_changed_p)
11216 goto retry;
11217
11218 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11219 {
11220 /* See if we have to hscroll. */
11221 if (!f->already_hscrolled_p)
11222 {
11223 f->already_hscrolled_p = 1;
11224 if (hscroll_windows (f->root_window))
11225 goto retry;
11226 }
11227
11228 /* Prevent various kinds of signals during display
11229 update. stdio is not robust about handling
11230 signals, which can cause an apparent I/O
11231 error. */
11232 if (interrupt_input)
11233 unrequest_sigio ();
11234 STOP_POLLING;
11235
11236 /* Update the display. */
11237 set_window_update_flags (XWINDOW (f->root_window), 1);
11238 pause |= update_frame (f, 0, 0);
11239 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11240 if (pause)
11241 break;
11242 #endif
11243
11244 f->updated_p = 1;
11245 }
11246 }
11247 }
11248
11249 if (!pause)
11250 {
11251 /* Do the mark_window_display_accurate after all windows have
11252 been redisplayed because this call resets flags in buffers
11253 which are needed for proper redisplay. */
11254 FOR_EACH_FRAME (tail, frame)
11255 {
11256 struct frame *f = XFRAME (frame);
11257 if (f->updated_p)
11258 {
11259 mark_window_display_accurate (f->root_window, 1);
11260 if (frame_up_to_date_hook)
11261 frame_up_to_date_hook (f);
11262 }
11263 }
11264 }
11265 }
11266 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11267 {
11268 Lisp_Object mini_window;
11269 struct frame *mini_frame;
11270
11271 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11272 /* Use list_of_error, not Qerror, so that
11273 we catch only errors and don't run the debugger. */
11274 internal_condition_case_1 (redisplay_window_1, selected_window,
11275 list_of_error,
11276 redisplay_window_error);
11277
11278 /* Compare desired and current matrices, perform output. */
11279
11280 update:
11281 /* If fonts changed, display again. */
11282 if (fonts_changed_p)
11283 goto retry;
11284
11285 /* Prevent various kinds of signals during display update.
11286 stdio is not robust about handling signals,
11287 which can cause an apparent I/O error. */
11288 if (interrupt_input)
11289 unrequest_sigio ();
11290 STOP_POLLING;
11291
11292 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11293 {
11294 if (hscroll_windows (selected_window))
11295 goto retry;
11296
11297 XWINDOW (selected_window)->must_be_updated_p = 1;
11298 pause = update_frame (sf, 0, 0);
11299 }
11300
11301 /* We may have called echo_area_display at the top of this
11302 function. If the echo area is on another frame, that may
11303 have put text on a frame other than the selected one, so the
11304 above call to update_frame would not have caught it. Catch
11305 it here. */
11306 mini_window = FRAME_MINIBUF_WINDOW (sf);
11307 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11308
11309 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11310 {
11311 XWINDOW (mini_window)->must_be_updated_p = 1;
11312 pause |= update_frame (mini_frame, 0, 0);
11313 if (!pause && hscroll_windows (mini_window))
11314 goto retry;
11315 }
11316 }
11317
11318 /* If display was paused because of pending input, make sure we do a
11319 thorough update the next time. */
11320 if (pause)
11321 {
11322 /* Prevent the optimization at the beginning of
11323 redisplay_internal that tries a single-line update of the
11324 line containing the cursor in the selected window. */
11325 CHARPOS (this_line_start_pos) = 0;
11326
11327 /* Let the overlay arrow be updated the next time. */
11328 update_overlay_arrows (0);
11329
11330 /* If we pause after scrolling, some rows in the current
11331 matrices of some windows are not valid. */
11332 if (!WINDOW_FULL_WIDTH_P (w)
11333 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11334 update_mode_lines = 1;
11335 }
11336 else
11337 {
11338 if (!consider_all_windows_p)
11339 {
11340 /* This has already been done above if
11341 consider_all_windows_p is set. */
11342 mark_window_display_accurate_1 (w, 1);
11343
11344 /* Say overlay arrows are up to date. */
11345 update_overlay_arrows (1);
11346
11347 if (frame_up_to_date_hook != 0)
11348 frame_up_to_date_hook (sf);
11349 }
11350
11351 update_mode_lines = 0;
11352 windows_or_buffers_changed = 0;
11353 cursor_type_changed = 0;
11354 }
11355
11356 /* Start SIGIO interrupts coming again. Having them off during the
11357 code above makes it less likely one will discard output, but not
11358 impossible, since there might be stuff in the system buffer here.
11359 But it is much hairier to try to do anything about that. */
11360 if (interrupt_input)
11361 request_sigio ();
11362 RESUME_POLLING;
11363
11364 /* If a frame has become visible which was not before, redisplay
11365 again, so that we display it. Expose events for such a frame
11366 (which it gets when becoming visible) don't call the parts of
11367 redisplay constructing glyphs, so simply exposing a frame won't
11368 display anything in this case. So, we have to display these
11369 frames here explicitly. */
11370 if (!pause)
11371 {
11372 Lisp_Object tail, frame;
11373 int new_count = 0;
11374
11375 FOR_EACH_FRAME (tail, frame)
11376 {
11377 int this_is_visible = 0;
11378
11379 if (XFRAME (frame)->visible)
11380 this_is_visible = 1;
11381 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11382 if (XFRAME (frame)->visible)
11383 this_is_visible = 1;
11384
11385 if (this_is_visible)
11386 new_count++;
11387 }
11388
11389 if (new_count != number_of_visible_frames)
11390 windows_or_buffers_changed++;
11391 }
11392
11393 /* Change frame size now if a change is pending. */
11394 do_pending_window_change (1);
11395
11396 /* If we just did a pending size change, or have additional
11397 visible frames, redisplay again. */
11398 if (windows_or_buffers_changed && !pause)
11399 goto retry;
11400
11401 /* Clear the face cache eventually. */
11402 if (consider_all_windows_p)
11403 {
11404 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11405 {
11406 clear_face_cache (0);
11407 clear_face_cache_count = 0;
11408 }
11409 #ifdef HAVE_WINDOW_SYSTEM
11410 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11411 {
11412 Lisp_Object tail, frame;
11413 FOR_EACH_FRAME (tail, frame)
11414 {
11415 struct frame *f = XFRAME (frame);
11416 if (FRAME_WINDOW_P (f))
11417 clear_image_cache (f, 0);
11418 }
11419 clear_image_cache_count = 0;
11420 }
11421 #endif /* HAVE_WINDOW_SYSTEM */
11422 }
11423
11424 end_of_redisplay:
11425 unbind_to (count, Qnil);
11426 RESUME_POLLING;
11427 }
11428
11429
11430 /* Redisplay, but leave alone any recent echo area message unless
11431 another message has been requested in its place.
11432
11433 This is useful in situations where you need to redisplay but no
11434 user action has occurred, making it inappropriate for the message
11435 area to be cleared. See tracking_off and
11436 wait_reading_process_output for examples of these situations.
11437
11438 FROM_WHERE is an integer saying from where this function was
11439 called. This is useful for debugging. */
11440
11441 void
11442 redisplay_preserve_echo_area (from_where)
11443 int from_where;
11444 {
11445 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11446
11447 if (!NILP (echo_area_buffer[1]))
11448 {
11449 /* We have a previously displayed message, but no current
11450 message. Redisplay the previous message. */
11451 display_last_displayed_message_p = 1;
11452 redisplay_internal (1);
11453 display_last_displayed_message_p = 0;
11454 }
11455 else
11456 redisplay_internal (1);
11457
11458 if (rif != NULL && rif->flush_display_optional)
11459 rif->flush_display_optional (NULL);
11460 }
11461
11462
11463 /* Function registered with record_unwind_protect in
11464 redisplay_internal. Reset redisplaying_p to the value it had
11465 before redisplay_internal was called, and clear
11466 prevent_freeing_realized_faces_p. It also selects the previously
11467 selected frame. */
11468
11469 static Lisp_Object
11470 unwind_redisplay (val)
11471 Lisp_Object val;
11472 {
11473 Lisp_Object old_redisplaying_p, old_frame;
11474
11475 old_redisplaying_p = XCAR (val);
11476 redisplaying_p = XFASTINT (old_redisplaying_p);
11477 old_frame = XCDR (val);
11478 if (! EQ (old_frame, selected_frame))
11479 select_frame_for_redisplay (old_frame);
11480 return Qnil;
11481 }
11482
11483
11484 /* Mark the display of window W as accurate or inaccurate. If
11485 ACCURATE_P is non-zero mark display of W as accurate. If
11486 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11487 redisplay_internal is called. */
11488
11489 static void
11490 mark_window_display_accurate_1 (w, accurate_p)
11491 struct window *w;
11492 int accurate_p;
11493 {
11494 if (BUFFERP (w->buffer))
11495 {
11496 struct buffer *b = XBUFFER (w->buffer);
11497
11498 w->last_modified
11499 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11500 w->last_overlay_modified
11501 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11502 w->last_had_star
11503 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11504
11505 if (accurate_p)
11506 {
11507 b->clip_changed = 0;
11508 b->prevent_redisplay_optimizations_p = 0;
11509
11510 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11511 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11512 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11513 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11514
11515 w->current_matrix->buffer = b;
11516 w->current_matrix->begv = BUF_BEGV (b);
11517 w->current_matrix->zv = BUF_ZV (b);
11518
11519 w->last_cursor = w->cursor;
11520 w->last_cursor_off_p = w->cursor_off_p;
11521
11522 if (w == XWINDOW (selected_window))
11523 w->last_point = make_number (BUF_PT (b));
11524 else
11525 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11526 }
11527 }
11528
11529 if (accurate_p)
11530 {
11531 w->window_end_valid = w->buffer;
11532 #if 0 /* This is incorrect with variable-height lines. */
11533 xassert (XINT (w->window_end_vpos)
11534 < (WINDOW_TOTAL_LINES (w)
11535 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11536 #endif
11537 w->update_mode_line = Qnil;
11538 }
11539 }
11540
11541
11542 /* Mark the display of windows in the window tree rooted at WINDOW as
11543 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11544 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11545 be redisplayed the next time redisplay_internal is called. */
11546
11547 void
11548 mark_window_display_accurate (window, accurate_p)
11549 Lisp_Object window;
11550 int accurate_p;
11551 {
11552 struct window *w;
11553
11554 for (; !NILP (window); window = w->next)
11555 {
11556 w = XWINDOW (window);
11557 mark_window_display_accurate_1 (w, accurate_p);
11558
11559 if (!NILP (w->vchild))
11560 mark_window_display_accurate (w->vchild, accurate_p);
11561 if (!NILP (w->hchild))
11562 mark_window_display_accurate (w->hchild, accurate_p);
11563 }
11564
11565 if (accurate_p)
11566 {
11567 update_overlay_arrows (1);
11568 }
11569 else
11570 {
11571 /* Force a thorough redisplay the next time by setting
11572 last_arrow_position and last_arrow_string to t, which is
11573 unequal to any useful value of Voverlay_arrow_... */
11574 update_overlay_arrows (-1);
11575 }
11576 }
11577
11578
11579 /* Return value in display table DP (Lisp_Char_Table *) for character
11580 C. Since a display table doesn't have any parent, we don't have to
11581 follow parent. Do not call this function directly but use the
11582 macro DISP_CHAR_VECTOR. */
11583
11584 Lisp_Object
11585 disp_char_vector (dp, c)
11586 struct Lisp_Char_Table *dp;
11587 int c;
11588 {
11589 int code[4], i;
11590 Lisp_Object val;
11591
11592 if (SINGLE_BYTE_CHAR_P (c))
11593 return (dp->contents[c]);
11594
11595 SPLIT_CHAR (c, code[0], code[1], code[2]);
11596 if (code[1] < 32)
11597 code[1] = -1;
11598 else if (code[2] < 32)
11599 code[2] = -1;
11600
11601 /* Here, the possible range of code[0] (== charset ID) is
11602 128..max_charset. Since the top level char table contains data
11603 for multibyte characters after 256th element, we must increment
11604 code[0] by 128 to get a correct index. */
11605 code[0] += 128;
11606 code[3] = -1; /* anchor */
11607
11608 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11609 {
11610 val = dp->contents[code[i]];
11611 if (!SUB_CHAR_TABLE_P (val))
11612 return (NILP (val) ? dp->defalt : val);
11613 }
11614
11615 /* Here, val is a sub char table. We return the default value of
11616 it. */
11617 return (dp->defalt);
11618 }
11619
11620
11621 \f
11622 /***********************************************************************
11623 Window Redisplay
11624 ***********************************************************************/
11625
11626 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11627
11628 static void
11629 redisplay_windows (window)
11630 Lisp_Object window;
11631 {
11632 while (!NILP (window))
11633 {
11634 struct window *w = XWINDOW (window);
11635
11636 if (!NILP (w->hchild))
11637 redisplay_windows (w->hchild);
11638 else if (!NILP (w->vchild))
11639 redisplay_windows (w->vchild);
11640 else
11641 {
11642 displayed_buffer = XBUFFER (w->buffer);
11643 /* Use list_of_error, not Qerror, so that
11644 we catch only errors and don't run the debugger. */
11645 internal_condition_case_1 (redisplay_window_0, window,
11646 list_of_error,
11647 redisplay_window_error);
11648 }
11649
11650 window = w->next;
11651 }
11652 }
11653
11654 static Lisp_Object
11655 redisplay_window_error ()
11656 {
11657 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11658 return Qnil;
11659 }
11660
11661 static Lisp_Object
11662 redisplay_window_0 (window)
11663 Lisp_Object window;
11664 {
11665 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11666 redisplay_window (window, 0);
11667 return Qnil;
11668 }
11669
11670 static Lisp_Object
11671 redisplay_window_1 (window)
11672 Lisp_Object window;
11673 {
11674 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11675 redisplay_window (window, 1);
11676 return Qnil;
11677 }
11678 \f
11679
11680 /* Increment GLYPH until it reaches END or CONDITION fails while
11681 adding (GLYPH)->pixel_width to X. */
11682
11683 #define SKIP_GLYPHS(glyph, end, x, condition) \
11684 do \
11685 { \
11686 (x) += (glyph)->pixel_width; \
11687 ++(glyph); \
11688 } \
11689 while ((glyph) < (end) && (condition))
11690
11691
11692 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11693 DELTA is the number of bytes by which positions recorded in ROW
11694 differ from current buffer positions.
11695
11696 Return 0 if cursor is not on this row. 1 otherwise. */
11697
11698 int
11699 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11700 struct window *w;
11701 struct glyph_row *row;
11702 struct glyph_matrix *matrix;
11703 int delta, delta_bytes, dy, dvpos;
11704 {
11705 struct glyph *glyph = row->glyphs[TEXT_AREA];
11706 struct glyph *end = glyph + row->used[TEXT_AREA];
11707 struct glyph *cursor = NULL;
11708 /* The first glyph that starts a sequence of glyphs from string. */
11709 struct glyph *string_start;
11710 /* The X coordinate of string_start. */
11711 int string_start_x;
11712 /* The last known character position. */
11713 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11714 /* The last known character position before string_start. */
11715 int string_before_pos;
11716 int x = row->x;
11717 int cursor_x = x;
11718 int cursor_from_overlay_pos = 0;
11719 int pt_old = PT - delta;
11720
11721 /* Skip over glyphs not having an object at the start of the row.
11722 These are special glyphs like truncation marks on terminal
11723 frames. */
11724 if (row->displays_text_p)
11725 while (glyph < end
11726 && INTEGERP (glyph->object)
11727 && glyph->charpos < 0)
11728 {
11729 x += glyph->pixel_width;
11730 ++glyph;
11731 }
11732
11733 string_start = NULL;
11734 while (glyph < end
11735 && !INTEGERP (glyph->object)
11736 && (!BUFFERP (glyph->object)
11737 || (last_pos = glyph->charpos) < pt_old))
11738 {
11739 if (! STRINGP (glyph->object))
11740 {
11741 string_start = NULL;
11742 x += glyph->pixel_width;
11743 ++glyph;
11744 if (cursor_from_overlay_pos
11745 && last_pos >= cursor_from_overlay_pos)
11746 {
11747 cursor_from_overlay_pos = 0;
11748 cursor = 0;
11749 }
11750 }
11751 else
11752 {
11753 if (string_start == NULL)
11754 {
11755 string_before_pos = last_pos;
11756 string_start = glyph;
11757 string_start_x = x;
11758 }
11759 /* Skip all glyphs from string. */
11760 do
11761 {
11762 Lisp_Object cprop;
11763 int pos;
11764 if ((cursor == NULL || glyph > cursor)
11765 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11766 Qcursor, (glyph)->object),
11767 !NILP (cprop))
11768 && (pos = string_buffer_position (w, glyph->object,
11769 string_before_pos),
11770 (pos == 0 /* From overlay */
11771 || pos == pt_old)))
11772 {
11773 /* Estimate overlay buffer position from the buffer
11774 positions of the glyphs before and after the overlay.
11775 Add 1 to last_pos so that if point corresponds to the
11776 glyph right after the overlay, we still use a 'cursor'
11777 property found in that overlay. */
11778 cursor_from_overlay_pos = (pos ? 0 : last_pos
11779 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11780 cursor = glyph;
11781 cursor_x = x;
11782 }
11783 x += glyph->pixel_width;
11784 ++glyph;
11785 }
11786 while (glyph < end && EQ (glyph->object, string_start->object));
11787 }
11788 }
11789
11790 if (cursor != NULL)
11791 {
11792 glyph = cursor;
11793 x = cursor_x;
11794 }
11795 else if (row->ends_in_ellipsis_p && glyph == end)
11796 {
11797 /* Scan back over the ellipsis glyphs, decrementing positions. */
11798 while (glyph > row->glyphs[TEXT_AREA]
11799 && (glyph - 1)->charpos == last_pos)
11800 glyph--, x -= glyph->pixel_width;
11801 /* That loop always goes one position too far,
11802 including the glyph before the ellipsis.
11803 So scan forward over that one. */
11804 x += glyph->pixel_width;
11805 glyph++;
11806 }
11807 else if (string_start
11808 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11809 {
11810 /* We may have skipped over point because the previous glyphs
11811 are from string. As there's no easy way to know the
11812 character position of the current glyph, find the correct
11813 glyph on point by scanning from string_start again. */
11814 Lisp_Object limit;
11815 Lisp_Object string;
11816 int pos;
11817
11818 limit = make_number (pt_old + 1);
11819 end = glyph;
11820 glyph = string_start;
11821 x = string_start_x;
11822 string = glyph->object;
11823 pos = string_buffer_position (w, string, string_before_pos);
11824 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11825 because we always put cursor after overlay strings. */
11826 while (pos == 0 && glyph < end)
11827 {
11828 string = glyph->object;
11829 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11830 if (glyph < end)
11831 pos = string_buffer_position (w, glyph->object, string_before_pos);
11832 }
11833
11834 while (glyph < end)
11835 {
11836 pos = XINT (Fnext_single_char_property_change
11837 (make_number (pos), Qdisplay, Qnil, limit));
11838 if (pos > pt_old)
11839 break;
11840 /* Skip glyphs from the same string. */
11841 string = glyph->object;
11842 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11843 /* Skip glyphs from an overlay. */
11844 while (glyph < end
11845 && ! string_buffer_position (w, glyph->object, pos))
11846 {
11847 string = glyph->object;
11848 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11849 }
11850 }
11851
11852 /* If we reached the end of the line, and end was from a string,
11853 cursor is not on this line. */
11854 if (glyph == end && row->continued_p)
11855 return 0;
11856 }
11857
11858 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11859 w->cursor.x = x;
11860 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11861 w->cursor.y = row->y + dy;
11862
11863 if (w == XWINDOW (selected_window))
11864 {
11865 if (!row->continued_p
11866 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11867 && row->x == 0)
11868 {
11869 this_line_buffer = XBUFFER (w->buffer);
11870
11871 CHARPOS (this_line_start_pos)
11872 = MATRIX_ROW_START_CHARPOS (row) + delta;
11873 BYTEPOS (this_line_start_pos)
11874 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11875
11876 CHARPOS (this_line_end_pos)
11877 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11878 BYTEPOS (this_line_end_pos)
11879 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11880
11881 this_line_y = w->cursor.y;
11882 this_line_pixel_height = row->height;
11883 this_line_vpos = w->cursor.vpos;
11884 this_line_start_x = row->x;
11885 }
11886 else
11887 CHARPOS (this_line_start_pos) = 0;
11888 }
11889
11890 return 1;
11891 }
11892
11893
11894 /* Run window scroll functions, if any, for WINDOW with new window
11895 start STARTP. Sets the window start of WINDOW to that position.
11896
11897 We assume that the window's buffer is really current. */
11898
11899 static INLINE struct text_pos
11900 run_window_scroll_functions (window, startp)
11901 Lisp_Object window;
11902 struct text_pos startp;
11903 {
11904 struct window *w = XWINDOW (window);
11905 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11906
11907 if (current_buffer != XBUFFER (w->buffer))
11908 abort ();
11909
11910 if (!NILP (Vwindow_scroll_functions))
11911 {
11912 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11913 make_number (CHARPOS (startp)));
11914 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11915 /* In case the hook functions switch buffers. */
11916 if (current_buffer != XBUFFER (w->buffer))
11917 set_buffer_internal_1 (XBUFFER (w->buffer));
11918 }
11919
11920 return startp;
11921 }
11922
11923
11924 /* Make sure the line containing the cursor is fully visible.
11925 A value of 1 means there is nothing to be done.
11926 (Either the line is fully visible, or it cannot be made so,
11927 or we cannot tell.)
11928
11929 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11930 is higher than window.
11931
11932 A value of 0 means the caller should do scrolling
11933 as if point had gone off the screen. */
11934
11935 static int
11936 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11937 struct window *w;
11938 int force_p;
11939 int current_matrix_p;
11940 {
11941 struct glyph_matrix *matrix;
11942 struct glyph_row *row;
11943 int window_height;
11944
11945 if (!make_cursor_line_fully_visible_p)
11946 return 1;
11947
11948 /* It's not always possible to find the cursor, e.g, when a window
11949 is full of overlay strings. Don't do anything in that case. */
11950 if (w->cursor.vpos < 0)
11951 return 1;
11952
11953 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11954 row = MATRIX_ROW (matrix, w->cursor.vpos);
11955
11956 /* If the cursor row is not partially visible, there's nothing to do. */
11957 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11958 return 1;
11959
11960 /* If the row the cursor is in is taller than the window's height,
11961 it's not clear what to do, so do nothing. */
11962 window_height = window_box_height (w);
11963 if (row->height >= window_height)
11964 {
11965 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
11966 return 1;
11967 }
11968 return 0;
11969
11970 #if 0
11971 /* This code used to try to scroll the window just enough to make
11972 the line visible. It returned 0 to say that the caller should
11973 allocate larger glyph matrices. */
11974
11975 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11976 {
11977 int dy = row->height - row->visible_height;
11978 w->vscroll = 0;
11979 w->cursor.y += dy;
11980 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11981 }
11982 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11983 {
11984 int dy = - (row->height - row->visible_height);
11985 w->vscroll = dy;
11986 w->cursor.y += dy;
11987 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11988 }
11989
11990 /* When we change the cursor y-position of the selected window,
11991 change this_line_y as well so that the display optimization for
11992 the cursor line of the selected window in redisplay_internal uses
11993 the correct y-position. */
11994 if (w == XWINDOW (selected_window))
11995 this_line_y = w->cursor.y;
11996
11997 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11998 redisplay with larger matrices. */
11999 if (matrix->nrows < required_matrix_height (w))
12000 {
12001 fonts_changed_p = 1;
12002 return 0;
12003 }
12004
12005 return 1;
12006 #endif /* 0 */
12007 }
12008
12009
12010 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12011 non-zero means only WINDOW is redisplayed in redisplay_internal.
12012 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12013 in redisplay_window to bring a partially visible line into view in
12014 the case that only the cursor has moved.
12015
12016 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12017 last screen line's vertical height extends past the end of the screen.
12018
12019 Value is
12020
12021 1 if scrolling succeeded
12022
12023 0 if scrolling didn't find point.
12024
12025 -1 if new fonts have been loaded so that we must interrupt
12026 redisplay, adjust glyph matrices, and try again. */
12027
12028 enum
12029 {
12030 SCROLLING_SUCCESS,
12031 SCROLLING_FAILED,
12032 SCROLLING_NEED_LARGER_MATRICES
12033 };
12034
12035 static int
12036 try_scrolling (window, just_this_one_p, scroll_conservatively,
12037 scroll_step, temp_scroll_step, last_line_misfit)
12038 Lisp_Object window;
12039 int just_this_one_p;
12040 EMACS_INT scroll_conservatively, scroll_step;
12041 int temp_scroll_step;
12042 int last_line_misfit;
12043 {
12044 struct window *w = XWINDOW (window);
12045 struct frame *f = XFRAME (w->frame);
12046 struct text_pos scroll_margin_pos;
12047 struct text_pos pos;
12048 struct text_pos startp;
12049 struct it it;
12050 Lisp_Object window_end;
12051 int this_scroll_margin;
12052 int dy = 0;
12053 int scroll_max;
12054 int rc;
12055 int amount_to_scroll = 0;
12056 Lisp_Object aggressive;
12057 int height;
12058 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12059
12060 #if GLYPH_DEBUG
12061 debug_method_add (w, "try_scrolling");
12062 #endif
12063
12064 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12065
12066 /* Compute scroll margin height in pixels. We scroll when point is
12067 within this distance from the top or bottom of the window. */
12068 if (scroll_margin > 0)
12069 {
12070 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12071 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12072 }
12073 else
12074 this_scroll_margin = 0;
12075
12076 /* Force scroll_conservatively to have a reasonable value so it doesn't
12077 cause an overflow while computing how much to scroll. */
12078 if (scroll_conservatively)
12079 scroll_conservatively = min (scroll_conservatively,
12080 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12081
12082 /* Compute how much we should try to scroll maximally to bring point
12083 into view. */
12084 if (scroll_step || scroll_conservatively || temp_scroll_step)
12085 scroll_max = max (scroll_step,
12086 max (scroll_conservatively, temp_scroll_step));
12087 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12088 || NUMBERP (current_buffer->scroll_up_aggressively))
12089 /* We're trying to scroll because of aggressive scrolling
12090 but no scroll_step is set. Choose an arbitrary one. Maybe
12091 there should be a variable for this. */
12092 scroll_max = 10;
12093 else
12094 scroll_max = 0;
12095 scroll_max *= FRAME_LINE_HEIGHT (f);
12096
12097 /* Decide whether we have to scroll down. Start at the window end
12098 and move this_scroll_margin up to find the position of the scroll
12099 margin. */
12100 window_end = Fwindow_end (window, Qt);
12101
12102 too_near_end:
12103
12104 CHARPOS (scroll_margin_pos) = XINT (window_end);
12105 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12106
12107 if (this_scroll_margin || extra_scroll_margin_lines)
12108 {
12109 start_display (&it, w, scroll_margin_pos);
12110 if (this_scroll_margin)
12111 move_it_vertically_backward (&it, this_scroll_margin);
12112 if (extra_scroll_margin_lines)
12113 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12114 scroll_margin_pos = it.current.pos;
12115 }
12116
12117 if (PT >= CHARPOS (scroll_margin_pos))
12118 {
12119 int y0;
12120
12121 /* Point is in the scroll margin at the bottom of the window, or
12122 below. Compute a new window start that makes point visible. */
12123
12124 /* Compute the distance from the scroll margin to PT.
12125 Give up if the distance is greater than scroll_max. */
12126 start_display (&it, w, scroll_margin_pos);
12127 y0 = it.current_y;
12128 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12129 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12130
12131 /* To make point visible, we have to move the window start
12132 down so that the line the cursor is in is visible, which
12133 means we have to add in the height of the cursor line. */
12134 dy = line_bottom_y (&it) - y0;
12135
12136 if (dy > scroll_max)
12137 return SCROLLING_FAILED;
12138
12139 /* Move the window start down. If scrolling conservatively,
12140 move it just enough down to make point visible. If
12141 scroll_step is set, move it down by scroll_step. */
12142 start_display (&it, w, startp);
12143
12144 if (scroll_conservatively)
12145 /* Set AMOUNT_TO_SCROLL to at least one line,
12146 and at most scroll_conservatively lines. */
12147 amount_to_scroll
12148 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12149 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12150 else if (scroll_step || temp_scroll_step)
12151 amount_to_scroll = scroll_max;
12152 else
12153 {
12154 aggressive = current_buffer->scroll_up_aggressively;
12155 height = WINDOW_BOX_TEXT_HEIGHT (w);
12156 if (NUMBERP (aggressive))
12157 {
12158 double float_amount = XFLOATINT (aggressive) * height;
12159 amount_to_scroll = float_amount;
12160 if (amount_to_scroll == 0 && float_amount > 0)
12161 amount_to_scroll = 1;
12162 }
12163 }
12164
12165 if (amount_to_scroll <= 0)
12166 return SCROLLING_FAILED;
12167
12168 /* If moving by amount_to_scroll leaves STARTP unchanged,
12169 move it down one screen line. */
12170
12171 move_it_vertically (&it, amount_to_scroll);
12172 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12173 move_it_by_lines (&it, 1, 1);
12174 startp = it.current.pos;
12175 }
12176 else
12177 {
12178 /* See if point is inside the scroll margin at the top of the
12179 window. */
12180 scroll_margin_pos = startp;
12181 if (this_scroll_margin)
12182 {
12183 start_display (&it, w, startp);
12184 move_it_vertically (&it, this_scroll_margin);
12185 scroll_margin_pos = it.current.pos;
12186 }
12187
12188 if (PT < CHARPOS (scroll_margin_pos))
12189 {
12190 /* Point is in the scroll margin at the top of the window or
12191 above what is displayed in the window. */
12192 int y0;
12193
12194 /* Compute the vertical distance from PT to the scroll
12195 margin position. Give up if distance is greater than
12196 scroll_max. */
12197 SET_TEXT_POS (pos, PT, PT_BYTE);
12198 start_display (&it, w, pos);
12199 y0 = it.current_y;
12200 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12201 it.last_visible_y, -1,
12202 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12203 dy = it.current_y - y0;
12204 if (dy > scroll_max)
12205 return SCROLLING_FAILED;
12206
12207 /* Compute new window start. */
12208 start_display (&it, w, startp);
12209
12210 if (scroll_conservatively)
12211 amount_to_scroll
12212 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12213 else if (scroll_step || temp_scroll_step)
12214 amount_to_scroll = scroll_max;
12215 else
12216 {
12217 aggressive = current_buffer->scroll_down_aggressively;
12218 height = WINDOW_BOX_TEXT_HEIGHT (w);
12219 if (NUMBERP (aggressive))
12220 {
12221 double float_amount = XFLOATINT (aggressive) * height;
12222 amount_to_scroll = float_amount;
12223 if (amount_to_scroll == 0 && float_amount > 0)
12224 amount_to_scroll = 1;
12225 }
12226 }
12227
12228 if (amount_to_scroll <= 0)
12229 return SCROLLING_FAILED;
12230
12231 move_it_vertically_backward (&it, amount_to_scroll);
12232 startp = it.current.pos;
12233 }
12234 }
12235
12236 /* Run window scroll functions. */
12237 startp = run_window_scroll_functions (window, startp);
12238
12239 /* Display the window. Give up if new fonts are loaded, or if point
12240 doesn't appear. */
12241 if (!try_window (window, startp, 0))
12242 rc = SCROLLING_NEED_LARGER_MATRICES;
12243 else if (w->cursor.vpos < 0)
12244 {
12245 clear_glyph_matrix (w->desired_matrix);
12246 rc = SCROLLING_FAILED;
12247 }
12248 else
12249 {
12250 /* Maybe forget recorded base line for line number display. */
12251 if (!just_this_one_p
12252 || current_buffer->clip_changed
12253 || BEG_UNCHANGED < CHARPOS (startp))
12254 w->base_line_number = Qnil;
12255
12256 /* If cursor ends up on a partially visible line,
12257 treat that as being off the bottom of the screen. */
12258 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12259 {
12260 clear_glyph_matrix (w->desired_matrix);
12261 ++extra_scroll_margin_lines;
12262 goto too_near_end;
12263 }
12264 rc = SCROLLING_SUCCESS;
12265 }
12266
12267 return rc;
12268 }
12269
12270
12271 /* Compute a suitable window start for window W if display of W starts
12272 on a continuation line. Value is non-zero if a new window start
12273 was computed.
12274
12275 The new window start will be computed, based on W's width, starting
12276 from the start of the continued line. It is the start of the
12277 screen line with the minimum distance from the old start W->start. */
12278
12279 static int
12280 compute_window_start_on_continuation_line (w)
12281 struct window *w;
12282 {
12283 struct text_pos pos, start_pos;
12284 int window_start_changed_p = 0;
12285
12286 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12287
12288 /* If window start is on a continuation line... Window start may be
12289 < BEGV in case there's invisible text at the start of the
12290 buffer (M-x rmail, for example). */
12291 if (CHARPOS (start_pos) > BEGV
12292 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12293 {
12294 struct it it;
12295 struct glyph_row *row;
12296
12297 /* Handle the case that the window start is out of range. */
12298 if (CHARPOS (start_pos) < BEGV)
12299 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12300 else if (CHARPOS (start_pos) > ZV)
12301 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12302
12303 /* Find the start of the continued line. This should be fast
12304 because scan_buffer is fast (newline cache). */
12305 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12306 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12307 row, DEFAULT_FACE_ID);
12308 reseat_at_previous_visible_line_start (&it);
12309
12310 /* If the line start is "too far" away from the window start,
12311 say it takes too much time to compute a new window start. */
12312 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12313 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12314 {
12315 int min_distance, distance;
12316
12317 /* Move forward by display lines to find the new window
12318 start. If window width was enlarged, the new start can
12319 be expected to be > the old start. If window width was
12320 decreased, the new window start will be < the old start.
12321 So, we're looking for the display line start with the
12322 minimum distance from the old window start. */
12323 pos = it.current.pos;
12324 min_distance = INFINITY;
12325 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12326 distance < min_distance)
12327 {
12328 min_distance = distance;
12329 pos = it.current.pos;
12330 move_it_by_lines (&it, 1, 0);
12331 }
12332
12333 /* Set the window start there. */
12334 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12335 window_start_changed_p = 1;
12336 }
12337 }
12338
12339 return window_start_changed_p;
12340 }
12341
12342
12343 /* Try cursor movement in case text has not changed in window WINDOW,
12344 with window start STARTP. Value is
12345
12346 CURSOR_MOVEMENT_SUCCESS if successful
12347
12348 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12349
12350 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12351 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12352 we want to scroll as if scroll-step were set to 1. See the code.
12353
12354 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12355 which case we have to abort this redisplay, and adjust matrices
12356 first. */
12357
12358 enum
12359 {
12360 CURSOR_MOVEMENT_SUCCESS,
12361 CURSOR_MOVEMENT_CANNOT_BE_USED,
12362 CURSOR_MOVEMENT_MUST_SCROLL,
12363 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12364 };
12365
12366 static int
12367 try_cursor_movement (window, startp, scroll_step)
12368 Lisp_Object window;
12369 struct text_pos startp;
12370 int *scroll_step;
12371 {
12372 struct window *w = XWINDOW (window);
12373 struct frame *f = XFRAME (w->frame);
12374 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12375
12376 #if GLYPH_DEBUG
12377 if (inhibit_try_cursor_movement)
12378 return rc;
12379 #endif
12380
12381 /* Handle case where text has not changed, only point, and it has
12382 not moved off the frame. */
12383 if (/* Point may be in this window. */
12384 PT >= CHARPOS (startp)
12385 /* Selective display hasn't changed. */
12386 && !current_buffer->clip_changed
12387 /* Function force-mode-line-update is used to force a thorough
12388 redisplay. It sets either windows_or_buffers_changed or
12389 update_mode_lines. So don't take a shortcut here for these
12390 cases. */
12391 && !update_mode_lines
12392 && !windows_or_buffers_changed
12393 && !cursor_type_changed
12394 /* Can't use this case if highlighting a region. When a
12395 region exists, cursor movement has to do more than just
12396 set the cursor. */
12397 && !(!NILP (Vtransient_mark_mode)
12398 && !NILP (current_buffer->mark_active))
12399 && NILP (w->region_showing)
12400 && NILP (Vshow_trailing_whitespace)
12401 /* Right after splitting windows, last_point may be nil. */
12402 && INTEGERP (w->last_point)
12403 /* This code is not used for mini-buffer for the sake of the case
12404 of redisplaying to replace an echo area message; since in
12405 that case the mini-buffer contents per se are usually
12406 unchanged. This code is of no real use in the mini-buffer
12407 since the handling of this_line_start_pos, etc., in redisplay
12408 handles the same cases. */
12409 && !EQ (window, minibuf_window)
12410 /* When splitting windows or for new windows, it happens that
12411 redisplay is called with a nil window_end_vpos or one being
12412 larger than the window. This should really be fixed in
12413 window.c. I don't have this on my list, now, so we do
12414 approximately the same as the old redisplay code. --gerd. */
12415 && INTEGERP (w->window_end_vpos)
12416 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12417 && (FRAME_WINDOW_P (f)
12418 || !overlay_arrow_in_current_buffer_p ()))
12419 {
12420 int this_scroll_margin, top_scroll_margin;
12421 struct glyph_row *row = NULL;
12422
12423 #if GLYPH_DEBUG
12424 debug_method_add (w, "cursor movement");
12425 #endif
12426
12427 /* Scroll if point within this distance from the top or bottom
12428 of the window. This is a pixel value. */
12429 this_scroll_margin = max (0, scroll_margin);
12430 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12431 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12432
12433 top_scroll_margin = this_scroll_margin;
12434 if (WINDOW_WANTS_HEADER_LINE_P (w))
12435 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12436
12437 /* Start with the row the cursor was displayed during the last
12438 not paused redisplay. Give up if that row is not valid. */
12439 if (w->last_cursor.vpos < 0
12440 || w->last_cursor.vpos >= w->current_matrix->nrows)
12441 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12442 else
12443 {
12444 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12445 if (row->mode_line_p)
12446 ++row;
12447 if (!row->enabled_p)
12448 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12449 }
12450
12451 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12452 {
12453 int scroll_p = 0;
12454 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12455
12456 if (PT > XFASTINT (w->last_point))
12457 {
12458 /* Point has moved forward. */
12459 while (MATRIX_ROW_END_CHARPOS (row) < PT
12460 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12461 {
12462 xassert (row->enabled_p);
12463 ++row;
12464 }
12465
12466 /* The end position of a row equals the start position
12467 of the next row. If PT is there, we would rather
12468 display it in the next line. */
12469 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12470 && MATRIX_ROW_END_CHARPOS (row) == PT
12471 && !cursor_row_p (w, row))
12472 ++row;
12473
12474 /* If within the scroll margin, scroll. Note that
12475 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12476 the next line would be drawn, and that
12477 this_scroll_margin can be zero. */
12478 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12479 || PT > MATRIX_ROW_END_CHARPOS (row)
12480 /* Line is completely visible last line in window
12481 and PT is to be set in the next line. */
12482 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12483 && PT == MATRIX_ROW_END_CHARPOS (row)
12484 && !row->ends_at_zv_p
12485 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12486 scroll_p = 1;
12487 }
12488 else if (PT < XFASTINT (w->last_point))
12489 {
12490 /* Cursor has to be moved backward. Note that PT >=
12491 CHARPOS (startp) because of the outer if-statement. */
12492 while (!row->mode_line_p
12493 && (MATRIX_ROW_START_CHARPOS (row) > PT
12494 || (MATRIX_ROW_START_CHARPOS (row) == PT
12495 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12496 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12497 row > w->current_matrix->rows
12498 && (row-1)->ends_in_newline_from_string_p))))
12499 && (row->y > top_scroll_margin
12500 || CHARPOS (startp) == BEGV))
12501 {
12502 xassert (row->enabled_p);
12503 --row;
12504 }
12505
12506 /* Consider the following case: Window starts at BEGV,
12507 there is invisible, intangible text at BEGV, so that
12508 display starts at some point START > BEGV. It can
12509 happen that we are called with PT somewhere between
12510 BEGV and START. Try to handle that case. */
12511 if (row < w->current_matrix->rows
12512 || row->mode_line_p)
12513 {
12514 row = w->current_matrix->rows;
12515 if (row->mode_line_p)
12516 ++row;
12517 }
12518
12519 /* Due to newlines in overlay strings, we may have to
12520 skip forward over overlay strings. */
12521 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12522 && MATRIX_ROW_END_CHARPOS (row) == PT
12523 && !cursor_row_p (w, row))
12524 ++row;
12525
12526 /* If within the scroll margin, scroll. */
12527 if (row->y < top_scroll_margin
12528 && CHARPOS (startp) != BEGV)
12529 scroll_p = 1;
12530 }
12531 else
12532 {
12533 /* Cursor did not move. So don't scroll even if cursor line
12534 is partially visible, as it was so before. */
12535 rc = CURSOR_MOVEMENT_SUCCESS;
12536 }
12537
12538 if (PT < MATRIX_ROW_START_CHARPOS (row)
12539 || PT > MATRIX_ROW_END_CHARPOS (row))
12540 {
12541 /* if PT is not in the glyph row, give up. */
12542 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12543 }
12544 else if (rc != CURSOR_MOVEMENT_SUCCESS
12545 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12546 && make_cursor_line_fully_visible_p)
12547 {
12548 if (PT == MATRIX_ROW_END_CHARPOS (row)
12549 && !row->ends_at_zv_p
12550 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12551 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12552 else if (row->height > window_box_height (w))
12553 {
12554 /* If we end up in a partially visible line, let's
12555 make it fully visible, except when it's taller
12556 than the window, in which case we can't do much
12557 about it. */
12558 *scroll_step = 1;
12559 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12560 }
12561 else
12562 {
12563 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12564 if (!cursor_row_fully_visible_p (w, 0, 1))
12565 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12566 else
12567 rc = CURSOR_MOVEMENT_SUCCESS;
12568 }
12569 }
12570 else if (scroll_p)
12571 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12572 else
12573 {
12574 do
12575 {
12576 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12577 {
12578 rc = CURSOR_MOVEMENT_SUCCESS;
12579 break;
12580 }
12581 ++row;
12582 }
12583 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12584 && MATRIX_ROW_START_CHARPOS (row) == PT
12585 && cursor_row_p (w, row));
12586 }
12587 }
12588 }
12589
12590 return rc;
12591 }
12592
12593 void
12594 set_vertical_scroll_bar (w)
12595 struct window *w;
12596 {
12597 int start, end, whole;
12598
12599 /* Calculate the start and end positions for the current window.
12600 At some point, it would be nice to choose between scrollbars
12601 which reflect the whole buffer size, with special markers
12602 indicating narrowing, and scrollbars which reflect only the
12603 visible region.
12604
12605 Note that mini-buffers sometimes aren't displaying any text. */
12606 if (!MINI_WINDOW_P (w)
12607 || (w == XWINDOW (minibuf_window)
12608 && NILP (echo_area_buffer[0])))
12609 {
12610 struct buffer *buf = XBUFFER (w->buffer);
12611 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12612 start = marker_position (w->start) - BUF_BEGV (buf);
12613 /* I don't think this is guaranteed to be right. For the
12614 moment, we'll pretend it is. */
12615 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12616
12617 if (end < start)
12618 end = start;
12619 if (whole < (end - start))
12620 whole = end - start;
12621 }
12622 else
12623 start = end = whole = 0;
12624
12625 /* Indicate what this scroll bar ought to be displaying now. */
12626 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12627 }
12628
12629
12630 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12631 selected_window is redisplayed.
12632
12633 We can return without actually redisplaying the window if
12634 fonts_changed_p is nonzero. In that case, redisplay_internal will
12635 retry. */
12636
12637 static void
12638 redisplay_window (window, just_this_one_p)
12639 Lisp_Object window;
12640 int just_this_one_p;
12641 {
12642 struct window *w = XWINDOW (window);
12643 struct frame *f = XFRAME (w->frame);
12644 struct buffer *buffer = XBUFFER (w->buffer);
12645 struct buffer *old = current_buffer;
12646 struct text_pos lpoint, opoint, startp;
12647 int update_mode_line;
12648 int tem;
12649 struct it it;
12650 /* Record it now because it's overwritten. */
12651 int current_matrix_up_to_date_p = 0;
12652 int used_current_matrix_p = 0;
12653 /* This is less strict than current_matrix_up_to_date_p.
12654 It indictes that the buffer contents and narrowing are unchanged. */
12655 int buffer_unchanged_p = 0;
12656 int temp_scroll_step = 0;
12657 int count = SPECPDL_INDEX ();
12658 int rc;
12659 int centering_position = -1;
12660 int last_line_misfit = 0;
12661
12662 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12663 opoint = lpoint;
12664
12665 /* W must be a leaf window here. */
12666 xassert (!NILP (w->buffer));
12667 #if GLYPH_DEBUG
12668 *w->desired_matrix->method = 0;
12669 #endif
12670
12671 specbind (Qinhibit_point_motion_hooks, Qt);
12672
12673 reconsider_clip_changes (w, buffer);
12674
12675 /* Has the mode line to be updated? */
12676 update_mode_line = (!NILP (w->update_mode_line)
12677 || update_mode_lines
12678 || buffer->clip_changed
12679 || buffer->prevent_redisplay_optimizations_p);
12680
12681 if (MINI_WINDOW_P (w))
12682 {
12683 if (w == XWINDOW (echo_area_window)
12684 && !NILP (echo_area_buffer[0]))
12685 {
12686 if (update_mode_line)
12687 /* We may have to update a tty frame's menu bar or a
12688 tool-bar. Example `M-x C-h C-h C-g'. */
12689 goto finish_menu_bars;
12690 else
12691 /* We've already displayed the echo area glyphs in this window. */
12692 goto finish_scroll_bars;
12693 }
12694 else if ((w != XWINDOW (minibuf_window)
12695 || minibuf_level == 0)
12696 /* When buffer is nonempty, redisplay window normally. */
12697 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12698 /* Quail displays non-mini buffers in minibuffer window.
12699 In that case, redisplay the window normally. */
12700 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12701 {
12702 /* W is a mini-buffer window, but it's not active, so clear
12703 it. */
12704 int yb = window_text_bottom_y (w);
12705 struct glyph_row *row;
12706 int y;
12707
12708 for (y = 0, row = w->desired_matrix->rows;
12709 y < yb;
12710 y += row->height, ++row)
12711 blank_row (w, row, y);
12712 goto finish_scroll_bars;
12713 }
12714
12715 clear_glyph_matrix (w->desired_matrix);
12716 }
12717
12718 /* Otherwise set up data on this window; select its buffer and point
12719 value. */
12720 /* Really select the buffer, for the sake of buffer-local
12721 variables. */
12722 set_buffer_internal_1 (XBUFFER (w->buffer));
12723 SET_TEXT_POS (opoint, PT, PT_BYTE);
12724
12725 current_matrix_up_to_date_p
12726 = (!NILP (w->window_end_valid)
12727 && !current_buffer->clip_changed
12728 && !current_buffer->prevent_redisplay_optimizations_p
12729 && XFASTINT (w->last_modified) >= MODIFF
12730 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12731
12732 buffer_unchanged_p
12733 = (!NILP (w->window_end_valid)
12734 && !current_buffer->clip_changed
12735 && XFASTINT (w->last_modified) >= MODIFF
12736 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12737
12738 /* When windows_or_buffers_changed is non-zero, we can't rely on
12739 the window end being valid, so set it to nil there. */
12740 if (windows_or_buffers_changed)
12741 {
12742 /* If window starts on a continuation line, maybe adjust the
12743 window start in case the window's width changed. */
12744 if (XMARKER (w->start)->buffer == current_buffer)
12745 compute_window_start_on_continuation_line (w);
12746
12747 w->window_end_valid = Qnil;
12748 }
12749
12750 /* Some sanity checks. */
12751 CHECK_WINDOW_END (w);
12752 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12753 abort ();
12754 if (BYTEPOS (opoint) < CHARPOS (opoint))
12755 abort ();
12756
12757 /* If %c is in mode line, update it if needed. */
12758 if (!NILP (w->column_number_displayed)
12759 /* This alternative quickly identifies a common case
12760 where no change is needed. */
12761 && !(PT == XFASTINT (w->last_point)
12762 && XFASTINT (w->last_modified) >= MODIFF
12763 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12764 && (XFASTINT (w->column_number_displayed)
12765 != (int) current_column ())) /* iftc */
12766 update_mode_line = 1;
12767
12768 /* Count number of windows showing the selected buffer. An indirect
12769 buffer counts as its base buffer. */
12770 if (!just_this_one_p)
12771 {
12772 struct buffer *current_base, *window_base;
12773 current_base = current_buffer;
12774 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12775 if (current_base->base_buffer)
12776 current_base = current_base->base_buffer;
12777 if (window_base->base_buffer)
12778 window_base = window_base->base_buffer;
12779 if (current_base == window_base)
12780 buffer_shared++;
12781 }
12782
12783 /* Point refers normally to the selected window. For any other
12784 window, set up appropriate value. */
12785 if (!EQ (window, selected_window))
12786 {
12787 int new_pt = XMARKER (w->pointm)->charpos;
12788 int new_pt_byte = marker_byte_position (w->pointm);
12789 if (new_pt < BEGV)
12790 {
12791 new_pt = BEGV;
12792 new_pt_byte = BEGV_BYTE;
12793 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12794 }
12795 else if (new_pt > (ZV - 1))
12796 {
12797 new_pt = ZV;
12798 new_pt_byte = ZV_BYTE;
12799 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12800 }
12801
12802 /* We don't use SET_PT so that the point-motion hooks don't run. */
12803 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12804 }
12805
12806 /* If any of the character widths specified in the display table
12807 have changed, invalidate the width run cache. It's true that
12808 this may be a bit late to catch such changes, but the rest of
12809 redisplay goes (non-fatally) haywire when the display table is
12810 changed, so why should we worry about doing any better? */
12811 if (current_buffer->width_run_cache)
12812 {
12813 struct Lisp_Char_Table *disptab = buffer_display_table ();
12814
12815 if (! disptab_matches_widthtab (disptab,
12816 XVECTOR (current_buffer->width_table)))
12817 {
12818 invalidate_region_cache (current_buffer,
12819 current_buffer->width_run_cache,
12820 BEG, Z);
12821 recompute_width_table (current_buffer, disptab);
12822 }
12823 }
12824
12825 /* If window-start is screwed up, choose a new one. */
12826 if (XMARKER (w->start)->buffer != current_buffer)
12827 goto recenter;
12828
12829 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12830
12831 /* If someone specified a new starting point but did not insist,
12832 check whether it can be used. */
12833 if (!NILP (w->optional_new_start)
12834 && CHARPOS (startp) >= BEGV
12835 && CHARPOS (startp) <= ZV)
12836 {
12837 w->optional_new_start = Qnil;
12838 start_display (&it, w, startp);
12839 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12840 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12841 if (IT_CHARPOS (it) == PT)
12842 w->force_start = Qt;
12843 /* IT may overshoot PT if text at PT is invisible. */
12844 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12845 w->force_start = Qt;
12846 }
12847
12848 /* Handle case where place to start displaying has been specified,
12849 unless the specified location is outside the accessible range. */
12850 if (!NILP (w->force_start)
12851 || w->frozen_window_start_p)
12852 {
12853 /* We set this later on if we have to adjust point. */
12854 int new_vpos = -1;
12855 int val;
12856
12857 w->force_start = Qnil;
12858 w->vscroll = 0;
12859 w->window_end_valid = Qnil;
12860
12861 /* Forget any recorded base line for line number display. */
12862 if (!buffer_unchanged_p)
12863 w->base_line_number = Qnil;
12864
12865 /* Redisplay the mode line. Select the buffer properly for that.
12866 Also, run the hook window-scroll-functions
12867 because we have scrolled. */
12868 /* Note, we do this after clearing force_start because
12869 if there's an error, it is better to forget about force_start
12870 than to get into an infinite loop calling the hook functions
12871 and having them get more errors. */
12872 if (!update_mode_line
12873 || ! NILP (Vwindow_scroll_functions))
12874 {
12875 update_mode_line = 1;
12876 w->update_mode_line = Qt;
12877 startp = run_window_scroll_functions (window, startp);
12878 }
12879
12880 w->last_modified = make_number (0);
12881 w->last_overlay_modified = make_number (0);
12882 if (CHARPOS (startp) < BEGV)
12883 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12884 else if (CHARPOS (startp) > ZV)
12885 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12886
12887 /* Redisplay, then check if cursor has been set during the
12888 redisplay. Give up if new fonts were loaded. */
12889 val = try_window (window, startp, 1);
12890 if (!val)
12891 {
12892 w->force_start = Qt;
12893 clear_glyph_matrix (w->desired_matrix);
12894 goto need_larger_matrices;
12895 }
12896 /* Point was outside the scroll margins. */
12897 if (val < 0)
12898 new_vpos = window_box_height (w) / 2;
12899
12900 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12901 {
12902 /* If point does not appear, try to move point so it does
12903 appear. The desired matrix has been built above, so we
12904 can use it here. */
12905 new_vpos = window_box_height (w) / 2;
12906 }
12907
12908 if (!cursor_row_fully_visible_p (w, 0, 0))
12909 {
12910 /* Point does appear, but on a line partly visible at end of window.
12911 Move it back to a fully-visible line. */
12912 new_vpos = window_box_height (w);
12913 }
12914
12915 /* If we need to move point for either of the above reasons,
12916 now actually do it. */
12917 if (new_vpos >= 0)
12918 {
12919 struct glyph_row *row;
12920
12921 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12922 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12923 ++row;
12924
12925 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12926 MATRIX_ROW_START_BYTEPOS (row));
12927
12928 if (w != XWINDOW (selected_window))
12929 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12930 else if (current_buffer == old)
12931 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12932
12933 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12934
12935 /* If we are highlighting the region, then we just changed
12936 the region, so redisplay to show it. */
12937 if (!NILP (Vtransient_mark_mode)
12938 && !NILP (current_buffer->mark_active))
12939 {
12940 clear_glyph_matrix (w->desired_matrix);
12941 if (!try_window (window, startp, 0))
12942 goto need_larger_matrices;
12943 }
12944 }
12945
12946 #if GLYPH_DEBUG
12947 debug_method_add (w, "forced window start");
12948 #endif
12949 goto done;
12950 }
12951
12952 /* Handle case where text has not changed, only point, and it has
12953 not moved off the frame, and we are not retrying after hscroll.
12954 (current_matrix_up_to_date_p is nonzero when retrying.) */
12955 if (current_matrix_up_to_date_p
12956 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12957 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12958 {
12959 switch (rc)
12960 {
12961 case CURSOR_MOVEMENT_SUCCESS:
12962 used_current_matrix_p = 1;
12963 goto done;
12964
12965 #if 0 /* try_cursor_movement never returns this value. */
12966 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12967 goto need_larger_matrices;
12968 #endif
12969
12970 case CURSOR_MOVEMENT_MUST_SCROLL:
12971 goto try_to_scroll;
12972
12973 default:
12974 abort ();
12975 }
12976 }
12977 /* If current starting point was originally the beginning of a line
12978 but no longer is, find a new starting point. */
12979 else if (!NILP (w->start_at_line_beg)
12980 && !(CHARPOS (startp) <= BEGV
12981 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12982 {
12983 #if GLYPH_DEBUG
12984 debug_method_add (w, "recenter 1");
12985 #endif
12986 goto recenter;
12987 }
12988
12989 /* Try scrolling with try_window_id. Value is > 0 if update has
12990 been done, it is -1 if we know that the same window start will
12991 not work. It is 0 if unsuccessful for some other reason. */
12992 else if ((tem = try_window_id (w)) != 0)
12993 {
12994 #if GLYPH_DEBUG
12995 debug_method_add (w, "try_window_id %d", tem);
12996 #endif
12997
12998 if (fonts_changed_p)
12999 goto need_larger_matrices;
13000 if (tem > 0)
13001 goto done;
13002
13003 /* Otherwise try_window_id has returned -1 which means that we
13004 don't want the alternative below this comment to execute. */
13005 }
13006 else if (CHARPOS (startp) >= BEGV
13007 && CHARPOS (startp) <= ZV
13008 && PT >= CHARPOS (startp)
13009 && (CHARPOS (startp) < ZV
13010 /* Avoid starting at end of buffer. */
13011 || CHARPOS (startp) == BEGV
13012 || (XFASTINT (w->last_modified) >= MODIFF
13013 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13014 {
13015
13016 /* If first window line is a continuation line, and window start
13017 is inside the modified region, but the first change is before
13018 current window start, we must select a new window start.*/
13019 if (NILP (w->start_at_line_beg)
13020 && CHARPOS (startp) > BEGV)
13021 {
13022 /* Make sure beg_unchanged and end_unchanged are up to date.
13023 Do it only if buffer has really changed. This may or may
13024 not have been done by try_window_id (see which) already. */
13025 if (MODIFF > SAVE_MODIFF
13026 /* This seems to happen sometimes after saving a buffer. */
13027 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13028 {
13029 if (GPT - BEG < BEG_UNCHANGED)
13030 BEG_UNCHANGED = GPT - BEG;
13031 if (Z - GPT < END_UNCHANGED)
13032 END_UNCHANGED = Z - GPT;
13033 }
13034
13035 if (CHARPOS (startp) > BEG + BEG_UNCHANGED
13036 && CHARPOS (startp) <= Z - END_UNCHANGED)
13037 {
13038 /* There doesn't seems to be a simple way to find a new
13039 window start that is near the old window start, so
13040 we just recenter. */
13041 goto recenter;
13042 }
13043 }
13044
13045 #if GLYPH_DEBUG
13046 debug_method_add (w, "same window start");
13047 #endif
13048
13049 /* Try to redisplay starting at same place as before.
13050 If point has not moved off frame, accept the results. */
13051 if (!current_matrix_up_to_date_p
13052 /* Don't use try_window_reusing_current_matrix in this case
13053 because a window scroll function can have changed the
13054 buffer. */
13055 || !NILP (Vwindow_scroll_functions)
13056 || MINI_WINDOW_P (w)
13057 || !(used_current_matrix_p
13058 = try_window_reusing_current_matrix (w)))
13059 {
13060 IF_DEBUG (debug_method_add (w, "1"));
13061 if (try_window (window, startp, 1) < 0)
13062 /* -1 means we need to scroll.
13063 0 means we need new matrices, but fonts_changed_p
13064 is set in that case, so we will detect it below. */
13065 goto try_to_scroll;
13066 }
13067
13068 if (fonts_changed_p)
13069 goto need_larger_matrices;
13070
13071 if (w->cursor.vpos >= 0)
13072 {
13073 if (!just_this_one_p
13074 || current_buffer->clip_changed
13075 || BEG_UNCHANGED < CHARPOS (startp))
13076 /* Forget any recorded base line for line number display. */
13077 w->base_line_number = Qnil;
13078
13079 if (!cursor_row_fully_visible_p (w, 1, 0))
13080 {
13081 clear_glyph_matrix (w->desired_matrix);
13082 last_line_misfit = 1;
13083 }
13084 /* Drop through and scroll. */
13085 else
13086 goto done;
13087 }
13088 else
13089 clear_glyph_matrix (w->desired_matrix);
13090 }
13091
13092 try_to_scroll:
13093
13094 w->last_modified = make_number (0);
13095 w->last_overlay_modified = make_number (0);
13096
13097 /* Redisplay the mode line. Select the buffer properly for that. */
13098 if (!update_mode_line)
13099 {
13100 update_mode_line = 1;
13101 w->update_mode_line = Qt;
13102 }
13103
13104 /* Try to scroll by specified few lines. */
13105 if ((scroll_conservatively
13106 || scroll_step
13107 || temp_scroll_step
13108 || NUMBERP (current_buffer->scroll_up_aggressively)
13109 || NUMBERP (current_buffer->scroll_down_aggressively))
13110 && !current_buffer->clip_changed
13111 && CHARPOS (startp) >= BEGV
13112 && CHARPOS (startp) <= ZV)
13113 {
13114 /* The function returns -1 if new fonts were loaded, 1 if
13115 successful, 0 if not successful. */
13116 int rc = try_scrolling (window, just_this_one_p,
13117 scroll_conservatively,
13118 scroll_step,
13119 temp_scroll_step, last_line_misfit);
13120 switch (rc)
13121 {
13122 case SCROLLING_SUCCESS:
13123 goto done;
13124
13125 case SCROLLING_NEED_LARGER_MATRICES:
13126 goto need_larger_matrices;
13127
13128 case SCROLLING_FAILED:
13129 break;
13130
13131 default:
13132 abort ();
13133 }
13134 }
13135
13136 /* Finally, just choose place to start which centers point */
13137
13138 recenter:
13139 if (centering_position < 0)
13140 centering_position = window_box_height (w) / 2;
13141
13142 #if GLYPH_DEBUG
13143 debug_method_add (w, "recenter");
13144 #endif
13145
13146 /* w->vscroll = 0; */
13147
13148 /* Forget any previously recorded base line for line number display. */
13149 if (!buffer_unchanged_p)
13150 w->base_line_number = Qnil;
13151
13152 /* Move backward half the height of the window. */
13153 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13154 it.current_y = it.last_visible_y;
13155 move_it_vertically_backward (&it, centering_position);
13156 xassert (IT_CHARPOS (it) >= BEGV);
13157
13158 /* The function move_it_vertically_backward may move over more
13159 than the specified y-distance. If it->w is small, e.g. a
13160 mini-buffer window, we may end up in front of the window's
13161 display area. Start displaying at the start of the line
13162 containing PT in this case. */
13163 if (it.current_y <= 0)
13164 {
13165 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13166 move_it_vertically_backward (&it, 0);
13167 #if 0
13168 /* I think this assert is bogus if buffer contains
13169 invisible text or images. KFS. */
13170 xassert (IT_CHARPOS (it) <= PT);
13171 #endif
13172 it.current_y = 0;
13173 }
13174
13175 it.current_x = it.hpos = 0;
13176
13177 /* Set startp here explicitly in case that helps avoid an infinite loop
13178 in case the window-scroll-functions functions get errors. */
13179 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13180
13181 /* Run scroll hooks. */
13182 startp = run_window_scroll_functions (window, it.current.pos);
13183
13184 /* Redisplay the window. */
13185 if (!current_matrix_up_to_date_p
13186 || windows_or_buffers_changed
13187 || cursor_type_changed
13188 /* Don't use try_window_reusing_current_matrix in this case
13189 because it can have changed the buffer. */
13190 || !NILP (Vwindow_scroll_functions)
13191 || !just_this_one_p
13192 || MINI_WINDOW_P (w)
13193 || !(used_current_matrix_p
13194 = try_window_reusing_current_matrix (w)))
13195 try_window (window, startp, 0);
13196
13197 /* If new fonts have been loaded (due to fontsets), give up. We
13198 have to start a new redisplay since we need to re-adjust glyph
13199 matrices. */
13200 if (fonts_changed_p)
13201 goto need_larger_matrices;
13202
13203 /* If cursor did not appear assume that the middle of the window is
13204 in the first line of the window. Do it again with the next line.
13205 (Imagine a window of height 100, displaying two lines of height
13206 60. Moving back 50 from it->last_visible_y will end in the first
13207 line.) */
13208 if (w->cursor.vpos < 0)
13209 {
13210 if (!NILP (w->window_end_valid)
13211 && PT >= Z - XFASTINT (w->window_end_pos))
13212 {
13213 clear_glyph_matrix (w->desired_matrix);
13214 move_it_by_lines (&it, 1, 0);
13215 try_window (window, it.current.pos, 0);
13216 }
13217 else if (PT < IT_CHARPOS (it))
13218 {
13219 clear_glyph_matrix (w->desired_matrix);
13220 move_it_by_lines (&it, -1, 0);
13221 try_window (window, it.current.pos, 0);
13222 }
13223 else
13224 {
13225 /* Not much we can do about it. */
13226 }
13227 }
13228
13229 /* Consider the following case: Window starts at BEGV, there is
13230 invisible, intangible text at BEGV, so that display starts at
13231 some point START > BEGV. It can happen that we are called with
13232 PT somewhere between BEGV and START. Try to handle that case. */
13233 if (w->cursor.vpos < 0)
13234 {
13235 struct glyph_row *row = w->current_matrix->rows;
13236 if (row->mode_line_p)
13237 ++row;
13238 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13239 }
13240
13241 if (!cursor_row_fully_visible_p (w, 0, 0))
13242 {
13243 /* If vscroll is enabled, disable it and try again. */
13244 if (w->vscroll)
13245 {
13246 w->vscroll = 0;
13247 clear_glyph_matrix (w->desired_matrix);
13248 goto recenter;
13249 }
13250
13251 /* If centering point failed to make the whole line visible,
13252 put point at the top instead. That has to make the whole line
13253 visible, if it can be done. */
13254 if (centering_position == 0)
13255 goto done;
13256
13257 clear_glyph_matrix (w->desired_matrix);
13258 centering_position = 0;
13259 goto recenter;
13260 }
13261
13262 done:
13263
13264 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13265 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13266 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13267 ? Qt : Qnil);
13268
13269 /* Display the mode line, if we must. */
13270 if ((update_mode_line
13271 /* If window not full width, must redo its mode line
13272 if (a) the window to its side is being redone and
13273 (b) we do a frame-based redisplay. This is a consequence
13274 of how inverted lines are drawn in frame-based redisplay. */
13275 || (!just_this_one_p
13276 && !FRAME_WINDOW_P (f)
13277 && !WINDOW_FULL_WIDTH_P (w))
13278 /* Line number to display. */
13279 || INTEGERP (w->base_line_pos)
13280 /* Column number is displayed and different from the one displayed. */
13281 || (!NILP (w->column_number_displayed)
13282 && (XFASTINT (w->column_number_displayed)
13283 != (int) current_column ()))) /* iftc */
13284 /* This means that the window has a mode line. */
13285 && (WINDOW_WANTS_MODELINE_P (w)
13286 || WINDOW_WANTS_HEADER_LINE_P (w)))
13287 {
13288 display_mode_lines (w);
13289
13290 /* If mode line height has changed, arrange for a thorough
13291 immediate redisplay using the correct mode line height. */
13292 if (WINDOW_WANTS_MODELINE_P (w)
13293 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13294 {
13295 fonts_changed_p = 1;
13296 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13297 = DESIRED_MODE_LINE_HEIGHT (w);
13298 }
13299
13300 /* If top line height has changed, arrange for a thorough
13301 immediate redisplay using the correct mode line height. */
13302 if (WINDOW_WANTS_HEADER_LINE_P (w)
13303 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13304 {
13305 fonts_changed_p = 1;
13306 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13307 = DESIRED_HEADER_LINE_HEIGHT (w);
13308 }
13309
13310 if (fonts_changed_p)
13311 goto need_larger_matrices;
13312 }
13313
13314 if (!line_number_displayed
13315 && !BUFFERP (w->base_line_pos))
13316 {
13317 w->base_line_pos = Qnil;
13318 w->base_line_number = Qnil;
13319 }
13320
13321 finish_menu_bars:
13322
13323 /* When we reach a frame's selected window, redo the frame's menu bar. */
13324 if (update_mode_line
13325 && EQ (FRAME_SELECTED_WINDOW (f), window))
13326 {
13327 int redisplay_menu_p = 0;
13328 int redisplay_tool_bar_p = 0;
13329
13330 if (FRAME_WINDOW_P (f))
13331 {
13332 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13333 || defined (USE_GTK)
13334 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13335 #else
13336 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13337 #endif
13338 }
13339 else
13340 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13341
13342 if (redisplay_menu_p)
13343 display_menu_bar (w);
13344
13345 #ifdef HAVE_WINDOW_SYSTEM
13346 #ifdef USE_GTK
13347 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13348 #else
13349 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13350 && (FRAME_TOOL_BAR_LINES (f) > 0
13351 || auto_resize_tool_bars_p);
13352
13353 #endif
13354
13355 if (redisplay_tool_bar_p)
13356 redisplay_tool_bar (f);
13357 #endif
13358 }
13359
13360 #ifdef HAVE_WINDOW_SYSTEM
13361 if (FRAME_WINDOW_P (f)
13362 && update_window_fringes (w, (just_this_one_p
13363 || (!used_current_matrix_p && !overlay_arrow_seen)
13364 || w->pseudo_window_p)))
13365 {
13366 update_begin (f);
13367 BLOCK_INPUT;
13368 if (draw_window_fringes (w, 1))
13369 x_draw_vertical_border (w);
13370 UNBLOCK_INPUT;
13371 update_end (f);
13372 }
13373 #endif /* HAVE_WINDOW_SYSTEM */
13374
13375 /* We go to this label, with fonts_changed_p nonzero,
13376 if it is necessary to try again using larger glyph matrices.
13377 We have to redeem the scroll bar even in this case,
13378 because the loop in redisplay_internal expects that. */
13379 need_larger_matrices:
13380 ;
13381 finish_scroll_bars:
13382
13383 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13384 {
13385 /* Set the thumb's position and size. */
13386 set_vertical_scroll_bar (w);
13387
13388 /* Note that we actually used the scroll bar attached to this
13389 window, so it shouldn't be deleted at the end of redisplay. */
13390 redeem_scroll_bar_hook (w);
13391 }
13392
13393 /* Restore current_buffer and value of point in it. */
13394 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13395 set_buffer_internal_1 (old);
13396 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13397
13398 unbind_to (count, Qnil);
13399 }
13400
13401
13402 /* Build the complete desired matrix of WINDOW with a window start
13403 buffer position POS.
13404
13405 Value is 1 if successful. It is zero if fonts were loaded during
13406 redisplay which makes re-adjusting glyph matrices necessary, and -1
13407 if point would appear in the scroll margins.
13408 (We check that only if CHECK_MARGINS is nonzero. */
13409
13410 int
13411 try_window (window, pos, check_margins)
13412 Lisp_Object window;
13413 struct text_pos pos;
13414 int check_margins;
13415 {
13416 struct window *w = XWINDOW (window);
13417 struct it it;
13418 struct glyph_row *last_text_row = NULL;
13419
13420 /* Make POS the new window start. */
13421 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13422
13423 /* Mark cursor position as unknown. No overlay arrow seen. */
13424 w->cursor.vpos = -1;
13425 overlay_arrow_seen = 0;
13426
13427 /* Initialize iterator and info to start at POS. */
13428 start_display (&it, w, pos);
13429
13430 /* Display all lines of W. */
13431 while (it.current_y < it.last_visible_y)
13432 {
13433 if (display_line (&it))
13434 last_text_row = it.glyph_row - 1;
13435 if (fonts_changed_p)
13436 return 0;
13437 }
13438
13439 /* Don't let the cursor end in the scroll margins. */
13440 if (check_margins
13441 && !MINI_WINDOW_P (w))
13442 {
13443 int this_scroll_margin;
13444
13445 this_scroll_margin = max (0, scroll_margin);
13446 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13447 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13448
13449 if ((w->cursor.y < this_scroll_margin
13450 && CHARPOS (pos) > BEGV
13451 && IT_CHARPOS (it) < ZV)
13452 /* rms: considering make_cursor_line_fully_visible_p here
13453 seems to give wrong results. We don't want to recenter
13454 when the last line is partly visible, we want to allow
13455 that case to be handled in the usual way. */
13456 || (w->cursor.y + 1) > it.last_visible_y)
13457 {
13458 w->cursor.vpos = -1;
13459 clear_glyph_matrix (w->desired_matrix);
13460 return -1;
13461 }
13462 }
13463
13464 /* If bottom moved off end of frame, change mode line percentage. */
13465 if (XFASTINT (w->window_end_pos) <= 0
13466 && Z != IT_CHARPOS (it))
13467 w->update_mode_line = Qt;
13468
13469 /* Set window_end_pos to the offset of the last character displayed
13470 on the window from the end of current_buffer. Set
13471 window_end_vpos to its row number. */
13472 if (last_text_row)
13473 {
13474 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13475 w->window_end_bytepos
13476 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13477 w->window_end_pos
13478 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13479 w->window_end_vpos
13480 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13481 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13482 ->displays_text_p);
13483 }
13484 else
13485 {
13486 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13487 w->window_end_pos = make_number (Z - ZV);
13488 w->window_end_vpos = make_number (0);
13489 }
13490
13491 /* But that is not valid info until redisplay finishes. */
13492 w->window_end_valid = Qnil;
13493 return 1;
13494 }
13495
13496
13497 \f
13498 /************************************************************************
13499 Window redisplay reusing current matrix when buffer has not changed
13500 ************************************************************************/
13501
13502 /* Try redisplay of window W showing an unchanged buffer with a
13503 different window start than the last time it was displayed by
13504 reusing its current matrix. Value is non-zero if successful.
13505 W->start is the new window start. */
13506
13507 static int
13508 try_window_reusing_current_matrix (w)
13509 struct window *w;
13510 {
13511 struct frame *f = XFRAME (w->frame);
13512 struct glyph_row *row, *bottom_row;
13513 struct it it;
13514 struct run run;
13515 struct text_pos start, new_start;
13516 int nrows_scrolled, i;
13517 struct glyph_row *last_text_row;
13518 struct glyph_row *last_reused_text_row;
13519 struct glyph_row *start_row;
13520 int start_vpos, min_y, max_y;
13521
13522 #if GLYPH_DEBUG
13523 if (inhibit_try_window_reusing)
13524 return 0;
13525 #endif
13526
13527 if (/* This function doesn't handle terminal frames. */
13528 !FRAME_WINDOW_P (f)
13529 /* Don't try to reuse the display if windows have been split
13530 or such. */
13531 || windows_or_buffers_changed
13532 || cursor_type_changed)
13533 return 0;
13534
13535 /* Can't do this if region may have changed. */
13536 if ((!NILP (Vtransient_mark_mode)
13537 && !NILP (current_buffer->mark_active))
13538 || !NILP (w->region_showing)
13539 || !NILP (Vshow_trailing_whitespace))
13540 return 0;
13541
13542 /* If top-line visibility has changed, give up. */
13543 if (WINDOW_WANTS_HEADER_LINE_P (w)
13544 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13545 return 0;
13546
13547 /* Give up if old or new display is scrolled vertically. We could
13548 make this function handle this, but right now it doesn't. */
13549 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13550 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13551 return 0;
13552
13553 /* The variable new_start now holds the new window start. The old
13554 start `start' can be determined from the current matrix. */
13555 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13556 start = start_row->start.pos;
13557 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13558
13559 /* Clear the desired matrix for the display below. */
13560 clear_glyph_matrix (w->desired_matrix);
13561
13562 if (CHARPOS (new_start) <= CHARPOS (start))
13563 {
13564 int first_row_y;
13565
13566 /* Don't use this method if the display starts with an ellipsis
13567 displayed for invisible text. It's not easy to handle that case
13568 below, and it's certainly not worth the effort since this is
13569 not a frequent case. */
13570 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13571 return 0;
13572
13573 IF_DEBUG (debug_method_add (w, "twu1"));
13574
13575 /* Display up to a row that can be reused. The variable
13576 last_text_row is set to the last row displayed that displays
13577 text. Note that it.vpos == 0 if or if not there is a
13578 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13579 start_display (&it, w, new_start);
13580 first_row_y = it.current_y;
13581 w->cursor.vpos = -1;
13582 last_text_row = last_reused_text_row = NULL;
13583
13584 while (it.current_y < it.last_visible_y
13585 && !fonts_changed_p)
13586 {
13587 /* If we have reached into the characters in the START row,
13588 that means the line boundaries have changed. So we
13589 can't start copying with the row START. Maybe it will
13590 work to start copying with the following row. */
13591 while (IT_CHARPOS (it) > CHARPOS (start))
13592 {
13593 /* Advance to the next row as the "start". */
13594 start_row++;
13595 start = start_row->start.pos;
13596 /* If there are no more rows to try, or just one, give up. */
13597 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13598 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13599 || CHARPOS (start) == ZV)
13600 {
13601 clear_glyph_matrix (w->desired_matrix);
13602 return 0;
13603 }
13604
13605 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13606 }
13607 /* If we have reached alignment,
13608 we can copy the rest of the rows. */
13609 if (IT_CHARPOS (it) == CHARPOS (start))
13610 break;
13611
13612 if (display_line (&it))
13613 last_text_row = it.glyph_row - 1;
13614 }
13615
13616 /* A value of current_y < last_visible_y means that we stopped
13617 at the previous window start, which in turn means that we
13618 have at least one reusable row. */
13619 if (it.current_y < it.last_visible_y)
13620 {
13621 /* IT.vpos always starts from 0; it counts text lines. */
13622 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13623
13624 /* Find PT if not already found in the lines displayed. */
13625 if (w->cursor.vpos < 0)
13626 {
13627 int dy = it.current_y - start_row->y;
13628
13629 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13630 row = row_containing_pos (w, PT, row, NULL, dy);
13631 if (row)
13632 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13633 dy, nrows_scrolled);
13634 else
13635 {
13636 clear_glyph_matrix (w->desired_matrix);
13637 return 0;
13638 }
13639 }
13640
13641 /* Scroll the display. Do it before the current matrix is
13642 changed. The problem here is that update has not yet
13643 run, i.e. part of the current matrix is not up to date.
13644 scroll_run_hook will clear the cursor, and use the
13645 current matrix to get the height of the row the cursor is
13646 in. */
13647 run.current_y = start_row->y;
13648 run.desired_y = it.current_y;
13649 run.height = it.last_visible_y - it.current_y;
13650
13651 if (run.height > 0 && run.current_y != run.desired_y)
13652 {
13653 update_begin (f);
13654 rif->update_window_begin_hook (w);
13655 rif->clear_window_mouse_face (w);
13656 rif->scroll_run_hook (w, &run);
13657 rif->update_window_end_hook (w, 0, 0);
13658 update_end (f);
13659 }
13660
13661 /* Shift current matrix down by nrows_scrolled lines. */
13662 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13663 rotate_matrix (w->current_matrix,
13664 start_vpos,
13665 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13666 nrows_scrolled);
13667
13668 /* Disable lines that must be updated. */
13669 for (i = 0; i < it.vpos; ++i)
13670 (start_row + i)->enabled_p = 0;
13671
13672 /* Re-compute Y positions. */
13673 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13674 max_y = it.last_visible_y;
13675 for (row = start_row + nrows_scrolled;
13676 row < bottom_row;
13677 ++row)
13678 {
13679 row->y = it.current_y;
13680 row->visible_height = row->height;
13681
13682 if (row->y < min_y)
13683 row->visible_height -= min_y - row->y;
13684 if (row->y + row->height > max_y)
13685 row->visible_height -= row->y + row->height - max_y;
13686 row->redraw_fringe_bitmaps_p = 1;
13687
13688 it.current_y += row->height;
13689
13690 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13691 last_reused_text_row = row;
13692 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13693 break;
13694 }
13695
13696 /* Disable lines in the current matrix which are now
13697 below the window. */
13698 for (++row; row < bottom_row; ++row)
13699 row->enabled_p = row->mode_line_p = 0;
13700 }
13701
13702 /* Update window_end_pos etc.; last_reused_text_row is the last
13703 reused row from the current matrix containing text, if any.
13704 The value of last_text_row is the last displayed line
13705 containing text. */
13706 if (last_reused_text_row)
13707 {
13708 w->window_end_bytepos
13709 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13710 w->window_end_pos
13711 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13712 w->window_end_vpos
13713 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13714 w->current_matrix));
13715 }
13716 else if (last_text_row)
13717 {
13718 w->window_end_bytepos
13719 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13720 w->window_end_pos
13721 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13722 w->window_end_vpos
13723 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13724 }
13725 else
13726 {
13727 /* This window must be completely empty. */
13728 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13729 w->window_end_pos = make_number (Z - ZV);
13730 w->window_end_vpos = make_number (0);
13731 }
13732 w->window_end_valid = Qnil;
13733
13734 /* Update hint: don't try scrolling again in update_window. */
13735 w->desired_matrix->no_scrolling_p = 1;
13736
13737 #if GLYPH_DEBUG
13738 debug_method_add (w, "try_window_reusing_current_matrix 1");
13739 #endif
13740 return 1;
13741 }
13742 else if (CHARPOS (new_start) > CHARPOS (start))
13743 {
13744 struct glyph_row *pt_row, *row;
13745 struct glyph_row *first_reusable_row;
13746 struct glyph_row *first_row_to_display;
13747 int dy;
13748 int yb = window_text_bottom_y (w);
13749
13750 /* Find the row starting at new_start, if there is one. Don't
13751 reuse a partially visible line at the end. */
13752 first_reusable_row = start_row;
13753 while (first_reusable_row->enabled_p
13754 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13755 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13756 < CHARPOS (new_start)))
13757 ++first_reusable_row;
13758
13759 /* Give up if there is no row to reuse. */
13760 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13761 || !first_reusable_row->enabled_p
13762 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13763 != CHARPOS (new_start)))
13764 return 0;
13765
13766 /* We can reuse fully visible rows beginning with
13767 first_reusable_row to the end of the window. Set
13768 first_row_to_display to the first row that cannot be reused.
13769 Set pt_row to the row containing point, if there is any. */
13770 pt_row = NULL;
13771 for (first_row_to_display = first_reusable_row;
13772 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13773 ++first_row_to_display)
13774 {
13775 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13776 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13777 pt_row = first_row_to_display;
13778 }
13779
13780 /* Start displaying at the start of first_row_to_display. */
13781 xassert (first_row_to_display->y < yb);
13782 init_to_row_start (&it, w, first_row_to_display);
13783
13784 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13785 - start_vpos);
13786 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13787 - nrows_scrolled);
13788 it.current_y = (first_row_to_display->y - first_reusable_row->y
13789 + WINDOW_HEADER_LINE_HEIGHT (w));
13790
13791 /* Display lines beginning with first_row_to_display in the
13792 desired matrix. Set last_text_row to the last row displayed
13793 that displays text. */
13794 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13795 if (pt_row == NULL)
13796 w->cursor.vpos = -1;
13797 last_text_row = NULL;
13798 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13799 if (display_line (&it))
13800 last_text_row = it.glyph_row - 1;
13801
13802 /* Give up If point isn't in a row displayed or reused. */
13803 if (w->cursor.vpos < 0)
13804 {
13805 clear_glyph_matrix (w->desired_matrix);
13806 return 0;
13807 }
13808
13809 /* If point is in a reused row, adjust y and vpos of the cursor
13810 position. */
13811 if (pt_row)
13812 {
13813 w->cursor.vpos -= nrows_scrolled;
13814 w->cursor.y -= first_reusable_row->y - start_row->y;
13815 }
13816
13817 /* Scroll the display. */
13818 run.current_y = first_reusable_row->y;
13819 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13820 run.height = it.last_visible_y - run.current_y;
13821 dy = run.current_y - run.desired_y;
13822
13823 if (run.height)
13824 {
13825 update_begin (f);
13826 rif->update_window_begin_hook (w);
13827 rif->clear_window_mouse_face (w);
13828 rif->scroll_run_hook (w, &run);
13829 rif->update_window_end_hook (w, 0, 0);
13830 update_end (f);
13831 }
13832
13833 /* Adjust Y positions of reused rows. */
13834 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13835 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13836 max_y = it.last_visible_y;
13837 for (row = first_reusable_row; row < first_row_to_display; ++row)
13838 {
13839 row->y -= dy;
13840 row->visible_height = row->height;
13841 if (row->y < min_y)
13842 row->visible_height -= min_y - row->y;
13843 if (row->y + row->height > max_y)
13844 row->visible_height -= row->y + row->height - max_y;
13845 row->redraw_fringe_bitmaps_p = 1;
13846 }
13847
13848 /* Scroll the current matrix. */
13849 xassert (nrows_scrolled > 0);
13850 rotate_matrix (w->current_matrix,
13851 start_vpos,
13852 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13853 -nrows_scrolled);
13854
13855 /* Disable rows not reused. */
13856 for (row -= nrows_scrolled; row < bottom_row; ++row)
13857 row->enabled_p = 0;
13858
13859 /* Point may have moved to a different line, so we cannot assume that
13860 the previous cursor position is valid; locate the correct row. */
13861 if (pt_row)
13862 {
13863 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13864 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13865 row++)
13866 {
13867 w->cursor.vpos++;
13868 w->cursor.y = row->y;
13869 }
13870 if (row < bottom_row)
13871 {
13872 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13873 while (glyph->charpos < PT)
13874 {
13875 w->cursor.hpos++;
13876 w->cursor.x += glyph->pixel_width;
13877 glyph++;
13878 }
13879 }
13880 }
13881
13882 /* Adjust window end. A null value of last_text_row means that
13883 the window end is in reused rows which in turn means that
13884 only its vpos can have changed. */
13885 if (last_text_row)
13886 {
13887 w->window_end_bytepos
13888 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13889 w->window_end_pos
13890 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13891 w->window_end_vpos
13892 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13893 }
13894 else
13895 {
13896 w->window_end_vpos
13897 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13898 }
13899
13900 w->window_end_valid = Qnil;
13901 w->desired_matrix->no_scrolling_p = 1;
13902
13903 #if GLYPH_DEBUG
13904 debug_method_add (w, "try_window_reusing_current_matrix 2");
13905 #endif
13906 return 1;
13907 }
13908
13909 return 0;
13910 }
13911
13912
13913 \f
13914 /************************************************************************
13915 Window redisplay reusing current matrix when buffer has changed
13916 ************************************************************************/
13917
13918 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13919 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13920 int *, int *));
13921 static struct glyph_row *
13922 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13923 struct glyph_row *));
13924
13925
13926 /* Return the last row in MATRIX displaying text. If row START is
13927 non-null, start searching with that row. IT gives the dimensions
13928 of the display. Value is null if matrix is empty; otherwise it is
13929 a pointer to the row found. */
13930
13931 static struct glyph_row *
13932 find_last_row_displaying_text (matrix, it, start)
13933 struct glyph_matrix *matrix;
13934 struct it *it;
13935 struct glyph_row *start;
13936 {
13937 struct glyph_row *row, *row_found;
13938
13939 /* Set row_found to the last row in IT->w's current matrix
13940 displaying text. The loop looks funny but think of partially
13941 visible lines. */
13942 row_found = NULL;
13943 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13944 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13945 {
13946 xassert (row->enabled_p);
13947 row_found = row;
13948 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13949 break;
13950 ++row;
13951 }
13952
13953 return row_found;
13954 }
13955
13956
13957 /* Return the last row in the current matrix of W that is not affected
13958 by changes at the start of current_buffer that occurred since W's
13959 current matrix was built. Value is null if no such row exists.
13960
13961 BEG_UNCHANGED us the number of characters unchanged at the start of
13962 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13963 first changed character in current_buffer. Characters at positions <
13964 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13965 when the current matrix was built. */
13966
13967 static struct glyph_row *
13968 find_last_unchanged_at_beg_row (w)
13969 struct window *w;
13970 {
13971 int first_changed_pos = BEG + BEG_UNCHANGED;
13972 struct glyph_row *row;
13973 struct glyph_row *row_found = NULL;
13974 int yb = window_text_bottom_y (w);
13975
13976 /* Find the last row displaying unchanged text. */
13977 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13978 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13979 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13980 {
13981 if (/* If row ends before first_changed_pos, it is unchanged,
13982 except in some case. */
13983 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13984 /* When row ends in ZV and we write at ZV it is not
13985 unchanged. */
13986 && !row->ends_at_zv_p
13987 /* When first_changed_pos is the end of a continued line,
13988 row is not unchanged because it may be no longer
13989 continued. */
13990 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13991 && (row->continued_p
13992 || row->exact_window_width_line_p)))
13993 row_found = row;
13994
13995 /* Stop if last visible row. */
13996 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13997 break;
13998
13999 ++row;
14000 }
14001
14002 return row_found;
14003 }
14004
14005
14006 /* Find the first glyph row in the current matrix of W that is not
14007 affected by changes at the end of current_buffer since the
14008 time W's current matrix was built.
14009
14010 Return in *DELTA the number of chars by which buffer positions in
14011 unchanged text at the end of current_buffer must be adjusted.
14012
14013 Return in *DELTA_BYTES the corresponding number of bytes.
14014
14015 Value is null if no such row exists, i.e. all rows are affected by
14016 changes. */
14017
14018 static struct glyph_row *
14019 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14020 struct window *w;
14021 int *delta, *delta_bytes;
14022 {
14023 struct glyph_row *row;
14024 struct glyph_row *row_found = NULL;
14025
14026 *delta = *delta_bytes = 0;
14027
14028 /* Display must not have been paused, otherwise the current matrix
14029 is not up to date. */
14030 if (NILP (w->window_end_valid))
14031 abort ();
14032
14033 /* A value of window_end_pos >= END_UNCHANGED means that the window
14034 end is in the range of changed text. If so, there is no
14035 unchanged row at the end of W's current matrix. */
14036 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14037 return NULL;
14038
14039 /* Set row to the last row in W's current matrix displaying text. */
14040 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14041
14042 /* If matrix is entirely empty, no unchanged row exists. */
14043 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14044 {
14045 /* The value of row is the last glyph row in the matrix having a
14046 meaningful buffer position in it. The end position of row
14047 corresponds to window_end_pos. This allows us to translate
14048 buffer positions in the current matrix to current buffer
14049 positions for characters not in changed text. */
14050 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14051 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14052 int last_unchanged_pos, last_unchanged_pos_old;
14053 struct glyph_row *first_text_row
14054 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14055
14056 *delta = Z - Z_old;
14057 *delta_bytes = Z_BYTE - Z_BYTE_old;
14058
14059 /* Set last_unchanged_pos to the buffer position of the last
14060 character in the buffer that has not been changed. Z is the
14061 index + 1 of the last character in current_buffer, i.e. by
14062 subtracting END_UNCHANGED we get the index of the last
14063 unchanged character, and we have to add BEG to get its buffer
14064 position. */
14065 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14066 last_unchanged_pos_old = last_unchanged_pos - *delta;
14067
14068 /* Search backward from ROW for a row displaying a line that
14069 starts at a minimum position >= last_unchanged_pos_old. */
14070 for (; row > first_text_row; --row)
14071 {
14072 /* This used to abort, but it can happen.
14073 It is ok to just stop the search instead here. KFS. */
14074 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14075 break;
14076
14077 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14078 row_found = row;
14079 }
14080 }
14081
14082 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14083 abort ();
14084
14085 return row_found;
14086 }
14087
14088
14089 /* Make sure that glyph rows in the current matrix of window W
14090 reference the same glyph memory as corresponding rows in the
14091 frame's frame matrix. This function is called after scrolling W's
14092 current matrix on a terminal frame in try_window_id and
14093 try_window_reusing_current_matrix. */
14094
14095 static void
14096 sync_frame_with_window_matrix_rows (w)
14097 struct window *w;
14098 {
14099 struct frame *f = XFRAME (w->frame);
14100 struct glyph_row *window_row, *window_row_end, *frame_row;
14101
14102 /* Preconditions: W must be a leaf window and full-width. Its frame
14103 must have a frame matrix. */
14104 xassert (NILP (w->hchild) && NILP (w->vchild));
14105 xassert (WINDOW_FULL_WIDTH_P (w));
14106 xassert (!FRAME_WINDOW_P (f));
14107
14108 /* If W is a full-width window, glyph pointers in W's current matrix
14109 have, by definition, to be the same as glyph pointers in the
14110 corresponding frame matrix. Note that frame matrices have no
14111 marginal areas (see build_frame_matrix). */
14112 window_row = w->current_matrix->rows;
14113 window_row_end = window_row + w->current_matrix->nrows;
14114 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14115 while (window_row < window_row_end)
14116 {
14117 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14118 struct glyph *end = window_row->glyphs[LAST_AREA];
14119
14120 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14121 frame_row->glyphs[TEXT_AREA] = start;
14122 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14123 frame_row->glyphs[LAST_AREA] = end;
14124
14125 /* Disable frame rows whose corresponding window rows have
14126 been disabled in try_window_id. */
14127 if (!window_row->enabled_p)
14128 frame_row->enabled_p = 0;
14129
14130 ++window_row, ++frame_row;
14131 }
14132 }
14133
14134
14135 /* Find the glyph row in window W containing CHARPOS. Consider all
14136 rows between START and END (not inclusive). END null means search
14137 all rows to the end of the display area of W. Value is the row
14138 containing CHARPOS or null. */
14139
14140 struct glyph_row *
14141 row_containing_pos (w, charpos, start, end, dy)
14142 struct window *w;
14143 int charpos;
14144 struct glyph_row *start, *end;
14145 int dy;
14146 {
14147 struct glyph_row *row = start;
14148 int last_y;
14149
14150 /* If we happen to start on a header-line, skip that. */
14151 if (row->mode_line_p)
14152 ++row;
14153
14154 if ((end && row >= end) || !row->enabled_p)
14155 return NULL;
14156
14157 last_y = window_text_bottom_y (w) - dy;
14158
14159 while (1)
14160 {
14161 /* Give up if we have gone too far. */
14162 if (end && row >= end)
14163 return NULL;
14164 /* This formerly returned if they were equal.
14165 I think that both quantities are of a "last plus one" type;
14166 if so, when they are equal, the row is within the screen. -- rms. */
14167 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14168 return NULL;
14169
14170 /* If it is in this row, return this row. */
14171 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14172 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14173 /* The end position of a row equals the start
14174 position of the next row. If CHARPOS is there, we
14175 would rather display it in the next line, except
14176 when this line ends in ZV. */
14177 && !row->ends_at_zv_p
14178 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14179 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14180 return row;
14181 ++row;
14182 }
14183 }
14184
14185
14186 /* Try to redisplay window W by reusing its existing display. W's
14187 current matrix must be up to date when this function is called,
14188 i.e. window_end_valid must not be nil.
14189
14190 Value is
14191
14192 1 if display has been updated
14193 0 if otherwise unsuccessful
14194 -1 if redisplay with same window start is known not to succeed
14195
14196 The following steps are performed:
14197
14198 1. Find the last row in the current matrix of W that is not
14199 affected by changes at the start of current_buffer. If no such row
14200 is found, give up.
14201
14202 2. Find the first row in W's current matrix that is not affected by
14203 changes at the end of current_buffer. Maybe there is no such row.
14204
14205 3. Display lines beginning with the row + 1 found in step 1 to the
14206 row found in step 2 or, if step 2 didn't find a row, to the end of
14207 the window.
14208
14209 4. If cursor is not known to appear on the window, give up.
14210
14211 5. If display stopped at the row found in step 2, scroll the
14212 display and current matrix as needed.
14213
14214 6. Maybe display some lines at the end of W, if we must. This can
14215 happen under various circumstances, like a partially visible line
14216 becoming fully visible, or because newly displayed lines are displayed
14217 in smaller font sizes.
14218
14219 7. Update W's window end information. */
14220
14221 static int
14222 try_window_id (w)
14223 struct window *w;
14224 {
14225 struct frame *f = XFRAME (w->frame);
14226 struct glyph_matrix *current_matrix = w->current_matrix;
14227 struct glyph_matrix *desired_matrix = w->desired_matrix;
14228 struct glyph_row *last_unchanged_at_beg_row;
14229 struct glyph_row *first_unchanged_at_end_row;
14230 struct glyph_row *row;
14231 struct glyph_row *bottom_row;
14232 int bottom_vpos;
14233 struct it it;
14234 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14235 struct text_pos start_pos;
14236 struct run run;
14237 int first_unchanged_at_end_vpos = 0;
14238 struct glyph_row *last_text_row, *last_text_row_at_end;
14239 struct text_pos start;
14240 int first_changed_charpos, last_changed_charpos;
14241
14242 #if GLYPH_DEBUG
14243 if (inhibit_try_window_id)
14244 return 0;
14245 #endif
14246
14247 /* This is handy for debugging. */
14248 #if 0
14249 #define GIVE_UP(X) \
14250 do { \
14251 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14252 return 0; \
14253 } while (0)
14254 #else
14255 #define GIVE_UP(X) return 0
14256 #endif
14257
14258 SET_TEXT_POS_FROM_MARKER (start, w->start);
14259
14260 /* Don't use this for mini-windows because these can show
14261 messages and mini-buffers, and we don't handle that here. */
14262 if (MINI_WINDOW_P (w))
14263 GIVE_UP (1);
14264
14265 /* This flag is used to prevent redisplay optimizations. */
14266 if (windows_or_buffers_changed || cursor_type_changed)
14267 GIVE_UP (2);
14268
14269 /* Verify that narrowing has not changed.
14270 Also verify that we were not told to prevent redisplay optimizations.
14271 It would be nice to further
14272 reduce the number of cases where this prevents try_window_id. */
14273 if (current_buffer->clip_changed
14274 || current_buffer->prevent_redisplay_optimizations_p)
14275 GIVE_UP (3);
14276
14277 /* Window must either use window-based redisplay or be full width. */
14278 if (!FRAME_WINDOW_P (f)
14279 && (!line_ins_del_ok
14280 || !WINDOW_FULL_WIDTH_P (w)))
14281 GIVE_UP (4);
14282
14283 /* Give up if point is not known NOT to appear in W. */
14284 if (PT < CHARPOS (start))
14285 GIVE_UP (5);
14286
14287 /* Another way to prevent redisplay optimizations. */
14288 if (XFASTINT (w->last_modified) == 0)
14289 GIVE_UP (6);
14290
14291 /* Verify that window is not hscrolled. */
14292 if (XFASTINT (w->hscroll) != 0)
14293 GIVE_UP (7);
14294
14295 /* Verify that display wasn't paused. */
14296 if (NILP (w->window_end_valid))
14297 GIVE_UP (8);
14298
14299 /* Can't use this if highlighting a region because a cursor movement
14300 will do more than just set the cursor. */
14301 if (!NILP (Vtransient_mark_mode)
14302 && !NILP (current_buffer->mark_active))
14303 GIVE_UP (9);
14304
14305 /* Likewise if highlighting trailing whitespace. */
14306 if (!NILP (Vshow_trailing_whitespace))
14307 GIVE_UP (11);
14308
14309 /* Likewise if showing a region. */
14310 if (!NILP (w->region_showing))
14311 GIVE_UP (10);
14312
14313 /* Can use this if overlay arrow position and or string have changed. */
14314 if (overlay_arrows_changed_p ())
14315 GIVE_UP (12);
14316
14317
14318 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14319 only if buffer has really changed. The reason is that the gap is
14320 initially at Z for freshly visited files. The code below would
14321 set end_unchanged to 0 in that case. */
14322 if (MODIFF > SAVE_MODIFF
14323 /* This seems to happen sometimes after saving a buffer. */
14324 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14325 {
14326 if (GPT - BEG < BEG_UNCHANGED)
14327 BEG_UNCHANGED = GPT - BEG;
14328 if (Z - GPT < END_UNCHANGED)
14329 END_UNCHANGED = Z - GPT;
14330 }
14331
14332 /* The position of the first and last character that has been changed. */
14333 first_changed_charpos = BEG + BEG_UNCHANGED;
14334 last_changed_charpos = Z - END_UNCHANGED;
14335
14336 /* If window starts after a line end, and the last change is in
14337 front of that newline, then changes don't affect the display.
14338 This case happens with stealth-fontification. Note that although
14339 the display is unchanged, glyph positions in the matrix have to
14340 be adjusted, of course. */
14341 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14342 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14343 && ((last_changed_charpos < CHARPOS (start)
14344 && CHARPOS (start) == BEGV)
14345 || (last_changed_charpos < CHARPOS (start) - 1
14346 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14347 {
14348 int Z_old, delta, Z_BYTE_old, delta_bytes;
14349 struct glyph_row *r0;
14350
14351 /* Compute how many chars/bytes have been added to or removed
14352 from the buffer. */
14353 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14354 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14355 delta = Z - Z_old;
14356 delta_bytes = Z_BYTE - Z_BYTE_old;
14357
14358 /* Give up if PT is not in the window. Note that it already has
14359 been checked at the start of try_window_id that PT is not in
14360 front of the window start. */
14361 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14362 GIVE_UP (13);
14363
14364 /* If window start is unchanged, we can reuse the whole matrix
14365 as is, after adjusting glyph positions. No need to compute
14366 the window end again, since its offset from Z hasn't changed. */
14367 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14368 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14369 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14370 /* PT must not be in a partially visible line. */
14371 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14372 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14373 {
14374 /* Adjust positions in the glyph matrix. */
14375 if (delta || delta_bytes)
14376 {
14377 struct glyph_row *r1
14378 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14379 increment_matrix_positions (w->current_matrix,
14380 MATRIX_ROW_VPOS (r0, current_matrix),
14381 MATRIX_ROW_VPOS (r1, current_matrix),
14382 delta, delta_bytes);
14383 }
14384
14385 /* Set the cursor. */
14386 row = row_containing_pos (w, PT, r0, NULL, 0);
14387 if (row)
14388 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14389 else
14390 abort ();
14391 return 1;
14392 }
14393 }
14394
14395 /* Handle the case that changes are all below what is displayed in
14396 the window, and that PT is in the window. This shortcut cannot
14397 be taken if ZV is visible in the window, and text has been added
14398 there that is visible in the window. */
14399 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14400 /* ZV is not visible in the window, or there are no
14401 changes at ZV, actually. */
14402 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14403 || first_changed_charpos == last_changed_charpos))
14404 {
14405 struct glyph_row *r0;
14406
14407 /* Give up if PT is not in the window. Note that it already has
14408 been checked at the start of try_window_id that PT is not in
14409 front of the window start. */
14410 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14411 GIVE_UP (14);
14412
14413 /* If window start is unchanged, we can reuse the whole matrix
14414 as is, without changing glyph positions since no text has
14415 been added/removed in front of the window end. */
14416 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14417 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14418 /* PT must not be in a partially visible line. */
14419 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14420 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14421 {
14422 /* We have to compute the window end anew since text
14423 can have been added/removed after it. */
14424 w->window_end_pos
14425 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14426 w->window_end_bytepos
14427 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14428
14429 /* Set the cursor. */
14430 row = row_containing_pos (w, PT, r0, NULL, 0);
14431 if (row)
14432 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14433 else
14434 abort ();
14435 return 2;
14436 }
14437 }
14438
14439 /* Give up if window start is in the changed area.
14440
14441 The condition used to read
14442
14443 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14444
14445 but why that was tested escapes me at the moment. */
14446 if (CHARPOS (start) >= first_changed_charpos
14447 && CHARPOS (start) <= last_changed_charpos)
14448 GIVE_UP (15);
14449
14450 /* Check that window start agrees with the start of the first glyph
14451 row in its current matrix. Check this after we know the window
14452 start is not in changed text, otherwise positions would not be
14453 comparable. */
14454 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14455 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14456 GIVE_UP (16);
14457
14458 /* Give up if the window ends in strings. Overlay strings
14459 at the end are difficult to handle, so don't try. */
14460 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14461 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14462 GIVE_UP (20);
14463
14464 /* Compute the position at which we have to start displaying new
14465 lines. Some of the lines at the top of the window might be
14466 reusable because they are not displaying changed text. Find the
14467 last row in W's current matrix not affected by changes at the
14468 start of current_buffer. Value is null if changes start in the
14469 first line of window. */
14470 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14471 if (last_unchanged_at_beg_row)
14472 {
14473 /* Avoid starting to display in the moddle of a character, a TAB
14474 for instance. This is easier than to set up the iterator
14475 exactly, and it's not a frequent case, so the additional
14476 effort wouldn't really pay off. */
14477 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14478 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14479 && last_unchanged_at_beg_row > w->current_matrix->rows)
14480 --last_unchanged_at_beg_row;
14481
14482 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14483 GIVE_UP (17);
14484
14485 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14486 GIVE_UP (18);
14487 start_pos = it.current.pos;
14488
14489 /* Start displaying new lines in the desired matrix at the same
14490 vpos we would use in the current matrix, i.e. below
14491 last_unchanged_at_beg_row. */
14492 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14493 current_matrix);
14494 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14495 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14496
14497 xassert (it.hpos == 0 && it.current_x == 0);
14498 }
14499 else
14500 {
14501 /* There are no reusable lines at the start of the window.
14502 Start displaying in the first text line. */
14503 start_display (&it, w, start);
14504 it.vpos = it.first_vpos;
14505 start_pos = it.current.pos;
14506 }
14507
14508 /* Find the first row that is not affected by changes at the end of
14509 the buffer. Value will be null if there is no unchanged row, in
14510 which case we must redisplay to the end of the window. delta
14511 will be set to the value by which buffer positions beginning with
14512 first_unchanged_at_end_row have to be adjusted due to text
14513 changes. */
14514 first_unchanged_at_end_row
14515 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14516 IF_DEBUG (debug_delta = delta);
14517 IF_DEBUG (debug_delta_bytes = delta_bytes);
14518
14519 /* Set stop_pos to the buffer position up to which we will have to
14520 display new lines. If first_unchanged_at_end_row != NULL, this
14521 is the buffer position of the start of the line displayed in that
14522 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14523 that we don't stop at a buffer position. */
14524 stop_pos = 0;
14525 if (first_unchanged_at_end_row)
14526 {
14527 xassert (last_unchanged_at_beg_row == NULL
14528 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14529
14530 /* If this is a continuation line, move forward to the next one
14531 that isn't. Changes in lines above affect this line.
14532 Caution: this may move first_unchanged_at_end_row to a row
14533 not displaying text. */
14534 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14535 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14536 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14537 < it.last_visible_y))
14538 ++first_unchanged_at_end_row;
14539
14540 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14541 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14542 >= it.last_visible_y))
14543 first_unchanged_at_end_row = NULL;
14544 else
14545 {
14546 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14547 + delta);
14548 first_unchanged_at_end_vpos
14549 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14550 xassert (stop_pos >= Z - END_UNCHANGED);
14551 }
14552 }
14553 else if (last_unchanged_at_beg_row == NULL)
14554 GIVE_UP (19);
14555
14556
14557 #if GLYPH_DEBUG
14558
14559 /* Either there is no unchanged row at the end, or the one we have
14560 now displays text. This is a necessary condition for the window
14561 end pos calculation at the end of this function. */
14562 xassert (first_unchanged_at_end_row == NULL
14563 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14564
14565 debug_last_unchanged_at_beg_vpos
14566 = (last_unchanged_at_beg_row
14567 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14568 : -1);
14569 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14570
14571 #endif /* GLYPH_DEBUG != 0 */
14572
14573
14574 /* Display new lines. Set last_text_row to the last new line
14575 displayed which has text on it, i.e. might end up as being the
14576 line where the window_end_vpos is. */
14577 w->cursor.vpos = -1;
14578 last_text_row = NULL;
14579 overlay_arrow_seen = 0;
14580 while (it.current_y < it.last_visible_y
14581 && !fonts_changed_p
14582 && (first_unchanged_at_end_row == NULL
14583 || IT_CHARPOS (it) < stop_pos))
14584 {
14585 if (display_line (&it))
14586 last_text_row = it.glyph_row - 1;
14587 }
14588
14589 if (fonts_changed_p)
14590 return -1;
14591
14592
14593 /* Compute differences in buffer positions, y-positions etc. for
14594 lines reused at the bottom of the window. Compute what we can
14595 scroll. */
14596 if (first_unchanged_at_end_row
14597 /* No lines reused because we displayed everything up to the
14598 bottom of the window. */
14599 && it.current_y < it.last_visible_y)
14600 {
14601 dvpos = (it.vpos
14602 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14603 current_matrix));
14604 dy = it.current_y - first_unchanged_at_end_row->y;
14605 run.current_y = first_unchanged_at_end_row->y;
14606 run.desired_y = run.current_y + dy;
14607 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14608 }
14609 else
14610 {
14611 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14612 first_unchanged_at_end_row = NULL;
14613 }
14614 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14615
14616
14617 /* Find the cursor if not already found. We have to decide whether
14618 PT will appear on this window (it sometimes doesn't, but this is
14619 not a very frequent case.) This decision has to be made before
14620 the current matrix is altered. A value of cursor.vpos < 0 means
14621 that PT is either in one of the lines beginning at
14622 first_unchanged_at_end_row or below the window. Don't care for
14623 lines that might be displayed later at the window end; as
14624 mentioned, this is not a frequent case. */
14625 if (w->cursor.vpos < 0)
14626 {
14627 /* Cursor in unchanged rows at the top? */
14628 if (PT < CHARPOS (start_pos)
14629 && last_unchanged_at_beg_row)
14630 {
14631 row = row_containing_pos (w, PT,
14632 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14633 last_unchanged_at_beg_row + 1, 0);
14634 if (row)
14635 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14636 }
14637
14638 /* Start from first_unchanged_at_end_row looking for PT. */
14639 else if (first_unchanged_at_end_row)
14640 {
14641 row = row_containing_pos (w, PT - delta,
14642 first_unchanged_at_end_row, NULL, 0);
14643 if (row)
14644 set_cursor_from_row (w, row, w->current_matrix, delta,
14645 delta_bytes, dy, dvpos);
14646 }
14647
14648 /* Give up if cursor was not found. */
14649 if (w->cursor.vpos < 0)
14650 {
14651 clear_glyph_matrix (w->desired_matrix);
14652 return -1;
14653 }
14654 }
14655
14656 /* Don't let the cursor end in the scroll margins. */
14657 {
14658 int this_scroll_margin, cursor_height;
14659
14660 this_scroll_margin = max (0, scroll_margin);
14661 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14662 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14663 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14664
14665 if ((w->cursor.y < this_scroll_margin
14666 && CHARPOS (start) > BEGV)
14667 /* Old redisplay didn't take scroll margin into account at the bottom,
14668 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14669 || (w->cursor.y + (make_cursor_line_fully_visible_p
14670 ? cursor_height + this_scroll_margin
14671 : 1)) > it.last_visible_y)
14672 {
14673 w->cursor.vpos = -1;
14674 clear_glyph_matrix (w->desired_matrix);
14675 return -1;
14676 }
14677 }
14678
14679 /* Scroll the display. Do it before changing the current matrix so
14680 that xterm.c doesn't get confused about where the cursor glyph is
14681 found. */
14682 if (dy && run.height)
14683 {
14684 update_begin (f);
14685
14686 if (FRAME_WINDOW_P (f))
14687 {
14688 rif->update_window_begin_hook (w);
14689 rif->clear_window_mouse_face (w);
14690 rif->scroll_run_hook (w, &run);
14691 rif->update_window_end_hook (w, 0, 0);
14692 }
14693 else
14694 {
14695 /* Terminal frame. In this case, dvpos gives the number of
14696 lines to scroll by; dvpos < 0 means scroll up. */
14697 int first_unchanged_at_end_vpos
14698 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14699 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14700 int end = (WINDOW_TOP_EDGE_LINE (w)
14701 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14702 + window_internal_height (w));
14703
14704 /* Perform the operation on the screen. */
14705 if (dvpos > 0)
14706 {
14707 /* Scroll last_unchanged_at_beg_row to the end of the
14708 window down dvpos lines. */
14709 set_terminal_window (end);
14710
14711 /* On dumb terminals delete dvpos lines at the end
14712 before inserting dvpos empty lines. */
14713 if (!scroll_region_ok)
14714 ins_del_lines (end - dvpos, -dvpos);
14715
14716 /* Insert dvpos empty lines in front of
14717 last_unchanged_at_beg_row. */
14718 ins_del_lines (from, dvpos);
14719 }
14720 else if (dvpos < 0)
14721 {
14722 /* Scroll up last_unchanged_at_beg_vpos to the end of
14723 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14724 set_terminal_window (end);
14725
14726 /* Delete dvpos lines in front of
14727 last_unchanged_at_beg_vpos. ins_del_lines will set
14728 the cursor to the given vpos and emit |dvpos| delete
14729 line sequences. */
14730 ins_del_lines (from + dvpos, dvpos);
14731
14732 /* On a dumb terminal insert dvpos empty lines at the
14733 end. */
14734 if (!scroll_region_ok)
14735 ins_del_lines (end + dvpos, -dvpos);
14736 }
14737
14738 set_terminal_window (0);
14739 }
14740
14741 update_end (f);
14742 }
14743
14744 /* Shift reused rows of the current matrix to the right position.
14745 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14746 text. */
14747 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14748 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14749 if (dvpos < 0)
14750 {
14751 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14752 bottom_vpos, dvpos);
14753 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14754 bottom_vpos, 0);
14755 }
14756 else if (dvpos > 0)
14757 {
14758 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14759 bottom_vpos, dvpos);
14760 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14761 first_unchanged_at_end_vpos + dvpos, 0);
14762 }
14763
14764 /* For frame-based redisplay, make sure that current frame and window
14765 matrix are in sync with respect to glyph memory. */
14766 if (!FRAME_WINDOW_P (f))
14767 sync_frame_with_window_matrix_rows (w);
14768
14769 /* Adjust buffer positions in reused rows. */
14770 if (delta)
14771 increment_matrix_positions (current_matrix,
14772 first_unchanged_at_end_vpos + dvpos,
14773 bottom_vpos, delta, delta_bytes);
14774
14775 /* Adjust Y positions. */
14776 if (dy)
14777 shift_glyph_matrix (w, current_matrix,
14778 first_unchanged_at_end_vpos + dvpos,
14779 bottom_vpos, dy);
14780
14781 if (first_unchanged_at_end_row)
14782 {
14783 first_unchanged_at_end_row += dvpos;
14784 if (first_unchanged_at_end_row->y >= it.last_visible_y
14785 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14786 first_unchanged_at_end_row = NULL;
14787 }
14788
14789 /* If scrolling up, there may be some lines to display at the end of
14790 the window. */
14791 last_text_row_at_end = NULL;
14792 if (dy < 0)
14793 {
14794 /* Scrolling up can leave for example a partially visible line
14795 at the end of the window to be redisplayed. */
14796 /* Set last_row to the glyph row in the current matrix where the
14797 window end line is found. It has been moved up or down in
14798 the matrix by dvpos. */
14799 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14800 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14801
14802 /* If last_row is the window end line, it should display text. */
14803 xassert (last_row->displays_text_p);
14804
14805 /* If window end line was partially visible before, begin
14806 displaying at that line. Otherwise begin displaying with the
14807 line following it. */
14808 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14809 {
14810 init_to_row_start (&it, w, last_row);
14811 it.vpos = last_vpos;
14812 it.current_y = last_row->y;
14813 }
14814 else
14815 {
14816 init_to_row_end (&it, w, last_row);
14817 it.vpos = 1 + last_vpos;
14818 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14819 ++last_row;
14820 }
14821
14822 /* We may start in a continuation line. If so, we have to
14823 get the right continuation_lines_width and current_x. */
14824 it.continuation_lines_width = last_row->continuation_lines_width;
14825 it.hpos = it.current_x = 0;
14826
14827 /* Display the rest of the lines at the window end. */
14828 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14829 while (it.current_y < it.last_visible_y
14830 && !fonts_changed_p)
14831 {
14832 /* Is it always sure that the display agrees with lines in
14833 the current matrix? I don't think so, so we mark rows
14834 displayed invalid in the current matrix by setting their
14835 enabled_p flag to zero. */
14836 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14837 if (display_line (&it))
14838 last_text_row_at_end = it.glyph_row - 1;
14839 }
14840 }
14841
14842 /* Update window_end_pos and window_end_vpos. */
14843 if (first_unchanged_at_end_row
14844 && !last_text_row_at_end)
14845 {
14846 /* Window end line if one of the preserved rows from the current
14847 matrix. Set row to the last row displaying text in current
14848 matrix starting at first_unchanged_at_end_row, after
14849 scrolling. */
14850 xassert (first_unchanged_at_end_row->displays_text_p);
14851 row = find_last_row_displaying_text (w->current_matrix, &it,
14852 first_unchanged_at_end_row);
14853 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14854
14855 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14856 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14857 w->window_end_vpos
14858 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14859 xassert (w->window_end_bytepos >= 0);
14860 IF_DEBUG (debug_method_add (w, "A"));
14861 }
14862 else if (last_text_row_at_end)
14863 {
14864 w->window_end_pos
14865 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14866 w->window_end_bytepos
14867 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14868 w->window_end_vpos
14869 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14870 xassert (w->window_end_bytepos >= 0);
14871 IF_DEBUG (debug_method_add (w, "B"));
14872 }
14873 else if (last_text_row)
14874 {
14875 /* We have displayed either to the end of the window or at the
14876 end of the window, i.e. the last row with text is to be found
14877 in the desired matrix. */
14878 w->window_end_pos
14879 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14880 w->window_end_bytepos
14881 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14882 w->window_end_vpos
14883 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14884 xassert (w->window_end_bytepos >= 0);
14885 }
14886 else if (first_unchanged_at_end_row == NULL
14887 && last_text_row == NULL
14888 && last_text_row_at_end == NULL)
14889 {
14890 /* Displayed to end of window, but no line containing text was
14891 displayed. Lines were deleted at the end of the window. */
14892 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14893 int vpos = XFASTINT (w->window_end_vpos);
14894 struct glyph_row *current_row = current_matrix->rows + vpos;
14895 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14896
14897 for (row = NULL;
14898 row == NULL && vpos >= first_vpos;
14899 --vpos, --current_row, --desired_row)
14900 {
14901 if (desired_row->enabled_p)
14902 {
14903 if (desired_row->displays_text_p)
14904 row = desired_row;
14905 }
14906 else if (current_row->displays_text_p)
14907 row = current_row;
14908 }
14909
14910 xassert (row != NULL);
14911 w->window_end_vpos = make_number (vpos + 1);
14912 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14913 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14914 xassert (w->window_end_bytepos >= 0);
14915 IF_DEBUG (debug_method_add (w, "C"));
14916 }
14917 else
14918 abort ();
14919
14920 #if 0 /* This leads to problems, for instance when the cursor is
14921 at ZV, and the cursor line displays no text. */
14922 /* Disable rows below what's displayed in the window. This makes
14923 debugging easier. */
14924 enable_glyph_matrix_rows (current_matrix,
14925 XFASTINT (w->window_end_vpos) + 1,
14926 bottom_vpos, 0);
14927 #endif
14928
14929 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14930 debug_end_vpos = XFASTINT (w->window_end_vpos));
14931
14932 /* Record that display has not been completed. */
14933 w->window_end_valid = Qnil;
14934 w->desired_matrix->no_scrolling_p = 1;
14935 return 3;
14936
14937 #undef GIVE_UP
14938 }
14939
14940
14941 \f
14942 /***********************************************************************
14943 More debugging support
14944 ***********************************************************************/
14945
14946 #if GLYPH_DEBUG
14947
14948 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14949 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14950 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14951
14952
14953 /* Dump the contents of glyph matrix MATRIX on stderr.
14954
14955 GLYPHS 0 means don't show glyph contents.
14956 GLYPHS 1 means show glyphs in short form
14957 GLYPHS > 1 means show glyphs in long form. */
14958
14959 void
14960 dump_glyph_matrix (matrix, glyphs)
14961 struct glyph_matrix *matrix;
14962 int glyphs;
14963 {
14964 int i;
14965 for (i = 0; i < matrix->nrows; ++i)
14966 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14967 }
14968
14969
14970 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14971 the glyph row and area where the glyph comes from. */
14972
14973 void
14974 dump_glyph (row, glyph, area)
14975 struct glyph_row *row;
14976 struct glyph *glyph;
14977 int area;
14978 {
14979 if (glyph->type == CHAR_GLYPH)
14980 {
14981 fprintf (stderr,
14982 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14983 glyph - row->glyphs[TEXT_AREA],
14984 'C',
14985 glyph->charpos,
14986 (BUFFERP (glyph->object)
14987 ? 'B'
14988 : (STRINGP (glyph->object)
14989 ? 'S'
14990 : '-')),
14991 glyph->pixel_width,
14992 glyph->u.ch,
14993 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14994 ? glyph->u.ch
14995 : '.'),
14996 glyph->face_id,
14997 glyph->left_box_line_p,
14998 glyph->right_box_line_p);
14999 }
15000 else if (glyph->type == STRETCH_GLYPH)
15001 {
15002 fprintf (stderr,
15003 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15004 glyph - row->glyphs[TEXT_AREA],
15005 'S',
15006 glyph->charpos,
15007 (BUFFERP (glyph->object)
15008 ? 'B'
15009 : (STRINGP (glyph->object)
15010 ? 'S'
15011 : '-')),
15012 glyph->pixel_width,
15013 0,
15014 '.',
15015 glyph->face_id,
15016 glyph->left_box_line_p,
15017 glyph->right_box_line_p);
15018 }
15019 else if (glyph->type == IMAGE_GLYPH)
15020 {
15021 fprintf (stderr,
15022 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15023 glyph - row->glyphs[TEXT_AREA],
15024 'I',
15025 glyph->charpos,
15026 (BUFFERP (glyph->object)
15027 ? 'B'
15028 : (STRINGP (glyph->object)
15029 ? 'S'
15030 : '-')),
15031 glyph->pixel_width,
15032 glyph->u.img_id,
15033 '.',
15034 glyph->face_id,
15035 glyph->left_box_line_p,
15036 glyph->right_box_line_p);
15037 }
15038 else if (glyph->type == COMPOSITE_GLYPH)
15039 {
15040 fprintf (stderr,
15041 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15042 glyph - row->glyphs[TEXT_AREA],
15043 '+',
15044 glyph->charpos,
15045 (BUFFERP (glyph->object)
15046 ? 'B'
15047 : (STRINGP (glyph->object)
15048 ? 'S'
15049 : '-')),
15050 glyph->pixel_width,
15051 glyph->u.cmp_id,
15052 '.',
15053 glyph->face_id,
15054 glyph->left_box_line_p,
15055 glyph->right_box_line_p);
15056 }
15057 }
15058
15059
15060 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15061 GLYPHS 0 means don't show glyph contents.
15062 GLYPHS 1 means show glyphs in short form
15063 GLYPHS > 1 means show glyphs in long form. */
15064
15065 void
15066 dump_glyph_row (row, vpos, glyphs)
15067 struct glyph_row *row;
15068 int vpos, glyphs;
15069 {
15070 if (glyphs != 1)
15071 {
15072 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15073 fprintf (stderr, "======================================================================\n");
15074
15075 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15076 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15077 vpos,
15078 MATRIX_ROW_START_CHARPOS (row),
15079 MATRIX_ROW_END_CHARPOS (row),
15080 row->used[TEXT_AREA],
15081 row->contains_overlapping_glyphs_p,
15082 row->enabled_p,
15083 row->truncated_on_left_p,
15084 row->truncated_on_right_p,
15085 row->continued_p,
15086 MATRIX_ROW_CONTINUATION_LINE_P (row),
15087 row->displays_text_p,
15088 row->ends_at_zv_p,
15089 row->fill_line_p,
15090 row->ends_in_middle_of_char_p,
15091 row->starts_in_middle_of_char_p,
15092 row->mouse_face_p,
15093 row->x,
15094 row->y,
15095 row->pixel_width,
15096 row->height,
15097 row->visible_height,
15098 row->ascent,
15099 row->phys_ascent);
15100 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15101 row->end.overlay_string_index,
15102 row->continuation_lines_width);
15103 fprintf (stderr, "%9d %5d\n",
15104 CHARPOS (row->start.string_pos),
15105 CHARPOS (row->end.string_pos));
15106 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15107 row->end.dpvec_index);
15108 }
15109
15110 if (glyphs > 1)
15111 {
15112 int area;
15113
15114 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15115 {
15116 struct glyph *glyph = row->glyphs[area];
15117 struct glyph *glyph_end = glyph + row->used[area];
15118
15119 /* Glyph for a line end in text. */
15120 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15121 ++glyph_end;
15122
15123 if (glyph < glyph_end)
15124 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15125
15126 for (; glyph < glyph_end; ++glyph)
15127 dump_glyph (row, glyph, area);
15128 }
15129 }
15130 else if (glyphs == 1)
15131 {
15132 int area;
15133
15134 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15135 {
15136 char *s = (char *) alloca (row->used[area] + 1);
15137 int i;
15138
15139 for (i = 0; i < row->used[area]; ++i)
15140 {
15141 struct glyph *glyph = row->glyphs[area] + i;
15142 if (glyph->type == CHAR_GLYPH
15143 && glyph->u.ch < 0x80
15144 && glyph->u.ch >= ' ')
15145 s[i] = glyph->u.ch;
15146 else
15147 s[i] = '.';
15148 }
15149
15150 s[i] = '\0';
15151 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15152 }
15153 }
15154 }
15155
15156
15157 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15158 Sdump_glyph_matrix, 0, 1, "p",
15159 doc: /* Dump the current matrix of the selected window to stderr.
15160 Shows contents of glyph row structures. With non-nil
15161 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15162 glyphs in short form, otherwise show glyphs in long form. */)
15163 (glyphs)
15164 Lisp_Object glyphs;
15165 {
15166 struct window *w = XWINDOW (selected_window);
15167 struct buffer *buffer = XBUFFER (w->buffer);
15168
15169 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15170 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15171 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15172 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15173 fprintf (stderr, "=============================================\n");
15174 dump_glyph_matrix (w->current_matrix,
15175 NILP (glyphs) ? 0 : XINT (glyphs));
15176 return Qnil;
15177 }
15178
15179
15180 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15181 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15182 ()
15183 {
15184 struct frame *f = XFRAME (selected_frame);
15185 dump_glyph_matrix (f->current_matrix, 1);
15186 return Qnil;
15187 }
15188
15189
15190 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15191 doc: /* Dump glyph row ROW to stderr.
15192 GLYPH 0 means don't dump glyphs.
15193 GLYPH 1 means dump glyphs in short form.
15194 GLYPH > 1 or omitted means dump glyphs in long form. */)
15195 (row, glyphs)
15196 Lisp_Object row, glyphs;
15197 {
15198 struct glyph_matrix *matrix;
15199 int vpos;
15200
15201 CHECK_NUMBER (row);
15202 matrix = XWINDOW (selected_window)->current_matrix;
15203 vpos = XINT (row);
15204 if (vpos >= 0 && vpos < matrix->nrows)
15205 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15206 vpos,
15207 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15208 return Qnil;
15209 }
15210
15211
15212 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15213 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15214 GLYPH 0 means don't dump glyphs.
15215 GLYPH 1 means dump glyphs in short form.
15216 GLYPH > 1 or omitted means dump glyphs in long form. */)
15217 (row, glyphs)
15218 Lisp_Object row, glyphs;
15219 {
15220 struct frame *sf = SELECTED_FRAME ();
15221 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15222 int vpos;
15223
15224 CHECK_NUMBER (row);
15225 vpos = XINT (row);
15226 if (vpos >= 0 && vpos < m->nrows)
15227 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15228 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15229 return Qnil;
15230 }
15231
15232
15233 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15234 doc: /* Toggle tracing of redisplay.
15235 With ARG, turn tracing on if and only if ARG is positive. */)
15236 (arg)
15237 Lisp_Object arg;
15238 {
15239 if (NILP (arg))
15240 trace_redisplay_p = !trace_redisplay_p;
15241 else
15242 {
15243 arg = Fprefix_numeric_value (arg);
15244 trace_redisplay_p = XINT (arg) > 0;
15245 }
15246
15247 return Qnil;
15248 }
15249
15250
15251 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15252 doc: /* Like `format', but print result to stderr.
15253 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15254 (nargs, args)
15255 int nargs;
15256 Lisp_Object *args;
15257 {
15258 Lisp_Object s = Fformat (nargs, args);
15259 fprintf (stderr, "%s", SDATA (s));
15260 return Qnil;
15261 }
15262
15263 #endif /* GLYPH_DEBUG */
15264
15265
15266 \f
15267 /***********************************************************************
15268 Building Desired Matrix Rows
15269 ***********************************************************************/
15270
15271 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15272 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15273
15274 static struct glyph_row *
15275 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15276 struct window *w;
15277 Lisp_Object overlay_arrow_string;
15278 {
15279 struct frame *f = XFRAME (WINDOW_FRAME (w));
15280 struct buffer *buffer = XBUFFER (w->buffer);
15281 struct buffer *old = current_buffer;
15282 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15283 int arrow_len = SCHARS (overlay_arrow_string);
15284 const unsigned char *arrow_end = arrow_string + arrow_len;
15285 const unsigned char *p;
15286 struct it it;
15287 int multibyte_p;
15288 int n_glyphs_before;
15289
15290 set_buffer_temp (buffer);
15291 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15292 it.glyph_row->used[TEXT_AREA] = 0;
15293 SET_TEXT_POS (it.position, 0, 0);
15294
15295 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15296 p = arrow_string;
15297 while (p < arrow_end)
15298 {
15299 Lisp_Object face, ilisp;
15300
15301 /* Get the next character. */
15302 if (multibyte_p)
15303 it.c = string_char_and_length (p, arrow_len, &it.len);
15304 else
15305 it.c = *p, it.len = 1;
15306 p += it.len;
15307
15308 /* Get its face. */
15309 ilisp = make_number (p - arrow_string);
15310 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15311 it.face_id = compute_char_face (f, it.c, face);
15312
15313 /* Compute its width, get its glyphs. */
15314 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15315 SET_TEXT_POS (it.position, -1, -1);
15316 PRODUCE_GLYPHS (&it);
15317
15318 /* If this character doesn't fit any more in the line, we have
15319 to remove some glyphs. */
15320 if (it.current_x > it.last_visible_x)
15321 {
15322 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15323 break;
15324 }
15325 }
15326
15327 set_buffer_temp (old);
15328 return it.glyph_row;
15329 }
15330
15331
15332 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15333 glyphs are only inserted for terminal frames since we can't really
15334 win with truncation glyphs when partially visible glyphs are
15335 involved. Which glyphs to insert is determined by
15336 produce_special_glyphs. */
15337
15338 static void
15339 insert_left_trunc_glyphs (it)
15340 struct it *it;
15341 {
15342 struct it truncate_it;
15343 struct glyph *from, *end, *to, *toend;
15344
15345 xassert (!FRAME_WINDOW_P (it->f));
15346
15347 /* Get the truncation glyphs. */
15348 truncate_it = *it;
15349 truncate_it.current_x = 0;
15350 truncate_it.face_id = DEFAULT_FACE_ID;
15351 truncate_it.glyph_row = &scratch_glyph_row;
15352 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15353 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15354 truncate_it.object = make_number (0);
15355 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15356
15357 /* Overwrite glyphs from IT with truncation glyphs. */
15358 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15359 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15360 to = it->glyph_row->glyphs[TEXT_AREA];
15361 toend = to + it->glyph_row->used[TEXT_AREA];
15362
15363 while (from < end)
15364 *to++ = *from++;
15365
15366 /* There may be padding glyphs left over. Overwrite them too. */
15367 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15368 {
15369 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15370 while (from < end)
15371 *to++ = *from++;
15372 }
15373
15374 if (to > toend)
15375 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15376 }
15377
15378
15379 /* Compute the pixel height and width of IT->glyph_row.
15380
15381 Most of the time, ascent and height of a display line will be equal
15382 to the max_ascent and max_height values of the display iterator
15383 structure. This is not the case if
15384
15385 1. We hit ZV without displaying anything. In this case, max_ascent
15386 and max_height will be zero.
15387
15388 2. We have some glyphs that don't contribute to the line height.
15389 (The glyph row flag contributes_to_line_height_p is for future
15390 pixmap extensions).
15391
15392 The first case is easily covered by using default values because in
15393 these cases, the line height does not really matter, except that it
15394 must not be zero. */
15395
15396 static void
15397 compute_line_metrics (it)
15398 struct it *it;
15399 {
15400 struct glyph_row *row = it->glyph_row;
15401 int area, i;
15402
15403 if (FRAME_WINDOW_P (it->f))
15404 {
15405 int i, min_y, max_y;
15406
15407 /* The line may consist of one space only, that was added to
15408 place the cursor on it. If so, the row's height hasn't been
15409 computed yet. */
15410 if (row->height == 0)
15411 {
15412 if (it->max_ascent + it->max_descent == 0)
15413 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15414 row->ascent = it->max_ascent;
15415 row->height = it->max_ascent + it->max_descent;
15416 row->phys_ascent = it->max_phys_ascent;
15417 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15418 row->extra_line_spacing = it->max_extra_line_spacing;
15419 }
15420
15421 /* Compute the width of this line. */
15422 row->pixel_width = row->x;
15423 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15424 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15425
15426 xassert (row->pixel_width >= 0);
15427 xassert (row->ascent >= 0 && row->height > 0);
15428
15429 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15430 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15431
15432 /* If first line's physical ascent is larger than its logical
15433 ascent, use the physical ascent, and make the row taller.
15434 This makes accented characters fully visible. */
15435 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15436 && row->phys_ascent > row->ascent)
15437 {
15438 row->height += row->phys_ascent - row->ascent;
15439 row->ascent = row->phys_ascent;
15440 }
15441
15442 /* Compute how much of the line is visible. */
15443 row->visible_height = row->height;
15444
15445 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15446 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15447
15448 if (row->y < min_y)
15449 row->visible_height -= min_y - row->y;
15450 if (row->y + row->height > max_y)
15451 row->visible_height -= row->y + row->height - max_y;
15452 }
15453 else
15454 {
15455 row->pixel_width = row->used[TEXT_AREA];
15456 if (row->continued_p)
15457 row->pixel_width -= it->continuation_pixel_width;
15458 else if (row->truncated_on_right_p)
15459 row->pixel_width -= it->truncation_pixel_width;
15460 row->ascent = row->phys_ascent = 0;
15461 row->height = row->phys_height = row->visible_height = 1;
15462 row->extra_line_spacing = 0;
15463 }
15464
15465 /* Compute a hash code for this row. */
15466 row->hash = 0;
15467 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15468 for (i = 0; i < row->used[area]; ++i)
15469 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15470 + row->glyphs[area][i].u.val
15471 + row->glyphs[area][i].face_id
15472 + row->glyphs[area][i].padding_p
15473 + (row->glyphs[area][i].type << 2));
15474
15475 it->max_ascent = it->max_descent = 0;
15476 it->max_phys_ascent = it->max_phys_descent = 0;
15477 }
15478
15479
15480 /* Append one space to the glyph row of iterator IT if doing a
15481 window-based redisplay. The space has the same face as
15482 IT->face_id. Value is non-zero if a space was added.
15483
15484 This function is called to make sure that there is always one glyph
15485 at the end of a glyph row that the cursor can be set on under
15486 window-systems. (If there weren't such a glyph we would not know
15487 how wide and tall a box cursor should be displayed).
15488
15489 At the same time this space let's a nicely handle clearing to the
15490 end of the line if the row ends in italic text. */
15491
15492 static int
15493 append_space_for_newline (it, default_face_p)
15494 struct it *it;
15495 int default_face_p;
15496 {
15497 if (FRAME_WINDOW_P (it->f))
15498 {
15499 int n = it->glyph_row->used[TEXT_AREA];
15500
15501 if (it->glyph_row->glyphs[TEXT_AREA] + n
15502 < it->glyph_row->glyphs[1 + TEXT_AREA])
15503 {
15504 /* Save some values that must not be changed.
15505 Must save IT->c and IT->len because otherwise
15506 ITERATOR_AT_END_P wouldn't work anymore after
15507 append_space_for_newline has been called. */
15508 enum display_element_type saved_what = it->what;
15509 int saved_c = it->c, saved_len = it->len;
15510 int saved_x = it->current_x;
15511 int saved_face_id = it->face_id;
15512 struct text_pos saved_pos;
15513 Lisp_Object saved_object;
15514 struct face *face;
15515
15516 saved_object = it->object;
15517 saved_pos = it->position;
15518
15519 it->what = IT_CHARACTER;
15520 bzero (&it->position, sizeof it->position);
15521 it->object = make_number (0);
15522 it->c = ' ';
15523 it->len = 1;
15524
15525 if (default_face_p)
15526 it->face_id = DEFAULT_FACE_ID;
15527 else if (it->face_before_selective_p)
15528 it->face_id = it->saved_face_id;
15529 face = FACE_FROM_ID (it->f, it->face_id);
15530 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15531
15532 PRODUCE_GLYPHS (it);
15533
15534 it->override_ascent = -1;
15535 it->constrain_row_ascent_descent_p = 0;
15536 it->current_x = saved_x;
15537 it->object = saved_object;
15538 it->position = saved_pos;
15539 it->what = saved_what;
15540 it->face_id = saved_face_id;
15541 it->len = saved_len;
15542 it->c = saved_c;
15543 return 1;
15544 }
15545 }
15546
15547 return 0;
15548 }
15549
15550
15551 /* Extend the face of the last glyph in the text area of IT->glyph_row
15552 to the end of the display line. Called from display_line.
15553 If the glyph row is empty, add a space glyph to it so that we
15554 know the face to draw. Set the glyph row flag fill_line_p. */
15555
15556 static void
15557 extend_face_to_end_of_line (it)
15558 struct it *it;
15559 {
15560 struct face *face;
15561 struct frame *f = it->f;
15562
15563 /* If line is already filled, do nothing. */
15564 if (it->current_x >= it->last_visible_x)
15565 return;
15566
15567 /* Face extension extends the background and box of IT->face_id
15568 to the end of the line. If the background equals the background
15569 of the frame, we don't have to do anything. */
15570 if (it->face_before_selective_p)
15571 face = FACE_FROM_ID (it->f, it->saved_face_id);
15572 else
15573 face = FACE_FROM_ID (f, it->face_id);
15574
15575 if (FRAME_WINDOW_P (f)
15576 && it->glyph_row->displays_text_p
15577 && face->box == FACE_NO_BOX
15578 && face->background == FRAME_BACKGROUND_PIXEL (f)
15579 && !face->stipple)
15580 return;
15581
15582 /* Set the glyph row flag indicating that the face of the last glyph
15583 in the text area has to be drawn to the end of the text area. */
15584 it->glyph_row->fill_line_p = 1;
15585
15586 /* If current character of IT is not ASCII, make sure we have the
15587 ASCII face. This will be automatically undone the next time
15588 get_next_display_element returns a multibyte character. Note
15589 that the character will always be single byte in unibyte text. */
15590 if (!SINGLE_BYTE_CHAR_P (it->c))
15591 {
15592 it->face_id = FACE_FOR_CHAR (f, face, 0);
15593 }
15594
15595 if (FRAME_WINDOW_P (f))
15596 {
15597 /* If the row is empty, add a space with the current face of IT,
15598 so that we know which face to draw. */
15599 if (it->glyph_row->used[TEXT_AREA] == 0)
15600 {
15601 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15602 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15603 it->glyph_row->used[TEXT_AREA] = 1;
15604 }
15605 }
15606 else
15607 {
15608 /* Save some values that must not be changed. */
15609 int saved_x = it->current_x;
15610 struct text_pos saved_pos;
15611 Lisp_Object saved_object;
15612 enum display_element_type saved_what = it->what;
15613 int saved_face_id = it->face_id;
15614
15615 saved_object = it->object;
15616 saved_pos = it->position;
15617
15618 it->what = IT_CHARACTER;
15619 bzero (&it->position, sizeof it->position);
15620 it->object = make_number (0);
15621 it->c = ' ';
15622 it->len = 1;
15623 it->face_id = face->id;
15624
15625 PRODUCE_GLYPHS (it);
15626
15627 while (it->current_x <= it->last_visible_x)
15628 PRODUCE_GLYPHS (it);
15629
15630 /* Don't count these blanks really. It would let us insert a left
15631 truncation glyph below and make us set the cursor on them, maybe. */
15632 it->current_x = saved_x;
15633 it->object = saved_object;
15634 it->position = saved_pos;
15635 it->what = saved_what;
15636 it->face_id = saved_face_id;
15637 }
15638 }
15639
15640
15641 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15642 trailing whitespace. */
15643
15644 static int
15645 trailing_whitespace_p (charpos)
15646 int charpos;
15647 {
15648 int bytepos = CHAR_TO_BYTE (charpos);
15649 int c = 0;
15650
15651 while (bytepos < ZV_BYTE
15652 && (c = FETCH_CHAR (bytepos),
15653 c == ' ' || c == '\t'))
15654 ++bytepos;
15655
15656 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15657 {
15658 if (bytepos != PT_BYTE)
15659 return 1;
15660 }
15661 return 0;
15662 }
15663
15664
15665 /* Highlight trailing whitespace, if any, in ROW. */
15666
15667 void
15668 highlight_trailing_whitespace (f, row)
15669 struct frame *f;
15670 struct glyph_row *row;
15671 {
15672 int used = row->used[TEXT_AREA];
15673
15674 if (used)
15675 {
15676 struct glyph *start = row->glyphs[TEXT_AREA];
15677 struct glyph *glyph = start + used - 1;
15678
15679 /* Skip over glyphs inserted to display the cursor at the
15680 end of a line, for extending the face of the last glyph
15681 to the end of the line on terminals, and for truncation
15682 and continuation glyphs. */
15683 while (glyph >= start
15684 && glyph->type == CHAR_GLYPH
15685 && INTEGERP (glyph->object))
15686 --glyph;
15687
15688 /* If last glyph is a space or stretch, and it's trailing
15689 whitespace, set the face of all trailing whitespace glyphs in
15690 IT->glyph_row to `trailing-whitespace'. */
15691 if (glyph >= start
15692 && BUFFERP (glyph->object)
15693 && (glyph->type == STRETCH_GLYPH
15694 || (glyph->type == CHAR_GLYPH
15695 && glyph->u.ch == ' '))
15696 && trailing_whitespace_p (glyph->charpos))
15697 {
15698 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15699 if (face_id < 0)
15700 return;
15701
15702 while (glyph >= start
15703 && BUFFERP (glyph->object)
15704 && (glyph->type == STRETCH_GLYPH
15705 || (glyph->type == CHAR_GLYPH
15706 && glyph->u.ch == ' ')))
15707 (glyph--)->face_id = face_id;
15708 }
15709 }
15710 }
15711
15712
15713 /* Value is non-zero if glyph row ROW in window W should be
15714 used to hold the cursor. */
15715
15716 static int
15717 cursor_row_p (w, row)
15718 struct window *w;
15719 struct glyph_row *row;
15720 {
15721 int cursor_row_p = 1;
15722
15723 if (PT == MATRIX_ROW_END_CHARPOS (row))
15724 {
15725 /* If the row ends with a newline from a string, we don't want
15726 the cursor there, but we still want it at the start of the
15727 string if the string starts in this row.
15728 If the row is continued it doesn't end in a newline. */
15729 if (CHARPOS (row->end.string_pos) >= 0)
15730 cursor_row_p = (row->continued_p
15731 || PT >= MATRIX_ROW_START_CHARPOS (row));
15732 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15733 {
15734 /* If the row ends in middle of a real character,
15735 and the line is continued, we want the cursor here.
15736 That's because MATRIX_ROW_END_CHARPOS would equal
15737 PT if PT is before the character. */
15738 if (!row->ends_in_ellipsis_p)
15739 cursor_row_p = row->continued_p;
15740 else
15741 /* If the row ends in an ellipsis, then
15742 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15743 We want that position to be displayed after the ellipsis. */
15744 cursor_row_p = 0;
15745 }
15746 /* If the row ends at ZV, display the cursor at the end of that
15747 row instead of at the start of the row below. */
15748 else if (row->ends_at_zv_p)
15749 cursor_row_p = 1;
15750 else
15751 cursor_row_p = 0;
15752 }
15753
15754 return cursor_row_p;
15755 }
15756
15757
15758 /* Construct the glyph row IT->glyph_row in the desired matrix of
15759 IT->w from text at the current position of IT. See dispextern.h
15760 for an overview of struct it. Value is non-zero if
15761 IT->glyph_row displays text, as opposed to a line displaying ZV
15762 only. */
15763
15764 static int
15765 display_line (it)
15766 struct it *it;
15767 {
15768 struct glyph_row *row = it->glyph_row;
15769 Lisp_Object overlay_arrow_string;
15770
15771 /* We always start displaying at hpos zero even if hscrolled. */
15772 xassert (it->hpos == 0 && it->current_x == 0);
15773
15774 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15775 >= it->w->desired_matrix->nrows)
15776 {
15777 it->w->nrows_scale_factor++;
15778 fonts_changed_p = 1;
15779 return 0;
15780 }
15781
15782 /* Is IT->w showing the region? */
15783 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15784
15785 /* Clear the result glyph row and enable it. */
15786 prepare_desired_row (row);
15787
15788 row->y = it->current_y;
15789 row->start = it->start;
15790 row->continuation_lines_width = it->continuation_lines_width;
15791 row->displays_text_p = 1;
15792 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15793 it->starts_in_middle_of_char_p = 0;
15794
15795 /* Arrange the overlays nicely for our purposes. Usually, we call
15796 display_line on only one line at a time, in which case this
15797 can't really hurt too much, or we call it on lines which appear
15798 one after another in the buffer, in which case all calls to
15799 recenter_overlay_lists but the first will be pretty cheap. */
15800 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15801
15802 /* Move over display elements that are not visible because we are
15803 hscrolled. This may stop at an x-position < IT->first_visible_x
15804 if the first glyph is partially visible or if we hit a line end. */
15805 if (it->current_x < it->first_visible_x)
15806 {
15807 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15808 MOVE_TO_POS | MOVE_TO_X);
15809 }
15810
15811 /* Get the initial row height. This is either the height of the
15812 text hscrolled, if there is any, or zero. */
15813 row->ascent = it->max_ascent;
15814 row->height = it->max_ascent + it->max_descent;
15815 row->phys_ascent = it->max_phys_ascent;
15816 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15817 row->extra_line_spacing = it->max_extra_line_spacing;
15818
15819 /* Loop generating characters. The loop is left with IT on the next
15820 character to display. */
15821 while (1)
15822 {
15823 int n_glyphs_before, hpos_before, x_before;
15824 int x, i, nglyphs;
15825 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15826
15827 /* Retrieve the next thing to display. Value is zero if end of
15828 buffer reached. */
15829 if (!get_next_display_element (it))
15830 {
15831 /* Maybe add a space at the end of this line that is used to
15832 display the cursor there under X. Set the charpos of the
15833 first glyph of blank lines not corresponding to any text
15834 to -1. */
15835 #ifdef HAVE_WINDOW_SYSTEM
15836 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15837 row->exact_window_width_line_p = 1;
15838 else
15839 #endif /* HAVE_WINDOW_SYSTEM */
15840 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15841 || row->used[TEXT_AREA] == 0)
15842 {
15843 row->glyphs[TEXT_AREA]->charpos = -1;
15844 row->displays_text_p = 0;
15845
15846 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15847 && (!MINI_WINDOW_P (it->w)
15848 || (minibuf_level && EQ (it->window, minibuf_window))))
15849 row->indicate_empty_line_p = 1;
15850 }
15851
15852 it->continuation_lines_width = 0;
15853 row->ends_at_zv_p = 1;
15854 break;
15855 }
15856
15857 /* Now, get the metrics of what we want to display. This also
15858 generates glyphs in `row' (which is IT->glyph_row). */
15859 n_glyphs_before = row->used[TEXT_AREA];
15860 x = it->current_x;
15861
15862 /* Remember the line height so far in case the next element doesn't
15863 fit on the line. */
15864 if (!it->truncate_lines_p)
15865 {
15866 ascent = it->max_ascent;
15867 descent = it->max_descent;
15868 phys_ascent = it->max_phys_ascent;
15869 phys_descent = it->max_phys_descent;
15870 }
15871
15872 PRODUCE_GLYPHS (it);
15873
15874 /* If this display element was in marginal areas, continue with
15875 the next one. */
15876 if (it->area != TEXT_AREA)
15877 {
15878 row->ascent = max (row->ascent, it->max_ascent);
15879 row->height = max (row->height, it->max_ascent + it->max_descent);
15880 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15881 row->phys_height = max (row->phys_height,
15882 it->max_phys_ascent + it->max_phys_descent);
15883 row->extra_line_spacing = max (row->extra_line_spacing,
15884 it->max_extra_line_spacing);
15885 set_iterator_to_next (it, 1);
15886 continue;
15887 }
15888
15889 /* Does the display element fit on the line? If we truncate
15890 lines, we should draw past the right edge of the window. If
15891 we don't truncate, we want to stop so that we can display the
15892 continuation glyph before the right margin. If lines are
15893 continued, there are two possible strategies for characters
15894 resulting in more than 1 glyph (e.g. tabs): Display as many
15895 glyphs as possible in this line and leave the rest for the
15896 continuation line, or display the whole element in the next
15897 line. Original redisplay did the former, so we do it also. */
15898 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15899 hpos_before = it->hpos;
15900 x_before = x;
15901
15902 if (/* Not a newline. */
15903 nglyphs > 0
15904 /* Glyphs produced fit entirely in the line. */
15905 && it->current_x < it->last_visible_x)
15906 {
15907 it->hpos += nglyphs;
15908 row->ascent = max (row->ascent, it->max_ascent);
15909 row->height = max (row->height, it->max_ascent + it->max_descent);
15910 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15911 row->phys_height = max (row->phys_height,
15912 it->max_phys_ascent + it->max_phys_descent);
15913 row->extra_line_spacing = max (row->extra_line_spacing,
15914 it->max_extra_line_spacing);
15915 if (it->current_x - it->pixel_width < it->first_visible_x)
15916 row->x = x - it->first_visible_x;
15917 }
15918 else
15919 {
15920 int new_x;
15921 struct glyph *glyph;
15922
15923 for (i = 0; i < nglyphs; ++i, x = new_x)
15924 {
15925 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15926 new_x = x + glyph->pixel_width;
15927
15928 if (/* Lines are continued. */
15929 !it->truncate_lines_p
15930 && (/* Glyph doesn't fit on the line. */
15931 new_x > it->last_visible_x
15932 /* Or it fits exactly on a window system frame. */
15933 || (new_x == it->last_visible_x
15934 && FRAME_WINDOW_P (it->f))))
15935 {
15936 /* End of a continued line. */
15937
15938 if (it->hpos == 0
15939 || (new_x == it->last_visible_x
15940 && FRAME_WINDOW_P (it->f)))
15941 {
15942 /* Current glyph is the only one on the line or
15943 fits exactly on the line. We must continue
15944 the line because we can't draw the cursor
15945 after the glyph. */
15946 row->continued_p = 1;
15947 it->current_x = new_x;
15948 it->continuation_lines_width += new_x;
15949 ++it->hpos;
15950 if (i == nglyphs - 1)
15951 {
15952 set_iterator_to_next (it, 1);
15953 #ifdef HAVE_WINDOW_SYSTEM
15954 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15955 {
15956 if (!get_next_display_element (it))
15957 {
15958 row->exact_window_width_line_p = 1;
15959 it->continuation_lines_width = 0;
15960 row->continued_p = 0;
15961 row->ends_at_zv_p = 1;
15962 }
15963 else if (ITERATOR_AT_END_OF_LINE_P (it))
15964 {
15965 row->continued_p = 0;
15966 row->exact_window_width_line_p = 1;
15967 }
15968 }
15969 #endif /* HAVE_WINDOW_SYSTEM */
15970 }
15971 }
15972 else if (CHAR_GLYPH_PADDING_P (*glyph)
15973 && !FRAME_WINDOW_P (it->f))
15974 {
15975 /* A padding glyph that doesn't fit on this line.
15976 This means the whole character doesn't fit
15977 on the line. */
15978 row->used[TEXT_AREA] = n_glyphs_before;
15979
15980 /* Fill the rest of the row with continuation
15981 glyphs like in 20.x. */
15982 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15983 < row->glyphs[1 + TEXT_AREA])
15984 produce_special_glyphs (it, IT_CONTINUATION);
15985
15986 row->continued_p = 1;
15987 it->current_x = x_before;
15988 it->continuation_lines_width += x_before;
15989
15990 /* Restore the height to what it was before the
15991 element not fitting on the line. */
15992 it->max_ascent = ascent;
15993 it->max_descent = descent;
15994 it->max_phys_ascent = phys_ascent;
15995 it->max_phys_descent = phys_descent;
15996 }
15997 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15998 {
15999 /* A TAB that extends past the right edge of the
16000 window. This produces a single glyph on
16001 window system frames. We leave the glyph in
16002 this row and let it fill the row, but don't
16003 consume the TAB. */
16004 it->continuation_lines_width += it->last_visible_x;
16005 row->ends_in_middle_of_char_p = 1;
16006 row->continued_p = 1;
16007 glyph->pixel_width = it->last_visible_x - x;
16008 it->starts_in_middle_of_char_p = 1;
16009 }
16010 else
16011 {
16012 /* Something other than a TAB that draws past
16013 the right edge of the window. Restore
16014 positions to values before the element. */
16015 row->used[TEXT_AREA] = n_glyphs_before + i;
16016
16017 /* Display continuation glyphs. */
16018 if (!FRAME_WINDOW_P (it->f))
16019 produce_special_glyphs (it, IT_CONTINUATION);
16020 row->continued_p = 1;
16021
16022 it->current_x = x_before;
16023 it->continuation_lines_width += x;
16024 extend_face_to_end_of_line (it);
16025
16026 if (nglyphs > 1 && i > 0)
16027 {
16028 row->ends_in_middle_of_char_p = 1;
16029 it->starts_in_middle_of_char_p = 1;
16030 }
16031
16032 /* Restore the height to what it was before the
16033 element not fitting on the line. */
16034 it->max_ascent = ascent;
16035 it->max_descent = descent;
16036 it->max_phys_ascent = phys_ascent;
16037 it->max_phys_descent = phys_descent;
16038 }
16039
16040 break;
16041 }
16042 else if (new_x > it->first_visible_x)
16043 {
16044 /* Increment number of glyphs actually displayed. */
16045 ++it->hpos;
16046
16047 if (x < it->first_visible_x)
16048 /* Glyph is partially visible, i.e. row starts at
16049 negative X position. */
16050 row->x = x - it->first_visible_x;
16051 }
16052 else
16053 {
16054 /* Glyph is completely off the left margin of the
16055 window. This should not happen because of the
16056 move_it_in_display_line at the start of this
16057 function, unless the text display area of the
16058 window is empty. */
16059 xassert (it->first_visible_x <= it->last_visible_x);
16060 }
16061 }
16062
16063 row->ascent = max (row->ascent, it->max_ascent);
16064 row->height = max (row->height, it->max_ascent + it->max_descent);
16065 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16066 row->phys_height = max (row->phys_height,
16067 it->max_phys_ascent + it->max_phys_descent);
16068 row->extra_line_spacing = max (row->extra_line_spacing,
16069 it->max_extra_line_spacing);
16070
16071 /* End of this display line if row is continued. */
16072 if (row->continued_p || row->ends_at_zv_p)
16073 break;
16074 }
16075
16076 at_end_of_line:
16077 /* Is this a line end? If yes, we're also done, after making
16078 sure that a non-default face is extended up to the right
16079 margin of the window. */
16080 if (ITERATOR_AT_END_OF_LINE_P (it))
16081 {
16082 int used_before = row->used[TEXT_AREA];
16083
16084 row->ends_in_newline_from_string_p = STRINGP (it->object);
16085
16086 #ifdef HAVE_WINDOW_SYSTEM
16087 /* Add a space at the end of the line that is used to
16088 display the cursor there. */
16089 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16090 append_space_for_newline (it, 0);
16091 #endif /* HAVE_WINDOW_SYSTEM */
16092
16093 /* Extend the face to the end of the line. */
16094 extend_face_to_end_of_line (it);
16095
16096 /* Make sure we have the position. */
16097 if (used_before == 0)
16098 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16099
16100 /* Consume the line end. This skips over invisible lines. */
16101 set_iterator_to_next (it, 1);
16102 it->continuation_lines_width = 0;
16103 break;
16104 }
16105
16106 /* Proceed with next display element. Note that this skips
16107 over lines invisible because of selective display. */
16108 set_iterator_to_next (it, 1);
16109
16110 /* If we truncate lines, we are done when the last displayed
16111 glyphs reach past the right margin of the window. */
16112 if (it->truncate_lines_p
16113 && (FRAME_WINDOW_P (it->f)
16114 ? (it->current_x >= it->last_visible_x)
16115 : (it->current_x > it->last_visible_x)))
16116 {
16117 /* Maybe add truncation glyphs. */
16118 if (!FRAME_WINDOW_P (it->f))
16119 {
16120 int i, n;
16121
16122 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16123 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16124 break;
16125
16126 for (n = row->used[TEXT_AREA]; i < n; ++i)
16127 {
16128 row->used[TEXT_AREA] = i;
16129 produce_special_glyphs (it, IT_TRUNCATION);
16130 }
16131 }
16132 #ifdef HAVE_WINDOW_SYSTEM
16133 else
16134 {
16135 /* Don't truncate if we can overflow newline into fringe. */
16136 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16137 {
16138 if (!get_next_display_element (it))
16139 {
16140 it->continuation_lines_width = 0;
16141 row->ends_at_zv_p = 1;
16142 row->exact_window_width_line_p = 1;
16143 break;
16144 }
16145 if (ITERATOR_AT_END_OF_LINE_P (it))
16146 {
16147 row->exact_window_width_line_p = 1;
16148 goto at_end_of_line;
16149 }
16150 }
16151 }
16152 #endif /* HAVE_WINDOW_SYSTEM */
16153
16154 row->truncated_on_right_p = 1;
16155 it->continuation_lines_width = 0;
16156 reseat_at_next_visible_line_start (it, 0);
16157 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16158 it->hpos = hpos_before;
16159 it->current_x = x_before;
16160 break;
16161 }
16162 }
16163
16164 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16165 at the left window margin. */
16166 if (it->first_visible_x
16167 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16168 {
16169 if (!FRAME_WINDOW_P (it->f))
16170 insert_left_trunc_glyphs (it);
16171 row->truncated_on_left_p = 1;
16172 }
16173
16174 /* If the start of this line is the overlay arrow-position, then
16175 mark this glyph row as the one containing the overlay arrow.
16176 This is clearly a mess with variable size fonts. It would be
16177 better to let it be displayed like cursors under X. */
16178 if ((row->displays_text_p || !overlay_arrow_seen)
16179 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16180 !NILP (overlay_arrow_string)))
16181 {
16182 /* Overlay arrow in window redisplay is a fringe bitmap. */
16183 if (STRINGP (overlay_arrow_string))
16184 {
16185 struct glyph_row *arrow_row
16186 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16187 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16188 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16189 struct glyph *p = row->glyphs[TEXT_AREA];
16190 struct glyph *p2, *end;
16191
16192 /* Copy the arrow glyphs. */
16193 while (glyph < arrow_end)
16194 *p++ = *glyph++;
16195
16196 /* Throw away padding glyphs. */
16197 p2 = p;
16198 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16199 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16200 ++p2;
16201 if (p2 > p)
16202 {
16203 while (p2 < end)
16204 *p++ = *p2++;
16205 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16206 }
16207 }
16208 else
16209 {
16210 xassert (INTEGERP (overlay_arrow_string));
16211 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16212 }
16213 overlay_arrow_seen = 1;
16214 }
16215
16216 /* Compute pixel dimensions of this line. */
16217 compute_line_metrics (it);
16218
16219 /* Remember the position at which this line ends. */
16220 row->end = it->current;
16221
16222 /* Record whether this row ends inside an ellipsis. */
16223 row->ends_in_ellipsis_p
16224 = (it->method == GET_FROM_DISPLAY_VECTOR
16225 && it->ellipsis_p);
16226
16227 /* Save fringe bitmaps in this row. */
16228 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16229 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16230 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16231 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16232
16233 it->left_user_fringe_bitmap = 0;
16234 it->left_user_fringe_face_id = 0;
16235 it->right_user_fringe_bitmap = 0;
16236 it->right_user_fringe_face_id = 0;
16237
16238 /* Maybe set the cursor. */
16239 if (it->w->cursor.vpos < 0
16240 && PT >= MATRIX_ROW_START_CHARPOS (row)
16241 && PT <= MATRIX_ROW_END_CHARPOS (row)
16242 && cursor_row_p (it->w, row))
16243 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16244
16245 /* Highlight trailing whitespace. */
16246 if (!NILP (Vshow_trailing_whitespace))
16247 highlight_trailing_whitespace (it->f, it->glyph_row);
16248
16249 /* Prepare for the next line. This line starts horizontally at (X
16250 HPOS) = (0 0). Vertical positions are incremented. As a
16251 convenience for the caller, IT->glyph_row is set to the next
16252 row to be used. */
16253 it->current_x = it->hpos = 0;
16254 it->current_y += row->height;
16255 ++it->vpos;
16256 ++it->glyph_row;
16257 it->start = it->current;
16258 return row->displays_text_p;
16259 }
16260
16261
16262 \f
16263 /***********************************************************************
16264 Menu Bar
16265 ***********************************************************************/
16266
16267 /* Redisplay the menu bar in the frame for window W.
16268
16269 The menu bar of X frames that don't have X toolkit support is
16270 displayed in a special window W->frame->menu_bar_window.
16271
16272 The menu bar of terminal frames is treated specially as far as
16273 glyph matrices are concerned. Menu bar lines are not part of
16274 windows, so the update is done directly on the frame matrix rows
16275 for the menu bar. */
16276
16277 static void
16278 display_menu_bar (w)
16279 struct window *w;
16280 {
16281 struct frame *f = XFRAME (WINDOW_FRAME (w));
16282 struct it it;
16283 Lisp_Object items;
16284 int i;
16285
16286 /* Don't do all this for graphical frames. */
16287 #ifdef HAVE_NTGUI
16288 if (!NILP (Vwindow_system))
16289 return;
16290 #endif
16291 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16292 if (FRAME_X_P (f))
16293 return;
16294 #endif
16295 #ifdef MAC_OS
16296 if (FRAME_MAC_P (f))
16297 return;
16298 #endif
16299
16300 #ifdef USE_X_TOOLKIT
16301 xassert (!FRAME_WINDOW_P (f));
16302 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16303 it.first_visible_x = 0;
16304 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16305 #else /* not USE_X_TOOLKIT */
16306 if (FRAME_WINDOW_P (f))
16307 {
16308 /* Menu bar lines are displayed in the desired matrix of the
16309 dummy window menu_bar_window. */
16310 struct window *menu_w;
16311 xassert (WINDOWP (f->menu_bar_window));
16312 menu_w = XWINDOW (f->menu_bar_window);
16313 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16314 MENU_FACE_ID);
16315 it.first_visible_x = 0;
16316 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16317 }
16318 else
16319 {
16320 /* This is a TTY frame, i.e. character hpos/vpos are used as
16321 pixel x/y. */
16322 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16323 MENU_FACE_ID);
16324 it.first_visible_x = 0;
16325 it.last_visible_x = FRAME_COLS (f);
16326 }
16327 #endif /* not USE_X_TOOLKIT */
16328
16329 if (! mode_line_inverse_video)
16330 /* Force the menu-bar to be displayed in the default face. */
16331 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16332
16333 /* Clear all rows of the menu bar. */
16334 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16335 {
16336 struct glyph_row *row = it.glyph_row + i;
16337 clear_glyph_row (row);
16338 row->enabled_p = 1;
16339 row->full_width_p = 1;
16340 }
16341
16342 /* Display all items of the menu bar. */
16343 items = FRAME_MENU_BAR_ITEMS (it.f);
16344 for (i = 0; i < XVECTOR (items)->size; i += 4)
16345 {
16346 Lisp_Object string;
16347
16348 /* Stop at nil string. */
16349 string = AREF (items, i + 1);
16350 if (NILP (string))
16351 break;
16352
16353 /* Remember where item was displayed. */
16354 AREF (items, i + 3) = make_number (it.hpos);
16355
16356 /* Display the item, pad with one space. */
16357 if (it.current_x < it.last_visible_x)
16358 display_string (NULL, string, Qnil, 0, 0, &it,
16359 SCHARS (string) + 1, 0, 0, -1);
16360 }
16361
16362 /* Fill out the line with spaces. */
16363 if (it.current_x < it.last_visible_x)
16364 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16365
16366 /* Compute the total height of the lines. */
16367 compute_line_metrics (&it);
16368 }
16369
16370
16371 \f
16372 /***********************************************************************
16373 Mode Line
16374 ***********************************************************************/
16375
16376 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16377 FORCE is non-zero, redisplay mode lines unconditionally.
16378 Otherwise, redisplay only mode lines that are garbaged. Value is
16379 the number of windows whose mode lines were redisplayed. */
16380
16381 static int
16382 redisplay_mode_lines (window, force)
16383 Lisp_Object window;
16384 int force;
16385 {
16386 int nwindows = 0;
16387
16388 while (!NILP (window))
16389 {
16390 struct window *w = XWINDOW (window);
16391
16392 if (WINDOWP (w->hchild))
16393 nwindows += redisplay_mode_lines (w->hchild, force);
16394 else if (WINDOWP (w->vchild))
16395 nwindows += redisplay_mode_lines (w->vchild, force);
16396 else if (force
16397 || FRAME_GARBAGED_P (XFRAME (w->frame))
16398 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16399 {
16400 struct text_pos lpoint;
16401 struct buffer *old = current_buffer;
16402
16403 /* Set the window's buffer for the mode line display. */
16404 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16405 set_buffer_internal_1 (XBUFFER (w->buffer));
16406
16407 /* Point refers normally to the selected window. For any
16408 other window, set up appropriate value. */
16409 if (!EQ (window, selected_window))
16410 {
16411 struct text_pos pt;
16412
16413 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16414 if (CHARPOS (pt) < BEGV)
16415 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16416 else if (CHARPOS (pt) > (ZV - 1))
16417 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16418 else
16419 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16420 }
16421
16422 /* Display mode lines. */
16423 clear_glyph_matrix (w->desired_matrix);
16424 if (display_mode_lines (w))
16425 {
16426 ++nwindows;
16427 w->must_be_updated_p = 1;
16428 }
16429
16430 /* Restore old settings. */
16431 set_buffer_internal_1 (old);
16432 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16433 }
16434
16435 window = w->next;
16436 }
16437
16438 return nwindows;
16439 }
16440
16441
16442 /* Display the mode and/or top line of window W. Value is the number
16443 of mode lines displayed. */
16444
16445 static int
16446 display_mode_lines (w)
16447 struct window *w;
16448 {
16449 Lisp_Object old_selected_window, old_selected_frame;
16450 int n = 0;
16451
16452 old_selected_frame = selected_frame;
16453 selected_frame = w->frame;
16454 old_selected_window = selected_window;
16455 XSETWINDOW (selected_window, w);
16456
16457 /* These will be set while the mode line specs are processed. */
16458 line_number_displayed = 0;
16459 w->column_number_displayed = Qnil;
16460
16461 if (WINDOW_WANTS_MODELINE_P (w))
16462 {
16463 struct window *sel_w = XWINDOW (old_selected_window);
16464
16465 /* Select mode line face based on the real selected window. */
16466 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16467 current_buffer->mode_line_format);
16468 ++n;
16469 }
16470
16471 if (WINDOW_WANTS_HEADER_LINE_P (w))
16472 {
16473 display_mode_line (w, HEADER_LINE_FACE_ID,
16474 current_buffer->header_line_format);
16475 ++n;
16476 }
16477
16478 selected_frame = old_selected_frame;
16479 selected_window = old_selected_window;
16480 return n;
16481 }
16482
16483
16484 /* Display mode or top line of window W. FACE_ID specifies which line
16485 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16486 FORMAT is the mode line format to display. Value is the pixel
16487 height of the mode line displayed. */
16488
16489 static int
16490 display_mode_line (w, face_id, format)
16491 struct window *w;
16492 enum face_id face_id;
16493 Lisp_Object format;
16494 {
16495 struct it it;
16496 struct face *face;
16497 int count = SPECPDL_INDEX ();
16498
16499 init_iterator (&it, w, -1, -1, NULL, face_id);
16500 prepare_desired_row (it.glyph_row);
16501
16502 it.glyph_row->mode_line_p = 1;
16503
16504 if (! mode_line_inverse_video)
16505 /* Force the mode-line to be displayed in the default face. */
16506 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16507
16508 record_unwind_protect (unwind_format_mode_line,
16509 format_mode_line_unwind_data (NULL, 0));
16510
16511 mode_line_target = MODE_LINE_DISPLAY;
16512
16513 /* Temporarily make frame's keyboard the current kboard so that
16514 kboard-local variables in the mode_line_format will get the right
16515 values. */
16516 push_frame_kboard (it.f);
16517 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16518 pop_frame_kboard ();
16519
16520 unbind_to (count, Qnil);
16521
16522 /* Fill up with spaces. */
16523 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16524
16525 compute_line_metrics (&it);
16526 it.glyph_row->full_width_p = 1;
16527 it.glyph_row->continued_p = 0;
16528 it.glyph_row->truncated_on_left_p = 0;
16529 it.glyph_row->truncated_on_right_p = 0;
16530
16531 /* Make a 3D mode-line have a shadow at its right end. */
16532 face = FACE_FROM_ID (it.f, face_id);
16533 extend_face_to_end_of_line (&it);
16534 if (face->box != FACE_NO_BOX)
16535 {
16536 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16537 + it.glyph_row->used[TEXT_AREA] - 1);
16538 last->right_box_line_p = 1;
16539 }
16540
16541 return it.glyph_row->height;
16542 }
16543
16544 /* Move element ELT in LIST to the front of LIST.
16545 Return the updated list. */
16546
16547 static Lisp_Object
16548 move_elt_to_front (elt, list)
16549 Lisp_Object elt, list;
16550 {
16551 register Lisp_Object tail, prev;
16552 register Lisp_Object tem;
16553
16554 tail = list;
16555 prev = Qnil;
16556 while (CONSP (tail))
16557 {
16558 tem = XCAR (tail);
16559
16560 if (EQ (elt, tem))
16561 {
16562 /* Splice out the link TAIL. */
16563 if (NILP (prev))
16564 list = XCDR (tail);
16565 else
16566 Fsetcdr (prev, XCDR (tail));
16567
16568 /* Now make it the first. */
16569 Fsetcdr (tail, list);
16570 return tail;
16571 }
16572 else
16573 prev = tail;
16574 tail = XCDR (tail);
16575 QUIT;
16576 }
16577
16578 /* Not found--return unchanged LIST. */
16579 return list;
16580 }
16581
16582 /* Contribute ELT to the mode line for window IT->w. How it
16583 translates into text depends on its data type.
16584
16585 IT describes the display environment in which we display, as usual.
16586
16587 DEPTH is the depth in recursion. It is used to prevent
16588 infinite recursion here.
16589
16590 FIELD_WIDTH is the number of characters the display of ELT should
16591 occupy in the mode line, and PRECISION is the maximum number of
16592 characters to display from ELT's representation. See
16593 display_string for details.
16594
16595 Returns the hpos of the end of the text generated by ELT.
16596
16597 PROPS is a property list to add to any string we encounter.
16598
16599 If RISKY is nonzero, remove (disregard) any properties in any string
16600 we encounter, and ignore :eval and :propertize.
16601
16602 The global variable `mode_line_target' determines whether the
16603 output is passed to `store_mode_line_noprop',
16604 `store_mode_line_string', or `display_string'. */
16605
16606 static int
16607 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16608 struct it *it;
16609 int depth;
16610 int field_width, precision;
16611 Lisp_Object elt, props;
16612 int risky;
16613 {
16614 int n = 0, field, prec;
16615 int literal = 0;
16616
16617 tail_recurse:
16618 if (depth > 100)
16619 elt = build_string ("*too-deep*");
16620
16621 depth++;
16622
16623 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16624 {
16625 case Lisp_String:
16626 {
16627 /* A string: output it and check for %-constructs within it. */
16628 unsigned char c;
16629 int offset = 0;
16630
16631 if (SCHARS (elt) > 0
16632 && (!NILP (props) || risky))
16633 {
16634 Lisp_Object oprops, aelt;
16635 oprops = Ftext_properties_at (make_number (0), elt);
16636
16637 /* If the starting string's properties are not what
16638 we want, translate the string. Also, if the string
16639 is risky, do that anyway. */
16640
16641 if (NILP (Fequal (props, oprops)) || risky)
16642 {
16643 /* If the starting string has properties,
16644 merge the specified ones onto the existing ones. */
16645 if (! NILP (oprops) && !risky)
16646 {
16647 Lisp_Object tem;
16648
16649 oprops = Fcopy_sequence (oprops);
16650 tem = props;
16651 while (CONSP (tem))
16652 {
16653 oprops = Fplist_put (oprops, XCAR (tem),
16654 XCAR (XCDR (tem)));
16655 tem = XCDR (XCDR (tem));
16656 }
16657 props = oprops;
16658 }
16659
16660 aelt = Fassoc (elt, mode_line_proptrans_alist);
16661 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16662 {
16663 /* AELT is what we want. Move it to the front
16664 without consing. */
16665 elt = XCAR (aelt);
16666 mode_line_proptrans_alist
16667 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16668 }
16669 else
16670 {
16671 Lisp_Object tem;
16672
16673 /* If AELT has the wrong props, it is useless.
16674 so get rid of it. */
16675 if (! NILP (aelt))
16676 mode_line_proptrans_alist
16677 = Fdelq (aelt, mode_line_proptrans_alist);
16678
16679 elt = Fcopy_sequence (elt);
16680 Fset_text_properties (make_number (0), Flength (elt),
16681 props, elt);
16682 /* Add this item to mode_line_proptrans_alist. */
16683 mode_line_proptrans_alist
16684 = Fcons (Fcons (elt, props),
16685 mode_line_proptrans_alist);
16686 /* Truncate mode_line_proptrans_alist
16687 to at most 50 elements. */
16688 tem = Fnthcdr (make_number (50),
16689 mode_line_proptrans_alist);
16690 if (! NILP (tem))
16691 XSETCDR (tem, Qnil);
16692 }
16693 }
16694 }
16695
16696 offset = 0;
16697
16698 if (literal)
16699 {
16700 prec = precision - n;
16701 switch (mode_line_target)
16702 {
16703 case MODE_LINE_NOPROP:
16704 case MODE_LINE_TITLE:
16705 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16706 break;
16707 case MODE_LINE_STRING:
16708 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16709 break;
16710 case MODE_LINE_DISPLAY:
16711 n += display_string (NULL, elt, Qnil, 0, 0, it,
16712 0, prec, 0, STRING_MULTIBYTE (elt));
16713 break;
16714 }
16715
16716 break;
16717 }
16718
16719 /* Handle the non-literal case. */
16720
16721 while ((precision <= 0 || n < precision)
16722 && SREF (elt, offset) != 0
16723 && (mode_line_target != MODE_LINE_DISPLAY
16724 || it->current_x < it->last_visible_x))
16725 {
16726 int last_offset = offset;
16727
16728 /* Advance to end of string or next format specifier. */
16729 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16730 ;
16731
16732 if (offset - 1 != last_offset)
16733 {
16734 int nchars, nbytes;
16735
16736 /* Output to end of string or up to '%'. Field width
16737 is length of string. Don't output more than
16738 PRECISION allows us. */
16739 offset--;
16740
16741 prec = c_string_width (SDATA (elt) + last_offset,
16742 offset - last_offset, precision - n,
16743 &nchars, &nbytes);
16744
16745 switch (mode_line_target)
16746 {
16747 case MODE_LINE_NOPROP:
16748 case MODE_LINE_TITLE:
16749 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16750 break;
16751 case MODE_LINE_STRING:
16752 {
16753 int bytepos = last_offset;
16754 int charpos = string_byte_to_char (elt, bytepos);
16755 int endpos = (precision <= 0
16756 ? string_byte_to_char (elt, offset)
16757 : charpos + nchars);
16758
16759 n += store_mode_line_string (NULL,
16760 Fsubstring (elt, make_number (charpos),
16761 make_number (endpos)),
16762 0, 0, 0, Qnil);
16763 }
16764 break;
16765 case MODE_LINE_DISPLAY:
16766 {
16767 int bytepos = last_offset;
16768 int charpos = string_byte_to_char (elt, bytepos);
16769
16770 if (precision <= 0)
16771 nchars = string_byte_to_char (elt, offset) - charpos;
16772 n += display_string (NULL, elt, Qnil, 0, charpos,
16773 it, 0, nchars, 0,
16774 STRING_MULTIBYTE (elt));
16775 }
16776 break;
16777 }
16778 }
16779 else /* c == '%' */
16780 {
16781 int percent_position = offset;
16782
16783 /* Get the specified minimum width. Zero means
16784 don't pad. */
16785 field = 0;
16786 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16787 field = field * 10 + c - '0';
16788
16789 /* Don't pad beyond the total padding allowed. */
16790 if (field_width - n > 0 && field > field_width - n)
16791 field = field_width - n;
16792
16793 /* Note that either PRECISION <= 0 or N < PRECISION. */
16794 prec = precision - n;
16795
16796 if (c == 'M')
16797 n += display_mode_element (it, depth, field, prec,
16798 Vglobal_mode_string, props,
16799 risky);
16800 else if (c != 0)
16801 {
16802 int multibyte;
16803 int bytepos, charpos;
16804 unsigned char *spec;
16805
16806 bytepos = percent_position;
16807 charpos = (STRING_MULTIBYTE (elt)
16808 ? string_byte_to_char (elt, bytepos)
16809 : bytepos);
16810
16811 spec
16812 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16813
16814 switch (mode_line_target)
16815 {
16816 case MODE_LINE_NOPROP:
16817 case MODE_LINE_TITLE:
16818 n += store_mode_line_noprop (spec, field, prec);
16819 break;
16820 case MODE_LINE_STRING:
16821 {
16822 int len = strlen (spec);
16823 Lisp_Object tem = make_string (spec, len);
16824 props = Ftext_properties_at (make_number (charpos), elt);
16825 /* Should only keep face property in props */
16826 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16827 }
16828 break;
16829 case MODE_LINE_DISPLAY:
16830 {
16831 int nglyphs_before, nwritten;
16832
16833 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16834 nwritten = display_string (spec, Qnil, elt,
16835 charpos, 0, it,
16836 field, prec, 0,
16837 multibyte);
16838
16839 /* Assign to the glyphs written above the
16840 string where the `%x' came from, position
16841 of the `%'. */
16842 if (nwritten > 0)
16843 {
16844 struct glyph *glyph
16845 = (it->glyph_row->glyphs[TEXT_AREA]
16846 + nglyphs_before);
16847 int i;
16848
16849 for (i = 0; i < nwritten; ++i)
16850 {
16851 glyph[i].object = elt;
16852 glyph[i].charpos = charpos;
16853 }
16854
16855 n += nwritten;
16856 }
16857 }
16858 break;
16859 }
16860 }
16861 else /* c == 0 */
16862 break;
16863 }
16864 }
16865 }
16866 break;
16867
16868 case Lisp_Symbol:
16869 /* A symbol: process the value of the symbol recursively
16870 as if it appeared here directly. Avoid error if symbol void.
16871 Special case: if value of symbol is a string, output the string
16872 literally. */
16873 {
16874 register Lisp_Object tem;
16875
16876 /* If the variable is not marked as risky to set
16877 then its contents are risky to use. */
16878 if (NILP (Fget (elt, Qrisky_local_variable)))
16879 risky = 1;
16880
16881 tem = Fboundp (elt);
16882 if (!NILP (tem))
16883 {
16884 tem = Fsymbol_value (elt);
16885 /* If value is a string, output that string literally:
16886 don't check for % within it. */
16887 if (STRINGP (tem))
16888 literal = 1;
16889
16890 if (!EQ (tem, elt))
16891 {
16892 /* Give up right away for nil or t. */
16893 elt = tem;
16894 goto tail_recurse;
16895 }
16896 }
16897 }
16898 break;
16899
16900 case Lisp_Cons:
16901 {
16902 register Lisp_Object car, tem;
16903
16904 /* A cons cell: five distinct cases.
16905 If first element is :eval or :propertize, do something special.
16906 If first element is a string or a cons, process all the elements
16907 and effectively concatenate them.
16908 If first element is a negative number, truncate displaying cdr to
16909 at most that many characters. If positive, pad (with spaces)
16910 to at least that many characters.
16911 If first element is a symbol, process the cadr or caddr recursively
16912 according to whether the symbol's value is non-nil or nil. */
16913 car = XCAR (elt);
16914 if (EQ (car, QCeval))
16915 {
16916 /* An element of the form (:eval FORM) means evaluate FORM
16917 and use the result as mode line elements. */
16918
16919 if (risky)
16920 break;
16921
16922 if (CONSP (XCDR (elt)))
16923 {
16924 Lisp_Object spec;
16925 spec = safe_eval (XCAR (XCDR (elt)));
16926 n += display_mode_element (it, depth, field_width - n,
16927 precision - n, spec, props,
16928 risky);
16929 }
16930 }
16931 else if (EQ (car, QCpropertize))
16932 {
16933 /* An element of the form (:propertize ELT PROPS...)
16934 means display ELT but applying properties PROPS. */
16935
16936 if (risky)
16937 break;
16938
16939 if (CONSP (XCDR (elt)))
16940 n += display_mode_element (it, depth, field_width - n,
16941 precision - n, XCAR (XCDR (elt)),
16942 XCDR (XCDR (elt)), risky);
16943 }
16944 else if (SYMBOLP (car))
16945 {
16946 tem = Fboundp (car);
16947 elt = XCDR (elt);
16948 if (!CONSP (elt))
16949 goto invalid;
16950 /* elt is now the cdr, and we know it is a cons cell.
16951 Use its car if CAR has a non-nil value. */
16952 if (!NILP (tem))
16953 {
16954 tem = Fsymbol_value (car);
16955 if (!NILP (tem))
16956 {
16957 elt = XCAR (elt);
16958 goto tail_recurse;
16959 }
16960 }
16961 /* Symbol's value is nil (or symbol is unbound)
16962 Get the cddr of the original list
16963 and if possible find the caddr and use that. */
16964 elt = XCDR (elt);
16965 if (NILP (elt))
16966 break;
16967 else if (!CONSP (elt))
16968 goto invalid;
16969 elt = XCAR (elt);
16970 goto tail_recurse;
16971 }
16972 else if (INTEGERP (car))
16973 {
16974 register int lim = XINT (car);
16975 elt = XCDR (elt);
16976 if (lim < 0)
16977 {
16978 /* Negative int means reduce maximum width. */
16979 if (precision <= 0)
16980 precision = -lim;
16981 else
16982 precision = min (precision, -lim);
16983 }
16984 else if (lim > 0)
16985 {
16986 /* Padding specified. Don't let it be more than
16987 current maximum. */
16988 if (precision > 0)
16989 lim = min (precision, lim);
16990
16991 /* If that's more padding than already wanted, queue it.
16992 But don't reduce padding already specified even if
16993 that is beyond the current truncation point. */
16994 field_width = max (lim, field_width);
16995 }
16996 goto tail_recurse;
16997 }
16998 else if (STRINGP (car) || CONSP (car))
16999 {
17000 register int limit = 50;
17001 /* Limit is to protect against circular lists. */
17002 while (CONSP (elt)
17003 && --limit > 0
17004 && (precision <= 0 || n < precision))
17005 {
17006 n += display_mode_element (it, depth,
17007 /* Do padding only after the last
17008 element in the list. */
17009 (! CONSP (XCDR (elt))
17010 ? field_width - n
17011 : 0),
17012 precision - n, XCAR (elt),
17013 props, risky);
17014 elt = XCDR (elt);
17015 }
17016 }
17017 }
17018 break;
17019
17020 default:
17021 invalid:
17022 elt = build_string ("*invalid*");
17023 goto tail_recurse;
17024 }
17025
17026 /* Pad to FIELD_WIDTH. */
17027 if (field_width > 0 && n < field_width)
17028 {
17029 switch (mode_line_target)
17030 {
17031 case MODE_LINE_NOPROP:
17032 case MODE_LINE_TITLE:
17033 n += store_mode_line_noprop ("", field_width - n, 0);
17034 break;
17035 case MODE_LINE_STRING:
17036 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17037 break;
17038 case MODE_LINE_DISPLAY:
17039 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17040 0, 0, 0);
17041 break;
17042 }
17043 }
17044
17045 return n;
17046 }
17047
17048 /* Store a mode-line string element in mode_line_string_list.
17049
17050 If STRING is non-null, display that C string. Otherwise, the Lisp
17051 string LISP_STRING is displayed.
17052
17053 FIELD_WIDTH is the minimum number of output glyphs to produce.
17054 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17055 with spaces. FIELD_WIDTH <= 0 means don't pad.
17056
17057 PRECISION is the maximum number of characters to output from
17058 STRING. PRECISION <= 0 means don't truncate the string.
17059
17060 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17061 properties to the string.
17062
17063 PROPS are the properties to add to the string.
17064 The mode_line_string_face face property is always added to the string.
17065 */
17066
17067 static int
17068 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17069 char *string;
17070 Lisp_Object lisp_string;
17071 int copy_string;
17072 int field_width;
17073 int precision;
17074 Lisp_Object props;
17075 {
17076 int len;
17077 int n = 0;
17078
17079 if (string != NULL)
17080 {
17081 len = strlen (string);
17082 if (precision > 0 && len > precision)
17083 len = precision;
17084 lisp_string = make_string (string, len);
17085 if (NILP (props))
17086 props = mode_line_string_face_prop;
17087 else if (!NILP (mode_line_string_face))
17088 {
17089 Lisp_Object face = Fplist_get (props, Qface);
17090 props = Fcopy_sequence (props);
17091 if (NILP (face))
17092 face = mode_line_string_face;
17093 else
17094 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17095 props = Fplist_put (props, Qface, face);
17096 }
17097 Fadd_text_properties (make_number (0), make_number (len),
17098 props, lisp_string);
17099 }
17100 else
17101 {
17102 len = XFASTINT (Flength (lisp_string));
17103 if (precision > 0 && len > precision)
17104 {
17105 len = precision;
17106 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17107 precision = -1;
17108 }
17109 if (!NILP (mode_line_string_face))
17110 {
17111 Lisp_Object face;
17112 if (NILP (props))
17113 props = Ftext_properties_at (make_number (0), lisp_string);
17114 face = Fplist_get (props, Qface);
17115 if (NILP (face))
17116 face = mode_line_string_face;
17117 else
17118 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17119 props = Fcons (Qface, Fcons (face, Qnil));
17120 if (copy_string)
17121 lisp_string = Fcopy_sequence (lisp_string);
17122 }
17123 if (!NILP (props))
17124 Fadd_text_properties (make_number (0), make_number (len),
17125 props, lisp_string);
17126 }
17127
17128 if (len > 0)
17129 {
17130 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17131 n += len;
17132 }
17133
17134 if (field_width > len)
17135 {
17136 field_width -= len;
17137 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17138 if (!NILP (props))
17139 Fadd_text_properties (make_number (0), make_number (field_width),
17140 props, lisp_string);
17141 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17142 n += field_width;
17143 }
17144
17145 return n;
17146 }
17147
17148
17149 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17150 1, 4, 0,
17151 doc: /* Format a string out of a mode line format specification.
17152 First arg FORMAT specifies the mode line format (see `mode-line-format'
17153 for details) to use.
17154
17155 Optional second arg FACE specifies the face property to put
17156 on all characters for which no face is specified.
17157 t means whatever face the window's mode line currently uses
17158 \(either `mode-line' or `mode-line-inactive', depending).
17159 nil means the default is no face property.
17160 If FACE is an integer, the value string has no text properties.
17161
17162 Optional third and fourth args WINDOW and BUFFER specify the window
17163 and buffer to use as the context for the formatting (defaults
17164 are the selected window and the window's buffer). */)
17165 (format, face, window, buffer)
17166 Lisp_Object format, face, window, buffer;
17167 {
17168 struct it it;
17169 int len;
17170 struct window *w;
17171 struct buffer *old_buffer = NULL;
17172 int face_id = -1;
17173 int no_props = INTEGERP (face);
17174 int count = SPECPDL_INDEX ();
17175 Lisp_Object str;
17176 int string_start = 0;
17177
17178 if (NILP (window))
17179 window = selected_window;
17180 CHECK_WINDOW (window);
17181 w = XWINDOW (window);
17182
17183 if (NILP (buffer))
17184 buffer = w->buffer;
17185 CHECK_BUFFER (buffer);
17186
17187 if (NILP (format))
17188 return build_string ("");
17189
17190 if (no_props)
17191 face = Qnil;
17192
17193 if (!NILP (face))
17194 {
17195 if (EQ (face, Qt))
17196 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17197 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17198 }
17199
17200 if (face_id < 0)
17201 face_id = DEFAULT_FACE_ID;
17202
17203 if (XBUFFER (buffer) != current_buffer)
17204 old_buffer = current_buffer;
17205
17206 /* Save things including mode_line_proptrans_alist,
17207 and set that to nil so that we don't alter the outer value. */
17208 record_unwind_protect (unwind_format_mode_line,
17209 format_mode_line_unwind_data (old_buffer, 1));
17210 mode_line_proptrans_alist = Qnil;
17211
17212 if (old_buffer)
17213 set_buffer_internal_1 (XBUFFER (buffer));
17214
17215 init_iterator (&it, w, -1, -1, NULL, face_id);
17216
17217 if (no_props)
17218 {
17219 mode_line_target = MODE_LINE_NOPROP;
17220 mode_line_string_face_prop = Qnil;
17221 mode_line_string_list = Qnil;
17222 string_start = MODE_LINE_NOPROP_LEN (0);
17223 }
17224 else
17225 {
17226 mode_line_target = MODE_LINE_STRING;
17227 mode_line_string_list = Qnil;
17228 mode_line_string_face = face;
17229 mode_line_string_face_prop
17230 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17231 }
17232
17233 push_frame_kboard (it.f);
17234 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17235 pop_frame_kboard ();
17236
17237 if (no_props)
17238 {
17239 len = MODE_LINE_NOPROP_LEN (string_start);
17240 str = make_string (mode_line_noprop_buf + string_start, len);
17241 }
17242 else
17243 {
17244 mode_line_string_list = Fnreverse (mode_line_string_list);
17245 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17246 make_string ("", 0));
17247 }
17248
17249 unbind_to (count, Qnil);
17250 return str;
17251 }
17252
17253 /* Write a null-terminated, right justified decimal representation of
17254 the positive integer D to BUF using a minimal field width WIDTH. */
17255
17256 static void
17257 pint2str (buf, width, d)
17258 register char *buf;
17259 register int width;
17260 register int d;
17261 {
17262 register char *p = buf;
17263
17264 if (d <= 0)
17265 *p++ = '0';
17266 else
17267 {
17268 while (d > 0)
17269 {
17270 *p++ = d % 10 + '0';
17271 d /= 10;
17272 }
17273 }
17274
17275 for (width -= (int) (p - buf); width > 0; --width)
17276 *p++ = ' ';
17277 *p-- = '\0';
17278 while (p > buf)
17279 {
17280 d = *buf;
17281 *buf++ = *p;
17282 *p-- = d;
17283 }
17284 }
17285
17286 /* Write a null-terminated, right justified decimal and "human
17287 readable" representation of the nonnegative integer D to BUF using
17288 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17289
17290 static const char power_letter[] =
17291 {
17292 0, /* not used */
17293 'k', /* kilo */
17294 'M', /* mega */
17295 'G', /* giga */
17296 'T', /* tera */
17297 'P', /* peta */
17298 'E', /* exa */
17299 'Z', /* zetta */
17300 'Y' /* yotta */
17301 };
17302
17303 static void
17304 pint2hrstr (buf, width, d)
17305 char *buf;
17306 int width;
17307 int d;
17308 {
17309 /* We aim to represent the nonnegative integer D as
17310 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17311 int quotient = d;
17312 int remainder = 0;
17313 /* -1 means: do not use TENTHS. */
17314 int tenths = -1;
17315 int exponent = 0;
17316
17317 /* Length of QUOTIENT.TENTHS as a string. */
17318 int length;
17319
17320 char * psuffix;
17321 char * p;
17322
17323 if (1000 <= quotient)
17324 {
17325 /* Scale to the appropriate EXPONENT. */
17326 do
17327 {
17328 remainder = quotient % 1000;
17329 quotient /= 1000;
17330 exponent++;
17331 }
17332 while (1000 <= quotient);
17333
17334 /* Round to nearest and decide whether to use TENTHS or not. */
17335 if (quotient <= 9)
17336 {
17337 tenths = remainder / 100;
17338 if (50 <= remainder % 100)
17339 {
17340 if (tenths < 9)
17341 tenths++;
17342 else
17343 {
17344 quotient++;
17345 if (quotient == 10)
17346 tenths = -1;
17347 else
17348 tenths = 0;
17349 }
17350 }
17351 }
17352 else
17353 if (500 <= remainder)
17354 {
17355 if (quotient < 999)
17356 quotient++;
17357 else
17358 {
17359 quotient = 1;
17360 exponent++;
17361 tenths = 0;
17362 }
17363 }
17364 }
17365
17366 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17367 if (tenths == -1 && quotient <= 99)
17368 if (quotient <= 9)
17369 length = 1;
17370 else
17371 length = 2;
17372 else
17373 length = 3;
17374 p = psuffix = buf + max (width, length);
17375
17376 /* Print EXPONENT. */
17377 if (exponent)
17378 *psuffix++ = power_letter[exponent];
17379 *psuffix = '\0';
17380
17381 /* Print TENTHS. */
17382 if (tenths >= 0)
17383 {
17384 *--p = '0' + tenths;
17385 *--p = '.';
17386 }
17387
17388 /* Print QUOTIENT. */
17389 do
17390 {
17391 int digit = quotient % 10;
17392 *--p = '0' + digit;
17393 }
17394 while ((quotient /= 10) != 0);
17395
17396 /* Print leading spaces. */
17397 while (buf < p)
17398 *--p = ' ';
17399 }
17400
17401 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17402 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17403 type of CODING_SYSTEM. Return updated pointer into BUF. */
17404
17405 static unsigned char invalid_eol_type[] = "(*invalid*)";
17406
17407 static char *
17408 decode_mode_spec_coding (coding_system, buf, eol_flag)
17409 Lisp_Object coding_system;
17410 register char *buf;
17411 int eol_flag;
17412 {
17413 Lisp_Object val;
17414 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17415 const unsigned char *eol_str;
17416 int eol_str_len;
17417 /* The EOL conversion we are using. */
17418 Lisp_Object eoltype;
17419
17420 val = Fget (coding_system, Qcoding_system);
17421 eoltype = Qnil;
17422
17423 if (!VECTORP (val)) /* Not yet decided. */
17424 {
17425 if (multibyte)
17426 *buf++ = '-';
17427 if (eol_flag)
17428 eoltype = eol_mnemonic_undecided;
17429 /* Don't mention EOL conversion if it isn't decided. */
17430 }
17431 else
17432 {
17433 Lisp_Object eolvalue;
17434
17435 eolvalue = Fget (coding_system, Qeol_type);
17436
17437 if (multibyte)
17438 *buf++ = XFASTINT (AREF (val, 1));
17439
17440 if (eol_flag)
17441 {
17442 /* The EOL conversion that is normal on this system. */
17443
17444 if (NILP (eolvalue)) /* Not yet decided. */
17445 eoltype = eol_mnemonic_undecided;
17446 else if (VECTORP (eolvalue)) /* Not yet decided. */
17447 eoltype = eol_mnemonic_undecided;
17448 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17449 eoltype = (XFASTINT (eolvalue) == 0
17450 ? eol_mnemonic_unix
17451 : (XFASTINT (eolvalue) == 1
17452 ? eol_mnemonic_dos : eol_mnemonic_mac));
17453 }
17454 }
17455
17456 if (eol_flag)
17457 {
17458 /* Mention the EOL conversion if it is not the usual one. */
17459 if (STRINGP (eoltype))
17460 {
17461 eol_str = SDATA (eoltype);
17462 eol_str_len = SBYTES (eoltype);
17463 }
17464 else if (INTEGERP (eoltype)
17465 && CHAR_VALID_P (XINT (eoltype), 0))
17466 {
17467 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17468 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17469 eol_str = tmp;
17470 }
17471 else
17472 {
17473 eol_str = invalid_eol_type;
17474 eol_str_len = sizeof (invalid_eol_type) - 1;
17475 }
17476 bcopy (eol_str, buf, eol_str_len);
17477 buf += eol_str_len;
17478 }
17479
17480 return buf;
17481 }
17482
17483 /* Return a string for the output of a mode line %-spec for window W,
17484 generated by character C. PRECISION >= 0 means don't return a
17485 string longer than that value. FIELD_WIDTH > 0 means pad the
17486 string returned with spaces to that value. Return 1 in *MULTIBYTE
17487 if the result is multibyte text.
17488
17489 Note we operate on the current buffer for most purposes,
17490 the exception being w->base_line_pos. */
17491
17492 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17493
17494 static char *
17495 decode_mode_spec (w, c, field_width, precision, multibyte)
17496 struct window *w;
17497 register int c;
17498 int field_width, precision;
17499 int *multibyte;
17500 {
17501 Lisp_Object obj;
17502 struct frame *f = XFRAME (WINDOW_FRAME (w));
17503 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17504 struct buffer *b = current_buffer;
17505
17506 obj = Qnil;
17507 *multibyte = 0;
17508
17509 switch (c)
17510 {
17511 case '*':
17512 if (!NILP (b->read_only))
17513 return "%";
17514 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17515 return "*";
17516 return "-";
17517
17518 case '+':
17519 /* This differs from %* only for a modified read-only buffer. */
17520 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17521 return "*";
17522 if (!NILP (b->read_only))
17523 return "%";
17524 return "-";
17525
17526 case '&':
17527 /* This differs from %* in ignoring read-only-ness. */
17528 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17529 return "*";
17530 return "-";
17531
17532 case '%':
17533 return "%";
17534
17535 case '[':
17536 {
17537 int i;
17538 char *p;
17539
17540 if (command_loop_level > 5)
17541 return "[[[... ";
17542 p = decode_mode_spec_buf;
17543 for (i = 0; i < command_loop_level; i++)
17544 *p++ = '[';
17545 *p = 0;
17546 return decode_mode_spec_buf;
17547 }
17548
17549 case ']':
17550 {
17551 int i;
17552 char *p;
17553
17554 if (command_loop_level > 5)
17555 return " ...]]]";
17556 p = decode_mode_spec_buf;
17557 for (i = 0; i < command_loop_level; i++)
17558 *p++ = ']';
17559 *p = 0;
17560 return decode_mode_spec_buf;
17561 }
17562
17563 case '-':
17564 {
17565 register int i;
17566
17567 /* Let lots_of_dashes be a string of infinite length. */
17568 if (mode_line_target == MODE_LINE_NOPROP ||
17569 mode_line_target == MODE_LINE_STRING)
17570 return "--";
17571 if (field_width <= 0
17572 || field_width > sizeof (lots_of_dashes))
17573 {
17574 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17575 decode_mode_spec_buf[i] = '-';
17576 decode_mode_spec_buf[i] = '\0';
17577 return decode_mode_spec_buf;
17578 }
17579 else
17580 return lots_of_dashes;
17581 }
17582
17583 case 'b':
17584 obj = b->name;
17585 break;
17586
17587 case 'c':
17588 {
17589 int col = (int) current_column (); /* iftc */
17590 w->column_number_displayed = make_number (col);
17591 pint2str (decode_mode_spec_buf, field_width, col);
17592 return decode_mode_spec_buf;
17593 }
17594
17595 case 'e':
17596 #ifndef SYSTEM_MALLOC
17597 {
17598 if (NILP (Vmemory_full))
17599 return "";
17600 else
17601 return "!MEM FULL! ";
17602 }
17603 #else
17604 return "";
17605 #endif
17606
17607 case 'F':
17608 /* %F displays the frame name. */
17609 if (!NILP (f->title))
17610 return (char *) SDATA (f->title);
17611 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17612 return (char *) SDATA (f->name);
17613 return "Emacs";
17614
17615 case 'f':
17616 obj = b->filename;
17617 break;
17618
17619 case 'i':
17620 {
17621 int size = ZV - BEGV;
17622 pint2str (decode_mode_spec_buf, field_width, size);
17623 return decode_mode_spec_buf;
17624 }
17625
17626 case 'I':
17627 {
17628 int size = ZV - BEGV;
17629 pint2hrstr (decode_mode_spec_buf, field_width, size);
17630 return decode_mode_spec_buf;
17631 }
17632
17633 case 'l':
17634 {
17635 int startpos = XMARKER (w->start)->charpos;
17636 int startpos_byte = marker_byte_position (w->start);
17637 int line, linepos, linepos_byte, topline;
17638 int nlines, junk;
17639 int height = WINDOW_TOTAL_LINES (w);
17640
17641 /* If we decided that this buffer isn't suitable for line numbers,
17642 don't forget that too fast. */
17643 if (EQ (w->base_line_pos, w->buffer))
17644 goto no_value;
17645 /* But do forget it, if the window shows a different buffer now. */
17646 else if (BUFFERP (w->base_line_pos))
17647 w->base_line_pos = Qnil;
17648
17649 /* If the buffer is very big, don't waste time. */
17650 if (INTEGERP (Vline_number_display_limit)
17651 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17652 {
17653 w->base_line_pos = Qnil;
17654 w->base_line_number = Qnil;
17655 goto no_value;
17656 }
17657
17658 if (!NILP (w->base_line_number)
17659 && !NILP (w->base_line_pos)
17660 && XFASTINT (w->base_line_pos) <= startpos)
17661 {
17662 line = XFASTINT (w->base_line_number);
17663 linepos = XFASTINT (w->base_line_pos);
17664 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17665 }
17666 else
17667 {
17668 line = 1;
17669 linepos = BUF_BEGV (b);
17670 linepos_byte = BUF_BEGV_BYTE (b);
17671 }
17672
17673 /* Count lines from base line to window start position. */
17674 nlines = display_count_lines (linepos, linepos_byte,
17675 startpos_byte,
17676 startpos, &junk);
17677
17678 topline = nlines + line;
17679
17680 /* Determine a new base line, if the old one is too close
17681 or too far away, or if we did not have one.
17682 "Too close" means it's plausible a scroll-down would
17683 go back past it. */
17684 if (startpos == BUF_BEGV (b))
17685 {
17686 w->base_line_number = make_number (topline);
17687 w->base_line_pos = make_number (BUF_BEGV (b));
17688 }
17689 else if (nlines < height + 25 || nlines > height * 3 + 50
17690 || linepos == BUF_BEGV (b))
17691 {
17692 int limit = BUF_BEGV (b);
17693 int limit_byte = BUF_BEGV_BYTE (b);
17694 int position;
17695 int distance = (height * 2 + 30) * line_number_display_limit_width;
17696
17697 if (startpos - distance > limit)
17698 {
17699 limit = startpos - distance;
17700 limit_byte = CHAR_TO_BYTE (limit);
17701 }
17702
17703 nlines = display_count_lines (startpos, startpos_byte,
17704 limit_byte,
17705 - (height * 2 + 30),
17706 &position);
17707 /* If we couldn't find the lines we wanted within
17708 line_number_display_limit_width chars per line,
17709 give up on line numbers for this window. */
17710 if (position == limit_byte && limit == startpos - distance)
17711 {
17712 w->base_line_pos = w->buffer;
17713 w->base_line_number = Qnil;
17714 goto no_value;
17715 }
17716
17717 w->base_line_number = make_number (topline - nlines);
17718 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17719 }
17720
17721 /* Now count lines from the start pos to point. */
17722 nlines = display_count_lines (startpos, startpos_byte,
17723 PT_BYTE, PT, &junk);
17724
17725 /* Record that we did display the line number. */
17726 line_number_displayed = 1;
17727
17728 /* Make the string to show. */
17729 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17730 return decode_mode_spec_buf;
17731 no_value:
17732 {
17733 char* p = decode_mode_spec_buf;
17734 int pad = field_width - 2;
17735 while (pad-- > 0)
17736 *p++ = ' ';
17737 *p++ = '?';
17738 *p++ = '?';
17739 *p = '\0';
17740 return decode_mode_spec_buf;
17741 }
17742 }
17743 break;
17744
17745 case 'm':
17746 obj = b->mode_name;
17747 break;
17748
17749 case 'n':
17750 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17751 return " Narrow";
17752 break;
17753
17754 case 'p':
17755 {
17756 int pos = marker_position (w->start);
17757 int total = BUF_ZV (b) - BUF_BEGV (b);
17758
17759 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17760 {
17761 if (pos <= BUF_BEGV (b))
17762 return "All";
17763 else
17764 return "Bottom";
17765 }
17766 else if (pos <= BUF_BEGV (b))
17767 return "Top";
17768 else
17769 {
17770 if (total > 1000000)
17771 /* Do it differently for a large value, to avoid overflow. */
17772 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17773 else
17774 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17775 /* We can't normally display a 3-digit number,
17776 so get us a 2-digit number that is close. */
17777 if (total == 100)
17778 total = 99;
17779 sprintf (decode_mode_spec_buf, "%2d%%", total);
17780 return decode_mode_spec_buf;
17781 }
17782 }
17783
17784 /* Display percentage of size above the bottom of the screen. */
17785 case 'P':
17786 {
17787 int toppos = marker_position (w->start);
17788 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17789 int total = BUF_ZV (b) - BUF_BEGV (b);
17790
17791 if (botpos >= BUF_ZV (b))
17792 {
17793 if (toppos <= BUF_BEGV (b))
17794 return "All";
17795 else
17796 return "Bottom";
17797 }
17798 else
17799 {
17800 if (total > 1000000)
17801 /* Do it differently for a large value, to avoid overflow. */
17802 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17803 else
17804 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17805 /* We can't normally display a 3-digit number,
17806 so get us a 2-digit number that is close. */
17807 if (total == 100)
17808 total = 99;
17809 if (toppos <= BUF_BEGV (b))
17810 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17811 else
17812 sprintf (decode_mode_spec_buf, "%2d%%", total);
17813 return decode_mode_spec_buf;
17814 }
17815 }
17816
17817 case 's':
17818 /* status of process */
17819 obj = Fget_buffer_process (Fcurrent_buffer ());
17820 if (NILP (obj))
17821 return "no process";
17822 #ifdef subprocesses
17823 obj = Fsymbol_name (Fprocess_status (obj));
17824 #endif
17825 break;
17826
17827 case 't': /* indicate TEXT or BINARY */
17828 #ifdef MODE_LINE_BINARY_TEXT
17829 return MODE_LINE_BINARY_TEXT (b);
17830 #else
17831 return "T";
17832 #endif
17833
17834 case 'z':
17835 /* coding-system (not including end-of-line format) */
17836 case 'Z':
17837 /* coding-system (including end-of-line type) */
17838 {
17839 int eol_flag = (c == 'Z');
17840 char *p = decode_mode_spec_buf;
17841
17842 if (! FRAME_WINDOW_P (f))
17843 {
17844 /* No need to mention EOL here--the terminal never needs
17845 to do EOL conversion. */
17846 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17847 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17848 }
17849 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17850 p, eol_flag);
17851
17852 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17853 #ifdef subprocesses
17854 obj = Fget_buffer_process (Fcurrent_buffer ());
17855 if (PROCESSP (obj))
17856 {
17857 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17858 p, eol_flag);
17859 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17860 p, eol_flag);
17861 }
17862 #endif /* subprocesses */
17863 #endif /* 0 */
17864 *p = 0;
17865 return decode_mode_spec_buf;
17866 }
17867 }
17868
17869 if (STRINGP (obj))
17870 {
17871 *multibyte = STRING_MULTIBYTE (obj);
17872 return (char *) SDATA (obj);
17873 }
17874 else
17875 return "";
17876 }
17877
17878
17879 /* Count up to COUNT lines starting from START / START_BYTE.
17880 But don't go beyond LIMIT_BYTE.
17881 Return the number of lines thus found (always nonnegative).
17882
17883 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17884
17885 static int
17886 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17887 int start, start_byte, limit_byte, count;
17888 int *byte_pos_ptr;
17889 {
17890 register unsigned char *cursor;
17891 unsigned char *base;
17892
17893 register int ceiling;
17894 register unsigned char *ceiling_addr;
17895 int orig_count = count;
17896
17897 /* If we are not in selective display mode,
17898 check only for newlines. */
17899 int selective_display = (!NILP (current_buffer->selective_display)
17900 && !INTEGERP (current_buffer->selective_display));
17901
17902 if (count > 0)
17903 {
17904 while (start_byte < limit_byte)
17905 {
17906 ceiling = BUFFER_CEILING_OF (start_byte);
17907 ceiling = min (limit_byte - 1, ceiling);
17908 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17909 base = (cursor = BYTE_POS_ADDR (start_byte));
17910 while (1)
17911 {
17912 if (selective_display)
17913 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17914 ;
17915 else
17916 while (*cursor != '\n' && ++cursor != ceiling_addr)
17917 ;
17918
17919 if (cursor != ceiling_addr)
17920 {
17921 if (--count == 0)
17922 {
17923 start_byte += cursor - base + 1;
17924 *byte_pos_ptr = start_byte;
17925 return orig_count;
17926 }
17927 else
17928 if (++cursor == ceiling_addr)
17929 break;
17930 }
17931 else
17932 break;
17933 }
17934 start_byte += cursor - base;
17935 }
17936 }
17937 else
17938 {
17939 while (start_byte > limit_byte)
17940 {
17941 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17942 ceiling = max (limit_byte, ceiling);
17943 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17944 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17945 while (1)
17946 {
17947 if (selective_display)
17948 while (--cursor != ceiling_addr
17949 && *cursor != '\n' && *cursor != 015)
17950 ;
17951 else
17952 while (--cursor != ceiling_addr && *cursor != '\n')
17953 ;
17954
17955 if (cursor != ceiling_addr)
17956 {
17957 if (++count == 0)
17958 {
17959 start_byte += cursor - base + 1;
17960 *byte_pos_ptr = start_byte;
17961 /* When scanning backwards, we should
17962 not count the newline posterior to which we stop. */
17963 return - orig_count - 1;
17964 }
17965 }
17966 else
17967 break;
17968 }
17969 /* Here we add 1 to compensate for the last decrement
17970 of CURSOR, which took it past the valid range. */
17971 start_byte += cursor - base + 1;
17972 }
17973 }
17974
17975 *byte_pos_ptr = limit_byte;
17976
17977 if (count < 0)
17978 return - orig_count + count;
17979 return orig_count - count;
17980
17981 }
17982
17983
17984 \f
17985 /***********************************************************************
17986 Displaying strings
17987 ***********************************************************************/
17988
17989 /* Display a NUL-terminated string, starting with index START.
17990
17991 If STRING is non-null, display that C string. Otherwise, the Lisp
17992 string LISP_STRING is displayed.
17993
17994 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17995 FACE_STRING. Display STRING or LISP_STRING with the face at
17996 FACE_STRING_POS in FACE_STRING:
17997
17998 Display the string in the environment given by IT, but use the
17999 standard display table, temporarily.
18000
18001 FIELD_WIDTH is the minimum number of output glyphs to produce.
18002 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18003 with spaces. If STRING has more characters, more than FIELD_WIDTH
18004 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18005
18006 PRECISION is the maximum number of characters to output from
18007 STRING. PRECISION < 0 means don't truncate the string.
18008
18009 This is roughly equivalent to printf format specifiers:
18010
18011 FIELD_WIDTH PRECISION PRINTF
18012 ----------------------------------------
18013 -1 -1 %s
18014 -1 10 %.10s
18015 10 -1 %10s
18016 20 10 %20.10s
18017
18018 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18019 display them, and < 0 means obey the current buffer's value of
18020 enable_multibyte_characters.
18021
18022 Value is the number of columns displayed. */
18023
18024 static int
18025 display_string (string, lisp_string, face_string, face_string_pos,
18026 start, it, field_width, precision, max_x, multibyte)
18027 unsigned char *string;
18028 Lisp_Object lisp_string;
18029 Lisp_Object face_string;
18030 int face_string_pos;
18031 int start;
18032 struct it *it;
18033 int field_width, precision, max_x;
18034 int multibyte;
18035 {
18036 int hpos_at_start = it->hpos;
18037 int saved_face_id = it->face_id;
18038 struct glyph_row *row = it->glyph_row;
18039
18040 /* Initialize the iterator IT for iteration over STRING beginning
18041 with index START. */
18042 reseat_to_string (it, string, lisp_string, start,
18043 precision, field_width, multibyte);
18044
18045 /* If displaying STRING, set up the face of the iterator
18046 from LISP_STRING, if that's given. */
18047 if (STRINGP (face_string))
18048 {
18049 int endptr;
18050 struct face *face;
18051
18052 it->face_id
18053 = face_at_string_position (it->w, face_string, face_string_pos,
18054 0, it->region_beg_charpos,
18055 it->region_end_charpos,
18056 &endptr, it->base_face_id, 0);
18057 face = FACE_FROM_ID (it->f, it->face_id);
18058 it->face_box_p = face->box != FACE_NO_BOX;
18059 }
18060
18061 /* Set max_x to the maximum allowed X position. Don't let it go
18062 beyond the right edge of the window. */
18063 if (max_x <= 0)
18064 max_x = it->last_visible_x;
18065 else
18066 max_x = min (max_x, it->last_visible_x);
18067
18068 /* Skip over display elements that are not visible. because IT->w is
18069 hscrolled. */
18070 if (it->current_x < it->first_visible_x)
18071 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18072 MOVE_TO_POS | MOVE_TO_X);
18073
18074 row->ascent = it->max_ascent;
18075 row->height = it->max_ascent + it->max_descent;
18076 row->phys_ascent = it->max_phys_ascent;
18077 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18078 row->extra_line_spacing = it->max_extra_line_spacing;
18079
18080 /* This condition is for the case that we are called with current_x
18081 past last_visible_x. */
18082 while (it->current_x < max_x)
18083 {
18084 int x_before, x, n_glyphs_before, i, nglyphs;
18085
18086 /* Get the next display element. */
18087 if (!get_next_display_element (it))
18088 break;
18089
18090 /* Produce glyphs. */
18091 x_before = it->current_x;
18092 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18093 PRODUCE_GLYPHS (it);
18094
18095 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18096 i = 0;
18097 x = x_before;
18098 while (i < nglyphs)
18099 {
18100 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18101
18102 if (!it->truncate_lines_p
18103 && x + glyph->pixel_width > max_x)
18104 {
18105 /* End of continued line or max_x reached. */
18106 if (CHAR_GLYPH_PADDING_P (*glyph))
18107 {
18108 /* A wide character is unbreakable. */
18109 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18110 it->current_x = x_before;
18111 }
18112 else
18113 {
18114 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18115 it->current_x = x;
18116 }
18117 break;
18118 }
18119 else if (x + glyph->pixel_width > it->first_visible_x)
18120 {
18121 /* Glyph is at least partially visible. */
18122 ++it->hpos;
18123 if (x < it->first_visible_x)
18124 it->glyph_row->x = x - it->first_visible_x;
18125 }
18126 else
18127 {
18128 /* Glyph is off the left margin of the display area.
18129 Should not happen. */
18130 abort ();
18131 }
18132
18133 row->ascent = max (row->ascent, it->max_ascent);
18134 row->height = max (row->height, it->max_ascent + it->max_descent);
18135 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18136 row->phys_height = max (row->phys_height,
18137 it->max_phys_ascent + it->max_phys_descent);
18138 row->extra_line_spacing = max (row->extra_line_spacing,
18139 it->max_extra_line_spacing);
18140 x += glyph->pixel_width;
18141 ++i;
18142 }
18143
18144 /* Stop if max_x reached. */
18145 if (i < nglyphs)
18146 break;
18147
18148 /* Stop at line ends. */
18149 if (ITERATOR_AT_END_OF_LINE_P (it))
18150 {
18151 it->continuation_lines_width = 0;
18152 break;
18153 }
18154
18155 set_iterator_to_next (it, 1);
18156
18157 /* Stop if truncating at the right edge. */
18158 if (it->truncate_lines_p
18159 && it->current_x >= it->last_visible_x)
18160 {
18161 /* Add truncation mark, but don't do it if the line is
18162 truncated at a padding space. */
18163 if (IT_CHARPOS (*it) < it->string_nchars)
18164 {
18165 if (!FRAME_WINDOW_P (it->f))
18166 {
18167 int i, n;
18168
18169 if (it->current_x > it->last_visible_x)
18170 {
18171 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18172 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18173 break;
18174 for (n = row->used[TEXT_AREA]; i < n; ++i)
18175 {
18176 row->used[TEXT_AREA] = i;
18177 produce_special_glyphs (it, IT_TRUNCATION);
18178 }
18179 }
18180 produce_special_glyphs (it, IT_TRUNCATION);
18181 }
18182 it->glyph_row->truncated_on_right_p = 1;
18183 }
18184 break;
18185 }
18186 }
18187
18188 /* Maybe insert a truncation at the left. */
18189 if (it->first_visible_x
18190 && IT_CHARPOS (*it) > 0)
18191 {
18192 if (!FRAME_WINDOW_P (it->f))
18193 insert_left_trunc_glyphs (it);
18194 it->glyph_row->truncated_on_left_p = 1;
18195 }
18196
18197 it->face_id = saved_face_id;
18198
18199 /* Value is number of columns displayed. */
18200 return it->hpos - hpos_at_start;
18201 }
18202
18203
18204 \f
18205 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18206 appears as an element of LIST or as the car of an element of LIST.
18207 If PROPVAL is a list, compare each element against LIST in that
18208 way, and return 1/2 if any element of PROPVAL is found in LIST.
18209 Otherwise return 0. This function cannot quit.
18210 The return value is 2 if the text is invisible but with an ellipsis
18211 and 1 if it's invisible and without an ellipsis. */
18212
18213 int
18214 invisible_p (propval, list)
18215 register Lisp_Object propval;
18216 Lisp_Object list;
18217 {
18218 register Lisp_Object tail, proptail;
18219
18220 for (tail = list; CONSP (tail); tail = XCDR (tail))
18221 {
18222 register Lisp_Object tem;
18223 tem = XCAR (tail);
18224 if (EQ (propval, tem))
18225 return 1;
18226 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18227 return NILP (XCDR (tem)) ? 1 : 2;
18228 }
18229
18230 if (CONSP (propval))
18231 {
18232 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18233 {
18234 Lisp_Object propelt;
18235 propelt = XCAR (proptail);
18236 for (tail = list; CONSP (tail); tail = XCDR (tail))
18237 {
18238 register Lisp_Object tem;
18239 tem = XCAR (tail);
18240 if (EQ (propelt, tem))
18241 return 1;
18242 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18243 return NILP (XCDR (tem)) ? 1 : 2;
18244 }
18245 }
18246 }
18247
18248 return 0;
18249 }
18250
18251 /* Calculate a width or height in pixels from a specification using
18252 the following elements:
18253
18254 SPEC ::=
18255 NUM - a (fractional) multiple of the default font width/height
18256 (NUM) - specifies exactly NUM pixels
18257 UNIT - a fixed number of pixels, see below.
18258 ELEMENT - size of a display element in pixels, see below.
18259 (NUM . SPEC) - equals NUM * SPEC
18260 (+ SPEC SPEC ...) - add pixel values
18261 (- SPEC SPEC ...) - subtract pixel values
18262 (- SPEC) - negate pixel value
18263
18264 NUM ::=
18265 INT or FLOAT - a number constant
18266 SYMBOL - use symbol's (buffer local) variable binding.
18267
18268 UNIT ::=
18269 in - pixels per inch *)
18270 mm - pixels per 1/1000 meter *)
18271 cm - pixels per 1/100 meter *)
18272 width - width of current font in pixels.
18273 height - height of current font in pixels.
18274
18275 *) using the ratio(s) defined in display-pixels-per-inch.
18276
18277 ELEMENT ::=
18278
18279 left-fringe - left fringe width in pixels
18280 right-fringe - right fringe width in pixels
18281
18282 left-margin - left margin width in pixels
18283 right-margin - right margin width in pixels
18284
18285 scroll-bar - scroll-bar area width in pixels
18286
18287 Examples:
18288
18289 Pixels corresponding to 5 inches:
18290 (5 . in)
18291
18292 Total width of non-text areas on left side of window (if scroll-bar is on left):
18293 '(space :width (+ left-fringe left-margin scroll-bar))
18294
18295 Align to first text column (in header line):
18296 '(space :align-to 0)
18297
18298 Align to middle of text area minus half the width of variable `my-image'
18299 containing a loaded image:
18300 '(space :align-to (0.5 . (- text my-image)))
18301
18302 Width of left margin minus width of 1 character in the default font:
18303 '(space :width (- left-margin 1))
18304
18305 Width of left margin minus width of 2 characters in the current font:
18306 '(space :width (- left-margin (2 . width)))
18307
18308 Center 1 character over left-margin (in header line):
18309 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18310
18311 Different ways to express width of left fringe plus left margin minus one pixel:
18312 '(space :width (- (+ left-fringe left-margin) (1)))
18313 '(space :width (+ left-fringe left-margin (- (1))))
18314 '(space :width (+ left-fringe left-margin (-1)))
18315
18316 */
18317
18318 #define NUMVAL(X) \
18319 ((INTEGERP (X) || FLOATP (X)) \
18320 ? XFLOATINT (X) \
18321 : - 1)
18322
18323 int
18324 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18325 double *res;
18326 struct it *it;
18327 Lisp_Object prop;
18328 void *font;
18329 int width_p, *align_to;
18330 {
18331 double pixels;
18332
18333 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18334 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18335
18336 if (NILP (prop))
18337 return OK_PIXELS (0);
18338
18339 if (SYMBOLP (prop))
18340 {
18341 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18342 {
18343 char *unit = SDATA (SYMBOL_NAME (prop));
18344
18345 if (unit[0] == 'i' && unit[1] == 'n')
18346 pixels = 1.0;
18347 else if (unit[0] == 'm' && unit[1] == 'm')
18348 pixels = 25.4;
18349 else if (unit[0] == 'c' && unit[1] == 'm')
18350 pixels = 2.54;
18351 else
18352 pixels = 0;
18353 if (pixels > 0)
18354 {
18355 double ppi;
18356 #ifdef HAVE_WINDOW_SYSTEM
18357 if (FRAME_WINDOW_P (it->f)
18358 && (ppi = (width_p
18359 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18360 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18361 ppi > 0))
18362 return OK_PIXELS (ppi / pixels);
18363 #endif
18364
18365 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18366 || (CONSP (Vdisplay_pixels_per_inch)
18367 && (ppi = (width_p
18368 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18369 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18370 ppi > 0)))
18371 return OK_PIXELS (ppi / pixels);
18372
18373 return 0;
18374 }
18375 }
18376
18377 #ifdef HAVE_WINDOW_SYSTEM
18378 if (EQ (prop, Qheight))
18379 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18380 if (EQ (prop, Qwidth))
18381 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18382 #else
18383 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18384 return OK_PIXELS (1);
18385 #endif
18386
18387 if (EQ (prop, Qtext))
18388 return OK_PIXELS (width_p
18389 ? window_box_width (it->w, TEXT_AREA)
18390 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18391
18392 if (align_to && *align_to < 0)
18393 {
18394 *res = 0;
18395 if (EQ (prop, Qleft))
18396 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18397 if (EQ (prop, Qright))
18398 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18399 if (EQ (prop, Qcenter))
18400 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18401 + window_box_width (it->w, TEXT_AREA) / 2);
18402 if (EQ (prop, Qleft_fringe))
18403 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18404 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18405 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18406 if (EQ (prop, Qright_fringe))
18407 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18408 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18409 : window_box_right_offset (it->w, TEXT_AREA));
18410 if (EQ (prop, Qleft_margin))
18411 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18412 if (EQ (prop, Qright_margin))
18413 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18414 if (EQ (prop, Qscroll_bar))
18415 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18416 ? 0
18417 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18418 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18419 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18420 : 0)));
18421 }
18422 else
18423 {
18424 if (EQ (prop, Qleft_fringe))
18425 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18426 if (EQ (prop, Qright_fringe))
18427 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18428 if (EQ (prop, Qleft_margin))
18429 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18430 if (EQ (prop, Qright_margin))
18431 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18432 if (EQ (prop, Qscroll_bar))
18433 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18434 }
18435
18436 prop = Fbuffer_local_value (prop, it->w->buffer);
18437 }
18438
18439 if (INTEGERP (prop) || FLOATP (prop))
18440 {
18441 int base_unit = (width_p
18442 ? FRAME_COLUMN_WIDTH (it->f)
18443 : FRAME_LINE_HEIGHT (it->f));
18444 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18445 }
18446
18447 if (CONSP (prop))
18448 {
18449 Lisp_Object car = XCAR (prop);
18450 Lisp_Object cdr = XCDR (prop);
18451
18452 if (SYMBOLP (car))
18453 {
18454 #ifdef HAVE_WINDOW_SYSTEM
18455 if (valid_image_p (prop))
18456 {
18457 int id = lookup_image (it->f, prop);
18458 struct image *img = IMAGE_FROM_ID (it->f, id);
18459
18460 return OK_PIXELS (width_p ? img->width : img->height);
18461 }
18462 #endif
18463 if (EQ (car, Qplus) || EQ (car, Qminus))
18464 {
18465 int first = 1;
18466 double px;
18467
18468 pixels = 0;
18469 while (CONSP (cdr))
18470 {
18471 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18472 font, width_p, align_to))
18473 return 0;
18474 if (first)
18475 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18476 else
18477 pixels += px;
18478 cdr = XCDR (cdr);
18479 }
18480 if (EQ (car, Qminus))
18481 pixels = -pixels;
18482 return OK_PIXELS (pixels);
18483 }
18484
18485 car = Fbuffer_local_value (car, it->w->buffer);
18486 }
18487
18488 if (INTEGERP (car) || FLOATP (car))
18489 {
18490 double fact;
18491 pixels = XFLOATINT (car);
18492 if (NILP (cdr))
18493 return OK_PIXELS (pixels);
18494 if (calc_pixel_width_or_height (&fact, it, cdr,
18495 font, width_p, align_to))
18496 return OK_PIXELS (pixels * fact);
18497 return 0;
18498 }
18499
18500 return 0;
18501 }
18502
18503 return 0;
18504 }
18505
18506 \f
18507 /***********************************************************************
18508 Glyph Display
18509 ***********************************************************************/
18510
18511 #ifdef HAVE_WINDOW_SYSTEM
18512
18513 #if GLYPH_DEBUG
18514
18515 void
18516 dump_glyph_string (s)
18517 struct glyph_string *s;
18518 {
18519 fprintf (stderr, "glyph string\n");
18520 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18521 s->x, s->y, s->width, s->height);
18522 fprintf (stderr, " ybase = %d\n", s->ybase);
18523 fprintf (stderr, " hl = %d\n", s->hl);
18524 fprintf (stderr, " left overhang = %d, right = %d\n",
18525 s->left_overhang, s->right_overhang);
18526 fprintf (stderr, " nchars = %d\n", s->nchars);
18527 fprintf (stderr, " extends to end of line = %d\n",
18528 s->extends_to_end_of_line_p);
18529 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18530 fprintf (stderr, " bg width = %d\n", s->background_width);
18531 }
18532
18533 #endif /* GLYPH_DEBUG */
18534
18535 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18536 of XChar2b structures for S; it can't be allocated in
18537 init_glyph_string because it must be allocated via `alloca'. W
18538 is the window on which S is drawn. ROW and AREA are the glyph row
18539 and area within the row from which S is constructed. START is the
18540 index of the first glyph structure covered by S. HL is a
18541 face-override for drawing S. */
18542
18543 #ifdef HAVE_NTGUI
18544 #define OPTIONAL_HDC(hdc) hdc,
18545 #define DECLARE_HDC(hdc) HDC hdc;
18546 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18547 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18548 #endif
18549
18550 #ifndef OPTIONAL_HDC
18551 #define OPTIONAL_HDC(hdc)
18552 #define DECLARE_HDC(hdc)
18553 #define ALLOCATE_HDC(hdc, f)
18554 #define RELEASE_HDC(hdc, f)
18555 #endif
18556
18557 static void
18558 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18559 struct glyph_string *s;
18560 DECLARE_HDC (hdc)
18561 XChar2b *char2b;
18562 struct window *w;
18563 struct glyph_row *row;
18564 enum glyph_row_area area;
18565 int start;
18566 enum draw_glyphs_face hl;
18567 {
18568 bzero (s, sizeof *s);
18569 s->w = w;
18570 s->f = XFRAME (w->frame);
18571 #ifdef HAVE_NTGUI
18572 s->hdc = hdc;
18573 #endif
18574 s->display = FRAME_X_DISPLAY (s->f);
18575 s->window = FRAME_X_WINDOW (s->f);
18576 s->char2b = char2b;
18577 s->hl = hl;
18578 s->row = row;
18579 s->area = area;
18580 s->first_glyph = row->glyphs[area] + start;
18581 s->height = row->height;
18582 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18583
18584 /* Display the internal border below the tool-bar window. */
18585 if (WINDOWP (s->f->tool_bar_window)
18586 && s->w == XWINDOW (s->f->tool_bar_window))
18587 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18588
18589 s->ybase = s->y + row->ascent;
18590 }
18591
18592
18593 /* Append the list of glyph strings with head H and tail T to the list
18594 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18595
18596 static INLINE void
18597 append_glyph_string_lists (head, tail, h, t)
18598 struct glyph_string **head, **tail;
18599 struct glyph_string *h, *t;
18600 {
18601 if (h)
18602 {
18603 if (*head)
18604 (*tail)->next = h;
18605 else
18606 *head = h;
18607 h->prev = *tail;
18608 *tail = t;
18609 }
18610 }
18611
18612
18613 /* Prepend the list of glyph strings with head H and tail T to the
18614 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18615 result. */
18616
18617 static INLINE void
18618 prepend_glyph_string_lists (head, tail, h, t)
18619 struct glyph_string **head, **tail;
18620 struct glyph_string *h, *t;
18621 {
18622 if (h)
18623 {
18624 if (*head)
18625 (*head)->prev = t;
18626 else
18627 *tail = t;
18628 t->next = *head;
18629 *head = h;
18630 }
18631 }
18632
18633
18634 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18635 Set *HEAD and *TAIL to the resulting list. */
18636
18637 static INLINE void
18638 append_glyph_string (head, tail, s)
18639 struct glyph_string **head, **tail;
18640 struct glyph_string *s;
18641 {
18642 s->next = s->prev = NULL;
18643 append_glyph_string_lists (head, tail, s, s);
18644 }
18645
18646
18647 /* Get face and two-byte form of character glyph GLYPH on frame F.
18648 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18649 a pointer to a realized face that is ready for display. */
18650
18651 static INLINE struct face *
18652 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18653 struct frame *f;
18654 struct glyph *glyph;
18655 XChar2b *char2b;
18656 int *two_byte_p;
18657 {
18658 struct face *face;
18659
18660 xassert (glyph->type == CHAR_GLYPH);
18661 face = FACE_FROM_ID (f, glyph->face_id);
18662
18663 if (two_byte_p)
18664 *two_byte_p = 0;
18665
18666 if (!glyph->multibyte_p)
18667 {
18668 /* Unibyte case. We don't have to encode, but we have to make
18669 sure to use a face suitable for unibyte. */
18670 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18671 }
18672 else if (glyph->u.ch < 128)
18673 {
18674 /* Case of ASCII in a face known to fit ASCII. */
18675 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18676 }
18677 else
18678 {
18679 int c1, c2, charset;
18680
18681 /* Split characters into bytes. If c2 is -1 afterwards, C is
18682 really a one-byte character so that byte1 is zero. */
18683 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18684 if (c2 > 0)
18685 STORE_XCHAR2B (char2b, c1, c2);
18686 else
18687 STORE_XCHAR2B (char2b, 0, c1);
18688
18689 /* Maybe encode the character in *CHAR2B. */
18690 if (charset != CHARSET_ASCII)
18691 {
18692 struct font_info *font_info
18693 = FONT_INFO_FROM_ID (f, face->font_info_id);
18694 if (font_info)
18695 glyph->font_type
18696 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18697 }
18698 }
18699
18700 /* Make sure X resources of the face are allocated. */
18701 xassert (face != NULL);
18702 PREPARE_FACE_FOR_DISPLAY (f, face);
18703 return face;
18704 }
18705
18706
18707 /* Fill glyph string S with composition components specified by S->cmp.
18708
18709 FACES is an array of faces for all components of this composition.
18710 S->gidx is the index of the first component for S.
18711
18712 OVERLAPS non-zero means S should draw the foreground only, and use
18713 its physical height for clipping. See also draw_glyphs.
18714
18715 Value is the index of a component not in S. */
18716
18717 static int
18718 fill_composite_glyph_string (s, faces, overlaps)
18719 struct glyph_string *s;
18720 struct face **faces;
18721 int overlaps;
18722 {
18723 int i;
18724
18725 xassert (s);
18726
18727 s->for_overlaps = overlaps;
18728
18729 s->face = faces[s->gidx];
18730 s->font = s->face->font;
18731 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18732
18733 /* For all glyphs of this composition, starting at the offset
18734 S->gidx, until we reach the end of the definition or encounter a
18735 glyph that requires the different face, add it to S. */
18736 ++s->nchars;
18737 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18738 ++s->nchars;
18739
18740 /* All glyph strings for the same composition has the same width,
18741 i.e. the width set for the first component of the composition. */
18742
18743 s->width = s->first_glyph->pixel_width;
18744
18745 /* If the specified font could not be loaded, use the frame's
18746 default font, but record the fact that we couldn't load it in
18747 the glyph string so that we can draw rectangles for the
18748 characters of the glyph string. */
18749 if (s->font == NULL)
18750 {
18751 s->font_not_found_p = 1;
18752 s->font = FRAME_FONT (s->f);
18753 }
18754
18755 /* Adjust base line for subscript/superscript text. */
18756 s->ybase += s->first_glyph->voffset;
18757
18758 xassert (s->face && s->face->gc);
18759
18760 /* This glyph string must always be drawn with 16-bit functions. */
18761 s->two_byte_p = 1;
18762
18763 return s->gidx + s->nchars;
18764 }
18765
18766
18767 /* Fill glyph string S from a sequence of character glyphs.
18768
18769 FACE_ID is the face id of the string. START is the index of the
18770 first glyph to consider, END is the index of the last + 1.
18771 OVERLAPS non-zero means S should draw the foreground only, and use
18772 its physical height for clipping. See also draw_glyphs.
18773
18774 Value is the index of the first glyph not in S. */
18775
18776 static int
18777 fill_glyph_string (s, face_id, start, end, overlaps)
18778 struct glyph_string *s;
18779 int face_id;
18780 int start, end, overlaps;
18781 {
18782 struct glyph *glyph, *last;
18783 int voffset;
18784 int glyph_not_available_p;
18785
18786 xassert (s->f == XFRAME (s->w->frame));
18787 xassert (s->nchars == 0);
18788 xassert (start >= 0 && end > start);
18789
18790 s->for_overlaps = overlaps,
18791 glyph = s->row->glyphs[s->area] + start;
18792 last = s->row->glyphs[s->area] + end;
18793 voffset = glyph->voffset;
18794
18795 glyph_not_available_p = glyph->glyph_not_available_p;
18796
18797 while (glyph < last
18798 && glyph->type == CHAR_GLYPH
18799 && glyph->voffset == voffset
18800 /* Same face id implies same font, nowadays. */
18801 && glyph->face_id == face_id
18802 && glyph->glyph_not_available_p == glyph_not_available_p)
18803 {
18804 int two_byte_p;
18805
18806 s->face = get_glyph_face_and_encoding (s->f, glyph,
18807 s->char2b + s->nchars,
18808 &two_byte_p);
18809 s->two_byte_p = two_byte_p;
18810 ++s->nchars;
18811 xassert (s->nchars <= end - start);
18812 s->width += glyph->pixel_width;
18813 ++glyph;
18814 }
18815
18816 s->font = s->face->font;
18817 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18818
18819 /* If the specified font could not be loaded, use the frame's font,
18820 but record the fact that we couldn't load it in
18821 S->font_not_found_p so that we can draw rectangles for the
18822 characters of the glyph string. */
18823 if (s->font == NULL || glyph_not_available_p)
18824 {
18825 s->font_not_found_p = 1;
18826 s->font = FRAME_FONT (s->f);
18827 }
18828
18829 /* Adjust base line for subscript/superscript text. */
18830 s->ybase += voffset;
18831
18832 xassert (s->face && s->face->gc);
18833 return glyph - s->row->glyphs[s->area];
18834 }
18835
18836
18837 /* Fill glyph string S from image glyph S->first_glyph. */
18838
18839 static void
18840 fill_image_glyph_string (s)
18841 struct glyph_string *s;
18842 {
18843 xassert (s->first_glyph->type == IMAGE_GLYPH);
18844 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18845 xassert (s->img);
18846 s->slice = s->first_glyph->slice;
18847 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18848 s->font = s->face->font;
18849 s->width = s->first_glyph->pixel_width;
18850
18851 /* Adjust base line for subscript/superscript text. */
18852 s->ybase += s->first_glyph->voffset;
18853 }
18854
18855
18856 /* Fill glyph string S from a sequence of stretch glyphs.
18857
18858 ROW is the glyph row in which the glyphs are found, AREA is the
18859 area within the row. START is the index of the first glyph to
18860 consider, END is the index of the last + 1.
18861
18862 Value is the index of the first glyph not in S. */
18863
18864 static int
18865 fill_stretch_glyph_string (s, row, area, start, end)
18866 struct glyph_string *s;
18867 struct glyph_row *row;
18868 enum glyph_row_area area;
18869 int start, end;
18870 {
18871 struct glyph *glyph, *last;
18872 int voffset, face_id;
18873
18874 xassert (s->first_glyph->type == STRETCH_GLYPH);
18875
18876 glyph = s->row->glyphs[s->area] + start;
18877 last = s->row->glyphs[s->area] + end;
18878 face_id = glyph->face_id;
18879 s->face = FACE_FROM_ID (s->f, face_id);
18880 s->font = s->face->font;
18881 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18882 s->width = glyph->pixel_width;
18883 s->nchars = 1;
18884 voffset = glyph->voffset;
18885
18886 for (++glyph;
18887 (glyph < last
18888 && glyph->type == STRETCH_GLYPH
18889 && glyph->voffset == voffset
18890 && glyph->face_id == face_id);
18891 ++glyph)
18892 s->width += glyph->pixel_width;
18893
18894 /* Adjust base line for subscript/superscript text. */
18895 s->ybase += voffset;
18896
18897 /* The case that face->gc == 0 is handled when drawing the glyph
18898 string by calling PREPARE_FACE_FOR_DISPLAY. */
18899 xassert (s->face);
18900 return glyph - s->row->glyphs[s->area];
18901 }
18902
18903
18904 /* EXPORT for RIF:
18905 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18906 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18907 assumed to be zero. */
18908
18909 void
18910 x_get_glyph_overhangs (glyph, f, left, right)
18911 struct glyph *glyph;
18912 struct frame *f;
18913 int *left, *right;
18914 {
18915 *left = *right = 0;
18916
18917 if (glyph->type == CHAR_GLYPH)
18918 {
18919 XFontStruct *font;
18920 struct face *face;
18921 struct font_info *font_info;
18922 XChar2b char2b;
18923 XCharStruct *pcm;
18924
18925 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18926 font = face->font;
18927 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18928 if (font /* ++KFS: Should this be font_info ? */
18929 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18930 {
18931 if (pcm->rbearing > pcm->width)
18932 *right = pcm->rbearing - pcm->width;
18933 if (pcm->lbearing < 0)
18934 *left = -pcm->lbearing;
18935 }
18936 }
18937 }
18938
18939
18940 /* Return the index of the first glyph preceding glyph string S that
18941 is overwritten by S because of S's left overhang. Value is -1
18942 if no glyphs are overwritten. */
18943
18944 static int
18945 left_overwritten (s)
18946 struct glyph_string *s;
18947 {
18948 int k;
18949
18950 if (s->left_overhang)
18951 {
18952 int x = 0, i;
18953 struct glyph *glyphs = s->row->glyphs[s->area];
18954 int first = s->first_glyph - glyphs;
18955
18956 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18957 x -= glyphs[i].pixel_width;
18958
18959 k = i + 1;
18960 }
18961 else
18962 k = -1;
18963
18964 return k;
18965 }
18966
18967
18968 /* Return the index of the first glyph preceding glyph string S that
18969 is overwriting S because of its right overhang. Value is -1 if no
18970 glyph in front of S overwrites S. */
18971
18972 static int
18973 left_overwriting (s)
18974 struct glyph_string *s;
18975 {
18976 int i, k, x;
18977 struct glyph *glyphs = s->row->glyphs[s->area];
18978 int first = s->first_glyph - glyphs;
18979
18980 k = -1;
18981 x = 0;
18982 for (i = first - 1; i >= 0; --i)
18983 {
18984 int left, right;
18985 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18986 if (x + right > 0)
18987 k = i;
18988 x -= glyphs[i].pixel_width;
18989 }
18990
18991 return k;
18992 }
18993
18994
18995 /* Return the index of the last glyph following glyph string S that is
18996 not overwritten by S because of S's right overhang. Value is -1 if
18997 no such glyph is found. */
18998
18999 static int
19000 right_overwritten (s)
19001 struct glyph_string *s;
19002 {
19003 int k = -1;
19004
19005 if (s->right_overhang)
19006 {
19007 int x = 0, i;
19008 struct glyph *glyphs = s->row->glyphs[s->area];
19009 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19010 int end = s->row->used[s->area];
19011
19012 for (i = first; i < end && s->right_overhang > x; ++i)
19013 x += glyphs[i].pixel_width;
19014
19015 k = i;
19016 }
19017
19018 return k;
19019 }
19020
19021
19022 /* Return the index of the last glyph following glyph string S that
19023 overwrites S because of its left overhang. Value is negative
19024 if no such glyph is found. */
19025
19026 static int
19027 right_overwriting (s)
19028 struct glyph_string *s;
19029 {
19030 int i, k, x;
19031 int end = s->row->used[s->area];
19032 struct glyph *glyphs = s->row->glyphs[s->area];
19033 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19034
19035 k = -1;
19036 x = 0;
19037 for (i = first; i < end; ++i)
19038 {
19039 int left, right;
19040 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19041 if (x - left < 0)
19042 k = i;
19043 x += glyphs[i].pixel_width;
19044 }
19045
19046 return k;
19047 }
19048
19049
19050 /* Get face and two-byte form of character C in face FACE_ID on frame
19051 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19052 means we want to display multibyte text. DISPLAY_P non-zero means
19053 make sure that X resources for the face returned are allocated.
19054 Value is a pointer to a realized face that is ready for display if
19055 DISPLAY_P is non-zero. */
19056
19057 static INLINE struct face *
19058 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19059 struct frame *f;
19060 int c, face_id;
19061 XChar2b *char2b;
19062 int multibyte_p, display_p;
19063 {
19064 struct face *face = FACE_FROM_ID (f, face_id);
19065
19066 if (!multibyte_p)
19067 {
19068 /* Unibyte case. We don't have to encode, but we have to make
19069 sure to use a face suitable for unibyte. */
19070 STORE_XCHAR2B (char2b, 0, c);
19071 face_id = FACE_FOR_CHAR (f, face, c);
19072 face = FACE_FROM_ID (f, face_id);
19073 }
19074 else if (c < 128)
19075 {
19076 /* Case of ASCII in a face known to fit ASCII. */
19077 STORE_XCHAR2B (char2b, 0, c);
19078 }
19079 else
19080 {
19081 int c1, c2, charset;
19082
19083 /* Split characters into bytes. If c2 is -1 afterwards, C is
19084 really a one-byte character so that byte1 is zero. */
19085 SPLIT_CHAR (c, charset, c1, c2);
19086 if (c2 > 0)
19087 STORE_XCHAR2B (char2b, c1, c2);
19088 else
19089 STORE_XCHAR2B (char2b, 0, c1);
19090
19091 /* Maybe encode the character in *CHAR2B. */
19092 if (face->font != NULL)
19093 {
19094 struct font_info *font_info
19095 = FONT_INFO_FROM_ID (f, face->font_info_id);
19096 if (font_info)
19097 rif->encode_char (c, char2b, font_info, 0);
19098 }
19099 }
19100
19101 /* Make sure X resources of the face are allocated. */
19102 #ifdef HAVE_X_WINDOWS
19103 if (display_p)
19104 #endif
19105 {
19106 xassert (face != NULL);
19107 PREPARE_FACE_FOR_DISPLAY (f, face);
19108 }
19109
19110 return face;
19111 }
19112
19113
19114 /* Set background width of glyph string S. START is the index of the
19115 first glyph following S. LAST_X is the right-most x-position + 1
19116 in the drawing area. */
19117
19118 static INLINE void
19119 set_glyph_string_background_width (s, start, last_x)
19120 struct glyph_string *s;
19121 int start;
19122 int last_x;
19123 {
19124 /* If the face of this glyph string has to be drawn to the end of
19125 the drawing area, set S->extends_to_end_of_line_p. */
19126
19127 if (start == s->row->used[s->area]
19128 && s->area == TEXT_AREA
19129 && ((s->row->fill_line_p
19130 && (s->hl == DRAW_NORMAL_TEXT
19131 || s->hl == DRAW_IMAGE_RAISED
19132 || s->hl == DRAW_IMAGE_SUNKEN))
19133 || s->hl == DRAW_MOUSE_FACE))
19134 s->extends_to_end_of_line_p = 1;
19135
19136 /* If S extends its face to the end of the line, set its
19137 background_width to the distance to the right edge of the drawing
19138 area. */
19139 if (s->extends_to_end_of_line_p)
19140 s->background_width = last_x - s->x + 1;
19141 else
19142 s->background_width = s->width;
19143 }
19144
19145
19146 /* Compute overhangs and x-positions for glyph string S and its
19147 predecessors, or successors. X is the starting x-position for S.
19148 BACKWARD_P non-zero means process predecessors. */
19149
19150 static void
19151 compute_overhangs_and_x (s, x, backward_p)
19152 struct glyph_string *s;
19153 int x;
19154 int backward_p;
19155 {
19156 if (backward_p)
19157 {
19158 while (s)
19159 {
19160 if (rif->compute_glyph_string_overhangs)
19161 rif->compute_glyph_string_overhangs (s);
19162 x -= s->width;
19163 s->x = x;
19164 s = s->prev;
19165 }
19166 }
19167 else
19168 {
19169 while (s)
19170 {
19171 if (rif->compute_glyph_string_overhangs)
19172 rif->compute_glyph_string_overhangs (s);
19173 s->x = x;
19174 x += s->width;
19175 s = s->next;
19176 }
19177 }
19178 }
19179
19180
19181
19182 /* The following macros are only called from draw_glyphs below.
19183 They reference the following parameters of that function directly:
19184 `w', `row', `area', and `overlap_p'
19185 as well as the following local variables:
19186 `s', `f', and `hdc' (in W32) */
19187
19188 #ifdef HAVE_NTGUI
19189 /* On W32, silently add local `hdc' variable to argument list of
19190 init_glyph_string. */
19191 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19192 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19193 #else
19194 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19195 init_glyph_string (s, char2b, w, row, area, start, hl)
19196 #endif
19197
19198 /* Add a glyph string for a stretch glyph to the list of strings
19199 between HEAD and TAIL. START is the index of the stretch glyph in
19200 row area AREA of glyph row ROW. END is the index of the last glyph
19201 in that glyph row area. X is the current output position assigned
19202 to the new glyph string constructed. HL overrides that face of the
19203 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19204 is the right-most x-position of the drawing area. */
19205
19206 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19207 and below -- keep them on one line. */
19208 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19209 do \
19210 { \
19211 s = (struct glyph_string *) alloca (sizeof *s); \
19212 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19213 START = fill_stretch_glyph_string (s, row, area, START, END); \
19214 append_glyph_string (&HEAD, &TAIL, s); \
19215 s->x = (X); \
19216 } \
19217 while (0)
19218
19219
19220 /* Add a glyph string for an image glyph to the list of strings
19221 between HEAD and TAIL. START is the index of the image glyph in
19222 row area AREA of glyph row ROW. END is the index of the last glyph
19223 in that glyph row area. X is the current output position assigned
19224 to the new glyph string constructed. HL overrides that face of the
19225 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19226 is the right-most x-position of the drawing area. */
19227
19228 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19229 do \
19230 { \
19231 s = (struct glyph_string *) alloca (sizeof *s); \
19232 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19233 fill_image_glyph_string (s); \
19234 append_glyph_string (&HEAD, &TAIL, s); \
19235 ++START; \
19236 s->x = (X); \
19237 } \
19238 while (0)
19239
19240
19241 /* Add a glyph string for a sequence of character glyphs to the list
19242 of strings between HEAD and TAIL. START is the index of the first
19243 glyph in row area AREA of glyph row ROW that is part of the new
19244 glyph string. END is the index of the last glyph in that glyph row
19245 area. X is the current output position assigned to the new glyph
19246 string constructed. HL overrides that face of the glyph; e.g. it
19247 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19248 right-most x-position of the drawing area. */
19249
19250 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19251 do \
19252 { \
19253 int c, face_id; \
19254 XChar2b *char2b; \
19255 \
19256 c = (row)->glyphs[area][START].u.ch; \
19257 face_id = (row)->glyphs[area][START].face_id; \
19258 \
19259 s = (struct glyph_string *) alloca (sizeof *s); \
19260 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19261 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19262 append_glyph_string (&HEAD, &TAIL, s); \
19263 s->x = (X); \
19264 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19265 } \
19266 while (0)
19267
19268
19269 /* Add a glyph string for a composite sequence to the list of strings
19270 between HEAD and TAIL. START is the index of the first glyph in
19271 row area AREA of glyph row ROW that is part of the new glyph
19272 string. END is the index of the last glyph in that glyph row area.
19273 X is the current output position assigned to the new glyph string
19274 constructed. HL overrides that face of the glyph; e.g. it is
19275 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19276 x-position of the drawing area. */
19277
19278 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19279 do { \
19280 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19281 int face_id = (row)->glyphs[area][START].face_id; \
19282 struct face *base_face = FACE_FROM_ID (f, face_id); \
19283 struct composition *cmp = composition_table[cmp_id]; \
19284 int glyph_len = cmp->glyph_len; \
19285 XChar2b *char2b; \
19286 struct face **faces; \
19287 struct glyph_string *first_s = NULL; \
19288 int n; \
19289 \
19290 base_face = base_face->ascii_face; \
19291 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19292 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19293 /* At first, fill in `char2b' and `faces'. */ \
19294 for (n = 0; n < glyph_len; n++) \
19295 { \
19296 int c = COMPOSITION_GLYPH (cmp, n); \
19297 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19298 faces[n] = FACE_FROM_ID (f, this_face_id); \
19299 get_char_face_and_encoding (f, c, this_face_id, \
19300 char2b + n, 1, 1); \
19301 } \
19302 \
19303 /* Make glyph_strings for each glyph sequence that is drawable by \
19304 the same face, and append them to HEAD/TAIL. */ \
19305 for (n = 0; n < cmp->glyph_len;) \
19306 { \
19307 s = (struct glyph_string *) alloca (sizeof *s); \
19308 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19309 append_glyph_string (&(HEAD), &(TAIL), s); \
19310 s->cmp = cmp; \
19311 s->gidx = n; \
19312 s->x = (X); \
19313 \
19314 if (n == 0) \
19315 first_s = s; \
19316 \
19317 n = fill_composite_glyph_string (s, faces, overlaps); \
19318 } \
19319 \
19320 ++START; \
19321 s = first_s; \
19322 } while (0)
19323
19324
19325 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19326 of AREA of glyph row ROW on window W between indices START and END.
19327 HL overrides the face for drawing glyph strings, e.g. it is
19328 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19329 x-positions of the drawing area.
19330
19331 This is an ugly monster macro construct because we must use alloca
19332 to allocate glyph strings (because draw_glyphs can be called
19333 asynchronously). */
19334
19335 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19336 do \
19337 { \
19338 HEAD = TAIL = NULL; \
19339 while (START < END) \
19340 { \
19341 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19342 switch (first_glyph->type) \
19343 { \
19344 case CHAR_GLYPH: \
19345 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19346 HL, X, LAST_X); \
19347 break; \
19348 \
19349 case COMPOSITE_GLYPH: \
19350 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19351 HL, X, LAST_X); \
19352 break; \
19353 \
19354 case STRETCH_GLYPH: \
19355 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19356 HL, X, LAST_X); \
19357 break; \
19358 \
19359 case IMAGE_GLYPH: \
19360 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19361 HL, X, LAST_X); \
19362 break; \
19363 \
19364 default: \
19365 abort (); \
19366 } \
19367 \
19368 set_glyph_string_background_width (s, START, LAST_X); \
19369 (X) += s->width; \
19370 } \
19371 } \
19372 while (0)
19373
19374
19375 /* Draw glyphs between START and END in AREA of ROW on window W,
19376 starting at x-position X. X is relative to AREA in W. HL is a
19377 face-override with the following meaning:
19378
19379 DRAW_NORMAL_TEXT draw normally
19380 DRAW_CURSOR draw in cursor face
19381 DRAW_MOUSE_FACE draw in mouse face.
19382 DRAW_INVERSE_VIDEO draw in mode line face
19383 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19384 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19385
19386 If OVERLAPS is non-zero, draw only the foreground of characters and
19387 clip to the physical height of ROW. Non-zero value also defines
19388 the overlapping part to be drawn:
19389
19390 OVERLAPS_PRED overlap with preceding rows
19391 OVERLAPS_SUCC overlap with succeeding rows
19392 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19393 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19394
19395 Value is the x-position reached, relative to AREA of W. */
19396
19397 static int
19398 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19399 struct window *w;
19400 int x;
19401 struct glyph_row *row;
19402 enum glyph_row_area area;
19403 int start, end;
19404 enum draw_glyphs_face hl;
19405 int overlaps;
19406 {
19407 struct glyph_string *head, *tail;
19408 struct glyph_string *s;
19409 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19410 int last_x, area_width;
19411 int x_reached;
19412 int i, j;
19413 struct frame *f = XFRAME (WINDOW_FRAME (w));
19414 DECLARE_HDC (hdc);
19415
19416 ALLOCATE_HDC (hdc, f);
19417
19418 /* Let's rather be paranoid than getting a SEGV. */
19419 end = min (end, row->used[area]);
19420 start = max (0, start);
19421 start = min (end, start);
19422
19423 /* Translate X to frame coordinates. Set last_x to the right
19424 end of the drawing area. */
19425 if (row->full_width_p)
19426 {
19427 /* X is relative to the left edge of W, without scroll bars
19428 or fringes. */
19429 x += WINDOW_LEFT_EDGE_X (w);
19430 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19431 }
19432 else
19433 {
19434 int area_left = window_box_left (w, area);
19435 x += area_left;
19436 area_width = window_box_width (w, area);
19437 last_x = area_left + area_width;
19438 }
19439
19440 /* Build a doubly-linked list of glyph_string structures between
19441 head and tail from what we have to draw. Note that the macro
19442 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19443 the reason we use a separate variable `i'. */
19444 i = start;
19445 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19446 if (tail)
19447 x_reached = tail->x + tail->background_width;
19448 else
19449 x_reached = x;
19450
19451 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19452 the row, redraw some glyphs in front or following the glyph
19453 strings built above. */
19454 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19455 {
19456 int dummy_x = 0;
19457 struct glyph_string *h, *t;
19458
19459 /* Compute overhangs for all glyph strings. */
19460 if (rif->compute_glyph_string_overhangs)
19461 for (s = head; s; s = s->next)
19462 rif->compute_glyph_string_overhangs (s);
19463
19464 /* Prepend glyph strings for glyphs in front of the first glyph
19465 string that are overwritten because of the first glyph
19466 string's left overhang. The background of all strings
19467 prepended must be drawn because the first glyph string
19468 draws over it. */
19469 i = left_overwritten (head);
19470 if (i >= 0)
19471 {
19472 j = i;
19473 BUILD_GLYPH_STRINGS (j, start, h, t,
19474 DRAW_NORMAL_TEXT, dummy_x, last_x);
19475 start = i;
19476 compute_overhangs_and_x (t, head->x, 1);
19477 prepend_glyph_string_lists (&head, &tail, h, t);
19478 clip_head = head;
19479 }
19480
19481 /* Prepend glyph strings for glyphs in front of the first glyph
19482 string that overwrite that glyph string because of their
19483 right overhang. For these strings, only the foreground must
19484 be drawn, because it draws over the glyph string at `head'.
19485 The background must not be drawn because this would overwrite
19486 right overhangs of preceding glyphs for which no glyph
19487 strings exist. */
19488 i = left_overwriting (head);
19489 if (i >= 0)
19490 {
19491 clip_head = head;
19492 BUILD_GLYPH_STRINGS (i, start, h, t,
19493 DRAW_NORMAL_TEXT, dummy_x, last_x);
19494 for (s = h; s; s = s->next)
19495 s->background_filled_p = 1;
19496 compute_overhangs_and_x (t, head->x, 1);
19497 prepend_glyph_string_lists (&head, &tail, h, t);
19498 }
19499
19500 /* Append glyphs strings for glyphs following the last glyph
19501 string tail that are overwritten by tail. The background of
19502 these strings has to be drawn because tail's foreground draws
19503 over it. */
19504 i = right_overwritten (tail);
19505 if (i >= 0)
19506 {
19507 BUILD_GLYPH_STRINGS (end, i, h, t,
19508 DRAW_NORMAL_TEXT, x, last_x);
19509 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19510 append_glyph_string_lists (&head, &tail, h, t);
19511 clip_tail = tail;
19512 }
19513
19514 /* Append glyph strings for glyphs following the last glyph
19515 string tail that overwrite tail. The foreground of such
19516 glyphs has to be drawn because it writes into the background
19517 of tail. The background must not be drawn because it could
19518 paint over the foreground of following glyphs. */
19519 i = right_overwriting (tail);
19520 if (i >= 0)
19521 {
19522 clip_tail = tail;
19523 BUILD_GLYPH_STRINGS (end, i, h, t,
19524 DRAW_NORMAL_TEXT, x, last_x);
19525 for (s = h; s; s = s->next)
19526 s->background_filled_p = 1;
19527 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19528 append_glyph_string_lists (&head, &tail, h, t);
19529 }
19530 if (clip_head || clip_tail)
19531 for (s = head; s; s = s->next)
19532 {
19533 s->clip_head = clip_head;
19534 s->clip_tail = clip_tail;
19535 }
19536 }
19537
19538 /* Draw all strings. */
19539 for (s = head; s; s = s->next)
19540 rif->draw_glyph_string (s);
19541
19542 if (area == TEXT_AREA
19543 && !row->full_width_p
19544 /* When drawing overlapping rows, only the glyph strings'
19545 foreground is drawn, which doesn't erase a cursor
19546 completely. */
19547 && !overlaps)
19548 {
19549 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19550 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19551 : (tail ? tail->x + tail->background_width : x));
19552
19553 int text_left = window_box_left (w, TEXT_AREA);
19554 x0 -= text_left;
19555 x1 -= text_left;
19556
19557 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19558 row->y, MATRIX_ROW_BOTTOM_Y (row));
19559 }
19560
19561 /* Value is the x-position up to which drawn, relative to AREA of W.
19562 This doesn't include parts drawn because of overhangs. */
19563 if (row->full_width_p)
19564 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19565 else
19566 x_reached -= window_box_left (w, area);
19567
19568 RELEASE_HDC (hdc, f);
19569
19570 return x_reached;
19571 }
19572
19573 /* Expand row matrix if too narrow. Don't expand if area
19574 is not present. */
19575
19576 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19577 { \
19578 if (!fonts_changed_p \
19579 && (it->glyph_row->glyphs[area] \
19580 < it->glyph_row->glyphs[area + 1])) \
19581 { \
19582 it->w->ncols_scale_factor++; \
19583 fonts_changed_p = 1; \
19584 } \
19585 }
19586
19587 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19588 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19589
19590 static INLINE void
19591 append_glyph (it)
19592 struct it *it;
19593 {
19594 struct glyph *glyph;
19595 enum glyph_row_area area = it->area;
19596
19597 xassert (it->glyph_row);
19598 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19599
19600 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19601 if (glyph < it->glyph_row->glyphs[area + 1])
19602 {
19603 glyph->charpos = CHARPOS (it->position);
19604 glyph->object = it->object;
19605 glyph->pixel_width = it->pixel_width;
19606 glyph->ascent = it->ascent;
19607 glyph->descent = it->descent;
19608 glyph->voffset = it->voffset;
19609 glyph->type = CHAR_GLYPH;
19610 glyph->multibyte_p = it->multibyte_p;
19611 glyph->left_box_line_p = it->start_of_box_run_p;
19612 glyph->right_box_line_p = it->end_of_box_run_p;
19613 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19614 || it->phys_descent > it->descent);
19615 glyph->padding_p = 0;
19616 glyph->glyph_not_available_p = it->glyph_not_available_p;
19617 glyph->face_id = it->face_id;
19618 glyph->u.ch = it->char_to_display;
19619 glyph->slice = null_glyph_slice;
19620 glyph->font_type = FONT_TYPE_UNKNOWN;
19621 ++it->glyph_row->used[area];
19622 }
19623 else
19624 IT_EXPAND_MATRIX_WIDTH (it, area);
19625 }
19626
19627 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19628 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19629
19630 static INLINE void
19631 append_composite_glyph (it)
19632 struct it *it;
19633 {
19634 struct glyph *glyph;
19635 enum glyph_row_area area = it->area;
19636
19637 xassert (it->glyph_row);
19638
19639 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19640 if (glyph < it->glyph_row->glyphs[area + 1])
19641 {
19642 glyph->charpos = CHARPOS (it->position);
19643 glyph->object = it->object;
19644 glyph->pixel_width = it->pixel_width;
19645 glyph->ascent = it->ascent;
19646 glyph->descent = it->descent;
19647 glyph->voffset = it->voffset;
19648 glyph->type = COMPOSITE_GLYPH;
19649 glyph->multibyte_p = it->multibyte_p;
19650 glyph->left_box_line_p = it->start_of_box_run_p;
19651 glyph->right_box_line_p = it->end_of_box_run_p;
19652 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19653 || it->phys_descent > it->descent);
19654 glyph->padding_p = 0;
19655 glyph->glyph_not_available_p = 0;
19656 glyph->face_id = it->face_id;
19657 glyph->u.cmp_id = it->cmp_id;
19658 glyph->slice = null_glyph_slice;
19659 glyph->font_type = FONT_TYPE_UNKNOWN;
19660 ++it->glyph_row->used[area];
19661 }
19662 else
19663 IT_EXPAND_MATRIX_WIDTH (it, area);
19664 }
19665
19666
19667 /* Change IT->ascent and IT->height according to the setting of
19668 IT->voffset. */
19669
19670 static INLINE void
19671 take_vertical_position_into_account (it)
19672 struct it *it;
19673 {
19674 if (it->voffset)
19675 {
19676 if (it->voffset < 0)
19677 /* Increase the ascent so that we can display the text higher
19678 in the line. */
19679 it->ascent -= it->voffset;
19680 else
19681 /* Increase the descent so that we can display the text lower
19682 in the line. */
19683 it->descent += it->voffset;
19684 }
19685 }
19686
19687
19688 /* Produce glyphs/get display metrics for the image IT is loaded with.
19689 See the description of struct display_iterator in dispextern.h for
19690 an overview of struct display_iterator. */
19691
19692 static void
19693 produce_image_glyph (it)
19694 struct it *it;
19695 {
19696 struct image *img;
19697 struct face *face;
19698 int glyph_ascent;
19699 struct glyph_slice slice;
19700
19701 xassert (it->what == IT_IMAGE);
19702
19703 face = FACE_FROM_ID (it->f, it->face_id);
19704 xassert (face);
19705 /* Make sure X resources of the face is loaded. */
19706 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19707
19708 if (it->image_id < 0)
19709 {
19710 /* Fringe bitmap. */
19711 it->ascent = it->phys_ascent = 0;
19712 it->descent = it->phys_descent = 0;
19713 it->pixel_width = 0;
19714 it->nglyphs = 0;
19715 return;
19716 }
19717
19718 img = IMAGE_FROM_ID (it->f, it->image_id);
19719 xassert (img);
19720 /* Make sure X resources of the image is loaded. */
19721 prepare_image_for_display (it->f, img);
19722
19723 slice.x = slice.y = 0;
19724 slice.width = img->width;
19725 slice.height = img->height;
19726
19727 if (INTEGERP (it->slice.x))
19728 slice.x = XINT (it->slice.x);
19729 else if (FLOATP (it->slice.x))
19730 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19731
19732 if (INTEGERP (it->slice.y))
19733 slice.y = XINT (it->slice.y);
19734 else if (FLOATP (it->slice.y))
19735 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19736
19737 if (INTEGERP (it->slice.width))
19738 slice.width = XINT (it->slice.width);
19739 else if (FLOATP (it->slice.width))
19740 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19741
19742 if (INTEGERP (it->slice.height))
19743 slice.height = XINT (it->slice.height);
19744 else if (FLOATP (it->slice.height))
19745 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19746
19747 if (slice.x >= img->width)
19748 slice.x = img->width;
19749 if (slice.y >= img->height)
19750 slice.y = img->height;
19751 if (slice.x + slice.width >= img->width)
19752 slice.width = img->width - slice.x;
19753 if (slice.y + slice.height > img->height)
19754 slice.height = img->height - slice.y;
19755
19756 if (slice.width == 0 || slice.height == 0)
19757 return;
19758
19759 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19760
19761 it->descent = slice.height - glyph_ascent;
19762 if (slice.y == 0)
19763 it->descent += img->vmargin;
19764 if (slice.y + slice.height == img->height)
19765 it->descent += img->vmargin;
19766 it->phys_descent = it->descent;
19767
19768 it->pixel_width = slice.width;
19769 if (slice.x == 0)
19770 it->pixel_width += img->hmargin;
19771 if (slice.x + slice.width == img->width)
19772 it->pixel_width += img->hmargin;
19773
19774 /* It's quite possible for images to have an ascent greater than
19775 their height, so don't get confused in that case. */
19776 if (it->descent < 0)
19777 it->descent = 0;
19778
19779 #if 0 /* this breaks image tiling */
19780 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19781 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19782 if (face_ascent > it->ascent)
19783 it->ascent = it->phys_ascent = face_ascent;
19784 #endif
19785
19786 it->nglyphs = 1;
19787
19788 if (face->box != FACE_NO_BOX)
19789 {
19790 if (face->box_line_width > 0)
19791 {
19792 if (slice.y == 0)
19793 it->ascent += face->box_line_width;
19794 if (slice.y + slice.height == img->height)
19795 it->descent += face->box_line_width;
19796 }
19797
19798 if (it->start_of_box_run_p && slice.x == 0)
19799 it->pixel_width += abs (face->box_line_width);
19800 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19801 it->pixel_width += abs (face->box_line_width);
19802 }
19803
19804 take_vertical_position_into_account (it);
19805
19806 if (it->glyph_row)
19807 {
19808 struct glyph *glyph;
19809 enum glyph_row_area area = it->area;
19810
19811 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19812 if (glyph < it->glyph_row->glyphs[area + 1])
19813 {
19814 glyph->charpos = CHARPOS (it->position);
19815 glyph->object = it->object;
19816 glyph->pixel_width = it->pixel_width;
19817 glyph->ascent = glyph_ascent;
19818 glyph->descent = it->descent;
19819 glyph->voffset = it->voffset;
19820 glyph->type = IMAGE_GLYPH;
19821 glyph->multibyte_p = it->multibyte_p;
19822 glyph->left_box_line_p = it->start_of_box_run_p;
19823 glyph->right_box_line_p = it->end_of_box_run_p;
19824 glyph->overlaps_vertically_p = 0;
19825 glyph->padding_p = 0;
19826 glyph->glyph_not_available_p = 0;
19827 glyph->face_id = it->face_id;
19828 glyph->u.img_id = img->id;
19829 glyph->slice = slice;
19830 glyph->font_type = FONT_TYPE_UNKNOWN;
19831 ++it->glyph_row->used[area];
19832 }
19833 else
19834 IT_EXPAND_MATRIX_WIDTH (it, area);
19835 }
19836 }
19837
19838
19839 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19840 of the glyph, WIDTH and HEIGHT are the width and height of the
19841 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19842
19843 static void
19844 append_stretch_glyph (it, object, width, height, ascent)
19845 struct it *it;
19846 Lisp_Object object;
19847 int width, height;
19848 int ascent;
19849 {
19850 struct glyph *glyph;
19851 enum glyph_row_area area = it->area;
19852
19853 xassert (ascent >= 0 && ascent <= height);
19854
19855 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19856 if (glyph < it->glyph_row->glyphs[area + 1])
19857 {
19858 glyph->charpos = CHARPOS (it->position);
19859 glyph->object = object;
19860 glyph->pixel_width = width;
19861 glyph->ascent = ascent;
19862 glyph->descent = height - ascent;
19863 glyph->voffset = it->voffset;
19864 glyph->type = STRETCH_GLYPH;
19865 glyph->multibyte_p = it->multibyte_p;
19866 glyph->left_box_line_p = it->start_of_box_run_p;
19867 glyph->right_box_line_p = it->end_of_box_run_p;
19868 glyph->overlaps_vertically_p = 0;
19869 glyph->padding_p = 0;
19870 glyph->glyph_not_available_p = 0;
19871 glyph->face_id = it->face_id;
19872 glyph->u.stretch.ascent = ascent;
19873 glyph->u.stretch.height = height;
19874 glyph->slice = null_glyph_slice;
19875 glyph->font_type = FONT_TYPE_UNKNOWN;
19876 ++it->glyph_row->used[area];
19877 }
19878 else
19879 IT_EXPAND_MATRIX_WIDTH (it, area);
19880 }
19881
19882
19883 /* Produce a stretch glyph for iterator IT. IT->object is the value
19884 of the glyph property displayed. The value must be a list
19885 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19886 being recognized:
19887
19888 1. `:width WIDTH' specifies that the space should be WIDTH *
19889 canonical char width wide. WIDTH may be an integer or floating
19890 point number.
19891
19892 2. `:relative-width FACTOR' specifies that the width of the stretch
19893 should be computed from the width of the first character having the
19894 `glyph' property, and should be FACTOR times that width.
19895
19896 3. `:align-to HPOS' specifies that the space should be wide enough
19897 to reach HPOS, a value in canonical character units.
19898
19899 Exactly one of the above pairs must be present.
19900
19901 4. `:height HEIGHT' specifies that the height of the stretch produced
19902 should be HEIGHT, measured in canonical character units.
19903
19904 5. `:relative-height FACTOR' specifies that the height of the
19905 stretch should be FACTOR times the height of the characters having
19906 the glyph property.
19907
19908 Either none or exactly one of 4 or 5 must be present.
19909
19910 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19911 of the stretch should be used for the ascent of the stretch.
19912 ASCENT must be in the range 0 <= ASCENT <= 100. */
19913
19914 static void
19915 produce_stretch_glyph (it)
19916 struct it *it;
19917 {
19918 /* (space :width WIDTH :height HEIGHT ...) */
19919 Lisp_Object prop, plist;
19920 int width = 0, height = 0, align_to = -1;
19921 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19922 int ascent = 0;
19923 double tem;
19924 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19925 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19926
19927 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19928
19929 /* List should start with `space'. */
19930 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19931 plist = XCDR (it->object);
19932
19933 /* Compute the width of the stretch. */
19934 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19935 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19936 {
19937 /* Absolute width `:width WIDTH' specified and valid. */
19938 zero_width_ok_p = 1;
19939 width = (int)tem;
19940 }
19941 else if (prop = Fplist_get (plist, QCrelative_width),
19942 NUMVAL (prop) > 0)
19943 {
19944 /* Relative width `:relative-width FACTOR' specified and valid.
19945 Compute the width of the characters having the `glyph'
19946 property. */
19947 struct it it2;
19948 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19949
19950 it2 = *it;
19951 if (it->multibyte_p)
19952 {
19953 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19954 - IT_BYTEPOS (*it));
19955 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19956 }
19957 else
19958 it2.c = *p, it2.len = 1;
19959
19960 it2.glyph_row = NULL;
19961 it2.what = IT_CHARACTER;
19962 x_produce_glyphs (&it2);
19963 width = NUMVAL (prop) * it2.pixel_width;
19964 }
19965 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19966 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19967 {
19968 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19969 align_to = (align_to < 0
19970 ? 0
19971 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19972 else if (align_to < 0)
19973 align_to = window_box_left_offset (it->w, TEXT_AREA);
19974 width = max (0, (int)tem + align_to - it->current_x);
19975 zero_width_ok_p = 1;
19976 }
19977 else
19978 /* Nothing specified -> width defaults to canonical char width. */
19979 width = FRAME_COLUMN_WIDTH (it->f);
19980
19981 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19982 width = 1;
19983
19984 /* Compute height. */
19985 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19986 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19987 {
19988 height = (int)tem;
19989 zero_height_ok_p = 1;
19990 }
19991 else if (prop = Fplist_get (plist, QCrelative_height),
19992 NUMVAL (prop) > 0)
19993 height = FONT_HEIGHT (font) * NUMVAL (prop);
19994 else
19995 height = FONT_HEIGHT (font);
19996
19997 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19998 height = 1;
19999
20000 /* Compute percentage of height used for ascent. If
20001 `:ascent ASCENT' is present and valid, use that. Otherwise,
20002 derive the ascent from the font in use. */
20003 if (prop = Fplist_get (plist, QCascent),
20004 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20005 ascent = height * NUMVAL (prop) / 100.0;
20006 else if (!NILP (prop)
20007 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20008 ascent = min (max (0, (int)tem), height);
20009 else
20010 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20011
20012 if (width > 0 && !it->truncate_lines_p
20013 && it->current_x + width > it->last_visible_x)
20014 width = it->last_visible_x - it->current_x - 1;
20015
20016 if (width > 0 && height > 0 && it->glyph_row)
20017 {
20018 Lisp_Object object = it->stack[it->sp - 1].string;
20019 if (!STRINGP (object))
20020 object = it->w->buffer;
20021 append_stretch_glyph (it, object, width, height, ascent);
20022 }
20023
20024 it->pixel_width = width;
20025 it->ascent = it->phys_ascent = ascent;
20026 it->descent = it->phys_descent = height - it->ascent;
20027 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20028
20029 take_vertical_position_into_account (it);
20030 }
20031
20032 /* Get line-height and line-spacing property at point.
20033 If line-height has format (HEIGHT TOTAL), return TOTAL
20034 in TOTAL_HEIGHT. */
20035
20036 static Lisp_Object
20037 get_line_height_property (it, prop)
20038 struct it *it;
20039 Lisp_Object prop;
20040 {
20041 Lisp_Object position;
20042
20043 if (STRINGP (it->object))
20044 position = make_number (IT_STRING_CHARPOS (*it));
20045 else if (BUFFERP (it->object))
20046 position = make_number (IT_CHARPOS (*it));
20047 else
20048 return Qnil;
20049
20050 return Fget_char_property (position, prop, it->object);
20051 }
20052
20053 /* Calculate line-height and line-spacing properties.
20054 An integer value specifies explicit pixel value.
20055 A float value specifies relative value to current face height.
20056 A cons (float . face-name) specifies relative value to
20057 height of specified face font.
20058
20059 Returns height in pixels, or nil. */
20060
20061
20062 static Lisp_Object
20063 calc_line_height_property (it, val, font, boff, override)
20064 struct it *it;
20065 Lisp_Object val;
20066 XFontStruct *font;
20067 int boff, override;
20068 {
20069 Lisp_Object face_name = Qnil;
20070 int ascent, descent, height;
20071
20072 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20073 return val;
20074
20075 if (CONSP (val))
20076 {
20077 face_name = XCAR (val);
20078 val = XCDR (val);
20079 if (!NUMBERP (val))
20080 val = make_number (1);
20081 if (NILP (face_name))
20082 {
20083 height = it->ascent + it->descent;
20084 goto scale;
20085 }
20086 }
20087
20088 if (NILP (face_name))
20089 {
20090 font = FRAME_FONT (it->f);
20091 boff = FRAME_BASELINE_OFFSET (it->f);
20092 }
20093 else if (EQ (face_name, Qt))
20094 {
20095 override = 0;
20096 }
20097 else
20098 {
20099 int face_id;
20100 struct face *face;
20101 struct font_info *font_info;
20102
20103 face_id = lookup_named_face (it->f, face_name, ' ', 0);
20104 if (face_id < 0)
20105 return make_number (-1);
20106
20107 face = FACE_FROM_ID (it->f, face_id);
20108 font = face->font;
20109 if (font == NULL)
20110 return make_number (-1);
20111
20112 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20113 boff = font_info->baseline_offset;
20114 if (font_info->vertical_centering)
20115 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20116 }
20117
20118 ascent = FONT_BASE (font) + boff;
20119 descent = FONT_DESCENT (font) - boff;
20120
20121 if (override)
20122 {
20123 it->override_ascent = ascent;
20124 it->override_descent = descent;
20125 it->override_boff = boff;
20126 }
20127
20128 height = ascent + descent;
20129
20130 scale:
20131 if (FLOATP (val))
20132 height = (int)(XFLOAT_DATA (val) * height);
20133 else if (INTEGERP (val))
20134 height *= XINT (val);
20135
20136 return make_number (height);
20137 }
20138
20139
20140 /* RIF:
20141 Produce glyphs/get display metrics for the display element IT is
20142 loaded with. See the description of struct it in dispextern.h
20143 for an overview of struct it. */
20144
20145 void
20146 x_produce_glyphs (it)
20147 struct it *it;
20148 {
20149 int extra_line_spacing = it->extra_line_spacing;
20150
20151 it->glyph_not_available_p = 0;
20152
20153 if (it->what == IT_CHARACTER)
20154 {
20155 XChar2b char2b;
20156 XFontStruct *font;
20157 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20158 XCharStruct *pcm;
20159 int font_not_found_p;
20160 struct font_info *font_info;
20161 int boff; /* baseline offset */
20162 /* We may change it->multibyte_p upon unibyte<->multibyte
20163 conversion. So, save the current value now and restore it
20164 later.
20165
20166 Note: It seems that we don't have to record multibyte_p in
20167 struct glyph because the character code itself tells if or
20168 not the character is multibyte. Thus, in the future, we must
20169 consider eliminating the field `multibyte_p' in the struct
20170 glyph. */
20171 int saved_multibyte_p = it->multibyte_p;
20172
20173 /* Maybe translate single-byte characters to multibyte, or the
20174 other way. */
20175 it->char_to_display = it->c;
20176 if (!ASCII_BYTE_P (it->c))
20177 {
20178 if (unibyte_display_via_language_environment
20179 && SINGLE_BYTE_CHAR_P (it->c)
20180 && (it->c >= 0240
20181 || !NILP (Vnonascii_translation_table)))
20182 {
20183 it->char_to_display = unibyte_char_to_multibyte (it->c);
20184 it->multibyte_p = 1;
20185 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20186 face = FACE_FROM_ID (it->f, it->face_id);
20187 }
20188 else if (!SINGLE_BYTE_CHAR_P (it->c)
20189 && !it->multibyte_p)
20190 {
20191 it->multibyte_p = 1;
20192 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20193 face = FACE_FROM_ID (it->f, it->face_id);
20194 }
20195 }
20196
20197 /* Get font to use. Encode IT->char_to_display. */
20198 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20199 &char2b, it->multibyte_p, 0);
20200 font = face->font;
20201
20202 /* When no suitable font found, use the default font. */
20203 font_not_found_p = font == NULL;
20204 if (font_not_found_p)
20205 {
20206 font = FRAME_FONT (it->f);
20207 boff = FRAME_BASELINE_OFFSET (it->f);
20208 font_info = NULL;
20209 }
20210 else
20211 {
20212 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20213 boff = font_info->baseline_offset;
20214 if (font_info->vertical_centering)
20215 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20216 }
20217
20218 if (it->char_to_display >= ' '
20219 && (!it->multibyte_p || it->char_to_display < 128))
20220 {
20221 /* Either unibyte or ASCII. */
20222 int stretched_p;
20223
20224 it->nglyphs = 1;
20225
20226 pcm = rif->per_char_metric (font, &char2b,
20227 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20228
20229 if (it->override_ascent >= 0)
20230 {
20231 it->ascent = it->override_ascent;
20232 it->descent = it->override_descent;
20233 boff = it->override_boff;
20234 }
20235 else
20236 {
20237 it->ascent = FONT_BASE (font) + boff;
20238 it->descent = FONT_DESCENT (font) - boff;
20239 }
20240
20241 if (pcm)
20242 {
20243 it->phys_ascent = pcm->ascent + boff;
20244 it->phys_descent = pcm->descent - boff;
20245 it->pixel_width = pcm->width;
20246 }
20247 else
20248 {
20249 it->glyph_not_available_p = 1;
20250 it->phys_ascent = it->ascent;
20251 it->phys_descent = it->descent;
20252 it->pixel_width = FONT_WIDTH (font);
20253 }
20254
20255 if (it->constrain_row_ascent_descent_p)
20256 {
20257 if (it->descent > it->max_descent)
20258 {
20259 it->ascent += it->descent - it->max_descent;
20260 it->descent = it->max_descent;
20261 }
20262 if (it->ascent > it->max_ascent)
20263 {
20264 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20265 it->ascent = it->max_ascent;
20266 }
20267 it->phys_ascent = min (it->phys_ascent, it->ascent);
20268 it->phys_descent = min (it->phys_descent, it->descent);
20269 extra_line_spacing = 0;
20270 }
20271
20272 /* If this is a space inside a region of text with
20273 `space-width' property, change its width. */
20274 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20275 if (stretched_p)
20276 it->pixel_width *= XFLOATINT (it->space_width);
20277
20278 /* If face has a box, add the box thickness to the character
20279 height. If character has a box line to the left and/or
20280 right, add the box line width to the character's width. */
20281 if (face->box != FACE_NO_BOX)
20282 {
20283 int thick = face->box_line_width;
20284
20285 if (thick > 0)
20286 {
20287 it->ascent += thick;
20288 it->descent += thick;
20289 }
20290 else
20291 thick = -thick;
20292
20293 if (it->start_of_box_run_p)
20294 it->pixel_width += thick;
20295 if (it->end_of_box_run_p)
20296 it->pixel_width += thick;
20297 }
20298
20299 /* If face has an overline, add the height of the overline
20300 (1 pixel) and a 1 pixel margin to the character height. */
20301 if (face->overline_p)
20302 it->ascent += 2;
20303
20304 if (it->constrain_row_ascent_descent_p)
20305 {
20306 if (it->ascent > it->max_ascent)
20307 it->ascent = it->max_ascent;
20308 if (it->descent > it->max_descent)
20309 it->descent = it->max_descent;
20310 }
20311
20312 take_vertical_position_into_account (it);
20313
20314 /* If we have to actually produce glyphs, do it. */
20315 if (it->glyph_row)
20316 {
20317 if (stretched_p)
20318 {
20319 /* Translate a space with a `space-width' property
20320 into a stretch glyph. */
20321 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20322 / FONT_HEIGHT (font));
20323 append_stretch_glyph (it, it->object, it->pixel_width,
20324 it->ascent + it->descent, ascent);
20325 }
20326 else
20327 append_glyph (it);
20328
20329 /* If characters with lbearing or rbearing are displayed
20330 in this line, record that fact in a flag of the
20331 glyph row. This is used to optimize X output code. */
20332 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20333 it->glyph_row->contains_overlapping_glyphs_p = 1;
20334 }
20335 }
20336 else if (it->char_to_display == '\n')
20337 {
20338 /* A newline has no width but we need the height of the line.
20339 But if previous part of the line set a height, don't
20340 increase that height */
20341
20342 Lisp_Object height;
20343 Lisp_Object total_height = Qnil;
20344
20345 it->override_ascent = -1;
20346 it->pixel_width = 0;
20347 it->nglyphs = 0;
20348
20349 height = get_line_height_property(it, Qline_height);
20350 /* Split (line-height total-height) list */
20351 if (CONSP (height)
20352 && CONSP (XCDR (height))
20353 && NILP (XCDR (XCDR (height))))
20354 {
20355 total_height = XCAR (XCDR (height));
20356 height = XCAR (height);
20357 }
20358 height = calc_line_height_property(it, height, font, boff, 1);
20359
20360 if (it->override_ascent >= 0)
20361 {
20362 it->ascent = it->override_ascent;
20363 it->descent = it->override_descent;
20364 boff = it->override_boff;
20365 }
20366 else
20367 {
20368 it->ascent = FONT_BASE (font) + boff;
20369 it->descent = FONT_DESCENT (font) - boff;
20370 }
20371
20372 if (EQ (height, Qt))
20373 {
20374 if (it->descent > it->max_descent)
20375 {
20376 it->ascent += it->descent - it->max_descent;
20377 it->descent = it->max_descent;
20378 }
20379 if (it->ascent > it->max_ascent)
20380 {
20381 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20382 it->ascent = it->max_ascent;
20383 }
20384 it->phys_ascent = min (it->phys_ascent, it->ascent);
20385 it->phys_descent = min (it->phys_descent, it->descent);
20386 it->constrain_row_ascent_descent_p = 1;
20387 extra_line_spacing = 0;
20388 }
20389 else
20390 {
20391 Lisp_Object spacing;
20392
20393 it->phys_ascent = it->ascent;
20394 it->phys_descent = it->descent;
20395
20396 if ((it->max_ascent > 0 || it->max_descent > 0)
20397 && face->box != FACE_NO_BOX
20398 && face->box_line_width > 0)
20399 {
20400 it->ascent += face->box_line_width;
20401 it->descent += face->box_line_width;
20402 }
20403 if (!NILP (height)
20404 && XINT (height) > it->ascent + it->descent)
20405 it->ascent = XINT (height) - it->descent;
20406
20407 if (!NILP (total_height))
20408 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20409 else
20410 {
20411 spacing = get_line_height_property(it, Qline_spacing);
20412 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20413 }
20414 if (INTEGERP (spacing))
20415 {
20416 extra_line_spacing = XINT (spacing);
20417 if (!NILP (total_height))
20418 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20419 }
20420 }
20421 }
20422 else if (it->char_to_display == '\t')
20423 {
20424 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20425 int x = it->current_x + it->continuation_lines_width;
20426 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20427
20428 /* If the distance from the current position to the next tab
20429 stop is less than a space character width, use the
20430 tab stop after that. */
20431 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20432 next_tab_x += tab_width;
20433
20434 it->pixel_width = next_tab_x - x;
20435 it->nglyphs = 1;
20436 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20437 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20438
20439 if (it->glyph_row)
20440 {
20441 append_stretch_glyph (it, it->object, it->pixel_width,
20442 it->ascent + it->descent, it->ascent);
20443 }
20444 }
20445 else
20446 {
20447 /* A multi-byte character. Assume that the display width of the
20448 character is the width of the character multiplied by the
20449 width of the font. */
20450
20451 /* If we found a font, this font should give us the right
20452 metrics. If we didn't find a font, use the frame's
20453 default font and calculate the width of the character
20454 from the charset width; this is what old redisplay code
20455 did. */
20456
20457 pcm = rif->per_char_metric (font, &char2b,
20458 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20459
20460 if (font_not_found_p || !pcm)
20461 {
20462 int charset = CHAR_CHARSET (it->char_to_display);
20463
20464 it->glyph_not_available_p = 1;
20465 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20466 * CHARSET_WIDTH (charset));
20467 it->phys_ascent = FONT_BASE (font) + boff;
20468 it->phys_descent = FONT_DESCENT (font) - boff;
20469 }
20470 else
20471 {
20472 it->pixel_width = pcm->width;
20473 it->phys_ascent = pcm->ascent + boff;
20474 it->phys_descent = pcm->descent - boff;
20475 if (it->glyph_row
20476 && (pcm->lbearing < 0
20477 || pcm->rbearing > pcm->width))
20478 it->glyph_row->contains_overlapping_glyphs_p = 1;
20479 }
20480 it->nglyphs = 1;
20481 it->ascent = FONT_BASE (font) + boff;
20482 it->descent = FONT_DESCENT (font) - boff;
20483 if (face->box != FACE_NO_BOX)
20484 {
20485 int thick = face->box_line_width;
20486
20487 if (thick > 0)
20488 {
20489 it->ascent += thick;
20490 it->descent += thick;
20491 }
20492 else
20493 thick = - thick;
20494
20495 if (it->start_of_box_run_p)
20496 it->pixel_width += thick;
20497 if (it->end_of_box_run_p)
20498 it->pixel_width += thick;
20499 }
20500
20501 /* If face has an overline, add the height of the overline
20502 (1 pixel) and a 1 pixel margin to the character height. */
20503 if (face->overline_p)
20504 it->ascent += 2;
20505
20506 take_vertical_position_into_account (it);
20507
20508 if (it->glyph_row)
20509 append_glyph (it);
20510 }
20511 it->multibyte_p = saved_multibyte_p;
20512 }
20513 else if (it->what == IT_COMPOSITION)
20514 {
20515 /* Note: A composition is represented as one glyph in the
20516 glyph matrix. There are no padding glyphs. */
20517 XChar2b char2b;
20518 XFontStruct *font;
20519 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20520 XCharStruct *pcm;
20521 int font_not_found_p;
20522 struct font_info *font_info;
20523 int boff; /* baseline offset */
20524 struct composition *cmp = composition_table[it->cmp_id];
20525
20526 /* Maybe translate single-byte characters to multibyte. */
20527 it->char_to_display = it->c;
20528 if (unibyte_display_via_language_environment
20529 && SINGLE_BYTE_CHAR_P (it->c)
20530 && (it->c >= 0240
20531 || (it->c >= 0200
20532 && !NILP (Vnonascii_translation_table))))
20533 {
20534 it->char_to_display = unibyte_char_to_multibyte (it->c);
20535 }
20536
20537 /* Get face and font to use. Encode IT->char_to_display. */
20538 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20539 face = FACE_FROM_ID (it->f, it->face_id);
20540 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20541 &char2b, it->multibyte_p, 0);
20542 font = face->font;
20543
20544 /* When no suitable font found, use the default font. */
20545 font_not_found_p = font == NULL;
20546 if (font_not_found_p)
20547 {
20548 font = FRAME_FONT (it->f);
20549 boff = FRAME_BASELINE_OFFSET (it->f);
20550 font_info = NULL;
20551 }
20552 else
20553 {
20554 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20555 boff = font_info->baseline_offset;
20556 if (font_info->vertical_centering)
20557 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20558 }
20559
20560 /* There are no padding glyphs, so there is only one glyph to
20561 produce for the composition. Important is that pixel_width,
20562 ascent and descent are the values of what is drawn by
20563 draw_glyphs (i.e. the values of the overall glyphs composed). */
20564 it->nglyphs = 1;
20565
20566 /* If we have not yet calculated pixel size data of glyphs of
20567 the composition for the current face font, calculate them
20568 now. Theoretically, we have to check all fonts for the
20569 glyphs, but that requires much time and memory space. So,
20570 here we check only the font of the first glyph. This leads
20571 to incorrect display very rarely, and C-l (recenter) can
20572 correct the display anyway. */
20573 if (cmp->font != (void *) font)
20574 {
20575 /* Ascent and descent of the font of the first character of
20576 this composition (adjusted by baseline offset). Ascent
20577 and descent of overall glyphs should not be less than
20578 them respectively. */
20579 int font_ascent = FONT_BASE (font) + boff;
20580 int font_descent = FONT_DESCENT (font) - boff;
20581 /* Bounding box of the overall glyphs. */
20582 int leftmost, rightmost, lowest, highest;
20583 int i, width, ascent, descent;
20584
20585 cmp->font = (void *) font;
20586
20587 /* Initialize the bounding box. */
20588 if (font_info
20589 && (pcm = rif->per_char_metric (font, &char2b,
20590 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20591 {
20592 width = pcm->width;
20593 ascent = pcm->ascent;
20594 descent = pcm->descent;
20595 }
20596 else
20597 {
20598 width = FONT_WIDTH (font);
20599 ascent = FONT_BASE (font);
20600 descent = FONT_DESCENT (font);
20601 }
20602
20603 rightmost = width;
20604 lowest = - descent + boff;
20605 highest = ascent + boff;
20606 leftmost = 0;
20607
20608 if (font_info
20609 && font_info->default_ascent
20610 && CHAR_TABLE_P (Vuse_default_ascent)
20611 && !NILP (Faref (Vuse_default_ascent,
20612 make_number (it->char_to_display))))
20613 highest = font_info->default_ascent + boff;
20614
20615 /* Draw the first glyph at the normal position. It may be
20616 shifted to right later if some other glyphs are drawn at
20617 the left. */
20618 cmp->offsets[0] = 0;
20619 cmp->offsets[1] = boff;
20620
20621 /* Set cmp->offsets for the remaining glyphs. */
20622 for (i = 1; i < cmp->glyph_len; i++)
20623 {
20624 int left, right, btm, top;
20625 int ch = COMPOSITION_GLYPH (cmp, i);
20626 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20627
20628 face = FACE_FROM_ID (it->f, face_id);
20629 get_char_face_and_encoding (it->f, ch, face->id,
20630 &char2b, it->multibyte_p, 0);
20631 font = face->font;
20632 if (font == NULL)
20633 {
20634 font = FRAME_FONT (it->f);
20635 boff = FRAME_BASELINE_OFFSET (it->f);
20636 font_info = NULL;
20637 }
20638 else
20639 {
20640 font_info
20641 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20642 boff = font_info->baseline_offset;
20643 if (font_info->vertical_centering)
20644 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20645 }
20646
20647 if (font_info
20648 && (pcm = rif->per_char_metric (font, &char2b,
20649 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20650 {
20651 width = pcm->width;
20652 ascent = pcm->ascent;
20653 descent = pcm->descent;
20654 }
20655 else
20656 {
20657 width = FONT_WIDTH (font);
20658 ascent = 1;
20659 descent = 0;
20660 }
20661
20662 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20663 {
20664 /* Relative composition with or without
20665 alternate chars. */
20666 left = (leftmost + rightmost - width) / 2;
20667 btm = - descent + boff;
20668 if (font_info && font_info->relative_compose
20669 && (! CHAR_TABLE_P (Vignore_relative_composition)
20670 || NILP (Faref (Vignore_relative_composition,
20671 make_number (ch)))))
20672 {
20673
20674 if (- descent >= font_info->relative_compose)
20675 /* One extra pixel between two glyphs. */
20676 btm = highest + 1;
20677 else if (ascent <= 0)
20678 /* One extra pixel between two glyphs. */
20679 btm = lowest - 1 - ascent - descent;
20680 }
20681 }
20682 else
20683 {
20684 /* A composition rule is specified by an integer
20685 value that encodes global and new reference
20686 points (GREF and NREF). GREF and NREF are
20687 specified by numbers as below:
20688
20689 0---1---2 -- ascent
20690 | |
20691 | |
20692 | |
20693 9--10--11 -- center
20694 | |
20695 ---3---4---5--- baseline
20696 | |
20697 6---7---8 -- descent
20698 */
20699 int rule = COMPOSITION_RULE (cmp, i);
20700 int gref, nref, grefx, grefy, nrefx, nrefy;
20701
20702 COMPOSITION_DECODE_RULE (rule, gref, nref);
20703 grefx = gref % 3, nrefx = nref % 3;
20704 grefy = gref / 3, nrefy = nref / 3;
20705
20706 left = (leftmost
20707 + grefx * (rightmost - leftmost) / 2
20708 - nrefx * width / 2);
20709 btm = ((grefy == 0 ? highest
20710 : grefy == 1 ? 0
20711 : grefy == 2 ? lowest
20712 : (highest + lowest) / 2)
20713 - (nrefy == 0 ? ascent + descent
20714 : nrefy == 1 ? descent - boff
20715 : nrefy == 2 ? 0
20716 : (ascent + descent) / 2));
20717 }
20718
20719 cmp->offsets[i * 2] = left;
20720 cmp->offsets[i * 2 + 1] = btm + descent;
20721
20722 /* Update the bounding box of the overall glyphs. */
20723 right = left + width;
20724 top = btm + descent + ascent;
20725 if (left < leftmost)
20726 leftmost = left;
20727 if (right > rightmost)
20728 rightmost = right;
20729 if (top > highest)
20730 highest = top;
20731 if (btm < lowest)
20732 lowest = btm;
20733 }
20734
20735 /* If there are glyphs whose x-offsets are negative,
20736 shift all glyphs to the right and make all x-offsets
20737 non-negative. */
20738 if (leftmost < 0)
20739 {
20740 for (i = 0; i < cmp->glyph_len; i++)
20741 cmp->offsets[i * 2] -= leftmost;
20742 rightmost -= leftmost;
20743 }
20744
20745 cmp->pixel_width = rightmost;
20746 cmp->ascent = highest;
20747 cmp->descent = - lowest;
20748 if (cmp->ascent < font_ascent)
20749 cmp->ascent = font_ascent;
20750 if (cmp->descent < font_descent)
20751 cmp->descent = font_descent;
20752 }
20753
20754 it->pixel_width = cmp->pixel_width;
20755 it->ascent = it->phys_ascent = cmp->ascent;
20756 it->descent = it->phys_descent = cmp->descent;
20757
20758 if (face->box != FACE_NO_BOX)
20759 {
20760 int thick = face->box_line_width;
20761
20762 if (thick > 0)
20763 {
20764 it->ascent += thick;
20765 it->descent += thick;
20766 }
20767 else
20768 thick = - thick;
20769
20770 if (it->start_of_box_run_p)
20771 it->pixel_width += thick;
20772 if (it->end_of_box_run_p)
20773 it->pixel_width += thick;
20774 }
20775
20776 /* If face has an overline, add the height of the overline
20777 (1 pixel) and a 1 pixel margin to the character height. */
20778 if (face->overline_p)
20779 it->ascent += 2;
20780
20781 take_vertical_position_into_account (it);
20782
20783 if (it->glyph_row)
20784 append_composite_glyph (it);
20785 }
20786 else if (it->what == IT_IMAGE)
20787 produce_image_glyph (it);
20788 else if (it->what == IT_STRETCH)
20789 produce_stretch_glyph (it);
20790
20791 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20792 because this isn't true for images with `:ascent 100'. */
20793 xassert (it->ascent >= 0 && it->descent >= 0);
20794 if (it->area == TEXT_AREA)
20795 it->current_x += it->pixel_width;
20796
20797 if (extra_line_spacing > 0)
20798 {
20799 it->descent += extra_line_spacing;
20800 if (extra_line_spacing > it->max_extra_line_spacing)
20801 it->max_extra_line_spacing = extra_line_spacing;
20802 }
20803
20804 it->max_ascent = max (it->max_ascent, it->ascent);
20805 it->max_descent = max (it->max_descent, it->descent);
20806 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20807 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20808 }
20809
20810 /* EXPORT for RIF:
20811 Output LEN glyphs starting at START at the nominal cursor position.
20812 Advance the nominal cursor over the text. The global variable
20813 updated_window contains the window being updated, updated_row is
20814 the glyph row being updated, and updated_area is the area of that
20815 row being updated. */
20816
20817 void
20818 x_write_glyphs (start, len)
20819 struct glyph *start;
20820 int len;
20821 {
20822 int x, hpos;
20823
20824 xassert (updated_window && updated_row);
20825 BLOCK_INPUT;
20826
20827 /* Write glyphs. */
20828
20829 hpos = start - updated_row->glyphs[updated_area];
20830 x = draw_glyphs (updated_window, output_cursor.x,
20831 updated_row, updated_area,
20832 hpos, hpos + len,
20833 DRAW_NORMAL_TEXT, 0);
20834
20835 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20836 if (updated_area == TEXT_AREA
20837 && updated_window->phys_cursor_on_p
20838 && updated_window->phys_cursor.vpos == output_cursor.vpos
20839 && updated_window->phys_cursor.hpos >= hpos
20840 && updated_window->phys_cursor.hpos < hpos + len)
20841 updated_window->phys_cursor_on_p = 0;
20842
20843 UNBLOCK_INPUT;
20844
20845 /* Advance the output cursor. */
20846 output_cursor.hpos += len;
20847 output_cursor.x = x;
20848 }
20849
20850
20851 /* EXPORT for RIF:
20852 Insert LEN glyphs from START at the nominal cursor position. */
20853
20854 void
20855 x_insert_glyphs (start, len)
20856 struct glyph *start;
20857 int len;
20858 {
20859 struct frame *f;
20860 struct window *w;
20861 int line_height, shift_by_width, shifted_region_width;
20862 struct glyph_row *row;
20863 struct glyph *glyph;
20864 int frame_x, frame_y, hpos;
20865
20866 xassert (updated_window && updated_row);
20867 BLOCK_INPUT;
20868 w = updated_window;
20869 f = XFRAME (WINDOW_FRAME (w));
20870
20871 /* Get the height of the line we are in. */
20872 row = updated_row;
20873 line_height = row->height;
20874
20875 /* Get the width of the glyphs to insert. */
20876 shift_by_width = 0;
20877 for (glyph = start; glyph < start + len; ++glyph)
20878 shift_by_width += glyph->pixel_width;
20879
20880 /* Get the width of the region to shift right. */
20881 shifted_region_width = (window_box_width (w, updated_area)
20882 - output_cursor.x
20883 - shift_by_width);
20884
20885 /* Shift right. */
20886 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20887 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20888
20889 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20890 line_height, shift_by_width);
20891
20892 /* Write the glyphs. */
20893 hpos = start - row->glyphs[updated_area];
20894 draw_glyphs (w, output_cursor.x, row, updated_area,
20895 hpos, hpos + len,
20896 DRAW_NORMAL_TEXT, 0);
20897
20898 /* Advance the output cursor. */
20899 output_cursor.hpos += len;
20900 output_cursor.x += shift_by_width;
20901 UNBLOCK_INPUT;
20902 }
20903
20904
20905 /* EXPORT for RIF:
20906 Erase the current text line from the nominal cursor position
20907 (inclusive) to pixel column TO_X (exclusive). The idea is that
20908 everything from TO_X onward is already erased.
20909
20910 TO_X is a pixel position relative to updated_area of
20911 updated_window. TO_X == -1 means clear to the end of this area. */
20912
20913 void
20914 x_clear_end_of_line (to_x)
20915 int to_x;
20916 {
20917 struct frame *f;
20918 struct window *w = updated_window;
20919 int max_x, min_y, max_y;
20920 int from_x, from_y, to_y;
20921
20922 xassert (updated_window && updated_row);
20923 f = XFRAME (w->frame);
20924
20925 if (updated_row->full_width_p)
20926 max_x = WINDOW_TOTAL_WIDTH (w);
20927 else
20928 max_x = window_box_width (w, updated_area);
20929 max_y = window_text_bottom_y (w);
20930
20931 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20932 of window. For TO_X > 0, truncate to end of drawing area. */
20933 if (to_x == 0)
20934 return;
20935 else if (to_x < 0)
20936 to_x = max_x;
20937 else
20938 to_x = min (to_x, max_x);
20939
20940 to_y = min (max_y, output_cursor.y + updated_row->height);
20941
20942 /* Notice if the cursor will be cleared by this operation. */
20943 if (!updated_row->full_width_p)
20944 notice_overwritten_cursor (w, updated_area,
20945 output_cursor.x, -1,
20946 updated_row->y,
20947 MATRIX_ROW_BOTTOM_Y (updated_row));
20948
20949 from_x = output_cursor.x;
20950
20951 /* Translate to frame coordinates. */
20952 if (updated_row->full_width_p)
20953 {
20954 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20955 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20956 }
20957 else
20958 {
20959 int area_left = window_box_left (w, updated_area);
20960 from_x += area_left;
20961 to_x += area_left;
20962 }
20963
20964 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20965 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20966 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20967
20968 /* Prevent inadvertently clearing to end of the X window. */
20969 if (to_x > from_x && to_y > from_y)
20970 {
20971 BLOCK_INPUT;
20972 rif->clear_frame_area (f, from_x, from_y,
20973 to_x - from_x, to_y - from_y);
20974 UNBLOCK_INPUT;
20975 }
20976 }
20977
20978 #endif /* HAVE_WINDOW_SYSTEM */
20979
20980
20981 \f
20982 /***********************************************************************
20983 Cursor types
20984 ***********************************************************************/
20985
20986 /* Value is the internal representation of the specified cursor type
20987 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20988 of the bar cursor. */
20989
20990 static enum text_cursor_kinds
20991 get_specified_cursor_type (arg, width)
20992 Lisp_Object arg;
20993 int *width;
20994 {
20995 enum text_cursor_kinds type;
20996
20997 if (NILP (arg))
20998 return NO_CURSOR;
20999
21000 if (EQ (arg, Qbox))
21001 return FILLED_BOX_CURSOR;
21002
21003 if (EQ (arg, Qhollow))
21004 return HOLLOW_BOX_CURSOR;
21005
21006 if (EQ (arg, Qbar))
21007 {
21008 *width = 2;
21009 return BAR_CURSOR;
21010 }
21011
21012 if (CONSP (arg)
21013 && EQ (XCAR (arg), Qbar)
21014 && INTEGERP (XCDR (arg))
21015 && XINT (XCDR (arg)) >= 0)
21016 {
21017 *width = XINT (XCDR (arg));
21018 return BAR_CURSOR;
21019 }
21020
21021 if (EQ (arg, Qhbar))
21022 {
21023 *width = 2;
21024 return HBAR_CURSOR;
21025 }
21026
21027 if (CONSP (arg)
21028 && EQ (XCAR (arg), Qhbar)
21029 && INTEGERP (XCDR (arg))
21030 && XINT (XCDR (arg)) >= 0)
21031 {
21032 *width = XINT (XCDR (arg));
21033 return HBAR_CURSOR;
21034 }
21035
21036 /* Treat anything unknown as "hollow box cursor".
21037 It was bad to signal an error; people have trouble fixing
21038 .Xdefaults with Emacs, when it has something bad in it. */
21039 type = HOLLOW_BOX_CURSOR;
21040
21041 return type;
21042 }
21043
21044 /* Set the default cursor types for specified frame. */
21045 void
21046 set_frame_cursor_types (f, arg)
21047 struct frame *f;
21048 Lisp_Object arg;
21049 {
21050 int width;
21051 Lisp_Object tem;
21052
21053 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21054 FRAME_CURSOR_WIDTH (f) = width;
21055
21056 /* By default, set up the blink-off state depending on the on-state. */
21057
21058 tem = Fassoc (arg, Vblink_cursor_alist);
21059 if (!NILP (tem))
21060 {
21061 FRAME_BLINK_OFF_CURSOR (f)
21062 = get_specified_cursor_type (XCDR (tem), &width);
21063 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21064 }
21065 else
21066 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21067 }
21068
21069
21070 /* Return the cursor we want to be displayed in window W. Return
21071 width of bar/hbar cursor through WIDTH arg. Return with
21072 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21073 (i.e. if the `system caret' should track this cursor).
21074
21075 In a mini-buffer window, we want the cursor only to appear if we
21076 are reading input from this window. For the selected window, we
21077 want the cursor type given by the frame parameter or buffer local
21078 setting of cursor-type. If explicitly marked off, draw no cursor.
21079 In all other cases, we want a hollow box cursor. */
21080
21081 static enum text_cursor_kinds
21082 get_window_cursor_type (w, glyph, width, active_cursor)
21083 struct window *w;
21084 struct glyph *glyph;
21085 int *width;
21086 int *active_cursor;
21087 {
21088 struct frame *f = XFRAME (w->frame);
21089 struct buffer *b = XBUFFER (w->buffer);
21090 int cursor_type = DEFAULT_CURSOR;
21091 Lisp_Object alt_cursor;
21092 int non_selected = 0;
21093
21094 *active_cursor = 1;
21095
21096 /* Echo area */
21097 if (cursor_in_echo_area
21098 && FRAME_HAS_MINIBUF_P (f)
21099 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21100 {
21101 if (w == XWINDOW (echo_area_window))
21102 {
21103 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21104 {
21105 *width = FRAME_CURSOR_WIDTH (f);
21106 return FRAME_DESIRED_CURSOR (f);
21107 }
21108 else
21109 return get_specified_cursor_type (b->cursor_type, width);
21110 }
21111
21112 *active_cursor = 0;
21113 non_selected = 1;
21114 }
21115
21116 /* Nonselected window or nonselected frame. */
21117 else if (w != XWINDOW (f->selected_window)
21118 #ifdef HAVE_WINDOW_SYSTEM
21119 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21120 #endif
21121 )
21122 {
21123 *active_cursor = 0;
21124
21125 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21126 return NO_CURSOR;
21127
21128 non_selected = 1;
21129 }
21130
21131 /* Never display a cursor in a window in which cursor-type is nil. */
21132 if (NILP (b->cursor_type))
21133 return NO_CURSOR;
21134
21135 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
21136 if (non_selected)
21137 {
21138 alt_cursor = b->cursor_in_non_selected_windows;
21139 return get_specified_cursor_type (alt_cursor, width);
21140 }
21141
21142 /* Get the normal cursor type for this window. */
21143 if (EQ (b->cursor_type, Qt))
21144 {
21145 cursor_type = FRAME_DESIRED_CURSOR (f);
21146 *width = FRAME_CURSOR_WIDTH (f);
21147 }
21148 else
21149 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21150
21151 /* Use normal cursor if not blinked off. */
21152 if (!w->cursor_off_p)
21153 {
21154 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
21155 if (cursor_type == FILLED_BOX_CURSOR)
21156 cursor_type = HOLLOW_BOX_CURSOR;
21157 }
21158 return cursor_type;
21159 }
21160
21161 /* Cursor is blinked off, so determine how to "toggle" it. */
21162
21163 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21164 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21165 return get_specified_cursor_type (XCDR (alt_cursor), width);
21166
21167 /* Then see if frame has specified a specific blink off cursor type. */
21168 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21169 {
21170 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21171 return FRAME_BLINK_OFF_CURSOR (f);
21172 }
21173
21174 #if 0
21175 /* Some people liked having a permanently visible blinking cursor,
21176 while others had very strong opinions against it. So it was
21177 decided to remove it. KFS 2003-09-03 */
21178
21179 /* Finally perform built-in cursor blinking:
21180 filled box <-> hollow box
21181 wide [h]bar <-> narrow [h]bar
21182 narrow [h]bar <-> no cursor
21183 other type <-> no cursor */
21184
21185 if (cursor_type == FILLED_BOX_CURSOR)
21186 return HOLLOW_BOX_CURSOR;
21187
21188 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21189 {
21190 *width = 1;
21191 return cursor_type;
21192 }
21193 #endif
21194
21195 return NO_CURSOR;
21196 }
21197
21198
21199 #ifdef HAVE_WINDOW_SYSTEM
21200
21201 /* Notice when the text cursor of window W has been completely
21202 overwritten by a drawing operation that outputs glyphs in AREA
21203 starting at X0 and ending at X1 in the line starting at Y0 and
21204 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21205 the rest of the line after X0 has been written. Y coordinates
21206 are window-relative. */
21207
21208 static void
21209 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21210 struct window *w;
21211 enum glyph_row_area area;
21212 int x0, y0, x1, y1;
21213 {
21214 int cx0, cx1, cy0, cy1;
21215 struct glyph_row *row;
21216
21217 if (!w->phys_cursor_on_p)
21218 return;
21219 if (area != TEXT_AREA)
21220 return;
21221
21222 if (w->phys_cursor.vpos < 0
21223 || w->phys_cursor.vpos >= w->current_matrix->nrows
21224 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21225 !(row->enabled_p && row->displays_text_p)))
21226 return;
21227
21228 if (row->cursor_in_fringe_p)
21229 {
21230 row->cursor_in_fringe_p = 0;
21231 draw_fringe_bitmap (w, row, 0);
21232 w->phys_cursor_on_p = 0;
21233 return;
21234 }
21235
21236 cx0 = w->phys_cursor.x;
21237 cx1 = cx0 + w->phys_cursor_width;
21238 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21239 return;
21240
21241 /* The cursor image will be completely removed from the
21242 screen if the output area intersects the cursor area in
21243 y-direction. When we draw in [y0 y1[, and some part of
21244 the cursor is at y < y0, that part must have been drawn
21245 before. When scrolling, the cursor is erased before
21246 actually scrolling, so we don't come here. When not
21247 scrolling, the rows above the old cursor row must have
21248 changed, and in this case these rows must have written
21249 over the cursor image.
21250
21251 Likewise if part of the cursor is below y1, with the
21252 exception of the cursor being in the first blank row at
21253 the buffer and window end because update_text_area
21254 doesn't draw that row. (Except when it does, but
21255 that's handled in update_text_area.) */
21256
21257 cy0 = w->phys_cursor.y;
21258 cy1 = cy0 + w->phys_cursor_height;
21259 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21260 return;
21261
21262 w->phys_cursor_on_p = 0;
21263 }
21264
21265 #endif /* HAVE_WINDOW_SYSTEM */
21266
21267 \f
21268 /************************************************************************
21269 Mouse Face
21270 ************************************************************************/
21271
21272 #ifdef HAVE_WINDOW_SYSTEM
21273
21274 /* EXPORT for RIF:
21275 Fix the display of area AREA of overlapping row ROW in window W
21276 with respect to the overlapping part OVERLAPS. */
21277
21278 void
21279 x_fix_overlapping_area (w, row, area, overlaps)
21280 struct window *w;
21281 struct glyph_row *row;
21282 enum glyph_row_area area;
21283 int overlaps;
21284 {
21285 int i, x;
21286
21287 BLOCK_INPUT;
21288
21289 x = 0;
21290 for (i = 0; i < row->used[area];)
21291 {
21292 if (row->glyphs[area][i].overlaps_vertically_p)
21293 {
21294 int start = i, start_x = x;
21295
21296 do
21297 {
21298 x += row->glyphs[area][i].pixel_width;
21299 ++i;
21300 }
21301 while (i < row->used[area]
21302 && row->glyphs[area][i].overlaps_vertically_p);
21303
21304 draw_glyphs (w, start_x, row, area,
21305 start, i,
21306 DRAW_NORMAL_TEXT, overlaps);
21307 }
21308 else
21309 {
21310 x += row->glyphs[area][i].pixel_width;
21311 ++i;
21312 }
21313 }
21314
21315 UNBLOCK_INPUT;
21316 }
21317
21318
21319 /* EXPORT:
21320 Draw the cursor glyph of window W in glyph row ROW. See the
21321 comment of draw_glyphs for the meaning of HL. */
21322
21323 void
21324 draw_phys_cursor_glyph (w, row, hl)
21325 struct window *w;
21326 struct glyph_row *row;
21327 enum draw_glyphs_face hl;
21328 {
21329 /* If cursor hpos is out of bounds, don't draw garbage. This can
21330 happen in mini-buffer windows when switching between echo area
21331 glyphs and mini-buffer. */
21332 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21333 {
21334 int on_p = w->phys_cursor_on_p;
21335 int x1;
21336 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21337 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21338 hl, 0);
21339 w->phys_cursor_on_p = on_p;
21340
21341 if (hl == DRAW_CURSOR)
21342 w->phys_cursor_width = x1 - w->phys_cursor.x;
21343 /* When we erase the cursor, and ROW is overlapped by other
21344 rows, make sure that these overlapping parts of other rows
21345 are redrawn. */
21346 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21347 {
21348 w->phys_cursor_width = x1 - w->phys_cursor.x;
21349
21350 if (row > w->current_matrix->rows
21351 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21352 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21353 OVERLAPS_ERASED_CURSOR);
21354
21355 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21356 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21357 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21358 OVERLAPS_ERASED_CURSOR);
21359 }
21360 }
21361 }
21362
21363
21364 /* EXPORT:
21365 Erase the image of a cursor of window W from the screen. */
21366
21367 void
21368 erase_phys_cursor (w)
21369 struct window *w;
21370 {
21371 struct frame *f = XFRAME (w->frame);
21372 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21373 int hpos = w->phys_cursor.hpos;
21374 int vpos = w->phys_cursor.vpos;
21375 int mouse_face_here_p = 0;
21376 struct glyph_matrix *active_glyphs = w->current_matrix;
21377 struct glyph_row *cursor_row;
21378 struct glyph *cursor_glyph;
21379 enum draw_glyphs_face hl;
21380
21381 /* No cursor displayed or row invalidated => nothing to do on the
21382 screen. */
21383 if (w->phys_cursor_type == NO_CURSOR)
21384 goto mark_cursor_off;
21385
21386 /* VPOS >= active_glyphs->nrows means that window has been resized.
21387 Don't bother to erase the cursor. */
21388 if (vpos >= active_glyphs->nrows)
21389 goto mark_cursor_off;
21390
21391 /* If row containing cursor is marked invalid, there is nothing we
21392 can do. */
21393 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21394 if (!cursor_row->enabled_p)
21395 goto mark_cursor_off;
21396
21397 /* If line spacing is > 0, old cursor may only be partially visible in
21398 window after split-window. So adjust visible height. */
21399 cursor_row->visible_height = min (cursor_row->visible_height,
21400 window_text_bottom_y (w) - cursor_row->y);
21401
21402 /* If row is completely invisible, don't attempt to delete a cursor which
21403 isn't there. This can happen if cursor is at top of a window, and
21404 we switch to a buffer with a header line in that window. */
21405 if (cursor_row->visible_height <= 0)
21406 goto mark_cursor_off;
21407
21408 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21409 if (cursor_row->cursor_in_fringe_p)
21410 {
21411 cursor_row->cursor_in_fringe_p = 0;
21412 draw_fringe_bitmap (w, cursor_row, 0);
21413 goto mark_cursor_off;
21414 }
21415
21416 /* This can happen when the new row is shorter than the old one.
21417 In this case, either draw_glyphs or clear_end_of_line
21418 should have cleared the cursor. Note that we wouldn't be
21419 able to erase the cursor in this case because we don't have a
21420 cursor glyph at hand. */
21421 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21422 goto mark_cursor_off;
21423
21424 /* If the cursor is in the mouse face area, redisplay that when
21425 we clear the cursor. */
21426 if (! NILP (dpyinfo->mouse_face_window)
21427 && w == XWINDOW (dpyinfo->mouse_face_window)
21428 && (vpos > dpyinfo->mouse_face_beg_row
21429 || (vpos == dpyinfo->mouse_face_beg_row
21430 && hpos >= dpyinfo->mouse_face_beg_col))
21431 && (vpos < dpyinfo->mouse_face_end_row
21432 || (vpos == dpyinfo->mouse_face_end_row
21433 && hpos < dpyinfo->mouse_face_end_col))
21434 /* Don't redraw the cursor's spot in mouse face if it is at the
21435 end of a line (on a newline). The cursor appears there, but
21436 mouse highlighting does not. */
21437 && cursor_row->used[TEXT_AREA] > hpos)
21438 mouse_face_here_p = 1;
21439
21440 /* Maybe clear the display under the cursor. */
21441 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21442 {
21443 int x, y, left_x;
21444 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21445 int width;
21446
21447 cursor_glyph = get_phys_cursor_glyph (w);
21448 if (cursor_glyph == NULL)
21449 goto mark_cursor_off;
21450
21451 width = cursor_glyph->pixel_width;
21452 left_x = window_box_left_offset (w, TEXT_AREA);
21453 x = w->phys_cursor.x;
21454 if (x < left_x)
21455 width -= left_x - x;
21456 width = min (width, window_box_width (w, TEXT_AREA) - x);
21457 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21458 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21459
21460 if (width > 0)
21461 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21462 }
21463
21464 /* Erase the cursor by redrawing the character underneath it. */
21465 if (mouse_face_here_p)
21466 hl = DRAW_MOUSE_FACE;
21467 else
21468 hl = DRAW_NORMAL_TEXT;
21469 draw_phys_cursor_glyph (w, cursor_row, hl);
21470
21471 mark_cursor_off:
21472 w->phys_cursor_on_p = 0;
21473 w->phys_cursor_type = NO_CURSOR;
21474 }
21475
21476
21477 /* EXPORT:
21478 Display or clear cursor of window W. If ON is zero, clear the
21479 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21480 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21481
21482 void
21483 display_and_set_cursor (w, on, hpos, vpos, x, y)
21484 struct window *w;
21485 int on, hpos, vpos, x, y;
21486 {
21487 struct frame *f = XFRAME (w->frame);
21488 int new_cursor_type;
21489 int new_cursor_width;
21490 int active_cursor;
21491 struct glyph_row *glyph_row;
21492 struct glyph *glyph;
21493
21494 /* This is pointless on invisible frames, and dangerous on garbaged
21495 windows and frames; in the latter case, the frame or window may
21496 be in the midst of changing its size, and x and y may be off the
21497 window. */
21498 if (! FRAME_VISIBLE_P (f)
21499 || FRAME_GARBAGED_P (f)
21500 || vpos >= w->current_matrix->nrows
21501 || hpos >= w->current_matrix->matrix_w)
21502 return;
21503
21504 /* If cursor is off and we want it off, return quickly. */
21505 if (!on && !w->phys_cursor_on_p)
21506 return;
21507
21508 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21509 /* If cursor row is not enabled, we don't really know where to
21510 display the cursor. */
21511 if (!glyph_row->enabled_p)
21512 {
21513 w->phys_cursor_on_p = 0;
21514 return;
21515 }
21516
21517 glyph = NULL;
21518 if (!glyph_row->exact_window_width_line_p
21519 || hpos < glyph_row->used[TEXT_AREA])
21520 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21521
21522 xassert (interrupt_input_blocked);
21523
21524 /* Set new_cursor_type to the cursor we want to be displayed. */
21525 new_cursor_type = get_window_cursor_type (w, glyph,
21526 &new_cursor_width, &active_cursor);
21527
21528 /* If cursor is currently being shown and we don't want it to be or
21529 it is in the wrong place, or the cursor type is not what we want,
21530 erase it. */
21531 if (w->phys_cursor_on_p
21532 && (!on
21533 || w->phys_cursor.x != x
21534 || w->phys_cursor.y != y
21535 || new_cursor_type != w->phys_cursor_type
21536 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21537 && new_cursor_width != w->phys_cursor_width)))
21538 erase_phys_cursor (w);
21539
21540 /* Don't check phys_cursor_on_p here because that flag is only set
21541 to zero in some cases where we know that the cursor has been
21542 completely erased, to avoid the extra work of erasing the cursor
21543 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21544 still not be visible, or it has only been partly erased. */
21545 if (on)
21546 {
21547 w->phys_cursor_ascent = glyph_row->ascent;
21548 w->phys_cursor_height = glyph_row->height;
21549
21550 /* Set phys_cursor_.* before x_draw_.* is called because some
21551 of them may need the information. */
21552 w->phys_cursor.x = x;
21553 w->phys_cursor.y = glyph_row->y;
21554 w->phys_cursor.hpos = hpos;
21555 w->phys_cursor.vpos = vpos;
21556 }
21557
21558 rif->draw_window_cursor (w, glyph_row, x, y,
21559 new_cursor_type, new_cursor_width,
21560 on, active_cursor);
21561 }
21562
21563
21564 /* Switch the display of W's cursor on or off, according to the value
21565 of ON. */
21566
21567 static void
21568 update_window_cursor (w, on)
21569 struct window *w;
21570 int on;
21571 {
21572 /* Don't update cursor in windows whose frame is in the process
21573 of being deleted. */
21574 if (w->current_matrix)
21575 {
21576 BLOCK_INPUT;
21577 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21578 w->phys_cursor.x, w->phys_cursor.y);
21579 UNBLOCK_INPUT;
21580 }
21581 }
21582
21583
21584 /* Call update_window_cursor with parameter ON_P on all leaf windows
21585 in the window tree rooted at W. */
21586
21587 static void
21588 update_cursor_in_window_tree (w, on_p)
21589 struct window *w;
21590 int on_p;
21591 {
21592 while (w)
21593 {
21594 if (!NILP (w->hchild))
21595 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21596 else if (!NILP (w->vchild))
21597 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21598 else
21599 update_window_cursor (w, on_p);
21600
21601 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21602 }
21603 }
21604
21605
21606 /* EXPORT:
21607 Display the cursor on window W, or clear it, according to ON_P.
21608 Don't change the cursor's position. */
21609
21610 void
21611 x_update_cursor (f, on_p)
21612 struct frame *f;
21613 int on_p;
21614 {
21615 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21616 }
21617
21618
21619 /* EXPORT:
21620 Clear the cursor of window W to background color, and mark the
21621 cursor as not shown. This is used when the text where the cursor
21622 is is about to be rewritten. */
21623
21624 void
21625 x_clear_cursor (w)
21626 struct window *w;
21627 {
21628 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21629 update_window_cursor (w, 0);
21630 }
21631
21632
21633 /* EXPORT:
21634 Display the active region described by mouse_face_* according to DRAW. */
21635
21636 void
21637 show_mouse_face (dpyinfo, draw)
21638 Display_Info *dpyinfo;
21639 enum draw_glyphs_face draw;
21640 {
21641 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21642 struct frame *f = XFRAME (WINDOW_FRAME (w));
21643
21644 if (/* If window is in the process of being destroyed, don't bother
21645 to do anything. */
21646 w->current_matrix != NULL
21647 /* Don't update mouse highlight if hidden */
21648 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21649 /* Recognize when we are called to operate on rows that don't exist
21650 anymore. This can happen when a window is split. */
21651 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21652 {
21653 int phys_cursor_on_p = w->phys_cursor_on_p;
21654 struct glyph_row *row, *first, *last;
21655
21656 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21657 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21658
21659 for (row = first; row <= last && row->enabled_p; ++row)
21660 {
21661 int start_hpos, end_hpos, start_x;
21662
21663 /* For all but the first row, the highlight starts at column 0. */
21664 if (row == first)
21665 {
21666 start_hpos = dpyinfo->mouse_face_beg_col;
21667 start_x = dpyinfo->mouse_face_beg_x;
21668 }
21669 else
21670 {
21671 start_hpos = 0;
21672 start_x = 0;
21673 }
21674
21675 if (row == last)
21676 end_hpos = dpyinfo->mouse_face_end_col;
21677 else
21678 {
21679 end_hpos = row->used[TEXT_AREA];
21680 if (draw == DRAW_NORMAL_TEXT)
21681 row->fill_line_p = 1; /* Clear to end of line */
21682 }
21683
21684 if (end_hpos > start_hpos)
21685 {
21686 draw_glyphs (w, start_x, row, TEXT_AREA,
21687 start_hpos, end_hpos,
21688 draw, 0);
21689
21690 row->mouse_face_p
21691 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21692 }
21693 }
21694
21695 /* When we've written over the cursor, arrange for it to
21696 be displayed again. */
21697 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21698 {
21699 BLOCK_INPUT;
21700 display_and_set_cursor (w, 1,
21701 w->phys_cursor.hpos, w->phys_cursor.vpos,
21702 w->phys_cursor.x, w->phys_cursor.y);
21703 UNBLOCK_INPUT;
21704 }
21705 }
21706
21707 /* Change the mouse cursor. */
21708 if (draw == DRAW_NORMAL_TEXT)
21709 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21710 else if (draw == DRAW_MOUSE_FACE)
21711 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21712 else
21713 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21714 }
21715
21716 /* EXPORT:
21717 Clear out the mouse-highlighted active region.
21718 Redraw it un-highlighted first. Value is non-zero if mouse
21719 face was actually drawn unhighlighted. */
21720
21721 int
21722 clear_mouse_face (dpyinfo)
21723 Display_Info *dpyinfo;
21724 {
21725 int cleared = 0;
21726
21727 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21728 {
21729 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21730 cleared = 1;
21731 }
21732
21733 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21734 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21735 dpyinfo->mouse_face_window = Qnil;
21736 dpyinfo->mouse_face_overlay = Qnil;
21737 return cleared;
21738 }
21739
21740
21741 /* EXPORT:
21742 Non-zero if physical cursor of window W is within mouse face. */
21743
21744 int
21745 cursor_in_mouse_face_p (w)
21746 struct window *w;
21747 {
21748 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21749 int in_mouse_face = 0;
21750
21751 if (WINDOWP (dpyinfo->mouse_face_window)
21752 && XWINDOW (dpyinfo->mouse_face_window) == w)
21753 {
21754 int hpos = w->phys_cursor.hpos;
21755 int vpos = w->phys_cursor.vpos;
21756
21757 if (vpos >= dpyinfo->mouse_face_beg_row
21758 && vpos <= dpyinfo->mouse_face_end_row
21759 && (vpos > dpyinfo->mouse_face_beg_row
21760 || hpos >= dpyinfo->mouse_face_beg_col)
21761 && (vpos < dpyinfo->mouse_face_end_row
21762 || hpos < dpyinfo->mouse_face_end_col
21763 || dpyinfo->mouse_face_past_end))
21764 in_mouse_face = 1;
21765 }
21766
21767 return in_mouse_face;
21768 }
21769
21770
21771
21772 \f
21773 /* Find the glyph matrix position of buffer position CHARPOS in window
21774 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21775 current glyphs must be up to date. If CHARPOS is above window
21776 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21777 of last line in W. In the row containing CHARPOS, stop before glyphs
21778 having STOP as object. */
21779
21780 #if 1 /* This is a version of fast_find_position that's more correct
21781 in the presence of hscrolling, for example. I didn't install
21782 it right away because the problem fixed is minor, it failed
21783 in 20.x as well, and I think it's too risky to install
21784 so near the release of 21.1. 2001-09-25 gerd. */
21785
21786 static int
21787 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21788 struct window *w;
21789 int charpos;
21790 int *hpos, *vpos, *x, *y;
21791 Lisp_Object stop;
21792 {
21793 struct glyph_row *row, *first;
21794 struct glyph *glyph, *end;
21795 int past_end = 0;
21796
21797 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21798 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21799 {
21800 *x = first->x;
21801 *y = first->y;
21802 *hpos = 0;
21803 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21804 return 1;
21805 }
21806
21807 row = row_containing_pos (w, charpos, first, NULL, 0);
21808 if (row == NULL)
21809 {
21810 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21811 past_end = 1;
21812 }
21813
21814 /* If whole rows or last part of a row came from a display overlay,
21815 row_containing_pos will skip over such rows because their end pos
21816 equals the start pos of the overlay or interval.
21817
21818 Move back if we have a STOP object and previous row's
21819 end glyph came from STOP. */
21820 if (!NILP (stop))
21821 {
21822 struct glyph_row *prev;
21823 while ((prev = row - 1, prev >= first)
21824 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21825 && prev->used[TEXT_AREA] > 0)
21826 {
21827 struct glyph *beg = prev->glyphs[TEXT_AREA];
21828 glyph = beg + prev->used[TEXT_AREA];
21829 while (--glyph >= beg
21830 && INTEGERP (glyph->object));
21831 if (glyph < beg
21832 || !EQ (stop, glyph->object))
21833 break;
21834 row = prev;
21835 }
21836 }
21837
21838 *x = row->x;
21839 *y = row->y;
21840 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21841
21842 glyph = row->glyphs[TEXT_AREA];
21843 end = glyph + row->used[TEXT_AREA];
21844
21845 /* Skip over glyphs not having an object at the start of the row.
21846 These are special glyphs like truncation marks on terminal
21847 frames. */
21848 if (row->displays_text_p)
21849 while (glyph < end
21850 && INTEGERP (glyph->object)
21851 && !EQ (stop, glyph->object)
21852 && glyph->charpos < 0)
21853 {
21854 *x += glyph->pixel_width;
21855 ++glyph;
21856 }
21857
21858 while (glyph < end
21859 && !INTEGERP (glyph->object)
21860 && !EQ (stop, glyph->object)
21861 && (!BUFFERP (glyph->object)
21862 || glyph->charpos < charpos))
21863 {
21864 *x += glyph->pixel_width;
21865 ++glyph;
21866 }
21867
21868 *hpos = glyph - row->glyphs[TEXT_AREA];
21869 return !past_end;
21870 }
21871
21872 #else /* not 1 */
21873
21874 static int
21875 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21876 struct window *w;
21877 int pos;
21878 int *hpos, *vpos, *x, *y;
21879 Lisp_Object stop;
21880 {
21881 int i;
21882 int lastcol;
21883 int maybe_next_line_p = 0;
21884 int line_start_position;
21885 int yb = window_text_bottom_y (w);
21886 struct glyph_row *row, *best_row;
21887 int row_vpos, best_row_vpos;
21888 int current_x;
21889
21890 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21891 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21892
21893 while (row->y < yb)
21894 {
21895 if (row->used[TEXT_AREA])
21896 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21897 else
21898 line_start_position = 0;
21899
21900 if (line_start_position > pos)
21901 break;
21902 /* If the position sought is the end of the buffer,
21903 don't include the blank lines at the bottom of the window. */
21904 else if (line_start_position == pos
21905 && pos == BUF_ZV (XBUFFER (w->buffer)))
21906 {
21907 maybe_next_line_p = 1;
21908 break;
21909 }
21910 else if (line_start_position > 0)
21911 {
21912 best_row = row;
21913 best_row_vpos = row_vpos;
21914 }
21915
21916 if (row->y + row->height >= yb)
21917 break;
21918
21919 ++row;
21920 ++row_vpos;
21921 }
21922
21923 /* Find the right column within BEST_ROW. */
21924 lastcol = 0;
21925 current_x = best_row->x;
21926 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21927 {
21928 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21929 int charpos = glyph->charpos;
21930
21931 if (BUFFERP (glyph->object))
21932 {
21933 if (charpos == pos)
21934 {
21935 *hpos = i;
21936 *vpos = best_row_vpos;
21937 *x = current_x;
21938 *y = best_row->y;
21939 return 1;
21940 }
21941 else if (charpos > pos)
21942 break;
21943 }
21944 else if (EQ (glyph->object, stop))
21945 break;
21946
21947 if (charpos > 0)
21948 lastcol = i;
21949 current_x += glyph->pixel_width;
21950 }
21951
21952 /* If we're looking for the end of the buffer,
21953 and we didn't find it in the line we scanned,
21954 use the start of the following line. */
21955 if (maybe_next_line_p)
21956 {
21957 ++best_row;
21958 ++best_row_vpos;
21959 lastcol = 0;
21960 current_x = best_row->x;
21961 }
21962
21963 *vpos = best_row_vpos;
21964 *hpos = lastcol + 1;
21965 *x = current_x;
21966 *y = best_row->y;
21967 return 0;
21968 }
21969
21970 #endif /* not 1 */
21971
21972
21973 /* Find the position of the glyph for position POS in OBJECT in
21974 window W's current matrix, and return in *X, *Y the pixel
21975 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21976
21977 RIGHT_P non-zero means return the position of the right edge of the
21978 glyph, RIGHT_P zero means return the left edge position.
21979
21980 If no glyph for POS exists in the matrix, return the position of
21981 the glyph with the next smaller position that is in the matrix, if
21982 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21983 exists in the matrix, return the position of the glyph with the
21984 next larger position in OBJECT.
21985
21986 Value is non-zero if a glyph was found. */
21987
21988 static int
21989 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21990 struct window *w;
21991 int pos;
21992 Lisp_Object object;
21993 int *hpos, *vpos, *x, *y;
21994 int right_p;
21995 {
21996 int yb = window_text_bottom_y (w);
21997 struct glyph_row *r;
21998 struct glyph *best_glyph = NULL;
21999 struct glyph_row *best_row = NULL;
22000 int best_x = 0;
22001
22002 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22003 r->enabled_p && r->y < yb;
22004 ++r)
22005 {
22006 struct glyph *g = r->glyphs[TEXT_AREA];
22007 struct glyph *e = g + r->used[TEXT_AREA];
22008 int gx;
22009
22010 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22011 if (EQ (g->object, object))
22012 {
22013 if (g->charpos == pos)
22014 {
22015 best_glyph = g;
22016 best_x = gx;
22017 best_row = r;
22018 goto found;
22019 }
22020 else if (best_glyph == NULL
22021 || ((abs (g->charpos - pos)
22022 < abs (best_glyph->charpos - pos))
22023 && (right_p
22024 ? g->charpos < pos
22025 : g->charpos > pos)))
22026 {
22027 best_glyph = g;
22028 best_x = gx;
22029 best_row = r;
22030 }
22031 }
22032 }
22033
22034 found:
22035
22036 if (best_glyph)
22037 {
22038 *x = best_x;
22039 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22040
22041 if (right_p)
22042 {
22043 *x += best_glyph->pixel_width;
22044 ++*hpos;
22045 }
22046
22047 *y = best_row->y;
22048 *vpos = best_row - w->current_matrix->rows;
22049 }
22050
22051 return best_glyph != NULL;
22052 }
22053
22054
22055 /* See if position X, Y is within a hot-spot of an image. */
22056
22057 static int
22058 on_hot_spot_p (hot_spot, x, y)
22059 Lisp_Object hot_spot;
22060 int x, y;
22061 {
22062 if (!CONSP (hot_spot))
22063 return 0;
22064
22065 if (EQ (XCAR (hot_spot), Qrect))
22066 {
22067 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22068 Lisp_Object rect = XCDR (hot_spot);
22069 Lisp_Object tem;
22070 if (!CONSP (rect))
22071 return 0;
22072 if (!CONSP (XCAR (rect)))
22073 return 0;
22074 if (!CONSP (XCDR (rect)))
22075 return 0;
22076 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22077 return 0;
22078 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22079 return 0;
22080 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22081 return 0;
22082 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22083 return 0;
22084 return 1;
22085 }
22086 else if (EQ (XCAR (hot_spot), Qcircle))
22087 {
22088 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22089 Lisp_Object circ = XCDR (hot_spot);
22090 Lisp_Object lr, lx0, ly0;
22091 if (CONSP (circ)
22092 && CONSP (XCAR (circ))
22093 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22094 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22095 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22096 {
22097 double r = XFLOATINT (lr);
22098 double dx = XINT (lx0) - x;
22099 double dy = XINT (ly0) - y;
22100 return (dx * dx + dy * dy <= r * r);
22101 }
22102 }
22103 else if (EQ (XCAR (hot_spot), Qpoly))
22104 {
22105 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22106 if (VECTORP (XCDR (hot_spot)))
22107 {
22108 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22109 Lisp_Object *poly = v->contents;
22110 int n = v->size;
22111 int i;
22112 int inside = 0;
22113 Lisp_Object lx, ly;
22114 int x0, y0;
22115
22116 /* Need an even number of coordinates, and at least 3 edges. */
22117 if (n < 6 || n & 1)
22118 return 0;
22119
22120 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22121 If count is odd, we are inside polygon. Pixels on edges
22122 may or may not be included depending on actual geometry of the
22123 polygon. */
22124 if ((lx = poly[n-2], !INTEGERP (lx))
22125 || (ly = poly[n-1], !INTEGERP (lx)))
22126 return 0;
22127 x0 = XINT (lx), y0 = XINT (ly);
22128 for (i = 0; i < n; i += 2)
22129 {
22130 int x1 = x0, y1 = y0;
22131 if ((lx = poly[i], !INTEGERP (lx))
22132 || (ly = poly[i+1], !INTEGERP (ly)))
22133 return 0;
22134 x0 = XINT (lx), y0 = XINT (ly);
22135
22136 /* Does this segment cross the X line? */
22137 if (x0 >= x)
22138 {
22139 if (x1 >= x)
22140 continue;
22141 }
22142 else if (x1 < x)
22143 continue;
22144 if (y > y0 && y > y1)
22145 continue;
22146 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22147 inside = !inside;
22148 }
22149 return inside;
22150 }
22151 }
22152 /* If we don't understand the format, pretend we're not in the hot-spot. */
22153 return 0;
22154 }
22155
22156 Lisp_Object
22157 find_hot_spot (map, x, y)
22158 Lisp_Object map;
22159 int x, y;
22160 {
22161 while (CONSP (map))
22162 {
22163 if (CONSP (XCAR (map))
22164 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22165 return XCAR (map);
22166 map = XCDR (map);
22167 }
22168
22169 return Qnil;
22170 }
22171
22172 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22173 3, 3, 0,
22174 doc: /* Lookup in image map MAP coordinates X and Y.
22175 An image map is an alist where each element has the format (AREA ID PLIST).
22176 An AREA is specified as either a rectangle, a circle, or a polygon:
22177 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22178 pixel coordinates of the upper left and bottom right corners.
22179 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22180 and the radius of the circle; r may be a float or integer.
22181 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22182 vector describes one corner in the polygon.
22183 Returns the alist element for the first matching AREA in MAP. */)
22184 (map, x, y)
22185 Lisp_Object map;
22186 Lisp_Object x, y;
22187 {
22188 if (NILP (map))
22189 return Qnil;
22190
22191 CHECK_NUMBER (x);
22192 CHECK_NUMBER (y);
22193
22194 return find_hot_spot (map, XINT (x), XINT (y));
22195 }
22196
22197
22198 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22199 static void
22200 define_frame_cursor1 (f, cursor, pointer)
22201 struct frame *f;
22202 Cursor cursor;
22203 Lisp_Object pointer;
22204 {
22205 /* Do not change cursor shape while dragging mouse. */
22206 if (!NILP (do_mouse_tracking))
22207 return;
22208
22209 if (!NILP (pointer))
22210 {
22211 if (EQ (pointer, Qarrow))
22212 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22213 else if (EQ (pointer, Qhand))
22214 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22215 else if (EQ (pointer, Qtext))
22216 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22217 else if (EQ (pointer, intern ("hdrag")))
22218 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22219 #ifdef HAVE_X_WINDOWS
22220 else if (EQ (pointer, intern ("vdrag")))
22221 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22222 #endif
22223 else if (EQ (pointer, intern ("hourglass")))
22224 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22225 else if (EQ (pointer, Qmodeline))
22226 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22227 else
22228 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22229 }
22230
22231 if (cursor != No_Cursor)
22232 rif->define_frame_cursor (f, cursor);
22233 }
22234
22235 /* Take proper action when mouse has moved to the mode or header line
22236 or marginal area AREA of window W, x-position X and y-position Y.
22237 X is relative to the start of the text display area of W, so the
22238 width of bitmap areas and scroll bars must be subtracted to get a
22239 position relative to the start of the mode line. */
22240
22241 static void
22242 note_mode_line_or_margin_highlight (window, x, y, area)
22243 Lisp_Object window;
22244 int x, y;
22245 enum window_part area;
22246 {
22247 struct window *w = XWINDOW (window);
22248 struct frame *f = XFRAME (w->frame);
22249 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22250 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22251 Lisp_Object pointer = Qnil;
22252 int charpos, dx, dy, width, height;
22253 Lisp_Object string, object = Qnil;
22254 Lisp_Object pos, help;
22255
22256 Lisp_Object mouse_face;
22257 int original_x_pixel = x;
22258 struct glyph * glyph = NULL;
22259 struct glyph_row *row;
22260
22261 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22262 {
22263 int x0;
22264 struct glyph *end;
22265
22266 string = mode_line_string (w, area, &x, &y, &charpos,
22267 &object, &dx, &dy, &width, &height);
22268
22269 row = (area == ON_MODE_LINE
22270 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22271 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22272
22273 /* Find glyph */
22274 if (row->mode_line_p && row->enabled_p)
22275 {
22276 glyph = row->glyphs[TEXT_AREA];
22277 end = glyph + row->used[TEXT_AREA];
22278
22279 for (x0 = original_x_pixel;
22280 glyph < end && x0 >= glyph->pixel_width;
22281 ++glyph)
22282 x0 -= glyph->pixel_width;
22283
22284 if (glyph >= end)
22285 glyph = NULL;
22286 }
22287 }
22288 else
22289 {
22290 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22291 string = marginal_area_string (w, area, &x, &y, &charpos,
22292 &object, &dx, &dy, &width, &height);
22293 }
22294
22295 help = Qnil;
22296
22297 if (IMAGEP (object))
22298 {
22299 Lisp_Object image_map, hotspot;
22300 if ((image_map = Fplist_get (XCDR (object), QCmap),
22301 !NILP (image_map))
22302 && (hotspot = find_hot_spot (image_map, dx, dy),
22303 CONSP (hotspot))
22304 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22305 {
22306 Lisp_Object area_id, plist;
22307
22308 area_id = XCAR (hotspot);
22309 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22310 If so, we could look for mouse-enter, mouse-leave
22311 properties in PLIST (and do something...). */
22312 hotspot = XCDR (hotspot);
22313 if (CONSP (hotspot)
22314 && (plist = XCAR (hotspot), CONSP (plist)))
22315 {
22316 pointer = Fplist_get (plist, Qpointer);
22317 if (NILP (pointer))
22318 pointer = Qhand;
22319 help = Fplist_get (plist, Qhelp_echo);
22320 if (!NILP (help))
22321 {
22322 help_echo_string = help;
22323 /* Is this correct? ++kfs */
22324 XSETWINDOW (help_echo_window, w);
22325 help_echo_object = w->buffer;
22326 help_echo_pos = charpos;
22327 }
22328 }
22329 }
22330 if (NILP (pointer))
22331 pointer = Fplist_get (XCDR (object), QCpointer);
22332 }
22333
22334 if (STRINGP (string))
22335 {
22336 pos = make_number (charpos);
22337 /* If we're on a string with `help-echo' text property, arrange
22338 for the help to be displayed. This is done by setting the
22339 global variable help_echo_string to the help string. */
22340 if (NILP (help))
22341 {
22342 help = Fget_text_property (pos, Qhelp_echo, string);
22343 if (!NILP (help))
22344 {
22345 help_echo_string = help;
22346 XSETWINDOW (help_echo_window, w);
22347 help_echo_object = string;
22348 help_echo_pos = charpos;
22349 }
22350 }
22351
22352 if (NILP (pointer))
22353 pointer = Fget_text_property (pos, Qpointer, string);
22354
22355 /* Change the mouse pointer according to what is under X/Y. */
22356 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22357 {
22358 Lisp_Object map;
22359 map = Fget_text_property (pos, Qlocal_map, string);
22360 if (!KEYMAPP (map))
22361 map = Fget_text_property (pos, Qkeymap, string);
22362 if (!KEYMAPP (map))
22363 cursor = dpyinfo->vertical_scroll_bar_cursor;
22364 }
22365
22366 /* Change the mouse face according to what is under X/Y. */
22367 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22368 if (!NILP (mouse_face)
22369 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22370 && glyph)
22371 {
22372 Lisp_Object b, e;
22373
22374 struct glyph * tmp_glyph;
22375
22376 int gpos;
22377 int gseq_length;
22378 int total_pixel_width;
22379 int ignore;
22380
22381 int vpos, hpos;
22382
22383 b = Fprevious_single_property_change (make_number (charpos + 1),
22384 Qmouse_face, string, Qnil);
22385 if (NILP (b))
22386 b = make_number (0);
22387
22388 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22389 if (NILP (e))
22390 e = make_number (SCHARS (string));
22391
22392 /* Calculate the position(glyph position: GPOS) of GLYPH in
22393 displayed string. GPOS is different from CHARPOS.
22394
22395 CHARPOS is the position of glyph in internal string
22396 object. A mode line string format has structures which
22397 is converted to a flatten by emacs lisp interpreter.
22398 The internal string is an element of the structures.
22399 The displayed string is the flatten string. */
22400 for (tmp_glyph = glyph - 1, gpos = 0;
22401 tmp_glyph->charpos >= XINT (b);
22402 tmp_glyph--, gpos++)
22403 {
22404 if (!EQ (tmp_glyph->object, glyph->object))
22405 break;
22406 }
22407
22408 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22409 displayed string holding GLYPH.
22410
22411 GSEQ_LENGTH is different from SCHARS (STRING).
22412 SCHARS (STRING) returns the length of the internal string. */
22413 for (tmp_glyph = glyph, gseq_length = gpos;
22414 tmp_glyph->charpos < XINT (e);
22415 tmp_glyph++, gseq_length++)
22416 {
22417 if (!EQ (tmp_glyph->object, glyph->object))
22418 break;
22419 }
22420
22421 total_pixel_width = 0;
22422 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22423 total_pixel_width += tmp_glyph->pixel_width;
22424
22425 /* Pre calculation of re-rendering position */
22426 vpos = (x - gpos);
22427 hpos = (area == ON_MODE_LINE
22428 ? (w->current_matrix)->nrows - 1
22429 : 0);
22430
22431 /* If the re-rendering position is included in the last
22432 re-rendering area, we should do nothing. */
22433 if ( EQ (window, dpyinfo->mouse_face_window)
22434 && dpyinfo->mouse_face_beg_col <= vpos
22435 && vpos < dpyinfo->mouse_face_end_col
22436 && dpyinfo->mouse_face_beg_row == hpos )
22437 return;
22438
22439 if (clear_mouse_face (dpyinfo))
22440 cursor = No_Cursor;
22441
22442 dpyinfo->mouse_face_beg_col = vpos;
22443 dpyinfo->mouse_face_beg_row = hpos;
22444
22445 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22446 dpyinfo->mouse_face_beg_y = 0;
22447
22448 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22449 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22450
22451 dpyinfo->mouse_face_end_x = 0;
22452 dpyinfo->mouse_face_end_y = 0;
22453
22454 dpyinfo->mouse_face_past_end = 0;
22455 dpyinfo->mouse_face_window = window;
22456
22457 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22458 charpos,
22459 0, 0, 0, &ignore,
22460 glyph->face_id, 1);
22461 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22462
22463 if (NILP (pointer))
22464 pointer = Qhand;
22465 }
22466 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22467 clear_mouse_face (dpyinfo);
22468 }
22469 define_frame_cursor1 (f, cursor, pointer);
22470 }
22471
22472
22473 /* EXPORT:
22474 Take proper action when the mouse has moved to position X, Y on
22475 frame F as regards highlighting characters that have mouse-face
22476 properties. Also de-highlighting chars where the mouse was before.
22477 X and Y can be negative or out of range. */
22478
22479 void
22480 note_mouse_highlight (f, x, y)
22481 struct frame *f;
22482 int x, y;
22483 {
22484 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22485 enum window_part part;
22486 Lisp_Object window;
22487 struct window *w;
22488 Cursor cursor = No_Cursor;
22489 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22490 struct buffer *b;
22491
22492 /* When a menu is active, don't highlight because this looks odd. */
22493 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
22494 if (popup_activated ())
22495 return;
22496 #endif
22497
22498 if (NILP (Vmouse_highlight)
22499 || !f->glyphs_initialized_p)
22500 return;
22501
22502 dpyinfo->mouse_face_mouse_x = x;
22503 dpyinfo->mouse_face_mouse_y = y;
22504 dpyinfo->mouse_face_mouse_frame = f;
22505
22506 if (dpyinfo->mouse_face_defer)
22507 return;
22508
22509 if (gc_in_progress)
22510 {
22511 dpyinfo->mouse_face_deferred_gc = 1;
22512 return;
22513 }
22514
22515 /* Which window is that in? */
22516 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22517
22518 /* If we were displaying active text in another window, clear that.
22519 Also clear if we move out of text area in same window. */
22520 if (! EQ (window, dpyinfo->mouse_face_window)
22521 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22522 && !NILP (dpyinfo->mouse_face_window)))
22523 clear_mouse_face (dpyinfo);
22524
22525 /* Not on a window -> return. */
22526 if (!WINDOWP (window))
22527 return;
22528
22529 /* Reset help_echo_string. It will get recomputed below. */
22530 help_echo_string = Qnil;
22531
22532 /* Convert to window-relative pixel coordinates. */
22533 w = XWINDOW (window);
22534 frame_to_window_pixel_xy (w, &x, &y);
22535
22536 /* Handle tool-bar window differently since it doesn't display a
22537 buffer. */
22538 if (EQ (window, f->tool_bar_window))
22539 {
22540 note_tool_bar_highlight (f, x, y);
22541 return;
22542 }
22543
22544 /* Mouse is on the mode, header line or margin? */
22545 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22546 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22547 {
22548 note_mode_line_or_margin_highlight (window, x, y, part);
22549 return;
22550 }
22551
22552 if (part == ON_VERTICAL_BORDER)
22553 {
22554 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22555 help_echo_string = build_string ("drag-mouse-1: resize");
22556 }
22557 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22558 || part == ON_SCROLL_BAR)
22559 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22560 else
22561 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22562
22563 /* Are we in a window whose display is up to date?
22564 And verify the buffer's text has not changed. */
22565 b = XBUFFER (w->buffer);
22566 if (part == ON_TEXT
22567 && EQ (w->window_end_valid, w->buffer)
22568 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22569 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22570 {
22571 int hpos, vpos, pos, i, dx, dy, area;
22572 struct glyph *glyph;
22573 Lisp_Object object;
22574 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22575 Lisp_Object *overlay_vec = NULL;
22576 int noverlays;
22577 struct buffer *obuf;
22578 int obegv, ozv, same_region;
22579
22580 /* Find the glyph under X/Y. */
22581 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22582
22583 /* Look for :pointer property on image. */
22584 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22585 {
22586 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22587 if (img != NULL && IMAGEP (img->spec))
22588 {
22589 Lisp_Object image_map, hotspot;
22590 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22591 !NILP (image_map))
22592 && (hotspot = find_hot_spot (image_map,
22593 glyph->slice.x + dx,
22594 glyph->slice.y + dy),
22595 CONSP (hotspot))
22596 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22597 {
22598 Lisp_Object area_id, plist;
22599
22600 area_id = XCAR (hotspot);
22601 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22602 If so, we could look for mouse-enter, mouse-leave
22603 properties in PLIST (and do something...). */
22604 hotspot = XCDR (hotspot);
22605 if (CONSP (hotspot)
22606 && (plist = XCAR (hotspot), CONSP (plist)))
22607 {
22608 pointer = Fplist_get (plist, Qpointer);
22609 if (NILP (pointer))
22610 pointer = Qhand;
22611 help_echo_string = Fplist_get (plist, Qhelp_echo);
22612 if (!NILP (help_echo_string))
22613 {
22614 help_echo_window = window;
22615 help_echo_object = glyph->object;
22616 help_echo_pos = glyph->charpos;
22617 }
22618 }
22619 }
22620 if (NILP (pointer))
22621 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22622 }
22623 }
22624
22625 /* Clear mouse face if X/Y not over text. */
22626 if (glyph == NULL
22627 || area != TEXT_AREA
22628 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22629 {
22630 if (clear_mouse_face (dpyinfo))
22631 cursor = No_Cursor;
22632 if (NILP (pointer))
22633 {
22634 if (area != TEXT_AREA)
22635 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22636 else
22637 pointer = Vvoid_text_area_pointer;
22638 }
22639 goto set_cursor;
22640 }
22641
22642 pos = glyph->charpos;
22643 object = glyph->object;
22644 if (!STRINGP (object) && !BUFFERP (object))
22645 goto set_cursor;
22646
22647 /* If we get an out-of-range value, return now; avoid an error. */
22648 if (BUFFERP (object) && pos > BUF_Z (b))
22649 goto set_cursor;
22650
22651 /* Make the window's buffer temporarily current for
22652 overlays_at and compute_char_face. */
22653 obuf = current_buffer;
22654 current_buffer = b;
22655 obegv = BEGV;
22656 ozv = ZV;
22657 BEGV = BEG;
22658 ZV = Z;
22659
22660 /* Is this char mouse-active or does it have help-echo? */
22661 position = make_number (pos);
22662
22663 if (BUFFERP (object))
22664 {
22665 /* Put all the overlays we want in a vector in overlay_vec. */
22666 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22667 /* Sort overlays into increasing priority order. */
22668 noverlays = sort_overlays (overlay_vec, noverlays, w);
22669 }
22670 else
22671 noverlays = 0;
22672
22673 same_region = (EQ (window, dpyinfo->mouse_face_window)
22674 && vpos >= dpyinfo->mouse_face_beg_row
22675 && vpos <= dpyinfo->mouse_face_end_row
22676 && (vpos > dpyinfo->mouse_face_beg_row
22677 || hpos >= dpyinfo->mouse_face_beg_col)
22678 && (vpos < dpyinfo->mouse_face_end_row
22679 || hpos < dpyinfo->mouse_face_end_col
22680 || dpyinfo->mouse_face_past_end));
22681
22682 if (same_region)
22683 cursor = No_Cursor;
22684
22685 /* Check mouse-face highlighting. */
22686 if (! same_region
22687 /* If there exists an overlay with mouse-face overlapping
22688 the one we are currently highlighting, we have to
22689 check if we enter the overlapping overlay, and then
22690 highlight only that. */
22691 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22692 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22693 {
22694 /* Find the highest priority overlay that has a mouse-face
22695 property. */
22696 overlay = Qnil;
22697 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22698 {
22699 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22700 if (!NILP (mouse_face))
22701 overlay = overlay_vec[i];
22702 }
22703
22704 /* If we're actually highlighting the same overlay as
22705 before, there's no need to do that again. */
22706 if (!NILP (overlay)
22707 && EQ (overlay, dpyinfo->mouse_face_overlay))
22708 goto check_help_echo;
22709
22710 dpyinfo->mouse_face_overlay = overlay;
22711
22712 /* Clear the display of the old active region, if any. */
22713 if (clear_mouse_face (dpyinfo))
22714 cursor = No_Cursor;
22715
22716 /* If no overlay applies, get a text property. */
22717 if (NILP (overlay))
22718 mouse_face = Fget_text_property (position, Qmouse_face, object);
22719
22720 /* Handle the overlay case. */
22721 if (!NILP (overlay))
22722 {
22723 /* Find the range of text around this char that
22724 should be active. */
22725 Lisp_Object before, after;
22726 int ignore;
22727
22728 before = Foverlay_start (overlay);
22729 after = Foverlay_end (overlay);
22730 /* Record this as the current active region. */
22731 fast_find_position (w, XFASTINT (before),
22732 &dpyinfo->mouse_face_beg_col,
22733 &dpyinfo->mouse_face_beg_row,
22734 &dpyinfo->mouse_face_beg_x,
22735 &dpyinfo->mouse_face_beg_y, Qnil);
22736
22737 dpyinfo->mouse_face_past_end
22738 = !fast_find_position (w, XFASTINT (after),
22739 &dpyinfo->mouse_face_end_col,
22740 &dpyinfo->mouse_face_end_row,
22741 &dpyinfo->mouse_face_end_x,
22742 &dpyinfo->mouse_face_end_y, Qnil);
22743 dpyinfo->mouse_face_window = window;
22744
22745 dpyinfo->mouse_face_face_id
22746 = face_at_buffer_position (w, pos, 0, 0,
22747 &ignore, pos + 1,
22748 !dpyinfo->mouse_face_hidden);
22749
22750 /* Display it as active. */
22751 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22752 cursor = No_Cursor;
22753 }
22754 /* Handle the text property case. */
22755 else if (!NILP (mouse_face) && BUFFERP (object))
22756 {
22757 /* Find the range of text around this char that
22758 should be active. */
22759 Lisp_Object before, after, beginning, end;
22760 int ignore;
22761
22762 beginning = Fmarker_position (w->start);
22763 end = make_number (BUF_Z (XBUFFER (object))
22764 - XFASTINT (w->window_end_pos));
22765 before
22766 = Fprevious_single_property_change (make_number (pos + 1),
22767 Qmouse_face,
22768 object, beginning);
22769 after
22770 = Fnext_single_property_change (position, Qmouse_face,
22771 object, end);
22772
22773 /* Record this as the current active region. */
22774 fast_find_position (w, XFASTINT (before),
22775 &dpyinfo->mouse_face_beg_col,
22776 &dpyinfo->mouse_face_beg_row,
22777 &dpyinfo->mouse_face_beg_x,
22778 &dpyinfo->mouse_face_beg_y, Qnil);
22779 dpyinfo->mouse_face_past_end
22780 = !fast_find_position (w, XFASTINT (after),
22781 &dpyinfo->mouse_face_end_col,
22782 &dpyinfo->mouse_face_end_row,
22783 &dpyinfo->mouse_face_end_x,
22784 &dpyinfo->mouse_face_end_y, Qnil);
22785 dpyinfo->mouse_face_window = window;
22786
22787 if (BUFFERP (object))
22788 dpyinfo->mouse_face_face_id
22789 = face_at_buffer_position (w, pos, 0, 0,
22790 &ignore, pos + 1,
22791 !dpyinfo->mouse_face_hidden);
22792
22793 /* Display it as active. */
22794 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22795 cursor = No_Cursor;
22796 }
22797 else if (!NILP (mouse_face) && STRINGP (object))
22798 {
22799 Lisp_Object b, e;
22800 int ignore;
22801
22802 b = Fprevious_single_property_change (make_number (pos + 1),
22803 Qmouse_face,
22804 object, Qnil);
22805 e = Fnext_single_property_change (position, Qmouse_face,
22806 object, Qnil);
22807 if (NILP (b))
22808 b = make_number (0);
22809 if (NILP (e))
22810 e = make_number (SCHARS (object) - 1);
22811
22812 fast_find_string_pos (w, XINT (b), object,
22813 &dpyinfo->mouse_face_beg_col,
22814 &dpyinfo->mouse_face_beg_row,
22815 &dpyinfo->mouse_face_beg_x,
22816 &dpyinfo->mouse_face_beg_y, 0);
22817 fast_find_string_pos (w, XINT (e), object,
22818 &dpyinfo->mouse_face_end_col,
22819 &dpyinfo->mouse_face_end_row,
22820 &dpyinfo->mouse_face_end_x,
22821 &dpyinfo->mouse_face_end_y, 1);
22822 dpyinfo->mouse_face_past_end = 0;
22823 dpyinfo->mouse_face_window = window;
22824 dpyinfo->mouse_face_face_id
22825 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22826 glyph->face_id, 1);
22827 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22828 cursor = No_Cursor;
22829 }
22830 else if (STRINGP (object) && NILP (mouse_face))
22831 {
22832 /* A string which doesn't have mouse-face, but
22833 the text ``under'' it might have. */
22834 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22835 int start = MATRIX_ROW_START_CHARPOS (r);
22836
22837 pos = string_buffer_position (w, object, start);
22838 if (pos > 0)
22839 mouse_face = get_char_property_and_overlay (make_number (pos),
22840 Qmouse_face,
22841 w->buffer,
22842 &overlay);
22843 if (!NILP (mouse_face) && !NILP (overlay))
22844 {
22845 Lisp_Object before = Foverlay_start (overlay);
22846 Lisp_Object after = Foverlay_end (overlay);
22847 int ignore;
22848
22849 /* Note that we might not be able to find position
22850 BEFORE in the glyph matrix if the overlay is
22851 entirely covered by a `display' property. In
22852 this case, we overshoot. So let's stop in
22853 the glyph matrix before glyphs for OBJECT. */
22854 fast_find_position (w, XFASTINT (before),
22855 &dpyinfo->mouse_face_beg_col,
22856 &dpyinfo->mouse_face_beg_row,
22857 &dpyinfo->mouse_face_beg_x,
22858 &dpyinfo->mouse_face_beg_y,
22859 object);
22860
22861 dpyinfo->mouse_face_past_end
22862 = !fast_find_position (w, XFASTINT (after),
22863 &dpyinfo->mouse_face_end_col,
22864 &dpyinfo->mouse_face_end_row,
22865 &dpyinfo->mouse_face_end_x,
22866 &dpyinfo->mouse_face_end_y,
22867 Qnil);
22868 dpyinfo->mouse_face_window = window;
22869 dpyinfo->mouse_face_face_id
22870 = face_at_buffer_position (w, pos, 0, 0,
22871 &ignore, pos + 1,
22872 !dpyinfo->mouse_face_hidden);
22873
22874 /* Display it as active. */
22875 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22876 cursor = No_Cursor;
22877 }
22878 }
22879 }
22880
22881 check_help_echo:
22882
22883 /* Look for a `help-echo' property. */
22884 if (NILP (help_echo_string)) {
22885 Lisp_Object help, overlay;
22886
22887 /* Check overlays first. */
22888 help = overlay = Qnil;
22889 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22890 {
22891 overlay = overlay_vec[i];
22892 help = Foverlay_get (overlay, Qhelp_echo);
22893 }
22894
22895 if (!NILP (help))
22896 {
22897 help_echo_string = help;
22898 help_echo_window = window;
22899 help_echo_object = overlay;
22900 help_echo_pos = pos;
22901 }
22902 else
22903 {
22904 Lisp_Object object = glyph->object;
22905 int charpos = glyph->charpos;
22906
22907 /* Try text properties. */
22908 if (STRINGP (object)
22909 && charpos >= 0
22910 && charpos < SCHARS (object))
22911 {
22912 help = Fget_text_property (make_number (charpos),
22913 Qhelp_echo, object);
22914 if (NILP (help))
22915 {
22916 /* If the string itself doesn't specify a help-echo,
22917 see if the buffer text ``under'' it does. */
22918 struct glyph_row *r
22919 = MATRIX_ROW (w->current_matrix, vpos);
22920 int start = MATRIX_ROW_START_CHARPOS (r);
22921 int pos = string_buffer_position (w, object, start);
22922 if (pos > 0)
22923 {
22924 help = Fget_char_property (make_number (pos),
22925 Qhelp_echo, w->buffer);
22926 if (!NILP (help))
22927 {
22928 charpos = pos;
22929 object = w->buffer;
22930 }
22931 }
22932 }
22933 }
22934 else if (BUFFERP (object)
22935 && charpos >= BEGV
22936 && charpos < ZV)
22937 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22938 object);
22939
22940 if (!NILP (help))
22941 {
22942 help_echo_string = help;
22943 help_echo_window = window;
22944 help_echo_object = object;
22945 help_echo_pos = charpos;
22946 }
22947 }
22948 }
22949
22950 /* Look for a `pointer' property. */
22951 if (NILP (pointer))
22952 {
22953 /* Check overlays first. */
22954 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22955 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22956
22957 if (NILP (pointer))
22958 {
22959 Lisp_Object object = glyph->object;
22960 int charpos = glyph->charpos;
22961
22962 /* Try text properties. */
22963 if (STRINGP (object)
22964 && charpos >= 0
22965 && charpos < SCHARS (object))
22966 {
22967 pointer = Fget_text_property (make_number (charpos),
22968 Qpointer, object);
22969 if (NILP (pointer))
22970 {
22971 /* If the string itself doesn't specify a pointer,
22972 see if the buffer text ``under'' it does. */
22973 struct glyph_row *r
22974 = MATRIX_ROW (w->current_matrix, vpos);
22975 int start = MATRIX_ROW_START_CHARPOS (r);
22976 int pos = string_buffer_position (w, object, start);
22977 if (pos > 0)
22978 pointer = Fget_char_property (make_number (pos),
22979 Qpointer, w->buffer);
22980 }
22981 }
22982 else if (BUFFERP (object)
22983 && charpos >= BEGV
22984 && charpos < ZV)
22985 pointer = Fget_text_property (make_number (charpos),
22986 Qpointer, object);
22987 }
22988 }
22989
22990 BEGV = obegv;
22991 ZV = ozv;
22992 current_buffer = obuf;
22993 }
22994
22995 set_cursor:
22996
22997 define_frame_cursor1 (f, cursor, pointer);
22998 }
22999
23000
23001 /* EXPORT for RIF:
23002 Clear any mouse-face on window W. This function is part of the
23003 redisplay interface, and is called from try_window_id and similar
23004 functions to ensure the mouse-highlight is off. */
23005
23006 void
23007 x_clear_window_mouse_face (w)
23008 struct window *w;
23009 {
23010 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23011 Lisp_Object window;
23012
23013 BLOCK_INPUT;
23014 XSETWINDOW (window, w);
23015 if (EQ (window, dpyinfo->mouse_face_window))
23016 clear_mouse_face (dpyinfo);
23017 UNBLOCK_INPUT;
23018 }
23019
23020
23021 /* EXPORT:
23022 Just discard the mouse face information for frame F, if any.
23023 This is used when the size of F is changed. */
23024
23025 void
23026 cancel_mouse_face (f)
23027 struct frame *f;
23028 {
23029 Lisp_Object window;
23030 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23031
23032 window = dpyinfo->mouse_face_window;
23033 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23034 {
23035 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23036 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23037 dpyinfo->mouse_face_window = Qnil;
23038 }
23039 }
23040
23041
23042 #endif /* HAVE_WINDOW_SYSTEM */
23043
23044 \f
23045 /***********************************************************************
23046 Exposure Events
23047 ***********************************************************************/
23048
23049 #ifdef HAVE_WINDOW_SYSTEM
23050
23051 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23052 which intersects rectangle R. R is in window-relative coordinates. */
23053
23054 static void
23055 expose_area (w, row, r, area)
23056 struct window *w;
23057 struct glyph_row *row;
23058 XRectangle *r;
23059 enum glyph_row_area area;
23060 {
23061 struct glyph *first = row->glyphs[area];
23062 struct glyph *end = row->glyphs[area] + row->used[area];
23063 struct glyph *last;
23064 int first_x, start_x, x;
23065
23066 if (area == TEXT_AREA && row->fill_line_p)
23067 /* If row extends face to end of line write the whole line. */
23068 draw_glyphs (w, 0, row, area,
23069 0, row->used[area],
23070 DRAW_NORMAL_TEXT, 0);
23071 else
23072 {
23073 /* Set START_X to the window-relative start position for drawing glyphs of
23074 AREA. The first glyph of the text area can be partially visible.
23075 The first glyphs of other areas cannot. */
23076 start_x = window_box_left_offset (w, area);
23077 x = start_x;
23078 if (area == TEXT_AREA)
23079 x += row->x;
23080
23081 /* Find the first glyph that must be redrawn. */
23082 while (first < end
23083 && x + first->pixel_width < r->x)
23084 {
23085 x += first->pixel_width;
23086 ++first;
23087 }
23088
23089 /* Find the last one. */
23090 last = first;
23091 first_x = x;
23092 while (last < end
23093 && x < r->x + r->width)
23094 {
23095 x += last->pixel_width;
23096 ++last;
23097 }
23098
23099 /* Repaint. */
23100 if (last > first)
23101 draw_glyphs (w, first_x - start_x, row, area,
23102 first - row->glyphs[area], last - row->glyphs[area],
23103 DRAW_NORMAL_TEXT, 0);
23104 }
23105 }
23106
23107
23108 /* Redraw the parts of the glyph row ROW on window W intersecting
23109 rectangle R. R is in window-relative coordinates. Value is
23110 non-zero if mouse-face was overwritten. */
23111
23112 static int
23113 expose_line (w, row, r)
23114 struct window *w;
23115 struct glyph_row *row;
23116 XRectangle *r;
23117 {
23118 xassert (row->enabled_p);
23119
23120 if (row->mode_line_p || w->pseudo_window_p)
23121 draw_glyphs (w, 0, row, TEXT_AREA,
23122 0, row->used[TEXT_AREA],
23123 DRAW_NORMAL_TEXT, 0);
23124 else
23125 {
23126 if (row->used[LEFT_MARGIN_AREA])
23127 expose_area (w, row, r, LEFT_MARGIN_AREA);
23128 if (row->used[TEXT_AREA])
23129 expose_area (w, row, r, TEXT_AREA);
23130 if (row->used[RIGHT_MARGIN_AREA])
23131 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23132 draw_row_fringe_bitmaps (w, row);
23133 }
23134
23135 return row->mouse_face_p;
23136 }
23137
23138
23139 /* Redraw those parts of glyphs rows during expose event handling that
23140 overlap other rows. Redrawing of an exposed line writes over parts
23141 of lines overlapping that exposed line; this function fixes that.
23142
23143 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23144 row in W's current matrix that is exposed and overlaps other rows.
23145 LAST_OVERLAPPING_ROW is the last such row. */
23146
23147 static void
23148 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23149 struct window *w;
23150 struct glyph_row *first_overlapping_row;
23151 struct glyph_row *last_overlapping_row;
23152 {
23153 struct glyph_row *row;
23154
23155 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23156 if (row->overlapping_p)
23157 {
23158 xassert (row->enabled_p && !row->mode_line_p);
23159
23160 if (row->used[LEFT_MARGIN_AREA])
23161 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23162
23163 if (row->used[TEXT_AREA])
23164 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23165
23166 if (row->used[RIGHT_MARGIN_AREA])
23167 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23168 }
23169 }
23170
23171
23172 /* Return non-zero if W's cursor intersects rectangle R. */
23173
23174 static int
23175 phys_cursor_in_rect_p (w, r)
23176 struct window *w;
23177 XRectangle *r;
23178 {
23179 XRectangle cr, result;
23180 struct glyph *cursor_glyph;
23181
23182 cursor_glyph = get_phys_cursor_glyph (w);
23183 if (cursor_glyph)
23184 {
23185 /* r is relative to W's box, but w->phys_cursor.x is relative
23186 to left edge of W's TEXT area. Adjust it. */
23187 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23188 cr.y = w->phys_cursor.y;
23189 cr.width = cursor_glyph->pixel_width;
23190 cr.height = w->phys_cursor_height;
23191 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23192 I assume the effect is the same -- and this is portable. */
23193 return x_intersect_rectangles (&cr, r, &result);
23194 }
23195 else
23196 return 0;
23197 }
23198
23199
23200 /* EXPORT:
23201 Draw a vertical window border to the right of window W if W doesn't
23202 have vertical scroll bars. */
23203
23204 void
23205 x_draw_vertical_border (w)
23206 struct window *w;
23207 {
23208 /* We could do better, if we knew what type of scroll-bar the adjacent
23209 windows (on either side) have... But we don't :-(
23210 However, I think this works ok. ++KFS 2003-04-25 */
23211
23212 /* Redraw borders between horizontally adjacent windows. Don't
23213 do it for frames with vertical scroll bars because either the
23214 right scroll bar of a window, or the left scroll bar of its
23215 neighbor will suffice as a border. */
23216 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23217 return;
23218
23219 if (!WINDOW_RIGHTMOST_P (w)
23220 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23221 {
23222 int x0, x1, y0, y1;
23223
23224 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23225 y1 -= 1;
23226
23227 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23228 x1 -= 1;
23229
23230 rif->draw_vertical_window_border (w, x1, y0, y1);
23231 }
23232 else if (!WINDOW_LEFTMOST_P (w)
23233 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23234 {
23235 int x0, x1, y0, y1;
23236
23237 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23238 y1 -= 1;
23239
23240 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23241 x0 -= 1;
23242
23243 rif->draw_vertical_window_border (w, x0, y0, y1);
23244 }
23245 }
23246
23247
23248 /* Redraw the part of window W intersection rectangle FR. Pixel
23249 coordinates in FR are frame-relative. Call this function with
23250 input blocked. Value is non-zero if the exposure overwrites
23251 mouse-face. */
23252
23253 static int
23254 expose_window (w, fr)
23255 struct window *w;
23256 XRectangle *fr;
23257 {
23258 struct frame *f = XFRAME (w->frame);
23259 XRectangle wr, r;
23260 int mouse_face_overwritten_p = 0;
23261
23262 /* If window is not yet fully initialized, do nothing. This can
23263 happen when toolkit scroll bars are used and a window is split.
23264 Reconfiguring the scroll bar will generate an expose for a newly
23265 created window. */
23266 if (w->current_matrix == NULL)
23267 return 0;
23268
23269 /* When we're currently updating the window, display and current
23270 matrix usually don't agree. Arrange for a thorough display
23271 later. */
23272 if (w == updated_window)
23273 {
23274 SET_FRAME_GARBAGED (f);
23275 return 0;
23276 }
23277
23278 /* Frame-relative pixel rectangle of W. */
23279 wr.x = WINDOW_LEFT_EDGE_X (w);
23280 wr.y = WINDOW_TOP_EDGE_Y (w);
23281 wr.width = WINDOW_TOTAL_WIDTH (w);
23282 wr.height = WINDOW_TOTAL_HEIGHT (w);
23283
23284 if (x_intersect_rectangles (fr, &wr, &r))
23285 {
23286 int yb = window_text_bottom_y (w);
23287 struct glyph_row *row;
23288 int cursor_cleared_p;
23289 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23290
23291 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23292 r.x, r.y, r.width, r.height));
23293
23294 /* Convert to window coordinates. */
23295 r.x -= WINDOW_LEFT_EDGE_X (w);
23296 r.y -= WINDOW_TOP_EDGE_Y (w);
23297
23298 /* Turn off the cursor. */
23299 if (!w->pseudo_window_p
23300 && phys_cursor_in_rect_p (w, &r))
23301 {
23302 x_clear_cursor (w);
23303 cursor_cleared_p = 1;
23304 }
23305 else
23306 cursor_cleared_p = 0;
23307
23308 /* Update lines intersecting rectangle R. */
23309 first_overlapping_row = last_overlapping_row = NULL;
23310 for (row = w->current_matrix->rows;
23311 row->enabled_p;
23312 ++row)
23313 {
23314 int y0 = row->y;
23315 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23316
23317 if ((y0 >= r.y && y0 < r.y + r.height)
23318 || (y1 > r.y && y1 < r.y + r.height)
23319 || (r.y >= y0 && r.y < y1)
23320 || (r.y + r.height > y0 && r.y + r.height < y1))
23321 {
23322 /* A header line may be overlapping, but there is no need
23323 to fix overlapping areas for them. KFS 2005-02-12 */
23324 if (row->overlapping_p && !row->mode_line_p)
23325 {
23326 if (first_overlapping_row == NULL)
23327 first_overlapping_row = row;
23328 last_overlapping_row = row;
23329 }
23330
23331 if (expose_line (w, row, &r))
23332 mouse_face_overwritten_p = 1;
23333 }
23334
23335 if (y1 >= yb)
23336 break;
23337 }
23338
23339 /* Display the mode line if there is one. */
23340 if (WINDOW_WANTS_MODELINE_P (w)
23341 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23342 row->enabled_p)
23343 && row->y < r.y + r.height)
23344 {
23345 if (expose_line (w, row, &r))
23346 mouse_face_overwritten_p = 1;
23347 }
23348
23349 if (!w->pseudo_window_p)
23350 {
23351 /* Fix the display of overlapping rows. */
23352 if (first_overlapping_row)
23353 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23354
23355 /* Draw border between windows. */
23356 x_draw_vertical_border (w);
23357
23358 /* Turn the cursor on again. */
23359 if (cursor_cleared_p)
23360 update_window_cursor (w, 1);
23361 }
23362 }
23363
23364 return mouse_face_overwritten_p;
23365 }
23366
23367
23368
23369 /* Redraw (parts) of all windows in the window tree rooted at W that
23370 intersect R. R contains frame pixel coordinates. Value is
23371 non-zero if the exposure overwrites mouse-face. */
23372
23373 static int
23374 expose_window_tree (w, r)
23375 struct window *w;
23376 XRectangle *r;
23377 {
23378 struct frame *f = XFRAME (w->frame);
23379 int mouse_face_overwritten_p = 0;
23380
23381 while (w && !FRAME_GARBAGED_P (f))
23382 {
23383 if (!NILP (w->hchild))
23384 mouse_face_overwritten_p
23385 |= expose_window_tree (XWINDOW (w->hchild), r);
23386 else if (!NILP (w->vchild))
23387 mouse_face_overwritten_p
23388 |= expose_window_tree (XWINDOW (w->vchild), r);
23389 else
23390 mouse_face_overwritten_p |= expose_window (w, r);
23391
23392 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23393 }
23394
23395 return mouse_face_overwritten_p;
23396 }
23397
23398
23399 /* EXPORT:
23400 Redisplay an exposed area of frame F. X and Y are the upper-left
23401 corner of the exposed rectangle. W and H are width and height of
23402 the exposed area. All are pixel values. W or H zero means redraw
23403 the entire frame. */
23404
23405 void
23406 expose_frame (f, x, y, w, h)
23407 struct frame *f;
23408 int x, y, w, h;
23409 {
23410 XRectangle r;
23411 int mouse_face_overwritten_p = 0;
23412
23413 TRACE ((stderr, "expose_frame "));
23414
23415 /* No need to redraw if frame will be redrawn soon. */
23416 if (FRAME_GARBAGED_P (f))
23417 {
23418 TRACE ((stderr, " garbaged\n"));
23419 return;
23420 }
23421
23422 /* If basic faces haven't been realized yet, there is no point in
23423 trying to redraw anything. This can happen when we get an expose
23424 event while Emacs is starting, e.g. by moving another window. */
23425 if (FRAME_FACE_CACHE (f) == NULL
23426 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23427 {
23428 TRACE ((stderr, " no faces\n"));
23429 return;
23430 }
23431
23432 if (w == 0 || h == 0)
23433 {
23434 r.x = r.y = 0;
23435 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23436 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23437 }
23438 else
23439 {
23440 r.x = x;
23441 r.y = y;
23442 r.width = w;
23443 r.height = h;
23444 }
23445
23446 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23447 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23448
23449 if (WINDOWP (f->tool_bar_window))
23450 mouse_face_overwritten_p
23451 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23452
23453 #ifdef HAVE_X_WINDOWS
23454 #ifndef MSDOS
23455 #ifndef USE_X_TOOLKIT
23456 if (WINDOWP (f->menu_bar_window))
23457 mouse_face_overwritten_p
23458 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23459 #endif /* not USE_X_TOOLKIT */
23460 #endif
23461 #endif
23462
23463 /* Some window managers support a focus-follows-mouse style with
23464 delayed raising of frames. Imagine a partially obscured frame,
23465 and moving the mouse into partially obscured mouse-face on that
23466 frame. The visible part of the mouse-face will be highlighted,
23467 then the WM raises the obscured frame. With at least one WM, KDE
23468 2.1, Emacs is not getting any event for the raising of the frame
23469 (even tried with SubstructureRedirectMask), only Expose events.
23470 These expose events will draw text normally, i.e. not
23471 highlighted. Which means we must redo the highlight here.
23472 Subsume it under ``we love X''. --gerd 2001-08-15 */
23473 /* Included in Windows version because Windows most likely does not
23474 do the right thing if any third party tool offers
23475 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23476 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23477 {
23478 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23479 if (f == dpyinfo->mouse_face_mouse_frame)
23480 {
23481 int x = dpyinfo->mouse_face_mouse_x;
23482 int y = dpyinfo->mouse_face_mouse_y;
23483 clear_mouse_face (dpyinfo);
23484 note_mouse_highlight (f, x, y);
23485 }
23486 }
23487 }
23488
23489
23490 /* EXPORT:
23491 Determine the intersection of two rectangles R1 and R2. Return
23492 the intersection in *RESULT. Value is non-zero if RESULT is not
23493 empty. */
23494
23495 int
23496 x_intersect_rectangles (r1, r2, result)
23497 XRectangle *r1, *r2, *result;
23498 {
23499 XRectangle *left, *right;
23500 XRectangle *upper, *lower;
23501 int intersection_p = 0;
23502
23503 /* Rearrange so that R1 is the left-most rectangle. */
23504 if (r1->x < r2->x)
23505 left = r1, right = r2;
23506 else
23507 left = r2, right = r1;
23508
23509 /* X0 of the intersection is right.x0, if this is inside R1,
23510 otherwise there is no intersection. */
23511 if (right->x <= left->x + left->width)
23512 {
23513 result->x = right->x;
23514
23515 /* The right end of the intersection is the minimum of the
23516 the right ends of left and right. */
23517 result->width = (min (left->x + left->width, right->x + right->width)
23518 - result->x);
23519
23520 /* Same game for Y. */
23521 if (r1->y < r2->y)
23522 upper = r1, lower = r2;
23523 else
23524 upper = r2, lower = r1;
23525
23526 /* The upper end of the intersection is lower.y0, if this is inside
23527 of upper. Otherwise, there is no intersection. */
23528 if (lower->y <= upper->y + upper->height)
23529 {
23530 result->y = lower->y;
23531
23532 /* The lower end of the intersection is the minimum of the lower
23533 ends of upper and lower. */
23534 result->height = (min (lower->y + lower->height,
23535 upper->y + upper->height)
23536 - result->y);
23537 intersection_p = 1;
23538 }
23539 }
23540
23541 return intersection_p;
23542 }
23543
23544 #endif /* HAVE_WINDOW_SYSTEM */
23545
23546 \f
23547 /***********************************************************************
23548 Initialization
23549 ***********************************************************************/
23550
23551 void
23552 syms_of_xdisp ()
23553 {
23554 Vwith_echo_area_save_vector = Qnil;
23555 staticpro (&Vwith_echo_area_save_vector);
23556
23557 Vmessage_stack = Qnil;
23558 staticpro (&Vmessage_stack);
23559
23560 Qinhibit_redisplay = intern ("inhibit-redisplay");
23561 staticpro (&Qinhibit_redisplay);
23562
23563 message_dolog_marker1 = Fmake_marker ();
23564 staticpro (&message_dolog_marker1);
23565 message_dolog_marker2 = Fmake_marker ();
23566 staticpro (&message_dolog_marker2);
23567 message_dolog_marker3 = Fmake_marker ();
23568 staticpro (&message_dolog_marker3);
23569
23570 #if GLYPH_DEBUG
23571 defsubr (&Sdump_frame_glyph_matrix);
23572 defsubr (&Sdump_glyph_matrix);
23573 defsubr (&Sdump_glyph_row);
23574 defsubr (&Sdump_tool_bar_row);
23575 defsubr (&Strace_redisplay);
23576 defsubr (&Strace_to_stderr);
23577 #endif
23578 #ifdef HAVE_WINDOW_SYSTEM
23579 defsubr (&Stool_bar_lines_needed);
23580 defsubr (&Slookup_image_map);
23581 #endif
23582 defsubr (&Sformat_mode_line);
23583
23584 staticpro (&Qmenu_bar_update_hook);
23585 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23586
23587 staticpro (&Qoverriding_terminal_local_map);
23588 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23589
23590 staticpro (&Qoverriding_local_map);
23591 Qoverriding_local_map = intern ("overriding-local-map");
23592
23593 staticpro (&Qwindow_scroll_functions);
23594 Qwindow_scroll_functions = intern ("window-scroll-functions");
23595
23596 staticpro (&Qredisplay_end_trigger_functions);
23597 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23598
23599 staticpro (&Qinhibit_point_motion_hooks);
23600 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23601
23602 QCdata = intern (":data");
23603 staticpro (&QCdata);
23604 Qdisplay = intern ("display");
23605 staticpro (&Qdisplay);
23606 Qspace_width = intern ("space-width");
23607 staticpro (&Qspace_width);
23608 Qraise = intern ("raise");
23609 staticpro (&Qraise);
23610 Qslice = intern ("slice");
23611 staticpro (&Qslice);
23612 Qspace = intern ("space");
23613 staticpro (&Qspace);
23614 Qmargin = intern ("margin");
23615 staticpro (&Qmargin);
23616 Qpointer = intern ("pointer");
23617 staticpro (&Qpointer);
23618 Qleft_margin = intern ("left-margin");
23619 staticpro (&Qleft_margin);
23620 Qright_margin = intern ("right-margin");
23621 staticpro (&Qright_margin);
23622 Qcenter = intern ("center");
23623 staticpro (&Qcenter);
23624 Qline_height = intern ("line-height");
23625 staticpro (&Qline_height);
23626 QCalign_to = intern (":align-to");
23627 staticpro (&QCalign_to);
23628 QCrelative_width = intern (":relative-width");
23629 staticpro (&QCrelative_width);
23630 QCrelative_height = intern (":relative-height");
23631 staticpro (&QCrelative_height);
23632 QCeval = intern (":eval");
23633 staticpro (&QCeval);
23634 QCpropertize = intern (":propertize");
23635 staticpro (&QCpropertize);
23636 QCfile = intern (":file");
23637 staticpro (&QCfile);
23638 Qfontified = intern ("fontified");
23639 staticpro (&Qfontified);
23640 Qfontification_functions = intern ("fontification-functions");
23641 staticpro (&Qfontification_functions);
23642 Qtrailing_whitespace = intern ("trailing-whitespace");
23643 staticpro (&Qtrailing_whitespace);
23644 Qescape_glyph = intern ("escape-glyph");
23645 staticpro (&Qescape_glyph);
23646 Qnobreak_space = intern ("nobreak-space");
23647 staticpro (&Qnobreak_space);
23648 Qimage = intern ("image");
23649 staticpro (&Qimage);
23650 QCmap = intern (":map");
23651 staticpro (&QCmap);
23652 QCpointer = intern (":pointer");
23653 staticpro (&QCpointer);
23654 Qrect = intern ("rect");
23655 staticpro (&Qrect);
23656 Qcircle = intern ("circle");
23657 staticpro (&Qcircle);
23658 Qpoly = intern ("poly");
23659 staticpro (&Qpoly);
23660 Qmessage_truncate_lines = intern ("message-truncate-lines");
23661 staticpro (&Qmessage_truncate_lines);
23662 Qgrow_only = intern ("grow-only");
23663 staticpro (&Qgrow_only);
23664 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23665 staticpro (&Qinhibit_menubar_update);
23666 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23667 staticpro (&Qinhibit_eval_during_redisplay);
23668 Qposition = intern ("position");
23669 staticpro (&Qposition);
23670 Qbuffer_position = intern ("buffer-position");
23671 staticpro (&Qbuffer_position);
23672 Qobject = intern ("object");
23673 staticpro (&Qobject);
23674 Qbar = intern ("bar");
23675 staticpro (&Qbar);
23676 Qhbar = intern ("hbar");
23677 staticpro (&Qhbar);
23678 Qbox = intern ("box");
23679 staticpro (&Qbox);
23680 Qhollow = intern ("hollow");
23681 staticpro (&Qhollow);
23682 Qhand = intern ("hand");
23683 staticpro (&Qhand);
23684 Qarrow = intern ("arrow");
23685 staticpro (&Qarrow);
23686 Qtext = intern ("text");
23687 staticpro (&Qtext);
23688 Qrisky_local_variable = intern ("risky-local-variable");
23689 staticpro (&Qrisky_local_variable);
23690 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23691 staticpro (&Qinhibit_free_realized_faces);
23692
23693 list_of_error = Fcons (Fcons (intern ("error"),
23694 Fcons (intern ("void-variable"), Qnil)),
23695 Qnil);
23696 staticpro (&list_of_error);
23697
23698 Qlast_arrow_position = intern ("last-arrow-position");
23699 staticpro (&Qlast_arrow_position);
23700 Qlast_arrow_string = intern ("last-arrow-string");
23701 staticpro (&Qlast_arrow_string);
23702
23703 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23704 staticpro (&Qoverlay_arrow_string);
23705 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23706 staticpro (&Qoverlay_arrow_bitmap);
23707
23708 echo_buffer[0] = echo_buffer[1] = Qnil;
23709 staticpro (&echo_buffer[0]);
23710 staticpro (&echo_buffer[1]);
23711
23712 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23713 staticpro (&echo_area_buffer[0]);
23714 staticpro (&echo_area_buffer[1]);
23715
23716 Vmessages_buffer_name = build_string ("*Messages*");
23717 staticpro (&Vmessages_buffer_name);
23718
23719 mode_line_proptrans_alist = Qnil;
23720 staticpro (&mode_line_proptrans_alist);
23721 mode_line_string_list = Qnil;
23722 staticpro (&mode_line_string_list);
23723 mode_line_string_face = Qnil;
23724 staticpro (&mode_line_string_face);
23725 mode_line_string_face_prop = Qnil;
23726 staticpro (&mode_line_string_face_prop);
23727 Vmode_line_unwind_vector = Qnil;
23728 staticpro (&Vmode_line_unwind_vector);
23729
23730 help_echo_string = Qnil;
23731 staticpro (&help_echo_string);
23732 help_echo_object = Qnil;
23733 staticpro (&help_echo_object);
23734 help_echo_window = Qnil;
23735 staticpro (&help_echo_window);
23736 previous_help_echo_string = Qnil;
23737 staticpro (&previous_help_echo_string);
23738 help_echo_pos = -1;
23739
23740 #ifdef HAVE_WINDOW_SYSTEM
23741 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23742 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23743 For example, if a block cursor is over a tab, it will be drawn as
23744 wide as that tab on the display. */);
23745 x_stretch_cursor_p = 0;
23746 #endif
23747
23748 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23749 doc: /* *Non-nil means highlight trailing whitespace.
23750 The face used for trailing whitespace is `trailing-whitespace'. */);
23751 Vshow_trailing_whitespace = Qnil;
23752
23753 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23754 doc: /* *Control highlighting of nobreak space and soft hyphen.
23755 A value of t means highlight the character itself (for nobreak space,
23756 use face `nobreak-space').
23757 A value of nil means no highlighting.
23758 Other values mean display the escape glyph followed by an ordinary
23759 space or ordinary hyphen. */);
23760 Vnobreak_char_display = Qt;
23761
23762 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23763 doc: /* *The pointer shape to show in void text areas.
23764 A value of nil means to show the text pointer. Other options are `arrow',
23765 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23766 Vvoid_text_area_pointer = Qarrow;
23767
23768 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23769 doc: /* Non-nil means don't actually do any redisplay.
23770 This is used for internal purposes. */);
23771 Vinhibit_redisplay = Qnil;
23772
23773 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23774 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23775 Vglobal_mode_string = Qnil;
23776
23777 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23778 doc: /* Marker for where to display an arrow on top of the buffer text.
23779 This must be the beginning of a line in order to work.
23780 See also `overlay-arrow-string'. */);
23781 Voverlay_arrow_position = Qnil;
23782
23783 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23784 doc: /* String to display as an arrow in non-window frames.
23785 See also `overlay-arrow-position'. */);
23786 Voverlay_arrow_string = build_string ("=>");
23787
23788 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23789 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23790 The symbols on this list are examined during redisplay to determine
23791 where to display overlay arrows. */);
23792 Voverlay_arrow_variable_list
23793 = Fcons (intern ("overlay-arrow-position"), Qnil);
23794
23795 DEFVAR_INT ("scroll-step", &scroll_step,
23796 doc: /* *The number of lines to try scrolling a window by when point moves out.
23797 If that fails to bring point back on frame, point is centered instead.
23798 If this is zero, point is always centered after it moves off frame.
23799 If you want scrolling to always be a line at a time, you should set
23800 `scroll-conservatively' to a large value rather than set this to 1. */);
23801
23802 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23803 doc: /* *Scroll up to this many lines, to bring point back on screen.
23804 A value of zero means to scroll the text to center point vertically
23805 in the window. */);
23806 scroll_conservatively = 0;
23807
23808 DEFVAR_INT ("scroll-margin", &scroll_margin,
23809 doc: /* *Number of lines of margin at the top and bottom of a window.
23810 Recenter the window whenever point gets within this many lines
23811 of the top or bottom of the window. */);
23812 scroll_margin = 0;
23813
23814 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23815 doc: /* Pixels per inch value for non-window system displays.
23816 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23817 Vdisplay_pixels_per_inch = make_float (72.0);
23818
23819 #if GLYPH_DEBUG
23820 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23821 #endif
23822
23823 DEFVAR_BOOL ("truncate-partial-width-windows",
23824 &truncate_partial_width_windows,
23825 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23826 truncate_partial_width_windows = 1;
23827
23828 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23829 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23830 Any other value means to use the appropriate face, `mode-line',
23831 `header-line', or `menu' respectively. */);
23832 mode_line_inverse_video = 1;
23833
23834 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23835 doc: /* *Maximum buffer size for which line number should be displayed.
23836 If the buffer is bigger than this, the line number does not appear
23837 in the mode line. A value of nil means no limit. */);
23838 Vline_number_display_limit = Qnil;
23839
23840 DEFVAR_INT ("line-number-display-limit-width",
23841 &line_number_display_limit_width,
23842 doc: /* *Maximum line width (in characters) for line number display.
23843 If the average length of the lines near point is bigger than this, then the
23844 line number may be omitted from the mode line. */);
23845 line_number_display_limit_width = 200;
23846
23847 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23848 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23849 highlight_nonselected_windows = 0;
23850
23851 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23852 doc: /* Non-nil if more than one frame is visible on this display.
23853 Minibuffer-only frames don't count, but iconified frames do.
23854 This variable is not guaranteed to be accurate except while processing
23855 `frame-title-format' and `icon-title-format'. */);
23856
23857 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23858 doc: /* Template for displaying the title bar of visible frames.
23859 \(Assuming the window manager supports this feature.)
23860 This variable has the same structure as `mode-line-format' (which see),
23861 and is used only on frames for which no explicit name has been set
23862 \(see `modify-frame-parameters'). */);
23863
23864 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23865 doc: /* Template for displaying the title bar of an iconified frame.
23866 \(Assuming the window manager supports this feature.)
23867 This variable has the same structure as `mode-line-format' (which see),
23868 and is used only on frames for which no explicit name has been set
23869 \(see `modify-frame-parameters'). */);
23870 Vicon_title_format
23871 = Vframe_title_format
23872 = Fcons (intern ("multiple-frames"),
23873 Fcons (build_string ("%b"),
23874 Fcons (Fcons (empty_string,
23875 Fcons (intern ("invocation-name"),
23876 Fcons (build_string ("@"),
23877 Fcons (intern ("system-name"),
23878 Qnil)))),
23879 Qnil)));
23880
23881 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23882 doc: /* Maximum number of lines to keep in the message log buffer.
23883 If nil, disable message logging. If t, log messages but don't truncate
23884 the buffer when it becomes large. */);
23885 Vmessage_log_max = make_number (50);
23886
23887 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23888 doc: /* Functions called before redisplay, if window sizes have changed.
23889 The value should be a list of functions that take one argument.
23890 Just before redisplay, for each frame, if any of its windows have changed
23891 size since the last redisplay, or have been split or deleted,
23892 all the functions in the list are called, with the frame as argument. */);
23893 Vwindow_size_change_functions = Qnil;
23894
23895 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23896 doc: /* List of functions to call before redisplaying a window with scrolling.
23897 Each function is called with two arguments, the window
23898 and its new display-start position. Note that the value of `window-end'
23899 is not valid when these functions are called. */);
23900 Vwindow_scroll_functions = Qnil;
23901
23902 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
23903 doc: /* Functions called when redisplay of a window reaches the end trigger.
23904 Each function is called with two arguments, the window and the end trigger value.
23905 See `set-window-redisplay-end-trigger'. */);
23906 Vredisplay_end_trigger_functions = Qnil;
23907
23908 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23909 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23910 mouse_autoselect_window = 0;
23911
23912 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23913 doc: /* *Non-nil means automatically resize tool-bars.
23914 This increases a tool-bar's height if not all tool-bar items are visible.
23915 It decreases a tool-bar's height when it would display blank lines
23916 otherwise. */);
23917 auto_resize_tool_bars_p = 1;
23918
23919 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23920 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23921 auto_raise_tool_bar_buttons_p = 1;
23922
23923 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23924 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23925 make_cursor_line_fully_visible_p = 1;
23926
23927 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
23928 doc: /* *Border below tool-bar in pixels.
23929 If an integer, use it as the height of the border.
23930 If it is one of `internal-border-width' or `border-width', use the
23931 value of the corresponding frame parameter.
23932 Otherwise, no border is added below the tool-bar. */);
23933 Vtool_bar_border = Qinternal_border_width;
23934
23935 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23936 doc: /* *Margin around tool-bar buttons in pixels.
23937 If an integer, use that for both horizontal and vertical margins.
23938 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23939 HORZ specifying the horizontal margin, and VERT specifying the
23940 vertical margin. */);
23941 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23942
23943 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23944 doc: /* *Relief thickness of tool-bar buttons. */);
23945 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23946
23947 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23948 doc: /* List of functions to call to fontify regions of text.
23949 Each function is called with one argument POS. Functions must
23950 fontify a region starting at POS in the current buffer, and give
23951 fontified regions the property `fontified'. */);
23952 Vfontification_functions = Qnil;
23953 Fmake_variable_buffer_local (Qfontification_functions);
23954
23955 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23956 &unibyte_display_via_language_environment,
23957 doc: /* *Non-nil means display unibyte text according to language environment.
23958 Specifically this means that unibyte non-ASCII characters
23959 are displayed by converting them to the equivalent multibyte characters
23960 according to the current language environment. As a result, they are
23961 displayed according to the current fontset. */);
23962 unibyte_display_via_language_environment = 0;
23963
23964 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23965 doc: /* *Maximum height for resizing mini-windows.
23966 If a float, it specifies a fraction of the mini-window frame's height.
23967 If an integer, it specifies a number of lines. */);
23968 Vmax_mini_window_height = make_float (0.25);
23969
23970 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23971 doc: /* *How to resize mini-windows.
23972 A value of nil means don't automatically resize mini-windows.
23973 A value of t means resize them to fit the text displayed in them.
23974 A value of `grow-only', the default, means let mini-windows grow
23975 only, until their display becomes empty, at which point the windows
23976 go back to their normal size. */);
23977 Vresize_mini_windows = Qgrow_only;
23978
23979 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23980 doc: /* Alist specifying how to blink the cursor off.
23981 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23982 `cursor-type' frame-parameter or variable equals ON-STATE,
23983 comparing using `equal', Emacs uses OFF-STATE to specify
23984 how to blink it off. ON-STATE and OFF-STATE are values for
23985 the `cursor-type' frame parameter.
23986
23987 If a frame's ON-STATE has no entry in this list,
23988 the frame's other specifications determine how to blink the cursor off. */);
23989 Vblink_cursor_alist = Qnil;
23990
23991 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23992 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23993 automatic_hscrolling_p = 1;
23994
23995 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23996 doc: /* *How many columns away from the window edge point is allowed to get
23997 before automatic hscrolling will horizontally scroll the window. */);
23998 hscroll_margin = 5;
23999
24000 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24001 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24002 When point is less than `hscroll-margin' columns from the window
24003 edge, automatic hscrolling will scroll the window by the amount of columns
24004 determined by this variable. If its value is a positive integer, scroll that
24005 many columns. If it's a positive floating-point number, it specifies the
24006 fraction of the window's width to scroll. If it's nil or zero, point will be
24007 centered horizontally after the scroll. Any other value, including negative
24008 numbers, are treated as if the value were zero.
24009
24010 Automatic hscrolling always moves point outside the scroll margin, so if
24011 point was more than scroll step columns inside the margin, the window will
24012 scroll more than the value given by the scroll step.
24013
24014 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24015 and `scroll-right' overrides this variable's effect. */);
24016 Vhscroll_step = make_number (0);
24017
24018 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24019 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24020 Bind this around calls to `message' to let it take effect. */);
24021 message_truncate_lines = 0;
24022
24023 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24024 doc: /* Normal hook run to update the menu bar definitions.
24025 Redisplay runs this hook before it redisplays the menu bar.
24026 This is used to update submenus such as Buffers,
24027 whose contents depend on various data. */);
24028 Vmenu_bar_update_hook = Qnil;
24029
24030 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24031 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24032 inhibit_menubar_update = 0;
24033
24034 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24035 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24036 inhibit_eval_during_redisplay = 0;
24037
24038 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24039 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24040 inhibit_free_realized_faces = 0;
24041
24042 #if GLYPH_DEBUG
24043 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24044 doc: /* Inhibit try_window_id display optimization. */);
24045 inhibit_try_window_id = 0;
24046
24047 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24048 doc: /* Inhibit try_window_reusing display optimization. */);
24049 inhibit_try_window_reusing = 0;
24050
24051 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24052 doc: /* Inhibit try_cursor_movement display optimization. */);
24053 inhibit_try_cursor_movement = 0;
24054 #endif /* GLYPH_DEBUG */
24055 }
24056
24057
24058 /* Initialize this module when Emacs starts. */
24059
24060 void
24061 init_xdisp ()
24062 {
24063 Lisp_Object root_window;
24064 struct window *mini_w;
24065
24066 current_header_line_height = current_mode_line_height = -1;
24067
24068 CHARPOS (this_line_start_pos) = 0;
24069
24070 mini_w = XWINDOW (minibuf_window);
24071 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24072
24073 if (!noninteractive)
24074 {
24075 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24076 int i;
24077
24078 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24079 set_window_height (root_window,
24080 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24081 0);
24082 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24083 set_window_height (minibuf_window, 1, 0);
24084
24085 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24086 mini_w->total_cols = make_number (FRAME_COLS (f));
24087
24088 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24089 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24090 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24091
24092 /* The default ellipsis glyphs `...'. */
24093 for (i = 0; i < 3; ++i)
24094 default_invis_vector[i] = make_number ('.');
24095 }
24096
24097 {
24098 /* Allocate the buffer for frame titles.
24099 Also used for `format-mode-line'. */
24100 int size = 100;
24101 mode_line_noprop_buf = (char *) xmalloc (size);
24102 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24103 mode_line_noprop_ptr = mode_line_noprop_buf;
24104 mode_line_target = MODE_LINE_DISPLAY;
24105 }
24106
24107 help_echo_showing_p = 0;
24108 }
24109
24110
24111 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24112 (do not change this comment) */