Fix Lisp_Object/int type confusion revealed by making Lisp_Object a union type:
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the the description of direct update
37 operations, below.).
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay() +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
125
126 Iteration over things to be be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator
128 (or init_string_iterator for that matter). Calls to
129 get_next_display_element fill the iterator structure with relevant
130 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 #include "lisp.h"
173 #include "frame.h"
174 #include "window.h"
175 #include "termchar.h"
176 #include "dispextern.h"
177 #include "buffer.h"
178 #include "charset.h"
179 #include "indent.h"
180 #include "commands.h"
181 #include "macros.h"
182 #include "disptab.h"
183 #include "termhooks.h"
184 #include "intervals.h"
185 #include "keyboard.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
189
190 #ifdef HAVE_X_WINDOWS
191 #include "xterm.h"
192 #endif
193 #ifdef WINDOWSNT
194 #include "w32term.h"
195 #endif
196
197 #define min(a, b) ((a) < (b) ? (a) : (b))
198 #define max(a, b) ((a) > (b) ? (a) : (b))
199
200 #define INFINITY 10000000
201
202 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
203 extern void set_frame_menubar ();
204 extern int pending_menu_activation;
205 #endif
206
207 extern int interrupt_input;
208 extern int command_loop_level;
209
210 extern int minibuffer_auto_raise;
211
212 extern Lisp_Object Qface;
213
214 extern Lisp_Object Voverriding_local_map;
215 extern Lisp_Object Voverriding_local_map_menu_flag;
216 extern Lisp_Object Qmenu_item;
217
218 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
219 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
220 Lisp_Object Qredisplay_end_trigger_functions;
221 Lisp_Object Qinhibit_point_motion_hooks;
222 Lisp_Object QCeval, Qwhen, QCfile, QCdata;
223 Lisp_Object Qfontified;
224
225 /* Functions called to fontify regions of text. */
226
227 Lisp_Object Vfontification_functions;
228 Lisp_Object Qfontification_functions;
229
230 /* Non-zero means draw tool bar buttons raised when the mouse moves
231 over them. */
232
233 int auto_raise_tool_bar_buttons_p;
234
235 /* Margin around tool bar buttons in pixels. */
236
237 int tool_bar_button_margin;
238
239 /* Thickness of shadow to draw around tool bar buttons. */
240
241 int tool_bar_button_relief;
242
243 /* Non-zero means automatically resize tool-bars so that all tool-bar
244 items are visible, and no blank lines remain. */
245
246 int auto_resize_tool_bars_p;
247
248 /* Non-nil means don't actually do any redisplay. */
249
250 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
251
252 /* Names of text properties relevant for redisplay. */
253
254 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
255 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
256
257 /* Symbols used in text property values. */
258
259 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
260 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
261 Lisp_Object Qmargin;
262 extern Lisp_Object Qheight;
263
264 /* Non-nil means highlight trailing whitespace. */
265
266 Lisp_Object Vshow_trailing_whitespace;
267
268 /* Name of the face used to highlight trailing whitespace. */
269
270 Lisp_Object Qtrailing_whitespace;
271
272 /* The symbol `image' which is the car of the lists used to represent
273 images in Lisp. */
274
275 Lisp_Object Qimage;
276
277 /* Non-zero means print newline to stdout before next mini-buffer
278 message. */
279
280 int noninteractive_need_newline;
281
282 /* Non-zero means print newline to message log before next message. */
283
284 static int message_log_need_newline;
285
286 \f
287 /* The buffer position of the first character appearing entirely or
288 partially on the line of the selected window which contains the
289 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
290 redisplay optimization in redisplay_internal. */
291
292 static struct text_pos this_line_start_pos;
293
294 /* Number of characters past the end of the line above, including the
295 terminating newline. */
296
297 static struct text_pos this_line_end_pos;
298
299 /* The vertical positions and the height of this line. */
300
301 static int this_line_vpos;
302 static int this_line_y;
303 static int this_line_pixel_height;
304
305 /* X position at which this display line starts. Usually zero;
306 negative if first character is partially visible. */
307
308 static int this_line_start_x;
309
310 /* Buffer that this_line_.* variables are referring to. */
311
312 static struct buffer *this_line_buffer;
313
314 /* Nonzero means truncate lines in all windows less wide than the
315 frame. */
316
317 int truncate_partial_width_windows;
318
319 /* A flag to control how to display unibyte 8-bit character. */
320
321 int unibyte_display_via_language_environment;
322
323 /* Nonzero means we have more than one non-mini-buffer-only frame.
324 Not guaranteed to be accurate except while parsing
325 frame-title-format. */
326
327 int multiple_frames;
328
329 Lisp_Object Vglobal_mode_string;
330
331 /* Marker for where to display an arrow on top of the buffer text. */
332
333 Lisp_Object Voverlay_arrow_position;
334
335 /* String to display for the arrow. Only used on terminal frames. */
336
337 Lisp_Object Voverlay_arrow_string;
338
339 /* Values of those variables at last redisplay. However, if
340 Voverlay_arrow_position is a marker, last_arrow_position is its
341 numerical position. */
342
343 static Lisp_Object last_arrow_position, last_arrow_string;
344
345 /* Like mode-line-format, but for the title bar on a visible frame. */
346
347 Lisp_Object Vframe_title_format;
348
349 /* Like mode-line-format, but for the title bar on an iconified frame. */
350
351 Lisp_Object Vicon_title_format;
352
353 /* List of functions to call when a window's size changes. These
354 functions get one arg, a frame on which one or more windows' sizes
355 have changed. */
356
357 static Lisp_Object Vwindow_size_change_functions;
358
359 Lisp_Object Qmenu_bar_update_hook;
360
361 /* Nonzero if overlay arrow has been displayed once in this window. */
362
363 static int overlay_arrow_seen;
364
365 /* Nonzero means highlight the region even in nonselected windows. */
366
367 int highlight_nonselected_windows;
368
369 /* If cursor motion alone moves point off frame, try scrolling this
370 many lines up or down if that will bring it back. */
371
372 static int scroll_step;
373
374 /* Non-0 means scroll just far enough to bring point back on the
375 screen, when appropriate. */
376
377 static int scroll_conservatively;
378
379 /* Recenter the window whenever point gets within this many lines of
380 the top or bottom of the window. This value is translated into a
381 pixel value by multiplying it with CANON_Y_UNIT, which means that
382 there is really a fixed pixel height scroll margin. */
383
384 int scroll_margin;
385
386 /* Number of windows showing the buffer of the selected window (or
387 another buffer with the same base buffer). keyboard.c refers to
388 this. */
389
390 int buffer_shared;
391
392 /* Vector containing glyphs for an ellipsis `...'. */
393
394 static Lisp_Object default_invis_vector[3];
395
396 /* Nonzero means display mode line highlighted. */
397
398 int mode_line_inverse_video;
399
400 /* Prompt to display in front of the mini-buffer contents. */
401
402 Lisp_Object minibuf_prompt;
403
404 /* Width of current mini-buffer prompt. Only set after display_line
405 of the line that contains the prompt. */
406
407 int minibuf_prompt_width;
408 int minibuf_prompt_pixel_width;
409
410 /* This is the window where the echo area message was displayed. It
411 is always a mini-buffer window, but it may not be the same window
412 currently active as a mini-buffer. */
413
414 Lisp_Object echo_area_window;
415
416 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
417 pushes the current message and the value of
418 message_enable_multibyte on the stack, the function restore_message
419 pops the stack and displays MESSAGE again. */
420
421 Lisp_Object Vmessage_stack;
422
423 /* Nonzero means multibyte characters were enabled when the echo area
424 message was specified. */
425
426 int message_enable_multibyte;
427
428 /* True if we should redraw the mode lines on the next redisplay. */
429
430 int update_mode_lines;
431
432 /* Nonzero if window sizes or contents have changed since last
433 redisplay that finished */
434
435 int windows_or_buffers_changed;
436
437 /* Nonzero after display_mode_line if %l was used and it displayed a
438 line number. */
439
440 int line_number_displayed;
441
442 /* Maximum buffer size for which to display line numbers. */
443
444 static int line_number_display_limit;
445
446 /* line width to consider when repostioning for line number display */
447
448 static int line_number_display_limit_width;
449
450 /* Number of lines to keep in the message log buffer. t means
451 infinite. nil means don't log at all. */
452
453 Lisp_Object Vmessage_log_max;
454
455 /* Current, index 0, and last displayed echo area message. Either
456 buffers from echo_buffers, or nil to indicate no message. */
457
458 Lisp_Object echo_area_buffer[2];
459
460 /* The buffers referenced from echo_area_buffer. */
461
462 static Lisp_Object echo_buffer[2];
463
464 /* A vector saved used in with_area_buffer to reduce consing. */
465
466 static Lisp_Object Vwith_echo_area_save_vector;
467
468 /* Non-zero means display_echo_area should display the last echo area
469 message again. Set by redisplay_preserve_echo_area. */
470
471 static int display_last_displayed_message_p;
472
473 /* Nonzero if echo area is being used by print; zero if being used by
474 message. */
475
476 int message_buf_print;
477
478 /* Maximum height for resizing mini-windows. Either a float
479 specifying a fraction of the available height, or an integer
480 specifying a number of lines. */
481
482 static Lisp_Object Vmax_mini_window_height;
483
484 /* Non-zero means we want a hollow cursor in windows that are not
485 selected. Zero means there's no cursor in such windows. */
486
487 int cursor_in_non_selected_windows;
488
489 /* A scratch glyph row with contents used for generating truncation
490 glyphs. Also used in direct_output_for_insert. */
491
492 #define MAX_SCRATCH_GLYPHS 100
493 struct glyph_row scratch_glyph_row;
494 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
495
496 /* Ascent and height of the last line processed by move_it_to. */
497
498 static int last_max_ascent, last_height;
499
500 /* The maximum distance to look ahead for text properties. Values
501 that are too small let us call compute_char_face and similar
502 functions too often which is expensive. Values that are too large
503 let us call compute_char_face and alike too often because we
504 might not be interested in text properties that far away. */
505
506 #define TEXT_PROP_DISTANCE_LIMIT 100
507
508 /* Non-zero means print traces of redisplay if compiled with
509 GLYPH_DEBUG != 0. */
510
511 #if GLYPH_DEBUG
512 int trace_redisplay_p;
513 #endif
514
515 /* Value returned from text property handlers (see below). */
516
517 enum prop_handled
518 {
519 HANDLED_NORMALLY,
520 HANDLED_RECOMPUTE_PROPS,
521 HANDLED_OVERLAY_STRING_CONSUMED,
522 HANDLED_RETURN
523 };
524
525 /* A description of text properties that redisplay is interested
526 in. */
527
528 struct props
529 {
530 /* The name of the property. */
531 Lisp_Object *name;
532
533 /* A unique index for the property. */
534 enum prop_idx idx;
535
536 /* A handler function called to set up iterator IT from the property
537 at IT's current position. Value is used to steer handle_stop. */
538 enum prop_handled (*handler) P_ ((struct it *it));
539 };
540
541 static enum prop_handled handle_face_prop P_ ((struct it *));
542 static enum prop_handled handle_invisible_prop P_ ((struct it *));
543 static enum prop_handled handle_display_prop P_ ((struct it *));
544 static enum prop_handled handle_composition_prop P_ ((struct it *));
545 static enum prop_handled handle_overlay_change P_ ((struct it *));
546 static enum prop_handled handle_fontified_prop P_ ((struct it *));
547
548 /* Properties handled by iterators. */
549
550 static struct props it_props[] =
551 {
552 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
553 /* Handle `face' before `display' because some sub-properties of
554 `display' need to know the face. */
555 {&Qface, FACE_PROP_IDX, handle_face_prop},
556 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
557 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
558 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
559 {NULL, 0, NULL}
560 };
561
562 /* Value is the position described by X. If X is a marker, value is
563 the marker_position of X. Otherwise, value is X. */
564
565 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
566
567 /* Enumeration returned by some move_it_.* functions internally. */
568
569 enum move_it_result
570 {
571 /* Not used. Undefined value. */
572 MOVE_UNDEFINED,
573
574 /* Move ended at the requested buffer position or ZV. */
575 MOVE_POS_MATCH_OR_ZV,
576
577 /* Move ended at the requested X pixel position. */
578 MOVE_X_REACHED,
579
580 /* Move within a line ended at the end of a line that must be
581 continued. */
582 MOVE_LINE_CONTINUED,
583
584 /* Move within a line ended at the end of a line that would
585 be displayed truncated. */
586 MOVE_LINE_TRUNCATED,
587
588 /* Move within a line ended at a line end. */
589 MOVE_NEWLINE_OR_CR
590 };
591
592
593 \f
594 /* Function prototypes. */
595
596 static void ensure_echo_area_buffers P_ ((void));
597 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
598 struct glyph_row *,
599 struct glyph_row *));
600 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
601 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
602 static void clear_garbaged_frames P_ ((void));
603 static int current_message_1 P_ ((Lisp_Object *));
604 static int truncate_message_1 P_ ((int));
605 static int set_message_1 P_ ((char *s, Lisp_Object, int, int));
606 static int display_echo_area P_ ((struct window *));
607 static int display_echo_area_1 P_ ((struct window *));
608 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
609 static int string_char_and_length P_ ((unsigned char *, int, int *));
610 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
611 struct text_pos));
612 static int compute_window_start_on_continuation_line P_ ((struct window *));
613 static Lisp_Object eval_handler P_ ((Lisp_Object));
614 static Lisp_Object eval_form P_ ((Lisp_Object));
615 static void insert_left_trunc_glyphs P_ ((struct it *));
616 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
617 static void extend_face_to_end_of_line P_ ((struct it *));
618 static int append_space P_ ((struct it *, int));
619 static void make_cursor_line_fully_visible P_ ((struct window *));
620 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
621 static int trailing_whitespace_p P_ ((int));
622 static int message_log_check_duplicate P_ ((int, int, int, int));
623 int invisible_p P_ ((Lisp_Object, Lisp_Object));
624 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
625 static void push_it P_ ((struct it *));
626 static void pop_it P_ ((struct it *));
627 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
628 static void redisplay_internal P_ ((int));
629 static int echo_area_display P_ ((int));
630 static void redisplay_windows P_ ((Lisp_Object));
631 static void redisplay_window P_ ((Lisp_Object, int));
632 static void update_menu_bar P_ ((struct frame *, int));
633 static int try_window_reusing_current_matrix P_ ((struct window *));
634 static int try_window_id P_ ((struct window *));
635 static int display_line P_ ((struct it *));
636 static void display_mode_lines P_ ((struct window *));
637 static void display_mode_line P_ ((struct window *, enum face_id,
638 Lisp_Object));
639 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
640 static char *decode_mode_spec P_ ((struct window *, int, int, int));
641 static void display_menu_bar P_ ((struct window *));
642 static int display_count_lines P_ ((int, int, int, int, int *));
643 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
644 int, int, struct it *, int, int, int, int));
645 static void compute_line_metrics P_ ((struct it *));
646 static void run_redisplay_end_trigger_hook P_ ((struct it *));
647 static int get_overlay_strings P_ ((struct it *));
648 static void next_overlay_string P_ ((struct it *));
649 void set_iterator_to_next P_ ((struct it *));
650 static void reseat P_ ((struct it *, struct text_pos, int));
651 static void reseat_1 P_ ((struct it *, struct text_pos, int));
652 static void back_to_previous_visible_line_start P_ ((struct it *));
653 static void reseat_at_previous_visible_line_start P_ ((struct it *));
654 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
655 static int next_element_from_display_vector P_ ((struct it *));
656 static int next_element_from_string P_ ((struct it *));
657 static int next_element_from_c_string P_ ((struct it *));
658 static int next_element_from_buffer P_ ((struct it *));
659 static int next_element_from_composition P_ ((struct it *));
660 static int next_element_from_image P_ ((struct it *));
661 static int next_element_from_stretch P_ ((struct it *));
662 static void load_overlay_strings P_ ((struct it *));
663 static void init_from_display_pos P_ ((struct it *, struct window *,
664 struct display_pos *));
665 static void reseat_to_string P_ ((struct it *, unsigned char *,
666 Lisp_Object, int, int, int, int));
667 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
668 int, int, int));
669 void move_it_vertically_backward P_ ((struct it *, int));
670 static void init_to_row_start P_ ((struct it *, struct window *,
671 struct glyph_row *));
672 static void init_to_row_end P_ ((struct it *, struct window *,
673 struct glyph_row *));
674 static void back_to_previous_line_start P_ ((struct it *));
675 static void forward_to_next_line_start P_ ((struct it *));
676 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
677 Lisp_Object, int));
678 static struct text_pos string_pos P_ ((int, Lisp_Object));
679 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
680 static int number_of_chars P_ ((unsigned char *, int));
681 static void compute_stop_pos P_ ((struct it *));
682 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
683 Lisp_Object));
684 static int face_before_or_after_it_pos P_ ((struct it *, int));
685 static int next_overlay_change P_ ((int));
686 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
687 Lisp_Object, struct text_pos *));
688
689 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
690 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
691
692 #ifdef HAVE_WINDOW_SYSTEM
693
694 static void update_tool_bar P_ ((struct frame *, int));
695 static void build_desired_tool_bar_string P_ ((struct frame *f));
696 static int redisplay_tool_bar P_ ((struct frame *));
697 static void display_tool_bar_line P_ ((struct it *));
698
699 #endif /* HAVE_WINDOW_SYSTEM */
700
701 \f
702 /***********************************************************************
703 Window display dimensions
704 ***********************************************************************/
705
706 /* Return the window-relative maximum y + 1 for glyph rows displaying
707 text in window W. This is the height of W minus the height of a
708 mode line, if any. */
709
710 INLINE int
711 window_text_bottom_y (w)
712 struct window *w;
713 {
714 struct frame *f = XFRAME (w->frame);
715 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
716
717 if (WINDOW_WANTS_MODELINE_P (w))
718 height -= CURRENT_MODE_LINE_HEIGHT (w);
719 return height;
720 }
721
722
723 /* Return the pixel width of display area AREA of window W. AREA < 0
724 means return the total width of W, not including bitmap areas to
725 the left and right of the window. */
726
727 INLINE int
728 window_box_width (w, area)
729 struct window *w;
730 int area;
731 {
732 struct frame *f = XFRAME (w->frame);
733 int width = XFASTINT (w->width);
734
735 if (!w->pseudo_window_p)
736 {
737 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
738
739 if (area == TEXT_AREA)
740 {
741 if (INTEGERP (w->left_margin_width))
742 width -= XFASTINT (w->left_margin_width);
743 if (INTEGERP (w->right_margin_width))
744 width -= XFASTINT (w->right_margin_width);
745 }
746 else if (area == LEFT_MARGIN_AREA)
747 width = (INTEGERP (w->left_margin_width)
748 ? XFASTINT (w->left_margin_width) : 0);
749 else if (area == RIGHT_MARGIN_AREA)
750 width = (INTEGERP (w->right_margin_width)
751 ? XFASTINT (w->right_margin_width) : 0);
752 }
753
754 return width * CANON_X_UNIT (f);
755 }
756
757
758 /* Return the pixel height of the display area of window W, not
759 including mode lines of W, if any.. */
760
761 INLINE int
762 window_box_height (w)
763 struct window *w;
764 {
765 struct frame *f = XFRAME (w->frame);
766 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
767
768 if (WINDOW_WANTS_MODELINE_P (w))
769 height -= CURRENT_MODE_LINE_HEIGHT (w);
770
771 if (WINDOW_WANTS_HEADER_LINE_P (w))
772 height -= CURRENT_HEADER_LINE_HEIGHT (w);
773
774 return height;
775 }
776
777
778 /* Return the frame-relative coordinate of the left edge of display
779 area AREA of window W. AREA < 0 means return the left edge of the
780 whole window, to the right of any bitmap area at the left side of
781 W. */
782
783 INLINE int
784 window_box_left (w, area)
785 struct window *w;
786 int area;
787 {
788 struct frame *f = XFRAME (w->frame);
789 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
790
791 if (!w->pseudo_window_p)
792 {
793 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
794 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
795
796 if (area == TEXT_AREA)
797 x += window_box_width (w, LEFT_MARGIN_AREA);
798 else if (area == RIGHT_MARGIN_AREA)
799 x += (window_box_width (w, LEFT_MARGIN_AREA)
800 + window_box_width (w, TEXT_AREA));
801 }
802
803 return x;
804 }
805
806
807 /* Return the frame-relative coordinate of the right edge of display
808 area AREA of window W. AREA < 0 means return the left edge of the
809 whole window, to the left of any bitmap area at the right side of
810 W. */
811
812 INLINE int
813 window_box_right (w, area)
814 struct window *w;
815 int area;
816 {
817 return window_box_left (w, area) + window_box_width (w, area);
818 }
819
820
821 /* Get the bounding box of the display area AREA of window W, without
822 mode lines, in frame-relative coordinates. AREA < 0 means the
823 whole window, not including bitmap areas to the left and right of
824 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
825 coordinates of the upper-left corner of the box. Return in
826 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
827
828 INLINE void
829 window_box (w, area, box_x, box_y, box_width, box_height)
830 struct window *w;
831 int area;
832 int *box_x, *box_y, *box_width, *box_height;
833 {
834 struct frame *f = XFRAME (w->frame);
835
836 *box_width = window_box_width (w, area);
837 *box_height = window_box_height (w);
838 *box_x = window_box_left (w, area);
839 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
840 + XFASTINT (w->top) * CANON_Y_UNIT (f));
841 if (WINDOW_WANTS_HEADER_LINE_P (w))
842 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
843 }
844
845
846 /* Get the bounding box of the display area AREA of window W, without
847 mode lines. AREA < 0 means the whole window, not including bitmap
848 areas to the left and right of the window. Return in *TOP_LEFT_X
849 and TOP_LEFT_Y the frame-relative pixel coordinates of the
850 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
851 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
852 box. */
853
854 INLINE void
855 window_box_edges (w, area, top_left_x, top_left_y,
856 bottom_right_x, bottom_right_y)
857 struct window *w;
858 int area;
859 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
860 {
861 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
862 bottom_right_y);
863 *bottom_right_x += *top_left_x;
864 *bottom_right_y += *top_left_y;
865 }
866
867
868 \f
869 /***********************************************************************
870 Utilities
871 ***********************************************************************/
872
873 /* Return the next character from STR which is MAXLEN bytes long.
874 Return in *LEN the length of the character. This is like
875 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
876 we find one, we return a `?', but with the length of the illegal
877 character. */
878
879 static INLINE int
880 string_char_and_length (str, maxlen, len)
881 unsigned char *str;
882 int maxlen, *len;
883 {
884 int c;
885
886 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
887 if (!CHAR_VALID_P (c, 1))
888 /* We may not change the length here because other places in Emacs
889 don't use this function, i.e. they silently accept illegal
890 characters. */
891 c = '?';
892
893 return c;
894 }
895
896
897
898 /* Given a position POS containing a valid character and byte position
899 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
900
901 static struct text_pos
902 string_pos_nchars_ahead (pos, string, nchars)
903 struct text_pos pos;
904 Lisp_Object string;
905 int nchars;
906 {
907 xassert (STRINGP (string) && nchars >= 0);
908
909 if (STRING_MULTIBYTE (string))
910 {
911 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
912 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
913 int len;
914
915 while (nchars--)
916 {
917 string_char_and_length (p, rest, &len);
918 p += len, rest -= len;
919 xassert (rest >= 0);
920 CHARPOS (pos) += 1;
921 BYTEPOS (pos) += len;
922 }
923 }
924 else
925 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
926
927 return pos;
928 }
929
930
931 /* Value is the text position, i.e. character and byte position,
932 for character position CHARPOS in STRING. */
933
934 static INLINE struct text_pos
935 string_pos (charpos, string)
936 int charpos;
937 Lisp_Object string;
938 {
939 struct text_pos pos;
940 xassert (STRINGP (string));
941 xassert (charpos >= 0);
942 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
943 return pos;
944 }
945
946
947 /* Value is a text position, i.e. character and byte position, for
948 character position CHARPOS in C string S. MULTIBYTE_P non-zero
949 means recognize multibyte characters. */
950
951 static struct text_pos
952 c_string_pos (charpos, s, multibyte_p)
953 int charpos;
954 unsigned char *s;
955 int multibyte_p;
956 {
957 struct text_pos pos;
958
959 xassert (s != NULL);
960 xassert (charpos >= 0);
961
962 if (multibyte_p)
963 {
964 int rest = strlen (s), len;
965
966 SET_TEXT_POS (pos, 0, 0);
967 while (charpos--)
968 {
969 string_char_and_length (s, rest, &len);
970 s += len, rest -= len;
971 xassert (rest >= 0);
972 CHARPOS (pos) += 1;
973 BYTEPOS (pos) += len;
974 }
975 }
976 else
977 SET_TEXT_POS (pos, charpos, charpos);
978
979 return pos;
980 }
981
982
983 /* Value is the number of characters in C string S. MULTIBYTE_P
984 non-zero means recognize multibyte characters. */
985
986 static int
987 number_of_chars (s, multibyte_p)
988 unsigned char *s;
989 int multibyte_p;
990 {
991 int nchars;
992
993 if (multibyte_p)
994 {
995 int rest = strlen (s), len;
996 unsigned char *p = (unsigned char *) s;
997
998 for (nchars = 0; rest > 0; ++nchars)
999 {
1000 string_char_and_length (p, rest, &len);
1001 rest -= len, p += len;
1002 }
1003 }
1004 else
1005 nchars = strlen (s);
1006
1007 return nchars;
1008 }
1009
1010
1011 /* Compute byte position NEWPOS->bytepos corresponding to
1012 NEWPOS->charpos. POS is a known position in string STRING.
1013 NEWPOS->charpos must be >= POS.charpos. */
1014
1015 static void
1016 compute_string_pos (newpos, pos, string)
1017 struct text_pos *newpos, pos;
1018 Lisp_Object string;
1019 {
1020 xassert (STRINGP (string));
1021 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1022
1023 if (STRING_MULTIBYTE (string))
1024 *newpos = string_pos_nchars_ahead (pos, string,
1025 CHARPOS (*newpos) - CHARPOS (pos));
1026 else
1027 BYTEPOS (*newpos) = CHARPOS (*newpos);
1028 }
1029
1030
1031 \f
1032 /***********************************************************************
1033 Lisp form evaluation
1034 ***********************************************************************/
1035
1036 /* Error handler for eval_form. */
1037
1038 static Lisp_Object
1039 eval_handler (arg)
1040 Lisp_Object arg;
1041 {
1042 return Qnil;
1043 }
1044
1045
1046 /* Evaluate SEXPR and return the result, or nil if something went
1047 wrong. */
1048
1049 static Lisp_Object
1050 eval_form (sexpr)
1051 Lisp_Object sexpr;
1052 {
1053 int count = specpdl_ptr - specpdl;
1054 Lisp_Object val;
1055 specbind (Qinhibit_redisplay, Qt);
1056 val = internal_condition_case_1 (Feval, sexpr, Qerror, eval_handler);
1057 return unbind_to (count, val);
1058 }
1059
1060
1061 \f
1062 /***********************************************************************
1063 Debugging
1064 ***********************************************************************/
1065
1066 #if 0
1067
1068 /* Define CHECK_IT to perform sanity checks on iterators.
1069 This is for debugging. It is too slow to do unconditionally. */
1070
1071 static void
1072 check_it (it)
1073 struct it *it;
1074 {
1075 if (it->method == next_element_from_string)
1076 {
1077 xassert (STRINGP (it->string));
1078 xassert (IT_STRING_CHARPOS (*it) >= 0);
1079 }
1080 else if (it->method == next_element_from_buffer)
1081 {
1082 /* Check that character and byte positions agree. */
1083 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1084 }
1085
1086 if (it->dpvec)
1087 xassert (it->current.dpvec_index >= 0);
1088 else
1089 xassert (it->current.dpvec_index < 0);
1090 }
1091
1092 #define CHECK_IT(IT) check_it ((IT))
1093
1094 #else /* not 0 */
1095
1096 #define CHECK_IT(IT) (void) 0
1097
1098 #endif /* not 0 */
1099
1100
1101 #if GLYPH_DEBUG
1102
1103 /* Check that the window end of window W is what we expect it
1104 to be---the last row in the current matrix displaying text. */
1105
1106 static void
1107 check_window_end (w)
1108 struct window *w;
1109 {
1110 if (!MINI_WINDOW_P (w)
1111 && !NILP (w->window_end_valid))
1112 {
1113 struct glyph_row *row;
1114 xassert ((row = MATRIX_ROW (w->current_matrix,
1115 XFASTINT (w->window_end_vpos)),
1116 !row->enabled_p
1117 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1118 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1119 }
1120 }
1121
1122 #define CHECK_WINDOW_END(W) check_window_end ((W))
1123
1124 #else /* not GLYPH_DEBUG */
1125
1126 #define CHECK_WINDOW_END(W) (void) 0
1127
1128 #endif /* not GLYPH_DEBUG */
1129
1130
1131 \f
1132 /***********************************************************************
1133 Iterator initialization
1134 ***********************************************************************/
1135
1136 /* Initialize IT for displaying current_buffer in window W, starting
1137 at character position CHARPOS. CHARPOS < 0 means that no buffer
1138 position is specified which is useful when the iterator is assigned
1139 a position later. BYTEPOS is the byte position corresponding to
1140 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1141
1142 If ROW is not null, calls to produce_glyphs with IT as parameter
1143 will produce glyphs in that row.
1144
1145 BASE_FACE_ID is the id of a base face to use. It must be one of
1146 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1147 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1148 displaying the tool-bar.
1149
1150 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1151 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1152 corresponding mode line glyph row of the desired matrix of W. */
1153
1154 void
1155 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1156 struct it *it;
1157 struct window *w;
1158 int charpos, bytepos;
1159 struct glyph_row *row;
1160 enum face_id base_face_id;
1161 {
1162 int highlight_region_p;
1163
1164 /* Some precondition checks. */
1165 xassert (w != NULL && it != NULL);
1166 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1167
1168 /* If face attributes have been changed since the last redisplay,
1169 free realized faces now because they depend on face definitions
1170 that might have changed. */
1171 if (face_change_count)
1172 {
1173 face_change_count = 0;
1174 free_all_realized_faces (Qnil);
1175 }
1176
1177 /* Use one of the mode line rows of W's desired matrix if
1178 appropriate. */
1179 if (row == NULL)
1180 {
1181 if (base_face_id == MODE_LINE_FACE_ID)
1182 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1183 else if (base_face_id == HEADER_LINE_FACE_ID)
1184 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1185 }
1186
1187 /* Clear IT. */
1188 bzero (it, sizeof *it);
1189 it->current.overlay_string_index = -1;
1190 it->current.dpvec_index = -1;
1191 it->base_face_id = base_face_id;
1192
1193 /* The window in which we iterate over current_buffer: */
1194 XSETWINDOW (it->window, w);
1195 it->w = w;
1196 it->f = XFRAME (w->frame);
1197
1198 /* If realized faces have been removed, e.g. because of face
1199 attribute changes of named faces, recompute them. */
1200 if (FRAME_FACE_CACHE (it->f)->used == 0)
1201 recompute_basic_faces (it->f);
1202
1203 /* Current value of the `space-width', and 'height' properties. */
1204 it->space_width = Qnil;
1205 it->font_height = Qnil;
1206
1207 /* Are control characters displayed as `^C'? */
1208 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1209
1210 /* -1 means everything between a CR and the following line end
1211 is invisible. >0 means lines indented more than this value are
1212 invisible. */
1213 it->selective = (INTEGERP (current_buffer->selective_display)
1214 ? XFASTINT (current_buffer->selective_display)
1215 : (!NILP (current_buffer->selective_display)
1216 ? -1 : 0));
1217 it->selective_display_ellipsis_p
1218 = !NILP (current_buffer->selective_display_ellipses);
1219
1220 /* Display table to use. */
1221 it->dp = window_display_table (w);
1222
1223 /* Are multibyte characters enabled in current_buffer? */
1224 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1225
1226 /* Non-zero if we should highlight the region. */
1227 highlight_region_p
1228 = (!NILP (Vtransient_mark_mode)
1229 && !NILP (current_buffer->mark_active)
1230 && XMARKER (current_buffer->mark)->buffer != 0);
1231
1232 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1233 start and end of a visible region in window IT->w. Set both to
1234 -1 to indicate no region. */
1235 if (highlight_region_p
1236 /* Maybe highlight only in selected window. */
1237 && (/* Either show region everywhere. */
1238 highlight_nonselected_windows
1239 /* Or show region in the selected window. */
1240 || w == XWINDOW (selected_window)
1241 /* Or show the region if we are in the mini-buffer and W is
1242 the window the mini-buffer refers to. */
1243 || (MINI_WINDOW_P (XWINDOW (selected_window))
1244 && w == XWINDOW (Vminibuf_scroll_window))))
1245 {
1246 int charpos = marker_position (current_buffer->mark);
1247 it->region_beg_charpos = min (PT, charpos);
1248 it->region_end_charpos = max (PT, charpos);
1249 }
1250 else
1251 it->region_beg_charpos = it->region_end_charpos = -1;
1252
1253 /* Get the position at which the redisplay_end_trigger hook should
1254 be run, if it is to be run at all. */
1255 if (MARKERP (w->redisplay_end_trigger)
1256 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1257 it->redisplay_end_trigger_charpos
1258 = marker_position (w->redisplay_end_trigger);
1259 else if (INTEGERP (w->redisplay_end_trigger))
1260 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1261
1262 /* Correct bogus values of tab_width. */
1263 it->tab_width = XINT (current_buffer->tab_width);
1264 if (it->tab_width <= 0 || it->tab_width > 1000)
1265 it->tab_width = 8;
1266
1267 /* Are lines in the display truncated? */
1268 it->truncate_lines_p
1269 = (base_face_id != DEFAULT_FACE_ID
1270 || XINT (it->w->hscroll)
1271 || (truncate_partial_width_windows
1272 && !WINDOW_FULL_WIDTH_P (it->w))
1273 || !NILP (current_buffer->truncate_lines));
1274
1275 /* Get dimensions of truncation and continuation glyphs. These are
1276 displayed as bitmaps under X, so we don't need them for such
1277 frames. */
1278 if (!FRAME_WINDOW_P (it->f))
1279 {
1280 if (it->truncate_lines_p)
1281 {
1282 /* We will need the truncation glyph. */
1283 xassert (it->glyph_row == NULL);
1284 produce_special_glyphs (it, IT_TRUNCATION);
1285 it->truncation_pixel_width = it->pixel_width;
1286 }
1287 else
1288 {
1289 /* We will need the continuation glyph. */
1290 xassert (it->glyph_row == NULL);
1291 produce_special_glyphs (it, IT_CONTINUATION);
1292 it->continuation_pixel_width = it->pixel_width;
1293 }
1294
1295 /* Reset these values to zero becaue the produce_special_glyphs
1296 above has changed them. */
1297 it->pixel_width = it->ascent = it->descent = 0;
1298 it->phys_ascent = it->phys_descent = 0;
1299 }
1300
1301 /* Set this after getting the dimensions of truncation and
1302 continuation glyphs, so that we don't produce glyphs when calling
1303 produce_special_glyphs, above. */
1304 it->glyph_row = row;
1305 it->area = TEXT_AREA;
1306
1307 /* Get the dimensions of the display area. The display area
1308 consists of the visible window area plus a horizontally scrolled
1309 part to the left of the window. All x-values are relative to the
1310 start of this total display area. */
1311 if (base_face_id != DEFAULT_FACE_ID)
1312 {
1313 /* Mode lines, menu bar in terminal frames. */
1314 it->first_visible_x = 0;
1315 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1316 }
1317 else
1318 {
1319 it->first_visible_x
1320 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1321 it->last_visible_x = (it->first_visible_x
1322 + window_box_width (w, TEXT_AREA));
1323
1324 /* If we truncate lines, leave room for the truncator glyph(s) at
1325 the right margin. Otherwise, leave room for the continuation
1326 glyph(s). Truncation and continuation glyphs are not inserted
1327 for window-based redisplay. */
1328 if (!FRAME_WINDOW_P (it->f))
1329 {
1330 if (it->truncate_lines_p)
1331 it->last_visible_x -= it->truncation_pixel_width;
1332 else
1333 it->last_visible_x -= it->continuation_pixel_width;
1334 }
1335
1336 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1337 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1338 }
1339
1340 /* Leave room for a border glyph. */
1341 if (!FRAME_WINDOW_P (it->f)
1342 && !WINDOW_RIGHTMOST_P (it->w))
1343 it->last_visible_x -= 1;
1344
1345 it->last_visible_y = window_text_bottom_y (w);
1346
1347 /* For mode lines and alike, arrange for the first glyph having a
1348 left box line if the face specifies a box. */
1349 if (base_face_id != DEFAULT_FACE_ID)
1350 {
1351 struct face *face;
1352
1353 it->face_id = base_face_id;
1354
1355 /* If we have a boxed mode line, make the first character appear
1356 with a left box line. */
1357 face = FACE_FROM_ID (it->f, base_face_id);
1358 if (face->box != FACE_NO_BOX)
1359 it->start_of_box_run_p = 1;
1360 }
1361
1362 /* If a buffer position was specified, set the iterator there,
1363 getting overlays and face properties from that position. */
1364 if (charpos > 0)
1365 {
1366 it->end_charpos = ZV;
1367 it->face_id = -1;
1368 IT_CHARPOS (*it) = charpos;
1369
1370 /* Compute byte position if not specified. */
1371 if (bytepos <= 0)
1372 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1373 else
1374 IT_BYTEPOS (*it) = bytepos;
1375
1376 /* Compute faces etc. */
1377 reseat (it, it->current.pos, 1);
1378 }
1379
1380 CHECK_IT (it);
1381 }
1382
1383
1384 /* Initialize IT for the display of window W with window start POS. */
1385
1386 void
1387 start_display (it, w, pos)
1388 struct it *it;
1389 struct window *w;
1390 struct text_pos pos;
1391 {
1392 int start_at_line_beg_p;
1393 struct glyph_row *row;
1394 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1395 int first_y;
1396
1397 row = w->desired_matrix->rows + first_vpos;
1398 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1399 first_y = it->current_y;
1400
1401 /* If window start is not at a line start, move back to the line
1402 start. This makes sure that we take continuation lines into
1403 account. */
1404 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1405 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1406 if (!start_at_line_beg_p)
1407 reseat_at_previous_visible_line_start (it);
1408
1409 /* If window start is not at a line start, skip forward to POS to
1410 get the correct continuation_lines_width and current_x. */
1411 if (!start_at_line_beg_p)
1412 {
1413 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1414
1415 /* If lines are continued, this line may end in the middle of a
1416 multi-glyph character (e.g. a control character displayed as
1417 \003, or in the middle of an overlay string). In this case
1418 move_it_to above will not have taken us to the start of
1419 the continuation line but to the end of the continued line. */
1420 if (!it->truncate_lines_p && it->current_x > 0)
1421 {
1422 if (it->current.dpvec_index >= 0
1423 || it->current.overlay_string_index >= 0)
1424 {
1425 set_iterator_to_next (it);
1426 move_it_in_display_line_to (it, -1, -1, 0);
1427 }
1428 it->continuation_lines_width += it->current_x;
1429 }
1430
1431 it->current_y = first_y;
1432 it->vpos = 0;
1433 it->current_x = it->hpos = 0;
1434 }
1435
1436 #if 0 /* Don't assert the following because start_display is sometimes
1437 called intentionally with a window start that is not at a
1438 line start. Please leave this code in as a comment. */
1439
1440 /* Window start should be on a line start, now. */
1441 xassert (it->continuation_lines_width
1442 || IT_CHARPOS (it) == BEGV
1443 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1444 #endif /* 0 */
1445 }
1446
1447
1448 /* Initialize IT for stepping through current_buffer in window W,
1449 starting at position POS that includes overlay string and display
1450 vector/ control character translation position information. */
1451
1452 static void
1453 init_from_display_pos (it, w, pos)
1454 struct it *it;
1455 struct window *w;
1456 struct display_pos *pos;
1457 {
1458 /* Keep in mind: the call to reseat in init_iterator skips invisible
1459 text, so we might end up at a position different from POS. This
1460 is only a problem when POS is a row start after a newline and an
1461 overlay starts there with an after-string, and the overlay has an
1462 invisible property. Since we don't skip invisible text in
1463 display_line and elsewhere immediately after consuming the
1464 newline before the row start, such a POS will not be in a string,
1465 but the call to init_iterator below will move us to the
1466 after-string. */
1467 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1468 NULL, DEFAULT_FACE_ID);
1469
1470 /* If position is within an overlay string, set up IT to
1471 the right overlay string. */
1472 if (pos->overlay_string_index >= 0)
1473 {
1474 int relative_index;
1475
1476 /* We already have the first chunk of overlay strings in
1477 IT->overlay_strings. Load more until the one for
1478 pos->overlay_string_index is in IT->overlay_strings. */
1479 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1480 {
1481 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1482 it->current.overlay_string_index = 0;
1483 while (n--)
1484 {
1485 load_overlay_strings (it);
1486 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1487 }
1488 }
1489
1490 it->current.overlay_string_index = pos->overlay_string_index;
1491 relative_index = (it->current.overlay_string_index
1492 % OVERLAY_STRING_CHUNK_SIZE);
1493 it->string = it->overlay_strings[relative_index];
1494 it->current.string_pos = pos->string_pos;
1495 it->method = next_element_from_string;
1496 }
1497 else if (CHARPOS (pos->string_pos) >= 0)
1498 {
1499 /* Recorded position is not in an overlay string, but in another
1500 string. This can only be a string from a `display' property.
1501 IT should already be filled with that string. */
1502 it->current.string_pos = pos->string_pos;
1503 xassert (STRINGP (it->string));
1504 }
1505
1506 /* Restore position in display vector translations or control
1507 character translations. */
1508 if (pos->dpvec_index >= 0)
1509 {
1510 /* This fills IT->dpvec. */
1511 get_next_display_element (it);
1512 xassert (it->dpvec && it->current.dpvec_index == 0);
1513 it->current.dpvec_index = pos->dpvec_index;
1514 }
1515
1516 CHECK_IT (it);
1517 }
1518
1519
1520 /* Initialize IT for stepping through current_buffer in window W
1521 starting at ROW->start. */
1522
1523 static void
1524 init_to_row_start (it, w, row)
1525 struct it *it;
1526 struct window *w;
1527 struct glyph_row *row;
1528 {
1529 init_from_display_pos (it, w, &row->start);
1530 it->continuation_lines_width = row->continuation_lines_width;
1531 CHECK_IT (it);
1532 }
1533
1534
1535 /* Initialize IT for stepping through current_buffer in window W
1536 starting in the line following ROW, i.e. starting at ROW->end. */
1537
1538 static void
1539 init_to_row_end (it, w, row)
1540 struct it *it;
1541 struct window *w;
1542 struct glyph_row *row;
1543 {
1544 init_from_display_pos (it, w, &row->end);
1545
1546 if (row->continued_p)
1547 it->continuation_lines_width = (row->continuation_lines_width
1548 + row->pixel_width);
1549 CHECK_IT (it);
1550 }
1551
1552
1553
1554 \f
1555 /***********************************************************************
1556 Text properties
1557 ***********************************************************************/
1558
1559 /* Called when IT reaches IT->stop_charpos. Handle text property and
1560 overlay changes. Set IT->stop_charpos to the next position where
1561 to stop. */
1562
1563 static void
1564 handle_stop (it)
1565 struct it *it;
1566 {
1567 enum prop_handled handled;
1568 int handle_overlay_change_p = 1;
1569 struct props *p;
1570
1571 it->dpvec = NULL;
1572 it->current.dpvec_index = -1;
1573
1574 do
1575 {
1576 handled = HANDLED_NORMALLY;
1577
1578 /* Call text property handlers. */
1579 for (p = it_props; p->handler; ++p)
1580 {
1581 handled = p->handler (it);
1582
1583 if (handled == HANDLED_RECOMPUTE_PROPS)
1584 break;
1585 else if (handled == HANDLED_RETURN)
1586 return;
1587 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1588 handle_overlay_change_p = 0;
1589 }
1590
1591 if (handled != HANDLED_RECOMPUTE_PROPS)
1592 {
1593 /* Don't check for overlay strings below when set to deliver
1594 characters from a display vector. */
1595 if (it->method == next_element_from_display_vector)
1596 handle_overlay_change_p = 0;
1597
1598 /* Handle overlay changes. */
1599 if (handle_overlay_change_p)
1600 handled = handle_overlay_change (it);
1601
1602 /* Determine where to stop next. */
1603 if (handled == HANDLED_NORMALLY)
1604 compute_stop_pos (it);
1605 }
1606 }
1607 while (handled == HANDLED_RECOMPUTE_PROPS);
1608 }
1609
1610
1611 /* Compute IT->stop_charpos from text property and overlay change
1612 information for IT's current position. */
1613
1614 static void
1615 compute_stop_pos (it)
1616 struct it *it;
1617 {
1618 register INTERVAL iv, next_iv;
1619 Lisp_Object object, limit, position;
1620
1621 /* If nowhere else, stop at the end. */
1622 it->stop_charpos = it->end_charpos;
1623
1624 if (STRINGP (it->string))
1625 {
1626 /* Strings are usually short, so don't limit the search for
1627 properties. */
1628 object = it->string;
1629 limit = Qnil;
1630 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1631 }
1632 else
1633 {
1634 int charpos;
1635
1636 /* If next overlay change is in front of the current stop pos
1637 (which is IT->end_charpos), stop there. Note: value of
1638 next_overlay_change is point-max if no overlay change
1639 follows. */
1640 charpos = next_overlay_change (IT_CHARPOS (*it));
1641 if (charpos < it->stop_charpos)
1642 it->stop_charpos = charpos;
1643
1644 /* If showing the region, we have to stop at the region
1645 start or end because the face might change there. */
1646 if (it->region_beg_charpos > 0)
1647 {
1648 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1649 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1650 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1651 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1652 }
1653
1654 /* Set up variables for computing the stop position from text
1655 property changes. */
1656 XSETBUFFER (object, current_buffer);
1657 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1658 XSETFASTINT (position, IT_CHARPOS (*it));
1659
1660 }
1661
1662 /* Get the interval containing IT's position. Value is a null
1663 interval if there isn't such an interval. */
1664 iv = validate_interval_range (object, &position, &position, 0);
1665 if (!NULL_INTERVAL_P (iv))
1666 {
1667 Lisp_Object values_here[LAST_PROP_IDX];
1668 struct props *p;
1669
1670 /* Get properties here. */
1671 for (p = it_props; p->handler; ++p)
1672 values_here[p->idx] = textget (iv->plist, *p->name);
1673
1674 /* Look for an interval following iv that has different
1675 properties. */
1676 for (next_iv = next_interval (iv);
1677 (!NULL_INTERVAL_P (next_iv)
1678 && (NILP (limit)
1679 || XFASTINT (limit) > next_iv->position));
1680 next_iv = next_interval (next_iv))
1681 {
1682 for (p = it_props; p->handler; ++p)
1683 {
1684 Lisp_Object new_value;
1685
1686 new_value = textget (next_iv->plist, *p->name);
1687 if (!EQ (values_here[p->idx], new_value))
1688 break;
1689 }
1690
1691 if (p->handler)
1692 break;
1693 }
1694
1695 if (!NULL_INTERVAL_P (next_iv))
1696 {
1697 if (INTEGERP (limit)
1698 && next_iv->position >= XFASTINT (limit))
1699 /* No text property change up to limit. */
1700 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1701 else
1702 /* Text properties change in next_iv. */
1703 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1704 }
1705 }
1706
1707 xassert (STRINGP (it->string)
1708 || (it->stop_charpos >= BEGV
1709 && it->stop_charpos >= IT_CHARPOS (*it)));
1710 }
1711
1712
1713 /* Return the position of the next overlay change after POS in
1714 current_buffer. Value is point-max if no overlay change
1715 follows. This is like `next-overlay-change' but doesn't use
1716 xmalloc. */
1717
1718 static int
1719 next_overlay_change (pos)
1720 int pos;
1721 {
1722 int noverlays;
1723 int endpos;
1724 Lisp_Object *overlays;
1725 int len;
1726 int i;
1727
1728 /* Get all overlays at the given position. */
1729 len = 10;
1730 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1731 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1732 if (noverlays > len)
1733 {
1734 len = noverlays;
1735 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1736 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1737 }
1738
1739 /* If any of these overlays ends before endpos,
1740 use its ending point instead. */
1741 for (i = 0; i < noverlays; ++i)
1742 {
1743 Lisp_Object oend;
1744 int oendpos;
1745
1746 oend = OVERLAY_END (overlays[i]);
1747 oendpos = OVERLAY_POSITION (oend);
1748 endpos = min (endpos, oendpos);
1749 }
1750
1751 return endpos;
1752 }
1753
1754
1755 \f
1756 /***********************************************************************
1757 Fontification
1758 ***********************************************************************/
1759
1760 /* Handle changes in the `fontified' property of the current buffer by
1761 calling hook functions from Qfontification_functions to fontify
1762 regions of text. */
1763
1764 static enum prop_handled
1765 handle_fontified_prop (it)
1766 struct it *it;
1767 {
1768 Lisp_Object prop, pos;
1769 enum prop_handled handled = HANDLED_NORMALLY;
1770 struct gcpro gcpro1;
1771
1772 /* Get the value of the `fontified' property at IT's current buffer
1773 position. (The `fontified' property doesn't have a special
1774 meaning in strings.) If the value is nil, call functions from
1775 Qfontification_functions. */
1776 if (!STRINGP (it->string)
1777 && it->s == NULL
1778 && !NILP (Vfontification_functions)
1779 && (pos = make_number (IT_CHARPOS (*it)),
1780 prop = Fget_char_property (pos, Qfontified, Qnil),
1781 NILP (prop)))
1782 {
1783 Lisp_Object args[2];
1784
1785 GCPRO1 (pos);
1786 /* Run the hook functions. */
1787 args[0] = Qfontification_functions;
1788 args[1] = pos;
1789 Frun_hook_with_args (2, args);
1790
1791 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
1792 something. This avoids an endless loop if they failed to
1793 fontify the text for which reason ever. */
1794 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
1795 handled = HANDLED_RECOMPUTE_PROPS;
1796 UNGCPRO;
1797 }
1798
1799 return handled;
1800 }
1801
1802
1803 \f
1804 /***********************************************************************
1805 Faces
1806 ***********************************************************************/
1807
1808 /* Set up iterator IT from face properties at its current position.
1809 Called from handle_stop. */
1810
1811 static enum prop_handled
1812 handle_face_prop (it)
1813 struct it *it;
1814 {
1815 int new_face_id, next_stop;
1816
1817 if (!STRINGP (it->string))
1818 {
1819 new_face_id
1820 = face_at_buffer_position (it->w,
1821 IT_CHARPOS (*it),
1822 it->region_beg_charpos,
1823 it->region_end_charpos,
1824 &next_stop,
1825 (IT_CHARPOS (*it)
1826 + TEXT_PROP_DISTANCE_LIMIT),
1827 0);
1828
1829 /* Is this a start of a run of characters with box face?
1830 Caveat: this can be called for a freshly initialized
1831 iterator; face_id is -1 is this case. We know that the new
1832 face will not change until limit, i.e. if the new face has a
1833 box, all characters up to limit will have one. But, as
1834 usual, we don't know whether limit is really the end. */
1835 if (new_face_id != it->face_id)
1836 {
1837 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1838
1839 /* If new face has a box but old face has not, this is
1840 the start of a run of characters with box, i.e. it has
1841 a shadow on the left side. The value of face_id of the
1842 iterator will be -1 if this is the initial call that gets
1843 the face. In this case, we have to look in front of IT's
1844 position and see whether there is a face != new_face_id. */
1845 it->start_of_box_run_p
1846 = (new_face->box != FACE_NO_BOX
1847 && (it->face_id >= 0
1848 || IT_CHARPOS (*it) == BEG
1849 || new_face_id != face_before_it_pos (it)));
1850 it->face_box_p = new_face->box != FACE_NO_BOX;
1851 }
1852 }
1853 else
1854 {
1855 new_face_id
1856 = face_at_string_position (it->w,
1857 it->string,
1858 IT_STRING_CHARPOS (*it),
1859 (it->current.overlay_string_index >= 0
1860 ? IT_CHARPOS (*it)
1861 : 0),
1862 it->region_beg_charpos,
1863 it->region_end_charpos,
1864 &next_stop,
1865 it->base_face_id);
1866
1867 #if 0 /* This shouldn't be neccessary. Let's check it. */
1868 /* If IT is used to display a mode line we would really like to
1869 use the mode line face instead of the frame's default face. */
1870 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
1871 && new_face_id == DEFAULT_FACE_ID)
1872 new_face_id = MODE_LINE_FACE_ID;
1873 #endif
1874
1875 /* Is this a start of a run of characters with box? Caveat:
1876 this can be called for a freshly allocated iterator; face_id
1877 is -1 is this case. We know that the new face will not
1878 change until the next check pos, i.e. if the new face has a
1879 box, all characters up to that position will have a
1880 box. But, as usual, we don't know whether that position
1881 is really the end. */
1882 if (new_face_id != it->face_id)
1883 {
1884 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1885 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
1886
1887 /* If new face has a box but old face hasn't, this is the
1888 start of a run of characters with box, i.e. it has a
1889 shadow on the left side. */
1890 it->start_of_box_run_p
1891 = new_face->box && (old_face == NULL || !old_face->box);
1892 it->face_box_p = new_face->box != FACE_NO_BOX;
1893 }
1894 }
1895
1896 it->face_id = new_face_id;
1897 return HANDLED_NORMALLY;
1898 }
1899
1900
1901 /* Compute the face one character before or after the current position
1902 of IT. BEFORE_P non-zero means get the face in front of IT's
1903 position. Value is the id of the face. */
1904
1905 static int
1906 face_before_or_after_it_pos (it, before_p)
1907 struct it *it;
1908 int before_p;
1909 {
1910 int face_id, limit;
1911 int next_check_charpos;
1912 struct text_pos pos;
1913
1914 xassert (it->s == NULL);
1915
1916 if (STRINGP (it->string))
1917 {
1918 /* No face change past the end of the string (for the case
1919 we are padding with spaces). No face change before the
1920 string start. */
1921 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
1922 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
1923 return it->face_id;
1924
1925 /* Set pos to the position before or after IT's current position. */
1926 if (before_p)
1927 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
1928 else
1929 /* For composition, we must check the character after the
1930 composition. */
1931 pos = (it->what == IT_COMPOSITION
1932 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
1933 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
1934
1935 /* Get the face for ASCII, or unibyte. */
1936 face_id
1937 = face_at_string_position (it->w,
1938 it->string,
1939 CHARPOS (pos),
1940 (it->current.overlay_string_index >= 0
1941 ? IT_CHARPOS (*it)
1942 : 0),
1943 it->region_beg_charpos,
1944 it->region_end_charpos,
1945 &next_check_charpos,
1946 it->base_face_id);
1947
1948 /* Correct the face for charsets different from ASCII. Do it
1949 for the multibyte case only. The face returned above is
1950 suitable for unibyte text if IT->string is unibyte. */
1951 if (STRING_MULTIBYTE (it->string))
1952 {
1953 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
1954 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
1955 int c, len;
1956 struct face *face = FACE_FROM_ID (it->f, face_id);
1957
1958 c = string_char_and_length (p, rest, &len);
1959 face_id = FACE_FOR_CHAR (it->f, face, c);
1960 }
1961 }
1962 else
1963 {
1964 if ((IT_CHARPOS (*it) >= ZV && !before_p)
1965 || (IT_CHARPOS (*it) <= BEGV && before_p))
1966 return it->face_id;
1967
1968 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
1969 pos = it->current.pos;
1970
1971 if (before_p)
1972 DEC_TEXT_POS (pos, it->multibyte_p);
1973 else
1974 {
1975 if (it->what == IT_COMPOSITION)
1976 /* For composition, we must check the position after the
1977 composition. */
1978 pos.charpos += it->cmp_len, pos.bytepos += it->len;
1979 else
1980 INC_TEXT_POS (pos, it->multibyte_p);
1981 }
1982 /* Determine face for CHARSET_ASCII, or unibyte. */
1983 face_id = face_at_buffer_position (it->w,
1984 CHARPOS (pos),
1985 it->region_beg_charpos,
1986 it->region_end_charpos,
1987 &next_check_charpos,
1988 limit, 0);
1989
1990 /* Correct the face for charsets different from ASCII. Do it
1991 for the multibyte case only. The face returned above is
1992 suitable for unibyte text if current_buffer is unibyte. */
1993 if (it->multibyte_p)
1994 {
1995 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
1996 struct face *face = FACE_FROM_ID (it->f, face_id);
1997 face_id = FACE_FOR_CHAR (it->f, face, c);
1998 }
1999 }
2000
2001 return face_id;
2002 }
2003
2004
2005 \f
2006 /***********************************************************************
2007 Invisible text
2008 ***********************************************************************/
2009
2010 /* Set up iterator IT from invisible properties at its current
2011 position. Called from handle_stop. */
2012
2013 static enum prop_handled
2014 handle_invisible_prop (it)
2015 struct it *it;
2016 {
2017 enum prop_handled handled = HANDLED_NORMALLY;
2018
2019 if (STRINGP (it->string))
2020 {
2021 extern Lisp_Object Qinvisible;
2022 Lisp_Object prop, end_charpos, limit, charpos;
2023
2024 /* Get the value of the invisible text property at the
2025 current position. Value will be nil if there is no such
2026 property. */
2027 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2028 prop = Fget_text_property (charpos, Qinvisible, it->string);
2029
2030 if (!NILP (prop))
2031 {
2032 handled = HANDLED_RECOMPUTE_PROPS;
2033
2034 /* Get the position at which the next change of the
2035 invisible text property can be found in IT->string.
2036 Value will be nil if the property value is the same for
2037 all the rest of IT->string. */
2038 XSETINT (limit, XSTRING (it->string)->size);
2039 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2040 it->string, limit);
2041
2042 /* Text at current position is invisible. The next
2043 change in the property is at position end_charpos.
2044 Move IT's current position to that position. */
2045 if (INTEGERP (end_charpos)
2046 && XFASTINT (end_charpos) < XFASTINT (limit))
2047 {
2048 struct text_pos old;
2049 old = it->current.string_pos;
2050 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2051 compute_string_pos (&it->current.string_pos, old, it->string);
2052 }
2053 else
2054 {
2055 /* The rest of the string is invisible. If this is an
2056 overlay string, proceed with the next overlay string
2057 or whatever comes and return a character from there. */
2058 if (it->current.overlay_string_index >= 0)
2059 {
2060 next_overlay_string (it);
2061 /* Don't check for overlay strings when we just
2062 finished processing them. */
2063 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2064 }
2065 else
2066 {
2067 struct Lisp_String *s = XSTRING (it->string);
2068 IT_STRING_CHARPOS (*it) = s->size;
2069 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2070 }
2071 }
2072 }
2073 }
2074 else
2075 {
2076 int visible_p, newpos, next_stop;
2077 Lisp_Object pos, prop;
2078
2079 /* First of all, is there invisible text at this position? */
2080 XSETFASTINT (pos, IT_CHARPOS (*it));
2081 prop = Fget_char_property (pos, Qinvisible, it->window);
2082
2083 /* If we are on invisible text, skip over it. */
2084 if (TEXT_PROP_MEANS_INVISIBLE (prop))
2085 {
2086 /* Record whether we have to display an ellipsis for the
2087 invisible text. */
2088 int display_ellipsis_p
2089 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2090
2091 handled = HANDLED_RECOMPUTE_PROPS;
2092
2093 /* Loop skipping over invisible text. The loop is left at
2094 ZV or with IT on the first char being visible again. */
2095 do
2096 {
2097 /* Try to skip some invisible text. Return value is the
2098 position reached which can be equal to IT's position
2099 if there is nothing invisible here. This skips both
2100 over invisible text properties and overlays with
2101 invisible property. */
2102 newpos = skip_invisible (IT_CHARPOS (*it),
2103 &next_stop, ZV, it->window);
2104
2105 /* If we skipped nothing at all we weren't at invisible
2106 text in the first place. If everything to the end of
2107 the buffer was skipped, end the loop. */
2108 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2109 visible_p = 1;
2110 else
2111 {
2112 /* We skipped some characters but not necessarily
2113 all there are. Check if we ended up on visible
2114 text. Fget_char_property returns the property of
2115 the char before the given position, i.e. if we
2116 get visible_p = 1, this means that the char at
2117 newpos is visible. */
2118 XSETFASTINT (pos, newpos);
2119 prop = Fget_char_property (pos, Qinvisible, it->window);
2120 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2121 }
2122
2123 /* If we ended up on invisible text, proceed to
2124 skip starting with next_stop. */
2125 if (!visible_p)
2126 IT_CHARPOS (*it) = next_stop;
2127 }
2128 while (!visible_p);
2129
2130 /* The position newpos is now either ZV or on visible text. */
2131 IT_CHARPOS (*it) = newpos;
2132 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2133
2134 /* Maybe return `...' next for the end of the invisible text. */
2135 if (display_ellipsis_p)
2136 {
2137 if (it->dp
2138 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2139 {
2140 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2141 it->dpvec = v->contents;
2142 it->dpend = v->contents + v->size;
2143 }
2144 else
2145 {
2146 /* Default `...'. */
2147 it->dpvec = default_invis_vector;
2148 it->dpend = default_invis_vector + 3;
2149 }
2150
2151 /* The ellipsis display does not replace the display of
2152 the character at the new position. Indicate this by
2153 setting IT->dpvec_char_len to zero. */
2154 it->dpvec_char_len = 0;
2155
2156 it->current.dpvec_index = 0;
2157 it->method = next_element_from_display_vector;
2158 }
2159 }
2160 }
2161
2162 return handled;
2163 }
2164
2165
2166 \f
2167 /***********************************************************************
2168 'display' property
2169 ***********************************************************************/
2170
2171 /* Set up iterator IT from `display' property at its current position.
2172 Called from handle_stop. */
2173
2174 static enum prop_handled
2175 handle_display_prop (it)
2176 struct it *it;
2177 {
2178 Lisp_Object prop, object;
2179 struct text_pos *position;
2180 int space_or_image_found_p;
2181
2182 if (STRINGP (it->string))
2183 {
2184 object = it->string;
2185 position = &it->current.string_pos;
2186 }
2187 else
2188 {
2189 object = Qnil;
2190 position = &it->current.pos;
2191 }
2192
2193 /* Reset those iterator values set from display property values. */
2194 it->font_height = Qnil;
2195 it->space_width = Qnil;
2196 it->voffset = 0;
2197
2198 /* We don't support recursive `display' properties, i.e. string
2199 values that have a string `display' property, that have a string
2200 `display' property etc. */
2201 if (!it->string_from_display_prop_p)
2202 it->area = TEXT_AREA;
2203
2204 prop = Fget_char_property (make_number (position->charpos),
2205 Qdisplay, object);
2206 if (NILP (prop))
2207 return HANDLED_NORMALLY;
2208
2209 space_or_image_found_p = 0;
2210 if (CONSP (prop)
2211 && CONSP (XCAR (prop))
2212 && !EQ (Qmargin, XCAR (XCAR (prop))))
2213 {
2214 /* A list of sub-properties. */
2215 while (CONSP (prop))
2216 {
2217 if (handle_single_display_prop (it, XCAR (prop), object, position))
2218 space_or_image_found_p = 1;
2219 prop = XCDR (prop);
2220 }
2221 }
2222 else if (VECTORP (prop))
2223 {
2224 int i;
2225 for (i = 0; i < XVECTOR (prop)->size; ++i)
2226 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2227 object, position))
2228 space_or_image_found_p = 1;
2229 }
2230 else
2231 {
2232 if (handle_single_display_prop (it, prop, object, position))
2233 space_or_image_found_p = 1;
2234 }
2235
2236 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2237 }
2238
2239
2240 /* Value is the position of the end of the `display' property starting
2241 at START_POS in OBJECT. */
2242
2243 static struct text_pos
2244 display_prop_end (it, object, start_pos)
2245 struct it *it;
2246 Lisp_Object object;
2247 struct text_pos start_pos;
2248 {
2249 Lisp_Object end;
2250 struct text_pos end_pos;
2251
2252 end = next_single_char_property_change (make_number (CHARPOS (start_pos)),
2253 Qdisplay, object, Qnil);
2254 CHARPOS (end_pos) = XFASTINT (end);
2255 if (STRINGP (object))
2256 compute_string_pos (&end_pos, start_pos, it->string);
2257 else
2258 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2259
2260 return end_pos;
2261 }
2262
2263
2264 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2265 is the object in which the `display' property was found. *POSITION
2266 is the position at which it was found.
2267
2268 If PROP is a `space' or `image' sub-property, set *POSITION to the
2269 end position of the `display' property.
2270
2271 Value is non-zero if a `space' or `image' property value was found. */
2272
2273 static int
2274 handle_single_display_prop (it, prop, object, position)
2275 struct it *it;
2276 Lisp_Object prop;
2277 Lisp_Object object;
2278 struct text_pos *position;
2279 {
2280 Lisp_Object value;
2281 int space_or_image_found_p = 0;
2282
2283 Lisp_Object form;
2284
2285 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2286 evaluated. If the result is nil, VALUE is ignored. */
2287 form = Qt;
2288 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2289 {
2290 prop = XCDR (prop);
2291 if (!CONSP (prop))
2292 return 0;
2293 form = XCAR (prop);
2294 prop = XCDR (prop);
2295 }
2296
2297 if (!NILP (form) && !EQ (form, Qt))
2298 {
2299 struct gcpro gcpro1;
2300 struct text_pos end_pos, pt;
2301
2302 end_pos = display_prop_end (it, object, *position);
2303 GCPRO1 (form);
2304
2305 /* Temporarily set point to the end position, and then evaluate
2306 the form. This makes `(eolp)' work as FORM. */
2307 CHARPOS (pt) = PT;
2308 BYTEPOS (pt) = PT_BYTE;
2309 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2310 form = eval_form (form);
2311 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2312 UNGCPRO;
2313 }
2314
2315 if (NILP (form))
2316 return 0;
2317
2318 if (CONSP (prop)
2319 && EQ (XCAR (prop), Qheight)
2320 && CONSP (XCDR (prop)))
2321 {
2322 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2323 return 0;
2324
2325 /* `(height HEIGHT)'. */
2326 it->font_height = XCAR (XCDR (prop));
2327 if (!NILP (it->font_height))
2328 {
2329 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2330 int new_height = -1;
2331
2332 if (CONSP (it->font_height)
2333 && (EQ (XCAR (it->font_height), Qplus)
2334 || EQ (XCAR (it->font_height), Qminus))
2335 && CONSP (XCDR (it->font_height))
2336 && INTEGERP (XCAR (XCDR (it->font_height))))
2337 {
2338 /* `(+ N)' or `(- N)' where N is an integer. */
2339 int steps = XINT (XCAR (XCDR (it->font_height)));
2340 if (EQ (XCAR (it->font_height), Qplus))
2341 steps = - steps;
2342 it->face_id = smaller_face (it->f, it->face_id, steps);
2343 }
2344 else if (SYMBOLP (it->font_height))
2345 {
2346 /* Call function with current height as argument.
2347 Value is the new height. */
2348 Lisp_Object form, height;
2349 struct gcpro gcpro1;
2350
2351 height = face->lface[LFACE_HEIGHT_INDEX];
2352 form = Fcons (it->font_height, Fcons (height, Qnil));
2353 GCPRO1 (form);
2354 height = eval_form (form);
2355 if (NUMBERP (height))
2356 new_height = XFLOATINT (height);
2357 UNGCPRO;
2358 }
2359 else if (NUMBERP (it->font_height))
2360 {
2361 /* Value is a multiple of the canonical char height. */
2362 struct face *face;
2363
2364 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2365 new_height = (XFLOATINT (it->font_height)
2366 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2367 }
2368 else
2369 {
2370 /* Evaluate IT->font_height with `height' bound to the
2371 current specified height to get the new height. */
2372 Lisp_Object value;
2373 int count = specpdl_ptr - specpdl;
2374
2375 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2376 value = eval_form (it->font_height);
2377 unbind_to (count, Qnil);
2378
2379 if (NUMBERP (value))
2380 new_height = XFLOATINT (value);
2381 }
2382
2383 if (new_height > 0)
2384 it->face_id = face_with_height (it->f, it->face_id, new_height);
2385 }
2386 }
2387 else if (CONSP (prop)
2388 && EQ (XCAR (prop), Qspace_width)
2389 && CONSP (XCDR (prop)))
2390 {
2391 /* `(space_width WIDTH)'. */
2392 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2393 return 0;
2394
2395 value = XCAR (XCDR (prop));
2396 if (NUMBERP (value) && XFLOATINT (value) > 0)
2397 it->space_width = value;
2398 }
2399 else if (CONSP (prop)
2400 && EQ (XCAR (prop), Qraise)
2401 && CONSP (XCDR (prop)))
2402 {
2403 /* `(raise FACTOR)'. */
2404 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2405 return 0;
2406
2407 #ifdef HAVE_WINDOW_SYSTEM
2408 value = XCAR (XCDR (prop));
2409 if (NUMBERP (value))
2410 {
2411 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2412 it->voffset = - (XFLOATINT (value)
2413 * (FONT_HEIGHT (face->font)));
2414 }
2415 #endif /* HAVE_WINDOW_SYSTEM */
2416 }
2417 else if (!it->string_from_display_prop_p)
2418 {
2419 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2420 VALUE) or `((margin nil) VALUE)' or VALUE. */
2421 Lisp_Object location, value;
2422 struct text_pos start_pos;
2423 int valid_p;
2424
2425 /* Characters having this form of property are not displayed, so
2426 we have to find the end of the property. */
2427 space_or_image_found_p = 1;
2428 start_pos = *position;
2429 *position = display_prop_end (it, object, start_pos);
2430 value = Qnil;
2431
2432 /* Let's stop at the new position and assume that all
2433 text properties change there. */
2434 it->stop_charpos = position->charpos;
2435
2436 location = Qunbound;
2437 if (CONSP (prop) && CONSP (XCAR (prop)))
2438 {
2439 Lisp_Object tem;
2440
2441 value = XCDR (prop);
2442 if (CONSP (value))
2443 value = XCAR (value);
2444
2445 tem = XCAR (prop);
2446 if (EQ (XCAR (tem), Qmargin)
2447 && (tem = XCDR (tem),
2448 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2449 (NILP (tem)
2450 || EQ (tem, Qleft_margin)
2451 || EQ (tem, Qright_margin))))
2452 location = tem;
2453 }
2454
2455 if (EQ (location, Qunbound))
2456 {
2457 location = Qnil;
2458 value = prop;
2459 }
2460
2461 #ifdef HAVE_WINDOW_SYSTEM
2462 if (FRAME_TERMCAP_P (it->f))
2463 valid_p = STRINGP (value);
2464 else
2465 valid_p = (STRINGP (value)
2466 || (CONSP (value) && EQ (XCAR (value), Qspace))
2467 || valid_image_p (value));
2468 #else /* not HAVE_WINDOW_SYSTEM */
2469 valid_p = STRINGP (value);
2470 #endif /* not HAVE_WINDOW_SYSTEM */
2471
2472 if ((EQ (location, Qleft_margin)
2473 || EQ (location, Qright_margin)
2474 || NILP (location))
2475 && valid_p)
2476 {
2477 /* Save current settings of IT so that we can restore them
2478 when we are finished with the glyph property value. */
2479 push_it (it);
2480
2481 if (NILP (location))
2482 it->area = TEXT_AREA;
2483 else if (EQ (location, Qleft_margin))
2484 it->area = LEFT_MARGIN_AREA;
2485 else
2486 it->area = RIGHT_MARGIN_AREA;
2487
2488 if (STRINGP (value))
2489 {
2490 it->string = value;
2491 it->multibyte_p = STRING_MULTIBYTE (it->string);
2492 it->current.overlay_string_index = -1;
2493 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2494 it->end_charpos = it->string_nchars
2495 = XSTRING (it->string)->size;
2496 it->method = next_element_from_string;
2497 it->stop_charpos = 0;
2498 it->string_from_display_prop_p = 1;
2499 }
2500 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2501 {
2502 it->method = next_element_from_stretch;
2503 it->object = value;
2504 it->current.pos = it->position = start_pos;
2505 }
2506 #ifdef HAVE_WINDOW_SYSTEM
2507 else
2508 {
2509 it->what = IT_IMAGE;
2510 it->image_id = lookup_image (it->f, value);
2511 it->position = start_pos;
2512 it->object = NILP (object) ? it->w->buffer : object;
2513 it->method = next_element_from_image;
2514
2515 /* Say that we don't have consumed the characters with
2516 `display' property yet. The call to pop_it in
2517 set_iterator_to_next will clean this up. */
2518 *position = start_pos;
2519 }
2520 #endif /* HAVE_WINDOW_SYSTEM */
2521 }
2522 }
2523
2524 return space_or_image_found_p;
2525 }
2526
2527
2528 \f
2529 /***********************************************************************
2530 `composition' property
2531 ***********************************************************************/
2532
2533 /* Set up iterator IT from `composition' property at its current
2534 position. Called from handle_stop. */
2535
2536 static enum prop_handled
2537 handle_composition_prop (it)
2538 struct it *it;
2539 {
2540 Lisp_Object prop, string;
2541 int pos, pos_byte, end;
2542 enum prop_handled handled = HANDLED_NORMALLY;
2543
2544 if (STRINGP (it->string))
2545 {
2546 pos = IT_STRING_CHARPOS (*it);
2547 pos_byte = IT_STRING_BYTEPOS (*it);
2548 string = it->string;
2549 }
2550 else
2551 {
2552 pos = IT_CHARPOS (*it);
2553 pos_byte = IT_BYTEPOS (*it);
2554 string = Qnil;
2555 }
2556
2557 /* If there's a valid composition and point is not inside of the
2558 composition (in the case that the composition is from the current
2559 buffer), draw a glyph composed from the composition components. */
2560 if (find_composition (pos, -1, &pos, &end, &prop, string)
2561 && COMPOSITION_VALID_P (pos, end, prop)
2562 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
2563 {
2564 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
2565
2566 if (id >= 0)
2567 {
2568 it->method = next_element_from_composition;
2569 it->cmp_id = id;
2570 it->cmp_len = COMPOSITION_LENGTH (prop);
2571 /* For a terminal, draw only the first character of the
2572 components. */
2573 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
2574 it->len = (STRINGP (it->string)
2575 ? string_char_to_byte (it->string, end)
2576 : CHAR_TO_BYTE (end)) - pos_byte;
2577 it->stop_charpos = end;
2578 handled = HANDLED_RETURN;
2579 }
2580 }
2581
2582 return handled;
2583 }
2584
2585
2586 \f
2587 /***********************************************************************
2588 Overlay strings
2589 ***********************************************************************/
2590
2591 /* The following structure is used to record overlay strings for
2592 later sorting in load_overlay_strings. */
2593
2594 struct overlay_entry
2595 {
2596 Lisp_Object string;
2597 int priority;
2598 int after_string_p;
2599 };
2600
2601
2602 /* Set up iterator IT from overlay strings at its current position.
2603 Called from handle_stop. */
2604
2605 static enum prop_handled
2606 handle_overlay_change (it)
2607 struct it *it;
2608 {
2609 /* Overlays are handled in current_buffer only. */
2610 if (STRINGP (it->string))
2611 return HANDLED_NORMALLY;
2612 else
2613 return (get_overlay_strings (it)
2614 ? HANDLED_RECOMPUTE_PROPS
2615 : HANDLED_NORMALLY);
2616 }
2617
2618
2619 /* Set up the next overlay string for delivery by IT, if there is an
2620 overlay string to deliver. Called by set_iterator_to_next when the
2621 end of the current overlay string is reached. If there are more
2622 overlay strings to display, IT->string and
2623 IT->current.overlay_string_index are set appropriately here.
2624 Otherwise IT->string is set to nil. */
2625
2626 static void
2627 next_overlay_string (it)
2628 struct it *it;
2629 {
2630 ++it->current.overlay_string_index;
2631 if (it->current.overlay_string_index == it->n_overlay_strings)
2632 {
2633 /* No more overlay strings. Restore IT's settings to what
2634 they were before overlay strings were processed, and
2635 continue to deliver from current_buffer. */
2636 pop_it (it);
2637 xassert (it->stop_charpos >= BEGV
2638 && it->stop_charpos <= it->end_charpos);
2639 it->string = Qnil;
2640 it->current.overlay_string_index = -1;
2641 SET_TEXT_POS (it->current.string_pos, -1, -1);
2642 it->n_overlay_strings = 0;
2643 it->method = next_element_from_buffer;
2644 }
2645 else
2646 {
2647 /* There are more overlay strings to process. If
2648 IT->current.overlay_string_index has advanced to a position
2649 where we must load IT->overlay_strings with more strings, do
2650 it. */
2651 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2652
2653 if (it->current.overlay_string_index && i == 0)
2654 load_overlay_strings (it);
2655
2656 /* Initialize IT to deliver display elements from the overlay
2657 string. */
2658 it->string = it->overlay_strings[i];
2659 it->multibyte_p = STRING_MULTIBYTE (it->string);
2660 SET_TEXT_POS (it->current.string_pos, 0, 0);
2661 it->method = next_element_from_string;
2662 it->stop_charpos = 0;
2663 }
2664
2665 CHECK_IT (it);
2666 }
2667
2668
2669 /* Compare two overlay_entry structures E1 and E2. Used as a
2670 comparison function for qsort in load_overlay_strings. Overlay
2671 strings for the same position are sorted so that
2672
2673 1. All after-strings come in front of before-strings.
2674
2675 2. Within after-strings, strings are sorted so that overlay strings
2676 from overlays with higher priorities come first.
2677
2678 2. Within before-strings, strings are sorted so that overlay
2679 strings from overlays with higher priorities come last.
2680
2681 Value is analogous to strcmp. */
2682
2683
2684 static int
2685 compare_overlay_entries (e1, e2)
2686 void *e1, *e2;
2687 {
2688 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
2689 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
2690 int result;
2691
2692 if (entry1->after_string_p != entry2->after_string_p)
2693 /* Let after-strings appear in front of before-strings. */
2694 result = entry1->after_string_p ? -1 : 1;
2695 else if (entry1->after_string_p)
2696 /* After-strings sorted in order of decreasing priority. */
2697 result = entry2->priority - entry1->priority;
2698 else
2699 /* Before-strings sorted in order of increasing priority. */
2700 result = entry1->priority - entry2->priority;
2701
2702 return result;
2703 }
2704
2705
2706 /* Load the vector IT->overlay_strings with overlay strings from IT's
2707 current buffer position. Set IT->n_overlays to the total number of
2708 overlay strings found.
2709
2710 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
2711 a time. On entry into load_overlay_strings,
2712 IT->current.overlay_string_index gives the number of overlay
2713 strings that have already been loaded by previous calls to this
2714 function.
2715
2716 Overlay strings are sorted so that after-string strings come in
2717 front of before-string strings. Within before and after-strings,
2718 strings are sorted by overlay priority. See also function
2719 compare_overlay_entries. */
2720
2721 static void
2722 load_overlay_strings (it)
2723 struct it *it;
2724 {
2725 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
2726 Lisp_Object ov, overlay, window, str;
2727 int start, end;
2728 int size = 20;
2729 int n = 0, i, j;
2730 struct overlay_entry *entries
2731 = (struct overlay_entry *) alloca (size * sizeof *entries);
2732
2733 /* Append the overlay string STRING of overlay OVERLAY to vector
2734 `entries' which has size `size' and currently contains `n'
2735 elements. AFTER_P non-zero means STRING is an after-string of
2736 OVERLAY. */
2737 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
2738 do \
2739 { \
2740 Lisp_Object priority; \
2741 \
2742 if (n == size) \
2743 { \
2744 int new_size = 2 * size; \
2745 struct overlay_entry *old = entries; \
2746 entries = \
2747 (struct overlay_entry *) alloca (new_size \
2748 * sizeof *entries); \
2749 bcopy (old, entries, size * sizeof *entries); \
2750 size = new_size; \
2751 } \
2752 \
2753 entries[n].string = (STRING); \
2754 priority = Foverlay_get ((OVERLAY), Qpriority); \
2755 entries[n].priority \
2756 = INTEGERP (priority) ? XFASTINT (priority) : 0; \
2757 entries[n].after_string_p = (AFTER_P); \
2758 ++n; \
2759 } \
2760 while (0)
2761
2762 /* Process overlay before the overlay center. */
2763 for (ov = current_buffer->overlays_before;
2764 CONSP (ov);
2765 ov = XCDR (ov))
2766 {
2767 overlay = XCAR (ov);
2768 xassert (OVERLAYP (overlay));
2769 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2770 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2771
2772 if (end < IT_CHARPOS (*it))
2773 break;
2774
2775 /* Skip this overlay if it doesn't start or end at IT's current
2776 position. */
2777 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2778 continue;
2779
2780 /* Skip this overlay if it doesn't apply to IT->w. */
2781 window = Foverlay_get (overlay, Qwindow);
2782 if (WINDOWP (window) && XWINDOW (window) != it->w)
2783 continue;
2784
2785 /* If overlay has a non-empty before-string, record it. */
2786 if (start == IT_CHARPOS (*it)
2787 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2788 && XSTRING (str)->size)
2789 RECORD_OVERLAY_STRING (overlay, str, 0);
2790
2791 /* If overlay has a non-empty after-string, record it. */
2792 if (end == IT_CHARPOS (*it)
2793 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2794 && XSTRING (str)->size)
2795 RECORD_OVERLAY_STRING (overlay, str, 1);
2796 }
2797
2798 /* Process overlays after the overlay center. */
2799 for (ov = current_buffer->overlays_after;
2800 CONSP (ov);
2801 ov = XCDR (ov))
2802 {
2803 overlay = XCAR (ov);
2804 xassert (OVERLAYP (overlay));
2805 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2806 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2807
2808 if (start > IT_CHARPOS (*it))
2809 break;
2810
2811 /* Skip this overlay if it doesn't start or end at IT's current
2812 position. */
2813 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2814 continue;
2815
2816 /* Skip this overlay if it doesn't apply to IT->w. */
2817 window = Foverlay_get (overlay, Qwindow);
2818 if (WINDOWP (window) && XWINDOW (window) != it->w)
2819 continue;
2820
2821 /* If overlay has a non-empty before-string, record it. */
2822 if (start == IT_CHARPOS (*it)
2823 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2824 && XSTRING (str)->size)
2825 RECORD_OVERLAY_STRING (overlay, str, 0);
2826
2827 /* If overlay has a non-empty after-string, record it. */
2828 if (end == IT_CHARPOS (*it)
2829 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2830 && XSTRING (str)->size)
2831 RECORD_OVERLAY_STRING (overlay, str, 1);
2832 }
2833
2834 #undef RECORD_OVERLAY_STRING
2835
2836 /* Sort entries. */
2837 qsort (entries, n, sizeof *entries, compare_overlay_entries);
2838
2839 /* Record the total number of strings to process. */
2840 it->n_overlay_strings = n;
2841
2842 /* IT->current.overlay_string_index is the number of overlay strings
2843 that have already been consumed by IT. Copy some of the
2844 remaining overlay strings to IT->overlay_strings. */
2845 i = 0;
2846 j = it->current.overlay_string_index;
2847 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
2848 it->overlay_strings[i++] = entries[j++].string;
2849
2850 CHECK_IT (it);
2851 }
2852
2853
2854 /* Get the first chunk of overlay strings at IT's current buffer
2855 position. Value is non-zero if at least one overlay string was
2856 found. */
2857
2858 static int
2859 get_overlay_strings (it)
2860 struct it *it;
2861 {
2862 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
2863 process. This fills IT->overlay_strings with strings, and sets
2864 IT->n_overlay_strings to the total number of strings to process.
2865 IT->pos.overlay_string_index has to be set temporarily to zero
2866 because load_overlay_strings needs this; it must be set to -1
2867 when no overlay strings are found because a zero value would
2868 indicate a position in the first overlay string. */
2869 it->current.overlay_string_index = 0;
2870 load_overlay_strings (it);
2871
2872 /* If we found overlay strings, set up IT to deliver display
2873 elements from the first one. Otherwise set up IT to deliver
2874 from current_buffer. */
2875 if (it->n_overlay_strings)
2876 {
2877 /* Make sure we know settings in current_buffer, so that we can
2878 restore meaningful values when we're done with the overlay
2879 strings. */
2880 compute_stop_pos (it);
2881 xassert (it->face_id >= 0);
2882
2883 /* Save IT's settings. They are restored after all overlay
2884 strings have been processed. */
2885 xassert (it->sp == 0);
2886 push_it (it);
2887
2888 /* Set up IT to deliver display elements from the first overlay
2889 string. */
2890 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2891 it->stop_charpos = 0;
2892 it->string = it->overlay_strings[0];
2893 it->multibyte_p = STRING_MULTIBYTE (it->string);
2894 xassert (STRINGP (it->string));
2895 it->method = next_element_from_string;
2896 }
2897 else
2898 {
2899 it->string = Qnil;
2900 it->current.overlay_string_index = -1;
2901 it->method = next_element_from_buffer;
2902 }
2903
2904 CHECK_IT (it);
2905
2906 /* Value is non-zero if we found at least one overlay string. */
2907 return STRINGP (it->string);
2908 }
2909
2910
2911 \f
2912 /***********************************************************************
2913 Saving and restoring state
2914 ***********************************************************************/
2915
2916 /* Save current settings of IT on IT->stack. Called, for example,
2917 before setting up IT for an overlay string, to be able to restore
2918 IT's settings to what they were after the overlay string has been
2919 processed. */
2920
2921 static void
2922 push_it (it)
2923 struct it *it;
2924 {
2925 struct iterator_stack_entry *p;
2926
2927 xassert (it->sp < 2);
2928 p = it->stack + it->sp;
2929
2930 p->stop_charpos = it->stop_charpos;
2931 xassert (it->face_id >= 0);
2932 p->face_id = it->face_id;
2933 p->string = it->string;
2934 p->pos = it->current;
2935 p->end_charpos = it->end_charpos;
2936 p->string_nchars = it->string_nchars;
2937 p->area = it->area;
2938 p->multibyte_p = it->multibyte_p;
2939 p->space_width = it->space_width;
2940 p->font_height = it->font_height;
2941 p->voffset = it->voffset;
2942 p->string_from_display_prop_p = it->string_from_display_prop_p;
2943 ++it->sp;
2944 }
2945
2946
2947 /* Restore IT's settings from IT->stack. Called, for example, when no
2948 more overlay strings must be processed, and we return to delivering
2949 display elements from a buffer, or when the end of a string from a
2950 `display' property is reached and we return to delivering display
2951 elements from an overlay string, or from a buffer. */
2952
2953 static void
2954 pop_it (it)
2955 struct it *it;
2956 {
2957 struct iterator_stack_entry *p;
2958
2959 xassert (it->sp > 0);
2960 --it->sp;
2961 p = it->stack + it->sp;
2962 it->stop_charpos = p->stop_charpos;
2963 it->face_id = p->face_id;
2964 it->string = p->string;
2965 it->current = p->pos;
2966 it->end_charpos = p->end_charpos;
2967 it->string_nchars = p->string_nchars;
2968 it->area = p->area;
2969 it->multibyte_p = p->multibyte_p;
2970 it->space_width = p->space_width;
2971 it->font_height = p->font_height;
2972 it->voffset = p->voffset;
2973 it->string_from_display_prop_p = p->string_from_display_prop_p;
2974 }
2975
2976
2977 \f
2978 /***********************************************************************
2979 Moving over lines
2980 ***********************************************************************/
2981
2982 /* Set IT's current position to the previous line start. */
2983
2984 static void
2985 back_to_previous_line_start (it)
2986 struct it *it;
2987 {
2988 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
2989 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
2990 }
2991
2992
2993 /* Set IT's current position to the next line start. */
2994
2995 static void
2996 forward_to_next_line_start (it)
2997 struct it *it;
2998 {
2999 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), 1);
3000 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3001 }
3002
3003
3004 /* Set IT's current position to the previous visible line start. Skip
3005 invisible text that is so either due to text properties or due to
3006 selective display. Caution: this does not change IT->current_x and
3007 IT->hpos. */
3008
3009 static void
3010 back_to_previous_visible_line_start (it)
3011 struct it *it;
3012 {
3013 int visible_p = 0;
3014
3015 /* Go back one newline if not on BEGV already. */
3016 if (IT_CHARPOS (*it) > BEGV)
3017 back_to_previous_line_start (it);
3018
3019 /* Move over lines that are invisible because of selective display
3020 or text properties. */
3021 while (IT_CHARPOS (*it) > BEGV
3022 && !visible_p)
3023 {
3024 visible_p = 1;
3025
3026 /* If selective > 0, then lines indented more than that values
3027 are invisible. */
3028 if (it->selective > 0
3029 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3030 it->selective))
3031 visible_p = 0;
3032 else
3033 {
3034 Lisp_Object prop;
3035
3036 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3037 Qinvisible, it->window);
3038 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3039 visible_p = 0;
3040 }
3041
3042 /* Back one more newline if the current one is invisible. */
3043 if (!visible_p)
3044 back_to_previous_line_start (it);
3045 }
3046
3047 xassert (IT_CHARPOS (*it) >= BEGV);
3048 xassert (IT_CHARPOS (*it) == BEGV
3049 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3050 CHECK_IT (it);
3051 }
3052
3053
3054 /* Reseat iterator IT at the previous visible line start. Skip
3055 invisible text that is so either due to text properties or due to
3056 selective display. At the end, update IT's overlay information,
3057 face information etc. */
3058
3059 static void
3060 reseat_at_previous_visible_line_start (it)
3061 struct it *it;
3062 {
3063 back_to_previous_visible_line_start (it);
3064 reseat (it, it->current.pos, 1);
3065 CHECK_IT (it);
3066 }
3067
3068
3069 /* Reseat iterator IT on the next visible line start in the current
3070 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3071 preceding the line start. Skip over invisible text that is so
3072 because of selective display. Compute faces, overlays etc at the
3073 new position. Note that this function does not skip over text that
3074 is invisible because of text properties. */
3075
3076 static void
3077 reseat_at_next_visible_line_start (it, on_newline_p)
3078 struct it *it;
3079 int on_newline_p;
3080 {
3081 /* Restore the buffer position when currently not delivering display
3082 elements from the current buffer. This is the case, for example,
3083 when called at the end of a truncated overlay string. */
3084 while (it->sp)
3085 pop_it (it);
3086 it->method = next_element_from_buffer;
3087
3088 /* Otherwise, scan_buffer would not work. */
3089 if (IT_CHARPOS (*it) < ZV)
3090 {
3091 /* If on a newline, advance past it. Otherwise, find the next
3092 newline which automatically gives us the position following
3093 the newline. */
3094 if (FETCH_BYTE (IT_BYTEPOS (*it)) == '\n')
3095 {
3096 ++IT_CHARPOS (*it);
3097 ++IT_BYTEPOS (*it);
3098 }
3099 else
3100 forward_to_next_line_start (it);
3101
3102 /* We must either have reached the end of the buffer or end up
3103 after a newline. */
3104 xassert (IT_CHARPOS (*it) == ZV
3105 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3106
3107 /* Skip over lines that are invisible because they are indented
3108 more than the value of IT->selective. */
3109 if (it->selective > 0)
3110 while (IT_CHARPOS (*it) < ZV
3111 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3112 it->selective))
3113 forward_to_next_line_start (it);
3114
3115 /* Position on the newline if we should. */
3116 if (on_newline_p
3117 && IT_CHARPOS (*it) > BEGV
3118 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n')
3119 {
3120 --IT_CHARPOS (*it);
3121 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3122 }
3123
3124 /* Set the iterator there. The 0 as the last parameter of
3125 reseat means don't force a text property lookup. The lookup
3126 is then only done if we've skipped past the iterator's
3127 check_charpos'es. This optimization is important because
3128 text property lookups tend to be expensive. */
3129 reseat (it, it->current.pos, 0);
3130 }
3131
3132 CHECK_IT (it);
3133 }
3134
3135
3136 \f
3137 /***********************************************************************
3138 Changing an iterator's position
3139 ***********************************************************************/
3140
3141 /* Change IT's current position to POS in current_buffer. If FORCE_P
3142 is non-zero, always check for text properties at the new position.
3143 Otherwise, text properties are only looked up if POS >=
3144 IT->check_charpos of a property. */
3145
3146 static void
3147 reseat (it, pos, force_p)
3148 struct it *it;
3149 struct text_pos pos;
3150 int force_p;
3151 {
3152 int original_pos = IT_CHARPOS (*it);
3153
3154 reseat_1 (it, pos, 0);
3155
3156 /* Determine where to check text properties. Avoid doing it
3157 where possible because text property lookup is very expensive. */
3158 if (force_p
3159 || CHARPOS (pos) > it->stop_charpos
3160 || CHARPOS (pos) < original_pos)
3161 handle_stop (it);
3162
3163 CHECK_IT (it);
3164 }
3165
3166
3167 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3168 IT->stop_pos to POS, also. */
3169
3170 static void
3171 reseat_1 (it, pos, set_stop_p)
3172 struct it *it;
3173 struct text_pos pos;
3174 int set_stop_p;
3175 {
3176 /* Don't call this function when scanning a C string. */
3177 xassert (it->s == NULL);
3178
3179 /* POS must be a reasonable value. */
3180 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3181
3182 it->current.pos = it->position = pos;
3183 XSETBUFFER (it->object, current_buffer);
3184 it->dpvec = NULL;
3185 it->current.dpvec_index = -1;
3186 it->current.overlay_string_index = -1;
3187 IT_STRING_CHARPOS (*it) = -1;
3188 IT_STRING_BYTEPOS (*it) = -1;
3189 it->string = Qnil;
3190 it->method = next_element_from_buffer;
3191 it->sp = 0;
3192
3193 if (set_stop_p)
3194 it->stop_charpos = CHARPOS (pos);
3195 }
3196
3197
3198 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3199 If S is non-null, it is a C string to iterate over. Otherwise,
3200 STRING gives a Lisp string to iterate over.
3201
3202 If PRECISION > 0, don't return more then PRECISION number of
3203 characters from the string.
3204
3205 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3206 characters have been returned. FIELD_WIDTH < 0 means an infinite
3207 field width.
3208
3209 MULTIBYTE = 0 means disable processing of multibyte characters,
3210 MULTIBYTE > 0 means enable it,
3211 MULTIBYTE < 0 means use IT->multibyte_p.
3212
3213 IT must be initialized via a prior call to init_iterator before
3214 calling this function. */
3215
3216 static void
3217 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3218 struct it *it;
3219 unsigned char *s;
3220 Lisp_Object string;
3221 int charpos;
3222 int precision, field_width, multibyte;
3223 {
3224 /* No region in strings. */
3225 it->region_beg_charpos = it->region_end_charpos = -1;
3226
3227 /* No text property checks performed by default, but see below. */
3228 it->stop_charpos = -1;
3229
3230 /* Set iterator position and end position. */
3231 bzero (&it->current, sizeof it->current);
3232 it->current.overlay_string_index = -1;
3233 it->current.dpvec_index = -1;
3234 xassert (charpos >= 0);
3235
3236 /* Use the setting of MULTIBYTE if specified. */
3237 if (multibyte >= 0)
3238 it->multibyte_p = multibyte > 0;
3239
3240 if (s == NULL)
3241 {
3242 xassert (STRINGP (string));
3243 it->string = string;
3244 it->s = NULL;
3245 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3246 it->method = next_element_from_string;
3247 it->current.string_pos = string_pos (charpos, string);
3248 }
3249 else
3250 {
3251 it->s = s;
3252 it->string = Qnil;
3253
3254 /* Note that we use IT->current.pos, not it->current.string_pos,
3255 for displaying C strings. */
3256 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3257 if (it->multibyte_p)
3258 {
3259 it->current.pos = c_string_pos (charpos, s, 1);
3260 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3261 }
3262 else
3263 {
3264 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3265 it->end_charpos = it->string_nchars = strlen (s);
3266 }
3267
3268 it->method = next_element_from_c_string;
3269 }
3270
3271 /* PRECISION > 0 means don't return more than PRECISION characters
3272 from the string. */
3273 if (precision > 0 && it->end_charpos - charpos > precision)
3274 it->end_charpos = it->string_nchars = charpos + precision;
3275
3276 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3277 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3278 FIELD_WIDTH < 0 means infinite field width. This is useful for
3279 padding with `-' at the end of a mode line. */
3280 if (field_width < 0)
3281 field_width = INFINITY;
3282 if (field_width > it->end_charpos - charpos)
3283 it->end_charpos = charpos + field_width;
3284
3285 /* Use the standard display table for displaying strings. */
3286 if (DISP_TABLE_P (Vstandard_display_table))
3287 it->dp = XCHAR_TABLE (Vstandard_display_table);
3288
3289 it->stop_charpos = charpos;
3290 CHECK_IT (it);
3291 }
3292
3293
3294 \f
3295 /***********************************************************************
3296 Iteration
3297 ***********************************************************************/
3298
3299 /* Load IT's display element fields with information about the next
3300 display element from the current position of IT. Value is zero if
3301 end of buffer (or C string) is reached. */
3302
3303 int
3304 get_next_display_element (it)
3305 struct it *it;
3306 {
3307 /* Non-zero means that we found an display element. Zero means that
3308 we hit the end of what we iterate over. Performance note: the
3309 function pointer `method' used here turns out to be faster than
3310 using a sequence of if-statements. */
3311 int success_p = (*it->method) (it);
3312
3313 if (it->what == IT_CHARACTER)
3314 {
3315 /* Map via display table or translate control characters.
3316 IT->c, IT->len etc. have been set to the next character by
3317 the function call above. If we have a display table, and it
3318 contains an entry for IT->c, translate it. Don't do this if
3319 IT->c itself comes from a display table, otherwise we could
3320 end up in an infinite recursion. (An alternative could be to
3321 count the recursion depth of this function and signal an
3322 error when a certain maximum depth is reached.) Is it worth
3323 it? */
3324 if (success_p && it->dpvec == NULL)
3325 {
3326 Lisp_Object dv;
3327
3328 if (it->dp
3329 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3330 VECTORP (dv)))
3331 {
3332 struct Lisp_Vector *v = XVECTOR (dv);
3333
3334 /* Return the first character from the display table
3335 entry, if not empty. If empty, don't display the
3336 current character. */
3337 if (v->size)
3338 {
3339 it->dpvec_char_len = it->len;
3340 it->dpvec = v->contents;
3341 it->dpend = v->contents + v->size;
3342 it->current.dpvec_index = 0;
3343 it->method = next_element_from_display_vector;
3344 }
3345
3346 success_p = get_next_display_element (it);
3347 }
3348
3349 /* Translate control characters into `\003' or `^C' form.
3350 Control characters coming from a display table entry are
3351 currently not translated because we use IT->dpvec to hold
3352 the translation. This could easily be changed but I
3353 don't believe that it is worth doing.
3354
3355 Non-printable multibyte characters are also translated
3356 octal form. */
3357 else if ((it->c < ' '
3358 && (it->area != TEXT_AREA
3359 || (it->c != '\n' && it->c != '\t')))
3360 || (it->c >= 127
3361 && it->len == 1)
3362 || !CHAR_PRINTABLE_P (it->c))
3363 {
3364 /* IT->c is a control character which must be displayed
3365 either as '\003' or as `^C' where the '\\' and '^'
3366 can be defined in the display table. Fill
3367 IT->ctl_chars with glyphs for what we have to
3368 display. Then, set IT->dpvec to these glyphs. */
3369 GLYPH g;
3370
3371 if (it->c < 128 && it->ctl_arrow_p)
3372 {
3373 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3374 if (it->dp
3375 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3376 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3377 g = XINT (DISP_CTRL_GLYPH (it->dp));
3378 else
3379 g = FAST_MAKE_GLYPH ('^', 0);
3380 XSETINT (it->ctl_chars[0], g);
3381
3382 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3383 XSETINT (it->ctl_chars[1], g);
3384
3385 /* Set up IT->dpvec and return first character from it. */
3386 it->dpvec_char_len = it->len;
3387 it->dpvec = it->ctl_chars;
3388 it->dpend = it->dpvec + 2;
3389 it->current.dpvec_index = 0;
3390 it->method = next_element_from_display_vector;
3391 get_next_display_element (it);
3392 }
3393 else
3394 {
3395 unsigned char str[MAX_MULTIBYTE_LENGTH];
3396 int len = CHAR_STRING (it->c, str);
3397 int i;
3398 GLYPH escape_glyph;
3399
3400 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3401 if (it->dp
3402 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3403 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3404 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3405 else
3406 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3407
3408 for (i = 0; i < len; i++)
3409 {
3410 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3411 /* Insert three more glyphs into IT->ctl_chars for
3412 the octal display of the character. */
3413 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3414 XSETINT (it->ctl_chars[i * 4 + 1], g);
3415 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3416 XSETINT (it->ctl_chars[i * 4 + 2], g);
3417 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3418 XSETINT (it->ctl_chars[i * 4 + 3], g);
3419 }
3420
3421 /* Set up IT->dpvec and return the first character
3422 from it. */
3423 it->dpvec_char_len = it->len;
3424 it->dpvec = it->ctl_chars;
3425 it->dpend = it->dpvec + len * 4;
3426 it->current.dpvec_index = 0;
3427 it->method = next_element_from_display_vector;
3428 get_next_display_element (it);
3429 }
3430 }
3431 }
3432
3433 /* Adjust face id for a multibyte character. There are no
3434 multibyte character in unibyte text. */
3435 if (it->multibyte_p
3436 && success_p
3437 && FRAME_WINDOW_P (it->f))
3438 {
3439 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3440 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
3441 }
3442 }
3443
3444 /* Is this character the last one of a run of characters with
3445 box? If yes, set IT->end_of_box_run_p to 1. */
3446 if (it->face_box_p
3447 && it->s == NULL)
3448 {
3449 int face_id;
3450 struct face *face;
3451
3452 it->end_of_box_run_p
3453 = ((face_id = face_after_it_pos (it),
3454 face_id != it->face_id)
3455 && (face = FACE_FROM_ID (it->f, face_id),
3456 face->box == FACE_NO_BOX));
3457 }
3458
3459 /* Value is 0 if end of buffer or string reached. */
3460 return success_p;
3461 }
3462
3463
3464 /* Move IT to the next display element.
3465
3466 Functions get_next_display_element and set_iterator_to_next are
3467 separate because I find this arrangement easier to handle than a
3468 get_next_display_element function that also increments IT's
3469 position. The way it is we can first look at an iterator's current
3470 display element, decide whether it fits on a line, and if it does,
3471 increment the iterator position. The other way around we probably
3472 would either need a flag indicating whether the iterator has to be
3473 incremented the next time, or we would have to implement a
3474 decrement position function which would not be easy to write. */
3475
3476 void
3477 set_iterator_to_next (it)
3478 struct it *it;
3479 {
3480 if (it->method == next_element_from_buffer)
3481 {
3482 /* The current display element of IT is a character from
3483 current_buffer. Advance in the buffer, and maybe skip over
3484 invisible lines that are so because of selective display. */
3485 if (ITERATOR_AT_END_OF_LINE_P (it))
3486 reseat_at_next_visible_line_start (it, 0);
3487 else
3488 {
3489 xassert (it->len != 0);
3490 IT_BYTEPOS (*it) += it->len;
3491 IT_CHARPOS (*it) += 1;
3492 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3493 }
3494 }
3495 else if (it->method == next_element_from_composition)
3496 {
3497 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
3498 if (STRINGP (it->string))
3499 {
3500 IT_STRING_BYTEPOS (*it) += it->len;
3501 IT_STRING_CHARPOS (*it) += it->cmp_len;
3502 it->method = next_element_from_string;
3503 goto consider_string_end;
3504 }
3505 else
3506 {
3507 IT_BYTEPOS (*it) += it->len;
3508 IT_CHARPOS (*it) += it->cmp_len;
3509 it->method = next_element_from_buffer;
3510 }
3511 }
3512 else if (it->method == next_element_from_c_string)
3513 {
3514 /* Current display element of IT is from a C string. */
3515 IT_BYTEPOS (*it) += it->len;
3516 IT_CHARPOS (*it) += 1;
3517 }
3518 else if (it->method == next_element_from_display_vector)
3519 {
3520 /* Current display element of IT is from a display table entry.
3521 Advance in the display table definition. Reset it to null if
3522 end reached, and continue with characters from buffers/
3523 strings. */
3524 ++it->current.dpvec_index;
3525
3526 /* Restore face of the iterator to what they were before the
3527 display vector entry (these entries may contain faces). */
3528 it->face_id = it->saved_face_id;
3529
3530 if (it->dpvec + it->current.dpvec_index == it->dpend)
3531 {
3532 if (it->s)
3533 it->method = next_element_from_c_string;
3534 else if (STRINGP (it->string))
3535 it->method = next_element_from_string;
3536 else
3537 it->method = next_element_from_buffer;
3538
3539 it->dpvec = NULL;
3540 it->current.dpvec_index = -1;
3541
3542 /* Skip over characters which were displayed via IT->dpvec. */
3543 if (it->dpvec_char_len < 0)
3544 reseat_at_next_visible_line_start (it, 1);
3545 else if (it->dpvec_char_len > 0)
3546 {
3547 it->len = it->dpvec_char_len;
3548 set_iterator_to_next (it);
3549 }
3550 }
3551 }
3552 else if (it->method == next_element_from_string)
3553 {
3554 /* Current display element is a character from a Lisp string. */
3555 xassert (it->s == NULL && STRINGP (it->string));
3556 IT_STRING_BYTEPOS (*it) += it->len;
3557 IT_STRING_CHARPOS (*it) += 1;
3558
3559 consider_string_end:
3560
3561 if (it->current.overlay_string_index >= 0)
3562 {
3563 /* IT->string is an overlay string. Advance to the
3564 next, if there is one. */
3565 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3566 next_overlay_string (it);
3567 }
3568 else
3569 {
3570 /* IT->string is not an overlay string. If we reached
3571 its end, and there is something on IT->stack, proceed
3572 with what is on the stack. This can be either another
3573 string, this time an overlay string, or a buffer. */
3574 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
3575 && it->sp > 0)
3576 {
3577 pop_it (it);
3578 if (!STRINGP (it->string))
3579 it->method = next_element_from_buffer;
3580 }
3581 }
3582 }
3583 else if (it->method == next_element_from_image
3584 || it->method == next_element_from_stretch)
3585 {
3586 /* The position etc with which we have to proceed are on
3587 the stack. The position may be at the end of a string,
3588 if the `display' property takes up the whole string. */
3589 pop_it (it);
3590 it->image_id = 0;
3591 if (STRINGP (it->string))
3592 {
3593 it->method = next_element_from_string;
3594 goto consider_string_end;
3595 }
3596 else
3597 it->method = next_element_from_buffer;
3598 }
3599 else
3600 /* There are no other methods defined, so this should be a bug. */
3601 abort ();
3602
3603 /* Reset flags indicating start and end of a sequence of
3604 characters with box. */
3605 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3606
3607 xassert (it->method != next_element_from_string
3608 || (STRINGP (it->string)
3609 && IT_STRING_CHARPOS (*it) >= 0));
3610 }
3611
3612
3613 /* Load IT's display element fields with information about the next
3614 display element which comes from a display table entry or from the
3615 result of translating a control character to one of the forms `^C'
3616 or `\003'. IT->dpvec holds the glyphs to return as characters. */
3617
3618 static int
3619 next_element_from_display_vector (it)
3620 struct it *it;
3621 {
3622 /* Precondition. */
3623 xassert (it->dpvec && it->current.dpvec_index >= 0);
3624
3625 /* Remember the current face id in case glyphs specify faces.
3626 IT's face is restored in set_iterator_to_next. */
3627 it->saved_face_id = it->face_id;
3628
3629 if (INTEGERP (*it->dpvec)
3630 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
3631 {
3632 int lface_id;
3633 GLYPH g;
3634
3635 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
3636 it->c = FAST_GLYPH_CHAR (g);
3637 it->len = CHAR_LEN (it->c);
3638
3639 /* The entry may contain a face id to use. Such a face id is
3640 the id of a Lisp face, not a realized face. A face id of
3641 zero means no face. */
3642 lface_id = FAST_GLYPH_FACE (g);
3643 if (lface_id)
3644 {
3645 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
3646 if (face_id >= 0)
3647 {
3648 it->face_id = face_id;
3649 }
3650 }
3651 }
3652 else
3653 /* Display table entry is invalid. Return a space. */
3654 it->c = ' ', it->len = 1;
3655
3656 /* Don't change position and object of the iterator here. They are
3657 still the values of the character that had this display table
3658 entry or was translated, and that's what we want. */
3659 it->what = IT_CHARACTER;
3660 return 1;
3661 }
3662
3663
3664 /* Load IT with the next display element from Lisp string IT->string.
3665 IT->current.string_pos is the current position within the string.
3666 If IT->current.overlay_string_index >= 0, the Lisp string is an
3667 overlay string. */
3668
3669 static int
3670 next_element_from_string (it)
3671 struct it *it;
3672 {
3673 struct text_pos position;
3674
3675 xassert (STRINGP (it->string));
3676 xassert (IT_STRING_CHARPOS (*it) >= 0);
3677 position = it->current.string_pos;
3678
3679 /* Time to check for invisible text? */
3680 if (IT_STRING_CHARPOS (*it) < it->end_charpos
3681 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
3682 {
3683 handle_stop (it);
3684
3685 /* Since a handler may have changed IT->method, we must
3686 recurse here. */
3687 return get_next_display_element (it);
3688 }
3689
3690 if (it->current.overlay_string_index >= 0)
3691 {
3692 /* Get the next character from an overlay string. In overlay
3693 strings, There is no field width or padding with spaces to
3694 do. */
3695 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3696 {
3697 it->what = IT_EOB;
3698 return 0;
3699 }
3700 else if (STRING_MULTIBYTE (it->string))
3701 {
3702 int remaining = (STRING_BYTES (XSTRING (it->string))
3703 - IT_STRING_BYTEPOS (*it));
3704 unsigned char *s = (XSTRING (it->string)->data
3705 + IT_STRING_BYTEPOS (*it));
3706 it->c = string_char_and_length (s, remaining, &it->len);
3707 }
3708 else
3709 {
3710 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3711 it->len = 1;
3712 }
3713 }
3714 else
3715 {
3716 /* Get the next character from a Lisp string that is not an
3717 overlay string. Such strings come from the mode line, for
3718 example. We may have to pad with spaces, or truncate the
3719 string. See also next_element_from_c_string. */
3720 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
3721 {
3722 it->what = IT_EOB;
3723 return 0;
3724 }
3725 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
3726 {
3727 /* Pad with spaces. */
3728 it->c = ' ', it->len = 1;
3729 CHARPOS (position) = BYTEPOS (position) = -1;
3730 }
3731 else if (STRING_MULTIBYTE (it->string))
3732 {
3733 int maxlen = (STRING_BYTES (XSTRING (it->string))
3734 - IT_STRING_BYTEPOS (*it));
3735 unsigned char *s = (XSTRING (it->string)->data
3736 + IT_STRING_BYTEPOS (*it));
3737 it->c = string_char_and_length (s, maxlen, &it->len);
3738 }
3739 else
3740 {
3741 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3742 it->len = 1;
3743 }
3744 }
3745
3746 /* Record what we have and where it came from. Note that we store a
3747 buffer position in IT->position although it could arguably be a
3748 string position. */
3749 it->what = IT_CHARACTER;
3750 it->object = it->string;
3751 it->position = position;
3752 return 1;
3753 }
3754
3755
3756 /* Load IT with next display element from C string IT->s.
3757 IT->string_nchars is the maximum number of characters to return
3758 from the string. IT->end_charpos may be greater than
3759 IT->string_nchars when this function is called, in which case we
3760 may have to return padding spaces. Value is zero if end of string
3761 reached, including padding spaces. */
3762
3763 static int
3764 next_element_from_c_string (it)
3765 struct it *it;
3766 {
3767 int success_p = 1;
3768
3769 xassert (it->s);
3770 it->what = IT_CHARACTER;
3771 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
3772 it->object = Qnil;
3773
3774 /* IT's position can be greater IT->string_nchars in case a field
3775 width or precision has been specified when the iterator was
3776 initialized. */
3777 if (IT_CHARPOS (*it) >= it->end_charpos)
3778 {
3779 /* End of the game. */
3780 it->what = IT_EOB;
3781 success_p = 0;
3782 }
3783 else if (IT_CHARPOS (*it) >= it->string_nchars)
3784 {
3785 /* Pad with spaces. */
3786 it->c = ' ', it->len = 1;
3787 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
3788 }
3789 else if (it->multibyte_p)
3790 {
3791 /* Implementation note: The calls to strlen apparently aren't a
3792 performance problem because there is no noticeable performance
3793 difference between Emacs running in unibyte or multibyte mode. */
3794 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
3795 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
3796 maxlen, &it->len);
3797 }
3798 else
3799 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
3800
3801 return success_p;
3802 }
3803
3804
3805 /* Set up IT to return characters from an ellipsis, if appropriate.
3806 The definition of the ellipsis glyphs may come from a display table
3807 entry. This function Fills IT with the first glyph from the
3808 ellipsis if an ellipsis is to be displayed. */
3809
3810 static int
3811 next_element_from_ellipsis (it)
3812 struct it *it;
3813 {
3814 if (it->selective_display_ellipsis_p)
3815 {
3816 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3817 {
3818 /* Use the display table definition for `...'. Invalid glyphs
3819 will be handled by the method returning elements from dpvec. */
3820 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3821 it->dpvec_char_len = it->len;
3822 it->dpvec = v->contents;
3823 it->dpend = v->contents + v->size;
3824 it->current.dpvec_index = 0;
3825 it->method = next_element_from_display_vector;
3826 }
3827 else
3828 {
3829 /* Use default `...' which is stored in default_invis_vector. */
3830 it->dpvec_char_len = it->len;
3831 it->dpvec = default_invis_vector;
3832 it->dpend = default_invis_vector + 3;
3833 it->current.dpvec_index = 0;
3834 it->method = next_element_from_display_vector;
3835 }
3836 }
3837 else
3838 reseat_at_next_visible_line_start (it, 1);
3839
3840 return get_next_display_element (it);
3841 }
3842
3843
3844 /* Deliver an image display element. The iterator IT is already
3845 filled with image information (done in handle_display_prop). Value
3846 is always 1. */
3847
3848
3849 static int
3850 next_element_from_image (it)
3851 struct it *it;
3852 {
3853 it->what = IT_IMAGE;
3854 return 1;
3855 }
3856
3857
3858 /* Fill iterator IT with next display element from a stretch glyph
3859 property. IT->object is the value of the text property. Value is
3860 always 1. */
3861
3862 static int
3863 next_element_from_stretch (it)
3864 struct it *it;
3865 {
3866 it->what = IT_STRETCH;
3867 return 1;
3868 }
3869
3870
3871 /* Load IT with the next display element from current_buffer. Value
3872 is zero if end of buffer reached. IT->stop_charpos is the next
3873 position at which to stop and check for text properties or buffer
3874 end. */
3875
3876 static int
3877 next_element_from_buffer (it)
3878 struct it *it;
3879 {
3880 int success_p = 1;
3881
3882 /* Check this assumption, otherwise, we would never enter the
3883 if-statement, below. */
3884 xassert (IT_CHARPOS (*it) >= BEGV
3885 && IT_CHARPOS (*it) <= it->stop_charpos);
3886
3887 if (IT_CHARPOS (*it) >= it->stop_charpos)
3888 {
3889 if (IT_CHARPOS (*it) >= it->end_charpos)
3890 {
3891 int overlay_strings_follow_p;
3892
3893 /* End of the game, except when overlay strings follow that
3894 haven't been returned yet. */
3895 if (it->overlay_strings_at_end_processed_p)
3896 overlay_strings_follow_p = 0;
3897 else
3898 {
3899 it->overlay_strings_at_end_processed_p = 1;
3900 overlay_strings_follow_p = get_overlay_strings (it);
3901 }
3902
3903 if (overlay_strings_follow_p)
3904 success_p = get_next_display_element (it);
3905 else
3906 {
3907 it->what = IT_EOB;
3908 it->position = it->current.pos;
3909 success_p = 0;
3910 }
3911 }
3912 else
3913 {
3914 handle_stop (it);
3915 return get_next_display_element (it);
3916 }
3917 }
3918 else
3919 {
3920 /* No face changes, overlays etc. in sight, so just return a
3921 character from current_buffer. */
3922 unsigned char *p;
3923
3924 /* Maybe run the redisplay end trigger hook. Performance note:
3925 This doesn't seem to cost measurable time. */
3926 if (it->redisplay_end_trigger_charpos
3927 && it->glyph_row
3928 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
3929 run_redisplay_end_trigger_hook (it);
3930
3931 /* Get the next character, maybe multibyte. */
3932 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
3933 if (it->multibyte_p && !ASCII_BYTE_P (*p))
3934 {
3935 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
3936 - IT_BYTEPOS (*it));
3937 it->c = string_char_and_length (p, maxlen, &it->len);
3938 }
3939 else
3940 it->c = *p, it->len = 1;
3941
3942 /* Record what we have and where it came from. */
3943 it->what = IT_CHARACTER;;
3944 it->object = it->w->buffer;
3945 it->position = it->current.pos;
3946
3947 /* Normally we return the character found above, except when we
3948 really want to return an ellipsis for selective display. */
3949 if (it->selective)
3950 {
3951 if (it->c == '\n')
3952 {
3953 /* A value of selective > 0 means hide lines indented more
3954 than that number of columns. */
3955 if (it->selective > 0
3956 && IT_CHARPOS (*it) + 1 < ZV
3957 && indented_beyond_p (IT_CHARPOS (*it) + 1,
3958 IT_BYTEPOS (*it) + 1,
3959 it->selective))
3960 {
3961 success_p = next_element_from_ellipsis (it);
3962 it->dpvec_char_len = -1;
3963 }
3964 }
3965 else if (it->c == '\r' && it->selective == -1)
3966 {
3967 /* A value of selective == -1 means that everything from the
3968 CR to the end of the line is invisible, with maybe an
3969 ellipsis displayed for it. */
3970 success_p = next_element_from_ellipsis (it);
3971 it->dpvec_char_len = -1;
3972 }
3973 }
3974 }
3975
3976 /* Value is zero if end of buffer reached. */
3977 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
3978 return success_p;
3979 }
3980
3981
3982 /* Run the redisplay end trigger hook for IT. */
3983
3984 static void
3985 run_redisplay_end_trigger_hook (it)
3986 struct it *it;
3987 {
3988 Lisp_Object args[3];
3989
3990 /* IT->glyph_row should be non-null, i.e. we should be actually
3991 displaying something, or otherwise we should not run the hook. */
3992 xassert (it->glyph_row);
3993
3994 /* Set up hook arguments. */
3995 args[0] = Qredisplay_end_trigger_functions;
3996 args[1] = it->window;
3997 XSETINT (args[2], it->redisplay_end_trigger_charpos);
3998 it->redisplay_end_trigger_charpos = 0;
3999
4000 /* Since we are *trying* to run these functions, don't try to run
4001 them again, even if they get an error. */
4002 it->w->redisplay_end_trigger = Qnil;
4003 Frun_hook_with_args (3, args);
4004
4005 /* Notice if it changed the face of the character we are on. */
4006 handle_face_prop (it);
4007 }
4008
4009
4010 /* Deliver a composition display element. The iterator IT is already
4011 filled with composition information (done in
4012 handle_composition_prop). Value is always 1. */
4013
4014 static int
4015 next_element_from_composition (it)
4016 struct it *it;
4017 {
4018 it->what = IT_COMPOSITION;
4019 it->position = (STRINGP (it->string)
4020 ? it->current.string_pos
4021 : it->current.pos);
4022 return 1;
4023 }
4024
4025
4026 \f
4027 /***********************************************************************
4028 Moving an iterator without producing glyphs
4029 ***********************************************************************/
4030
4031 /* Move iterator IT to a specified buffer or X position within one
4032 line on the display without producing glyphs.
4033
4034 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4035 whichever is reached first.
4036
4037 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4038
4039 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4040 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4041 TO_X includes the amount by which a window is horizontally
4042 scrolled.
4043
4044 Value is
4045
4046 MOVE_POS_MATCH_OR_ZV
4047 - when TO_POS or ZV was reached.
4048
4049 MOVE_X_REACHED
4050 -when TO_X was reached before TO_POS or ZV were reached.
4051
4052 MOVE_LINE_CONTINUED
4053 - when we reached the end of the display area and the line must
4054 be continued.
4055
4056 MOVE_LINE_TRUNCATED
4057 - when we reached the end of the display area and the line is
4058 truncated.
4059
4060 MOVE_NEWLINE_OR_CR
4061 - when we stopped at a line end, i.e. a newline or a CR and selective
4062 display is on. */
4063
4064 static enum move_it_result
4065 move_it_in_display_line_to (it, to_charpos, to_x, op)
4066 struct it *it;
4067 int to_charpos, to_x, op;
4068 {
4069 enum move_it_result result = MOVE_UNDEFINED;
4070 struct glyph_row *saved_glyph_row;
4071
4072 /* Don't produce glyphs in produce_glyphs. */
4073 saved_glyph_row = it->glyph_row;
4074 it->glyph_row = NULL;
4075
4076 while (1)
4077 {
4078 int x, i;
4079
4080 /* Stop when ZV or TO_CHARPOS reached. */
4081 if (!get_next_display_element (it)
4082 || ((op & MOVE_TO_POS) != 0
4083 && BUFFERP (it->object)
4084 && IT_CHARPOS (*it) >= to_charpos))
4085 {
4086 result = MOVE_POS_MATCH_OR_ZV;
4087 break;
4088 }
4089
4090 /* The call to produce_glyphs will get the metrics of the
4091 display element IT is loaded with. We record in x the
4092 x-position before this display element in case it does not
4093 fit on the line. */
4094 x = it->current_x;
4095 PRODUCE_GLYPHS (it);
4096
4097 if (it->area != TEXT_AREA)
4098 {
4099 set_iterator_to_next (it);
4100 continue;
4101 }
4102
4103 /* The number of glyphs we get back in IT->nglyphs will normally
4104 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4105 character on a terminal frame, or (iii) a line end. For the
4106 second case, IT->nglyphs - 1 padding glyphs will be present
4107 (on X frames, there is only one glyph produced for a
4108 composite character.
4109
4110 The behavior implemented below means, for continuation lines,
4111 that as many spaces of a TAB as fit on the current line are
4112 displayed there. For terminal frames, as many glyphs of a
4113 multi-glyph character are displayed in the current line, too.
4114 This is what the old redisplay code did, and we keep it that
4115 way. Under X, the whole shape of a complex character must
4116 fit on the line or it will be completely displayed in the
4117 next line.
4118
4119 Note that both for tabs and padding glyphs, all glyphs have
4120 the same width. */
4121 if (it->nglyphs)
4122 {
4123 /* More than one glyph or glyph doesn't fit on line. All
4124 glyphs have the same width. */
4125 int single_glyph_width = it->pixel_width / it->nglyphs;
4126 int new_x;
4127
4128 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4129 {
4130 new_x = x + single_glyph_width;
4131
4132 /* We want to leave anything reaching TO_X to the caller. */
4133 if ((op & MOVE_TO_X) && new_x > to_x)
4134 {
4135 it->current_x = x;
4136 result = MOVE_X_REACHED;
4137 break;
4138 }
4139 else if (/* Lines are continued. */
4140 !it->truncate_lines_p
4141 && (/* And glyph doesn't fit on the line. */
4142 new_x > it->last_visible_x
4143 /* Or it fits exactly and we're on a window
4144 system frame. */
4145 || (new_x == it->last_visible_x
4146 && FRAME_WINDOW_P (it->f))))
4147 {
4148 if (/* IT->hpos == 0 means the very first glyph
4149 doesn't fit on the line, e.g. a wide image. */
4150 it->hpos == 0
4151 || (new_x == it->last_visible_x
4152 && FRAME_WINDOW_P (it->f)))
4153 {
4154 ++it->hpos;
4155 it->current_x = new_x;
4156 if (i == it->nglyphs - 1)
4157 set_iterator_to_next (it);
4158 }
4159 else
4160 it->current_x = x;
4161
4162 result = MOVE_LINE_CONTINUED;
4163 break;
4164 }
4165 else if (new_x > it->first_visible_x)
4166 {
4167 /* Glyph is visible. Increment number of glyphs that
4168 would be displayed. */
4169 ++it->hpos;
4170 }
4171 else
4172 {
4173 /* Glyph is completely off the left margin of the display
4174 area. Nothing to do. */
4175 }
4176 }
4177
4178 if (result != MOVE_UNDEFINED)
4179 break;
4180 }
4181 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4182 {
4183 /* Stop when TO_X specified and reached. This check is
4184 necessary here because of lines consisting of a line end,
4185 only. The line end will not produce any glyphs and we
4186 would never get MOVE_X_REACHED. */
4187 xassert (it->nglyphs == 0);
4188 result = MOVE_X_REACHED;
4189 break;
4190 }
4191
4192 /* Is this a line end? If yes, we're done. */
4193 if (ITERATOR_AT_END_OF_LINE_P (it))
4194 {
4195 result = MOVE_NEWLINE_OR_CR;
4196 break;
4197 }
4198
4199 /* The current display element has been consumed. Advance
4200 to the next. */
4201 set_iterator_to_next (it);
4202
4203 /* Stop if lines are truncated and IT's current x-position is
4204 past the right edge of the window now. */
4205 if (it->truncate_lines_p
4206 && it->current_x >= it->last_visible_x)
4207 {
4208 result = MOVE_LINE_TRUNCATED;
4209 break;
4210 }
4211 }
4212
4213 /* Restore the iterator settings altered at the beginning of this
4214 function. */
4215 it->glyph_row = saved_glyph_row;
4216 return result;
4217 }
4218
4219
4220 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4221 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4222 the description of enum move_operation_enum.
4223
4224 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4225 screen line, this function will set IT to the next position >
4226 TO_CHARPOS. */
4227
4228 void
4229 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4230 struct it *it;
4231 int to_charpos, to_x, to_y, to_vpos;
4232 int op;
4233 {
4234 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4235 int line_height;
4236
4237 while (1)
4238 {
4239 if (op & MOVE_TO_VPOS)
4240 {
4241 /* If no TO_CHARPOS and no TO_X specified, stop at the
4242 start of the line TO_VPOS. */
4243 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4244 {
4245 if (it->vpos == to_vpos)
4246 break;
4247 skip = move_it_in_display_line_to (it, -1, -1, 0);
4248 }
4249 else
4250 {
4251 /* TO_VPOS >= 0 means stop at TO_X in the line at
4252 TO_VPOS, or at TO_POS, whichever comes first. */
4253 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4254
4255 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4256 break;
4257 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4258 {
4259 /* We have reached TO_X but not in the line we want. */
4260 skip = move_it_in_display_line_to (it, to_charpos,
4261 -1, MOVE_TO_POS);
4262 if (skip == MOVE_POS_MATCH_OR_ZV)
4263 break;
4264 }
4265 }
4266 }
4267 else if (op & MOVE_TO_Y)
4268 {
4269 struct it it_backup;
4270 int done_p;
4271
4272 /* TO_Y specified means stop at TO_X in the line containing
4273 TO_Y---or at TO_CHARPOS if this is reached first. The
4274 problem is that we can't really tell whether the line
4275 contains TO_Y before we have completely scanned it, and
4276 this may skip past TO_X. What we do is to first scan to
4277 TO_X.
4278
4279 If TO_X is not specified, use a TO_X of zero. The reason
4280 is to make the outcome of this function more predictable.
4281 If we didn't use TO_X == 0, we would stop at the end of
4282 the line which is probably not what a caller would expect
4283 to happen. */
4284 skip = move_it_in_display_line_to (it, to_charpos,
4285 ((op & MOVE_TO_X)
4286 ? to_x : 0),
4287 (MOVE_TO_X
4288 | (op & MOVE_TO_POS)));
4289
4290 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4291 if (skip == MOVE_POS_MATCH_OR_ZV)
4292 break;
4293
4294 /* If TO_X was reached, we would like to know whether TO_Y
4295 is in the line. This can only be said if we know the
4296 total line height which requires us to scan the rest of
4297 the line. */
4298 done_p = 0;
4299 if (skip == MOVE_X_REACHED)
4300 {
4301 it_backup = *it;
4302 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4303 op & MOVE_TO_POS);
4304 }
4305
4306 /* Now, decide whether TO_Y is in this line. */
4307 line_height = it->max_ascent + it->max_descent;
4308
4309 if (to_y >= it->current_y
4310 && to_y < it->current_y + line_height)
4311 {
4312 if (skip == MOVE_X_REACHED)
4313 /* If TO_Y is in this line and TO_X was reached above,
4314 we scanned too far. We have to restore IT's settings
4315 to the ones before skipping. */
4316 *it = it_backup;
4317 done_p = 1;
4318 }
4319 else if (skip == MOVE_X_REACHED)
4320 {
4321 skip = skip2;
4322 if (skip == MOVE_POS_MATCH_OR_ZV)
4323 done_p = 1;
4324 }
4325
4326 if (done_p)
4327 break;
4328 }
4329 else
4330 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4331
4332 switch (skip)
4333 {
4334 case MOVE_POS_MATCH_OR_ZV:
4335 return;
4336
4337 case MOVE_NEWLINE_OR_CR:
4338 set_iterator_to_next (it);
4339 it->continuation_lines_width = 0;
4340 break;
4341
4342 case MOVE_LINE_TRUNCATED:
4343 it->continuation_lines_width = 0;
4344 reseat_at_next_visible_line_start (it, 0);
4345 if ((op & MOVE_TO_POS) != 0
4346 && IT_CHARPOS (*it) > to_charpos)
4347 goto out;
4348 break;
4349
4350 case MOVE_LINE_CONTINUED:
4351 it->continuation_lines_width += it->current_x;
4352 break;
4353
4354 default:
4355 abort ();
4356 }
4357
4358 /* Reset/increment for the next run. */
4359 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4360 it->current_x = it->hpos = 0;
4361 it->current_y += it->max_ascent + it->max_descent;
4362 ++it->vpos;
4363 last_height = it->max_ascent + it->max_descent;
4364 last_max_ascent = it->max_ascent;
4365 it->max_ascent = it->max_descent = 0;
4366 }
4367 out:;
4368 }
4369
4370
4371 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4372
4373 If DY > 0, move IT backward at least that many pixels. DY = 0
4374 means move IT backward to the preceding line start or BEGV. This
4375 function may move over more than DY pixels if IT->current_y - DY
4376 ends up in the middle of a line; in this case IT->current_y will be
4377 set to the top of the line moved to. */
4378
4379 void
4380 move_it_vertically_backward (it, dy)
4381 struct it *it;
4382 int dy;
4383 {
4384 int nlines, h, line_height;
4385 struct it it2;
4386 int start_pos = IT_CHARPOS (*it);
4387
4388 xassert (dy >= 0);
4389
4390 /* Estimate how many newlines we must move back. */
4391 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4392
4393 /* Set the iterator's position that many lines back. */
4394 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4395 back_to_previous_visible_line_start (it);
4396
4397 /* Reseat the iterator here. When moving backward, we don't want
4398 reseat to skip forward over invisible text, set up the iterator
4399 to deliver from overlay strings at the new position etc. So,
4400 use reseat_1 here. */
4401 reseat_1 (it, it->current.pos, 1);
4402
4403 /* We are now surely at a line start. */
4404 it->current_x = it->hpos = 0;
4405
4406 /* Move forward and see what y-distance we moved. First move to the
4407 start of the next line so that we get its height. We need this
4408 height to be able to tell whether we reached the specified
4409 y-distance. */
4410 it2 = *it;
4411 it2.max_ascent = it2.max_descent = 0;
4412 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4413 MOVE_TO_POS | MOVE_TO_VPOS);
4414 xassert (IT_CHARPOS (*it) >= BEGV);
4415 line_height = it2.max_ascent + it2.max_descent;
4416 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4417 xassert (IT_CHARPOS (*it) >= BEGV);
4418 h = it2.current_y - it->current_y;
4419 nlines = it2.vpos - it->vpos;
4420
4421 /* Correct IT's y and vpos position. */
4422 it->vpos -= nlines;
4423 it->current_y -= h;
4424
4425 if (dy == 0)
4426 {
4427 /* DY == 0 means move to the start of the screen line. The
4428 value of nlines is > 0 if continuation lines were involved. */
4429 if (nlines > 0)
4430 move_it_by_lines (it, nlines, 1);
4431 xassert (IT_CHARPOS (*it) <= start_pos);
4432 }
4433 else if (nlines)
4434 {
4435 /* The y-position we try to reach. Note that h has been
4436 subtracted in front of the if-statement. */
4437 int target_y = it->current_y + h - dy;
4438
4439 /* If we did not reach target_y, try to move further backward if
4440 we can. If we moved too far backward, try to move forward. */
4441 if (target_y < it->current_y
4442 && IT_CHARPOS (*it) > BEGV)
4443 {
4444 move_it_vertically (it, target_y - it->current_y);
4445 xassert (IT_CHARPOS (*it) >= BEGV);
4446 }
4447 else if (target_y >= it->current_y + line_height
4448 && IT_CHARPOS (*it) < ZV)
4449 {
4450 move_it_vertically (it, target_y - (it->current_y + line_height));
4451 xassert (IT_CHARPOS (*it) >= BEGV);
4452 }
4453 }
4454 }
4455
4456
4457 /* Move IT by a specified amount of pixel lines DY. DY negative means
4458 move backwards. DY = 0 means move to start of screen line. At the
4459 end, IT will be on the start of a screen line. */
4460
4461 void
4462 move_it_vertically (it, dy)
4463 struct it *it;
4464 int dy;
4465 {
4466 if (dy <= 0)
4467 move_it_vertically_backward (it, -dy);
4468 else if (dy > 0)
4469 {
4470 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4471 MOVE_TO_POS | MOVE_TO_Y);
4472
4473 /* If buffer ends in ZV without a newline, move to the start of
4474 the line to satisfy the post-condition. */
4475 if (IT_CHARPOS (*it) == ZV
4476 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4477 move_it_by_lines (it, 0, 0);
4478 }
4479 }
4480
4481
4482 /* Return non-zero if some text between buffer positions START_CHARPOS
4483 and END_CHARPOS is invisible. IT->window is the window for text
4484 property lookup. */
4485
4486 static int
4487 invisible_text_between_p (it, start_charpos, end_charpos)
4488 struct it *it;
4489 int start_charpos, end_charpos;
4490 {
4491 Lisp_Object prop, limit;
4492 int invisible_found_p;
4493
4494 xassert (it != NULL && start_charpos <= end_charpos);
4495
4496 /* Is text at START invisible? */
4497 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4498 it->window);
4499 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4500 invisible_found_p = 1;
4501 else
4502 {
4503 limit = next_single_char_property_change (make_number (start_charpos),
4504 Qinvisible, Qnil,
4505 make_number (end_charpos));
4506 invisible_found_p = XFASTINT (limit) < end_charpos;
4507 }
4508
4509 return invisible_found_p;
4510 }
4511
4512
4513 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
4514 negative means move up. DVPOS == 0 means move to the start of the
4515 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4516 NEED_Y_P is zero, IT->current_y will be left unchanged.
4517
4518 Further optimization ideas: If we would know that IT->f doesn't use
4519 a face with proportional font, we could be faster for
4520 truncate-lines nil. */
4521
4522 void
4523 move_it_by_lines (it, dvpos, need_y_p)
4524 struct it *it;
4525 int dvpos, need_y_p;
4526 {
4527 struct position pos;
4528
4529 if (!FRAME_WINDOW_P (it->f))
4530 {
4531 struct text_pos textpos;
4532
4533 /* We can use vmotion on frames without proportional fonts. */
4534 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
4535 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
4536 reseat (it, textpos, 1);
4537 it->vpos += pos.vpos;
4538 it->current_y += pos.vpos;
4539 }
4540 else if (dvpos == 0)
4541 {
4542 /* DVPOS == 0 means move to the start of the screen line. */
4543 move_it_vertically_backward (it, 0);
4544 xassert (it->current_x == 0 && it->hpos == 0);
4545 }
4546 else if (dvpos > 0)
4547 {
4548 /* If there are no continuation lines, and if there is no
4549 selective display, try the simple method of moving forward
4550 DVPOS newlines, then see where we are. */
4551 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4552 {
4553 int shortage = 0, charpos;
4554
4555 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
4556 charpos = IT_CHARPOS (*it) + 1;
4557 else
4558 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
4559 &shortage, 0);
4560
4561 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
4562 {
4563 struct text_pos pos;
4564 CHARPOS (pos) = charpos;
4565 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4566 reseat (it, pos, 1);
4567 it->vpos += dvpos - shortage;
4568 it->hpos = it->current_x = 0;
4569 return;
4570 }
4571 }
4572
4573 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
4574 }
4575 else
4576 {
4577 struct it it2;
4578 int start_charpos, i;
4579
4580 /* If there are no continuation lines, and if there is no
4581 selective display, try the simple method of moving backward
4582 -DVPOS newlines. */
4583 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4584 {
4585 int shortage;
4586 int charpos = IT_CHARPOS (*it);
4587 int bytepos = IT_BYTEPOS (*it);
4588
4589 /* If in the middle of a line, go to its start. */
4590 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
4591 {
4592 charpos = find_next_newline_no_quit (charpos, -1);
4593 bytepos = CHAR_TO_BYTE (charpos);
4594 }
4595
4596 if (charpos == BEGV)
4597 {
4598 struct text_pos pos;
4599 CHARPOS (pos) = charpos;
4600 BYTEPOS (pos) = bytepos;
4601 reseat (it, pos, 1);
4602 it->hpos = it->current_x = 0;
4603 return;
4604 }
4605 else
4606 {
4607 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
4608 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
4609 {
4610 struct text_pos pos;
4611 CHARPOS (pos) = charpos;
4612 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4613 reseat (it, pos, 1);
4614 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
4615 it->hpos = it->current_x = 0;
4616 return;
4617 }
4618 }
4619 }
4620
4621 /* Go back -DVPOS visible lines and reseat the iterator there. */
4622 start_charpos = IT_CHARPOS (*it);
4623 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
4624 back_to_previous_visible_line_start (it);
4625 reseat (it, it->current.pos, 1);
4626 it->current_x = it->hpos = 0;
4627
4628 /* Above call may have moved too far if continuation lines
4629 are involved. Scan forward and see if it did. */
4630 it2 = *it;
4631 it2.vpos = it2.current_y = 0;
4632 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
4633 it->vpos -= it2.vpos;
4634 it->current_y -= it2.current_y;
4635 it->current_x = it->hpos = 0;
4636
4637 /* If we moved too far, move IT some lines forward. */
4638 if (it2.vpos > -dvpos)
4639 {
4640 int delta = it2.vpos + dvpos;
4641 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
4642 }
4643 }
4644 }
4645
4646
4647 \f
4648 /***********************************************************************
4649 Messages
4650 ***********************************************************************/
4651
4652
4653 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
4654 to *Messages*. */
4655
4656 void
4657 add_to_log (format, arg1, arg2)
4658 char *format;
4659 Lisp_Object arg1, arg2;
4660 {
4661 Lisp_Object args[3];
4662 Lisp_Object msg, fmt;
4663 char *buffer;
4664 int len;
4665 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4666
4667 fmt = msg = Qnil;
4668 GCPRO4 (fmt, msg, arg1, arg2);
4669
4670 args[0] = fmt = build_string (format);
4671 args[1] = arg1;
4672 args[2] = arg2;
4673 msg = Fformat (3, args);
4674
4675 len = STRING_BYTES (XSTRING (msg)) + 1;
4676 buffer = (char *) alloca (len);
4677 strcpy (buffer, XSTRING (msg)->data);
4678
4679 message_dolog (buffer, len, 1, 0);
4680 UNGCPRO;
4681 }
4682
4683
4684 /* Output a newline in the *Messages* buffer if "needs" one. */
4685
4686 void
4687 message_log_maybe_newline ()
4688 {
4689 if (message_log_need_newline)
4690 message_dolog ("", 0, 1, 0);
4691 }
4692
4693
4694 /* Add a string M of length LEN to the message log, optionally
4695 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
4696 nonzero, means interpret the contents of M as multibyte. This
4697 function calls low-level routines in order to bypass text property
4698 hooks, etc. which might not be safe to run. */
4699
4700 void
4701 message_dolog (m, len, nlflag, multibyte)
4702 char *m;
4703 int len, nlflag, multibyte;
4704 {
4705 if (!NILP (Vmessage_log_max))
4706 {
4707 struct buffer *oldbuf;
4708 Lisp_Object oldpoint, oldbegv, oldzv;
4709 int old_windows_or_buffers_changed = windows_or_buffers_changed;
4710 int point_at_end = 0;
4711 int zv_at_end = 0;
4712 Lisp_Object old_deactivate_mark, tem;
4713 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4714
4715 old_deactivate_mark = Vdeactivate_mark;
4716 oldbuf = current_buffer;
4717 Fset_buffer (Fget_buffer_create (build_string ("*Messages*")));
4718 current_buffer->undo_list = Qt;
4719
4720 oldpoint = Fpoint_marker ();
4721 oldbegv = Fpoint_min_marker ();
4722 oldzv = Fpoint_max_marker ();
4723 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
4724
4725 if (PT == Z)
4726 point_at_end = 1;
4727 if (ZV == Z)
4728 zv_at_end = 1;
4729
4730 BEGV = BEG;
4731 BEGV_BYTE = BEG_BYTE;
4732 ZV = Z;
4733 ZV_BYTE = Z_BYTE;
4734 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4735
4736 /* Insert the string--maybe converting multibyte to single byte
4737 or vice versa, so that all the text fits the buffer. */
4738 if (multibyte
4739 && NILP (current_buffer->enable_multibyte_characters))
4740 {
4741 int i, c, nbytes;
4742 unsigned char work[1];
4743
4744 /* Convert a multibyte string to single-byte
4745 for the *Message* buffer. */
4746 for (i = 0; i < len; i += nbytes)
4747 {
4748 c = string_char_and_length (m + i, len - i, &nbytes);
4749 work[0] = (SINGLE_BYTE_CHAR_P (c)
4750 ? c
4751 : multibyte_char_to_unibyte (c, Qnil));
4752 insert_1_both (work, 1, 1, 1, 0, 0);
4753 }
4754 }
4755 else if (! multibyte
4756 && ! NILP (current_buffer->enable_multibyte_characters))
4757 {
4758 int i, c, nbytes;
4759 unsigned char *msg = (unsigned char *) m;
4760 unsigned char str[MAX_MULTIBYTE_LENGTH];
4761 /* Convert a single-byte string to multibyte
4762 for the *Message* buffer. */
4763 for (i = 0; i < len; i++)
4764 {
4765 c = unibyte_char_to_multibyte (msg[i]);
4766 nbytes = CHAR_STRING (c, str);
4767 insert_1_both (str, 1, nbytes, 1, 0, 0);
4768 }
4769 }
4770 else if (len)
4771 insert_1 (m, len, 1, 0, 0);
4772
4773 if (nlflag)
4774 {
4775 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
4776 insert_1 ("\n", 1, 1, 0, 0);
4777
4778 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
4779 this_bol = PT;
4780 this_bol_byte = PT_BYTE;
4781
4782 if (this_bol > BEG)
4783 {
4784 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
4785 prev_bol = PT;
4786 prev_bol_byte = PT_BYTE;
4787
4788 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
4789 this_bol, this_bol_byte);
4790 if (dup)
4791 {
4792 del_range_both (prev_bol, prev_bol_byte,
4793 this_bol, this_bol_byte, 0);
4794 if (dup > 1)
4795 {
4796 char dupstr[40];
4797 int duplen;
4798
4799 /* If you change this format, don't forget to also
4800 change message_log_check_duplicate. */
4801 sprintf (dupstr, " [%d times]", dup);
4802 duplen = strlen (dupstr);
4803 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
4804 insert_1 (dupstr, duplen, 1, 0, 1);
4805 }
4806 }
4807 }
4808
4809 if (NATNUMP (Vmessage_log_max))
4810 {
4811 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
4812 -XFASTINT (Vmessage_log_max) - 1, 0);
4813 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
4814 }
4815 }
4816 BEGV = XMARKER (oldbegv)->charpos;
4817 BEGV_BYTE = marker_byte_position (oldbegv);
4818
4819 if (zv_at_end)
4820 {
4821 ZV = Z;
4822 ZV_BYTE = Z_BYTE;
4823 }
4824 else
4825 {
4826 ZV = XMARKER (oldzv)->charpos;
4827 ZV_BYTE = marker_byte_position (oldzv);
4828 }
4829
4830 if (point_at_end)
4831 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4832 else
4833 /* We can't do Fgoto_char (oldpoint) because it will run some
4834 Lisp code. */
4835 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
4836 XMARKER (oldpoint)->bytepos);
4837
4838 UNGCPRO;
4839 free_marker (oldpoint);
4840 free_marker (oldbegv);
4841 free_marker (oldzv);
4842
4843 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
4844 set_buffer_internal (oldbuf);
4845 if (NILP (tem))
4846 windows_or_buffers_changed = old_windows_or_buffers_changed;
4847 message_log_need_newline = !nlflag;
4848 Vdeactivate_mark = old_deactivate_mark;
4849 }
4850 }
4851
4852
4853 /* We are at the end of the buffer after just having inserted a newline.
4854 (Note: We depend on the fact we won't be crossing the gap.)
4855 Check to see if the most recent message looks a lot like the previous one.
4856 Return 0 if different, 1 if the new one should just replace it, or a
4857 value N > 1 if we should also append " [N times]". */
4858
4859 static int
4860 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
4861 int prev_bol, this_bol;
4862 int prev_bol_byte, this_bol_byte;
4863 {
4864 int i;
4865 int len = Z_BYTE - 1 - this_bol_byte;
4866 int seen_dots = 0;
4867 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
4868 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
4869
4870 for (i = 0; i < len; i++)
4871 {
4872 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
4873 && p1[i] != '\n')
4874 seen_dots = 1;
4875 if (p1[i] != p2[i])
4876 return seen_dots;
4877 }
4878 p1 += len;
4879 if (*p1 == '\n')
4880 return 2;
4881 if (*p1++ == ' ' && *p1++ == '[')
4882 {
4883 int n = 0;
4884 while (*p1 >= '0' && *p1 <= '9')
4885 n = n * 10 + *p1++ - '0';
4886 if (strncmp (p1, " times]\n", 8) == 0)
4887 return n+1;
4888 }
4889 return 0;
4890 }
4891
4892
4893 /* Display an echo area message M with a specified length of LEN
4894 chars. The string may include null characters. If M is 0, clear
4895 out any existing message, and let the mini-buffer text show through.
4896
4897 The buffer M must continue to exist until after the echo area gets
4898 cleared or some other message gets displayed there. This means do
4899 not pass text that is stored in a Lisp string; do not pass text in
4900 a buffer that was alloca'd. */
4901
4902 void
4903 message2 (m, len, multibyte)
4904 char *m;
4905 int len;
4906 int multibyte;
4907 {
4908 /* First flush out any partial line written with print. */
4909 message_log_maybe_newline ();
4910 if (m)
4911 message_dolog (m, len, 1, multibyte);
4912 message2_nolog (m, len, multibyte);
4913 }
4914
4915
4916 /* The non-logging counterpart of message2. */
4917
4918 void
4919 message2_nolog (m, len, multibyte)
4920 char *m;
4921 int len;
4922 {
4923 struct frame *sf = SELECTED_FRAME ();
4924 message_enable_multibyte = multibyte;
4925
4926 if (noninteractive)
4927 {
4928 if (noninteractive_need_newline)
4929 putc ('\n', stderr);
4930 noninteractive_need_newline = 0;
4931 if (m)
4932 fwrite (m, len, 1, stderr);
4933 if (cursor_in_echo_area == 0)
4934 fprintf (stderr, "\n");
4935 fflush (stderr);
4936 }
4937 /* A null message buffer means that the frame hasn't really been
4938 initialized yet. Error messages get reported properly by
4939 cmd_error, so this must be just an informative message; toss it. */
4940 else if (INTERACTIVE
4941 && sf->glyphs_initialized_p
4942 && FRAME_MESSAGE_BUF (sf))
4943 {
4944 Lisp_Object mini_window;
4945 struct frame *f;
4946
4947 /* Get the frame containing the mini-buffer
4948 that the selected frame is using. */
4949 mini_window = FRAME_MINIBUF_WINDOW (sf);
4950 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
4951
4952 FRAME_SAMPLE_VISIBILITY (f);
4953 if (FRAME_VISIBLE_P (sf)
4954 && ! FRAME_VISIBLE_P (f))
4955 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
4956
4957 if (m)
4958 {
4959 set_message (m, Qnil, len, multibyte);
4960 if (minibuffer_auto_raise)
4961 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
4962 }
4963 else
4964 clear_message (1, 1);
4965
4966 do_pending_window_change (0);
4967 echo_area_display (1);
4968 do_pending_window_change (0);
4969 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
4970 (*frame_up_to_date_hook) (f);
4971 }
4972 }
4973
4974
4975 /* Display an echo area message M with a specified length of NBYTES
4976 bytes. The string may include null characters. If M is not a
4977 string, clear out any existing message, and let the mini-buffer
4978 text show through. */
4979
4980 void
4981 message3 (m, nbytes, multibyte)
4982 Lisp_Object m;
4983 int nbytes;
4984 int multibyte;
4985 {
4986 struct gcpro gcpro1;
4987
4988 GCPRO1 (m);
4989
4990 /* First flush out any partial line written with print. */
4991 message_log_maybe_newline ();
4992 if (STRINGP (m))
4993 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
4994 message3_nolog (m, nbytes, multibyte);
4995
4996 UNGCPRO;
4997 }
4998
4999
5000 /* The non-logging version of message3. */
5001
5002 void
5003 message3_nolog (m, nbytes, multibyte)
5004 Lisp_Object m;
5005 int nbytes, multibyte;
5006 {
5007 struct frame *sf = SELECTED_FRAME ();
5008 message_enable_multibyte = multibyte;
5009
5010 if (noninteractive)
5011 {
5012 if (noninteractive_need_newline)
5013 putc ('\n', stderr);
5014 noninteractive_need_newline = 0;
5015 if (STRINGP (m))
5016 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5017 if (cursor_in_echo_area == 0)
5018 fprintf (stderr, "\n");
5019 fflush (stderr);
5020 }
5021 /* A null message buffer means that the frame hasn't really been
5022 initialized yet. Error messages get reported properly by
5023 cmd_error, so this must be just an informative message; toss it. */
5024 else if (INTERACTIVE
5025 && sf->glyphs_initialized_p
5026 && FRAME_MESSAGE_BUF (sf))
5027 {
5028 Lisp_Object mini_window;
5029 Lisp_Object frame;
5030 struct frame *f;
5031
5032 /* Get the frame containing the mini-buffer
5033 that the selected frame is using. */
5034 mini_window = FRAME_MINIBUF_WINDOW (sf);
5035 frame = XWINDOW (mini_window)->frame;
5036 f = XFRAME (frame);
5037
5038 FRAME_SAMPLE_VISIBILITY (f);
5039 if (FRAME_VISIBLE_P (sf)
5040 && !FRAME_VISIBLE_P (f))
5041 Fmake_frame_visible (frame);
5042
5043 if (STRINGP (m) && XSTRING (m)->size)
5044 {
5045 set_message (NULL, m, nbytes, multibyte);
5046 if (minibuffer_auto_raise)
5047 Fraise_frame (frame);
5048 }
5049 else
5050 clear_message (1, 1);
5051
5052 do_pending_window_change (0);
5053 echo_area_display (1);
5054 do_pending_window_change (0);
5055 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5056 (*frame_up_to_date_hook) (f);
5057 }
5058 }
5059
5060
5061 /* Display a null-terminated echo area message M. If M is 0, clear
5062 out any existing message, and let the mini-buffer text show through.
5063
5064 The buffer M must continue to exist until after the echo area gets
5065 cleared or some other message gets displayed there. Do not pass
5066 text that is stored in a Lisp string. Do not pass text in a buffer
5067 that was alloca'd. */
5068
5069 void
5070 message1 (m)
5071 char *m;
5072 {
5073 message2 (m, (m ? strlen (m) : 0), 0);
5074 }
5075
5076
5077 /* The non-logging counterpart of message1. */
5078
5079 void
5080 message1_nolog (m)
5081 char *m;
5082 {
5083 message2_nolog (m, (m ? strlen (m) : 0), 0);
5084 }
5085
5086 /* Display a message M which contains a single %s
5087 which gets replaced with STRING. */
5088
5089 void
5090 message_with_string (m, string, log)
5091 char *m;
5092 Lisp_Object string;
5093 int log;
5094 {
5095 if (noninteractive)
5096 {
5097 if (m)
5098 {
5099 if (noninteractive_need_newline)
5100 putc ('\n', stderr);
5101 noninteractive_need_newline = 0;
5102 fprintf (stderr, m, XSTRING (string)->data);
5103 if (cursor_in_echo_area == 0)
5104 fprintf (stderr, "\n");
5105 fflush (stderr);
5106 }
5107 }
5108 else if (INTERACTIVE)
5109 {
5110 /* The frame whose minibuffer we're going to display the message on.
5111 It may be larger than the selected frame, so we need
5112 to use its buffer, not the selected frame's buffer. */
5113 Lisp_Object mini_window;
5114 struct frame *f, *sf = SELECTED_FRAME ();
5115
5116 /* Get the frame containing the minibuffer
5117 that the selected frame is using. */
5118 mini_window = FRAME_MINIBUF_WINDOW (sf);
5119 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5120
5121 /* A null message buffer means that the frame hasn't really been
5122 initialized yet. Error messages get reported properly by
5123 cmd_error, so this must be just an informative message; toss it. */
5124 if (FRAME_MESSAGE_BUF (f))
5125 {
5126 int len;
5127 char *a[1];
5128 a[0] = (char *) XSTRING (string)->data;
5129
5130 len = doprnt (FRAME_MESSAGE_BUF (f),
5131 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5132
5133 if (log)
5134 message2 (FRAME_MESSAGE_BUF (f), len,
5135 STRING_MULTIBYTE (string));
5136 else
5137 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5138 STRING_MULTIBYTE (string));
5139
5140 /* Print should start at the beginning of the message
5141 buffer next time. */
5142 message_buf_print = 0;
5143 }
5144 }
5145 }
5146
5147
5148 /* Dump an informative message to the minibuf. If M is 0, clear out
5149 any existing message, and let the mini-buffer text show through. */
5150
5151 /* VARARGS 1 */
5152 void
5153 message (m, a1, a2, a3)
5154 char *m;
5155 EMACS_INT a1, a2, a3;
5156 {
5157 if (noninteractive)
5158 {
5159 if (m)
5160 {
5161 if (noninteractive_need_newline)
5162 putc ('\n', stderr);
5163 noninteractive_need_newline = 0;
5164 fprintf (stderr, m, a1, a2, a3);
5165 if (cursor_in_echo_area == 0)
5166 fprintf (stderr, "\n");
5167 fflush (stderr);
5168 }
5169 }
5170 else if (INTERACTIVE)
5171 {
5172 /* The frame whose mini-buffer we're going to display the message
5173 on. It may be larger than the selected frame, so we need to
5174 use its buffer, not the selected frame's buffer. */
5175 Lisp_Object mini_window;
5176 struct frame *f, *sf = SELECTED_FRAME ();
5177
5178 /* Get the frame containing the mini-buffer
5179 that the selected frame is using. */
5180 mini_window = FRAME_MINIBUF_WINDOW (sf);
5181 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5182
5183 /* A null message buffer means that the frame hasn't really been
5184 initialized yet. Error messages get reported properly by
5185 cmd_error, so this must be just an informative message; toss
5186 it. */
5187 if (FRAME_MESSAGE_BUF (f))
5188 {
5189 if (m)
5190 {
5191 int len;
5192 #ifdef NO_ARG_ARRAY
5193 char *a[3];
5194 a[0] = (char *) a1;
5195 a[1] = (char *) a2;
5196 a[2] = (char *) a3;
5197
5198 len = doprnt (FRAME_MESSAGE_BUF (f),
5199 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5200 #else
5201 len = doprnt (FRAME_MESSAGE_BUF (f),
5202 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5203 (char **) &a1);
5204 #endif /* NO_ARG_ARRAY */
5205
5206 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5207 }
5208 else
5209 message1 (0);
5210
5211 /* Print should start at the beginning of the message
5212 buffer next time. */
5213 message_buf_print = 0;
5214 }
5215 }
5216 }
5217
5218
5219 /* The non-logging version of message. */
5220
5221 void
5222 message_nolog (m, a1, a2, a3)
5223 char *m;
5224 EMACS_INT a1, a2, a3;
5225 {
5226 Lisp_Object old_log_max;
5227 old_log_max = Vmessage_log_max;
5228 Vmessage_log_max = Qnil;
5229 message (m, a1, a2, a3);
5230 Vmessage_log_max = old_log_max;
5231 }
5232
5233
5234 /* Display the current message in the current mini-buffer. This is
5235 only called from error handlers in process.c, and is not time
5236 critical. */
5237
5238 void
5239 update_echo_area ()
5240 {
5241 if (!NILP (echo_area_buffer[0]))
5242 {
5243 Lisp_Object string;
5244 string = Fcurrent_message ();
5245 message3 (string, XSTRING (string)->size,
5246 !NILP (current_buffer->enable_multibyte_characters));
5247 }
5248 }
5249
5250
5251 /* Make sure echo area buffers in echo_buffers[] are life. If they
5252 aren't, make new ones. */
5253
5254 static void
5255 ensure_echo_area_buffers ()
5256 {
5257 int i;
5258
5259 for (i = 0; i < 2; ++i)
5260 if (!BUFFERP (echo_buffer[i])
5261 || NILP (XBUFFER (echo_buffer[i])->name))
5262 {
5263 char name[30];
5264 sprintf (name, " *Echo Area %d*", i);
5265 echo_buffer[i] = Fget_buffer_create (build_string (name));
5266 }
5267 }
5268
5269
5270 /* Call FN with args A1..A5 with either the current or last displayed
5271 echo_area_buffer as current buffer.
5272
5273 WHICH zero means use the current message buffer
5274 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5275 from echo_buffer[] and clear it.
5276
5277 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5278 suitable buffer from echo_buffer[] and clear it.
5279
5280 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5281 that the current message becomes the last displayed one, make
5282 choose a suitable buffer for echo_area_buffer[0], and clear it.
5283
5284 Value is what FN returns. */
5285
5286 static int
5287 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4, a5)
5288 struct window *w;
5289 int which;
5290 int (*fn) ();
5291 int a1, a2, a3, a4, a5;
5292 {
5293 Lisp_Object buffer;
5294 int this_one, the_other, clear_buffer_p, rc;
5295 int count = specpdl_ptr - specpdl;
5296
5297 /* If buffers aren't life, make new ones. */
5298 ensure_echo_area_buffers ();
5299
5300 clear_buffer_p = 0;
5301
5302 if (which == 0)
5303 this_one = 0, the_other = 1;
5304 else if (which > 0)
5305 this_one = 1, the_other = 0;
5306 else
5307 {
5308 this_one = 0, the_other = 1;
5309 clear_buffer_p = 1;
5310
5311 /* We need a fresh one in case the current echo buffer equals
5312 the one containing the last displayed echo area message. */
5313 if (!NILP (echo_area_buffer[this_one])
5314 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5315 echo_area_buffer[this_one] = Qnil;
5316 }
5317
5318 /* Choose a suitable buffer from echo_buffer[] is we don't
5319 have one. */
5320 if (NILP (echo_area_buffer[this_one]))
5321 {
5322 echo_area_buffer[this_one]
5323 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5324 ? echo_buffer[the_other]
5325 : echo_buffer[this_one]);
5326 clear_buffer_p = 1;
5327 }
5328
5329 buffer = echo_area_buffer[this_one];
5330
5331 record_unwind_protect (unwind_with_echo_area_buffer,
5332 with_echo_area_buffer_unwind_data (w));
5333
5334 /* Make the echo area buffer current. Note that for display
5335 purposes, it is not necessary that the displayed window's buffer
5336 == current_buffer, except for text property lookup. So, let's
5337 only set that buffer temporarily here without doing a full
5338 Fset_window_buffer. We must also change w->pointm, though,
5339 because otherwise an assertions in unshow_buffer fails, and Emacs
5340 aborts. */
5341 set_buffer_internal_1 (XBUFFER (buffer));
5342 if (w)
5343 {
5344 w->buffer = buffer;
5345 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5346 }
5347 current_buffer->truncate_lines = Qnil;
5348 current_buffer->undo_list = Qt;
5349 current_buffer->read_only = Qnil;
5350
5351 if (clear_buffer_p && Z > BEG)
5352 del_range (BEG, Z);
5353
5354 xassert (BEGV >= BEG);
5355 xassert (ZV <= Z && ZV >= BEGV);
5356
5357 rc = fn (a1, a2, a3, a4, a5);
5358
5359 xassert (BEGV >= BEG);
5360 xassert (ZV <= Z && ZV >= BEGV);
5361
5362 unbind_to (count, Qnil);
5363 return rc;
5364 }
5365
5366
5367 /* Save state that should be preserved around the call to the function
5368 FN called in with_echo_area_buffer. */
5369
5370 static Lisp_Object
5371 with_echo_area_buffer_unwind_data (w)
5372 struct window *w;
5373 {
5374 int i = 0;
5375 Lisp_Object vector;
5376
5377 /* Reduce consing by keeping one vector in
5378 Vwith_echo_area_save_vector. */
5379 vector = Vwith_echo_area_save_vector;
5380 Vwith_echo_area_save_vector = Qnil;
5381
5382 if (NILP (vector))
5383 vector = Fmake_vector (make_number (7), Qnil);
5384
5385 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5386 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5387 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
5388
5389 if (w)
5390 {
5391 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5392 XVECTOR (vector)->contents[i++] = w->buffer;
5393 XVECTOR (vector)->contents[i++]
5394 = make_number (XMARKER (w->pointm)->charpos);
5395 XVECTOR (vector)->contents[i++]
5396 = make_number (XMARKER (w->pointm)->bytepos);
5397 }
5398 else
5399 {
5400 int end = i + 4;
5401 while (i < end)
5402 XVECTOR (vector)->contents[i++] = Qnil;
5403 }
5404
5405 xassert (i == XVECTOR (vector)->size);
5406 return vector;
5407 }
5408
5409
5410 /* Restore global state from VECTOR which was created by
5411 with_echo_area_buffer_unwind_data. */
5412
5413 static Lisp_Object
5414 unwind_with_echo_area_buffer (vector)
5415 Lisp_Object vector;
5416 {
5417 int i = 0;
5418
5419 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
5420 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5421 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
5422
5423 if (WINDOWP (XVECTOR (vector)->contents[i]))
5424 {
5425 struct window *w;
5426 Lisp_Object buffer, charpos, bytepos;
5427
5428 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5429 buffer = XVECTOR (vector)->contents[i]; ++i;
5430 charpos = XVECTOR (vector)->contents[i]; ++i;
5431 bytepos = XVECTOR (vector)->contents[i]; ++i;
5432
5433 w->buffer = buffer;
5434 set_marker_both (w->pointm, buffer,
5435 XFASTINT (charpos), XFASTINT (bytepos));
5436 }
5437
5438 Vwith_echo_area_save_vector = vector;
5439 return Qnil;
5440 }
5441
5442
5443 /* Set up the echo area for use by print functions. MULTIBYTE_P
5444 non-zero means we will print multibyte. */
5445
5446 void
5447 setup_echo_area_for_printing (multibyte_p)
5448 int multibyte_p;
5449 {
5450 ensure_echo_area_buffers ();
5451
5452 if (!message_buf_print)
5453 {
5454 /* A message has been output since the last time we printed.
5455 Choose a fresh echo area buffer. */
5456 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5457 echo_area_buffer[0] = echo_buffer[1];
5458 else
5459 echo_area_buffer[0] = echo_buffer[0];
5460
5461 /* Switch to that buffer and clear it. */
5462 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5463 if (Z > BEG)
5464 del_range (BEG, Z);
5465 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5466
5467 /* Set up the buffer for the multibyteness we need. */
5468 if (multibyte_p
5469 != !NILP (current_buffer->enable_multibyte_characters))
5470 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5471
5472 /* Raise the frame containing the echo area. */
5473 if (minibuffer_auto_raise)
5474 {
5475 struct frame *sf = SELECTED_FRAME ();
5476 Lisp_Object mini_window;
5477 mini_window = FRAME_MINIBUF_WINDOW (sf);
5478 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5479 }
5480
5481 message_buf_print = 1;
5482 }
5483 else if (current_buffer != XBUFFER (echo_area_buffer[0]))
5484 /* Someone switched buffers between print requests. */
5485 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5486 }
5487
5488
5489 /* Display an echo area message in window W. Value is non-zero if W's
5490 height is changed. If display_last_displayed_message_p is
5491 non-zero, display the message that was last displayed, otherwise
5492 display the current message. */
5493
5494 static int
5495 display_echo_area (w)
5496 struct window *w;
5497 {
5498 int i, no_message_p, window_height_changed_p, count;
5499
5500 /* Temporarily disable garbage collections while displaying the echo
5501 area. This is done because a GC can print a message itself.
5502 That message would modify the echo area buffer's contents while a
5503 redisplay of the buffer is going on, and seriously confuse
5504 redisplay. */
5505 count = inhibit_garbage_collection ();
5506
5507 /* If there is no message, we must call display_echo_area_1
5508 nevertheless because it resizes the window. But we will have to
5509 reset the echo_area_buffer in question to nil at the end because
5510 with_echo_area_buffer will sets it to an empty buffer. */
5511 i = display_last_displayed_message_p ? 1 : 0;
5512 no_message_p = NILP (echo_area_buffer[i]);
5513
5514 window_height_changed_p
5515 = with_echo_area_buffer (w, display_last_displayed_message_p,
5516 (int (*) ()) display_echo_area_1, w);
5517
5518 if (no_message_p)
5519 echo_area_buffer[i] = Qnil;
5520
5521 unbind_to (count, Qnil);
5522 return window_height_changed_p;
5523 }
5524
5525
5526 /* Helper for display_echo_area. Display the current buffer which
5527 contains the current echo area message in window W, a mini-window.
5528 Change the height of W so that all of the message is displayed.
5529 Value is non-zero if height of W was changed. */
5530
5531 static int
5532 display_echo_area_1 (w)
5533 struct window *w;
5534 {
5535 Lisp_Object window;
5536 struct text_pos start;
5537 int window_height_changed_p = 0;
5538
5539 /* Do this before displaying, so that we have a large enough glyph
5540 matrix for the display. */
5541 window_height_changed_p = resize_mini_window (w, 0);
5542
5543 /* Display. */
5544 clear_glyph_matrix (w->desired_matrix);
5545 XSETWINDOW (window, w);
5546 SET_TEXT_POS (start, BEG, BEG_BYTE);
5547 try_window (window, start);
5548
5549 return window_height_changed_p;
5550 }
5551
5552
5553 /* Resize the echo area window to exactly the size needed for the
5554 currently displayed message, if there is one. */
5555
5556 void
5557 resize_echo_area_axactly ()
5558 {
5559 if (BUFFERP (echo_area_buffer[0])
5560 && WINDOWP (echo_area_window))
5561 {
5562 struct window *w = XWINDOW (echo_area_window);
5563 int resized_p;
5564
5565 resized_p = with_echo_area_buffer (w, 0,
5566 (int (*) ()) resize_mini_window,
5567 w, 1);
5568 if (resized_p)
5569 {
5570 ++windows_or_buffers_changed;
5571 ++update_mode_lines;
5572 redisplay_internal (0);
5573 }
5574 }
5575 }
5576
5577
5578 /* Resize mini-window W to fit the size of its contents. EXACT:P
5579 means size the window exactly to the size needed. Otherwise, it's
5580 only enlarged until W's buffer is empty. Value is non-zero if
5581 the window height has been changed. */
5582
5583 int
5584 resize_mini_window (w, exact_p)
5585 struct window *w;
5586 int exact_p;
5587 {
5588 struct frame *f = XFRAME (w->frame);
5589 int window_height_changed_p = 0;
5590
5591 xassert (MINI_WINDOW_P (w));
5592
5593 /* Nil means don't try to resize. */
5594 if (NILP (Vmax_mini_window_height)
5595 || (FRAME_X_P (f) && f->output_data.x == NULL))
5596 return 0;
5597
5598 if (!FRAME_MINIBUF_ONLY_P (f))
5599 {
5600 struct it it;
5601 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
5602 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
5603 int height, max_height;
5604 int unit = CANON_Y_UNIT (f);
5605 struct text_pos start;
5606
5607 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
5608
5609 /* Compute the max. number of lines specified by the user. */
5610 if (FLOATP (Vmax_mini_window_height))
5611 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
5612 else if (INTEGERP (Vmax_mini_window_height))
5613 max_height = XINT (Vmax_mini_window_height);
5614 else
5615 max_height = total_height / 4;
5616
5617 /* Correct that max. height if it's bogus. */
5618 max_height = max (1, max_height);
5619 max_height = min (total_height, max_height);
5620
5621 /* Find out the height of the text in the window. */
5622 last_height = 0;
5623 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
5624 if (it.max_ascent == 0 && it.max_descent == 0)
5625 height = it.current_y + last_height;
5626 else
5627 height = it.current_y + it.max_ascent + it.max_descent;
5628 height = (height + unit - 1) / unit;
5629
5630 /* Compute a suitable window start. */
5631 if (height > max_height)
5632 {
5633 height = max_height;
5634 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
5635 move_it_vertically_backward (&it, (height - 1) * unit);
5636 start = it.current.pos;
5637 }
5638 else
5639 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5640 SET_MARKER_FROM_TEXT_POS (w->start, start);
5641
5642 /* Let it grow only, until we display an empty message, in which
5643 case the window shrinks again. */
5644 if (height > XFASTINT (w->height))
5645 {
5646 int old_height = XFASTINT (w->height);
5647 freeze_window_starts (f, 1);
5648 grow_mini_window (w, height - XFASTINT (w->height));
5649 window_height_changed_p = XFASTINT (w->height) != old_height;
5650 }
5651 else if (height < XFASTINT (w->height)
5652 && (exact_p || BEGV == ZV))
5653 {
5654 int old_height = XFASTINT (w->height);
5655 freeze_window_starts (f, 0);
5656 shrink_mini_window (w);
5657 window_height_changed_p = XFASTINT (w->height) != old_height;
5658 }
5659 }
5660
5661 return window_height_changed_p;
5662 }
5663
5664
5665 /* Value is the current message, a string, or nil if there is no
5666 current message. */
5667
5668 Lisp_Object
5669 current_message ()
5670 {
5671 Lisp_Object msg;
5672
5673 if (NILP (echo_area_buffer[0]))
5674 msg = Qnil;
5675 else
5676 {
5677 with_echo_area_buffer (0, 0, (int (*) ()) current_message_1, &msg);
5678 if (NILP (msg))
5679 echo_area_buffer[0] = Qnil;
5680 }
5681
5682 return msg;
5683 }
5684
5685
5686 static int
5687 current_message_1 (msg)
5688 Lisp_Object *msg;
5689 {
5690 if (Z > BEG)
5691 *msg = make_buffer_string (BEG, Z, 1);
5692 else
5693 *msg = Qnil;
5694 return 0;
5695 }
5696
5697
5698 /* Push the current message on Vmessage_stack for later restauration
5699 by restore_message. Value is non-zero if the current message isn't
5700 empty. This is a relatively infrequent operation, so it's not
5701 worth optimizing. */
5702
5703 int
5704 push_message ()
5705 {
5706 Lisp_Object msg;
5707 msg = current_message ();
5708 Vmessage_stack = Fcons (msg, Vmessage_stack);
5709 return STRINGP (msg);
5710 }
5711
5712
5713 /* Restore message display from the top of Vmessage_stack. */
5714
5715 void
5716 restore_message ()
5717 {
5718 Lisp_Object msg;
5719
5720 xassert (CONSP (Vmessage_stack));
5721 msg = XCAR (Vmessage_stack);
5722 if (STRINGP (msg))
5723 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
5724 else
5725 message3_nolog (msg, 0, 0);
5726 }
5727
5728
5729 /* Pop the top-most entry off Vmessage_stack. */
5730
5731 void
5732 pop_message ()
5733 {
5734 xassert (CONSP (Vmessage_stack));
5735 Vmessage_stack = XCDR (Vmessage_stack);
5736 }
5737
5738
5739 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
5740 exits. If the stack is not empty, we have a missing pop_message
5741 somewhere. */
5742
5743 void
5744 check_message_stack ()
5745 {
5746 if (!NILP (Vmessage_stack))
5747 abort ();
5748 }
5749
5750
5751 /* Truncate to NCHARS what will be displayed in the echo area the next
5752 time we display it---but don't redisplay it now. */
5753
5754 void
5755 truncate_echo_area (nchars)
5756 int nchars;
5757 {
5758 if (nchars == 0)
5759 echo_area_buffer[0] = Qnil;
5760 /* A null message buffer means that the frame hasn't really been
5761 initialized yet. Error messages get reported properly by
5762 cmd_error, so this must be just an informative message; toss it. */
5763 else if (!noninteractive
5764 && INTERACTIVE
5765 && !NILP (echo_area_buffer[0]))
5766 {
5767 struct frame *sf = SELECTED_FRAME ();
5768 if (FRAME_MESSAGE_BUF (sf))
5769 with_echo_area_buffer (0, 0, (int (*) ()) truncate_message_1, nchars);
5770 }
5771 }
5772
5773
5774 /* Helper function for truncate_echo_area. Truncate the current
5775 message to at most NCHARS characters. */
5776
5777 static int
5778 truncate_message_1 (nchars)
5779 int nchars;
5780 {
5781 if (BEG + nchars < Z)
5782 del_range (BEG + nchars, Z);
5783 if (Z == BEG)
5784 echo_area_buffer[0] = Qnil;
5785 return 0;
5786 }
5787
5788
5789 /* Set the current message to a substring of S or STRING.
5790
5791 If STRING is a Lisp string, set the message to the first NBYTES
5792 bytes from STRING. NBYTES zero means use the whole string. If
5793 STRING is multibyte, the message will be displayed multibyte.
5794
5795 If S is not null, set the message to the first LEN bytes of S. LEN
5796 zero means use the whole string. MULTIBYTE_P non-zero means S is
5797 multibyte. Display the message multibyte in that case. */
5798
5799 void
5800 set_message (s, string, nbytes, multibyte_p)
5801 char *s;
5802 Lisp_Object string;
5803 int nbytes;
5804 {
5805 message_enable_multibyte
5806 = ((s && multibyte_p)
5807 || (STRINGP (string) && STRING_MULTIBYTE (string)));
5808
5809 with_echo_area_buffer (0, -1, (int (*) ()) set_message_1,
5810 s, string, nbytes, multibyte_p);
5811 message_buf_print = 0;
5812 }
5813
5814
5815 /* Helper function for set_message. Arguments have the same meaning
5816 as there. This function is called with the echo area buffer being
5817 current. */
5818
5819 static int
5820 set_message_1 (s, string, nbytes, multibyte_p)
5821 char *s;
5822 Lisp_Object string;
5823 int nbytes, multibyte_p;
5824 {
5825 xassert (BEG == Z);
5826
5827 /* Change multibyteness of the echo buffer appropriately. */
5828 if (message_enable_multibyte
5829 != !NILP (current_buffer->enable_multibyte_characters))
5830 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
5831
5832 /* Insert new message at BEG. */
5833 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5834
5835 if (STRINGP (string))
5836 {
5837 int nchars;
5838
5839 if (nbytes == 0)
5840 nbytes = XSTRING (string)->size_byte;
5841 nchars = string_byte_to_char (string, nbytes);
5842
5843 /* This function takes care of single/multibyte conversion. We
5844 just have to ensure that the echo area buffer has the right
5845 setting of enable_multibyte_characters. */
5846 insert_from_string (string, 0, 0, nchars, nbytes, 1);
5847 }
5848 else if (s)
5849 {
5850 if (nbytes == 0)
5851 nbytes = strlen (s);
5852
5853 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
5854 {
5855 /* Convert from multi-byte to single-byte. */
5856 int i, c, n;
5857 unsigned char work[1];
5858
5859 /* Convert a multibyte string to single-byte. */
5860 for (i = 0; i < nbytes; i += n)
5861 {
5862 c = string_char_and_length (s + i, nbytes - i, &n);
5863 work[0] = (SINGLE_BYTE_CHAR_P (c)
5864 ? c
5865 : multibyte_char_to_unibyte (c, Qnil));
5866 insert_1_both (work, 1, 1, 1, 0, 0);
5867 }
5868 }
5869 else if (!multibyte_p
5870 && !NILP (current_buffer->enable_multibyte_characters))
5871 {
5872 /* Convert from single-byte to multi-byte. */
5873 int i, c, n;
5874 unsigned char *msg = (unsigned char *) s;
5875 unsigned char str[MAX_MULTIBYTE_LENGTH];
5876
5877 /* Convert a single-byte string to multibyte. */
5878 for (i = 0; i < nbytes; i++)
5879 {
5880 c = unibyte_char_to_multibyte (msg[i]);
5881 n = CHAR_STRING (c, str);
5882 insert_1_both (str, 1, n, 1, 0, 0);
5883 }
5884 }
5885 else
5886 insert_1 (s, nbytes, 1, 0, 0);
5887 }
5888
5889 return 0;
5890 }
5891
5892
5893 /* Clear messages. CURRENT_P non-zero means clear the current
5894 message. LAST_DISPLAYED_P non-zero means clear the message
5895 last displayed. */
5896
5897 void
5898 clear_message (current_p, last_displayed_p)
5899 int current_p, last_displayed_p;
5900 {
5901 if (current_p)
5902 echo_area_buffer[0] = Qnil;
5903
5904 if (last_displayed_p)
5905 echo_area_buffer[1] = Qnil;
5906
5907 message_buf_print = 0;
5908 }
5909
5910 /* Clear garbaged frames.
5911
5912 This function is used where the old redisplay called
5913 redraw_garbaged_frames which in turn called redraw_frame which in
5914 turn called clear_frame. The call to clear_frame was a source of
5915 flickering. I believe a clear_frame is not necessary. It should
5916 suffice in the new redisplay to invalidate all current matrices,
5917 and ensure a complete redisplay of all windows. */
5918
5919 static void
5920 clear_garbaged_frames ()
5921 {
5922 if (frame_garbaged)
5923 {
5924 Lisp_Object tail, frame;
5925
5926 FOR_EACH_FRAME (tail, frame)
5927 {
5928 struct frame *f = XFRAME (frame);
5929
5930 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
5931 {
5932 clear_current_matrices (f);
5933 f->garbaged = 0;
5934 }
5935 }
5936
5937 frame_garbaged = 0;
5938 ++windows_or_buffers_changed;
5939 }
5940 }
5941
5942
5943 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
5944 is non-zero update selected_frame. Value is non-zero if the
5945 mini-windows height has been changed. */
5946
5947 static int
5948 echo_area_display (update_frame_p)
5949 int update_frame_p;
5950 {
5951 Lisp_Object mini_window;
5952 struct window *w;
5953 struct frame *f;
5954 int window_height_changed_p = 0;
5955 struct frame *sf = SELECTED_FRAME ();
5956
5957 mini_window = FRAME_MINIBUF_WINDOW (sf);
5958 w = XWINDOW (mini_window);
5959 f = XFRAME (WINDOW_FRAME (w));
5960
5961 /* Don't display if frame is invisible or not yet initialized. */
5962 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
5963 return 0;
5964
5965 #ifdef HAVE_WINDOW_SYSTEM
5966 /* When Emacs starts, selected_frame may be a visible terminal
5967 frame, even if we run under a window system. If we let this
5968 through, a message would be displayed on the terminal. */
5969 if (EQ (selected_frame, Vterminal_frame)
5970 && !NILP (Vwindow_system))
5971 return 0;
5972 #endif /* HAVE_WINDOW_SYSTEM */
5973
5974 /* Redraw garbaged frames. */
5975 if (frame_garbaged)
5976 clear_garbaged_frames ();
5977
5978 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
5979 {
5980 echo_area_window = mini_window;
5981 window_height_changed_p = display_echo_area (w);
5982 w->must_be_updated_p = 1;
5983
5984 if (update_frame_p)
5985 {
5986 /* Not called from redisplay_internal. If we changed
5987 window configuration, we must redisplay thoroughly.
5988 Otherwise, we can do with updating what we displayed
5989 above. */
5990 if (window_height_changed_p)
5991 {
5992 ++windows_or_buffers_changed;
5993 ++update_mode_lines;
5994 redisplay_internal (0);
5995 }
5996 else if (FRAME_WINDOW_P (f))
5997 {
5998 update_single_window (w, 1);
5999 rif->flush_display (f);
6000 }
6001 else
6002 update_frame (f, 1, 1);
6003 }
6004 }
6005 else if (!EQ (mini_window, selected_window))
6006 windows_or_buffers_changed++;
6007
6008 /* Last displayed message is now the current message. */
6009 echo_area_buffer[1] = echo_area_buffer[0];
6010
6011 /* Prevent redisplay optimization in redisplay_internal by resetting
6012 this_line_start_pos. This is done because the mini-buffer now
6013 displays the message instead of its buffer text. */
6014 if (EQ (mini_window, selected_window))
6015 CHARPOS (this_line_start_pos) = 0;
6016
6017 return window_height_changed_p;
6018 }
6019
6020
6021 \f
6022 /***********************************************************************
6023 Frame Titles
6024 ***********************************************************************/
6025
6026
6027 #ifdef HAVE_WINDOW_SYSTEM
6028
6029 /* A buffer for constructing frame titles in it; allocated from the
6030 heap in init_xdisp and resized as needed in store_frame_title_char. */
6031
6032 static char *frame_title_buf;
6033
6034 /* The buffer's end, and a current output position in it. */
6035
6036 static char *frame_title_buf_end;
6037 static char *frame_title_ptr;
6038
6039
6040 /* Store a single character C for the frame title in frame_title_buf.
6041 Re-allocate frame_title_buf if necessary. */
6042
6043 static void
6044 store_frame_title_char (c)
6045 char c;
6046 {
6047 /* If output position has reached the end of the allocated buffer,
6048 double the buffer's size. */
6049 if (frame_title_ptr == frame_title_buf_end)
6050 {
6051 int len = frame_title_ptr - frame_title_buf;
6052 int new_size = 2 * len * sizeof *frame_title_buf;
6053 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6054 frame_title_buf_end = frame_title_buf + new_size;
6055 frame_title_ptr = frame_title_buf + len;
6056 }
6057
6058 *frame_title_ptr++ = c;
6059 }
6060
6061
6062 /* Store part of a frame title in frame_title_buf, beginning at
6063 frame_title_ptr. STR is the string to store. Do not copy more
6064 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
6065 the whole string. Pad with spaces until FIELD_WIDTH number of
6066 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
6067 Called from display_mode_element when it is used to build a frame
6068 title. */
6069
6070 static int
6071 store_frame_title (str, field_width, precision)
6072 unsigned char *str;
6073 int field_width, precision;
6074 {
6075 int n = 0;
6076
6077 /* Copy at most PRECISION chars from STR. */
6078 while ((precision <= 0 || n < precision)
6079 && *str)
6080 {
6081 store_frame_title_char (*str++);
6082 ++n;
6083 }
6084
6085 /* Fill up with spaces until FIELD_WIDTH reached. */
6086 while (field_width > 0
6087 && n < field_width)
6088 {
6089 store_frame_title_char (' ');
6090 ++n;
6091 }
6092
6093 return n;
6094 }
6095
6096
6097 /* Set the title of FRAME, if it has changed. The title format is
6098 Vicon_title_format if FRAME is iconified, otherwise it is
6099 frame_title_format. */
6100
6101 static void
6102 x_consider_frame_title (frame)
6103 Lisp_Object frame;
6104 {
6105 struct frame *f = XFRAME (frame);
6106
6107 if (FRAME_WINDOW_P (f)
6108 || FRAME_MINIBUF_ONLY_P (f)
6109 || f->explicit_name)
6110 {
6111 /* Do we have more than one visible frame on this X display? */
6112 Lisp_Object tail;
6113 Lisp_Object fmt;
6114 struct buffer *obuf;
6115 int len;
6116 struct it it;
6117
6118 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
6119 {
6120 struct frame *tf = XFRAME (XCAR (tail));
6121
6122 if (tf != f
6123 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6124 && !FRAME_MINIBUF_ONLY_P (tf)
6125 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6126 break;
6127 }
6128
6129 /* Set global variable indicating that multiple frames exist. */
6130 multiple_frames = CONSP (tail);
6131
6132 /* Switch to the buffer of selected window of the frame. Set up
6133 frame_title_ptr so that display_mode_element will output into it;
6134 then display the title. */
6135 obuf = current_buffer;
6136 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6137 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6138 frame_title_ptr = frame_title_buf;
6139 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6140 NULL, DEFAULT_FACE_ID);
6141 len = display_mode_element (&it, 0, -1, -1, fmt);
6142 frame_title_ptr = NULL;
6143 set_buffer_internal (obuf);
6144
6145 /* Set the title only if it's changed. This avoids consing in
6146 the common case where it hasn't. (If it turns out that we've
6147 already wasted too much time by walking through the list with
6148 display_mode_element, then we might need to optimize at a
6149 higher level than this.) */
6150 if (! STRINGP (f->name)
6151 || STRING_BYTES (XSTRING (f->name)) != len
6152 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6153 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6154 }
6155 }
6156
6157 #else /* not HAVE_WINDOW_SYSTEM */
6158
6159 #define frame_title_ptr ((char *)0)
6160 #define store_frame_title(str, mincol, maxcol) 0
6161
6162 #endif /* not HAVE_WINDOW_SYSTEM */
6163
6164
6165
6166 \f
6167 /***********************************************************************
6168 Menu Bars
6169 ***********************************************************************/
6170
6171
6172 /* Prepare for redisplay by updating menu-bar item lists when
6173 appropriate. This can call eval. */
6174
6175 void
6176 prepare_menu_bars ()
6177 {
6178 int all_windows;
6179 struct gcpro gcpro1, gcpro2;
6180 struct frame *f;
6181 struct frame *tooltip_frame;
6182
6183 #ifdef HAVE_X_WINDOWS
6184 tooltip_frame = tip_frame;
6185 #else
6186 tooltip_frame = NULL;
6187 #endif
6188
6189 /* Update all frame titles based on their buffer names, etc. We do
6190 this before the menu bars so that the buffer-menu will show the
6191 up-to-date frame titles. */
6192 #ifdef HAVE_WINDOW_SYSTEM
6193 if (windows_or_buffers_changed || update_mode_lines)
6194 {
6195 Lisp_Object tail, frame;
6196
6197 FOR_EACH_FRAME (tail, frame)
6198 {
6199 f = XFRAME (frame);
6200 if (f != tooltip_frame
6201 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6202 x_consider_frame_title (frame);
6203 }
6204 }
6205 #endif /* HAVE_WINDOW_SYSTEM */
6206
6207 /* Update the menu bar item lists, if appropriate. This has to be
6208 done before any actual redisplay or generation of display lines. */
6209 all_windows = (update_mode_lines
6210 || buffer_shared > 1
6211 || windows_or_buffers_changed);
6212 if (all_windows)
6213 {
6214 Lisp_Object tail, frame;
6215 int count = specpdl_ptr - specpdl;
6216
6217 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6218
6219 FOR_EACH_FRAME (tail, frame)
6220 {
6221 f = XFRAME (frame);
6222
6223 /* Ignore tooltip frame. */
6224 if (f == tooltip_frame)
6225 continue;
6226
6227 /* If a window on this frame changed size, report that to
6228 the user and clear the size-change flag. */
6229 if (FRAME_WINDOW_SIZES_CHANGED (f))
6230 {
6231 Lisp_Object functions;
6232
6233 /* Clear flag first in case we get an error below. */
6234 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6235 functions = Vwindow_size_change_functions;
6236 GCPRO2 (tail, functions);
6237
6238 while (CONSP (functions))
6239 {
6240 call1 (XCAR (functions), frame);
6241 functions = XCDR (functions);
6242 }
6243 UNGCPRO;
6244 }
6245
6246 GCPRO1 (tail);
6247 update_menu_bar (f, 0);
6248 #ifdef HAVE_WINDOW_SYSTEM
6249 update_tool_bar (f, 0);
6250 #endif
6251 UNGCPRO;
6252 }
6253
6254 unbind_to (count, Qnil);
6255 }
6256 else
6257 {
6258 struct frame *sf = SELECTED_FRAME ();
6259 update_menu_bar (sf, 1);
6260 #ifdef HAVE_WINDOW_SYSTEM
6261 update_tool_bar (sf, 1);
6262 #endif
6263 }
6264
6265 /* Motif needs this. See comment in xmenu.c. Turn it off when
6266 pending_menu_activation is not defined. */
6267 #ifdef USE_X_TOOLKIT
6268 pending_menu_activation = 0;
6269 #endif
6270 }
6271
6272
6273 /* Update the menu bar item list for frame F. This has to be done
6274 before we start to fill in any display lines, because it can call
6275 eval.
6276
6277 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6278
6279 static void
6280 update_menu_bar (f, save_match_data)
6281 struct frame *f;
6282 int save_match_data;
6283 {
6284 Lisp_Object window;
6285 register struct window *w;
6286
6287 window = FRAME_SELECTED_WINDOW (f);
6288 w = XWINDOW (window);
6289
6290 if (update_mode_lines)
6291 w->update_mode_line = Qt;
6292
6293 if (FRAME_WINDOW_P (f)
6294 ?
6295 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6296 FRAME_EXTERNAL_MENU_BAR (f)
6297 #else
6298 FRAME_MENU_BAR_LINES (f) > 0
6299 #endif
6300 : FRAME_MENU_BAR_LINES (f) > 0)
6301 {
6302 /* If the user has switched buffers or windows, we need to
6303 recompute to reflect the new bindings. But we'll
6304 recompute when update_mode_lines is set too; that means
6305 that people can use force-mode-line-update to request
6306 that the menu bar be recomputed. The adverse effect on
6307 the rest of the redisplay algorithm is about the same as
6308 windows_or_buffers_changed anyway. */
6309 if (windows_or_buffers_changed
6310 || !NILP (w->update_mode_line)
6311 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6312 < BUF_MODIFF (XBUFFER (w->buffer)))
6313 != !NILP (w->last_had_star))
6314 || ((!NILP (Vtransient_mark_mode)
6315 && !NILP (XBUFFER (w->buffer)->mark_active))
6316 != !NILP (w->region_showing)))
6317 {
6318 struct buffer *prev = current_buffer;
6319 int count = specpdl_ptr - specpdl;
6320
6321 set_buffer_internal_1 (XBUFFER (w->buffer));
6322 if (save_match_data)
6323 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6324 if (NILP (Voverriding_local_map_menu_flag))
6325 {
6326 specbind (Qoverriding_terminal_local_map, Qnil);
6327 specbind (Qoverriding_local_map, Qnil);
6328 }
6329
6330 /* Run the Lucid hook. */
6331 call1 (Vrun_hooks, Qactivate_menubar_hook);
6332
6333 /* If it has changed current-menubar from previous value,
6334 really recompute the menu-bar from the value. */
6335 if (! NILP (Vlucid_menu_bar_dirty_flag))
6336 call0 (Qrecompute_lucid_menubar);
6337
6338 safe_run_hooks (Qmenu_bar_update_hook);
6339 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6340
6341 /* Redisplay the menu bar in case we changed it. */
6342 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6343 if (FRAME_WINDOW_P (f))
6344 set_frame_menubar (f, 0, 0);
6345 else
6346 /* On a terminal screen, the menu bar is an ordinary screen
6347 line, and this makes it get updated. */
6348 w->update_mode_line = Qt;
6349 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6350 /* In the non-toolkit version, the menu bar is an ordinary screen
6351 line, and this makes it get updated. */
6352 w->update_mode_line = Qt;
6353 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6354
6355 unbind_to (count, Qnil);
6356 set_buffer_internal_1 (prev);
6357 }
6358 }
6359 }
6360
6361
6362 \f
6363 /***********************************************************************
6364 Tool-bars
6365 ***********************************************************************/
6366
6367 #ifdef HAVE_WINDOW_SYSTEM
6368
6369 /* Update the tool-bar item list for frame F. This has to be done
6370 before we start to fill in any display lines. Called from
6371 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6372 and restore it here. */
6373
6374 static void
6375 update_tool_bar (f, save_match_data)
6376 struct frame *f;
6377 int save_match_data;
6378 {
6379 if (WINDOWP (f->tool_bar_window)
6380 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
6381 {
6382 Lisp_Object window;
6383 struct window *w;
6384
6385 window = FRAME_SELECTED_WINDOW (f);
6386 w = XWINDOW (window);
6387
6388 /* If the user has switched buffers or windows, we need to
6389 recompute to reflect the new bindings. But we'll
6390 recompute when update_mode_lines is set too; that means
6391 that people can use force-mode-line-update to request
6392 that the menu bar be recomputed. The adverse effect on
6393 the rest of the redisplay algorithm is about the same as
6394 windows_or_buffers_changed anyway. */
6395 if (windows_or_buffers_changed
6396 || !NILP (w->update_mode_line)
6397 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6398 < BUF_MODIFF (XBUFFER (w->buffer)))
6399 != !NILP (w->last_had_star))
6400 || ((!NILP (Vtransient_mark_mode)
6401 && !NILP (XBUFFER (w->buffer)->mark_active))
6402 != !NILP (w->region_showing)))
6403 {
6404 struct buffer *prev = current_buffer;
6405 int count = specpdl_ptr - specpdl;
6406
6407 /* Set current_buffer to the buffer of the selected
6408 window of the frame, so that we get the right local
6409 keymaps. */
6410 set_buffer_internal_1 (XBUFFER (w->buffer));
6411
6412 /* Save match data, if we must. */
6413 if (save_match_data)
6414 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6415
6416 /* Make sure that we don't accidentally use bogus keymaps. */
6417 if (NILP (Voverriding_local_map_menu_flag))
6418 {
6419 specbind (Qoverriding_terminal_local_map, Qnil);
6420 specbind (Qoverriding_local_map, Qnil);
6421 }
6422
6423 /* Build desired tool-bar items from keymaps. */
6424 f->desired_tool_bar_items
6425 = tool_bar_items (f->desired_tool_bar_items,
6426 &f->n_desired_tool_bar_items);
6427
6428 /* Redisplay the tool-bar in case we changed it. */
6429 w->update_mode_line = Qt;
6430
6431 unbind_to (count, Qnil);
6432 set_buffer_internal_1 (prev);
6433 }
6434 }
6435 }
6436
6437
6438 /* Set F->desired_tool_bar_string to a Lisp string representing frame
6439 F's desired tool-bar contents. F->desired_tool_bar_items must have
6440 been set up previously by calling prepare_menu_bars. */
6441
6442 static void
6443 build_desired_tool_bar_string (f)
6444 struct frame *f;
6445 {
6446 int i, size, size_needed, string_idx;
6447 struct gcpro gcpro1, gcpro2, gcpro3;
6448 Lisp_Object image, plist, props;
6449
6450 image = plist = props = Qnil;
6451 GCPRO3 (image, plist, props);
6452
6453 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
6454 Otherwise, make a new string. */
6455
6456 /* The size of the string we might be able to reuse. */
6457 size = (STRINGP (f->desired_tool_bar_string)
6458 ? XSTRING (f->desired_tool_bar_string)->size
6459 : 0);
6460
6461 /* Each image in the string we build is preceded by a space,
6462 and there is a space at the end. */
6463 size_needed = f->n_desired_tool_bar_items + 1;
6464
6465 /* Reuse f->desired_tool_bar_string, if possible. */
6466 if (size < size_needed)
6467 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
6468 make_number (' '));
6469 else
6470 {
6471 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
6472 Fremove_text_properties (make_number (0), make_number (size),
6473 props, f->desired_tool_bar_string);
6474 }
6475
6476 /* Put a `display' property on the string for the images to display,
6477 put a `menu_item' property on tool-bar items with a value that
6478 is the index of the item in F's tool-bar item vector. */
6479 for (i = 0, string_idx = 0;
6480 i < f->n_desired_tool_bar_items;
6481 ++i, string_idx += 1)
6482 {
6483 #define PROP(IDX) \
6484 (XVECTOR (f->desired_tool_bar_items) \
6485 ->contents[i * TOOL_BAR_ITEM_NSLOTS + (IDX)])
6486
6487 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
6488 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
6489 int margin, relief;
6490 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
6491 extern Lisp_Object Qlaplace;
6492
6493 /* If image is a vector, choose the image according to the
6494 button state. */
6495 image = PROP (TOOL_BAR_ITEM_IMAGES);
6496 if (VECTORP (image))
6497 {
6498 enum tool_bar_item_image idx;
6499
6500 if (enabled_p)
6501 idx = (selected_p
6502 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
6503 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
6504 else
6505 idx = (selected_p
6506 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
6507 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
6508
6509 xassert (XVECTOR (image)->size >= idx);
6510 image = XVECTOR (image)->contents[idx];
6511 }
6512
6513 /* Ignore invalid image specifications. */
6514 if (!valid_image_p (image))
6515 continue;
6516
6517 /* Display the tool-bar button pressed, or depressed. */
6518 plist = Fcopy_sequence (XCDR (image));
6519
6520 /* Compute margin and relief to draw. */
6521 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
6522 margin = relief + max (0, tool_bar_button_margin);
6523
6524 if (auto_raise_tool_bar_buttons_p)
6525 {
6526 /* Add a `:relief' property to the image spec if the item is
6527 selected. */
6528 if (selected_p)
6529 {
6530 plist = Fplist_put (plist, QCrelief, make_number (-relief));
6531 margin -= relief;
6532 }
6533 }
6534 else
6535 {
6536 /* If image is selected, display it pressed, i.e. with a
6537 negative relief. If it's not selected, display it with a
6538 raised relief. */
6539 plist = Fplist_put (plist, QCrelief,
6540 (selected_p
6541 ? make_number (-relief)
6542 : make_number (relief)));
6543 margin -= relief;
6544 }
6545
6546 /* Put a margin around the image. */
6547 if (margin)
6548 plist = Fplist_put (plist, QCmargin, make_number (margin));
6549
6550 /* If button is not enabled, make the image appear disabled by
6551 applying an appropriate algorithm to it. */
6552 if (!enabled_p)
6553 plist = Fplist_put (plist, QCalgorithm, Qlaplace);
6554
6555 /* Put a `display' text property on the string for the image to
6556 display. Put a `menu-item' property on the string that gives
6557 the start of this item's properties in the tool-bar items
6558 vector. */
6559 image = Fcons (Qimage, plist);
6560 props = list4 (Qdisplay, image,
6561 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
6562 Fadd_text_properties (make_number (string_idx),
6563 make_number (string_idx + 1),
6564 props, f->desired_tool_bar_string);
6565 #undef PROP
6566 }
6567
6568 UNGCPRO;
6569 }
6570
6571
6572 /* Display one line of the tool-bar of frame IT->f. */
6573
6574 static void
6575 display_tool_bar_line (it)
6576 struct it *it;
6577 {
6578 struct glyph_row *row = it->glyph_row;
6579 int max_x = it->last_visible_x;
6580 struct glyph *last;
6581
6582 prepare_desired_row (row);
6583 row->y = it->current_y;
6584
6585 while (it->current_x < max_x)
6586 {
6587 int x_before, x, n_glyphs_before, i, nglyphs;
6588
6589 /* Get the next display element. */
6590 if (!get_next_display_element (it))
6591 break;
6592
6593 /* Produce glyphs. */
6594 x_before = it->current_x;
6595 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
6596 PRODUCE_GLYPHS (it);
6597
6598 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
6599 i = 0;
6600 x = x_before;
6601 while (i < nglyphs)
6602 {
6603 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
6604
6605 if (x + glyph->pixel_width > max_x)
6606 {
6607 /* Glyph doesn't fit on line. */
6608 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
6609 it->current_x = x;
6610 goto out;
6611 }
6612
6613 ++it->hpos;
6614 x += glyph->pixel_width;
6615 ++i;
6616 }
6617
6618 /* Stop at line ends. */
6619 if (ITERATOR_AT_END_OF_LINE_P (it))
6620 break;
6621
6622 set_iterator_to_next (it);
6623 }
6624
6625 out:;
6626
6627 row->displays_text_p = row->used[TEXT_AREA] != 0;
6628 extend_face_to_end_of_line (it);
6629 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
6630 last->right_box_line_p = 1;
6631 compute_line_metrics (it);
6632
6633 /* If line is empty, make it occupy the rest of the tool-bar. */
6634 if (!row->displays_text_p)
6635 {
6636 row->height = row->phys_height = it->last_visible_y - row->y;
6637 row->ascent = row->phys_ascent = 0;
6638 }
6639
6640 row->full_width_p = 1;
6641 row->continued_p = 0;
6642 row->truncated_on_left_p = 0;
6643 row->truncated_on_right_p = 0;
6644
6645 it->current_x = it->hpos = 0;
6646 it->current_y += row->height;
6647 ++it->vpos;
6648 ++it->glyph_row;
6649 }
6650
6651
6652 /* Value is the number of screen lines needed to make all tool-bar
6653 items of frame F visible. */
6654
6655 static int
6656 tool_bar_lines_needed (f)
6657 struct frame *f;
6658 {
6659 struct window *w = XWINDOW (f->tool_bar_window);
6660 struct it it;
6661
6662 /* Initialize an iterator for iteration over
6663 F->desired_tool_bar_string in the tool-bar window of frame F. */
6664 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
6665 it.first_visible_x = 0;
6666 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6667 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
6668
6669 while (!ITERATOR_AT_END_P (&it))
6670 {
6671 it.glyph_row = w->desired_matrix->rows;
6672 clear_glyph_row (it.glyph_row);
6673 display_tool_bar_line (&it);
6674 }
6675
6676 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
6677 }
6678
6679
6680 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
6681 height should be changed. */
6682
6683 static int
6684 redisplay_tool_bar (f)
6685 struct frame *f;
6686 {
6687 struct window *w;
6688 struct it it;
6689 struct glyph_row *row;
6690 int change_height_p = 0;
6691
6692 /* If frame hasn't a tool-bar window or if it is zero-height, don't
6693 do anything. This means you must start with tool-bar-lines
6694 non-zero to get the auto-sizing effect. Or in other words, you
6695 can turn off tool-bars by specifying tool-bar-lines zero. */
6696 if (!WINDOWP (f->tool_bar_window)
6697 || (w = XWINDOW (f->tool_bar_window),
6698 XFASTINT (w->height) == 0))
6699 return 0;
6700
6701 /* Set up an iterator for the tool-bar window. */
6702 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
6703 it.first_visible_x = 0;
6704 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6705 row = it.glyph_row;
6706
6707 /* Build a string that represents the contents of the tool-bar. */
6708 build_desired_tool_bar_string (f);
6709 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
6710
6711 /* Display as many lines as needed to display all tool-bar items. */
6712 while (it.current_y < it.last_visible_y)
6713 display_tool_bar_line (&it);
6714
6715 /* It doesn't make much sense to try scrolling in the tool-bar
6716 window, so don't do it. */
6717 w->desired_matrix->no_scrolling_p = 1;
6718 w->must_be_updated_p = 1;
6719
6720 if (auto_resize_tool_bars_p)
6721 {
6722 int nlines;
6723
6724 /* If there are blank lines at the end, except for a partially
6725 visible blank line at the end that is smaller than
6726 CANON_Y_UNIT, change the tool-bar's height. */
6727 row = it.glyph_row - 1;
6728 if (!row->displays_text_p
6729 && row->height >= CANON_Y_UNIT (f))
6730 change_height_p = 1;
6731
6732 /* If row displays tool-bar items, but is partially visible,
6733 change the tool-bar's height. */
6734 if (row->displays_text_p
6735 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
6736 change_height_p = 1;
6737
6738 /* Resize windows as needed by changing the `tool-bar-lines'
6739 frame parameter. */
6740 if (change_height_p
6741 && (nlines = tool_bar_lines_needed (f),
6742 nlines != XFASTINT (w->height)))
6743 {
6744 extern Lisp_Object Qtool_bar_lines;
6745 Lisp_Object frame;
6746
6747 XSETFRAME (frame, f);
6748 clear_glyph_matrix (w->desired_matrix);
6749 Fmodify_frame_parameters (frame,
6750 Fcons (Fcons (Qtool_bar_lines,
6751 make_number (nlines)),
6752 Qnil));
6753 fonts_changed_p = 1;
6754 }
6755 }
6756
6757 return change_height_p;
6758 }
6759
6760
6761 /* Get information about the tool-bar item which is displayed in GLYPH
6762 on frame F. Return in *PROP_IDX the index where tool-bar item
6763 properties start in F->current_tool_bar_items. Value is zero if
6764 GLYPH doesn't display a tool-bar item. */
6765
6766 int
6767 tool_bar_item_info (f, glyph, prop_idx)
6768 struct frame *f;
6769 struct glyph *glyph;
6770 int *prop_idx;
6771 {
6772 Lisp_Object prop;
6773 int success_p;
6774
6775 /* Get the text property `menu-item' at pos. The value of that
6776 property is the start index of this item's properties in
6777 F->current_tool_bar_items. */
6778 prop = Fget_text_property (make_number (glyph->charpos),
6779 Qmenu_item, f->current_tool_bar_string);
6780 if (INTEGERP (prop))
6781 {
6782 *prop_idx = XINT (prop);
6783 success_p = 1;
6784 }
6785 else
6786 success_p = 0;
6787
6788 return success_p;
6789 }
6790
6791 #endif /* HAVE_WINDOW_SYSTEM */
6792
6793
6794 \f
6795 /************************************************************************
6796 Horizontal scrolling
6797 ************************************************************************/
6798
6799 static int hscroll_window_tree P_ ((Lisp_Object));
6800 static int hscroll_windows P_ ((Lisp_Object));
6801
6802 /* For all leaf windows in the window tree rooted at WINDOW, set their
6803 hscroll value so that PT is (i) visible in the window, and (ii) so
6804 that it is not within a certain margin at the window's left and
6805 right border. Value is non-zero if any window's hscroll has been
6806 changed. */
6807
6808 static int
6809 hscroll_window_tree (window)
6810 Lisp_Object window;
6811 {
6812 int hscrolled_p = 0;
6813
6814 while (WINDOWP (window))
6815 {
6816 struct window *w = XWINDOW (window);
6817
6818 if (WINDOWP (w->hchild))
6819 hscrolled_p |= hscroll_window_tree (w->hchild);
6820 else if (WINDOWP (w->vchild))
6821 hscrolled_p |= hscroll_window_tree (w->vchild);
6822 else if (w->cursor.vpos >= 0)
6823 {
6824 int hscroll_margin, text_area_x, text_area_y;
6825 int text_area_width, text_area_height;
6826 struct glyph_row *current_cursor_row
6827 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
6828 struct glyph_row *desired_cursor_row
6829 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
6830 struct glyph_row *cursor_row
6831 = (desired_cursor_row->enabled_p
6832 ? desired_cursor_row
6833 : current_cursor_row);
6834
6835 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
6836 &text_area_width, &text_area_height);
6837
6838 /* Scroll when cursor is inside this scroll margin. */
6839 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
6840
6841 if ((XFASTINT (w->hscroll)
6842 && w->cursor.x < hscroll_margin)
6843 || (cursor_row->enabled_p
6844 && cursor_row->truncated_on_right_p
6845 && (w->cursor.x > text_area_width - hscroll_margin)))
6846 {
6847 struct it it;
6848 int hscroll;
6849 struct buffer *saved_current_buffer;
6850 int pt;
6851
6852 /* Find point in a display of infinite width. */
6853 saved_current_buffer = current_buffer;
6854 current_buffer = XBUFFER (w->buffer);
6855
6856 if (w == XWINDOW (selected_window))
6857 pt = BUF_PT (current_buffer);
6858 else
6859 {
6860 pt = marker_position (w->pointm);
6861 pt = max (BEGV, pt);
6862 pt = min (ZV, pt);
6863 }
6864
6865 /* Move iterator to pt starting at cursor_row->start in
6866 a line with infinite width. */
6867 init_to_row_start (&it, w, cursor_row);
6868 it.last_visible_x = INFINITY;
6869 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
6870 current_buffer = saved_current_buffer;
6871
6872 /* Center cursor in window. */
6873 hscroll = (max (0, it.current_x - text_area_width / 2)
6874 / CANON_X_UNIT (it.f));
6875
6876 /* Don't call Fset_window_hscroll if value hasn't
6877 changed because it will prevent redisplay
6878 optimizations. */
6879 if (XFASTINT (w->hscroll) != hscroll)
6880 {
6881 Fset_window_hscroll (window, make_number (hscroll));
6882 hscrolled_p = 1;
6883 }
6884 }
6885 }
6886
6887 window = w->next;
6888 }
6889
6890 /* Value is non-zero if hscroll of any leaf window has been changed. */
6891 return hscrolled_p;
6892 }
6893
6894
6895 /* Set hscroll so that cursor is visible and not inside horizontal
6896 scroll margins for all windows in the tree rooted at WINDOW. See
6897 also hscroll_window_tree above. Value is non-zero if any window's
6898 hscroll has been changed. If it has, desired matrices on the frame
6899 of WINDOW are cleared. */
6900
6901 static int
6902 hscroll_windows (window)
6903 Lisp_Object window;
6904 {
6905 int hscrolled_p = hscroll_window_tree (window);
6906 if (hscrolled_p)
6907 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
6908 return hscrolled_p;
6909 }
6910
6911
6912 \f
6913 /************************************************************************
6914 Redisplay
6915 ************************************************************************/
6916
6917 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
6918 to a non-zero value. This is sometimes handy to have in a debugger
6919 session. */
6920
6921 #if GLYPH_DEBUG
6922
6923 /* First and last unchanged row for try_window_id. */
6924
6925 int debug_first_unchanged_at_end_vpos;
6926 int debug_last_unchanged_at_beg_vpos;
6927
6928 /* Delta vpos and y. */
6929
6930 int debug_dvpos, debug_dy;
6931
6932 /* Delta in characters and bytes for try_window_id. */
6933
6934 int debug_delta, debug_delta_bytes;
6935
6936 /* Values of window_end_pos and window_end_vpos at the end of
6937 try_window_id. */
6938
6939 int debug_end_pos, debug_end_vpos;
6940
6941 /* Append a string to W->desired_matrix->method. FMT is a printf
6942 format string. A1...A9 are a supplement for a variable-length
6943 argument list. If trace_redisplay_p is non-zero also printf the
6944 resulting string to stderr. */
6945
6946 static void
6947 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
6948 struct window *w;
6949 char *fmt;
6950 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
6951 {
6952 char buffer[512];
6953 char *method = w->desired_matrix->method;
6954 int len = strlen (method);
6955 int size = sizeof w->desired_matrix->method;
6956 int remaining = size - len - 1;
6957
6958 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
6959 if (len && remaining)
6960 {
6961 method[len] = '|';
6962 --remaining, ++len;
6963 }
6964
6965 strncpy (method + len, buffer, remaining);
6966
6967 if (trace_redisplay_p)
6968 fprintf (stderr, "%p (%s): %s\n",
6969 w,
6970 ((BUFFERP (w->buffer)
6971 && STRINGP (XBUFFER (w->buffer)->name))
6972 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
6973 : "no buffer"),
6974 buffer);
6975 }
6976
6977 #endif /* GLYPH_DEBUG */
6978
6979
6980 /* This counter is used to clear the face cache every once in a while
6981 in redisplay_internal. It is incremented for each redisplay.
6982 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
6983 cleared. */
6984
6985 #define CLEAR_FACE_CACHE_COUNT 10000
6986 static int clear_face_cache_count;
6987
6988 /* Record the previous terminal frame we displayed. */
6989
6990 static struct frame *previous_terminal_frame;
6991
6992 /* Non-zero while redisplay_internal is in progress. */
6993
6994 int redisplaying_p;
6995
6996
6997 /* Value is non-zero if all changes in window W, which displays
6998 current_buffer, are in the text between START and END. START is a
6999 buffer position, END is given as a distance from Z. Used in
7000 redisplay_internal for display optimization. */
7001
7002 static INLINE int
7003 text_outside_line_unchanged_p (w, start, end)
7004 struct window *w;
7005 int start, end;
7006 {
7007 int unchanged_p = 1;
7008
7009 /* If text or overlays have changed, see where. */
7010 if (XFASTINT (w->last_modified) < MODIFF
7011 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7012 {
7013 /* Gap in the line? */
7014 if (GPT < start || Z - GPT < end)
7015 unchanged_p = 0;
7016
7017 /* Changes start in front of the line, or end after it? */
7018 if (unchanged_p
7019 && (BEG_UNCHANGED < start - 1
7020 || END_UNCHANGED < end))
7021 unchanged_p = 0;
7022
7023 /* If selective display, can't optimize if changes start at the
7024 beginning of the line. */
7025 if (unchanged_p
7026 && INTEGERP (current_buffer->selective_display)
7027 && XINT (current_buffer->selective_display) > 0
7028 && (BEG_UNCHANGED < start || GPT <= start))
7029 unchanged_p = 0;
7030 }
7031
7032 return unchanged_p;
7033 }
7034
7035
7036 /* Do a frame update, taking possible shortcuts into account. This is
7037 the main external entry point for redisplay.
7038
7039 If the last redisplay displayed an echo area message and that message
7040 is no longer requested, we clear the echo area or bring back the
7041 mini-buffer if that is in use. */
7042
7043 void
7044 redisplay ()
7045 {
7046 redisplay_internal (0);
7047 }
7048
7049 /* Return 1 if point moved out of or into a composition. Otherwise
7050 return 0. PREV_BUF and PREV_PT are the last point buffer and
7051 position. BUF and PT are the current point buffer and position. */
7052
7053 int
7054 check_point_in_composition (prev_buf, prev_pt, buf, pt)
7055 struct buffer *prev_buf, *buf;
7056 int prev_pt, pt;
7057 {
7058 int start, end;
7059 Lisp_Object prop;
7060 Lisp_Object buffer;
7061
7062 XSETBUFFER (buffer, buf);
7063 /* Check a composition at the last point if point moved within the
7064 same buffer. */
7065 if (prev_buf == buf)
7066 {
7067 if (prev_pt == pt)
7068 /* Point didn't move. */
7069 return 0;
7070
7071 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7072 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7073 && COMPOSITION_VALID_P (start, end, prop)
7074 && start < prev_pt && end > prev_pt)
7075 /* The last point was within the composition. Return 1 iff
7076 point moved out of the composition. */
7077 return (pt <= start || pt >= end);
7078 }
7079
7080 /* Check a composition at the current point. */
7081 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7082 && find_composition (pt, -1, &start, &end, &prop, buffer)
7083 && COMPOSITION_VALID_P (start, end, prop)
7084 && start < pt && end > pt);
7085 }
7086
7087 /* Reconsider the setting of B->clip_changed which is displayed
7088 in window W. */
7089
7090 static INLINE void
7091 reconsider_clip_changes (w, b)
7092 struct window *w;
7093 struct buffer *b;
7094 {
7095 if (b->prevent_redisplay_optimizations_p)
7096 b->clip_changed = 1;
7097 else if (b->clip_changed
7098 && !NILP (w->window_end_valid)
7099 && w->current_matrix->buffer == b
7100 && w->current_matrix->zv == BUF_ZV (b)
7101 && w->current_matrix->begv == BUF_BEGV (b))
7102 b->clip_changed = 0;
7103
7104 /* If display wasn't paused, and W is not a tool bar window, see if
7105 point has been moved into or out of a composition. In that case,
7106 we set b->clip_changed to 1 to force updating the screen. If
7107 b->clip_changed has already been set to 1, we can skip this
7108 check. */
7109 if (!b->clip_changed
7110 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7111 {
7112 int pt;
7113
7114 if (w == XWINDOW (selected_window))
7115 pt = BUF_PT (current_buffer);
7116 else
7117 pt = marker_position (w->pointm);
7118
7119 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
7120 || pt != XINT (w->last_point))
7121 && check_point_in_composition (w->current_matrix->buffer,
7122 XINT (w->last_point),
7123 XBUFFER (w->buffer), pt))
7124 b->clip_changed = 1;
7125 }
7126 }
7127
7128
7129 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7130 response to any user action; therefore, we should preserve the echo
7131 area. (Actually, our caller does that job.) Perhaps in the future
7132 avoid recentering windows if it is not necessary; currently that
7133 causes some problems. */
7134
7135 static void
7136 redisplay_internal (preserve_echo_area)
7137 int preserve_echo_area;
7138 {
7139 struct window *w = XWINDOW (selected_window);
7140 struct frame *f = XFRAME (w->frame);
7141 int pause;
7142 int must_finish = 0;
7143 struct text_pos tlbufpos, tlendpos;
7144 int number_of_visible_frames;
7145 int count;
7146 struct frame *sf = SELECTED_FRAME ();
7147
7148 /* Non-zero means redisplay has to consider all windows on all
7149 frames. Zero means, only selected_window is considered. */
7150 int consider_all_windows_p;
7151
7152 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7153
7154 /* No redisplay if running in batch mode or frame is not yet fully
7155 initialized, or redisplay is explicitly turned off by setting
7156 Vinhibit_redisplay. */
7157 if (noninteractive
7158 || !NILP (Vinhibit_redisplay)
7159 || !f->glyphs_initialized_p)
7160 return;
7161
7162 /* The flag redisplay_performed_directly_p is set by
7163 direct_output_for_insert when it already did the whole screen
7164 update necessary. */
7165 if (redisplay_performed_directly_p)
7166 {
7167 redisplay_performed_directly_p = 0;
7168 if (!hscroll_windows (selected_window))
7169 return;
7170 }
7171
7172 #ifdef USE_X_TOOLKIT
7173 if (popup_activated ())
7174 return;
7175 #endif
7176
7177 /* I don't think this happens but let's be paranoid. */
7178 if (redisplaying_p)
7179 return;
7180
7181 /* Record a function that resets redisplaying_p to its old value
7182 when we leave this function. */
7183 count = specpdl_ptr - specpdl;
7184 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7185 ++redisplaying_p;
7186
7187 retry:
7188
7189 reconsider_clip_changes (w, current_buffer);
7190
7191 /* If new fonts have been loaded that make a glyph matrix adjustment
7192 necessary, do it. */
7193 if (fonts_changed_p)
7194 {
7195 adjust_glyphs (NULL);
7196 ++windows_or_buffers_changed;
7197 fonts_changed_p = 0;
7198 }
7199
7200 if (! FRAME_WINDOW_P (sf)
7201 && previous_terminal_frame != sf)
7202 {
7203 /* Since frames on an ASCII terminal share the same display
7204 area, displaying a different frame means redisplay the whole
7205 thing. */
7206 windows_or_buffers_changed++;
7207 SET_FRAME_GARBAGED (sf);
7208 XSETFRAME (Vterminal_frame, sf);
7209 }
7210 previous_terminal_frame = sf;
7211
7212 /* Set the visible flags for all frames. Do this before checking
7213 for resized or garbaged frames; they want to know if their frames
7214 are visible. See the comment in frame.h for
7215 FRAME_SAMPLE_VISIBILITY. */
7216 {
7217 Lisp_Object tail, frame;
7218
7219 number_of_visible_frames = 0;
7220
7221 FOR_EACH_FRAME (tail, frame)
7222 {
7223 struct frame *f = XFRAME (frame);
7224
7225 FRAME_SAMPLE_VISIBILITY (f);
7226 if (FRAME_VISIBLE_P (f))
7227 ++number_of_visible_frames;
7228 clear_desired_matrices (f);
7229 }
7230 }
7231
7232 /* Notice any pending interrupt request to change frame size. */
7233 do_pending_window_change (1);
7234
7235 /* Clear frames marked as garbaged. */
7236 if (frame_garbaged)
7237 clear_garbaged_frames ();
7238
7239 /* Build menubar and tool-bar items. */
7240 prepare_menu_bars ();
7241
7242 if (windows_or_buffers_changed)
7243 update_mode_lines++;
7244
7245 /* Detect case that we need to write or remove a star in the mode line. */
7246 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
7247 {
7248 w->update_mode_line = Qt;
7249 if (buffer_shared > 1)
7250 update_mode_lines++;
7251 }
7252
7253 /* If %c is in the mode line, update it if needed. */
7254 if (!NILP (w->column_number_displayed)
7255 /* This alternative quickly identifies a common case
7256 where no change is needed. */
7257 && !(PT == XFASTINT (w->last_point)
7258 && XFASTINT (w->last_modified) >= MODIFF
7259 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
7260 && XFASTINT (w->column_number_displayed) != current_column ())
7261 w->update_mode_line = Qt;
7262
7263 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
7264
7265 /* The variable buffer_shared is set in redisplay_window and
7266 indicates that we redisplay a buffer in different windows. See
7267 there. */
7268 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
7269
7270 /* If specs for an arrow have changed, do thorough redisplay
7271 to ensure we remove any arrow that should no longer exist. */
7272 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
7273 || ! EQ (Voverlay_arrow_string, last_arrow_string))
7274 consider_all_windows_p = windows_or_buffers_changed = 1;
7275
7276 /* Normally the message* functions will have already displayed and
7277 updated the echo area, but the frame may have been trashed, or
7278 the update may have been preempted, so display the echo area
7279 again here. Checking both message buffers captures the case that
7280 the echo area should be cleared. */
7281 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
7282 {
7283 int window_height_changed_p = echo_area_display (0);
7284 must_finish = 1;
7285
7286 if (fonts_changed_p)
7287 goto retry;
7288 else if (window_height_changed_p)
7289 {
7290 consider_all_windows_p = 1;
7291 ++update_mode_lines;
7292 ++windows_or_buffers_changed;
7293
7294 /* If window configuration was changed, frames may have been
7295 marked garbaged. Clear them or we will experience
7296 surprises wrt scrolling. */
7297 if (frame_garbaged)
7298 clear_garbaged_frames ();
7299 }
7300 }
7301 else if (w == XWINDOW (minibuf_window)
7302 && (current_buffer->clip_changed
7303 || XFASTINT (w->last_modified) < MODIFF
7304 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7305 && resize_mini_window (w, 0))
7306 {
7307 /* Resized active mini-window to fit the size of what it is
7308 showing if its contents might have changed. */
7309 must_finish = 1;
7310 consider_all_windows_p = 1;
7311 ++windows_or_buffers_changed;
7312 ++update_mode_lines;
7313
7314 /* If window configuration was changed, frames may have been
7315 marked garbaged. Clear them or we will experience
7316 surprises wrt scrolling. */
7317 if (frame_garbaged)
7318 clear_garbaged_frames ();
7319 }
7320
7321
7322 /* If showing the region, and mark has changed, we must redisplay
7323 the whole window. The assignment to this_line_start_pos prevents
7324 the optimization directly below this if-statement. */
7325 if (((!NILP (Vtransient_mark_mode)
7326 && !NILP (XBUFFER (w->buffer)->mark_active))
7327 != !NILP (w->region_showing))
7328 || (!NILP (w->region_showing)
7329 && !EQ (w->region_showing,
7330 Fmarker_position (XBUFFER (w->buffer)->mark))))
7331 CHARPOS (this_line_start_pos) = 0;
7332
7333 /* Optimize the case that only the line containing the cursor in the
7334 selected window has changed. Variables starting with this_ are
7335 set in display_line and record information about the line
7336 containing the cursor. */
7337 tlbufpos = this_line_start_pos;
7338 tlendpos = this_line_end_pos;
7339 if (!consider_all_windows_p
7340 && CHARPOS (tlbufpos) > 0
7341 && NILP (w->update_mode_line)
7342 && !current_buffer->clip_changed
7343 && FRAME_VISIBLE_P (XFRAME (w->frame))
7344 && !FRAME_OBSCURED_P (XFRAME (w->frame))
7345 /* Make sure recorded data applies to current buffer, etc. */
7346 && this_line_buffer == current_buffer
7347 && current_buffer == XBUFFER (w->buffer)
7348 && NILP (w->force_start)
7349 /* Point must be on the line that we have info recorded about. */
7350 && PT >= CHARPOS (tlbufpos)
7351 && PT <= Z - CHARPOS (tlendpos)
7352 /* All text outside that line, including its final newline,
7353 must be unchanged */
7354 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7355 CHARPOS (tlendpos)))
7356 {
7357 if (CHARPOS (tlbufpos) > BEGV
7358 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7359 && (CHARPOS (tlbufpos) == ZV
7360 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
7361 /* Former continuation line has disappeared by becoming empty */
7362 goto cancel;
7363 else if (XFASTINT (w->last_modified) < MODIFF
7364 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
7365 || MINI_WINDOW_P (w))
7366 {
7367 /* We have to handle the case of continuation around a
7368 wide-column character (See the comment in indent.c around
7369 line 885).
7370
7371 For instance, in the following case:
7372
7373 -------- Insert --------
7374 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7375 J_I_ ==> J_I_ `^^' are cursors.
7376 ^^ ^^
7377 -------- --------
7378
7379 As we have to redraw the line above, we should goto cancel. */
7380
7381 struct it it;
7382 int line_height_before = this_line_pixel_height;
7383
7384 /* Note that start_display will handle the case that the
7385 line starting at tlbufpos is a continuation lines. */
7386 start_display (&it, w, tlbufpos);
7387
7388 /* Implementation note: It this still necessary? */
7389 if (it.current_x != this_line_start_x)
7390 goto cancel;
7391
7392 TRACE ((stderr, "trying display optimization 1\n"));
7393 w->cursor.vpos = -1;
7394 overlay_arrow_seen = 0;
7395 it.vpos = this_line_vpos;
7396 it.current_y = this_line_y;
7397 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
7398 display_line (&it);
7399
7400 /* If line contains point, is not continued,
7401 and ends at same distance from eob as before, we win */
7402 if (w->cursor.vpos >= 0
7403 /* Line is not continued, otherwise this_line_start_pos
7404 would have been set to 0 in display_line. */
7405 && CHARPOS (this_line_start_pos)
7406 /* Line ends as before. */
7407 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
7408 /* Line has same height as before. Otherwise other lines
7409 would have to be shifted up or down. */
7410 && this_line_pixel_height == line_height_before)
7411 {
7412 /* If this is not the window's last line, we must adjust
7413 the charstarts of the lines below. */
7414 if (it.current_y < it.last_visible_y)
7415 {
7416 struct glyph_row *row
7417 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
7418 int delta, delta_bytes;
7419
7420 if (Z - CHARPOS (tlendpos) == ZV)
7421 {
7422 /* This line ends at end of (accessible part of)
7423 buffer. There is no newline to count. */
7424 delta = (Z
7425 - CHARPOS (tlendpos)
7426 - MATRIX_ROW_START_CHARPOS (row));
7427 delta_bytes = (Z_BYTE
7428 - BYTEPOS (tlendpos)
7429 - MATRIX_ROW_START_BYTEPOS (row));
7430 }
7431 else
7432 {
7433 /* This line ends in a newline. Must take
7434 account of the newline and the rest of the
7435 text that follows. */
7436 delta = (Z
7437 - CHARPOS (tlendpos)
7438 - MATRIX_ROW_START_CHARPOS (row));
7439 delta_bytes = (Z_BYTE
7440 - BYTEPOS (tlendpos)
7441 - MATRIX_ROW_START_BYTEPOS (row));
7442 }
7443
7444 increment_glyph_matrix_buffer_positions (w->current_matrix,
7445 this_line_vpos + 1,
7446 w->current_matrix->nrows,
7447 delta, delta_bytes);
7448 }
7449
7450 /* If this row displays text now but previously didn't,
7451 or vice versa, w->window_end_vpos may have to be
7452 adjusted. */
7453 if ((it.glyph_row - 1)->displays_text_p)
7454 {
7455 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
7456 XSETINT (w->window_end_vpos, this_line_vpos);
7457 }
7458 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
7459 && this_line_vpos > 0)
7460 XSETINT (w->window_end_vpos, this_line_vpos - 1);
7461 w->window_end_valid = Qnil;
7462
7463 /* Update hint: No need to try to scroll in update_window. */
7464 w->desired_matrix->no_scrolling_p = 1;
7465
7466 #if GLYPH_DEBUG
7467 *w->desired_matrix->method = 0;
7468 debug_method_add (w, "optimization 1");
7469 #endif
7470 goto update;
7471 }
7472 else
7473 goto cancel;
7474 }
7475 else if (/* Cursor position hasn't changed. */
7476 PT == XFASTINT (w->last_point)
7477 /* Make sure the cursor was last displayed
7478 in this window. Otherwise we have to reposition it. */
7479 && 0 <= w->cursor.vpos
7480 && XINT (w->height) > w->cursor.vpos)
7481 {
7482 if (!must_finish)
7483 {
7484 do_pending_window_change (1);
7485
7486 /* We used to always goto end_of_redisplay here, but this
7487 isn't enough if we have a blinking cursor. */
7488 if (w->cursor_off_p == w->last_cursor_off_p)
7489 goto end_of_redisplay;
7490 }
7491 goto update;
7492 }
7493 /* If highlighting the region, or if the cursor is in the echo area,
7494 then we can't just move the cursor. */
7495 else if (! (!NILP (Vtransient_mark_mode)
7496 && !NILP (current_buffer->mark_active))
7497 && (w == XWINDOW (current_buffer->last_selected_window)
7498 || highlight_nonselected_windows)
7499 && NILP (w->region_showing)
7500 && NILP (Vshow_trailing_whitespace)
7501 && !cursor_in_echo_area)
7502 {
7503 struct it it;
7504 struct glyph_row *row;
7505
7506 /* Skip from tlbufpos to PT and see where it is. Note that
7507 PT may be in invisible text. If so, we will end at the
7508 next visible position. */
7509 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
7510 NULL, DEFAULT_FACE_ID);
7511 it.current_x = this_line_start_x;
7512 it.current_y = this_line_y;
7513 it.vpos = this_line_vpos;
7514
7515 /* The call to move_it_to stops in front of PT, but
7516 moves over before-strings. */
7517 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
7518
7519 if (it.vpos == this_line_vpos
7520 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
7521 row->enabled_p))
7522 {
7523 xassert (this_line_vpos == it.vpos);
7524 xassert (this_line_y == it.current_y);
7525 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
7526 goto update;
7527 }
7528 else
7529 goto cancel;
7530 }
7531
7532 cancel:
7533 /* Text changed drastically or point moved off of line. */
7534 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
7535 }
7536
7537 CHARPOS (this_line_start_pos) = 0;
7538 consider_all_windows_p |= buffer_shared > 1;
7539 ++clear_face_cache_count;
7540
7541
7542 /* Build desired matrices. If consider_all_windows_p is non-zero,
7543 do it for all windows on all frames. Otherwise do it for
7544 selected_window, only. */
7545
7546 if (consider_all_windows_p)
7547 {
7548 Lisp_Object tail, frame;
7549
7550 /* Clear the face cache eventually. */
7551 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
7552 {
7553 clear_face_cache (0);
7554 clear_face_cache_count = 0;
7555 }
7556
7557 /* Recompute # windows showing selected buffer. This will be
7558 incremented each time such a window is displayed. */
7559 buffer_shared = 0;
7560
7561 FOR_EACH_FRAME (tail, frame)
7562 {
7563 struct frame *f = XFRAME (frame);
7564 if (FRAME_WINDOW_P (f) || f == sf)
7565 {
7566 /* Mark all the scroll bars to be removed; we'll redeem
7567 the ones we want when we redisplay their windows. */
7568 if (condemn_scroll_bars_hook)
7569 (*condemn_scroll_bars_hook) (f);
7570
7571 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7572 redisplay_windows (FRAME_ROOT_WINDOW (f));
7573
7574 /* Any scroll bars which redisplay_windows should have
7575 nuked should now go away. */
7576 if (judge_scroll_bars_hook)
7577 (*judge_scroll_bars_hook) (f);
7578 }
7579 }
7580 }
7581 else if (FRAME_VISIBLE_P (sf)
7582 && !FRAME_OBSCURED_P (sf))
7583 redisplay_window (selected_window, 1);
7584
7585
7586 /* Compare desired and current matrices, perform output. */
7587
7588 update:
7589
7590 /* If fonts changed, display again. */
7591 if (fonts_changed_p)
7592 goto retry;
7593
7594 /* Prevent various kinds of signals during display update.
7595 stdio is not robust about handling signals,
7596 which can cause an apparent I/O error. */
7597 if (interrupt_input)
7598 unrequest_sigio ();
7599 stop_polling ();
7600
7601 if (consider_all_windows_p)
7602 {
7603 Lisp_Object tail;
7604 struct frame *f;
7605 int hscrolled_p;
7606
7607 pause = 0;
7608 hscrolled_p = 0;
7609
7610 /* See if we have to hscroll. */
7611 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7612 if (FRAMEP (XCAR (tail)))
7613 {
7614 f = XFRAME (XCAR (tail));
7615
7616 if ((FRAME_WINDOW_P (f)
7617 || f == sf)
7618 && FRAME_VISIBLE_P (f)
7619 && !FRAME_OBSCURED_P (f)
7620 && hscroll_windows (f->root_window))
7621 hscrolled_p = 1;
7622 }
7623
7624 if (hscrolled_p)
7625 goto retry;
7626
7627 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7628 {
7629 if (!FRAMEP (XCAR (tail)))
7630 continue;
7631
7632 f = XFRAME (XCAR (tail));
7633
7634 if ((FRAME_WINDOW_P (f) || f == sf)
7635 && FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7636 {
7637 /* Mark all windows as to be updated. */
7638 set_window_update_flags (XWINDOW (f->root_window), 1);
7639 pause |= update_frame (f, 0, 0);
7640 if (!pause)
7641 {
7642 mark_window_display_accurate (f->root_window, 1);
7643 if (frame_up_to_date_hook != 0)
7644 (*frame_up_to_date_hook) (f);
7645 }
7646 }
7647 }
7648 }
7649 else
7650 {
7651 if (FRAME_VISIBLE_P (sf)
7652 && !FRAME_OBSCURED_P (sf))
7653 {
7654 if (hscroll_windows (selected_window))
7655 goto retry;
7656
7657 XWINDOW (selected_window)->must_be_updated_p = 1;
7658 pause = update_frame (sf, 0, 0);
7659 }
7660 else
7661 pause = 0;
7662
7663 /* We may have called echo_area_display at the top of this
7664 function. If the echo area is on another frame, that may
7665 have put text on a frame other than the selected one, so the
7666 above call to update_frame would not have caught it. Catch
7667 it here. */
7668 {
7669 Lisp_Object mini_window;
7670 struct frame *mini_frame;
7671
7672 mini_window = FRAME_MINIBUF_WINDOW (sf);
7673 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7674
7675 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
7676 {
7677 XWINDOW (mini_window)->must_be_updated_p = 1;
7678 pause |= update_frame (mini_frame, 0, 0);
7679 if (!pause && hscroll_windows (mini_window))
7680 goto retry;
7681 }
7682 }
7683 }
7684
7685 /* If display was paused because of pending input, make sure we do a
7686 thorough update the next time. */
7687 if (pause)
7688 {
7689 /* Prevent the optimization at the beginning of
7690 redisplay_internal that tries a single-line update of the
7691 line containing the cursor in the selected window. */
7692 CHARPOS (this_line_start_pos) = 0;
7693
7694 /* Let the overlay arrow be updated the next time. */
7695 if (!NILP (last_arrow_position))
7696 {
7697 last_arrow_position = Qt;
7698 last_arrow_string = Qt;
7699 }
7700
7701 /* If we pause after scrolling, some rows in the current
7702 matrices of some windows are not valid. */
7703 if (!WINDOW_FULL_WIDTH_P (w)
7704 && !FRAME_WINDOW_P (XFRAME (w->frame)))
7705 update_mode_lines = 1;
7706 }
7707
7708 /* Now text on frame agrees with windows, so put info into the
7709 windows for partial redisplay to follow. */
7710 if (!pause)
7711 {
7712 register struct buffer *b = XBUFFER (w->buffer);
7713
7714 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
7715 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
7716 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
7717 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
7718
7719 if (consider_all_windows_p)
7720 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
7721 else
7722 {
7723 XSETFASTINT (w->last_point, BUF_PT (b));
7724 w->last_cursor = w->cursor;
7725 w->last_cursor_off_p = w->cursor_off_p;
7726
7727 b->clip_changed = 0;
7728 b->prevent_redisplay_optimizations_p = 0;
7729 w->update_mode_line = Qnil;
7730 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
7731 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
7732 w->last_had_star
7733 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7734 ? Qt : Qnil);
7735
7736 /* Record if we are showing a region, so can make sure to
7737 update it fully at next redisplay. */
7738 w->region_showing = (!NILP (Vtransient_mark_mode)
7739 && (w == XWINDOW (current_buffer->last_selected_window)
7740 || highlight_nonselected_windows)
7741 && !NILP (XBUFFER (w->buffer)->mark_active)
7742 ? Fmarker_position (XBUFFER (w->buffer)->mark)
7743 : Qnil);
7744
7745 w->window_end_valid = w->buffer;
7746 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
7747 last_arrow_string = Voverlay_arrow_string;
7748 if (frame_up_to_date_hook != 0)
7749 (*frame_up_to_date_hook) (sf);
7750
7751 w->current_matrix->buffer = b;
7752 w->current_matrix->begv = BUF_BEGV (b);
7753 w->current_matrix->zv = BUF_ZV (b);
7754 }
7755
7756 update_mode_lines = 0;
7757 windows_or_buffers_changed = 0;
7758 }
7759
7760 /* Start SIGIO interrupts coming again. Having them off during the
7761 code above makes it less likely one will discard output, but not
7762 impossible, since there might be stuff in the system buffer here.
7763 But it is much hairier to try to do anything about that. */
7764 if (interrupt_input)
7765 request_sigio ();
7766 start_polling ();
7767
7768 /* If a frame has become visible which was not before, redisplay
7769 again, so that we display it. Expose events for such a frame
7770 (which it gets when becoming visible) don't call the parts of
7771 redisplay constructing glyphs, so simply exposing a frame won't
7772 display anything in this case. So, we have to display these
7773 frames here explicitly. */
7774 if (!pause)
7775 {
7776 Lisp_Object tail, frame;
7777 int new_count = 0;
7778
7779 FOR_EACH_FRAME (tail, frame)
7780 {
7781 int this_is_visible = 0;
7782
7783 if (XFRAME (frame)->visible)
7784 this_is_visible = 1;
7785 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
7786 if (XFRAME (frame)->visible)
7787 this_is_visible = 1;
7788
7789 if (this_is_visible)
7790 new_count++;
7791 }
7792
7793 if (new_count != number_of_visible_frames)
7794 windows_or_buffers_changed++;
7795 }
7796
7797 /* Change frame size now if a change is pending. */
7798 do_pending_window_change (1);
7799
7800 /* If we just did a pending size change, or have additional
7801 visible frames, redisplay again. */
7802 if (windows_or_buffers_changed && !pause)
7803 goto retry;
7804
7805 end_of_redisplay:;
7806
7807 unbind_to (count, Qnil);
7808 }
7809
7810
7811 /* Redisplay, but leave alone any recent echo area message unless
7812 another message has been requested in its place.
7813
7814 This is useful in situations where you need to redisplay but no
7815 user action has occurred, making it inappropriate for the message
7816 area to be cleared. See tracking_off and
7817 wait_reading_process_input for examples of these situations. */
7818
7819 void
7820 redisplay_preserve_echo_area ()
7821 {
7822 if (!NILP (echo_area_buffer[1]))
7823 {
7824 /* We have a previously displayed message, but no current
7825 message. Redisplay the previous message. */
7826 display_last_displayed_message_p = 1;
7827 redisplay_internal (1);
7828 display_last_displayed_message_p = 0;
7829 }
7830 else
7831 redisplay_internal (1);
7832 }
7833
7834
7835 /* Function registered with record_unwind_protect in
7836 redisplay_internal. Clears the flag indicating that a redisplay is
7837 in progress. */
7838
7839 static Lisp_Object
7840 unwind_redisplay (old_redisplaying_p)
7841 Lisp_Object old_redisplaying_p;
7842 {
7843 redisplaying_p = XFASTINT (old_redisplaying_p);
7844 return Qnil;
7845 }
7846
7847
7848 /* Mark the display of windows in the window tree rooted at WINDOW as
7849 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
7850 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
7851 the next time redisplay_internal is called. */
7852
7853 void
7854 mark_window_display_accurate (window, accurate_p)
7855 Lisp_Object window;
7856 int accurate_p;
7857 {
7858 struct window *w;
7859
7860 for (; !NILP (window); window = w->next)
7861 {
7862 w = XWINDOW (window);
7863
7864 if (BUFFERP (w->buffer))
7865 {
7866 struct buffer *b = XBUFFER (w->buffer);
7867
7868 XSETFASTINT (w->last_modified,
7869 accurate_p ? BUF_MODIFF (b) : 0);
7870 XSETFASTINT (w->last_overlay_modified,
7871 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
7872 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
7873 ? Qt : Qnil);
7874
7875 #if 0 /* I don't think this is necessary because display_line does it.
7876 Let's check it. */
7877 /* Record if we are showing a region, so can make sure to
7878 update it fully at next redisplay. */
7879 w->region_showing
7880 = (!NILP (Vtransient_mark_mode)
7881 && (w == XWINDOW (current_buffer->last_selected_window)
7882 || highlight_nonselected_windows)
7883 && (!NILP (b->mark_active)
7884 ? Fmarker_position (b->mark)
7885 : Qnil));
7886 #endif
7887
7888 if (accurate_p)
7889 {
7890 b->clip_changed = 0;
7891 b->prevent_redisplay_optimizations_p = 0;
7892 w->current_matrix->buffer = b;
7893 w->current_matrix->begv = BUF_BEGV (b);
7894 w->current_matrix->zv = BUF_ZV (b);
7895 w->last_cursor = w->cursor;
7896 w->last_cursor_off_p = w->cursor_off_p;
7897 if (w == XWINDOW (selected_window))
7898 w->last_point = make_number (BUF_PT (b));
7899 else
7900 w->last_point = make_number (XMARKER (w->pointm)->charpos);
7901 }
7902 }
7903
7904 w->window_end_valid = w->buffer;
7905 w->update_mode_line = Qnil;
7906
7907 if (!NILP (w->vchild))
7908 mark_window_display_accurate (w->vchild, accurate_p);
7909 if (!NILP (w->hchild))
7910 mark_window_display_accurate (w->hchild, accurate_p);
7911 }
7912
7913 if (accurate_p)
7914 {
7915 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
7916 last_arrow_string = Voverlay_arrow_string;
7917 }
7918 else
7919 {
7920 /* Force a thorough redisplay the next time by setting
7921 last_arrow_position and last_arrow_string to t, which is
7922 unequal to any useful value of Voverlay_arrow_... */
7923 last_arrow_position = Qt;
7924 last_arrow_string = Qt;
7925 }
7926 }
7927
7928
7929 /* Return value in display table DP (Lisp_Char_Table *) for character
7930 C. Since a display table doesn't have any parent, we don't have to
7931 follow parent. Do not call this function directly but use the
7932 macro DISP_CHAR_VECTOR. */
7933
7934 Lisp_Object
7935 disp_char_vector (dp, c)
7936 struct Lisp_Char_Table *dp;
7937 int c;
7938 {
7939 int code[4], i;
7940 Lisp_Object val;
7941
7942 if (SINGLE_BYTE_CHAR_P (c))
7943 return (dp->contents[c]);
7944
7945 SPLIT_NON_ASCII_CHAR (c, code[0], code[1], code[2]);
7946 if (code[1] < 32)
7947 code[1] = -1;
7948 else if (code[2] < 32)
7949 code[2] = -1;
7950
7951 /* Here, the possible range of code[0] (== charset ID) is
7952 128..max_charset. Since the top level char table contains data
7953 for multibyte characters after 256th element, we must increment
7954 code[0] by 128 to get a correct index. */
7955 code[0] += 128;
7956 code[3] = -1; /* anchor */
7957
7958 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
7959 {
7960 val = dp->contents[code[i]];
7961 if (!SUB_CHAR_TABLE_P (val))
7962 return (NILP (val) ? dp->defalt : val);
7963 }
7964
7965 /* Here, val is a sub char table. We return the default value of
7966 it. */
7967 return (dp->defalt);
7968 }
7969
7970
7971 \f
7972 /***********************************************************************
7973 Window Redisplay
7974 ***********************************************************************/
7975
7976 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
7977
7978 static void
7979 redisplay_windows (window)
7980 Lisp_Object window;
7981 {
7982 while (!NILP (window))
7983 {
7984 struct window *w = XWINDOW (window);
7985
7986 if (!NILP (w->hchild))
7987 redisplay_windows (w->hchild);
7988 else if (!NILP (w->vchild))
7989 redisplay_windows (w->vchild);
7990 else
7991 redisplay_window (window, 0);
7992
7993 window = w->next;
7994 }
7995 }
7996
7997
7998 /* Set cursor position of W. PT is assumed to be displayed in ROW.
7999 DELTA is the number of bytes by which positions recorded in ROW
8000 differ from current buffer positions. */
8001
8002 void
8003 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8004 struct window *w;
8005 struct glyph_row *row;
8006 struct glyph_matrix *matrix;
8007 int delta, delta_bytes, dy, dvpos;
8008 {
8009 struct glyph *glyph = row->glyphs[TEXT_AREA];
8010 struct glyph *end = glyph + row->used[TEXT_AREA];
8011 int x = row->x;
8012 int pt_old = PT - delta;
8013
8014 /* Skip over glyphs not having an object at the start of the row.
8015 These are special glyphs like truncation marks on terminal
8016 frames. */
8017 if (row->displays_text_p)
8018 while (glyph < end
8019 && INTEGERP (glyph->object)
8020 && glyph->charpos < 0)
8021 {
8022 x += glyph->pixel_width;
8023 ++glyph;
8024 }
8025
8026 while (glyph < end
8027 && !INTEGERP (glyph->object)
8028 && (!BUFFERP (glyph->object)
8029 || glyph->charpos < pt_old))
8030 {
8031 x += glyph->pixel_width;
8032 ++glyph;
8033 }
8034
8035 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8036 w->cursor.x = x;
8037 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8038 w->cursor.y = row->y + dy;
8039
8040 if (w == XWINDOW (selected_window))
8041 {
8042 if (!row->continued_p
8043 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8044 && row->x == 0)
8045 {
8046 this_line_buffer = XBUFFER (w->buffer);
8047
8048 CHARPOS (this_line_start_pos)
8049 = MATRIX_ROW_START_CHARPOS (row) + delta;
8050 BYTEPOS (this_line_start_pos)
8051 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8052
8053 CHARPOS (this_line_end_pos)
8054 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8055 BYTEPOS (this_line_end_pos)
8056 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8057
8058 this_line_y = w->cursor.y;
8059 this_line_pixel_height = row->height;
8060 this_line_vpos = w->cursor.vpos;
8061 this_line_start_x = row->x;
8062 }
8063 else
8064 CHARPOS (this_line_start_pos) = 0;
8065 }
8066 }
8067
8068
8069 /* Run window scroll functions, if any, for WINDOW with new window
8070 start STARTP. Sets the window start of WINDOW to that position.
8071
8072 We assume that the window's buffer is really current. */
8073
8074 static INLINE struct text_pos
8075 run_window_scroll_functions (window, startp)
8076 Lisp_Object window;
8077 struct text_pos startp;
8078 {
8079 struct window *w = XWINDOW (window);
8080 SET_MARKER_FROM_TEXT_POS (w->start, startp);
8081
8082 if (current_buffer != XBUFFER (w->buffer))
8083 abort ();
8084
8085 if (!NILP (Vwindow_scroll_functions))
8086 {
8087 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8088 make_number (CHARPOS (startp)));
8089 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8090 /* In case the hook functions switch buffers. */
8091 if (current_buffer != XBUFFER (w->buffer))
8092 set_buffer_internal_1 (XBUFFER (w->buffer));
8093 }
8094
8095 return startp;
8096 }
8097
8098
8099 /* Modify the desired matrix of window W and W->vscroll so that the
8100 line containing the cursor is fully visible. */
8101
8102 static void
8103 make_cursor_line_fully_visible (w)
8104 struct window *w;
8105 {
8106 struct glyph_matrix *matrix;
8107 struct glyph_row *row;
8108 int header_line_height;
8109
8110 /* It's not always possible to find the cursor, e.g, when a window
8111 is full of overlay strings. Don't do anything in that case. */
8112 if (w->cursor.vpos < 0)
8113 return;
8114
8115 matrix = w->desired_matrix;
8116 row = MATRIX_ROW (matrix, w->cursor.vpos);
8117
8118 /* If row->y == top y of window display area, the window isn't tall
8119 enough to display a single line. There is nothing we can do
8120 about it. */
8121 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
8122 if (row->y == header_line_height)
8123 return;
8124
8125 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
8126 {
8127 int dy = row->height - row->visible_height;
8128 w->vscroll = 0;
8129 w->cursor.y += dy;
8130 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8131 }
8132 else if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row))
8133 {
8134 int dy = - (row->height - row->visible_height);
8135 w->vscroll = dy;
8136 w->cursor.y += dy;
8137 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8138 }
8139
8140 /* When we change the cursor y-position of the selected window,
8141 change this_line_y as well so that the display optimization for
8142 the cursor line of the selected window in redisplay_internal uses
8143 the correct y-position. */
8144 if (w == XWINDOW (selected_window))
8145 this_line_y = w->cursor.y;
8146 }
8147
8148
8149 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8150 non-zero means only WINDOW is redisplayed in redisplay_internal.
8151 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8152 in redisplay_window to bring a partially visible line into view in
8153 the case that only the cursor has moved.
8154
8155 Value is
8156
8157 1 if scrolling succeeded
8158
8159 0 if scrolling didn't find point.
8160
8161 -1 if new fonts have been loaded so that we must interrupt
8162 redisplay, adjust glyph matrices, and try again. */
8163
8164 static int
8165 try_scrolling (window, just_this_one_p, scroll_conservatively,
8166 scroll_step, temp_scroll_step)
8167 Lisp_Object window;
8168 int just_this_one_p;
8169 int scroll_conservatively, scroll_step;
8170 int temp_scroll_step;
8171 {
8172 struct window *w = XWINDOW (window);
8173 struct frame *f = XFRAME (w->frame);
8174 struct text_pos scroll_margin_pos;
8175 struct text_pos pos;
8176 struct text_pos startp;
8177 struct it it;
8178 Lisp_Object window_end;
8179 int this_scroll_margin;
8180 int dy = 0;
8181 int scroll_max;
8182 int line_height, rc;
8183 int amount_to_scroll = 0;
8184 Lisp_Object aggressive;
8185 int height;
8186
8187 #if GLYPH_DEBUG
8188 debug_method_add (w, "try_scrolling");
8189 #endif
8190
8191 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8192
8193 /* Compute scroll margin height in pixels. We scroll when point is
8194 within this distance from the top or bottom of the window. */
8195 if (scroll_margin > 0)
8196 {
8197 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8198 this_scroll_margin *= CANON_Y_UNIT (f);
8199 }
8200 else
8201 this_scroll_margin = 0;
8202
8203 /* Compute how much we should try to scroll maximally to bring point
8204 into view. */
8205 if (scroll_step)
8206 scroll_max = scroll_step;
8207 else if (scroll_conservatively)
8208 scroll_max = scroll_conservatively;
8209 else if (temp_scroll_step)
8210 scroll_max = temp_scroll_step;
8211 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8212 || NUMBERP (current_buffer->scroll_up_aggressively))
8213 /* We're trying to scroll because of aggressive scrolling
8214 but no scroll_step is set. Choose an arbitrary one. Maybe
8215 there should be a variable for this. */
8216 scroll_max = 10;
8217 else
8218 scroll_max = 0;
8219 scroll_max *= CANON_Y_UNIT (f);
8220
8221 /* Decide whether we have to scroll down. Start at the window end
8222 and move this_scroll_margin up to find the position of the scroll
8223 margin. */
8224 window_end = Fwindow_end (window, Qt);
8225 CHARPOS (scroll_margin_pos) = XINT (window_end);
8226 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8227 if (this_scroll_margin)
8228 {
8229 start_display (&it, w, scroll_margin_pos);
8230 move_it_vertically (&it, - this_scroll_margin);
8231 scroll_margin_pos = it.current.pos;
8232 }
8233
8234 if (PT >= CHARPOS (scroll_margin_pos))
8235 {
8236 int y0;
8237
8238 /* Point is in the scroll margin at the bottom of the window, or
8239 below. Compute a new window start that makes point visible. */
8240
8241 /* Compute the distance from the scroll margin to PT.
8242 Give up if the distance is greater than scroll_max. */
8243 start_display (&it, w, scroll_margin_pos);
8244 y0 = it.current_y;
8245 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8246 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8247 line_height = (it.max_ascent + it.max_descent
8248 ? it.max_ascent + it.max_descent
8249 : last_height);
8250 dy = it.current_y + line_height - y0;
8251 if (dy > scroll_max)
8252 return 0;
8253
8254 /* Move the window start down. If scrolling conservatively,
8255 move it just enough down to make point visible. If
8256 scroll_step is set, move it down by scroll_step. */
8257 start_display (&it, w, startp);
8258
8259 if (scroll_conservatively)
8260 amount_to_scroll = dy;
8261 else if (scroll_step || temp_scroll_step)
8262 amount_to_scroll = scroll_max;
8263 else
8264 {
8265 aggressive = current_buffer->scroll_down_aggressively;
8266 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8267 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8268 if (NUMBERP (aggressive))
8269 amount_to_scroll = XFLOATINT (aggressive) * height;
8270 }
8271
8272 if (amount_to_scroll <= 0)
8273 return 0;
8274
8275 move_it_vertically (&it, amount_to_scroll);
8276 startp = it.current.pos;
8277 }
8278 else
8279 {
8280 /* See if point is inside the scroll margin at the top of the
8281 window. */
8282 scroll_margin_pos = startp;
8283 if (this_scroll_margin)
8284 {
8285 start_display (&it, w, startp);
8286 move_it_vertically (&it, this_scroll_margin);
8287 scroll_margin_pos = it.current.pos;
8288 }
8289
8290 if (PT < CHARPOS (scroll_margin_pos))
8291 {
8292 /* Point is in the scroll margin at the top of the window or
8293 above what is displayed in the window. */
8294 int y0;
8295
8296 /* Compute the vertical distance from PT to the scroll
8297 margin position. Give up if distance is greater than
8298 scroll_max. */
8299 SET_TEXT_POS (pos, PT, PT_BYTE);
8300 start_display (&it, w, pos);
8301 y0 = it.current_y;
8302 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8303 it.last_visible_y, -1,
8304 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8305 dy = it.current_y - y0;
8306 if (dy > scroll_max)
8307 return 0;
8308
8309 /* Compute new window start. */
8310 start_display (&it, w, startp);
8311
8312 if (scroll_conservatively)
8313 amount_to_scroll = dy;
8314 else if (scroll_step || temp_scroll_step)
8315 amount_to_scroll = scroll_max;
8316 else
8317 {
8318 aggressive = current_buffer->scroll_up_aggressively;
8319 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8320 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8321 if (NUMBERP (aggressive))
8322 amount_to_scroll = XFLOATINT (aggressive) * height;
8323 }
8324
8325 if (amount_to_scroll <= 0)
8326 return 0;
8327
8328 move_it_vertically (&it, - amount_to_scroll);
8329 startp = it.current.pos;
8330 }
8331 }
8332
8333 /* Run window scroll functions. */
8334 startp = run_window_scroll_functions (window, startp);
8335
8336 /* Display the window. Give up if new fonts are loaded, or if point
8337 doesn't appear. */
8338 if (!try_window (window, startp))
8339 rc = -1;
8340 else if (w->cursor.vpos < 0)
8341 {
8342 clear_glyph_matrix (w->desired_matrix);
8343 rc = 0;
8344 }
8345 else
8346 {
8347 /* Maybe forget recorded base line for line number display. */
8348 if (!just_this_one_p
8349 || current_buffer->clip_changed
8350 || BEG_UNCHANGED < CHARPOS (startp))
8351 w->base_line_number = Qnil;
8352
8353 /* If cursor ends up on a partially visible line, shift display
8354 lines up or down. */
8355 make_cursor_line_fully_visible (w);
8356 rc = 1;
8357 }
8358
8359 return rc;
8360 }
8361
8362
8363 /* Compute a suitable window start for window W if display of W starts
8364 on a continuation line. Value is non-zero if a new window start
8365 was computed.
8366
8367 The new window start will be computed, based on W's width, starting
8368 from the start of the continued line. It is the start of the
8369 screen line with the minimum distance from the old start W->start. */
8370
8371 static int
8372 compute_window_start_on_continuation_line (w)
8373 struct window *w;
8374 {
8375 struct text_pos pos, start_pos;
8376 int window_start_changed_p = 0;
8377
8378 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
8379
8380 /* If window start is on a continuation line... Window start may be
8381 < BEGV in case there's invisible text at the start of the
8382 buffer (M-x rmail, for example). */
8383 if (CHARPOS (start_pos) > BEGV
8384 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
8385 {
8386 struct it it;
8387 struct glyph_row *row;
8388
8389 /* Handle the case that the window start is out of range. */
8390 if (CHARPOS (start_pos) < BEGV)
8391 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
8392 else if (CHARPOS (start_pos) > ZV)
8393 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
8394
8395 /* Find the start of the continued line. This should be fast
8396 because scan_buffer is fast (newline cache). */
8397 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
8398 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
8399 row, DEFAULT_FACE_ID);
8400 reseat_at_previous_visible_line_start (&it);
8401
8402 /* If the line start is "too far" away from the window start,
8403 say it takes too much time to compute a new window start. */
8404 if (CHARPOS (start_pos) - IT_CHARPOS (it)
8405 < XFASTINT (w->height) * XFASTINT (w->width))
8406 {
8407 int min_distance, distance;
8408
8409 /* Move forward by display lines to find the new window
8410 start. If window width was enlarged, the new start can
8411 be expected to be > the old start. If window width was
8412 decreased, the new window start will be < the old start.
8413 So, we're looking for the display line start with the
8414 minimum distance from the old window start. */
8415 pos = it.current.pos;
8416 min_distance = INFINITY;
8417 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
8418 distance < min_distance)
8419 {
8420 min_distance = distance;
8421 pos = it.current.pos;
8422 move_it_by_lines (&it, 1, 0);
8423 }
8424
8425 /* Set the window start there. */
8426 SET_MARKER_FROM_TEXT_POS (w->start, pos);
8427 window_start_changed_p = 1;
8428 }
8429 }
8430
8431 return window_start_changed_p;
8432 }
8433
8434
8435 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
8436 selected_window is redisplayed. */
8437
8438 static void
8439 redisplay_window (window, just_this_one_p)
8440 Lisp_Object window;
8441 int just_this_one_p;
8442 {
8443 struct window *w = XWINDOW (window);
8444 struct frame *f = XFRAME (w->frame);
8445 struct buffer *buffer = XBUFFER (w->buffer);
8446 struct buffer *old = current_buffer;
8447 struct text_pos lpoint, opoint, startp;
8448 int update_mode_line;
8449 int tem;
8450 struct it it;
8451 /* Record it now because it's overwritten. */
8452 int current_matrix_up_to_date_p = 0;
8453 int really_switched_buffer = 0;
8454 int temp_scroll_step = 0;
8455 int count = specpdl_ptr - specpdl;
8456
8457 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8458 opoint = lpoint;
8459
8460 /* W must be a leaf window here. */
8461 xassert (!NILP (w->buffer));
8462 #if GLYPH_DEBUG
8463 *w->desired_matrix->method = 0;
8464 #endif
8465
8466 specbind (Qinhibit_point_motion_hooks, Qt);
8467
8468 reconsider_clip_changes (w, buffer);
8469
8470 /* Has the mode line to be updated? */
8471 update_mode_line = (!NILP (w->update_mode_line)
8472 || update_mode_lines
8473 || buffer->clip_changed);
8474
8475 if (MINI_WINDOW_P (w))
8476 {
8477 if (w == XWINDOW (echo_area_window)
8478 && !NILP (echo_area_buffer[0]))
8479 {
8480 if (update_mode_line)
8481 /* We may have to update a tty frame's menu bar or a
8482 tool-bar. Example `M-x C-h C-h C-g'. */
8483 goto finish_menu_bars;
8484 else
8485 /* We've already displayed the echo area glyphs in this window. */
8486 goto finish_scroll_bars;
8487 }
8488 else if (w != XWINDOW (minibuf_window))
8489 {
8490 /* W is a mini-buffer window, but it's not the currently
8491 active one, so clear it. */
8492 int yb = window_text_bottom_y (w);
8493 struct glyph_row *row;
8494 int y;
8495
8496 for (y = 0, row = w->desired_matrix->rows;
8497 y < yb;
8498 y += row->height, ++row)
8499 blank_row (w, row, y);
8500 goto finish_scroll_bars;
8501 }
8502 }
8503
8504 /* Otherwise set up data on this window; select its buffer and point
8505 value. */
8506 if (update_mode_line)
8507 {
8508 /* Really select the buffer, for the sake of buffer-local
8509 variables. */
8510 set_buffer_internal_1 (XBUFFER (w->buffer));
8511 really_switched_buffer = 1;
8512 }
8513 else
8514 set_buffer_temp (XBUFFER (w->buffer));
8515 SET_TEXT_POS (opoint, PT, PT_BYTE);
8516
8517 current_matrix_up_to_date_p
8518 = (!NILP (w->window_end_valid)
8519 && !current_buffer->clip_changed
8520 && XFASTINT (w->last_modified) >= MODIFF
8521 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
8522
8523 /* When windows_or_buffers_changed is non-zero, we can't rely on
8524 the window end being valid, so set it to nil there. */
8525 if (windows_or_buffers_changed)
8526 {
8527 /* If window starts on a continuation line, maybe adjust the
8528 window start in case the window's width changed. */
8529 if (XMARKER (w->start)->buffer == current_buffer)
8530 compute_window_start_on_continuation_line (w);
8531
8532 w->window_end_valid = Qnil;
8533 }
8534
8535 /* Some sanity checks. */
8536 CHECK_WINDOW_END (w);
8537 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
8538 abort ();
8539 if (BYTEPOS (opoint) < CHARPOS (opoint))
8540 abort ();
8541
8542 /* If %c is in mode line, update it if needed. */
8543 if (!NILP (w->column_number_displayed)
8544 /* This alternative quickly identifies a common case
8545 where no change is needed. */
8546 && !(PT == XFASTINT (w->last_point)
8547 && XFASTINT (w->last_modified) >= MODIFF
8548 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8549 && XFASTINT (w->column_number_displayed) != current_column ())
8550 update_mode_line = 1;
8551
8552 /* Count number of windows showing the selected buffer. An indirect
8553 buffer counts as its base buffer. */
8554 if (!just_this_one_p)
8555 {
8556 struct buffer *current_base, *window_base;
8557 current_base = current_buffer;
8558 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
8559 if (current_base->base_buffer)
8560 current_base = current_base->base_buffer;
8561 if (window_base->base_buffer)
8562 window_base = window_base->base_buffer;
8563 if (current_base == window_base)
8564 buffer_shared++;
8565 }
8566
8567 /* Point refers normally to the selected window. For any other
8568 window, set up appropriate value. */
8569 if (!EQ (window, selected_window))
8570 {
8571 int new_pt = XMARKER (w->pointm)->charpos;
8572 int new_pt_byte = marker_byte_position (w->pointm);
8573 if (new_pt < BEGV)
8574 {
8575 new_pt = BEGV;
8576 new_pt_byte = BEGV_BYTE;
8577 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
8578 }
8579 else if (new_pt > (ZV - 1))
8580 {
8581 new_pt = ZV;
8582 new_pt_byte = ZV_BYTE;
8583 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
8584 }
8585
8586 /* We don't use SET_PT so that the point-motion hooks don't run. */
8587 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
8588 }
8589
8590 /* If any of the character widths specified in the display table
8591 have changed, invalidate the width run cache. It's true that
8592 this may be a bit late to catch such changes, but the rest of
8593 redisplay goes (non-fatally) haywire when the display table is
8594 changed, so why should we worry about doing any better? */
8595 if (current_buffer->width_run_cache)
8596 {
8597 struct Lisp_Char_Table *disptab = buffer_display_table ();
8598
8599 if (! disptab_matches_widthtab (disptab,
8600 XVECTOR (current_buffer->width_table)))
8601 {
8602 invalidate_region_cache (current_buffer,
8603 current_buffer->width_run_cache,
8604 BEG, Z);
8605 recompute_width_table (current_buffer, disptab);
8606 }
8607 }
8608
8609 /* If window-start is screwed up, choose a new one. */
8610 if (XMARKER (w->start)->buffer != current_buffer)
8611 goto recenter;
8612
8613 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8614
8615 /* If someone specified a new starting point but did not insist,
8616 check whether it can be used. */
8617 if (!NILP (w->optional_new_start)
8618 && CHARPOS (startp) >= BEGV
8619 && CHARPOS (startp) <= ZV)
8620 {
8621 w->optional_new_start = Qnil;
8622 /* This takes a mini-buffer prompt into account. */
8623 start_display (&it, w, startp);
8624 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8625 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8626 if (IT_CHARPOS (it) == PT)
8627 w->force_start = Qt;
8628 }
8629
8630 /* Handle case where place to start displaying has been specified,
8631 unless the specified location is outside the accessible range. */
8632 if (!NILP (w->force_start)
8633 || w->frozen_window_start_p)
8634 {
8635 w->force_start = Qnil;
8636 w->vscroll = 0;
8637 w->window_end_valid = Qnil;
8638
8639 /* Forget any recorded base line for line number display. */
8640 if (!current_matrix_up_to_date_p
8641 || current_buffer->clip_changed)
8642 w->base_line_number = Qnil;
8643
8644 /* Redisplay the mode line. Select the buffer properly for that.
8645 Also, run the hook window-scroll-functions
8646 because we have scrolled. */
8647 /* Note, we do this after clearing force_start because
8648 if there's an error, it is better to forget about force_start
8649 than to get into an infinite loop calling the hook functions
8650 and having them get more errors. */
8651 if (!update_mode_line
8652 || ! NILP (Vwindow_scroll_functions))
8653 {
8654 if (!really_switched_buffer)
8655 {
8656 set_buffer_temp (old);
8657 set_buffer_internal_1 (XBUFFER (w->buffer));
8658 really_switched_buffer = 1;
8659 }
8660
8661 update_mode_line = 1;
8662 w->update_mode_line = Qt;
8663 startp = run_window_scroll_functions (window, startp);
8664 }
8665
8666 XSETFASTINT (w->last_modified, 0);
8667 XSETFASTINT (w->last_overlay_modified, 0);
8668 if (CHARPOS (startp) < BEGV)
8669 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
8670 else if (CHARPOS (startp) > ZV)
8671 SET_TEXT_POS (startp, ZV, ZV_BYTE);
8672
8673 /* Redisplay, then check if cursor has been set during the
8674 redisplay. Give up if new fonts were loaded. */
8675 if (!try_window (window, startp))
8676 {
8677 w->force_start = Qt;
8678 clear_glyph_matrix (w->desired_matrix);
8679 goto restore_buffers;
8680 }
8681
8682 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
8683 {
8684 /* If point does not appear, or on a line that is not fully
8685 visible, move point so it does appear. The desired
8686 matrix has been built above, so we can use it. */
8687 int height = window_box_height (w) / 2;
8688 struct glyph_row *row = MATRIX_ROW (w->desired_matrix, 0);
8689
8690 while (row->y < height)
8691 ++row;
8692
8693 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
8694 MATRIX_ROW_START_BYTEPOS (row));
8695
8696 if (w != XWINDOW (selected_window))
8697 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
8698 else if (current_buffer == old)
8699 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8700
8701 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
8702
8703 /* If we are highlighting the region, then we just changed
8704 the region, so redisplay to show it. */
8705 if (!NILP (Vtransient_mark_mode)
8706 && !NILP (current_buffer->mark_active))
8707 {
8708 clear_glyph_matrix (w->desired_matrix);
8709 if (!try_window (window, startp))
8710 goto restore_buffers;
8711 }
8712 }
8713
8714 make_cursor_line_fully_visible (w);
8715 #if GLYPH_DEBUG
8716 debug_method_add (w, "forced window start");
8717 #endif
8718 goto done;
8719 }
8720
8721 /* Handle case where text has not changed, only point, and it has
8722 not moved off the frame. */
8723 if (current_matrix_up_to_date_p
8724 /* Point may be in this window. */
8725 && PT >= CHARPOS (startp)
8726 /* If we don't check this, we are called to move the cursor in a
8727 horizontally split window with a current matrix that doesn't
8728 fit the display. */
8729 && !windows_or_buffers_changed
8730 /* Selective display hasn't changed. */
8731 && !current_buffer->clip_changed
8732 /* If force-mode-line-update was called, really redisplay;
8733 that's how redisplay is forced after e.g. changing
8734 buffer-invisibility-spec. */
8735 && NILP (w->update_mode_line)
8736 /* Can't use this case if highlighting a region. When a
8737 region exists, cursor movement has to do more than just
8738 set the cursor. */
8739 && !(!NILP (Vtransient_mark_mode)
8740 && !NILP (current_buffer->mark_active))
8741 && NILP (w->region_showing)
8742 && NILP (Vshow_trailing_whitespace)
8743 /* Right after splitting windows, last_point may be nil. */
8744 && INTEGERP (w->last_point)
8745 /* This code is not used for mini-buffer for the sake of the case
8746 of redisplaying to replace an echo area message; since in
8747 that case the mini-buffer contents per se are usually
8748 unchanged. This code is of no real use in the mini-buffer
8749 since the handling of this_line_start_pos, etc., in redisplay
8750 handles the same cases. */
8751 && !EQ (window, minibuf_window)
8752 /* When splitting windows or for new windows, it happens that
8753 redisplay is called with a nil window_end_vpos or one being
8754 larger than the window. This should really be fixed in
8755 window.c. I don't have this on my list, now, so we do
8756 approximately the same as the old redisplay code. --gerd. */
8757 && INTEGERP (w->window_end_vpos)
8758 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
8759 && (FRAME_WINDOW_P (f)
8760 || !MARKERP (Voverlay_arrow_position)
8761 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
8762 {
8763 int this_scroll_margin;
8764 struct glyph_row *row;
8765 int scroll_p;
8766
8767 #if GLYPH_DEBUG
8768 debug_method_add (w, "cursor movement");
8769 #endif
8770
8771 /* Scroll if point within this distance from the top or bottom
8772 of the window. This is a pixel value. */
8773 this_scroll_margin = max (0, scroll_margin);
8774 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
8775 this_scroll_margin *= CANON_Y_UNIT (f);
8776
8777 /* Start with the row the cursor was displayed during the last
8778 not paused redisplay. Give up if that row is not valid. */
8779 if (w->last_cursor.vpos >= w->current_matrix->nrows)
8780 goto try_to_scroll;
8781 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
8782 if (row->mode_line_p)
8783 ++row;
8784 if (!row->enabled_p)
8785 goto try_to_scroll;
8786
8787 scroll_p = 0;
8788 if (PT > XFASTINT (w->last_point))
8789 {
8790 /* Point has moved forward. */
8791 int last_y = window_text_bottom_y (w) - this_scroll_margin;
8792
8793 while ((MATRIX_ROW_END_CHARPOS (row) < PT
8794 /* The end position of a row equals the start
8795 position of the next row. If PT is there, we
8796 would rather display it in the next line, except
8797 when this line ends in ZV. */
8798 || (MATRIX_ROW_END_CHARPOS (row) == PT
8799 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
8800 || !row->ends_at_zv_p)))
8801 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
8802 {
8803 xassert (row->enabled_p);
8804 ++row;
8805 }
8806
8807 /* If within the scroll margin, scroll. Note that
8808 MATRIX_ROW_BOTTOM_Y gives the pixel position at which the
8809 next line would be drawn, and that this_scroll_margin can
8810 be zero. */
8811 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
8812 || PT > MATRIX_ROW_END_CHARPOS (row)
8813 /* Line is completely visible last line in window and PT
8814 is to be set in the next line. */
8815 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
8816 && PT == MATRIX_ROW_END_CHARPOS (row)
8817 && !row->ends_at_zv_p
8818 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
8819 scroll_p = 1;
8820 }
8821 else if (PT < XFASTINT (w->last_point))
8822 {
8823 /* Cursor has to be moved backward. Note that PT >=
8824 CHARPOS (startp) because of the outer if-statement. */
8825 while (!row->mode_line_p
8826 && (MATRIX_ROW_START_CHARPOS (row) > PT
8827 || (MATRIX_ROW_START_CHARPOS (row) == PT
8828 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
8829 && (row->y > this_scroll_margin
8830 || CHARPOS (startp) == BEGV))
8831 {
8832 xassert (row->enabled_p);
8833 --row;
8834 }
8835
8836 /* Consider the following case: Window starts at BEGV, there
8837 is invisible, intangible text at BEGV, so that display
8838 starts at some point START > BEGV. It can happen that
8839 we are called with PT somewhere between BEGV and START.
8840 Try to handle that case. */
8841 if (row < w->current_matrix->rows
8842 || row->mode_line_p)
8843 {
8844 row = w->current_matrix->rows;
8845 if (row->mode_line_p)
8846 ++row;
8847 }
8848
8849 /* Due to newlines in overlay strings, we may have to skip
8850 forward over overlay strings. */
8851 while (MATRIX_ROW_END_CHARPOS (row) == PT
8852 && MATRIX_ROW_ENDS_IN_OVERLAY_STRING_P (row)
8853 && !row->ends_at_zv_p)
8854 ++row;
8855
8856 /* If within the scroll margin, scroll. */
8857 if (row->y < this_scroll_margin
8858 && CHARPOS (startp) != BEGV)
8859 scroll_p = 1;
8860 }
8861
8862 /* if PT is not in the glyph row, give up. */
8863 if (PT < MATRIX_ROW_START_CHARPOS (row)
8864 || PT > MATRIX_ROW_END_CHARPOS (row))
8865 goto try_to_scroll;
8866
8867 /* If we end up in a partially visible line, let's make it fully
8868 visible. This can be done most easily by using the existing
8869 scrolling code. */
8870 if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8871 {
8872 temp_scroll_step = 1;
8873 goto try_to_scroll;
8874 }
8875 else if (scroll_p)
8876 goto try_to_scroll;
8877
8878 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8879 goto done;
8880 }
8881
8882 /* If current starting point was originally the beginning of a line
8883 but no longer is, find a new starting point. */
8884 else if (!NILP (w->start_at_line_beg)
8885 && !(CHARPOS (startp) <= BEGV
8886 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
8887 {
8888 #if GLYPH_DEBUG
8889 debug_method_add (w, "recenter 1");
8890 #endif
8891 goto recenter;
8892 }
8893
8894 /* Try scrolling with try_window_id. */
8895 else if (/* Windows and buffers haven't changed. */
8896 !windows_or_buffers_changed
8897 /* Window must be either use window-based redisplay or
8898 be full width. */
8899 && (FRAME_WINDOW_P (f)
8900 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
8901 && !MINI_WINDOW_P (w)
8902 /* Point is not known NOT to appear in window. */
8903 && PT >= CHARPOS (startp)
8904 && XFASTINT (w->last_modified)
8905 /* Window is not hscrolled. */
8906 && XFASTINT (w->hscroll) == 0
8907 /* Selective display has not changed. */
8908 && !current_buffer->clip_changed
8909 /* Current matrix is up to date. */
8910 && !NILP (w->window_end_valid)
8911 /* Can't use this case if highlighting a region because
8912 a cursor movement will do more than just set the cursor. */
8913 && !(!NILP (Vtransient_mark_mode)
8914 && !NILP (current_buffer->mark_active))
8915 && NILP (w->region_showing)
8916 && NILP (Vshow_trailing_whitespace)
8917 /* Overlay arrow position and string not changed. */
8918 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
8919 && EQ (last_arrow_string, Voverlay_arrow_string)
8920 /* Value is > 0 if update has been done, it is -1 if we
8921 know that the same window start will not work. It is 0
8922 if unsuccessful for some other reason. */
8923 && (tem = try_window_id (w)) != 0)
8924 {
8925 #if GLYPH_DEBUG
8926 debug_method_add (w, "try_window_id");
8927 #endif
8928
8929 if (fonts_changed_p)
8930 goto restore_buffers;
8931 if (tem > 0)
8932 goto done;
8933 /* Otherwise try_window_id has returned -1 which means that we
8934 don't want the alternative below this comment to execute. */
8935 }
8936 else if (CHARPOS (startp) >= BEGV
8937 && CHARPOS (startp) <= ZV
8938 && PT >= CHARPOS (startp)
8939 && (CHARPOS (startp) < ZV
8940 /* Avoid starting at end of buffer. */
8941 || CHARPOS (startp) == BEGV
8942 || (XFASTINT (w->last_modified) >= MODIFF
8943 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
8944 {
8945 #if GLYPH_DEBUG
8946 debug_method_add (w, "same window start");
8947 #endif
8948
8949 /* Try to redisplay starting at same place as before.
8950 If point has not moved off frame, accept the results. */
8951 if (!current_matrix_up_to_date_p
8952 /* Don't use try_window_reusing_current_matrix in this case
8953 because a window scroll function can have changed the
8954 buffer. */
8955 || !NILP (Vwindow_scroll_functions)
8956 || MINI_WINDOW_P (w)
8957 || !try_window_reusing_current_matrix (w))
8958 {
8959 IF_DEBUG (debug_method_add (w, "1"));
8960 try_window (window, startp);
8961 }
8962
8963 if (fonts_changed_p)
8964 goto restore_buffers;
8965
8966 if (w->cursor.vpos >= 0)
8967 {
8968 if (!just_this_one_p
8969 || current_buffer->clip_changed
8970 || BEG_UNCHANGED < CHARPOS (startp))
8971 /* Forget any recorded base line for line number display. */
8972 w->base_line_number = Qnil;
8973
8974 make_cursor_line_fully_visible (w);
8975 goto done;
8976 }
8977 else
8978 clear_glyph_matrix (w->desired_matrix);
8979 }
8980
8981 try_to_scroll:
8982
8983 XSETFASTINT (w->last_modified, 0);
8984 XSETFASTINT (w->last_overlay_modified, 0);
8985
8986 /* Redisplay the mode line. Select the buffer properly for that. */
8987 if (!update_mode_line)
8988 {
8989 if (!really_switched_buffer)
8990 {
8991 set_buffer_temp (old);
8992 set_buffer_internal_1 (XBUFFER (w->buffer));
8993 really_switched_buffer = 1;
8994 }
8995 update_mode_line = 1;
8996 w->update_mode_line = Qt;
8997 }
8998
8999 /* Try to scroll by specified few lines. */
9000 if ((scroll_conservatively
9001 || scroll_step
9002 || temp_scroll_step
9003 || NUMBERP (current_buffer->scroll_up_aggressively)
9004 || NUMBERP (current_buffer->scroll_down_aggressively))
9005 && !current_buffer->clip_changed
9006 && CHARPOS (startp) >= BEGV
9007 && CHARPOS (startp) <= ZV)
9008 {
9009 /* The function returns -1 if new fonts were loaded, 1 if
9010 successful, 0 if not successful. */
9011 int rc = try_scrolling (window, just_this_one_p,
9012 scroll_conservatively,
9013 scroll_step,
9014 temp_scroll_step);
9015 if (rc > 0)
9016 goto done;
9017 else if (rc < 0)
9018 goto restore_buffers;
9019 }
9020
9021 /* Finally, just choose place to start which centers point */
9022
9023 recenter:
9024
9025 #if GLYPH_DEBUG
9026 debug_method_add (w, "recenter");
9027 #endif
9028
9029 /* w->vscroll = 0; */
9030
9031 /* Forget any previously recorded base line for line number display. */
9032 if (!current_matrix_up_to_date_p
9033 || current_buffer->clip_changed)
9034 w->base_line_number = Qnil;
9035
9036 /* Move backward half the height of the window. */
9037 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9038 it.current_y = it.last_visible_y;
9039 move_it_vertically_backward (&it, it.last_visible_y / 2);
9040 xassert (IT_CHARPOS (it) >= BEGV);
9041
9042 /* The function move_it_vertically_backward may move over more
9043 than the specified y-distance. If it->w is small, e.g. a
9044 mini-buffer window, we may end up in front of the window's
9045 display area. Start displaying at the start of the line
9046 containing PT in this case. */
9047 if (it.current_y <= 0)
9048 {
9049 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9050 move_it_vertically (&it, 0);
9051 xassert (IT_CHARPOS (it) <= PT);
9052 it.current_y = 0;
9053 }
9054
9055 it.current_x = it.hpos = 0;
9056
9057 /* Set startp here explicitly in case that helps avoid an infinite loop
9058 in case the window-scroll-functions functions get errors. */
9059 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9060
9061 /* Run scroll hooks. */
9062 startp = run_window_scroll_functions (window, it.current.pos);
9063
9064 /* Redisplay the window. */
9065 if (!current_matrix_up_to_date_p
9066 || windows_or_buffers_changed
9067 /* Don't use try_window_reusing_current_matrix in this case
9068 because it can have changed the buffer. */
9069 || !NILP (Vwindow_scroll_functions)
9070 || !just_this_one_p
9071 || MINI_WINDOW_P (w)
9072 || !try_window_reusing_current_matrix (w))
9073 try_window (window, startp);
9074
9075 /* If new fonts have been loaded (due to fontsets), give up. We
9076 have to start a new redisplay since we need to re-adjust glyph
9077 matrices. */
9078 if (fonts_changed_p)
9079 goto restore_buffers;
9080
9081 /* If cursor did not appear assume that the middle of the window is
9082 in the first line of the window. Do it again with the next line.
9083 (Imagine a window of height 100, displaying two lines of height
9084 60. Moving back 50 from it->last_visible_y will end in the first
9085 line.) */
9086 if (w->cursor.vpos < 0)
9087 {
9088 if (!NILP (w->window_end_valid)
9089 && PT >= Z - XFASTINT (w->window_end_pos))
9090 {
9091 clear_glyph_matrix (w->desired_matrix);
9092 move_it_by_lines (&it, 1, 0);
9093 try_window (window, it.current.pos);
9094 }
9095 else if (PT < IT_CHARPOS (it))
9096 {
9097 clear_glyph_matrix (w->desired_matrix);
9098 move_it_by_lines (&it, -1, 0);
9099 try_window (window, it.current.pos);
9100 }
9101 else
9102 {
9103 /* Not much we can do about it. */
9104 }
9105 }
9106
9107 /* Consider the following case: Window starts at BEGV, there is
9108 invisible, intangible text at BEGV, so that display starts at
9109 some point START > BEGV. It can happen that we are called with
9110 PT somewhere between BEGV and START. Try to handle that case. */
9111 if (w->cursor.vpos < 0)
9112 {
9113 struct glyph_row *row = w->current_matrix->rows;
9114 if (row->mode_line_p)
9115 ++row;
9116 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9117 }
9118
9119 make_cursor_line_fully_visible (w);
9120
9121 done:
9122
9123 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9124 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9125 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9126 ? Qt : Qnil);
9127
9128 /* Display the mode line, if we must. */
9129 if ((update_mode_line
9130 /* If window not full width, must redo its mode line
9131 if (a) the window to its side is being redone and
9132 (b) we do a frame-based redisplay. This is a consequence
9133 of how inverted lines are drawn in frame-based redisplay. */
9134 || (!just_this_one_p
9135 && !FRAME_WINDOW_P (f)
9136 && !WINDOW_FULL_WIDTH_P (w))
9137 /* Line number to display. */
9138 || INTEGERP (w->base_line_pos)
9139 /* Column number is displayed and different from the one displayed. */
9140 || (!NILP (w->column_number_displayed)
9141 && XFASTINT (w->column_number_displayed) != current_column ()))
9142 /* This means that the window has a mode line. */
9143 && (WINDOW_WANTS_MODELINE_P (w)
9144 || WINDOW_WANTS_HEADER_LINE_P (w)))
9145 {
9146 display_mode_lines (w);
9147
9148 /* If mode line height has changed, arrange for a thorough
9149 immediate redisplay using the correct mode line height. */
9150 if (WINDOW_WANTS_MODELINE_P (w)
9151 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
9152 {
9153 fonts_changed_p = 1;
9154 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9155 = DESIRED_MODE_LINE_HEIGHT (w);
9156 }
9157
9158 /* If top line height has changed, arrange for a thorough
9159 immediate redisplay using the correct mode line height. */
9160 if (WINDOW_WANTS_HEADER_LINE_P (w)
9161 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
9162 {
9163 fonts_changed_p = 1;
9164 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9165 = DESIRED_HEADER_LINE_HEIGHT (w);
9166 }
9167
9168 if (fonts_changed_p)
9169 goto restore_buffers;
9170 }
9171
9172 if (!line_number_displayed
9173 && !BUFFERP (w->base_line_pos))
9174 {
9175 w->base_line_pos = Qnil;
9176 w->base_line_number = Qnil;
9177 }
9178
9179 finish_menu_bars:
9180
9181 /* When we reach a frame's selected window, redo the frame's menu bar. */
9182 if (update_mode_line
9183 && EQ (FRAME_SELECTED_WINDOW (f), window))
9184 {
9185 int redisplay_menu_p = 0;
9186
9187 if (FRAME_WINDOW_P (f))
9188 {
9189 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
9190 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
9191 #else
9192 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9193 #endif
9194 }
9195 else
9196 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9197
9198 if (redisplay_menu_p)
9199 display_menu_bar (w);
9200
9201 #ifdef HAVE_WINDOW_SYSTEM
9202 if (WINDOWP (f->tool_bar_window)
9203 && (FRAME_TOOL_BAR_LINES (f) > 0
9204 || auto_resize_tool_bars_p))
9205 redisplay_tool_bar (f);
9206 #endif
9207 }
9208
9209 finish_scroll_bars:
9210
9211 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9212 {
9213 int start, end, whole;
9214
9215 /* Calculate the start and end positions for the current window.
9216 At some point, it would be nice to choose between scrollbars
9217 which reflect the whole buffer size, with special markers
9218 indicating narrowing, and scrollbars which reflect only the
9219 visible region.
9220
9221 Note that mini-buffers sometimes aren't displaying any text. */
9222 if (!MINI_WINDOW_P (w)
9223 || (w == XWINDOW (minibuf_window)
9224 && NILP (echo_area_buffer[0])))
9225 {
9226 whole = ZV - BEGV;
9227 start = marker_position (w->start) - BEGV;
9228 /* I don't think this is guaranteed to be right. For the
9229 moment, we'll pretend it is. */
9230 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
9231
9232 if (end < start)
9233 end = start;
9234 if (whole < (end - start))
9235 whole = end - start;
9236 }
9237 else
9238 start = end = whole = 0;
9239
9240 /* Indicate what this scroll bar ought to be displaying now. */
9241 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
9242
9243 /* Note that we actually used the scroll bar attached to this
9244 window, so it shouldn't be deleted at the end of redisplay. */
9245 (*redeem_scroll_bar_hook) (w);
9246 }
9247
9248 restore_buffers:
9249
9250 /* Restore current_buffer and value of point in it. */
9251 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
9252 if (really_switched_buffer)
9253 set_buffer_internal_1 (old);
9254 else
9255 set_buffer_temp (old);
9256 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
9257
9258 unbind_to (count, Qnil);
9259 }
9260
9261
9262 /* Build the complete desired matrix of WINDOW with a window start
9263 buffer position POS. Value is non-zero if successful. It is zero
9264 if fonts were loaded during redisplay which makes re-adjusting
9265 glyph matrices necessary. */
9266
9267 int
9268 try_window (window, pos)
9269 Lisp_Object window;
9270 struct text_pos pos;
9271 {
9272 struct window *w = XWINDOW (window);
9273 struct it it;
9274 struct glyph_row *last_text_row = NULL;
9275
9276 /* Make POS the new window start. */
9277 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
9278
9279 /* Mark cursor position as unknown. No overlay arrow seen. */
9280 w->cursor.vpos = -1;
9281 overlay_arrow_seen = 0;
9282
9283 /* Initialize iterator and info to start at POS. */
9284 start_display (&it, w, pos);
9285
9286 /* Display all lines of W. */
9287 while (it.current_y < it.last_visible_y)
9288 {
9289 if (display_line (&it))
9290 last_text_row = it.glyph_row - 1;
9291 if (fonts_changed_p)
9292 return 0;
9293 }
9294
9295 /* If bottom moved off end of frame, change mode line percentage. */
9296 if (XFASTINT (w->window_end_pos) <= 0
9297 && Z != IT_CHARPOS (it))
9298 w->update_mode_line = Qt;
9299
9300 /* Set window_end_pos to the offset of the last character displayed
9301 on the window from the end of current_buffer. Set
9302 window_end_vpos to its row number. */
9303 if (last_text_row)
9304 {
9305 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9306 w->window_end_bytepos
9307 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9308 XSETFASTINT (w->window_end_pos,
9309 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9310 XSETFASTINT (w->window_end_vpos,
9311 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9312 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9313 ->displays_text_p);
9314 }
9315 else
9316 {
9317 w->window_end_bytepos = 0;
9318 XSETFASTINT (w->window_end_pos, 0);
9319 XSETFASTINT (w->window_end_vpos, 0);
9320 }
9321
9322 /* But that is not valid info until redisplay finishes. */
9323 w->window_end_valid = Qnil;
9324 return 1;
9325 }
9326
9327
9328 \f
9329 /************************************************************************
9330 Window redisplay reusing current matrix when buffer has not changed
9331 ************************************************************************/
9332
9333 /* Try redisplay of window W showing an unchanged buffer with a
9334 different window start than the last time it was displayed by
9335 reusing its current matrix. Value is non-zero if successful.
9336 W->start is the new window start. */
9337
9338 static int
9339 try_window_reusing_current_matrix (w)
9340 struct window *w;
9341 {
9342 struct frame *f = XFRAME (w->frame);
9343 struct glyph_row *row, *bottom_row;
9344 struct it it;
9345 struct run run;
9346 struct text_pos start, new_start;
9347 int nrows_scrolled, i;
9348 struct glyph_row *last_text_row;
9349 struct glyph_row *last_reused_text_row;
9350 struct glyph_row *start_row;
9351 int start_vpos, min_y, max_y;
9352
9353 /* Right now this function doesn't handle terminal frames. */
9354 if (!FRAME_WINDOW_P (f))
9355 return 0;
9356
9357 /* Can't do this if region may have changed. */
9358 if ((!NILP (Vtransient_mark_mode)
9359 && !NILP (current_buffer->mark_active))
9360 || !NILP (w->region_showing)
9361 || !NILP (Vshow_trailing_whitespace))
9362 return 0;
9363
9364 /* If top-line visibility has changed, give up. */
9365 if (WINDOW_WANTS_HEADER_LINE_P (w)
9366 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
9367 return 0;
9368
9369 /* Give up if old or new display is scrolled vertically. We could
9370 make this function handle this, but right now it doesn't. */
9371 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9372 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
9373 return 0;
9374
9375 /* The variable new_start now holds the new window start. The old
9376 start `start' can be determined from the current matrix. */
9377 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
9378 start = start_row->start.pos;
9379 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
9380
9381 /* Clear the desired matrix for the display below. */
9382 clear_glyph_matrix (w->desired_matrix);
9383
9384 if (CHARPOS (new_start) <= CHARPOS (start))
9385 {
9386 int first_row_y;
9387
9388 IF_DEBUG (debug_method_add (w, "twu1"));
9389
9390 /* Display up to a row that can be reused. The variable
9391 last_text_row is set to the last row displayed that displays
9392 text. */
9393 start_display (&it, w, new_start);
9394 first_row_y = it.current_y;
9395 w->cursor.vpos = -1;
9396 last_text_row = last_reused_text_row = NULL;
9397 while (it.current_y < it.last_visible_y
9398 && IT_CHARPOS (it) < CHARPOS (start)
9399 && !fonts_changed_p)
9400 if (display_line (&it))
9401 last_text_row = it.glyph_row - 1;
9402
9403 /* A value of current_y < last_visible_y means that we stopped
9404 at the previous window start, which in turn means that we
9405 have at least one reusable row. */
9406 if (it.current_y < it.last_visible_y)
9407 {
9408 nrows_scrolled = it.vpos;
9409
9410 /* Find PT if not already found in the lines displayed. */
9411 if (w->cursor.vpos < 0)
9412 {
9413 int dy = it.current_y - first_row_y;
9414
9415 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9416 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9417 {
9418 if (PT >= MATRIX_ROW_START_CHARPOS (row)
9419 && PT < MATRIX_ROW_END_CHARPOS (row))
9420 {
9421 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
9422 dy, nrows_scrolled);
9423 break;
9424 }
9425
9426 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
9427 break;
9428
9429 ++row;
9430 }
9431
9432 /* Give up if point was not found. This shouldn't
9433 happen often; not more often than with try_window
9434 itself. */
9435 if (w->cursor.vpos < 0)
9436 {
9437 clear_glyph_matrix (w->desired_matrix);
9438 return 0;
9439 }
9440 }
9441
9442 /* Scroll the display. Do it before the current matrix is
9443 changed. The problem here is that update has not yet
9444 run, i.e. part of the current matrix is not up to date.
9445 scroll_run_hook will clear the cursor, and use the
9446 current matrix to get the height of the row the cursor is
9447 in. */
9448 run.current_y = first_row_y;
9449 run.desired_y = it.current_y;
9450 run.height = it.last_visible_y - it.current_y;
9451 if (run.height > 0
9452 && run.current_y != run.desired_y)
9453 {
9454 update_begin (f);
9455 rif->update_window_begin_hook (w);
9456 rif->scroll_run_hook (w, &run);
9457 rif->update_window_end_hook (w, 0);
9458 update_end (f);
9459 }
9460
9461 /* Shift current matrix down by nrows_scrolled lines. */
9462 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9463 rotate_matrix (w->current_matrix,
9464 start_vpos,
9465 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9466 nrows_scrolled);
9467
9468 /* Disable lines not reused. */
9469 for (i = 0; i < it.vpos; ++i)
9470 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
9471
9472 /* Re-compute Y positions. */
9473 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix) + nrows_scrolled;
9474 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9475 max_y = it.last_visible_y;
9476 while (row < bottom_row)
9477 {
9478 row->y = it.current_y;
9479
9480 if (row->y < min_y)
9481 row->visible_height = row->height - (min_y - row->y);
9482 else if (row->y + row->height > max_y)
9483 row->visible_height
9484 = row->height - (row->y + row->height - max_y);
9485 else
9486 row->visible_height = row->height;
9487
9488 it.current_y += row->height;
9489 ++it.vpos;
9490
9491 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9492 last_reused_text_row = row;
9493 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
9494 break;
9495 ++row;
9496 }
9497 }
9498
9499 /* Update window_end_pos etc.; last_reused_text_row is the last
9500 reused row from the current matrix containing text, if any.
9501 The value of last_text_row is the last displayed line
9502 containing text. */
9503 if (last_reused_text_row)
9504 {
9505 w->window_end_bytepos
9506 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
9507 XSETFASTINT (w->window_end_pos,
9508 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
9509 XSETFASTINT (w->window_end_vpos,
9510 MATRIX_ROW_VPOS (last_reused_text_row,
9511 w->current_matrix));
9512 }
9513 else if (last_text_row)
9514 {
9515 w->window_end_bytepos
9516 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9517 XSETFASTINT (w->window_end_pos,
9518 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9519 XSETFASTINT (w->window_end_vpos,
9520 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9521 }
9522 else
9523 {
9524 /* This window must be completely empty. */
9525 w->window_end_bytepos = 0;
9526 XSETFASTINT (w->window_end_pos, 0);
9527 XSETFASTINT (w->window_end_vpos, 0);
9528 }
9529 w->window_end_valid = Qnil;
9530
9531 /* Update hint: don't try scrolling again in update_window. */
9532 w->desired_matrix->no_scrolling_p = 1;
9533
9534 #if GLYPH_DEBUG
9535 debug_method_add (w, "try_window_reusing_current_matrix 1");
9536 #endif
9537 return 1;
9538 }
9539 else if (CHARPOS (new_start) > CHARPOS (start))
9540 {
9541 struct glyph_row *pt_row, *row;
9542 struct glyph_row *first_reusable_row;
9543 struct glyph_row *first_row_to_display;
9544 int dy;
9545 int yb = window_text_bottom_y (w);
9546
9547 IF_DEBUG (debug_method_add (w, "twu2"));
9548
9549 /* Find the row starting at new_start, if there is one. Don't
9550 reuse a partially visible line at the end. */
9551 first_reusable_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9552 while (first_reusable_row->enabled_p
9553 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
9554 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9555 < CHARPOS (new_start)))
9556 ++first_reusable_row;
9557
9558 /* Give up if there is no row to reuse. */
9559 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
9560 || !first_reusable_row->enabled_p
9561 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9562 != CHARPOS (new_start)))
9563 return 0;
9564
9565 /* We can reuse fully visible rows beginning with
9566 first_reusable_row to the end of the window. Set
9567 first_row_to_display to the first row that cannot be reused.
9568 Set pt_row to the row containing point, if there is any. */
9569 first_row_to_display = first_reusable_row;
9570 pt_row = NULL;
9571 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
9572 {
9573 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
9574 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
9575 pt_row = first_row_to_display;
9576
9577 ++first_row_to_display;
9578 }
9579
9580 /* Start displaying at the start of first_row_to_display. */
9581 xassert (first_row_to_display->y < yb);
9582 init_to_row_start (&it, w, first_row_to_display);
9583 nrows_scrolled = MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix);
9584 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
9585 - nrows_scrolled);
9586 it.current_y = first_row_to_display->y - first_reusable_row->y;
9587
9588 /* Display lines beginning with first_row_to_display in the
9589 desired matrix. Set last_text_row to the last row displayed
9590 that displays text. */
9591 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
9592 if (pt_row == NULL)
9593 w->cursor.vpos = -1;
9594 last_text_row = NULL;
9595 while (it.current_y < it.last_visible_y && !fonts_changed_p)
9596 if (display_line (&it))
9597 last_text_row = it.glyph_row - 1;
9598
9599 /* Give up If point isn't in a row displayed or reused. */
9600 if (w->cursor.vpos < 0)
9601 {
9602 clear_glyph_matrix (w->desired_matrix);
9603 return 0;
9604 }
9605
9606 /* If point is in a reused row, adjust y and vpos of the cursor
9607 position. */
9608 if (pt_row)
9609 {
9610 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
9611 w->current_matrix);
9612 w->cursor.y -= first_reusable_row->y;
9613 }
9614
9615 /* Scroll the display. */
9616 run.current_y = first_reusable_row->y;
9617 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9618 run.height = it.last_visible_y - run.current_y;
9619 if (run.height)
9620 {
9621 struct frame *f = XFRAME (WINDOW_FRAME (w));
9622 update_begin (f);
9623 rif->update_window_begin_hook (w);
9624 rif->scroll_run_hook (w, &run);
9625 rif->update_window_end_hook (w, 0);
9626 update_end (f);
9627 }
9628
9629 /* Adjust Y positions of reused rows. */
9630 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9631 row = first_reusable_row;
9632 dy = first_reusable_row->y;
9633 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9634 max_y = it.last_visible_y;
9635 while (row < first_row_to_display)
9636 {
9637 row->y -= dy;
9638 if (row->y < min_y)
9639 row->visible_height = row->height - (min_y - row->y);
9640 else if (row->y + row->height > max_y)
9641 row->visible_height
9642 = row->height - (row->y + row->height - max_y);
9643 else
9644 row->visible_height = row->height;
9645 ++row;
9646 }
9647
9648 /* Disable rows not reused. */
9649 while (row < bottom_row)
9650 {
9651 row->enabled_p = 0;
9652 ++row;
9653 }
9654
9655 /* Scroll the current matrix. */
9656 xassert (nrows_scrolled > 0);
9657 rotate_matrix (w->current_matrix,
9658 start_vpos,
9659 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9660 -nrows_scrolled);
9661
9662 /* Adjust window end. A null value of last_text_row means that
9663 the window end is in reused rows which in turn means that
9664 only its vpos can have changed. */
9665 if (last_text_row)
9666 {
9667 w->window_end_bytepos
9668 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9669 XSETFASTINT (w->window_end_pos,
9670 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9671 XSETFASTINT (w->window_end_vpos,
9672 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9673 }
9674 else
9675 {
9676 XSETFASTINT (w->window_end_vpos,
9677 XFASTINT (w->window_end_vpos) - nrows_scrolled);
9678 }
9679
9680 w->window_end_valid = Qnil;
9681 w->desired_matrix->no_scrolling_p = 1;
9682
9683 #if GLYPH_DEBUG
9684 debug_method_add (w, "try_window_reusing_current_matrix 2");
9685 #endif
9686 return 1;
9687 }
9688
9689 return 0;
9690 }
9691
9692
9693 \f
9694 /************************************************************************
9695 Window redisplay reusing current matrix when buffer has changed
9696 ************************************************************************/
9697
9698 static struct glyph_row *get_last_unchanged_at_beg_row P_ ((struct window *));
9699 static struct glyph_row *get_first_unchanged_at_end_row P_ ((struct window *,
9700 int *, int *));
9701 static struct glyph_row *
9702 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
9703 struct glyph_row *));
9704
9705
9706 /* Return the last row in MATRIX displaying text. If row START is
9707 non-null, start searching with that row. IT gives the dimensions
9708 of the display. Value is null if matrix is empty; otherwise it is
9709 a pointer to the row found. */
9710
9711 static struct glyph_row *
9712 find_last_row_displaying_text (matrix, it, start)
9713 struct glyph_matrix *matrix;
9714 struct it *it;
9715 struct glyph_row *start;
9716 {
9717 struct glyph_row *row, *row_found;
9718
9719 /* Set row_found to the last row in IT->w's current matrix
9720 displaying text. The loop looks funny but think of partially
9721 visible lines. */
9722 row_found = NULL;
9723 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
9724 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9725 {
9726 xassert (row->enabled_p);
9727 row_found = row;
9728 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
9729 break;
9730 ++row;
9731 }
9732
9733 return row_found;
9734 }
9735
9736
9737 /* Return the last row in the current matrix of W that is not affected
9738 by changes at the start of current_buffer that occurred since the
9739 last time W was redisplayed. Value is null if no such row exists.
9740
9741 The global variable beg_unchanged has to contain the number of
9742 bytes unchanged at the start of current_buffer. BEG +
9743 beg_unchanged is the buffer position of the first changed byte in
9744 current_buffer. Characters at positions < BEG + beg_unchanged are
9745 at the same buffer positions as they were when the current matrix
9746 was built. */
9747
9748 static struct glyph_row *
9749 get_last_unchanged_at_beg_row (w)
9750 struct window *w;
9751 {
9752 int first_changed_pos = BEG + BEG_UNCHANGED;
9753 struct glyph_row *row;
9754 struct glyph_row *row_found = NULL;
9755 int yb = window_text_bottom_y (w);
9756
9757 /* Find the last row displaying unchanged text. */
9758 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9759 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
9760 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
9761 {
9762 if (/* If row ends before first_changed_pos, it is unchanged,
9763 except in some case. */
9764 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
9765 /* When row ends in ZV and we write at ZV it is not
9766 unchanged. */
9767 && !row->ends_at_zv_p
9768 /* When first_changed_pos is the end of a continued line,
9769 row is not unchanged because it may be no longer
9770 continued. */
9771 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
9772 && row->continued_p))
9773 row_found = row;
9774
9775 /* Stop if last visible row. */
9776 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
9777 break;
9778
9779 ++row;
9780 }
9781
9782 return row_found;
9783 }
9784
9785
9786 /* Find the first glyph row in the current matrix of W that is not
9787 affected by changes at the end of current_buffer since the last
9788 time the window was redisplayed. Return in *DELTA the number of
9789 chars by which buffer positions in unchanged text at the end of
9790 current_buffer must be adjusted. Return in *DELTA_BYTES the
9791 corresponding number of bytes. Value is null if no such row
9792 exists, i.e. all rows are affected by changes. */
9793
9794 static struct glyph_row *
9795 get_first_unchanged_at_end_row (w, delta, delta_bytes)
9796 struct window *w;
9797 int *delta, *delta_bytes;
9798 {
9799 struct glyph_row *row;
9800 struct glyph_row *row_found = NULL;
9801
9802 *delta = *delta_bytes = 0;
9803
9804 /* A value of window_end_pos >= end_unchanged means that the window
9805 end is in the range of changed text. If so, there is no
9806 unchanged row at the end of W's current matrix. */
9807 xassert (!NILP (w->window_end_valid));
9808 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
9809 return NULL;
9810
9811 /* Set row to the last row in W's current matrix displaying text. */
9812 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
9813
9814 /* If matrix is entirely empty, no unchanged row exists. */
9815 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9816 {
9817 /* The value of row is the last glyph row in the matrix having a
9818 meaningful buffer position in it. The end position of row
9819 corresponds to window_end_pos. This allows us to translate
9820 buffer positions in the current matrix to current buffer
9821 positions for characters not in changed text. */
9822 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
9823 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
9824 int last_unchanged_pos, last_unchanged_pos_old;
9825 struct glyph_row *first_text_row
9826 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9827
9828 *delta = Z - Z_old;
9829 *delta_bytes = Z_BYTE - Z_BYTE_old;
9830
9831 /* Set last_unchanged_pos to the buffer position of the last
9832 character in the buffer that has not been changed. Z is the
9833 index + 1 of the last byte in current_buffer, i.e. by
9834 subtracting end_unchanged we get the index of the last
9835 unchanged character, and we have to add BEG to get its buffer
9836 position. */
9837 last_unchanged_pos = Z - END_UNCHANGED + BEG;
9838 last_unchanged_pos_old = last_unchanged_pos - *delta;
9839
9840 /* Search backward from ROW for a row displaying a line that
9841 starts at a minimum position >= last_unchanged_pos_old. */
9842 while (row >= first_text_row)
9843 {
9844 xassert (row->enabled_p);
9845 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row));
9846
9847 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
9848 row_found = row;
9849 --row;
9850 }
9851 }
9852
9853 xassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
9854 return row_found;
9855 }
9856
9857
9858 /* Make sure that glyph rows in the current matrix of window W
9859 reference the same glyph memory as corresponding rows in the
9860 frame's frame matrix. This function is called after scrolling W's
9861 current matrix on a terminal frame in try_window_id and
9862 try_window_reusing_current_matrix. */
9863
9864 static void
9865 sync_frame_with_window_matrix_rows (w)
9866 struct window *w;
9867 {
9868 struct frame *f = XFRAME (w->frame);
9869 struct glyph_row *window_row, *window_row_end, *frame_row;
9870
9871 /* Preconditions: W must be a leaf window and full-width. Its frame
9872 must have a frame matrix. */
9873 xassert (NILP (w->hchild) && NILP (w->vchild));
9874 xassert (WINDOW_FULL_WIDTH_P (w));
9875 xassert (!FRAME_WINDOW_P (f));
9876
9877 /* If W is a full-width window, glyph pointers in W's current matrix
9878 have, by definition, to be the same as glyph pointers in the
9879 corresponding frame matrix. */
9880 window_row = w->current_matrix->rows;
9881 window_row_end = window_row + w->current_matrix->nrows;
9882 frame_row = f->current_matrix->rows + XFASTINT (w->top);
9883 while (window_row < window_row_end)
9884 {
9885 int area;
9886
9887 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
9888 frame_row->glyphs[area] = window_row->glyphs[area];
9889
9890 /* Disable frame rows whose corresponding window rows have
9891 been disabled in try_window_id. */
9892 if (!window_row->enabled_p)
9893 frame_row->enabled_p = 0;
9894
9895 ++window_row, ++frame_row;
9896 }
9897 }
9898
9899
9900 /* Find the glyph row in window W containing CHARPOS. Consider all
9901 rows between START and END (not inclusive). END null means search
9902 all rows to the end of the display area of W. Value is the row
9903 containing CHARPOS or null. */
9904
9905 static struct glyph_row *
9906 row_containing_pos (w, charpos, start, end)
9907 struct window *w;
9908 int charpos;
9909 struct glyph_row *start, *end;
9910 {
9911 struct glyph_row *row = start;
9912 int last_y;
9913
9914 /* If we happen to start on a header-line, skip that. */
9915 if (row->mode_line_p)
9916 ++row;
9917
9918 if ((end && row >= end) || !row->enabled_p)
9919 return NULL;
9920
9921 last_y = window_text_bottom_y (w);
9922
9923 while ((end == NULL || row < end)
9924 && (MATRIX_ROW_END_CHARPOS (row) < charpos
9925 /* The end position of a row equals the start
9926 position of the next row. If CHARPOS is there, we
9927 would rather display it in the next line, except
9928 when this line ends in ZV. */
9929 || (MATRIX_ROW_END_CHARPOS (row) == charpos
9930 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
9931 || !row->ends_at_zv_p)))
9932 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9933 ++row;
9934
9935 /* Give up if CHARPOS not found. */
9936 if ((end && row >= end)
9937 || charpos < MATRIX_ROW_START_CHARPOS (row)
9938 || charpos > MATRIX_ROW_END_CHARPOS (row))
9939 row = NULL;
9940
9941 return row;
9942 }
9943
9944
9945 /* Try to redisplay window W by reusing its existing display. W's
9946 current matrix must be up to date when this function is called,
9947 i.e. window_end_valid must not be nil.
9948
9949 Value is
9950
9951 1 if display has been updated
9952 0 if otherwise unsuccessful
9953 -1 if redisplay with same window start is known not to succeed
9954
9955 The following steps are performed:
9956
9957 1. Find the last row in the current matrix of W that is not
9958 affected by changes at the start of current_buffer. If no such row
9959 is found, give up.
9960
9961 2. Find the first row in W's current matrix that is not affected by
9962 changes at the end of current_buffer. Maybe there is no such row.
9963
9964 3. Display lines beginning with the row + 1 found in step 1 to the
9965 row found in step 2 or, if step 2 didn't find a row, to the end of
9966 the window.
9967
9968 4. If cursor is not known to appear on the window, give up.
9969
9970 5. If display stopped at the row found in step 2, scroll the
9971 display and current matrix as needed.
9972
9973 6. Maybe display some lines at the end of W, if we must. This can
9974 happen under various circumstances, like a partially visible line
9975 becoming fully visible, or because newly displayed lines are displayed
9976 in smaller font sizes.
9977
9978 7. Update W's window end information. */
9979
9980 /* Check that window end is what we expect it to be. */
9981
9982 static int
9983 try_window_id (w)
9984 struct window *w;
9985 {
9986 struct frame *f = XFRAME (w->frame);
9987 struct glyph_matrix *current_matrix = w->current_matrix;
9988 struct glyph_matrix *desired_matrix = w->desired_matrix;
9989 struct glyph_row *last_unchanged_at_beg_row;
9990 struct glyph_row *first_unchanged_at_end_row;
9991 struct glyph_row *row;
9992 struct glyph_row *bottom_row;
9993 int bottom_vpos;
9994 struct it it;
9995 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
9996 struct text_pos start_pos;
9997 struct run run;
9998 int first_unchanged_at_end_vpos = 0;
9999 struct glyph_row *last_text_row, *last_text_row_at_end;
10000 struct text_pos start;
10001
10002 SET_TEXT_POS_FROM_MARKER (start, w->start);
10003
10004 /* Check pre-conditions. Window end must be valid, otherwise
10005 the current matrix would not be up to date. */
10006 xassert (!NILP (w->window_end_valid));
10007 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10008 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10009
10010 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10011 only if buffer has really changed. The reason is that the gap is
10012 initially at Z for freshly visited files. The code below would
10013 set end_unchanged to 0 in that case. */
10014 if (MODIFF > SAVE_MODIFF
10015 /* This seems to happen sometimes after saving a buffer. */
10016 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
10017 {
10018 if (GPT - BEG < BEG_UNCHANGED)
10019 BEG_UNCHANGED = GPT - BEG;
10020 if (Z - GPT < END_UNCHANGED)
10021 END_UNCHANGED = Z - GPT;
10022 }
10023
10024 /* If window starts after a line end, and the last change is in
10025 front of that newline, then changes don't affect the display.
10026 This case happens with stealth-fontification. */
10027 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10028 if (CHARPOS (start) > BEGV
10029 && Z - END_UNCHANGED < CHARPOS (start) - 1
10030 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10031 && PT < MATRIX_ROW_END_CHARPOS (row))
10032 {
10033 /* We have to update window end positions because the buffer's
10034 size has changed. */
10035 w->window_end_pos
10036 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10037 w->window_end_bytepos
10038 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10039 return 1;
10040 }
10041
10042 /* Return quickly if changes are all below what is displayed in the
10043 window, and if PT is in the window. */
10044 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
10045 && PT < MATRIX_ROW_END_CHARPOS (row))
10046 {
10047 /* We have to update window end positions because the buffer's
10048 size has changed. */
10049 w->window_end_pos
10050 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10051 w->window_end_bytepos
10052 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10053 return 1;
10054 }
10055
10056 /* Check that window start agrees with the start of the first glyph
10057 row in its current matrix. Check this after we know the window
10058 start is not in changed text, otherwise positions would not be
10059 comparable. */
10060 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10061 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10062 return 0;
10063
10064 /* Compute the position at which we have to start displaying new
10065 lines. Some of the lines at the top of the window might be
10066 reusable because they are not displaying changed text. Find the
10067 last row in W's current matrix not affected by changes at the
10068 start of current_buffer. Value is null if changes start in the
10069 first line of window. */
10070 last_unchanged_at_beg_row = get_last_unchanged_at_beg_row (w);
10071 if (last_unchanged_at_beg_row)
10072 {
10073 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10074 start_pos = it.current.pos;
10075
10076 /* Start displaying new lines in the desired matrix at the same
10077 vpos we would use in the current matrix, i.e. below
10078 last_unchanged_at_beg_row. */
10079 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10080 current_matrix);
10081 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10082 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10083
10084 xassert (it.hpos == 0 && it.current_x == 0);
10085 }
10086 else
10087 {
10088 /* There are no reusable lines at the start of the window.
10089 Start displaying in the first line. */
10090 start_display (&it, w, start);
10091 start_pos = it.current.pos;
10092 }
10093
10094 /* Find the first row that is not affected by changes at the end of
10095 the buffer. Value will be null if there is no unchanged row, in
10096 which case we must redisplay to the end of the window. delta
10097 will be set to the value by which buffer positions beginning with
10098 first_unchanged_at_end_row have to be adjusted due to text
10099 changes. */
10100 first_unchanged_at_end_row
10101 = get_first_unchanged_at_end_row (w, &delta, &delta_bytes);
10102 IF_DEBUG (debug_delta = delta);
10103 IF_DEBUG (debug_delta_bytes = delta_bytes);
10104
10105 /* Set stop_pos to the buffer position up to which we will have to
10106 display new lines. If first_unchanged_at_end_row != NULL, this
10107 is the buffer position of the start of the line displayed in that
10108 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10109 that we don't stop at a buffer position. */
10110 stop_pos = 0;
10111 if (first_unchanged_at_end_row)
10112 {
10113 xassert (last_unchanged_at_beg_row == NULL
10114 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10115
10116 /* If this is a continuation line, move forward to the next one
10117 that isn't. Changes in lines above affect this line.
10118 Caution: this may move first_unchanged_at_end_row to a row
10119 not displaying text. */
10120 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
10121 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10122 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10123 < it.last_visible_y))
10124 ++first_unchanged_at_end_row;
10125
10126 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10127 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10128 >= it.last_visible_y))
10129 first_unchanged_at_end_row = NULL;
10130 else
10131 {
10132 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
10133 + delta);
10134 first_unchanged_at_end_vpos
10135 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
10136 xassert (stop_pos >= Z - END_UNCHANGED);
10137 }
10138 }
10139 else if (last_unchanged_at_beg_row == NULL)
10140 return 0;
10141
10142
10143 #if GLYPH_DEBUG
10144
10145 /* Either there is no unchanged row at the end, or the one we have
10146 now displays text. This is a necessary condition for the window
10147 end pos calculation at the end of this function. */
10148 xassert (first_unchanged_at_end_row == NULL
10149 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
10150
10151 debug_last_unchanged_at_beg_vpos
10152 = (last_unchanged_at_beg_row
10153 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
10154 : -1);
10155 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
10156
10157 #endif /* GLYPH_DEBUG != 0 */
10158
10159
10160 /* Display new lines. Set last_text_row to the last new line
10161 displayed which has text on it, i.e. might end up as being the
10162 line where the window_end_vpos is. */
10163 w->cursor.vpos = -1;
10164 last_text_row = NULL;
10165 overlay_arrow_seen = 0;
10166 while (it.current_y < it.last_visible_y
10167 && !fonts_changed_p
10168 && (first_unchanged_at_end_row == NULL
10169 || IT_CHARPOS (it) < stop_pos))
10170 {
10171 if (display_line (&it))
10172 last_text_row = it.glyph_row - 1;
10173 }
10174
10175 if (fonts_changed_p)
10176 return -1;
10177
10178
10179 /* Compute differences in buffer positions, y-positions etc. for
10180 lines reused at the bottom of the window. Compute what we can
10181 scroll. */
10182 if (first_unchanged_at_end_row
10183 /* No lines reused because we displayed everything up to the
10184 bottom of the window. */
10185 && it.current_y < it.last_visible_y)
10186 {
10187 dvpos = (it.vpos
10188 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10189 current_matrix));
10190 dy = it.current_y - first_unchanged_at_end_row->y;
10191 run.current_y = first_unchanged_at_end_row->y;
10192 run.desired_y = run.current_y + dy;
10193 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10194 }
10195 else
10196 {
10197 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10198 first_unchanged_at_end_row = NULL;
10199 }
10200 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10201
10202
10203 /* Find the cursor if not already found. We have to decide whether
10204 PT will appear on this window (it sometimes doesn't, but this is
10205 not a very frequent case.) This decision has to be made before
10206 the current matrix is altered. A value of cursor.vpos < 0 means
10207 that PT is either in one of the lines beginning at
10208 first_unchanged_at_end_row or below the window. Don't care for
10209 lines that might be displayed later at the window end; as
10210 mentioned, this is not a frequent case. */
10211 if (w->cursor.vpos < 0)
10212 {
10213 /* Cursor in unchanged rows at the top? */
10214 if (PT < CHARPOS (start_pos)
10215 && last_unchanged_at_beg_row)
10216 {
10217 row = row_containing_pos (w, PT,
10218 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
10219 last_unchanged_at_beg_row + 1);
10220 xassert (row && row <= last_unchanged_at_beg_row);
10221 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10222 }
10223
10224 /* Start from first_unchanged_at_end_row looking for PT. */
10225 else if (first_unchanged_at_end_row)
10226 {
10227 row = row_containing_pos (w, PT - delta,
10228 first_unchanged_at_end_row, NULL);
10229 if (row)
10230 set_cursor_from_row (w, row, w->current_matrix, delta,
10231 delta_bytes, dy, dvpos);
10232 }
10233
10234 /* Give up if cursor was not found. */
10235 if (w->cursor.vpos < 0)
10236 {
10237 clear_glyph_matrix (w->desired_matrix);
10238 return -1;
10239 }
10240 }
10241
10242 /* Don't let the cursor end in the scroll margins. */
10243 {
10244 int this_scroll_margin, cursor_height;
10245
10246 this_scroll_margin = max (0, scroll_margin);
10247 this_scroll_margin = min (this_scroll_margin,
10248 XFASTINT (w->height) / 4);
10249 this_scroll_margin *= CANON_Y_UNIT (it.f);
10250 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
10251
10252 if ((w->cursor.y < this_scroll_margin
10253 && CHARPOS (start) > BEGV)
10254 /* Don't take scroll margin into account at the bottom because
10255 old redisplay didn't do it either. */
10256 || w->cursor.y + cursor_height > it.last_visible_y)
10257 {
10258 w->cursor.vpos = -1;
10259 clear_glyph_matrix (w->desired_matrix);
10260 return -1;
10261 }
10262 }
10263
10264 /* Scroll the display. Do it before changing the current matrix so
10265 that xterm.c doesn't get confused about where the cursor glyph is
10266 found. */
10267 if (dy)
10268 {
10269 update_begin (f);
10270
10271 if (FRAME_WINDOW_P (f))
10272 {
10273 rif->update_window_begin_hook (w);
10274 rif->scroll_run_hook (w, &run);
10275 rif->update_window_end_hook (w, 0);
10276 }
10277 else
10278 {
10279 /* Terminal frame. In this case, dvpos gives the number of
10280 lines to scroll by; dvpos < 0 means scroll up. */
10281 int first_unchanged_at_end_vpos
10282 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
10283 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
10284 int end = XFASTINT (w->top) + window_internal_height (w);
10285
10286 /* Perform the operation on the screen. */
10287 if (dvpos > 0)
10288 {
10289 /* Scroll last_unchanged_at_beg_row to the end of the
10290 window down dvpos lines. */
10291 set_terminal_window (end);
10292
10293 /* On dumb terminals delete dvpos lines at the end
10294 before inserting dvpos empty lines. */
10295 if (!scroll_region_ok)
10296 ins_del_lines (end - dvpos, -dvpos);
10297
10298 /* Insert dvpos empty lines in front of
10299 last_unchanged_at_beg_row. */
10300 ins_del_lines (from, dvpos);
10301 }
10302 else if (dvpos < 0)
10303 {
10304 /* Scroll up last_unchanged_at_beg_vpos to the end of
10305 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10306 set_terminal_window (end);
10307
10308 /* Delete dvpos lines in front of
10309 last_unchanged_at_beg_vpos. ins_del_lines will set
10310 the cursor to the given vpos and emit |dvpos| delete
10311 line sequences. */
10312 ins_del_lines (from + dvpos, dvpos);
10313
10314 /* On a dumb terminal insert dvpos empty lines at the
10315 end. */
10316 if (!scroll_region_ok)
10317 ins_del_lines (end + dvpos, -dvpos);
10318 }
10319
10320 set_terminal_window (0);
10321 }
10322
10323 update_end (f);
10324 }
10325
10326 /* Shift reused rows of the current matrix to the right position.
10327 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
10328 text. */
10329 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10330 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
10331 if (dvpos < 0)
10332 {
10333 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
10334 bottom_vpos, dvpos);
10335 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
10336 bottom_vpos, 0);
10337 }
10338 else if (dvpos > 0)
10339 {
10340 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
10341 bottom_vpos, dvpos);
10342 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
10343 first_unchanged_at_end_vpos + dvpos, 0);
10344 }
10345
10346 /* For frame-based redisplay, make sure that current frame and window
10347 matrix are in sync with respect to glyph memory. */
10348 if (!FRAME_WINDOW_P (f))
10349 sync_frame_with_window_matrix_rows (w);
10350
10351 /* Adjust buffer positions in reused rows. */
10352 if (delta)
10353 increment_glyph_matrix_buffer_positions (current_matrix,
10354 first_unchanged_at_end_vpos + dvpos,
10355 bottom_vpos, delta, delta_bytes);
10356
10357 /* Adjust Y positions. */
10358 if (dy)
10359 shift_glyph_matrix (w, current_matrix,
10360 first_unchanged_at_end_vpos + dvpos,
10361 bottom_vpos, dy);
10362
10363 if (first_unchanged_at_end_row)
10364 first_unchanged_at_end_row += dvpos;
10365
10366 /* If scrolling up, there may be some lines to display at the end of
10367 the window. */
10368 last_text_row_at_end = NULL;
10369 if (dy < 0)
10370 {
10371 /* Set last_row to the glyph row in the current matrix where the
10372 window end line is found. It has been moved up or down in
10373 the matrix by dvpos. */
10374 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
10375 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
10376
10377 /* If last_row is the window end line, it should display text. */
10378 xassert (last_row->displays_text_p);
10379
10380 /* If window end line was partially visible before, begin
10381 displaying at that line. Otherwise begin displaying with the
10382 line following it. */
10383 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
10384 {
10385 init_to_row_start (&it, w, last_row);
10386 it.vpos = last_vpos;
10387 it.current_y = last_row->y;
10388 }
10389 else
10390 {
10391 init_to_row_end (&it, w, last_row);
10392 it.vpos = 1 + last_vpos;
10393 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
10394 ++last_row;
10395 }
10396
10397 /* We may start in a continuation line. If so, we have to get
10398 the right continuation_lines_width and current_x. */
10399 it.continuation_lines_width = last_row->continuation_lines_width;
10400 it.hpos = it.current_x = 0;
10401
10402 /* Display the rest of the lines at the window end. */
10403 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10404 while (it.current_y < it.last_visible_y
10405 && !fonts_changed_p)
10406 {
10407 /* Is it always sure that the display agrees with lines in
10408 the current matrix? I don't think so, so we mark rows
10409 displayed invalid in the current matrix by setting their
10410 enabled_p flag to zero. */
10411 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
10412 if (display_line (&it))
10413 last_text_row_at_end = it.glyph_row - 1;
10414 }
10415 }
10416
10417 /* Update window_end_pos and window_end_vpos. */
10418 if (first_unchanged_at_end_row
10419 && first_unchanged_at_end_row->y < it.last_visible_y
10420 && !last_text_row_at_end)
10421 {
10422 /* Window end line if one of the preserved rows from the current
10423 matrix. Set row to the last row displaying text in current
10424 matrix starting at first_unchanged_at_end_row, after
10425 scrolling. */
10426 xassert (first_unchanged_at_end_row->displays_text_p);
10427 row = find_last_row_displaying_text (w->current_matrix, &it,
10428 first_unchanged_at_end_row);
10429 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
10430
10431 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
10432 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10433 XSETFASTINT (w->window_end_vpos,
10434 MATRIX_ROW_VPOS (row, w->current_matrix));
10435 }
10436 else if (last_text_row_at_end)
10437 {
10438 XSETFASTINT (w->window_end_pos,
10439 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
10440 w->window_end_bytepos
10441 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
10442 XSETFASTINT (w->window_end_vpos,
10443 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
10444 }
10445 else if (last_text_row)
10446 {
10447 /* We have displayed either to the end of the window or at the
10448 end of the window, i.e. the last row with text is to be found
10449 in the desired matrix. */
10450 XSETFASTINT (w->window_end_pos,
10451 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10452 w->window_end_bytepos
10453 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10454 XSETFASTINT (w->window_end_vpos,
10455 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
10456 }
10457 else if (first_unchanged_at_end_row == NULL
10458 && last_text_row == NULL
10459 && last_text_row_at_end == NULL)
10460 {
10461 /* Displayed to end of window, but no line containing text was
10462 displayed. Lines were deleted at the end of the window. */
10463 int vpos;
10464 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
10465
10466 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
10467 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
10468 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
10469 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
10470 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
10471 break;
10472
10473 w->window_end_vpos = make_number (vpos);
10474 }
10475 else
10476 abort ();
10477
10478 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
10479 debug_end_vpos = XFASTINT (w->window_end_vpos));
10480
10481 /* Record that display has not been completed. */
10482 w->window_end_valid = Qnil;
10483 w->desired_matrix->no_scrolling_p = 1;
10484 return 1;
10485 }
10486
10487
10488 \f
10489 /***********************************************************************
10490 More debugging support
10491 ***********************************************************************/
10492
10493 #if GLYPH_DEBUG
10494
10495 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
10496 static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
10497
10498
10499 /* Dump the contents of glyph matrix MATRIX on stderr. If
10500 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
10501
10502 void
10503 dump_glyph_matrix (matrix, with_glyphs_p)
10504 struct glyph_matrix *matrix;
10505 int with_glyphs_p;
10506 {
10507 int i;
10508 for (i = 0; i < matrix->nrows; ++i)
10509 dump_glyph_row (matrix, i, with_glyphs_p);
10510 }
10511
10512
10513 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
10514 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
10515
10516 void
10517 dump_glyph_row (matrix, vpos, with_glyphs_p)
10518 struct glyph_matrix *matrix;
10519 int vpos, with_glyphs_p;
10520 {
10521 struct glyph_row *row;
10522
10523 if (vpos < 0 || vpos >= matrix->nrows)
10524 return;
10525
10526 row = MATRIX_ROW (matrix, vpos);
10527
10528 fprintf (stderr, "Row Start End Used oEI><O\\CTZF X Y W\n");
10529 fprintf (stderr, "=============================================\n");
10530
10531 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d\n",
10532 row - matrix->rows,
10533 MATRIX_ROW_START_CHARPOS (row),
10534 MATRIX_ROW_END_CHARPOS (row),
10535 row->used[TEXT_AREA],
10536 row->contains_overlapping_glyphs_p,
10537 row->enabled_p,
10538 row->inverse_p,
10539 row->truncated_on_left_p,
10540 row->truncated_on_right_p,
10541 row->overlay_arrow_p,
10542 row->continued_p,
10543 MATRIX_ROW_CONTINUATION_LINE_P (row),
10544 row->displays_text_p,
10545 row->ends_at_zv_p,
10546 row->fill_line_p,
10547 row->x,
10548 row->y,
10549 row->pixel_width);
10550 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
10551 row->end.overlay_string_index);
10552 fprintf (stderr, "%9d %5d\n",
10553 CHARPOS (row->start.string_pos),
10554 CHARPOS (row->end.string_pos));
10555 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
10556 row->end.dpvec_index);
10557
10558 if (with_glyphs_p)
10559 {
10560 struct glyph *glyph, *glyph_end;
10561 int prev_had_glyphs_p;
10562
10563 glyph = row->glyphs[TEXT_AREA];
10564 glyph_end = glyph + row->used[TEXT_AREA];
10565
10566 /* Glyph for a line end in text. */
10567 if (glyph == glyph_end && glyph->charpos > 0)
10568 ++glyph_end;
10569
10570 if (glyph < glyph_end)
10571 {
10572 fprintf (stderr, " Glyph Type Pos W Code C Face LR\n");
10573 prev_had_glyphs_p = 1;
10574 }
10575 else
10576 prev_had_glyphs_p = 0;
10577
10578 while (glyph < glyph_end)
10579 {
10580 if (glyph->type == CHAR_GLYPH)
10581 {
10582 fprintf (stderr,
10583 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10584 glyph - row->glyphs[TEXT_AREA],
10585 'C',
10586 glyph->charpos,
10587 glyph->pixel_width,
10588 glyph->u.ch,
10589 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
10590 ? glyph->u.ch
10591 : '.'),
10592 glyph->face_id,
10593 glyph->left_box_line_p,
10594 glyph->right_box_line_p);
10595 }
10596 else if (glyph->type == STRETCH_GLYPH)
10597 {
10598 fprintf (stderr,
10599 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10600 glyph - row->glyphs[TEXT_AREA],
10601 'S',
10602 glyph->charpos,
10603 glyph->pixel_width,
10604 0,
10605 '.',
10606 glyph->face_id,
10607 glyph->left_box_line_p,
10608 glyph->right_box_line_p);
10609 }
10610 else if (glyph->type == IMAGE_GLYPH)
10611 {
10612 fprintf (stderr,
10613 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
10614 glyph - row->glyphs[TEXT_AREA],
10615 'I',
10616 glyph->charpos,
10617 glyph->pixel_width,
10618 glyph->u.img_id,
10619 '.',
10620 glyph->face_id,
10621 glyph->left_box_line_p,
10622 glyph->right_box_line_p);
10623 }
10624 ++glyph;
10625 }
10626 }
10627 }
10628
10629
10630 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
10631 Sdump_glyph_matrix, 0, 1, "p",
10632 "Dump the current matrix of the selected window to stderr.\n\
10633 Shows contents of glyph row structures. With non-nil optional\n\
10634 parameter WITH-GLYPHS-P, dump glyphs as well.")
10635 (with_glyphs_p)
10636 {
10637 struct window *w = XWINDOW (selected_window);
10638 struct buffer *buffer = XBUFFER (w->buffer);
10639
10640 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
10641 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
10642 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
10643 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
10644 fprintf (stderr, "=============================================\n");
10645 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
10646 return Qnil;
10647 }
10648
10649
10650 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
10651 "Dump glyph row ROW to stderr.")
10652 (row)
10653 Lisp_Object row;
10654 {
10655 CHECK_NUMBER (row, 0);
10656 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
10657 return Qnil;
10658 }
10659
10660
10661 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
10662 0, 0, "", "")
10663 ()
10664 {
10665 struct frame *sf = SELECTED_FRAME ();
10666 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
10667 ->current_matrix);
10668 dump_glyph_row (m, 0, 1);
10669 return Qnil;
10670 }
10671
10672
10673 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
10674 Strace_redisplay_toggle, 0, 0, "",
10675 "Toggle tracing of redisplay.")
10676 ()
10677 {
10678 trace_redisplay_p = !trace_redisplay_p;
10679 return Qnil;
10680 }
10681
10682
10683 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
10684 "Print STRING to stderr.")
10685 (string)
10686 Lisp_Object string;
10687 {
10688 CHECK_STRING (string, 0);
10689 fprintf (stderr, "%s", XSTRING (string)->data);
10690 return Qnil;
10691 }
10692
10693 #endif /* GLYPH_DEBUG */
10694
10695
10696 \f
10697 /***********************************************************************
10698 Building Desired Matrix Rows
10699 ***********************************************************************/
10700
10701 /* Return a temporary glyph row holding the glyphs of an overlay
10702 arrow. Only used for non-window-redisplay windows. */
10703
10704 static struct glyph_row *
10705 get_overlay_arrow_glyph_row (w)
10706 struct window *w;
10707 {
10708 struct frame *f = XFRAME (WINDOW_FRAME (w));
10709 struct buffer *buffer = XBUFFER (w->buffer);
10710 struct buffer *old = current_buffer;
10711 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
10712 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
10713 unsigned char *arrow_end = arrow_string + arrow_len;
10714 unsigned char *p;
10715 struct it it;
10716 int multibyte_p;
10717 int n_glyphs_before;
10718
10719 set_buffer_temp (buffer);
10720 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
10721 it.glyph_row->used[TEXT_AREA] = 0;
10722 SET_TEXT_POS (it.position, 0, 0);
10723
10724 multibyte_p = !NILP (buffer->enable_multibyte_characters);
10725 p = arrow_string;
10726 while (p < arrow_end)
10727 {
10728 Lisp_Object face, ilisp;
10729
10730 /* Get the next character. */
10731 if (multibyte_p)
10732 it.c = string_char_and_length (p, arrow_len, &it.len);
10733 else
10734 it.c = *p, it.len = 1;
10735 p += it.len;
10736
10737 /* Get its face. */
10738 XSETFASTINT (ilisp, p - arrow_string);
10739 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
10740 it.face_id = compute_char_face (f, it.c, face);
10741
10742 /* Compute its width, get its glyphs. */
10743 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
10744 SET_TEXT_POS (it.position, -1, -1);
10745 PRODUCE_GLYPHS (&it);
10746
10747 /* If this character doesn't fit any more in the line, we have
10748 to remove some glyphs. */
10749 if (it.current_x > it.last_visible_x)
10750 {
10751 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
10752 break;
10753 }
10754 }
10755
10756 set_buffer_temp (old);
10757 return it.glyph_row;
10758 }
10759
10760
10761 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
10762 glyphs are only inserted for terminal frames since we can't really
10763 win with truncation glyphs when partially visible glyphs are
10764 involved. Which glyphs to insert is determined by
10765 produce_special_glyphs. */
10766
10767 static void
10768 insert_left_trunc_glyphs (it)
10769 struct it *it;
10770 {
10771 struct it truncate_it;
10772 struct glyph *from, *end, *to, *toend;
10773
10774 xassert (!FRAME_WINDOW_P (it->f));
10775
10776 /* Get the truncation glyphs. */
10777 truncate_it = *it;
10778 truncate_it.current_x = 0;
10779 truncate_it.face_id = DEFAULT_FACE_ID;
10780 truncate_it.glyph_row = &scratch_glyph_row;
10781 truncate_it.glyph_row->used[TEXT_AREA] = 0;
10782 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
10783 truncate_it.object = make_number (0);
10784 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
10785
10786 /* Overwrite glyphs from IT with truncation glyphs. */
10787 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
10788 end = from + truncate_it.glyph_row->used[TEXT_AREA];
10789 to = it->glyph_row->glyphs[TEXT_AREA];
10790 toend = to + it->glyph_row->used[TEXT_AREA];
10791
10792 while (from < end)
10793 *to++ = *from++;
10794
10795 /* There may be padding glyphs left over. Remove them. */
10796 from = to;
10797 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
10798 ++from;
10799 while (from < toend)
10800 *to++ = *from++;
10801
10802 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
10803 }
10804
10805
10806 /* Compute the pixel height and width of IT->glyph_row.
10807
10808 Most of the time, ascent and height of a display line will be equal
10809 to the max_ascent and max_height values of the display iterator
10810 structure. This is not the case if
10811
10812 1. We hit ZV without displaying anything. In this case, max_ascent
10813 and max_height will be zero.
10814
10815 2. We have some glyphs that don't contribute to the line height.
10816 (The glyph row flag contributes_to_line_height_p is for future
10817 pixmap extensions).
10818
10819 The first case is easily covered by using default values because in
10820 these cases, the line height does not really matter, except that it
10821 must not be zero. */
10822
10823 static void
10824 compute_line_metrics (it)
10825 struct it *it;
10826 {
10827 struct glyph_row *row = it->glyph_row;
10828 int area, i;
10829
10830 if (FRAME_WINDOW_P (it->f))
10831 {
10832 int i, header_line_height;
10833
10834 /* The line may consist of one space only, that was added to
10835 place the cursor on it. If so, the row's height hasn't been
10836 computed yet. */
10837 if (row->height == 0)
10838 {
10839 if (it->max_ascent + it->max_descent == 0)
10840 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
10841 row->ascent = it->max_ascent;
10842 row->height = it->max_ascent + it->max_descent;
10843 row->phys_ascent = it->max_phys_ascent;
10844 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
10845 }
10846
10847 /* Compute the width of this line. */
10848 row->pixel_width = row->x;
10849 for (i = 0; i < row->used[TEXT_AREA]; ++i)
10850 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
10851
10852 xassert (row->pixel_width >= 0);
10853 xassert (row->ascent >= 0 && row->height > 0);
10854
10855 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
10856 || MATRIX_ROW_OVERLAPS_PRED_P (row));
10857
10858 /* If first line's physical ascent is larger than its logical
10859 ascent, use the physical ascent, and make the row taller.
10860 This makes accented characters fully visible. */
10861 if (row == it->w->desired_matrix->rows
10862 && row->phys_ascent > row->ascent)
10863 {
10864 row->height += row->phys_ascent - row->ascent;
10865 row->ascent = row->phys_ascent;
10866 }
10867
10868 /* Compute how much of the line is visible. */
10869 row->visible_height = row->height;
10870
10871 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
10872 if (row->y < header_line_height)
10873 row->visible_height -= header_line_height - row->y;
10874 else
10875 {
10876 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
10877 if (row->y + row->height > max_y)
10878 row->visible_height -= row->y + row->height - max_y;
10879 }
10880 }
10881 else
10882 {
10883 row->pixel_width = row->used[TEXT_AREA];
10884 row->ascent = row->phys_ascent = 0;
10885 row->height = row->phys_height = row->visible_height = 1;
10886 }
10887
10888 /* Compute a hash code for this row. */
10889 row->hash = 0;
10890 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
10891 for (i = 0; i < row->used[area]; ++i)
10892 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
10893 + row->glyphs[area][i].u.val
10894 + row->glyphs[area][i].face_id
10895 + row->glyphs[area][i].padding_p
10896 + (row->glyphs[area][i].type << 2));
10897
10898 it->max_ascent = it->max_descent = 0;
10899 it->max_phys_ascent = it->max_phys_descent = 0;
10900 }
10901
10902
10903 /* Append one space to the glyph row of iterator IT if doing a
10904 window-based redisplay. DEFAULT_FACE_P non-zero means let the
10905 space have the default face, otherwise let it have the same face as
10906 IT->face_id. Value is non-zero if a space was added.
10907
10908 This function is called to make sure that there is always one glyph
10909 at the end of a glyph row that the cursor can be set on under
10910 window-systems. (If there weren't such a glyph we would not know
10911 how wide and tall a box cursor should be displayed).
10912
10913 At the same time this space let's a nicely handle clearing to the
10914 end of the line if the row ends in italic text. */
10915
10916 static int
10917 append_space (it, default_face_p)
10918 struct it *it;
10919 int default_face_p;
10920 {
10921 if (FRAME_WINDOW_P (it->f))
10922 {
10923 int n = it->glyph_row->used[TEXT_AREA];
10924
10925 if (it->glyph_row->glyphs[TEXT_AREA] + n
10926 < it->glyph_row->glyphs[1 + TEXT_AREA])
10927 {
10928 /* Save some values that must not be changed. */
10929 int saved_x = it->current_x;
10930 struct text_pos saved_pos;
10931 int saved_what = it->what;
10932 int saved_face_id = it->face_id;
10933 Lisp_Object saved_object;
10934 struct face *face;
10935
10936 saved_object = it->object;
10937 saved_pos = it->position;
10938
10939 it->what = IT_CHARACTER;
10940 bzero (&it->position, sizeof it->position);
10941 it->object = make_number (0);
10942 it->c = ' ';
10943 it->len = 1;
10944
10945 if (default_face_p)
10946 it->face_id = DEFAULT_FACE_ID;
10947 face = FACE_FROM_ID (it->f, it->face_id);
10948 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
10949
10950 PRODUCE_GLYPHS (it);
10951
10952 it->current_x = saved_x;
10953 it->object = saved_object;
10954 it->position = saved_pos;
10955 it->what = saved_what;
10956 it->face_id = saved_face_id;
10957 return 1;
10958 }
10959 }
10960
10961 return 0;
10962 }
10963
10964
10965 /* Extend the face of the last glyph in the text area of IT->glyph_row
10966 to the end of the display line. Called from display_line.
10967 If the glyph row is empty, add a space glyph to it so that we
10968 know the face to draw. Set the glyph row flag fill_line_p. */
10969
10970 static void
10971 extend_face_to_end_of_line (it)
10972 struct it *it;
10973 {
10974 struct face *face;
10975 struct frame *f = it->f;
10976
10977 /* If line is already filled, do nothing. */
10978 if (it->current_x >= it->last_visible_x)
10979 return;
10980
10981 /* Face extension extends the background and box of IT->face_id
10982 to the end of the line. If the background equals the background
10983 of the frame, we haven't to do anything. */
10984 face = FACE_FROM_ID (f, it->face_id);
10985 if (FRAME_WINDOW_P (f)
10986 && face->box == FACE_NO_BOX
10987 && face->background == FRAME_BACKGROUND_PIXEL (f)
10988 && !face->stipple)
10989 return;
10990
10991 /* Set the glyph row flag indicating that the face of the last glyph
10992 in the text area has to be drawn to the end of the text area. */
10993 it->glyph_row->fill_line_p = 1;
10994
10995 /* If current character of IT is not ASCII, make sure we have the
10996 ASCII face. This will be automatically undone the next time
10997 get_next_display_element returns a multibyte character. Note
10998 that the character will always be single byte in unibyte text. */
10999 if (!SINGLE_BYTE_CHAR_P (it->c))
11000 {
11001 it->face_id = FACE_FOR_CHAR (f, face, 0);
11002 }
11003
11004 if (FRAME_WINDOW_P (f))
11005 {
11006 /* If the row is empty, add a space with the current face of IT,
11007 so that we know which face to draw. */
11008 if (it->glyph_row->used[TEXT_AREA] == 0)
11009 {
11010 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
11011 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
11012 it->glyph_row->used[TEXT_AREA] = 1;
11013 }
11014 }
11015 else
11016 {
11017 /* Save some values that must not be changed. */
11018 int saved_x = it->current_x;
11019 struct text_pos saved_pos;
11020 Lisp_Object saved_object;
11021 int saved_what = it->what;
11022
11023 saved_object = it->object;
11024 saved_pos = it->position;
11025
11026 it->what = IT_CHARACTER;
11027 bzero (&it->position, sizeof it->position);
11028 it->object = make_number (0);
11029 it->c = ' ';
11030 it->len = 1;
11031
11032 PRODUCE_GLYPHS (it);
11033
11034 while (it->current_x <= it->last_visible_x)
11035 PRODUCE_GLYPHS (it);
11036
11037 /* Don't count these blanks really. It would let us insert a left
11038 truncation glyph below and make us set the cursor on them, maybe. */
11039 it->current_x = saved_x;
11040 it->object = saved_object;
11041 it->position = saved_pos;
11042 it->what = saved_what;
11043 }
11044 }
11045
11046
11047 /* Value is non-zero if text starting at CHARPOS in current_buffer is
11048 trailing whitespace. */
11049
11050 static int
11051 trailing_whitespace_p (charpos)
11052 int charpos;
11053 {
11054 int bytepos = CHAR_TO_BYTE (charpos);
11055 int c = 0;
11056
11057 while (bytepos < ZV_BYTE
11058 && (c = FETCH_CHAR (bytepos),
11059 c == ' ' || c == '\t'))
11060 ++bytepos;
11061
11062 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
11063 {
11064 if (bytepos != PT_BYTE)
11065 return 1;
11066 }
11067 return 0;
11068 }
11069
11070
11071 /* Highlight trailing whitespace, if any, in ROW. */
11072
11073 void
11074 highlight_trailing_whitespace (f, row)
11075 struct frame *f;
11076 struct glyph_row *row;
11077 {
11078 int used = row->used[TEXT_AREA];
11079
11080 if (used)
11081 {
11082 struct glyph *start = row->glyphs[TEXT_AREA];
11083 struct glyph *glyph = start + used - 1;
11084
11085 /* Skip over the space glyph inserted to display the
11086 cursor at the end of a line. */
11087 if (glyph->type == CHAR_GLYPH
11088 && glyph->u.ch == ' '
11089 && INTEGERP (glyph->object))
11090 --glyph;
11091
11092 /* If last glyph is a space or stretch, and it's trailing
11093 whitespace, set the face of all trailing whitespace glyphs in
11094 IT->glyph_row to `trailing-whitespace'. */
11095 if (glyph >= start
11096 && BUFFERP (glyph->object)
11097 && (glyph->type == STRETCH_GLYPH
11098 || (glyph->type == CHAR_GLYPH
11099 && glyph->u.ch == ' '))
11100 && trailing_whitespace_p (glyph->charpos))
11101 {
11102 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
11103
11104 while (glyph >= start
11105 && BUFFERP (glyph->object)
11106 && (glyph->type == STRETCH_GLYPH
11107 || (glyph->type == CHAR_GLYPH
11108 && glyph->u.ch == ' ')))
11109 (glyph--)->face_id = face_id;
11110 }
11111 }
11112 }
11113
11114
11115 /* Construct the glyph row IT->glyph_row in the desired matrix of
11116 IT->w from text at the current position of IT. See dispextern.h
11117 for an overview of struct it. Value is non-zero if
11118 IT->glyph_row displays text, as opposed to a line displaying ZV
11119 only. */
11120
11121 static int
11122 display_line (it)
11123 struct it *it;
11124 {
11125 struct glyph_row *row = it->glyph_row;
11126
11127 /* We always start displaying at hpos zero even if hscrolled. */
11128 xassert (it->hpos == 0 && it->current_x == 0);
11129
11130 /* We must not display in a row that's not a text row. */
11131 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
11132 < it->w->desired_matrix->nrows);
11133
11134 /* Is IT->w showing the region? */
11135 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
11136
11137 /* Clear the result glyph row and enable it. */
11138 prepare_desired_row (row);
11139
11140 row->y = it->current_y;
11141 row->start = it->current;
11142 row->continuation_lines_width = it->continuation_lines_width;
11143 row->displays_text_p = 1;
11144
11145 /* Arrange the overlays nicely for our purposes. Usually, we call
11146 display_line on only one line at a time, in which case this
11147 can't really hurt too much, or we call it on lines which appear
11148 one after another in the buffer, in which case all calls to
11149 recenter_overlay_lists but the first will be pretty cheap. */
11150 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
11151
11152 /* Move over display elements that are not visible because we are
11153 hscrolled. This may stop at an x-position < IT->first_visible_x
11154 if the first glyph is partially visible or if we hit a line end. */
11155 if (it->current_x < it->first_visible_x)
11156 move_it_in_display_line_to (it, ZV, it->first_visible_x,
11157 MOVE_TO_POS | MOVE_TO_X);
11158
11159 /* Get the initial row height. This is either the height of the
11160 text hscrolled, if there is any, or zero. */
11161 row->ascent = it->max_ascent;
11162 row->height = it->max_ascent + it->max_descent;
11163 row->phys_ascent = it->max_phys_ascent;
11164 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11165
11166 /* Loop generating characters. The loop is left with IT on the next
11167 character to display. */
11168 while (1)
11169 {
11170 int n_glyphs_before, hpos_before, x_before;
11171 int x, i, nglyphs;
11172
11173 /* Retrieve the next thing to display. Value is zero if end of
11174 buffer reached. */
11175 if (!get_next_display_element (it))
11176 {
11177 /* Maybe add a space at the end of this line that is used to
11178 display the cursor there under X. Set the charpos of the
11179 first glyph of blank lines not corresponding to any text
11180 to -1. */
11181 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
11182 || row->used[TEXT_AREA] == 0)
11183 {
11184 row->glyphs[TEXT_AREA]->charpos = -1;
11185 row->displays_text_p = 0;
11186
11187 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
11188 row->indicate_empty_line_p = 1;
11189 }
11190
11191 it->continuation_lines_width = 0;
11192 row->ends_at_zv_p = 1;
11193 break;
11194 }
11195
11196 /* Now, get the metrics of what we want to display. This also
11197 generates glyphs in `row' (which is IT->glyph_row). */
11198 n_glyphs_before = row->used[TEXT_AREA];
11199 x = it->current_x;
11200 PRODUCE_GLYPHS (it);
11201
11202 /* If this display element was in marginal areas, continue with
11203 the next one. */
11204 if (it->area != TEXT_AREA)
11205 {
11206 row->ascent = max (row->ascent, it->max_ascent);
11207 row->height = max (row->height, it->max_ascent + it->max_descent);
11208 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11209 row->phys_height = max (row->phys_height,
11210 it->max_phys_ascent + it->max_phys_descent);
11211 set_iterator_to_next (it);
11212 continue;
11213 }
11214
11215 /* Does the display element fit on the line? If we truncate
11216 lines, we should draw past the right edge of the window. If
11217 we don't truncate, we want to stop so that we can display the
11218 continuation glyph before the right margin. If lines are
11219 continued, there are two possible strategies for characters
11220 resulting in more than 1 glyph (e.g. tabs): Display as many
11221 glyphs as possible in this line and leave the rest for the
11222 continuation line, or display the whole element in the next
11223 line. Original redisplay did the former, so we do it also. */
11224 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11225 hpos_before = it->hpos;
11226 x_before = x;
11227
11228 if (nglyphs == 1
11229 && it->current_x < it->last_visible_x)
11230 {
11231 ++it->hpos;
11232 row->ascent = max (row->ascent, it->max_ascent);
11233 row->height = max (row->height, it->max_ascent + it->max_descent);
11234 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11235 row->phys_height = max (row->phys_height,
11236 it->max_phys_ascent + it->max_phys_descent);
11237 if (it->current_x - it->pixel_width < it->first_visible_x)
11238 row->x = x - it->first_visible_x;
11239 }
11240 else
11241 {
11242 int new_x;
11243 struct glyph *glyph;
11244
11245 for (i = 0; i < nglyphs; ++i, x = new_x)
11246 {
11247 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11248 new_x = x + glyph->pixel_width;
11249
11250 if (/* Lines are continued. */
11251 !it->truncate_lines_p
11252 && (/* Glyph doesn't fit on the line. */
11253 new_x > it->last_visible_x
11254 /* Or it fits exactly on a window system frame. */
11255 || (new_x == it->last_visible_x
11256 && FRAME_WINDOW_P (it->f))))
11257 {
11258 /* End of a continued line. */
11259
11260 if (it->hpos == 0
11261 || (new_x == it->last_visible_x
11262 && FRAME_WINDOW_P (it->f)))
11263 {
11264 /* Current glyph fits exactly on the line. We
11265 must continue the line because we can't draw
11266 the cursor after the glyph. */
11267 row->continued_p = 1;
11268 it->current_x = new_x;
11269 it->continuation_lines_width += new_x;
11270 ++it->hpos;
11271 if (i == nglyphs - 1)
11272 set_iterator_to_next (it);
11273 }
11274 else
11275 {
11276 /* Display element draws past the right edge of
11277 the window. Restore positions to values
11278 before the element. The next line starts
11279 with current_x before the glyph that could
11280 not be displayed, so that TAB works right. */
11281 row->used[TEXT_AREA] = n_glyphs_before + i;
11282
11283 /* Display continuation glyphs. */
11284 if (!FRAME_WINDOW_P (it->f))
11285 produce_special_glyphs (it, IT_CONTINUATION);
11286 row->continued_p = 1;
11287
11288 it->current_x = x;
11289 it->continuation_lines_width += x;
11290 }
11291 break;
11292 }
11293 else if (new_x > it->first_visible_x)
11294 {
11295 /* Increment number of glyphs actually displayed. */
11296 ++it->hpos;
11297
11298 if (x < it->first_visible_x)
11299 /* Glyph is partially visible, i.e. row starts at
11300 negative X position. */
11301 row->x = x - it->first_visible_x;
11302 }
11303 else
11304 {
11305 /* Glyph is completely off the left margin of the
11306 window. This should not happen because of the
11307 move_it_in_display_line at the start of
11308 this function. */
11309 abort ();
11310 }
11311 }
11312
11313 row->ascent = max (row->ascent, it->max_ascent);
11314 row->height = max (row->height, it->max_ascent + it->max_descent);
11315 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11316 row->phys_height = max (row->phys_height,
11317 it->max_phys_ascent + it->max_phys_descent);
11318
11319 /* End of this display line if row is continued. */
11320 if (row->continued_p)
11321 break;
11322 }
11323
11324 /* Is this a line end? If yes, we're also done, after making
11325 sure that a non-default face is extended up to the right
11326 margin of the window. */
11327 if (ITERATOR_AT_END_OF_LINE_P (it))
11328 {
11329 int used_before = row->used[TEXT_AREA];
11330
11331 /* Add a space at the end of the line that is used to
11332 display the cursor there. */
11333 append_space (it, 0);
11334
11335 /* Extend the face to the end of the line. */
11336 extend_face_to_end_of_line (it);
11337
11338 /* Make sure we have the position. */
11339 if (used_before == 0)
11340 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
11341
11342 /* Consume the line end. This skips over invisible lines. */
11343 set_iterator_to_next (it);
11344 it->continuation_lines_width = 0;
11345 break;
11346 }
11347
11348 /* Proceed with next display element. Note that this skips
11349 over lines invisible because of selective display. */
11350 set_iterator_to_next (it);
11351
11352 /* If we truncate lines, we are done when the last displayed
11353 glyphs reach past the right margin of the window. */
11354 if (it->truncate_lines_p
11355 && (FRAME_WINDOW_P (it->f)
11356 ? (it->current_x >= it->last_visible_x)
11357 : (it->current_x > it->last_visible_x)))
11358 {
11359 /* Maybe add truncation glyphs. */
11360 if (!FRAME_WINDOW_P (it->f))
11361 {
11362 --it->glyph_row->used[TEXT_AREA];
11363 produce_special_glyphs (it, IT_TRUNCATION);
11364 }
11365
11366 row->truncated_on_right_p = 1;
11367 it->continuation_lines_width = 0;
11368 reseat_at_next_visible_line_start (it, 0);
11369 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
11370 it->hpos = hpos_before;
11371 it->current_x = x_before;
11372 break;
11373 }
11374 }
11375
11376 /* If line is not empty and hscrolled, maybe insert truncation glyphs
11377 at the left window margin. */
11378 if (it->first_visible_x
11379 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
11380 {
11381 if (!FRAME_WINDOW_P (it->f))
11382 insert_left_trunc_glyphs (it);
11383 row->truncated_on_left_p = 1;
11384 }
11385
11386 /* If the start of this line is the overlay arrow-position, then
11387 mark this glyph row as the one containing the overlay arrow.
11388 This is clearly a mess with variable size fonts. It would be
11389 better to let it be displayed like cursors under X. */
11390 if (MARKERP (Voverlay_arrow_position)
11391 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
11392 && (MATRIX_ROW_START_CHARPOS (row)
11393 == marker_position (Voverlay_arrow_position))
11394 && STRINGP (Voverlay_arrow_string)
11395 && ! overlay_arrow_seen)
11396 {
11397 /* Overlay arrow in window redisplay is a bitmap. */
11398 if (!FRAME_WINDOW_P (it->f))
11399 {
11400 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
11401 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
11402 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
11403 struct glyph *p = row->glyphs[TEXT_AREA];
11404 struct glyph *p2, *end;
11405
11406 /* Copy the arrow glyphs. */
11407 while (glyph < arrow_end)
11408 *p++ = *glyph++;
11409
11410 /* Throw away padding glyphs. */
11411 p2 = p;
11412 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
11413 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
11414 ++p2;
11415 if (p2 > p)
11416 {
11417 while (p2 < end)
11418 *p++ = *p2++;
11419 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
11420 }
11421 }
11422
11423 overlay_arrow_seen = 1;
11424 row->overlay_arrow_p = 1;
11425 }
11426
11427 /* Compute pixel dimensions of this line. */
11428 compute_line_metrics (it);
11429
11430 /* Remember the position at which this line ends. */
11431 row->end = it->current;
11432
11433 /* Maybe set the cursor. If you change this, it's probably a good
11434 idea to also change the code in redisplay_window for cursor
11435 movement in an unchanged window. */
11436 if (it->w->cursor.vpos < 0
11437 && PT >= MATRIX_ROW_START_CHARPOS (row)
11438 && MATRIX_ROW_END_CHARPOS (row) >= PT
11439 && !(MATRIX_ROW_END_CHARPOS (row) == PT
11440 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11441 || !row->ends_at_zv_p)))
11442 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
11443
11444 /* Highlight trailing whitespace. */
11445 if (!NILP (Vshow_trailing_whitespace))
11446 highlight_trailing_whitespace (it->f, it->glyph_row);
11447
11448 /* Prepare for the next line. This line starts horizontally at (X
11449 HPOS) = (0 0). Vertical positions are incremented. As a
11450 convenience for the caller, IT->glyph_row is set to the next
11451 row to be used. */
11452 it->current_x = it->hpos = 0;
11453 it->current_y += row->height;
11454 ++it->vpos;
11455 ++it->glyph_row;
11456 return row->displays_text_p;
11457 }
11458
11459
11460 \f
11461 /***********************************************************************
11462 Menu Bar
11463 ***********************************************************************/
11464
11465 /* Redisplay the menu bar in the frame for window W.
11466
11467 The menu bar of X frames that don't have X toolkit support is
11468 displayed in a special window W->frame->menu_bar_window.
11469
11470 The menu bar of terminal frames is treated specially as far as
11471 glyph matrices are concerned. Menu bar lines are not part of
11472 windows, so the update is done directly on the frame matrix rows
11473 for the menu bar. */
11474
11475 static void
11476 display_menu_bar (w)
11477 struct window *w;
11478 {
11479 struct frame *f = XFRAME (WINDOW_FRAME (w));
11480 struct it it;
11481 Lisp_Object items;
11482 int i;
11483
11484 /* Don't do all this for graphical frames. */
11485 #ifdef HAVE_NTGUI
11486 if (!NILP (Vwindow_system))
11487 return;
11488 #endif
11489 #ifdef USE_X_TOOLKIT
11490 if (FRAME_X_P (f))
11491 return;
11492 #endif
11493
11494 #ifdef USE_X_TOOLKIT
11495 xassert (!FRAME_WINDOW_P (f));
11496 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
11497 it.first_visible_x = 0;
11498 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11499 #else /* not USE_X_TOOLKIT */
11500 if (FRAME_WINDOW_P (f))
11501 {
11502 /* Menu bar lines are displayed in the desired matrix of the
11503 dummy window menu_bar_window. */
11504 struct window *menu_w;
11505 xassert (WINDOWP (f->menu_bar_window));
11506 menu_w = XWINDOW (f->menu_bar_window);
11507 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
11508 MENU_FACE_ID);
11509 it.first_visible_x = 0;
11510 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11511 }
11512 else
11513 {
11514 /* This is a TTY frame, i.e. character hpos/vpos are used as
11515 pixel x/y. */
11516 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
11517 MENU_FACE_ID);
11518 it.first_visible_x = 0;
11519 it.last_visible_x = FRAME_WIDTH (f);
11520 }
11521 #endif /* not USE_X_TOOLKIT */
11522
11523 /* Clear all rows of the menu bar. */
11524 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
11525 {
11526 struct glyph_row *row = it.glyph_row + i;
11527 clear_glyph_row (row);
11528 row->enabled_p = 1;
11529 row->full_width_p = 1;
11530 }
11531
11532 /* Make the first line of the menu bar appear in reverse video. */
11533 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11534
11535 /* Display all items of the menu bar. */
11536 items = FRAME_MENU_BAR_ITEMS (it.f);
11537 for (i = 0; i < XVECTOR (items)->size; i += 4)
11538 {
11539 Lisp_Object string;
11540
11541 /* Stop at nil string. */
11542 string = XVECTOR (items)->contents[i + 1];
11543 if (NILP (string))
11544 break;
11545
11546 /* Remember where item was displayed. */
11547 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
11548
11549 /* Display the item, pad with one space. */
11550 if (it.current_x < it.last_visible_x)
11551 display_string (NULL, string, Qnil, 0, 0, &it,
11552 XSTRING (string)->size + 1, 0, 0, -1);
11553 }
11554
11555 /* Fill out the line with spaces. */
11556 if (it.current_x < it.last_visible_x)
11557 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
11558
11559 /* Compute the total height of the lines. */
11560 compute_line_metrics (&it);
11561 }
11562
11563
11564 \f
11565 /***********************************************************************
11566 Mode Line
11567 ***********************************************************************/
11568
11569 /* Display the mode and/or top line of window W. */
11570
11571 static void
11572 display_mode_lines (w)
11573 struct window *w;
11574 {
11575 /* These will be set while the mode line specs are processed. */
11576 line_number_displayed = 0;
11577 w->column_number_displayed = Qnil;
11578
11579 if (WINDOW_WANTS_MODELINE_P (w))
11580 display_mode_line (w, MODE_LINE_FACE_ID,
11581 current_buffer->mode_line_format);
11582
11583 if (WINDOW_WANTS_HEADER_LINE_P (w))
11584 display_mode_line (w, HEADER_LINE_FACE_ID,
11585 current_buffer->header_line_format);
11586 }
11587
11588
11589 /* Display mode or top line of window W. FACE_ID specifies which line
11590 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
11591 FORMAT is the mode line format to display. */
11592
11593 static void
11594 display_mode_line (w, face_id, format)
11595 struct window *w;
11596 enum face_id face_id;
11597 Lisp_Object format;
11598 {
11599 struct it it;
11600 struct face *face;
11601
11602 init_iterator (&it, w, -1, -1, NULL, face_id);
11603 prepare_desired_row (it.glyph_row);
11604
11605 /* Temporarily make frame's keyboard the current kboard so that
11606 kboard-local variables in the mode_line_format will get the right
11607 values. */
11608 push_frame_kboard (it.f);
11609 display_mode_element (&it, 0, 0, 0, format);
11610 pop_frame_kboard ();
11611
11612 /* Fill up with spaces. */
11613 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
11614
11615 compute_line_metrics (&it);
11616 it.glyph_row->full_width_p = 1;
11617 it.glyph_row->mode_line_p = 1;
11618 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11619 it.glyph_row->continued_p = 0;
11620 it.glyph_row->truncated_on_left_p = 0;
11621 it.glyph_row->truncated_on_right_p = 0;
11622
11623 /* Make a 3D mode-line have a shadow at its right end. */
11624 face = FACE_FROM_ID (it.f, face_id);
11625 extend_face_to_end_of_line (&it);
11626 if (face->box != FACE_NO_BOX)
11627 {
11628 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
11629 + it.glyph_row->used[TEXT_AREA] - 1);
11630 last->right_box_line_p = 1;
11631 }
11632 }
11633
11634
11635 /* Contribute ELT to the mode line for window IT->w. How it
11636 translates into text depends on its data type.
11637
11638 IT describes the display environment in which we display, as usual.
11639
11640 DEPTH is the depth in recursion. It is used to prevent
11641 infinite recursion here.
11642
11643 FIELD_WIDTH is the number of characters the display of ELT should
11644 occupy in the mode line, and PRECISION is the maximum number of
11645 characters to display from ELT's representation. See
11646 display_string for details. *
11647
11648 Returns the hpos of the end of the text generated by ELT. */
11649
11650 static int
11651 display_mode_element (it, depth, field_width, precision, elt)
11652 struct it *it;
11653 int depth;
11654 int field_width, precision;
11655 Lisp_Object elt;
11656 {
11657 int n = 0, field, prec;
11658
11659 tail_recurse:
11660 if (depth > 10)
11661 goto invalid;
11662
11663 depth++;
11664
11665 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
11666 {
11667 case Lisp_String:
11668 {
11669 /* A string: output it and check for %-constructs within it. */
11670 unsigned char c;
11671 unsigned char *this = XSTRING (elt)->data;
11672 unsigned char *lisp_string = this;
11673
11674 while ((precision <= 0 || n < precision)
11675 && *this
11676 && (frame_title_ptr
11677 || it->current_x < it->last_visible_x))
11678 {
11679 unsigned char *last = this;
11680
11681 /* Advance to end of string or next format specifier. */
11682 while ((c = *this++) != '\0' && c != '%')
11683 ;
11684
11685 if (this - 1 != last)
11686 {
11687 /* Output to end of string or up to '%'. Field width
11688 is length of string. Don't output more than
11689 PRECISION allows us. */
11690 prec = --this - last;
11691 if (precision > 0 && prec > precision - n)
11692 prec = precision - n;
11693
11694 if (frame_title_ptr)
11695 n += store_frame_title (last, prec, prec);
11696 else
11697 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
11698 it, 0, prec, 0, -1);
11699 }
11700 else /* c == '%' */
11701 {
11702 unsigned char *percent_position = this;
11703
11704 /* Get the specified minimum width. Zero means
11705 don't pad. */
11706 field = 0;
11707 while ((c = *this++) >= '0' && c <= '9')
11708 field = field * 10 + c - '0';
11709
11710 /* Don't pad beyond the total padding allowed. */
11711 if (field_width - n > 0 && field > field_width - n)
11712 field = field_width - n;
11713
11714 /* Note that either PRECISION <= 0 or N < PRECISION. */
11715 prec = precision - n;
11716
11717 if (c == 'M')
11718 n += display_mode_element (it, depth, field, prec,
11719 Vglobal_mode_string);
11720 else if (c != 0)
11721 {
11722 unsigned char *spec
11723 = decode_mode_spec (it->w, c, field, prec);
11724
11725 if (frame_title_ptr)
11726 n += store_frame_title (spec, field, prec);
11727 else
11728 {
11729 int nglyphs_before
11730 = it->glyph_row->used[TEXT_AREA];
11731 int charpos
11732 = percent_position - XSTRING (elt)->data;
11733 int nwritten
11734 = display_string (spec, Qnil, elt, charpos, 0, it,
11735 field, prec, 0, -1);
11736
11737 /* Assign to the glyphs written above the
11738 string where the `%x' came from, position
11739 of the `%'. */
11740 if (nwritten > 0)
11741 {
11742 struct glyph *glyph
11743 = (it->glyph_row->glyphs[TEXT_AREA]
11744 + nglyphs_before);
11745 int i;
11746
11747 for (i = 0; i < nwritten; ++i)
11748 {
11749 glyph[i].object = elt;
11750 glyph[i].charpos = charpos;
11751 }
11752
11753 n += nwritten;
11754 }
11755 }
11756 }
11757 }
11758 }
11759 }
11760 break;
11761
11762 case Lisp_Symbol:
11763 /* A symbol: process the value of the symbol recursively
11764 as if it appeared here directly. Avoid error if symbol void.
11765 Special case: if value of symbol is a string, output the string
11766 literally. */
11767 {
11768 register Lisp_Object tem;
11769 tem = Fboundp (elt);
11770 if (!NILP (tem))
11771 {
11772 tem = Fsymbol_value (elt);
11773 /* If value is a string, output that string literally:
11774 don't check for % within it. */
11775 if (STRINGP (tem))
11776 {
11777 prec = XSTRING (tem)->size;
11778 if (precision > 0 && prec > precision - n)
11779 prec = precision - n;
11780 if (frame_title_ptr)
11781 n += store_frame_title (XSTRING (tem)->data, -1, prec);
11782 else
11783 n += display_string (NULL, tem, Qnil, 0, 0, it,
11784 0, prec, 0, -1);
11785 }
11786 else if (!EQ (tem, elt))
11787 {
11788 /* Give up right away for nil or t. */
11789 elt = tem;
11790 goto tail_recurse;
11791 }
11792 }
11793 }
11794 break;
11795
11796 case Lisp_Cons:
11797 {
11798 register Lisp_Object car, tem;
11799
11800 /* A cons cell: three distinct cases.
11801 If first element is a string or a cons, process all the elements
11802 and effectively concatenate them.
11803 If first element is a negative number, truncate displaying cdr to
11804 at most that many characters. If positive, pad (with spaces)
11805 to at least that many characters.
11806 If first element is a symbol, process the cadr or caddr recursively
11807 according to whether the symbol's value is non-nil or nil. */
11808 car = XCAR (elt);
11809 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
11810 {
11811 /* An element of the form (:eval FORM) means evaluate FORM
11812 and use the result as mode line elements. */
11813 struct gcpro gcpro1;
11814 Lisp_Object spec;
11815
11816 spec = eval_form (XCAR (XCDR (elt)));
11817 GCPRO1 (spec);
11818 n += display_mode_element (it, depth, field_width - n,
11819 precision - n, spec);
11820 UNGCPRO;
11821 }
11822 else if (SYMBOLP (car))
11823 {
11824 tem = Fboundp (car);
11825 elt = XCDR (elt);
11826 if (!CONSP (elt))
11827 goto invalid;
11828 /* elt is now the cdr, and we know it is a cons cell.
11829 Use its car if CAR has a non-nil value. */
11830 if (!NILP (tem))
11831 {
11832 tem = Fsymbol_value (car);
11833 if (!NILP (tem))
11834 {
11835 elt = XCAR (elt);
11836 goto tail_recurse;
11837 }
11838 }
11839 /* Symbol's value is nil (or symbol is unbound)
11840 Get the cddr of the original list
11841 and if possible find the caddr and use that. */
11842 elt = XCDR (elt);
11843 if (NILP (elt))
11844 break;
11845 else if (!CONSP (elt))
11846 goto invalid;
11847 elt = XCAR (elt);
11848 goto tail_recurse;
11849 }
11850 else if (INTEGERP (car))
11851 {
11852 register int lim = XINT (car);
11853 elt = XCDR (elt);
11854 if (lim < 0)
11855 {
11856 /* Negative int means reduce maximum width. */
11857 if (precision <= 0)
11858 precision = -lim;
11859 else
11860 precision = min (precision, -lim);
11861 }
11862 else if (lim > 0)
11863 {
11864 /* Padding specified. Don't let it be more than
11865 current maximum. */
11866 if (precision > 0)
11867 lim = min (precision, lim);
11868
11869 /* If that's more padding than already wanted, queue it.
11870 But don't reduce padding already specified even if
11871 that is beyond the current truncation point. */
11872 field_width = max (lim, field_width);
11873 }
11874 goto tail_recurse;
11875 }
11876 else if (STRINGP (car) || CONSP (car))
11877 {
11878 register int limit = 50;
11879 /* Limit is to protect against circular lists. */
11880 while (CONSP (elt)
11881 && --limit > 0
11882 && (precision <= 0 || n < precision))
11883 {
11884 n += display_mode_element (it, depth, field_width - n,
11885 precision - n, XCAR (elt));
11886 elt = XCDR (elt);
11887 }
11888 }
11889 }
11890 break;
11891
11892 default:
11893 invalid:
11894 if (frame_title_ptr)
11895 n += store_frame_title ("*invalid*", 0, precision - n);
11896 else
11897 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
11898 precision - n, 0, 0);
11899 return n;
11900 }
11901
11902 /* Pad to FIELD_WIDTH. */
11903 if (field_width > 0 && n < field_width)
11904 {
11905 if (frame_title_ptr)
11906 n += store_frame_title ("", field_width - n, 0);
11907 else
11908 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
11909 0, 0, 0);
11910 }
11911
11912 return n;
11913 }
11914
11915
11916 /* Write a null-terminated, right justified decimal representation of
11917 the positive integer D to BUF using a minimal field width WIDTH. */
11918
11919 static void
11920 pint2str (buf, width, d)
11921 register char *buf;
11922 register int width;
11923 register int d;
11924 {
11925 register char *p = buf;
11926
11927 if (d <= 0)
11928 *p++ = '0';
11929 else
11930 {
11931 while (d > 0)
11932 {
11933 *p++ = d % 10 + '0';
11934 d /= 10;
11935 }
11936 }
11937
11938 for (width -= (int) (p - buf); width > 0; --width)
11939 *p++ = ' ';
11940 *p-- = '\0';
11941 while (p > buf)
11942 {
11943 d = *buf;
11944 *buf++ = *p;
11945 *p-- = d;
11946 }
11947 }
11948
11949 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
11950 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
11951 type of CODING_SYSTEM. Return updated pointer into BUF. */
11952
11953 static unsigned char invalid_eol_type[] = "(*invalid*)";
11954
11955 static char *
11956 decode_mode_spec_coding (coding_system, buf, eol_flag)
11957 Lisp_Object coding_system;
11958 register char *buf;
11959 int eol_flag;
11960 {
11961 Lisp_Object val;
11962 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
11963 unsigned char *eol_str;
11964 int eol_str_len;
11965 /* The EOL conversion we are using. */
11966 Lisp_Object eoltype;
11967
11968 val = Fget (coding_system, Qcoding_system);
11969
11970 if (!VECTORP (val)) /* Not yet decided. */
11971 {
11972 if (multibyte)
11973 *buf++ = '-';
11974 if (eol_flag)
11975 eoltype = eol_mnemonic_undecided;
11976 /* Don't mention EOL conversion if it isn't decided. */
11977 }
11978 else
11979 {
11980 Lisp_Object eolvalue;
11981
11982 eolvalue = Fget (coding_system, Qeol_type);
11983
11984 if (multibyte)
11985 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
11986
11987 if (eol_flag)
11988 {
11989 /* The EOL conversion that is normal on this system. */
11990
11991 if (NILP (eolvalue)) /* Not yet decided. */
11992 eoltype = eol_mnemonic_undecided;
11993 else if (VECTORP (eolvalue)) /* Not yet decided. */
11994 eoltype = eol_mnemonic_undecided;
11995 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
11996 eoltype = (XFASTINT (eolvalue) == 0
11997 ? eol_mnemonic_unix
11998 : (XFASTINT (eolvalue) == 1
11999 ? eol_mnemonic_dos : eol_mnemonic_mac));
12000 }
12001 }
12002
12003 if (eol_flag)
12004 {
12005 /* Mention the EOL conversion if it is not the usual one. */
12006 if (STRINGP (eoltype))
12007 {
12008 eol_str = XSTRING (eoltype)->data;
12009 eol_str_len = XSTRING (eoltype)->size;
12010 }
12011 else if (INTEGERP (eoltype)
12012 && CHAR_VALID_P (XINT (eoltype), 0))
12013 {
12014 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
12015 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
12016 }
12017 else
12018 {
12019 eol_str = invalid_eol_type;
12020 eol_str_len = sizeof (invalid_eol_type) - 1;
12021 }
12022 bcopy (eol_str, buf, eol_str_len);
12023 buf += eol_str_len;
12024 }
12025
12026 return buf;
12027 }
12028
12029 /* Return a string for the output of a mode line %-spec for window W,
12030 generated by character C. PRECISION >= 0 means don't return a
12031 string longer than that value. FIELD_WIDTH > 0 means pad the
12032 string returned with spaces to that value. */
12033
12034 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
12035
12036 static char *
12037 decode_mode_spec (w, c, field_width, precision)
12038 struct window *w;
12039 register int c;
12040 int field_width, precision;
12041 {
12042 Lisp_Object obj;
12043 struct frame *f = XFRAME (WINDOW_FRAME (w));
12044 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
12045 struct buffer *b = XBUFFER (w->buffer);
12046
12047 obj = Qnil;
12048
12049 switch (c)
12050 {
12051 case '*':
12052 if (!NILP (b->read_only))
12053 return "%";
12054 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12055 return "*";
12056 return "-";
12057
12058 case '+':
12059 /* This differs from %* only for a modified read-only buffer. */
12060 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12061 return "*";
12062 if (!NILP (b->read_only))
12063 return "%";
12064 return "-";
12065
12066 case '&':
12067 /* This differs from %* in ignoring read-only-ness. */
12068 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12069 return "*";
12070 return "-";
12071
12072 case '%':
12073 return "%";
12074
12075 case '[':
12076 {
12077 int i;
12078 char *p;
12079
12080 if (command_loop_level > 5)
12081 return "[[[... ";
12082 p = decode_mode_spec_buf;
12083 for (i = 0; i < command_loop_level; i++)
12084 *p++ = '[';
12085 *p = 0;
12086 return decode_mode_spec_buf;
12087 }
12088
12089 case ']':
12090 {
12091 int i;
12092 char *p;
12093
12094 if (command_loop_level > 5)
12095 return " ...]]]";
12096 p = decode_mode_spec_buf;
12097 for (i = 0; i < command_loop_level; i++)
12098 *p++ = ']';
12099 *p = 0;
12100 return decode_mode_spec_buf;
12101 }
12102
12103 case '-':
12104 {
12105 register int i;
12106
12107 /* Let lots_of_dashes be a string of infinite length. */
12108 if (field_width <= 0
12109 || field_width > sizeof (lots_of_dashes))
12110 {
12111 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
12112 decode_mode_spec_buf[i] = '-';
12113 decode_mode_spec_buf[i] = '\0';
12114 return decode_mode_spec_buf;
12115 }
12116 else
12117 return lots_of_dashes;
12118 }
12119
12120 case 'b':
12121 obj = b->name;
12122 break;
12123
12124 case 'c':
12125 {
12126 int col = current_column ();
12127 XSETFASTINT (w->column_number_displayed, col);
12128 pint2str (decode_mode_spec_buf, field_width, col);
12129 return decode_mode_spec_buf;
12130 }
12131
12132 case 'F':
12133 /* %F displays the frame name. */
12134 if (!NILP (f->title))
12135 return (char *) XSTRING (f->title)->data;
12136 if (f->explicit_name || ! FRAME_WINDOW_P (f))
12137 return (char *) XSTRING (f->name)->data;
12138 return "Emacs";
12139
12140 case 'f':
12141 obj = b->filename;
12142 break;
12143
12144 case 'l':
12145 {
12146 int startpos = XMARKER (w->start)->charpos;
12147 int startpos_byte = marker_byte_position (w->start);
12148 int line, linepos, linepos_byte, topline;
12149 int nlines, junk;
12150 int height = XFASTINT (w->height);
12151
12152 /* If we decided that this buffer isn't suitable for line numbers,
12153 don't forget that too fast. */
12154 if (EQ (w->base_line_pos, w->buffer))
12155 goto no_value;
12156 /* But do forget it, if the window shows a different buffer now. */
12157 else if (BUFFERP (w->base_line_pos))
12158 w->base_line_pos = Qnil;
12159
12160 /* If the buffer is very big, don't waste time. */
12161 if (BUF_ZV (b) - BUF_BEGV (b) > line_number_display_limit)
12162 {
12163 w->base_line_pos = Qnil;
12164 w->base_line_number = Qnil;
12165 goto no_value;
12166 }
12167
12168 if (!NILP (w->base_line_number)
12169 && !NILP (w->base_line_pos)
12170 && XFASTINT (w->base_line_pos) <= startpos)
12171 {
12172 line = XFASTINT (w->base_line_number);
12173 linepos = XFASTINT (w->base_line_pos);
12174 linepos_byte = buf_charpos_to_bytepos (b, linepos);
12175 }
12176 else
12177 {
12178 line = 1;
12179 linepos = BUF_BEGV (b);
12180 linepos_byte = BUF_BEGV_BYTE (b);
12181 }
12182
12183 /* Count lines from base line to window start position. */
12184 nlines = display_count_lines (linepos, linepos_byte,
12185 startpos_byte,
12186 startpos, &junk);
12187
12188 topline = nlines + line;
12189
12190 /* Determine a new base line, if the old one is too close
12191 or too far away, or if we did not have one.
12192 "Too close" means it's plausible a scroll-down would
12193 go back past it. */
12194 if (startpos == BUF_BEGV (b))
12195 {
12196 XSETFASTINT (w->base_line_number, topline);
12197 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
12198 }
12199 else if (nlines < height + 25 || nlines > height * 3 + 50
12200 || linepos == BUF_BEGV (b))
12201 {
12202 int limit = BUF_BEGV (b);
12203 int limit_byte = BUF_BEGV_BYTE (b);
12204 int position;
12205 int distance = (height * 2 + 30) * line_number_display_limit_width;
12206
12207 if (startpos - distance > limit)
12208 {
12209 limit = startpos - distance;
12210 limit_byte = CHAR_TO_BYTE (limit);
12211 }
12212
12213 nlines = display_count_lines (startpos, startpos_byte,
12214 limit_byte,
12215 - (height * 2 + 30),
12216 &position);
12217 /* If we couldn't find the lines we wanted within
12218 line_number_display_limit_width chars per line,
12219 give up on line numbers for this window. */
12220 if (position == limit_byte && limit == startpos - distance)
12221 {
12222 w->base_line_pos = w->buffer;
12223 w->base_line_number = Qnil;
12224 goto no_value;
12225 }
12226
12227 XSETFASTINT (w->base_line_number, topline - nlines);
12228 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
12229 }
12230
12231 /* Now count lines from the start pos to point. */
12232 nlines = display_count_lines (startpos, startpos_byte,
12233 PT_BYTE, PT, &junk);
12234
12235 /* Record that we did display the line number. */
12236 line_number_displayed = 1;
12237
12238 /* Make the string to show. */
12239 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
12240 return decode_mode_spec_buf;
12241 no_value:
12242 {
12243 char* p = decode_mode_spec_buf;
12244 int pad = field_width - 2;
12245 while (pad-- > 0)
12246 *p++ = ' ';
12247 *p++ = '?';
12248 *p = '?';
12249 return decode_mode_spec_buf;
12250 }
12251 }
12252 break;
12253
12254 case 'm':
12255 obj = b->mode_name;
12256 break;
12257
12258 case 'n':
12259 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
12260 return " Narrow";
12261 break;
12262
12263 case 'p':
12264 {
12265 int pos = marker_position (w->start);
12266 int total = BUF_ZV (b) - BUF_BEGV (b);
12267
12268 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
12269 {
12270 if (pos <= BUF_BEGV (b))
12271 return "All";
12272 else
12273 return "Bottom";
12274 }
12275 else if (pos <= BUF_BEGV (b))
12276 return "Top";
12277 else
12278 {
12279 if (total > 1000000)
12280 /* Do it differently for a large value, to avoid overflow. */
12281 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12282 else
12283 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
12284 /* We can't normally display a 3-digit number,
12285 so get us a 2-digit number that is close. */
12286 if (total == 100)
12287 total = 99;
12288 sprintf (decode_mode_spec_buf, "%2d%%", total);
12289 return decode_mode_spec_buf;
12290 }
12291 }
12292
12293 /* Display percentage of size above the bottom of the screen. */
12294 case 'P':
12295 {
12296 int toppos = marker_position (w->start);
12297 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
12298 int total = BUF_ZV (b) - BUF_BEGV (b);
12299
12300 if (botpos >= BUF_ZV (b))
12301 {
12302 if (toppos <= BUF_BEGV (b))
12303 return "All";
12304 else
12305 return "Bottom";
12306 }
12307 else
12308 {
12309 if (total > 1000000)
12310 /* Do it differently for a large value, to avoid overflow. */
12311 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12312 else
12313 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
12314 /* We can't normally display a 3-digit number,
12315 so get us a 2-digit number that is close. */
12316 if (total == 100)
12317 total = 99;
12318 if (toppos <= BUF_BEGV (b))
12319 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
12320 else
12321 sprintf (decode_mode_spec_buf, "%2d%%", total);
12322 return decode_mode_spec_buf;
12323 }
12324 }
12325
12326 case 's':
12327 /* status of process */
12328 obj = Fget_buffer_process (w->buffer);
12329 if (NILP (obj))
12330 return "no process";
12331 #ifdef subprocesses
12332 obj = Fsymbol_name (Fprocess_status (obj));
12333 #endif
12334 break;
12335
12336 case 't': /* indicate TEXT or BINARY */
12337 #ifdef MODE_LINE_BINARY_TEXT
12338 return MODE_LINE_BINARY_TEXT (b);
12339 #else
12340 return "T";
12341 #endif
12342
12343 case 'z':
12344 /* coding-system (not including end-of-line format) */
12345 case 'Z':
12346 /* coding-system (including end-of-line type) */
12347 {
12348 int eol_flag = (c == 'Z');
12349 char *p = decode_mode_spec_buf;
12350
12351 if (! FRAME_WINDOW_P (f))
12352 {
12353 /* No need to mention EOL here--the terminal never needs
12354 to do EOL conversion. */
12355 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
12356 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
12357 }
12358 p = decode_mode_spec_coding (b->buffer_file_coding_system,
12359 p, eol_flag);
12360
12361 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
12362 #ifdef subprocesses
12363 obj = Fget_buffer_process (Fcurrent_buffer ());
12364 if (PROCESSP (obj))
12365 {
12366 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
12367 p, eol_flag);
12368 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
12369 p, eol_flag);
12370 }
12371 #endif /* subprocesses */
12372 #endif /* 0 */
12373 *p = 0;
12374 return decode_mode_spec_buf;
12375 }
12376 }
12377
12378 if (STRINGP (obj))
12379 return (char *) XSTRING (obj)->data;
12380 else
12381 return "";
12382 }
12383
12384
12385 /* Count up to COUNT lines starting from START / START_BYTE.
12386 But don't go beyond LIMIT_BYTE.
12387 Return the number of lines thus found (always nonnegative).
12388
12389 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
12390
12391 static int
12392 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
12393 int start, start_byte, limit_byte, count;
12394 int *byte_pos_ptr;
12395 {
12396 register unsigned char *cursor;
12397 unsigned char *base;
12398
12399 register int ceiling;
12400 register unsigned char *ceiling_addr;
12401 int orig_count = count;
12402
12403 /* If we are not in selective display mode,
12404 check only for newlines. */
12405 int selective_display = (!NILP (current_buffer->selective_display)
12406 && !INTEGERP (current_buffer->selective_display));
12407
12408 if (count > 0)
12409 {
12410 while (start_byte < limit_byte)
12411 {
12412 ceiling = BUFFER_CEILING_OF (start_byte);
12413 ceiling = min (limit_byte - 1, ceiling);
12414 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
12415 base = (cursor = BYTE_POS_ADDR (start_byte));
12416 while (1)
12417 {
12418 if (selective_display)
12419 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
12420 ;
12421 else
12422 while (*cursor != '\n' && ++cursor != ceiling_addr)
12423 ;
12424
12425 if (cursor != ceiling_addr)
12426 {
12427 if (--count == 0)
12428 {
12429 start_byte += cursor - base + 1;
12430 *byte_pos_ptr = start_byte;
12431 return orig_count;
12432 }
12433 else
12434 if (++cursor == ceiling_addr)
12435 break;
12436 }
12437 else
12438 break;
12439 }
12440 start_byte += cursor - base;
12441 }
12442 }
12443 else
12444 {
12445 while (start_byte > limit_byte)
12446 {
12447 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
12448 ceiling = max (limit_byte, ceiling);
12449 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
12450 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
12451 while (1)
12452 {
12453 if (selective_display)
12454 while (--cursor != ceiling_addr
12455 && *cursor != '\n' && *cursor != 015)
12456 ;
12457 else
12458 while (--cursor != ceiling_addr && *cursor != '\n')
12459 ;
12460
12461 if (cursor != ceiling_addr)
12462 {
12463 if (++count == 0)
12464 {
12465 start_byte += cursor - base + 1;
12466 *byte_pos_ptr = start_byte;
12467 /* When scanning backwards, we should
12468 not count the newline posterior to which we stop. */
12469 return - orig_count - 1;
12470 }
12471 }
12472 else
12473 break;
12474 }
12475 /* Here we add 1 to compensate for the last decrement
12476 of CURSOR, which took it past the valid range. */
12477 start_byte += cursor - base + 1;
12478 }
12479 }
12480
12481 *byte_pos_ptr = limit_byte;
12482
12483 if (count < 0)
12484 return - orig_count + count;
12485 return orig_count - count;
12486
12487 }
12488
12489
12490 \f
12491 /***********************************************************************
12492 Displaying strings
12493 ***********************************************************************/
12494
12495 /* Display a NUL-terminated string, starting with index START.
12496
12497 If STRING is non-null, display that C string. Otherwise, the Lisp
12498 string LISP_STRING is displayed.
12499
12500 If FACE_STRING is not nil, FACE_STRING_POS is a position in
12501 FACE_STRING. Display STRING or LISP_STRING with the face at
12502 FACE_STRING_POS in FACE_STRING:
12503
12504 Display the string in the environment given by IT, but use the
12505 standard display table, temporarily.
12506
12507 FIELD_WIDTH is the minimum number of output glyphs to produce.
12508 If STRING has fewer characters than FIELD_WIDTH, pad to the right
12509 with spaces. If STRING has more characters, more than FIELD_WIDTH
12510 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
12511
12512 PRECISION is the maximum number of characters to output from
12513 STRING. PRECISION < 0 means don't truncate the string.
12514
12515 This is roughly equivalent to printf format specifiers:
12516
12517 FIELD_WIDTH PRECISION PRINTF
12518 ----------------------------------------
12519 -1 -1 %s
12520 -1 10 %.10s
12521 10 -1 %10s
12522 20 10 %20.10s
12523
12524 MULTIBYTE zero means do not display multibyte chars, > 0 means do
12525 display them, and < 0 means obey the current buffer's value of
12526 enable_multibyte_characters.
12527
12528 Value is the number of glyphs produced. */
12529
12530 static int
12531 display_string (string, lisp_string, face_string, face_string_pos,
12532 start, it, field_width, precision, max_x, multibyte)
12533 unsigned char *string;
12534 Lisp_Object lisp_string;
12535 Lisp_Object face_string;
12536 int face_string_pos;
12537 int start;
12538 struct it *it;
12539 int field_width, precision, max_x;
12540 int multibyte;
12541 {
12542 int hpos_at_start = it->hpos;
12543 int saved_face_id = it->face_id;
12544 struct glyph_row *row = it->glyph_row;
12545
12546 /* Initialize the iterator IT for iteration over STRING beginning
12547 with index START. We assume that IT may be modified here (which
12548 means that display_line has to do something when displaying a
12549 mini-buffer prompt, which it does). */
12550 reseat_to_string (it, string, lisp_string, start,
12551 precision, field_width, multibyte);
12552
12553 /* If displaying STRING, set up the face of the iterator
12554 from LISP_STRING, if that's given. */
12555 if (STRINGP (face_string))
12556 {
12557 int endptr;
12558 struct face *face;
12559
12560 it->face_id
12561 = face_at_string_position (it->w, face_string, face_string_pos,
12562 0, it->region_beg_charpos,
12563 it->region_end_charpos,
12564 &endptr, it->base_face_id);
12565 face = FACE_FROM_ID (it->f, it->face_id);
12566 it->face_box_p = face->box != FACE_NO_BOX;
12567 }
12568
12569 /* Set max_x to the maximum allowed X position. Don't let it go
12570 beyond the right edge of the window. */
12571 if (max_x <= 0)
12572 max_x = it->last_visible_x;
12573 else
12574 max_x = min (max_x, it->last_visible_x);
12575
12576 /* Skip over display elements that are not visible. because IT->w is
12577 hscrolled. */
12578 if (it->current_x < it->first_visible_x)
12579 move_it_in_display_line_to (it, 100000, it->first_visible_x,
12580 MOVE_TO_POS | MOVE_TO_X);
12581
12582 row->ascent = it->max_ascent;
12583 row->height = it->max_ascent + it->max_descent;
12584 row->phys_ascent = it->max_phys_ascent;
12585 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12586
12587 /* This condition is for the case that we are called with current_x
12588 past last_visible_x. */
12589 while (it->current_x < max_x)
12590 {
12591 int x_before, x, n_glyphs_before, i, nglyphs;
12592
12593 /* Get the next display element. */
12594 if (!get_next_display_element (it))
12595 break;
12596
12597 /* Produce glyphs. */
12598 x_before = it->current_x;
12599 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
12600 PRODUCE_GLYPHS (it);
12601
12602 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
12603 i = 0;
12604 x = x_before;
12605 while (i < nglyphs)
12606 {
12607 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12608
12609 if (!it->truncate_lines_p
12610 && x + glyph->pixel_width > max_x)
12611 {
12612 /* End of continued line or max_x reached. */
12613 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
12614 it->current_x = x;
12615 break;
12616 }
12617 else if (x + glyph->pixel_width > it->first_visible_x)
12618 {
12619 /* Glyph is at least partially visible. */
12620 ++it->hpos;
12621 if (x < it->first_visible_x)
12622 it->glyph_row->x = x - it->first_visible_x;
12623 }
12624 else
12625 {
12626 /* Glyph is off the left margin of the display area.
12627 Should not happen. */
12628 abort ();
12629 }
12630
12631 row->ascent = max (row->ascent, it->max_ascent);
12632 row->height = max (row->height, it->max_ascent + it->max_descent);
12633 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12634 row->phys_height = max (row->phys_height,
12635 it->max_phys_ascent + it->max_phys_descent);
12636 x += glyph->pixel_width;
12637 ++i;
12638 }
12639
12640 /* Stop if max_x reached. */
12641 if (i < nglyphs)
12642 break;
12643
12644 /* Stop at line ends. */
12645 if (ITERATOR_AT_END_OF_LINE_P (it))
12646 {
12647 it->continuation_lines_width = 0;
12648 break;
12649 }
12650
12651 set_iterator_to_next (it);
12652
12653 /* Stop if truncating at the right edge. */
12654 if (it->truncate_lines_p
12655 && it->current_x >= it->last_visible_x)
12656 {
12657 /* Add truncation mark, but don't do it if the line is
12658 truncated at a padding space. */
12659 if (IT_CHARPOS (*it) < it->string_nchars)
12660 {
12661 if (!FRAME_WINDOW_P (it->f))
12662 produce_special_glyphs (it, IT_TRUNCATION);
12663 it->glyph_row->truncated_on_right_p = 1;
12664 }
12665 break;
12666 }
12667 }
12668
12669 /* Maybe insert a truncation at the left. */
12670 if (it->first_visible_x
12671 && IT_CHARPOS (*it) > 0)
12672 {
12673 if (!FRAME_WINDOW_P (it->f))
12674 insert_left_trunc_glyphs (it);
12675 it->glyph_row->truncated_on_left_p = 1;
12676 }
12677
12678 it->face_id = saved_face_id;
12679
12680 /* Value is number of columns displayed. */
12681 return it->hpos - hpos_at_start;
12682 }
12683
12684
12685 \f
12686 /* This is like a combination of memq and assq. Return 1 if PROPVAL
12687 appears as an element of LIST or as the car of an element of LIST.
12688 If PROPVAL is a list, compare each element against LIST in that
12689 way, and return 1 if any element of PROPVAL is found in LIST.
12690 Otherwise return 0. This function cannot quit. */
12691
12692 int
12693 invisible_p (propval, list)
12694 register Lisp_Object propval;
12695 Lisp_Object list;
12696 {
12697 register Lisp_Object tail, proptail;
12698 for (tail = list; CONSP (tail); tail = XCDR (tail))
12699 {
12700 register Lisp_Object tem;
12701 tem = XCAR (tail);
12702 if (EQ (propval, tem))
12703 return 1;
12704 if (CONSP (tem) && EQ (propval, XCAR (tem)))
12705 return 1;
12706 }
12707 if (CONSP (propval))
12708 for (proptail = propval; CONSP (proptail);
12709 proptail = XCDR (proptail))
12710 {
12711 Lisp_Object propelt;
12712 propelt = XCAR (proptail);
12713 for (tail = list; CONSP (tail); tail = XCDR (tail))
12714 {
12715 register Lisp_Object tem;
12716 tem = XCAR (tail);
12717 if (EQ (propelt, tem))
12718 return 1;
12719 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
12720 return 1;
12721 }
12722 }
12723 return 0;
12724 }
12725
12726
12727 /* Return 1 if PROPVAL appears as the car of an element of LIST and
12728 the cdr of that element is non-nil. If PROPVAL is a list, check
12729 each element of PROPVAL in that way, and the first time some
12730 element is found, return 1 if the cdr of that element is non-nil.
12731 Otherwise return 0. This function cannot quit. */
12732
12733 int
12734 invisible_ellipsis_p (propval, list)
12735 register Lisp_Object propval;
12736 Lisp_Object list;
12737 {
12738 register Lisp_Object tail, proptail;
12739
12740 for (tail = list; CONSP (tail); tail = XCDR (tail))
12741 {
12742 register Lisp_Object tem;
12743 tem = XCAR (tail);
12744 if (CONSP (tem) && EQ (propval, XCAR (tem)))
12745 return ! NILP (XCDR (tem));
12746 }
12747
12748 if (CONSP (propval))
12749 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
12750 {
12751 Lisp_Object propelt;
12752 propelt = XCAR (proptail);
12753 for (tail = list; CONSP (tail); tail = XCDR (tail))
12754 {
12755 register Lisp_Object tem;
12756 tem = XCAR (tail);
12757 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
12758 return ! NILP (XCDR (tem));
12759 }
12760 }
12761
12762 return 0;
12763 }
12764
12765
12766 \f
12767 /***********************************************************************
12768 Initialization
12769 ***********************************************************************/
12770
12771 void
12772 syms_of_xdisp ()
12773 {
12774 Vwith_echo_area_save_vector = Qnil;
12775 staticpro (&Vwith_echo_area_save_vector);
12776
12777 Vmessage_stack = Qnil;
12778 staticpro (&Vmessage_stack);
12779
12780 Qinhibit_redisplay = intern ("inhibit-redisplay");
12781 staticpro (&Qinhibit_redisplay);
12782
12783 #if GLYPH_DEBUG
12784 defsubr (&Sdump_glyph_matrix);
12785 defsubr (&Sdump_glyph_row);
12786 defsubr (&Sdump_tool_bar_row);
12787 defsubr (&Strace_redisplay_toggle);
12788 defsubr (&Strace_to_stderr);
12789 #endif
12790
12791 staticpro (&Qmenu_bar_update_hook);
12792 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
12793
12794 staticpro (&Qoverriding_terminal_local_map);
12795 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
12796
12797 staticpro (&Qoverriding_local_map);
12798 Qoverriding_local_map = intern ("overriding-local-map");
12799
12800 staticpro (&Qwindow_scroll_functions);
12801 Qwindow_scroll_functions = intern ("window-scroll-functions");
12802
12803 staticpro (&Qredisplay_end_trigger_functions);
12804 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
12805
12806 staticpro (&Qinhibit_point_motion_hooks);
12807 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
12808
12809 QCdata = intern (":data");
12810 staticpro (&QCdata);
12811 Qdisplay = intern ("display");
12812 staticpro (&Qdisplay);
12813 Qspace_width = intern ("space-width");
12814 staticpro (&Qspace_width);
12815 Qraise = intern ("raise");
12816 staticpro (&Qraise);
12817 Qspace = intern ("space");
12818 staticpro (&Qspace);
12819 Qmargin = intern ("margin");
12820 staticpro (&Qmargin);
12821 Qleft_margin = intern ("left-margin");
12822 staticpro (&Qleft_margin);
12823 Qright_margin = intern ("right-margin");
12824 staticpro (&Qright_margin);
12825 Qalign_to = intern ("align-to");
12826 staticpro (&Qalign_to);
12827 QCalign_to = intern (":align-to");
12828 staticpro (&QCalign_to);
12829 Qrelative_width = intern ("relative-width");
12830 staticpro (&Qrelative_width);
12831 QCrelative_width = intern (":relative-width");
12832 staticpro (&QCrelative_width);
12833 QCrelative_height = intern (":relative-height");
12834 staticpro (&QCrelative_height);
12835 QCeval = intern (":eval");
12836 staticpro (&QCeval);
12837 Qwhen = intern ("when");
12838 staticpro (&Qwhen);
12839 QCfile = intern (":file");
12840 staticpro (&QCfile);
12841 Qfontified = intern ("fontified");
12842 staticpro (&Qfontified);
12843 Qfontification_functions = intern ("fontification-functions");
12844 staticpro (&Qfontification_functions);
12845 Qtrailing_whitespace = intern ("trailing-whitespace");
12846 staticpro (&Qtrailing_whitespace);
12847 Qimage = intern ("image");
12848 staticpro (&Qimage);
12849
12850 last_arrow_position = Qnil;
12851 last_arrow_string = Qnil;
12852 staticpro (&last_arrow_position);
12853 staticpro (&last_arrow_string);
12854
12855 echo_buffer[0] = echo_buffer[1] = Qnil;
12856 staticpro (&echo_buffer[0]);
12857 staticpro (&echo_buffer[1]);
12858
12859 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
12860 staticpro (&echo_area_buffer[0]);
12861 staticpro (&echo_area_buffer[1]);
12862
12863 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
12864 "Non-nil means highlight trailing whitespace.\n\
12865 The face used for trailing whitespace is `trailing-whitespace'.");
12866 Vshow_trailing_whitespace = Qnil;
12867
12868 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
12869 "Non-nil means don't actually do any redisplay.\n\
12870 This is used for internal purposes.");
12871 Vinhibit_redisplay = Qnil;
12872
12873 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
12874 "String (or mode line construct) included (normally) in `mode-line-format'.");
12875 Vglobal_mode_string = Qnil;
12876
12877 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
12878 "Marker for where to display an arrow on top of the buffer text.\n\
12879 This must be the beginning of a line in order to work.\n\
12880 See also `overlay-arrow-string'.");
12881 Voverlay_arrow_position = Qnil;
12882
12883 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
12884 "String to display as an arrow. See also `overlay-arrow-position'.");
12885 Voverlay_arrow_string = Qnil;
12886
12887 DEFVAR_INT ("scroll-step", &scroll_step,
12888 "*The number of lines to try scrolling a window by when point moves out.\n\
12889 If that fails to bring point back on frame, point is centered instead.\n\
12890 If this is zero, point is always centered after it moves off frame.");
12891
12892 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
12893 "*Scroll up to this many lines, to bring point back on screen.\n\
12894 A value of zero means to scroll the text to center point vertically\n\
12895 in the window.");
12896 scroll_conservatively = 0;
12897
12898 DEFVAR_INT ("scroll-margin", &scroll_margin,
12899 "*Number of lines of margin at the top and bottom of a window.\n\
12900 Recenter the window whenever point gets within this many lines\n\
12901 of the top or bottom of the window.");
12902 scroll_margin = 0;
12903
12904 #if GLYPH_DEBUG
12905 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
12906 #endif
12907
12908 DEFVAR_BOOL ("truncate-partial-width-windows",
12909 &truncate_partial_width_windows,
12910 "*Non-nil means truncate lines in all windows less than full frame wide.");
12911 truncate_partial_width_windows = 1;
12912
12913 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
12914 "*Non-nil means use inverse video for the mode line.");
12915 mode_line_inverse_video = 1;
12916
12917 DEFVAR_INT ("line-number-display-limit", &line_number_display_limit,
12918 "*Maximum buffer size for which line number should be displayed.\n\
12919 If the buffer is bigger than this, the line number does not appear\n\
12920 in the mode line.");
12921 line_number_display_limit = 1000000;
12922
12923 DEFVAR_INT ("line-number-display-limit-width", &line_number_display_limit_width,
12924 "*Maximum line width (in characters) for line number display.\n\
12925 If the average length of the lines near point is bigger than this, then the\n\
12926 line number may be omitted from the mode line.");
12927 line_number_display_limit_width = 200;
12928
12929 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
12930 "*Non-nil means highlight region even in nonselected windows.");
12931 highlight_nonselected_windows = 0;
12932
12933 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
12934 "Non-nil if more than one frame is visible on this display.\n\
12935 Minibuffer-only frames don't count, but iconified frames do.\n\
12936 This variable is not guaranteed to be accurate except while processing\n\
12937 `frame-title-format' and `icon-title-format'.");
12938
12939 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
12940 "Template for displaying the title bar of visible frames.\n\
12941 \(Assuming the window manager supports this feature.)\n\
12942 This variable has the same structure as `mode-line-format' (which see),\n\
12943 and is used only on frames for which no explicit name has been set\n\
12944 \(see `modify-frame-parameters').");
12945 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
12946 "Template for displaying the title bar of an iconified frame.\n\
12947 \(Assuming the window manager supports this feature.)\n\
12948 This variable has the same structure as `mode-line-format' (which see),\n\
12949 and is used only on frames for which no explicit name has been set\n\
12950 \(see `modify-frame-parameters').");
12951 Vicon_title_format
12952 = Vframe_title_format
12953 = Fcons (intern ("multiple-frames"),
12954 Fcons (build_string ("%b"),
12955 Fcons (Fcons (build_string (""),
12956 Fcons (intern ("invocation-name"),
12957 Fcons (build_string ("@"),
12958 Fcons (intern ("system-name"),
12959 Qnil)))),
12960 Qnil)));
12961
12962 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
12963 "Maximum number of lines to keep in the message log buffer.\n\
12964 If nil, disable message logging. If t, log messages but don't truncate\n\
12965 the buffer when it becomes large.");
12966 XSETFASTINT (Vmessage_log_max, 50);
12967
12968 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
12969 "Functions called before redisplay, if window sizes have changed.\n\
12970 The value should be a list of functions that take one argument.\n\
12971 Just before redisplay, for each frame, if any of its windows have changed\n\
12972 size since the last redisplay, or have been split or deleted,\n\
12973 all the functions in the list are called, with the frame as argument.");
12974 Vwindow_size_change_functions = Qnil;
12975
12976 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
12977 "List of Functions to call before redisplaying a window with scrolling.\n\
12978 Each function is called with two arguments, the window\n\
12979 and its new display-start position. Note that the value of `window-end'\n\
12980 is not valid when these functions are called.");
12981 Vwindow_scroll_functions = Qnil;
12982
12983 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
12984 "*Non-nil means automatically resize tool-bars.\n\
12985 This increases a tool-bar's height if not all tool-bar items are visible.\n\
12986 It decreases a tool-bar's height when it would display blank lines\n\
12987 otherwise.");
12988 auto_resize_tool_bars_p = 1;
12989
12990 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
12991 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
12992 auto_raise_tool_bar_buttons_p = 1;
12993
12994 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
12995 "*Margin around tool-bar buttons in pixels.");
12996 tool_bar_button_margin = 1;
12997
12998 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
12999 "Relief thickness of tool-bar buttons.");
13000 tool_bar_button_relief = 3;
13001
13002 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
13003 "List of functions to call to fontify regions of text.\n\
13004 Each function is called with one argument POS. Functions must\n\
13005 fontify a region starting at POS in the current buffer, and give\n\
13006 fontified regions the property `fontified'.\n\
13007 This variable automatically becomes buffer-local when set.");
13008 Vfontification_functions = Qnil;
13009 Fmake_local_variable (Qfontification_functions);
13010
13011 DEFVAR_BOOL ("unibyte-display-via-language-environment",
13012 &unibyte_display_via_language_environment,
13013 "*Non-nil means display unibyte text according to language environment.\n\
13014 Specifically this means that unibyte non-ASCII characters\n\
13015 are displayed by converting them to the equivalent multibyte characters\n\
13016 according to the current language environment. As a result, they are\n\
13017 displayed according to the current fontset.");
13018 unibyte_display_via_language_environment = 0;
13019
13020 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
13021 "*Maximum height for resizing mini-windows.\n\
13022 If a float, it specifies a fraction of the mini-window frame's height.\n\
13023 If an integer, it specifies a number of lines.\n\
13024 If nil, don't resize.");
13025 Vmax_mini_window_height = make_float (0.25);
13026
13027 DEFVAR_BOOL ("cursor-in-non-selected-windows",
13028 &cursor_in_non_selected_windows,
13029 "*Non-nil means display a hollow cursor in non-selected windows.\n\
13030 Nil means don't display a cursor there.");
13031 cursor_in_non_selected_windows = 1;
13032 }
13033
13034
13035 /* Initialize this module when Emacs starts. */
13036
13037 void
13038 init_xdisp ()
13039 {
13040 Lisp_Object root_window;
13041 struct window *mini_w;
13042
13043 CHARPOS (this_line_start_pos) = 0;
13044
13045 mini_w = XWINDOW (minibuf_window);
13046 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
13047
13048 if (!noninteractive)
13049 {
13050 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
13051 int i;
13052
13053 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
13054 set_window_height (root_window,
13055 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
13056 0);
13057 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
13058 set_window_height (minibuf_window, 1, 0);
13059
13060 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
13061 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
13062
13063 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
13064 scratch_glyph_row.glyphs[TEXT_AREA + 1]
13065 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
13066
13067 /* The default ellipsis glyphs `...'. */
13068 for (i = 0; i < 3; ++i)
13069 XSETFASTINT (default_invis_vector[i], '.');
13070 }
13071
13072 #ifdef HAVE_WINDOW_SYSTEM
13073 {
13074 /* Allocate the buffer for frame titles. */
13075 int size = 100;
13076 frame_title_buf = (char *) xmalloc (size);
13077 frame_title_buf_end = frame_title_buf + size;
13078 frame_title_ptr = NULL;
13079 }
13080 #endif /* HAVE_WINDOW_SYSTEM */
13081 }
13082
13083