]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(string_char_and_length): Fix previous change.
[gnu-emacs] / 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 #ifdef STDC_HEADERS
173 #include <stdlib.h>
174 #endif
175 #include "lisp.h"
176 #include "frame.h"
177 #include "window.h"
178 #include "termchar.h"
179 #include "dispextern.h"
180 #include "buffer.h"
181 #include "charset.h"
182 #include "indent.h"
183 #include "commands.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "keyboard.h"
189 #include "coding.h"
190 #include "process.h"
191 #include "region-cache.h"
192
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.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, QCwhen;
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 toolbar buttons raised when the mouse moves
231 over them. */
232
233 int auto_raise_toolbar_buttons_p;
234
235 /* Margin around toolbar buttons in pixels. */
236
237 int toolbar_button_margin;
238
239 /* Thickness of shadow to draw around toolbar buttons. */
240
241 int toolbar_button_relief;
242
243 /* Non-zero means automatically resize toolbars so that all toolbar
244 items are visible, and no blank lines remain. */
245
246 int auto_resize_toolbars_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, Qwidth, Qalign_to;
255 extern Lisp_Object Qface, Qinvisible, Qimage;
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, Qheight, Qraise;
261
262 /* Name of the variable controlling the highlighting of trailing
263 whitespace. The implementation uses find_symbol_value to get its
264 value. */
265
266 Lisp_Object Qshow_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 characters of overlap to show, when scrolling a one-line
387 window such as a minibuffer. */
388
389 static int minibuffer_scroll_overlap;
390
391 /* Number of windows showing the buffer of the selected window (or
392 another buffer with the same base buffer). keyboard.c refers to
393 this. */
394
395 int buffer_shared;
396
397 /* Vector containing glyphs for an ellipsis `...'. */
398
399 static Lisp_Object default_invis_vector[3];
400
401 /* Nonzero means display mode line highlighted. */
402
403 int mode_line_inverse_video;
404
405 /* Prompt to display in front of the mini-buffer contents. */
406
407 Lisp_Object minibuf_prompt;
408
409 /* Width of current mini-buffer prompt. Only set after display_line
410 of the line that contains the prompt. */
411
412 int minibuf_prompt_width;
413 int minibuf_prompt_pixel_width;
414
415 /* Message to display instead of mini-buffer contents. This is what
416 the functions error and message make, and command echoing uses it
417 as well. It overrides the minibuf_prompt as well as the buffer. */
418
419 char *echo_area_glyphs;
420
421 /* A Lisp string to display instead of mini-buffer contents, analogous
422 to echo_area_glyphs. If this is a string, display that string.
423 Otherwise, if echo_area_glyphs is non-null, display that. */
424
425 Lisp_Object echo_area_message;
426
427 /* This is the length of the message in echo_area_glyphs or
428 echo_area_message. */
429
430 int echo_area_glyphs_length;
431
432 /* Value of echo_area_glyphs when it was last acted on. If this is
433 nonzero, there is a message on the frame in the mini-buffer and it
434 should be erased as soon as it is no longer requested to appear. */
435
436 char *previous_echo_glyphs;
437 Lisp_Object previous_echo_area_message;
438 static int previous_echo_glyphs_length;
439
440 /* This is the window where the echo area message was displayed. It
441 is always a mini-buffer window, but it may not be the same window
442 currently active as a mini-buffer. */
443
444 Lisp_Object echo_area_window;
445
446 /* Nonzero means multibyte characters were enabled when the echo area
447 message was specified. */
448
449 int message_enable_multibyte;
450
451 /* True if we should redraw the mode lines on the next redisplay. */
452
453 int update_mode_lines;
454
455 /* Smallest number of characters before the gap at any time since last
456 redisplay that finished. Valid for current buffer when
457 try_window_id can be called. */
458
459 int beg_unchanged;
460
461 /* Smallest number of characters after the gap at any time since last
462 redisplay that finished. Valid for current buffer when
463 try_window_id can be called. */
464
465 int end_unchanged;
466
467 /* MODIFF as of last redisplay that finished; if it matches MODIFF,
468 and overlay_unchanged_modified matches OVERLAY_MODIFF, that means
469 beg_unchanged and end_unchanged contain no useful information. */
470
471 int unchanged_modified;
472
473 /* OVERLAY_MODIFF as of last redisplay that finished. */
474
475 int overlay_unchanged_modified;
476
477 /* Nonzero if window sizes or contents have changed since last
478 redisplay that finished */
479
480 int windows_or_buffers_changed;
481
482 /* Nonzero after display_mode_line if %l was used and it displayed a
483 line number. */
484
485 int line_number_displayed;
486
487 /* Maximum buffer size for which to display line numbers. */
488
489 static int line_number_display_limit;
490
491 /* Number of lines to keep in the message log buffer. t means
492 infinite. nil means don't log at all. */
493
494 Lisp_Object Vmessage_log_max;
495
496 /* A scratch glyph row with contents used for generating truncation
497 glyphs. Also used in direct_output_for_insert. */
498
499 #define MAX_SCRATCH_GLYPHS 100
500 struct glyph_row scratch_glyph_row;
501 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
502
503 /* Ascent and height of the last line processed by move_it_to. */
504
505 static int last_max_ascent, last_height;
506
507 /* The maximum distance to look ahead for text properties. Values
508 that are too small let us call compute_char_face and similar
509 functions too often which is expensive. Values that are too large
510 let us call compute_char_face and alike too often because we
511 might not be interested in text properties that far away. */
512
513 #define TEXT_PROP_DISTANCE_LIMIT 100
514
515 /* Non-zero means print traces of redisplay if compiled with
516 GLYPH_DEBUG != 0. */
517
518 #if GLYPH_DEBUG
519 int trace_redisplay_p;
520 #endif
521
522 /* Value returned from text property handlers (see below). */
523
524 enum prop_handled
525 {
526 HANDLED_NORMALLY,
527 HANDLED_RECOMPUTE_PROPS,
528 HANDLED_OVERLAY_STRING_CONSUMED,
529 HANDLED_RETURN
530 };
531
532 /* A description of text properties that redisplay is interested
533 in. */
534
535 struct props
536 {
537 /* The name of the property. */
538 Lisp_Object *name;
539
540 /* A unique index for the property. */
541 enum prop_idx idx;
542
543 /* A handler function called to set up iterator IT from the property
544 at IT's current position. Value is used to steer handle_stop. */
545 enum prop_handled (*handler) P_ ((struct it *it));
546 };
547
548 static enum prop_handled handle_face_prop P_ ((struct it *));
549 static enum prop_handled handle_invisible_prop P_ ((struct it *));
550 static enum prop_handled handle_display_prop P_ ((struct it *));
551 static enum prop_handled handle_overlay_change P_ ((struct it *));
552 static enum prop_handled handle_fontified_prop P_ ((struct it *));
553
554 /* Properties handled by iterators. */
555
556 static struct props it_props[] =
557 {
558 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
559 /* Handle `face' before `display' because some sub-properties of
560 `display' need to know the face. */
561 {&Qface, FACE_PROP_IDX, handle_face_prop},
562 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
563 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
564 {NULL, 0, NULL}
565 };
566
567 /* Value is the position described by X. If X is a marker, value is
568 the marker_position of X. Otherwise, value is X. */
569
570 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
571
572 /* Enumeration returned by some move_it_.* functions internally. */
573
574 enum move_it_result
575 {
576 /* Not used. Undefined value. */
577 MOVE_UNDEFINED,
578
579 /* Move ended at the requested buffer position or ZV. */
580 MOVE_POS_MATCH_OR_ZV,
581
582 /* Move ended at the requested X pixel position. */
583 MOVE_X_REACHED,
584
585 /* Move within a line ended at the end of a line that must be
586 continued. */
587 MOVE_LINE_CONTINUED,
588
589 /* Move within a line ended at the end of a line that would
590 be displayed truncated. */
591 MOVE_LINE_TRUNCATED,
592
593 /* Move within a line ended at a line end. */
594 MOVE_NEWLINE_OR_CR
595 };
596
597
598 \f
599 /* Function prototypes. */
600
601 static int string_char_and_length P_ ((unsigned char *, int, int *));
602 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
603 struct text_pos));
604 static int compute_window_start_on_continuation_line P_ ((struct window *));
605 static Lisp_Object eval_handler P_ ((Lisp_Object));
606 static Lisp_Object eval_form P_ ((Lisp_Object));
607 static void insert_left_trunc_glyphs P_ ((struct it *));
608 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
609 static void extend_face_to_end_of_line P_ ((struct it *));
610 static void append_space P_ ((struct it *, int));
611 static void make_cursor_line_fully_visible P_ ((struct window *));
612 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
613 static int trailing_whitespace_p P_ ((int));
614 static int message_log_check_duplicate P_ ((int, int, int, int));
615 int invisible_p P_ ((Lisp_Object, Lisp_Object));
616 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
617 static void push_it P_ ((struct it *));
618 static void pop_it P_ ((struct it *));
619 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
620 static void redisplay_internal P_ ((int));
621 static void echo_area_display P_ ((int));
622 static void redisplay_windows P_ ((Lisp_Object));
623 static void redisplay_window P_ ((Lisp_Object, int));
624 static void update_menu_bar P_ ((struct frame *, int));
625 static int try_window_reusing_current_matrix P_ ((struct window *));
626 static int try_window_id P_ ((struct window *));
627 static int display_line P_ ((struct it *));
628 static void display_mode_lines P_ ((struct window *));
629 static void display_mode_line P_ ((struct window *, enum face_id,
630 Lisp_Object));
631 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
632 static char *decode_mode_spec P_ ((struct window *, char, int, int));
633 static void display_menu_bar P_ ((struct window *));
634 static int display_count_lines P_ ((int, int, int, int, int *));
635 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
636 int, int, struct it *, int, int, int, int));
637 static void compute_line_metrics P_ ((struct it *));
638 static void run_redisplay_end_trigger_hook P_ ((struct it *));
639 static int get_overlay_strings P_ ((struct it *));
640 static void next_overlay_string P_ ((struct it *));
641 void set_iterator_to_next P_ ((struct it *));
642 static void reseat P_ ((struct it *, struct text_pos, int));
643 static void reseat_1 P_ ((struct it *, struct text_pos, int));
644 static void back_to_previous_visible_line_start P_ ((struct it *));
645 static void reseat_at_previous_visible_line_start P_ ((struct it *));
646 static int next_element_from_display_vector P_ ((struct it *));
647 static int next_element_from_string P_ ((struct it *));
648 static int next_element_from_c_string P_ ((struct it *));
649 static int next_element_from_buffer P_ ((struct it *));
650 static int next_element_from_image P_ ((struct it *));
651 static int next_element_from_stretch P_ ((struct it *));
652 static void load_overlay_strings P_ ((struct it *));
653 static void init_from_display_pos P_ ((struct it *, struct window *,
654 struct display_pos *));
655 static void reseat_to_string P_ ((struct it *, unsigned char *,
656 Lisp_Object, int, int, int, int));
657 static int charset_at_position P_ ((struct text_pos));
658 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
659 int, int, int));
660 void move_it_vertically_backward P_ ((struct it *, int));
661 static void init_to_row_start P_ ((struct it *, struct window *,
662 struct glyph_row *));
663 static void init_to_row_end P_ ((struct it *, struct window *,
664 struct glyph_row *));
665 static void back_to_previous_line_start P_ ((struct it *));
666 static void forward_to_next_line_start P_ ((struct it *));
667 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
668 Lisp_Object, int));
669 static struct text_pos string_pos P_ ((int, Lisp_Object));
670 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
671 static int number_of_chars P_ ((unsigned char *, int));
672 static void compute_stop_pos P_ ((struct it *));
673 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
674 Lisp_Object));
675 static int face_before_or_after_it_pos P_ ((struct it *, int));
676 static int next_overlay_change P_ ((int));
677 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
678 Lisp_Object, struct text_pos *));
679
680 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
681 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
682
683 #ifdef HAVE_WINDOW_SYSTEM
684
685 static void update_toolbar P_ ((struct frame *, int));
686 static void build_desired_toolbar_string P_ ((struct frame *f));
687 static int redisplay_toolbar P_ ((struct frame *));
688 static void display_toolbar_line P_ ((struct it *));
689
690 #endif /* HAVE_WINDOW_SYSTEM */
691
692 \f
693 /***********************************************************************
694 Window display dimensions
695 ***********************************************************************/
696
697 /* Return the window-relative maximum y + 1 for glyph rows displaying
698 text in window W. This is the height of W minus the height of a
699 mode line, if any. */
700
701 INLINE int
702 window_text_bottom_y (w)
703 struct window *w;
704 {
705 struct frame *f = XFRAME (w->frame);
706 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
707
708 if (WINDOW_WANTS_MODELINE_P (w))
709 height -= CURRENT_MODE_LINE_HEIGHT (w);
710 return height;
711 }
712
713
714 /* Return the pixel width of display area AREA of window W. AREA < 0
715 means return the total width of W, not including bitmap areas to
716 the left and right of the window. */
717
718 INLINE int
719 window_box_width (w, area)
720 struct window *w;
721 int area;
722 {
723 struct frame *f = XFRAME (w->frame);
724 int width = XFASTINT (w->width);
725
726 if (!w->pseudo_window_p)
727 {
728 width -= FRAME_SCROLL_BAR_WIDTH (f) + 2 * FRAME_FLAGS_AREA_COLS (f);
729
730 if (area == TEXT_AREA)
731 {
732 if (INTEGERP (w->left_margin_width))
733 width -= XFASTINT (w->left_margin_width);
734 if (INTEGERP (w->right_margin_width))
735 width -= XFASTINT (w->right_margin_width);
736 }
737 else if (area == LEFT_MARGIN_AREA)
738 width = (INTEGERP (w->left_margin_width)
739 ? XFASTINT (w->left_margin_width) : 0);
740 else if (area == RIGHT_MARGIN_AREA)
741 width = (INTEGERP (w->right_margin_width)
742 ? XFASTINT (w->right_margin_width) : 0);
743 }
744
745 return width * CANON_X_UNIT (f);
746 }
747
748
749 /* Return the pixel height of the display area of window W, not
750 including mode lines of W, if any.. */
751
752 INLINE int
753 window_box_height (w)
754 struct window *w;
755 {
756 struct frame *f = XFRAME (w->frame);
757 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
758
759 if (WINDOW_WANTS_MODELINE_P (w))
760 height -= CURRENT_MODE_LINE_HEIGHT (w);
761
762 if (WINDOW_WANTS_TOP_LINE_P (w))
763 height -= CURRENT_TOP_LINE_HEIGHT (w);
764
765 return height;
766 }
767
768
769 /* Return the frame-relative coordinate of the left edge of display
770 area AREA of window W. AREA < 0 means return the left edge of the
771 whole window, to the right of any bitmap area at the left side of
772 W. */
773
774 INLINE int
775 window_box_left (w, area)
776 struct window *w;
777 int area;
778 {
779 struct frame *f = XFRAME (w->frame);
780 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
781
782 if (!w->pseudo_window_p)
783 {
784 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
785 + FRAME_FLAGS_AREA_WIDTH (f));
786
787 if (area == TEXT_AREA)
788 x += window_box_width (w, LEFT_MARGIN_AREA);
789 else if (area == RIGHT_MARGIN_AREA)
790 x += (window_box_width (w, LEFT_MARGIN_AREA)
791 + window_box_width (w, TEXT_AREA));
792 }
793
794 return x;
795 }
796
797
798 /* Return the frame-relative coordinate of the right edge of display
799 area AREA of window W. AREA < 0 means return the left edge of the
800 whole window, to the left of any bitmap area at the right side of
801 W. */
802
803 INLINE int
804 window_box_right (w, area)
805 struct window *w;
806 int area;
807 {
808 return window_box_left (w, area) + window_box_width (w, area);
809 }
810
811
812 /* Get the bounding box of the display area AREA of window W, without
813 mode lines, in frame-relative coordinates. AREA < 0 means the
814 whole window, not including bitmap areas to the left and right of
815 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
816 coordinates of the upper-left corner of the box. Return in
817 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
818
819 INLINE void
820 window_box (w, area, box_x, box_y, box_width, box_height)
821 struct window *w;
822 int area;
823 int *box_x, *box_y, *box_width, *box_height;
824 {
825 struct frame *f = XFRAME (w->frame);
826
827 *box_width = window_box_width (w, area);
828 *box_height = window_box_height (w);
829 *box_x = window_box_left (w, area);
830 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
831 + XFASTINT (w->top) * CANON_Y_UNIT (f));
832 if (WINDOW_WANTS_TOP_LINE_P (w))
833 *box_y += CURRENT_TOP_LINE_HEIGHT (w);
834 }
835
836
837 /* Get the bounding box of the display area AREA of window W, without
838 mode lines. AREA < 0 means the whole window, not including bitmap
839 areas to the left and right of the window. Return in *TOP_LEFT_X
840 and TOP_LEFT_Y the frame-relative pixel coordinates of the
841 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
842 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
843 box. */
844
845 INLINE void
846 window_box_edges (w, area, top_left_x, top_left_y,
847 bottom_right_x, bottom_right_y)
848 struct window *w;
849 int area;
850 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
851 {
852 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
853 bottom_right_y);
854 *bottom_right_x += *top_left_x;
855 *bottom_right_y += *top_left_y;
856 }
857
858
859 \f
860 /***********************************************************************
861 Utilities
862 ***********************************************************************/
863
864 /* Return the next character from STR which is MAXLEN bytes long.
865 Return in *LEN the length of the character. This is like
866 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
867 we find one, we return a `?', but with the length of the illegal
868 character. */
869
870 static INLINE int
871 string_char_and_length (str, maxlen, len)
872 unsigned char *str;
873 int maxlen, *len;
874 {
875 int c;
876
877 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
878 if (!CHAR_VALID_P (c, 1))
879 /* We may not change the length here because other places in Emacs
880 don't use this function, i.e. they silently accept illegal
881 characters. */
882 c = '?';
883
884 return c;
885 }
886
887
888
889 /* Given a position POS containing a valid character and byte position
890 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
891
892 static struct text_pos
893 string_pos_nchars_ahead (pos, string, nchars)
894 struct text_pos pos;
895 Lisp_Object string;
896 int nchars;
897 {
898 xassert (STRINGP (string) && nchars >= 0);
899
900 if (STRING_MULTIBYTE (string))
901 {
902 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
903 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
904 int len;
905
906 while (nchars--)
907 {
908 string_char_and_length (p, rest, &len);
909 p += len, rest -= len;
910 xassert (rest >= 0);
911 CHARPOS (pos) += 1;
912 BYTEPOS (pos) += len;
913 }
914 }
915 else
916 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
917
918 return pos;
919 }
920
921
922 /* Value is the text position, i.e. character and byte position,
923 for character position CHARPOS in STRING. */
924
925 static INLINE struct text_pos
926 string_pos (charpos, string)
927 int charpos;
928 Lisp_Object string;
929 {
930 struct text_pos pos;
931 xassert (STRINGP (string));
932 xassert (charpos >= 0);
933 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
934 return pos;
935 }
936
937
938 /* Value is a text position, i.e. character and byte position, for
939 character position CHARPOS in C string S. MULTIBYTE_P non-zero
940 means recognize multibyte characters. */
941
942 static struct text_pos
943 c_string_pos (charpos, s, multibyte_p)
944 int charpos;
945 unsigned char *s;
946 int multibyte_p;
947 {
948 struct text_pos pos;
949
950 xassert (s != NULL);
951 xassert (charpos >= 0);
952
953 if (multibyte_p)
954 {
955 int rest = strlen (s), len;
956
957 SET_TEXT_POS (pos, 0, 0);
958 while (charpos--)
959 {
960 string_char_and_length (s, rest, &len);
961 s += len, rest -= len;
962 xassert (rest >= 0);
963 CHARPOS (pos) += 1;
964 BYTEPOS (pos) += len;
965 }
966 }
967 else
968 SET_TEXT_POS (pos, charpos, charpos);
969
970 return pos;
971 }
972
973
974 /* Value is the number of characters in C string S. MULTIBYTE_P
975 non-zero means recognize multibyte characters. */
976
977 static int
978 number_of_chars (s, multibyte_p)
979 unsigned char *s;
980 int multibyte_p;
981 {
982 int nchars;
983
984 if (multibyte_p)
985 {
986 int rest = strlen (s), len;
987 unsigned char *p = (unsigned char *) s;
988
989 for (nchars = 0; rest > 0; ++nchars)
990 {
991 string_char_and_length (p, rest, &len);
992 rest -= len, p += len;
993 }
994 }
995 else
996 nchars = strlen (s);
997
998 return nchars;
999 }
1000
1001
1002 /* Compute byte position NEWPOS->bytepos corresponding to
1003 NEWPOS->charpos. POS is a known position in string STRING.
1004 NEWPOS->charpos must be >= POS.charpos. */
1005
1006 static void
1007 compute_string_pos (newpos, pos, string)
1008 struct text_pos *newpos, pos;
1009 Lisp_Object string;
1010 {
1011 xassert (STRINGP (string));
1012 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1013
1014 if (STRING_MULTIBYTE (string))
1015 *newpos = string_pos_nchars_ahead (pos, CHARPOS (*newpos) - CHARPOS (pos),
1016 string);
1017 else
1018 BYTEPOS (*newpos) = CHARPOS (*newpos);
1019 }
1020
1021
1022 /* Return the charset of the character at position POS in
1023 current_buffer. */
1024
1025 static int
1026 charset_at_position (pos)
1027 struct text_pos pos;
1028 {
1029 int c, multibyte_p;
1030 unsigned char *p = BYTE_POS_ADDR (BYTEPOS (pos));
1031
1032 multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1033 if (multibyte_p)
1034 {
1035 int maxlen = ((BYTEPOS (pos) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
1036 - BYTEPOS (pos));
1037 int len;
1038 c = string_char_and_length (p, maxlen, &len);
1039 }
1040 else
1041 c = *p;
1042
1043 return CHAR_CHARSET (c);
1044 }
1045
1046
1047 \f
1048 /***********************************************************************
1049 Lisp form evaluation
1050 ***********************************************************************/
1051
1052 /* Error handler for eval_form. */
1053
1054 static Lisp_Object
1055 eval_handler (arg)
1056 Lisp_Object arg;
1057 {
1058 return Qnil;
1059 }
1060
1061
1062 /* Evaluate SEXPR and return the result, or nil if something went
1063 wrong. */
1064
1065 static Lisp_Object
1066 eval_form (sexpr)
1067 Lisp_Object sexpr;
1068 {
1069 int count = specpdl_ptr - specpdl;
1070 Lisp_Object val;
1071 specbind (Qinhibit_redisplay, Qt);
1072 val = internal_condition_case_1 (Feval, sexpr, Qerror, eval_handler);
1073 return unbind_to (count, val);
1074 }
1075
1076
1077 \f
1078 /***********************************************************************
1079 Debugging
1080 ***********************************************************************/
1081
1082 #if 0
1083
1084 /* Define CHECK_IT to perform sanity checks on iterators.
1085 This is for debugging. It is too slow to do unconditionally. */
1086
1087 static void
1088 check_it (it)
1089 struct it *it;
1090 {
1091 if (it->method == next_element_from_string)
1092 {
1093 xassert (STRINGP (it->string));
1094 xassert (IT_STRING_CHARPOS (*it) >= 0);
1095 }
1096 else if (it->method == next_element_from_buffer)
1097 {
1098 /* Check that character and byte positions agree. */
1099 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1100 }
1101
1102 if (it->dpvec)
1103 xassert (it->current.dpvec_index >= 0);
1104 else
1105 xassert (it->current.dpvec_index < 0);
1106 }
1107
1108 #define CHECK_IT(IT) check_it ((IT))
1109
1110 #else /* not 0 */
1111
1112 #define CHECK_IT(IT) (void) 0
1113
1114 #endif /* not 0 */
1115
1116
1117 #if GLYPH_DEBUG
1118
1119 /* Check that the window end of window W is what we expect it
1120 to be---the last row in the current matrix displaying text. */
1121
1122 static void
1123 check_window_end (w)
1124 struct window *w;
1125 {
1126 if (!MINI_WINDOW_P (w)
1127 && !NILP (w->window_end_valid))
1128 {
1129 struct glyph_row *row;
1130 xassert ((row = MATRIX_ROW (w->current_matrix,
1131 XFASTINT (w->window_end_vpos)),
1132 !row->enabled_p
1133 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1134 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1135 }
1136 }
1137
1138 #define CHECK_WINDOW_END(W) check_window_end ((W))
1139
1140 #else /* not GLYPH_DEBUG */
1141
1142 #define CHECK_WINDOW_END(W) (void) 0
1143
1144 #endif /* not GLYPH_DEBUG */
1145
1146
1147 \f
1148 /***********************************************************************
1149 Iterator initialization
1150 ***********************************************************************/
1151
1152 /* Initialize IT for displaying current_buffer in window W, starting
1153 at character position CHARPOS. CHARPOS < 0 means that no buffer
1154 position is specified which is useful when the iterator is assigned
1155 a position later. BYTEPOS is the byte position corresponding to
1156 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1157
1158 If ROW is not null, calls to produce_glyphs with IT as parameter
1159 will produce glyphs in that row.
1160
1161 BASE_FACE_ID is the id of a base face to use. It must be one of
1162 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1163 TOP_LINE_FACE_ID for displaying mode lines, or TOOLBAR_FACE_ID for
1164 displaying the toolbar.
1165
1166 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1167 TOP_LINE_FACE_ID, the iterator will be initialized to use the
1168 corresponding mode line glyph row of the desired matrix of W. */
1169
1170 void
1171 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1172 struct it *it;
1173 struct window *w;
1174 int charpos, bytepos;
1175 struct glyph_row *row;
1176 enum face_id base_face_id;
1177 {
1178 int highlight_region_p;
1179 Lisp_Object value;
1180
1181 /* Some precondition checks. */
1182 xassert (w != NULL && it != NULL);
1183 xassert (charpos < 0 || current_buffer == XBUFFER (w->buffer));
1184 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1185
1186 /* If face attributes have been changed since the last redisplay,
1187 free realized faces now because they depend on face definitions
1188 that might have changed. */
1189 if (face_change_count)
1190 {
1191 face_change_count = 0;
1192 free_all_realized_faces (Qnil);
1193 }
1194
1195 /* Use one of the mode line rows of W's desired matrix if
1196 appropriate. */
1197 if (row == NULL)
1198 {
1199 if (base_face_id == MODE_LINE_FACE_ID)
1200 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1201 else if (base_face_id == TOP_LINE_FACE_ID)
1202 row = MATRIX_TOP_LINE_ROW (w->desired_matrix);
1203 }
1204
1205 /* Clear IT. */
1206 bzero (it, sizeof *it);
1207 it->current.overlay_string_index = -1;
1208 it->current.dpvec_index = -1;
1209 it->charset = CHARSET_ASCII;
1210 it->base_face_id = base_face_id;
1211
1212 /* The window in which we iterate over current_buffer: */
1213 XSETWINDOW (it->window, w);
1214 it->w = w;
1215 it->f = XFRAME (w->frame);
1216
1217 /* If realized faces have been removed, e.g. because of face
1218 attribute changes of named faces, recompute them. */
1219 if (FRAME_FACE_CACHE (it->f)->used == 0)
1220 recompute_basic_faces (it->f);
1221
1222 /* Should we highlight trailing whitespace? */
1223 value = find_symbol_value (Qshow_trailing_whitespace);
1224 it->show_trailing_whitespace_p
1225 = EQ (value, Qunbound) ? 0 : !NILP (value);
1226
1227 /* Current value of the `space-width', and 'height' properties. */
1228 it->space_width = Qnil;
1229 it->font_height = Qnil;
1230
1231 /* Are control characters displayed as `^C'? */
1232 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1233
1234 /* -1 means everything between a CR and the following line end
1235 is invisible. >0 means lines indented more than this value are
1236 invisible. */
1237 it->selective = (INTEGERP (current_buffer->selective_display)
1238 ? XFASTINT (current_buffer->selective_display)
1239 : (!NILP (current_buffer->selective_display)
1240 ? -1 : 0));
1241 it->selective_display_ellipsis_p
1242 = !NILP (current_buffer->selective_display_ellipses);
1243
1244 /* Display table to use. */
1245 it->dp = window_display_table (w);
1246
1247 /* Are multibyte characters enabled in current_buffer? */
1248 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1249
1250 /* Non-zero if we should highlight the region. */
1251 highlight_region_p
1252 = (!NILP (Vtransient_mark_mode)
1253 && !NILP (current_buffer->mark_active)
1254 && XMARKER (current_buffer->mark)->buffer != 0);
1255
1256 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1257 start and end of a visible region in window IT->w. Set both to
1258 -1 to indicate no region. */
1259 if (highlight_region_p
1260 /* Maybe highlight only in selected window. */
1261 && (/* Either show region everywhere. */
1262 highlight_nonselected_windows
1263 /* Or show region in the selected window. */
1264 || w == XWINDOW (selected_window)
1265 /* Or show the region if we are in the mini-buffer and W is
1266 the window the mini-buffer refers to. */
1267 || (MINI_WINDOW_P (XWINDOW (selected_window))
1268 && w == XWINDOW (Vminibuf_scroll_window))))
1269 {
1270 int charpos = marker_position (current_buffer->mark);
1271 it->region_beg_charpos = min (PT, charpos);
1272 it->region_end_charpos = max (PT, charpos);
1273 }
1274 else
1275 it->region_beg_charpos = it->region_end_charpos = -1;
1276
1277 /* Get the position at which the redisplay_end_trigger hook should
1278 be run, if it is to be run at all. */
1279 if (MARKERP (w->redisplay_end_trigger)
1280 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1281 it->redisplay_end_trigger_charpos
1282 = marker_position (w->redisplay_end_trigger);
1283 else if (INTEGERP (w->redisplay_end_trigger))
1284 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1285
1286 /* Correct bogus values of tab_width. */
1287 it->tab_width = XINT (current_buffer->tab_width);
1288 if (it->tab_width <= 0 || it->tab_width > 1000)
1289 it->tab_width = 8;
1290
1291 /* Are lines in the display truncated? */
1292 it->truncate_lines_p
1293 = (base_face_id != DEFAULT_FACE_ID
1294 || XINT (it->w->hscroll)
1295 || (truncate_partial_width_windows
1296 && !WINDOW_FULL_WIDTH_P (it->w))
1297 || !NILP (current_buffer->truncate_lines));
1298
1299 /* Get dimensions of truncation and continuation glyphs. These are
1300 displayed as bitmaps under X, so we don't need them for such
1301 frames. */
1302 if (!FRAME_WINDOW_P (it->f))
1303 {
1304 if (it->truncate_lines_p)
1305 {
1306 /* We will need the truncation glyph. */
1307 xassert (it->glyph_row == NULL);
1308 produce_special_glyphs (it, IT_TRUNCATION);
1309 it->truncation_pixel_width = it->pixel_width;
1310 }
1311 else
1312 {
1313 /* We will need the continuation glyph. */
1314 xassert (it->glyph_row == NULL);
1315 produce_special_glyphs (it, IT_CONTINUATION);
1316 it->continuation_pixel_width = it->pixel_width;
1317 }
1318
1319 /* Reset these values to zero becaue the produce_special_glyphs
1320 above has changed them. */
1321 it->pixel_width = it->ascent = it->descent = 0;
1322 }
1323
1324 /* Set this after getting the dimensions of truncation and
1325 continuation glyphs, so that we don't produce glyphs when calling
1326 produce_special_glyphs, above. */
1327 it->glyph_row = row;
1328 it->area = TEXT_AREA;
1329
1330 /* Get the dimensions of the display area. The display area
1331 consists of the visible window area plus a horizontally scrolled
1332 part to the left of the window. All x-values are relative to the
1333 start of this total display area. */
1334 if (base_face_id != DEFAULT_FACE_ID)
1335 {
1336 /* Mode lines, menu bar in terminal frames. */
1337 it->first_visible_x = 0;
1338 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1339 }
1340 else
1341 {
1342 it->first_visible_x
1343 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1344 it->last_visible_x = (it->first_visible_x
1345 + window_box_width (w, TEXT_AREA));
1346
1347 /* If we truncate lines, leave room for the truncator glyph(s) at
1348 the right margin. Otherwise, leave room for the continuation
1349 glyph(s). Truncation and continuation glyphs are not inserted
1350 for window-based redisplay. */
1351 if (!FRAME_WINDOW_P (it->f))
1352 {
1353 if (it->truncate_lines_p)
1354 it->last_visible_x -= it->truncation_pixel_width;
1355 else
1356 it->last_visible_x -= it->continuation_pixel_width;
1357 }
1358
1359 it->top_line_p = WINDOW_WANTS_TOP_LINE_P (w);
1360 it->current_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w) + w->vscroll;
1361 }
1362
1363 /* Leave room for a border glyph. */
1364 if (!FRAME_WINDOW_P (it->f)
1365 && !WINDOW_RIGHTMOST_P (it->w))
1366 it->last_visible_x -= 1;
1367
1368 it->last_visible_y = window_text_bottom_y (w);
1369
1370 /* For mode lines and alike, arrange for the first glyph having a
1371 left box line if the face specifies a box. */
1372 if (base_face_id != DEFAULT_FACE_ID)
1373 {
1374 struct face *face;
1375
1376 it->face_id = base_face_id;
1377
1378 /* If we have a boxed mode line, make the first character appear
1379 with a left box line. */
1380 face = FACE_FROM_ID (it->f, base_face_id);
1381 if (face->box != FACE_NO_BOX)
1382 it->start_of_box_run_p = 1;
1383 }
1384
1385 /* If a buffer position was specified, set the iterator there,
1386 getting overlays and face properties from that position. */
1387 if (charpos > 0)
1388 {
1389 it->end_charpos = ZV;
1390 it->face_id = -1;
1391 IT_CHARPOS (*it) = charpos;
1392
1393 /* Compute byte position if not specified. */
1394 if (bytepos <= 0)
1395 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1396 else
1397 IT_BYTEPOS (*it) = bytepos;
1398
1399 /* Compute faces etc. */
1400 reseat (it, it->current.pos, 1);
1401 }
1402
1403 CHECK_IT (it);
1404 }
1405
1406
1407 /* Initialize IT for the display of window W with window start POS. */
1408
1409 void
1410 start_display (it, w, pos)
1411 struct it *it;
1412 struct window *w;
1413 struct text_pos pos;
1414 {
1415 int start_at_line_beg_p;
1416 struct glyph_row *row;
1417 int first_vpos = WINDOW_WANTS_TOP_LINE_P (w) ? 1 : 0;
1418 int first_y;
1419
1420 row = w->desired_matrix->rows + first_vpos;
1421 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1422 first_y = it->current_y;
1423
1424 /* If window start is not at a line start, move back to the line
1425 start. This makes sure that we take continuation lines into
1426 account. */
1427 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1428 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1429 if (!start_at_line_beg_p)
1430 reseat_at_previous_visible_line_start (it);
1431
1432 #if NO_PROMPT_IN_BUFFER
1433 /* Take the mini-buffer prompt width into account for tab
1434 calculations. */
1435 if (MINI_WINDOW_P (w) && IT_CHARPOS (*it) == BEGV)
1436 {
1437 /* Why is mini-buffer_prompt_width guaranteed to be set here? */
1438 it->prompt_width = minibuf_prompt_pixel_width;
1439 }
1440 #endif /* NO_PROMPT_IN_BUFFER */
1441
1442 /* If window start is not at a line start, skip forward to POS to
1443 get the correct continuation_lines_width and current_x. */
1444 if (!start_at_line_beg_p)
1445 {
1446 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1447
1448 /* If lines are continued, this line may end in the middle of a
1449 multi-glyph character (e.g. a control character displayed as
1450 \003, or in the middle of an overlay string). In this case
1451 move_it_to above will not have taken us to the start of
1452 the continuation line but to the end of the continued line. */
1453 if (!it->truncate_lines_p && it->current_x > 0)
1454 {
1455 if (it->current.dpvec_index >= 0
1456 || it->current.overlay_string_index >= 0)
1457 {
1458 set_iterator_to_next (it);
1459 move_it_in_display_line_to (it, -1, -1, 0);
1460 }
1461 it->continuation_lines_width += it->current_x;
1462 }
1463
1464 it->current_y = first_y;
1465 it->vpos = 0;
1466 it->current_x = it->hpos = 0;
1467 }
1468
1469 #if 0 /* Don't assert the following because start_display is sometimes
1470 called intentionally with a window start that is not at a
1471 line start. Please leave this code in as a comment. */
1472
1473 /* Window start should be on a line start, now. */
1474 xassert (it->continuation_lines_width
1475 || IT_CHARPOS (it) == BEGV
1476 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1477 #endif /* 0 */
1478 }
1479
1480
1481 /* Initialize IT for stepping through current_buffer in window W,
1482 starting at position POS that includes overlay string and display
1483 vector/ control character translation position information. */
1484
1485 static void
1486 init_from_display_pos (it, w, pos)
1487 struct it *it;
1488 struct window *w;
1489 struct display_pos *pos;
1490 {
1491 /* Keep in mind: the call to reseat in init_iterator skips invisible
1492 text, so we might end up at a position different from POS. This
1493 is only a problem when POS is a row start after a newline and an
1494 overlay starts there with an after-string, and the overlay has an
1495 invisible property. Since we don't skip invisible text in
1496 display_line and elsewhere immediately after consuming the
1497 newline before the row start, such a POS will not be in a string,
1498 but the call to init_iterator below will move us to the
1499 after-string. */
1500 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1501 NULL, DEFAULT_FACE_ID);
1502
1503 /* If position is within an overlay string, set up IT to
1504 the right overlay string. */
1505 if (pos->overlay_string_index >= 0)
1506 {
1507 int relative_index;
1508
1509 /* We already have the first chunk of overlay strings in
1510 IT->overlay_strings. Load more until the one for
1511 pos->overlay_string_index is in IT->overlay_strings. */
1512 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1513 {
1514 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1515 it->current.overlay_string_index = 0;
1516 while (n--)
1517 {
1518 load_overlay_strings (it);
1519 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1520 }
1521 }
1522
1523 it->current.overlay_string_index = pos->overlay_string_index;
1524 relative_index = (it->current.overlay_string_index
1525 % OVERLAY_STRING_CHUNK_SIZE);
1526 it->string = it->overlay_strings[relative_index];
1527 it->current.string_pos = pos->string_pos;
1528 it->method = next_element_from_string;
1529 }
1530 else if (CHARPOS (pos->string_pos) >= 0)
1531 {
1532 /* Recorded position is not in an overlay string, but in another
1533 string. This can only be a string from a `display' property.
1534 IT should already be filled with that string. */
1535 it->current.string_pos = pos->string_pos;
1536 xassert (STRINGP (it->string));
1537 }
1538
1539 /* Restore position in display vector translations or control
1540 character translations. */
1541 if (pos->dpvec_index >= 0)
1542 {
1543 /* This fills IT->dpvec. */
1544 get_next_display_element (it);
1545 xassert (it->dpvec && it->current.dpvec_index == 0);
1546 it->current.dpvec_index = pos->dpvec_index;
1547 }
1548
1549 CHECK_IT (it);
1550 }
1551
1552
1553 /* Initialize IT for stepping through current_buffer in window W
1554 starting at ROW->start. */
1555
1556 static void
1557 init_to_row_start (it, w, row)
1558 struct it *it;
1559 struct window *w;
1560 struct glyph_row *row;
1561 {
1562 init_from_display_pos (it, w, &row->start);
1563 it->continuation_lines_width = row->continuation_lines_width;
1564 CHECK_IT (it);
1565 }
1566
1567
1568 /* Initialize IT for stepping through current_buffer in window W
1569 starting in the line following ROW, i.e. starting at ROW->end. */
1570
1571 static void
1572 init_to_row_end (it, w, row)
1573 struct it *it;
1574 struct window *w;
1575 struct glyph_row *row;
1576 {
1577 init_from_display_pos (it, w, &row->end);
1578
1579 if (row->continued_p)
1580 it->continuation_lines_width = (row->continuation_lines_width
1581 + row->pixel_width);
1582 CHECK_IT (it);
1583 }
1584
1585
1586
1587 \f
1588 /***********************************************************************
1589 Text properties
1590 ***********************************************************************/
1591
1592 /* Called when IT reaches IT->stop_charpos. Handle text property and
1593 overlay changes. Set IT->stop_charpos to the next position where
1594 to stop. */
1595
1596 static void
1597 handle_stop (it)
1598 struct it *it;
1599 {
1600 enum prop_handled handled;
1601 int handle_overlay_change_p = 1;
1602 struct props *p;
1603
1604 it->dpvec = NULL;
1605 it->current.dpvec_index = -1;
1606
1607 do
1608 {
1609 handled = HANDLED_NORMALLY;
1610
1611 /* Call text property handlers. */
1612 for (p = it_props; p->handler; ++p)
1613 {
1614 handled = p->handler (it);
1615
1616 if (handled == HANDLED_RECOMPUTE_PROPS)
1617 break;
1618 else if (handled == HANDLED_RETURN)
1619 return;
1620 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1621 handle_overlay_change_p = 0;
1622 }
1623
1624 if (handled != HANDLED_RECOMPUTE_PROPS)
1625 {
1626 /* Don't check for overlay strings below when set to deliver
1627 characters from a display vector. */
1628 if (it->method == next_element_from_display_vector)
1629 handle_overlay_change_p = 0;
1630
1631 /* Handle overlay changes. */
1632 if (handle_overlay_change_p)
1633 handled = handle_overlay_change (it);
1634
1635 /* Determine where to stop next. */
1636 if (handled == HANDLED_NORMALLY)
1637 compute_stop_pos (it);
1638 }
1639 }
1640 while (handled == HANDLED_RECOMPUTE_PROPS);
1641 }
1642
1643
1644 /* Compute IT->stop_charpos from text property and overlay change
1645 information for IT's current position. */
1646
1647 static void
1648 compute_stop_pos (it)
1649 struct it *it;
1650 {
1651 register INTERVAL iv, next_iv;
1652 Lisp_Object object, limit, position;
1653
1654 /* If nowhere else, stop at the end. */
1655 it->stop_charpos = it->end_charpos;
1656
1657 if (STRINGP (it->string))
1658 {
1659 /* Strings are usually short, so don't limit the search for
1660 properties. */
1661 object = it->string;
1662 limit = Qnil;
1663 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1664 }
1665 else
1666 {
1667 int charpos;
1668
1669 /* If next overlay change is in front of the current stop pos
1670 (which is IT->end_charpos), stop there. Note: value of
1671 next_overlay_change is point-max if no overlay change
1672 follows. */
1673 charpos = next_overlay_change (IT_CHARPOS (*it));
1674 if (charpos < it->stop_charpos)
1675 it->stop_charpos = charpos;
1676
1677 /* If showing the region, we have to stop at the region
1678 start or end because the face might change there. */
1679 if (it->region_beg_charpos > 0)
1680 {
1681 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1682 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1683 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1684 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1685 }
1686
1687 /* Set up variables for computing the stop position from text
1688 property changes. */
1689 XSETBUFFER (object, current_buffer);
1690 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1691 XSETFASTINT (position, IT_CHARPOS (*it));
1692
1693 }
1694
1695 /* Get the interval containing IT's position. Value is a null
1696 interval if there isn't such an interval. */
1697 iv = validate_interval_range (object, &position, &position, 0);
1698 if (!NULL_INTERVAL_P (iv))
1699 {
1700 Lisp_Object values_here[LAST_PROP_IDX];
1701 struct props *p;
1702
1703 /* Get properties here. */
1704 for (p = it_props; p->handler; ++p)
1705 values_here[p->idx] = textget (iv->plist, *p->name);
1706
1707 /* Look for an interval following iv that has different
1708 properties. */
1709 for (next_iv = next_interval (iv);
1710 (!NULL_INTERVAL_P (next_iv)
1711 && (NILP (limit)
1712 || XFASTINT (limit) > next_iv->position));
1713 next_iv = next_interval (next_iv))
1714 {
1715 for (p = it_props; p->handler; ++p)
1716 {
1717 Lisp_Object new_value;
1718
1719 new_value = textget (next_iv->plist, *p->name);
1720 if (!EQ (values_here[p->idx], new_value))
1721 break;
1722 }
1723
1724 if (p->handler)
1725 break;
1726 }
1727
1728 if (!NULL_INTERVAL_P (next_iv))
1729 {
1730 if (INTEGERP (limit)
1731 && next_iv->position >= XFASTINT (limit))
1732 /* No text property change up to limit. */
1733 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1734 else
1735 /* Text properties change in next_iv. */
1736 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1737 }
1738 }
1739
1740 xassert (STRINGP (it->string)
1741 || (it->stop_charpos >= BEGV
1742 && it->stop_charpos >= IT_CHARPOS (*it)));
1743 }
1744
1745
1746 /* Return the position of the next overlay change after POS in
1747 current_buffer. Value is point-max if no overlay change
1748 follows. This is like `next-overlay-change' but doesn't use
1749 xmalloc. */
1750
1751 static int
1752 next_overlay_change (pos)
1753 int pos;
1754 {
1755 int noverlays;
1756 int endpos;
1757 Lisp_Object *overlays;
1758 int len;
1759 int i;
1760
1761 /* Get all overlays at the given position. */
1762 len = 10;
1763 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1764 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1765 if (noverlays > len)
1766 {
1767 len = noverlays;
1768 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1769 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL);
1770 }
1771
1772 /* If any of these overlays ends before endpos,
1773 use its ending point instead. */
1774 for (i = 0; i < noverlays; ++i)
1775 {
1776 Lisp_Object oend;
1777 int oendpos;
1778
1779 oend = OVERLAY_END (overlays[i]);
1780 oendpos = OVERLAY_POSITION (oend);
1781 endpos = min (endpos, oendpos);
1782 }
1783
1784 return endpos;
1785 }
1786
1787
1788 \f
1789 /***********************************************************************
1790 Fontification
1791 ***********************************************************************/
1792
1793 /* Handle changes in the `fontified' property of the current buffer by
1794 calling hook functions from Qfontification_functions to fontify
1795 regions of text. */
1796
1797 static enum prop_handled
1798 handle_fontified_prop (it)
1799 struct it *it;
1800 {
1801 Lisp_Object prop, pos;
1802 enum prop_handled handled = HANDLED_NORMALLY;
1803
1804 /* Get the value of the `fontified' property at IT's current buffer
1805 position. (The `fontified' property doesn't have a special
1806 meaning in strings.) If the value is nil, call functions from
1807 Qfontification_functions. */
1808 if (!STRINGP (it->string)
1809 && it->s == NULL
1810 && !NILP (Vfontification_functions)
1811 && (pos = make_number (IT_CHARPOS (*it)),
1812 prop = Fget_char_property (pos, Qfontified, Qnil),
1813 NILP (prop)))
1814 {
1815 Lisp_Object args[2];
1816
1817 /* Run the hook functions. */
1818 args[0] = Qfontification_functions;
1819 args[1] = pos;
1820 Frun_hook_with_args (make_number (2), args);
1821
1822 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
1823 something. This avoids an endless loop if they failed to
1824 fontify the text for which reason ever. */
1825 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
1826 handled = HANDLED_RECOMPUTE_PROPS;
1827 }
1828
1829 return handled;
1830 }
1831
1832
1833 \f
1834 /***********************************************************************
1835 Faces
1836 ***********************************************************************/
1837
1838 /* Set up iterator IT from face properties at its current position.
1839 Called from handle_stop. */
1840
1841 static enum prop_handled
1842 handle_face_prop (it)
1843 struct it *it;
1844 {
1845 int new_face_id, next_stop;
1846
1847 if (!STRINGP (it->string))
1848 {
1849 new_face_id
1850 = face_at_buffer_position (it->w,
1851 IT_CHARPOS (*it),
1852 it->region_beg_charpos,
1853 it->region_end_charpos,
1854 &next_stop,
1855 (IT_CHARPOS (*it)
1856 + TEXT_PROP_DISTANCE_LIMIT),
1857 0);
1858
1859 /* Is this a start of a run of characters with box face?
1860 Caveat: this can be called for a freshly initialized
1861 iterator; face_id is -1 is this case. We know that the new
1862 face will not change until limit, i.e. if the new face has a
1863 box, all characters up to limit will have one. But, as
1864 usual, we don't know whether limit is really the end. */
1865 if (new_face_id != it->face_id)
1866 {
1867 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1868
1869 /* If new face has a box but old face has not, this is
1870 the start of a run of characters with box, i.e. it has
1871 a shadow on the left side. The value of face_id of the
1872 iterator will be -1 if this is the initial call that gets
1873 the face. In this case, we have to look in front of IT's
1874 position and see whether there is a face != new_face_id. */
1875 it->start_of_box_run_p
1876 = (new_face->box != FACE_NO_BOX
1877 && (it->face_id >= 0
1878 || IT_CHARPOS (*it) == BEG
1879 || new_face_id != face_before_it_pos (it)));
1880 it->face_box_p = new_face->box != FACE_NO_BOX;
1881 }
1882 }
1883 else
1884 {
1885 new_face_id
1886 = face_at_string_position (it->w,
1887 it->string,
1888 IT_STRING_CHARPOS (*it),
1889 (it->current.overlay_string_index >= 0
1890 ? IT_CHARPOS (*it)
1891 : 0),
1892 it->region_beg_charpos,
1893 it->region_end_charpos,
1894 &next_stop,
1895 it->base_face_id);
1896
1897 #if 0 /* This shouldn't be neccessary. Let's check it. */
1898 /* If IT is used to display a mode line we would really like to
1899 use the mode line face instead of the frame's default face. */
1900 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
1901 && new_face_id == DEFAULT_FACE_ID)
1902 new_face_id = MODE_LINE_FACE_ID;
1903 #endif
1904
1905 /* Is this a start of a run of characters with box? Caveat:
1906 this can be called for a freshly allocated iterator; face_id
1907 is -1 is this case. We know that the new face will not
1908 change until the next check pos, i.e. if the new face has a
1909 box, all characters up to that position will have a
1910 box. But, as usual, we don't know whether that position
1911 is really the end. */
1912 if (new_face_id != it->face_id)
1913 {
1914 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1915 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
1916
1917 /* If new face has a box but old face hasn't, this is the
1918 start of a run of characters with box, i.e. it has a
1919 shadow on the left side. */
1920 it->start_of_box_run_p
1921 = new_face->box && (old_face == NULL || !old_face->box);
1922 it->face_box_p = new_face->box != FACE_NO_BOX;
1923 }
1924 }
1925
1926 it->face_id = new_face_id;
1927 it->charset = CHARSET_ASCII;
1928 return HANDLED_NORMALLY;
1929 }
1930
1931
1932 /* Compute the face one character before or after the current position
1933 of IT. BEFORE_P non-zero means get the face in front of IT's
1934 position. Value is the id of the face. */
1935
1936 static int
1937 face_before_or_after_it_pos (it, before_p)
1938 struct it *it;
1939 int before_p;
1940 {
1941 int face_id, limit;
1942 int next_check_charpos;
1943 struct text_pos pos;
1944
1945 xassert (it->s == NULL);
1946
1947 if (STRINGP (it->string))
1948 {
1949 /* No face change past the end of the string (for the case
1950 we are padding with spaces). No face change before the
1951 string start. */
1952 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
1953 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
1954 return it->face_id;
1955
1956 /* Set pos to the position before or after IT's current position. */
1957 if (before_p)
1958 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
1959 else
1960 pos = string_pos (IT_STRING_CHARPOS (*it) + 1, it->string);
1961
1962 /* Get the face for ASCII, or unibyte. */
1963 face_id
1964 = face_at_string_position (it->w,
1965 it->string,
1966 CHARPOS (pos),
1967 (it->current.overlay_string_index >= 0
1968 ? IT_CHARPOS (*it)
1969 : 0),
1970 it->region_beg_charpos,
1971 it->region_end_charpos,
1972 &next_check_charpos,
1973 it->base_face_id);
1974
1975 /* Correct the face for charsets different from ASCII. Do it
1976 for the multibyte case only. The face returned above is
1977 suitable for unibyte text if IT->string is unibyte. */
1978 if (STRING_MULTIBYTE (it->string))
1979 {
1980 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
1981 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
1982 int c, len, charset;
1983
1984 c = string_char_and_length (p, rest, &len);
1985 charset = CHAR_CHARSET (c);
1986 if (charset != CHARSET_ASCII)
1987 face_id = FACE_FOR_CHARSET (it->f, face_id, charset);
1988 }
1989 }
1990 else
1991 {
1992 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
1993 pos = it->current.pos;
1994
1995 if (before_p)
1996 DEC_TEXT_POS (pos);
1997 else
1998 INC_TEXT_POS (pos);
1999
2000 /* Determine face for CHARSET_ASCII, or unibyte. */
2001 face_id = face_at_buffer_position (it->w,
2002 CHARPOS (pos),
2003 it->region_beg_charpos,
2004 it->region_end_charpos,
2005 &next_check_charpos,
2006 limit, 0);
2007
2008 /* Correct the face for charsets different from ASCII. Do it
2009 for the multibyte case only. The face returned above is
2010 suitable for unibyte text if current_buffer is unibyte. */
2011 if (it->multibyte_p)
2012 {
2013 int charset = charset_at_position (pos);
2014 if (charset != CHARSET_ASCII)
2015 face_id = FACE_FOR_CHARSET (it->f, face_id, charset);
2016 }
2017 }
2018
2019 return face_id;
2020 }
2021
2022
2023 \f
2024 /***********************************************************************
2025 Invisible text
2026 ***********************************************************************/
2027
2028 /* Set up iterator IT from invisible properties at its current
2029 position. Called from handle_stop. */
2030
2031 static enum prop_handled
2032 handle_invisible_prop (it)
2033 struct it *it;
2034 {
2035 enum prop_handled handled = HANDLED_NORMALLY;
2036
2037 if (STRINGP (it->string))
2038 {
2039 extern Lisp_Object Qinvisible;
2040 Lisp_Object prop, end_charpos, limit, charpos;
2041
2042 /* Get the value of the invisible text property at the
2043 current position. Value will be nil if there is no such
2044 property. */
2045 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2046 prop = Fget_text_property (charpos, Qinvisible, it->string);
2047
2048 if (!NILP (prop))
2049 {
2050 handled = HANDLED_RECOMPUTE_PROPS;
2051
2052 /* Get the position at which the next change of the
2053 invisible text property can be found in IT->string.
2054 Value will be nil if the property value is the same for
2055 all the rest of IT->string. */
2056 XSETINT (limit, XSTRING (it->string)->size);
2057 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2058 it->string, limit);
2059
2060 /* Text at current position is invisible. The next
2061 change in the property is at position end_charpos.
2062 Move IT's current position to that position. */
2063 if (INTEGERP (end_charpos)
2064 && XFASTINT (end_charpos) < XFASTINT (limit))
2065 {
2066 struct text_pos old;
2067 old = it->current.string_pos;
2068 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2069 compute_string_pos (&it->current.string_pos, old, it->string);
2070 }
2071 else
2072 {
2073 /* The rest of the string is invisible. If this is an
2074 overlay string, proceed with the next overlay string
2075 or whatever comes and return a character from there. */
2076 if (it->current.overlay_string_index >= 0)
2077 {
2078 next_overlay_string (it);
2079 /* Don't check for overlay strings when we just
2080 finished processing them. */
2081 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2082 }
2083 else
2084 {
2085 struct Lisp_String *s = XSTRING (it->string);
2086 IT_STRING_CHARPOS (*it) = s->size;
2087 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2088 }
2089 }
2090 }
2091 }
2092 else
2093 {
2094 int visible_p, newpos, next_stop;
2095 Lisp_Object pos, prop;
2096
2097 /* First of all, is there invisible text at this position? */
2098 XSETFASTINT (pos, IT_CHARPOS (*it));
2099 prop = Fget_char_property (pos, Qinvisible, it->window);
2100
2101 /* If we are on invisible text, skip over it. */
2102 if (TEXT_PROP_MEANS_INVISIBLE (prop))
2103 {
2104 /* Record whether we have to display an ellipsis for the
2105 invisible text. */
2106 int display_ellipsis_p
2107 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2108
2109 handled = HANDLED_RECOMPUTE_PROPS;
2110
2111 /* Loop skipping over invisible text. The loop is left at
2112 ZV or with IT on the first char being visible again. */
2113 do
2114 {
2115 /* Try to skip some invisible text. Return value is the
2116 position reached which can be equal to IT's position
2117 if there is nothing invisible here. This skips both
2118 over invisible text properties and overlays with
2119 invisible property. */
2120 newpos = skip_invisible (IT_CHARPOS (*it),
2121 &next_stop, ZV, it->window);
2122
2123 /* If we skipped nothing at all we weren't at invisible
2124 text in the first place. If everything to the end of
2125 the buffer was skipped, end the loop. */
2126 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2127 visible_p = 1;
2128 else
2129 {
2130 /* We skipped some characters but not necessarily
2131 all there are. Check if we ended up on visible
2132 text. Fget_char_property returns the property of
2133 the char before the given position, i.e. if we
2134 get visible_p = 1, this means that the char at
2135 newpos is visible. */
2136 XSETFASTINT (pos, newpos);
2137 prop = Fget_char_property (pos, Qinvisible, it->window);
2138 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2139 }
2140
2141 /* If we ended up on invisible text, proceed to
2142 skip starting with next_stop. */
2143 if (!visible_p)
2144 IT_CHARPOS (*it) = next_stop;
2145 }
2146 while (!visible_p);
2147
2148 /* The position newpos is now either ZV or on visible text. */
2149 IT_CHARPOS (*it) = newpos;
2150 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2151
2152 /* Maybe return `...' next for the end of the invisible text. */
2153 if (display_ellipsis_p)
2154 {
2155 if (it->dp
2156 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2157 {
2158 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2159 it->dpvec = v->contents;
2160 it->dpend = v->contents + v->size;
2161 }
2162 else
2163 {
2164 /* Default `...'. */
2165 it->dpvec = default_invis_vector;
2166 it->dpend = default_invis_vector + 3;
2167 }
2168
2169 /* The ellipsis display does not replace the display of
2170 the character at the new position. Indicate this by
2171 setting IT->dpvec_char_len to zero. */
2172 it->dpvec_char_len = 0;
2173
2174 it->current.dpvec_index = 0;
2175 it->method = next_element_from_display_vector;
2176 }
2177 }
2178 }
2179
2180 return handled;
2181 }
2182
2183
2184 \f
2185 /***********************************************************************
2186 'display' property
2187 ***********************************************************************/
2188
2189 /* Set up iterator IT from `display' property at its current position.
2190 Called from handle_stop. */
2191
2192 static enum prop_handled
2193 handle_display_prop (it)
2194 struct it *it;
2195 {
2196 Lisp_Object prop, object;
2197 struct text_pos *position;
2198 int space_or_image_found_p;
2199
2200 if (STRINGP (it->string))
2201 {
2202 object = it->string;
2203 position = &it->current.string_pos;
2204 }
2205 else
2206 {
2207 object = Qnil;
2208 position = &it->current.pos;
2209 }
2210
2211 /* Reset those iterator values set from display property values. */
2212 it->font_height = Qnil;
2213 it->space_width = Qnil;
2214 it->voffset = 0;
2215
2216 /* We don't support recursive `display' properties, i.e. string
2217 values that have a string `display' property, that have a string
2218 `display' property etc. */
2219 if (!it->string_from_display_prop_p)
2220 it->area = TEXT_AREA;
2221
2222 prop = Fget_char_property (make_number (position->charpos),
2223 Qdisplay, object);
2224 if (NILP (prop))
2225 return HANDLED_NORMALLY;
2226
2227 space_or_image_found_p = 0;
2228 if (CONSP (prop) && CONSP (XCAR (prop)))
2229 {
2230 while (CONSP (prop))
2231 {
2232 if (handle_single_display_prop (it, XCAR (prop), object, position))
2233 space_or_image_found_p = 1;
2234 prop = XCDR (prop);
2235 }
2236 }
2237 else if (VECTORP (prop))
2238 {
2239 int i;
2240 for (i = 0; i < XVECTOR (prop)->size; ++i)
2241 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2242 object, position))
2243 space_or_image_found_p = 1;
2244 }
2245 else
2246 {
2247 if (handle_single_display_prop (it, prop, object, position))
2248 space_or_image_found_p = 1;
2249 }
2250
2251 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2252 }
2253
2254
2255 /* Value is the position of the end of the `display' property stating
2256 at START_POS in OBJECT. */
2257
2258 static struct text_pos
2259 display_prop_end (it, object, start_pos)
2260 struct it *it;
2261 Lisp_Object object;
2262 struct text_pos start_pos;
2263 {
2264 Lisp_Object end;
2265 struct text_pos end_pos;
2266
2267 /* Characters having this form of property are not displayed, so
2268 we have to find the end of the property. */
2269 end = Fnext_single_property_change (make_number (start_pos.charpos),
2270 Qdisplay, object, Qnil);
2271 if (NILP (end))
2272 {
2273 /* A nil value of `end' means there are no changes of the
2274 property to the end of the buffer or string. */
2275 if (it->current.overlay_string_index >= 0)
2276 end_pos.charpos = XSTRING (it->string)->size;
2277 else
2278 end_pos.charpos = it->end_charpos;
2279 }
2280 else
2281 end_pos.charpos = XFASTINT (end);
2282
2283 if (STRINGP (it->string))
2284 compute_string_pos (&end_pos, start_pos, it->string);
2285 else
2286 end_pos.bytepos = CHAR_TO_BYTE (end_pos.charpos);
2287
2288 return end_pos;
2289 }
2290
2291
2292 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2293 is the object in which the `display' property was found. *POSITION
2294 is the position at which it was found.
2295
2296 If PROP is a `space' or `image' sub-property, set *POSITION to the
2297 end position of the `display' property.
2298
2299 Value is non-zero if a `space' or `image' property value was found. */
2300
2301 static int
2302 handle_single_display_prop (it, prop, object, position)
2303 struct it *it;
2304 Lisp_Object prop;
2305 Lisp_Object object;
2306 struct text_pos *position;
2307 {
2308 Lisp_Object value;
2309 int space_or_image_found_p = 0;
2310
2311 Lisp_Object form;
2312
2313 /* If PROP is a list of the form `(:when FORM VALUE)', FORM is
2314 evaluated. If the result is nil, VALUE is ignored. */
2315 form = Qt;
2316 if (CONSP (prop) && EQ (XCAR (prop), QCwhen))
2317 {
2318 prop = XCDR (prop);
2319 if (!CONSP (prop))
2320 return 0;
2321 form = XCAR (prop);
2322 prop = XCDR (prop);
2323 if (!CONSP (prop))
2324 return 0;
2325 prop = XCAR (prop);
2326 }
2327
2328 if (!NILP (form) && !EQ (form, Qt))
2329 {
2330 struct gcpro gcpro1;
2331 struct text_pos end_pos, pt;
2332
2333 end_pos = display_prop_end (it, object, *position);
2334 GCPRO1 (form);
2335
2336 /* Temporarily set point to the end position, and then evaluate
2337 the form. This makes `(eolp)' work as FORM. */
2338 CHARPOS (pt) = PT;
2339 BYTEPOS (pt) = PT_BYTE;
2340 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2341 form = eval_form (form);
2342 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2343 UNGCPRO;
2344 }
2345
2346 if (NILP (form))
2347 return 0;
2348
2349 if (CONSP (prop)
2350 && EQ (XCAR (prop), Qheight)
2351 && CONSP (XCDR (prop)))
2352 {
2353 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2354 return 0;
2355
2356 /* `(height HEIGHT)'. */
2357 it->font_height = XCAR (XCDR (prop));
2358 if (!NILP (it->font_height))
2359 {
2360 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2361 int new_height = -1;
2362
2363 if (CONSP (it->font_height)
2364 && (EQ (XCAR (it->font_height), Qplus)
2365 || EQ (XCAR (it->font_height), Qminus))
2366 && CONSP (XCDR (it->font_height))
2367 && INTEGERP (XCAR (XCDR (it->font_height))))
2368 {
2369 /* `(+ N)' or `(- N)' where N is an integer. */
2370 int steps = XINT (XCAR (XCDR (it->font_height)));
2371 if (EQ (XCAR (it->font_height), Qplus))
2372 steps = - steps;
2373 it->face_id = smaller_face (it->f, it->face_id, steps);
2374 }
2375 else if (SYMBOLP (it->font_height))
2376 {
2377 /* Call function with current height as argument.
2378 Value is the new height. */
2379 Lisp_Object form, height;
2380 struct gcpro gcpro1;
2381
2382 height = face->lface[LFACE_HEIGHT_INDEX];
2383 form = Fcons (it->font_height, Fcons (height, Qnil));
2384 GCPRO1 (form);
2385 height = eval_form (form);
2386 if (NUMBERP (height))
2387 new_height = XFLOATINT (height);
2388 UNGCPRO;
2389 }
2390 else if (NUMBERP (it->font_height))
2391 {
2392 /* Value is a multiple of the canonical char height. */
2393 struct face *face;
2394
2395 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2396 new_height = (XFLOATINT (it->font_height)
2397 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2398 }
2399 else
2400 {
2401 /* Evaluate IT->font_height with `height' bound to the
2402 current specified height to get the new height. */
2403 Lisp_Object value;
2404 int count = specpdl_ptr - specpdl;
2405
2406 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2407 value = eval_form (it->font_height);
2408 unbind_to (count, Qnil);
2409
2410 if (NUMBERP (value))
2411 new_height = XFLOATINT (value);
2412 }
2413
2414 if (new_height > 0)
2415 it->face_id = face_with_height (it->f, it->face_id, new_height);
2416 }
2417 }
2418 else if (CONSP (prop)
2419 && EQ (XCAR (prop), Qspace_width)
2420 && CONSP (XCDR (prop)))
2421 {
2422 /* `(space_width WIDTH)'. */
2423 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2424 return 0;
2425
2426 value = XCAR (XCDR (prop));
2427 if (NUMBERP (value) && XFLOATINT (value) > 0)
2428 it->space_width = value;
2429 }
2430 else if (CONSP (prop)
2431 && EQ (XCAR (prop), Qraise)
2432 && CONSP (XCDR (prop)))
2433 {
2434 #ifdef HAVE_WINDOW_SYSTEM
2435 /* `(raise FACTOR)'. */
2436 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2437 return 0;
2438
2439 value = XCAR (XCDR (prop));
2440 if (NUMBERP (value))
2441 {
2442 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2443 it->voffset = - (XFLOATINT (value)
2444 * (face->font->ascent + face->font->descent));
2445 }
2446 #endif /* HAVE_WINDOW_SYSTEM */
2447 }
2448 else if (!it->string_from_display_prop_p)
2449 {
2450 /* `(left-margin VALUE)' or `(right-margin VALUE)
2451 or `(nil VALUE)' or VALUE. */
2452 Lisp_Object location, value;
2453 struct text_pos start_pos;
2454 int valid_p;
2455
2456 /* Characters having this form of property are not displayed, so
2457 we have to find the end of the property. */
2458 space_or_image_found_p = 1;
2459 start_pos = *position;
2460 *position = display_prop_end (it, object, start_pos);
2461
2462 /* Let's stop at the new position and assume that all
2463 text properties change there. */
2464 it->stop_charpos = position->charpos;
2465
2466 if (CONSP (prop)
2467 && !EQ (XCAR (prop), Qspace)
2468 && !EQ (XCAR (prop), Qimage))
2469 {
2470 location = XCAR (prop);
2471 value = XCDR (prop);
2472 }
2473 else
2474 {
2475 location = Qnil;
2476 value = prop;
2477 }
2478
2479 #ifdef HAVE_WINDOW_SYSTEM
2480 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2481 valid_p = STRINGP (value);
2482 else
2483 valid_p = (STRINGP (value)
2484 || (CONSP (value) && EQ (XCAR (value), Qspace))
2485 || valid_image_p (value));
2486 #else /* not HAVE_WINDOW_SYSTEM */
2487 valid_p = STRINGP (value);
2488 #endif /* not HAVE_WINDOW_SYSTEM */
2489
2490 if ((EQ (location, Qleft_margin)
2491 || EQ (location, Qright_margin)
2492 || NILP (location))
2493 && valid_p)
2494 {
2495 /* Save current settings of IT so that we can restore them
2496 when we are finished with the glyph property value. */
2497 push_it (it);
2498
2499 if (NILP (location))
2500 it->area = TEXT_AREA;
2501 else if (EQ (location, Qleft_margin))
2502 it->area = LEFT_MARGIN_AREA;
2503 else
2504 it->area = RIGHT_MARGIN_AREA;
2505
2506 if (STRINGP (value))
2507 {
2508 it->string = value;
2509 it->multibyte_p = STRING_MULTIBYTE (it->string);
2510 it->current.overlay_string_index = -1;
2511 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2512 it->end_charpos = it->string_nchars
2513 = XSTRING (it->string)->size;
2514 it->method = next_element_from_string;
2515 it->stop_charpos = 0;
2516 it->string_from_display_prop_p = 1;
2517 }
2518 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2519 {
2520 it->method = next_element_from_stretch;
2521 it->object = value;
2522 it->current.pos = it->position = start_pos;
2523 }
2524 #ifdef HAVE_WINDOW_SYSTEM
2525 else
2526 {
2527 it->what = IT_IMAGE;
2528 it->image_id = lookup_image (it->f, value);
2529 it->position = start_pos;
2530 it->object = NILP (object) ? it->w->buffer : object;
2531 it->method = next_element_from_image;
2532
2533 /* Say that we don't have consumed the characters with
2534 `display' property yet. The call to pop_it in
2535 set_iterator_to_next will clean this up. */
2536 *position = start_pos;
2537 }
2538 #endif /* HAVE_WINDOW_SYSTEM */
2539 }
2540 }
2541
2542 return space_or_image_found_p;
2543 }
2544
2545
2546 \f
2547 /***********************************************************************
2548 Overlay strings
2549 ***********************************************************************/
2550
2551 /* The following structure is used to record overlay strings for
2552 later sorting in load_overlay_strings. */
2553
2554 struct overlay_entry
2555 {
2556 Lisp_Object string;
2557 int priority;
2558 int after_string_p;
2559 };
2560
2561
2562 /* Set up iterator IT from overlay strings at its current position.
2563 Called from handle_stop. */
2564
2565 static enum prop_handled
2566 handle_overlay_change (it)
2567 struct it *it;
2568 {
2569 /* Overlays are handled in current_buffer only. */
2570 if (STRINGP (it->string))
2571 return HANDLED_NORMALLY;
2572 else
2573 return (get_overlay_strings (it)
2574 ? HANDLED_RECOMPUTE_PROPS
2575 : HANDLED_NORMALLY);
2576 }
2577
2578
2579 /* Set up the next overlay string for delivery by IT, if there is an
2580 overlay string to deliver. Called by set_iterator_to_next when the
2581 end of the current overlay string is reached. If there are more
2582 overlay strings to display, IT->string and
2583 IT->current.overlay_string_index are set appropriately here.
2584 Otherwise IT->string is set to nil. */
2585
2586 static void
2587 next_overlay_string (it)
2588 struct it *it;
2589 {
2590 ++it->current.overlay_string_index;
2591 if (it->current.overlay_string_index == it->n_overlay_strings)
2592 {
2593 /* No more overlay strings. Restore IT's settings to what
2594 they were before overlay strings were processed, and
2595 continue to deliver from current_buffer. */
2596 pop_it (it);
2597 xassert (it->stop_charpos >= BEGV
2598 && it->stop_charpos <= it->end_charpos);
2599 it->string = Qnil;
2600 it->current.overlay_string_index = -1;
2601 SET_TEXT_POS (it->current.string_pos, -1, -1);
2602 it->n_overlay_strings = 0;
2603 it->method = next_element_from_buffer;
2604 }
2605 else
2606 {
2607 /* There are more overlay strings to process. If
2608 IT->current.overlay_string_index has advanced to a position
2609 where we must load IT->overlay_strings with more strings, do
2610 it. */
2611 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2612
2613 if (it->current.overlay_string_index && i == 0)
2614 load_overlay_strings (it);
2615
2616 /* Initialize IT to deliver display elements from the overlay
2617 string. */
2618 it->string = it->overlay_strings[i];
2619 it->multibyte_p = STRING_MULTIBYTE (it->string);
2620 SET_TEXT_POS (it->current.string_pos, 0, 0);
2621 it->method = next_element_from_string;
2622 it->stop_charpos = 0;
2623 }
2624
2625 CHECK_IT (it);
2626 }
2627
2628
2629 /* Compare two overlay_entry structures E1 and E2. Used as a
2630 comparison function for qsort in load_overlay_strings. Overlay
2631 strings for the same position are sorted so that
2632
2633 1. All after-strings come in front of before-strings.
2634
2635 2. Within after-strings, strings are sorted so that overlay strings
2636 from overlays with higher priorities come first.
2637
2638 2. Within before-strings, strings are sorted so that overlay
2639 strings from overlays with higher priorities come last.
2640
2641 Value is analogous to strcmp. */
2642
2643
2644 static int
2645 compare_overlay_entries (e1, e2)
2646 void *e1, *e2;
2647 {
2648 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
2649 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
2650 int result;
2651
2652 if (entry1->after_string_p != entry2->after_string_p)
2653 /* Let after-strings appear in front of before-strings. */
2654 result = entry1->after_string_p ? -1 : 1;
2655 else if (entry1->after_string_p)
2656 /* After-strings sorted in order of decreasing priority. */
2657 result = entry2->priority - entry1->priority;
2658 else
2659 /* Before-strings sorted in order of increasing priority. */
2660 result = entry1->priority - entry2->priority;
2661
2662 return result;
2663 }
2664
2665
2666 /* Load the vector IT->overlay_strings with overlay strings from IT's
2667 current buffer position. Set IT->n_overlays to the total number of
2668 overlay strings found.
2669
2670 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
2671 a time. On entry into load_overlay_strings,
2672 IT->current.overlay_string_index gives the number of overlay
2673 strings that have already been loaded by previous calls to this
2674 function.
2675
2676 Overlay strings are sorted so that after-string strings come in
2677 front of before-string strings. Within before and after-strings,
2678 strings are sorted by overlay priority. See also function
2679 compare_overlay_entries. */
2680
2681 static void
2682 load_overlay_strings (it)
2683 struct it *it;
2684 {
2685 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
2686 Lisp_Object ov, overlay, window, str;
2687 int start, end;
2688 int size = 20;
2689 int n = 0, i, j;
2690 struct overlay_entry *entries
2691 = (struct overlay_entry *) alloca (size * sizeof *entries);
2692
2693 /* Append the overlay string STRING of overlay OVERLAY to vector
2694 `entries' which has size `size' and currently contains `n'
2695 elements. AFTER_P non-zero means STRING is an after-string of
2696 OVERLAY. */
2697 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
2698 do \
2699 { \
2700 Lisp_Object priority; \
2701 \
2702 if (n == size) \
2703 { \
2704 int new_size = 2 * size; \
2705 struct overlay_entry *old = entries; \
2706 entries = \
2707 (struct overlay_entry *) alloca (new_size \
2708 * sizeof *entries); \
2709 bcopy (old, entries, size * sizeof *entries); \
2710 size = new_size; \
2711 } \
2712 \
2713 entries[n].string = (STRING); \
2714 priority = Foverlay_get ((OVERLAY), Qpriority); \
2715 entries[n].priority \
2716 = INTEGERP (priority) ? XFASTINT (priority) : 0; \
2717 entries[n].after_string_p = (AFTER_P); \
2718 ++n; \
2719 } \
2720 while (0)
2721
2722 /* Process overlay before the overlay center. */
2723 for (ov = current_buffer->overlays_before;
2724 CONSP (ov);
2725 ov = XCONS (ov)->cdr)
2726 {
2727 overlay = XCONS (ov)->car;
2728 xassert (OVERLAYP (overlay));
2729 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2730 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2731
2732 if (end < IT_CHARPOS (*it))
2733 break;
2734
2735 /* Skip this overlay if it doesn't start or end at IT's current
2736 position. */
2737 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2738 continue;
2739
2740 /* Skip this overlay if it doesn't apply to IT->w. */
2741 window = Foverlay_get (overlay, Qwindow);
2742 if (WINDOWP (window) && XWINDOW (window) != it->w)
2743 continue;
2744
2745 /* If overlay has a non-empty before-string, record it. */
2746 if (start == IT_CHARPOS (*it)
2747 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2748 && XSTRING (str)->size)
2749 RECORD_OVERLAY_STRING (overlay, str, 0);
2750
2751 /* If overlay has a non-empty after-string, record it. */
2752 if (end == IT_CHARPOS (*it)
2753 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2754 && XSTRING (str)->size)
2755 RECORD_OVERLAY_STRING (overlay, str, 1);
2756 }
2757
2758 /* Process overlays after the overlay center. */
2759 for (ov = current_buffer->overlays_after;
2760 CONSP (ov);
2761 ov = XCONS (ov)->cdr)
2762 {
2763 overlay = XCONS (ov)->car;
2764 xassert (OVERLAYP (overlay));
2765 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2766 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2767
2768 if (start > IT_CHARPOS (*it))
2769 break;
2770
2771 /* Skip this overlay if it doesn't start or end at IT's current
2772 position. */
2773 if (end != IT_CHARPOS (*it) && start != IT_CHARPOS (*it))
2774 continue;
2775
2776 /* Skip this overlay if it doesn't apply to IT->w. */
2777 window = Foverlay_get (overlay, Qwindow);
2778 if (WINDOWP (window) && XWINDOW (window) != it->w)
2779 continue;
2780
2781 /* If overlay has a non-empty before-string, record it. */
2782 if (start == IT_CHARPOS (*it)
2783 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2784 && XSTRING (str)->size)
2785 RECORD_OVERLAY_STRING (overlay, str, 0);
2786
2787 /* If overlay has a non-empty after-string, record it. */
2788 if (end == IT_CHARPOS (*it)
2789 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2790 && XSTRING (str)->size)
2791 RECORD_OVERLAY_STRING (overlay, str, 1);
2792 }
2793
2794 #undef RECORD_OVERLAY_STRING
2795
2796 /* Sort entries. */
2797 qsort (entries, n, sizeof *entries, compare_overlay_entries);
2798
2799 /* Record the total number of strings to process. */
2800 it->n_overlay_strings = n;
2801
2802 /* IT->current.overlay_string_index is the number of overlay strings
2803 that have already been consumed by IT. Copy some of the
2804 remaining overlay strings to IT->overlay_strings. */
2805 i = 0;
2806 j = it->current.overlay_string_index;
2807 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
2808 it->overlay_strings[i++] = entries[j++].string;
2809
2810 CHECK_IT (it);
2811 }
2812
2813
2814 /* Get the first chunk of overlay strings at IT's current buffer
2815 position. Value is non-zero if at least one overlay string was
2816 found. */
2817
2818 static int
2819 get_overlay_strings (it)
2820 struct it *it;
2821 {
2822 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
2823 process. This fills IT->overlay_strings with strings, and sets
2824 IT->n_overlay_strings to the total number of strings to process.
2825 IT->pos.overlay_string_index has to be set temporarily to zero
2826 because load_overlay_strings needs this; it must be set to -1
2827 when no overlay strings are found because a zero value would
2828 indicate a position in the first overlay string. */
2829 it->current.overlay_string_index = 0;
2830 load_overlay_strings (it);
2831
2832 /* If we found overlay strings, set up IT to deliver display
2833 elements from the first one. Otherwise set up IT to deliver
2834 from current_buffer. */
2835 if (it->n_overlay_strings)
2836 {
2837 /* Make sure we know settings in current_buffer, so that we can
2838 restore meaningful values when we're done with the overlay
2839 strings. */
2840 compute_stop_pos (it);
2841 xassert (it->face_id >= 0);
2842
2843 /* Save IT's settings. They are restored after all overlay
2844 strings have been processed. */
2845 xassert (it->sp == 0);
2846 push_it (it);
2847
2848 /* Set up IT to deliver display elements from the first overlay
2849 string. */
2850 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2851 it->stop_charpos = 0;
2852 it->string = it->overlay_strings[0];
2853 it->multibyte_p = STRING_MULTIBYTE (it->string);
2854 xassert (STRINGP (it->string));
2855 it->method = next_element_from_string;
2856 }
2857 else
2858 {
2859 it->string = Qnil;
2860 it->current.overlay_string_index = -1;
2861 it->method = next_element_from_buffer;
2862 }
2863
2864 CHECK_IT (it);
2865
2866 /* Value is non-zero if we found at least one overlay string. */
2867 return STRINGP (it->string);
2868 }
2869
2870
2871 \f
2872 /***********************************************************************
2873 Saving and restoring state
2874 ***********************************************************************/
2875
2876 /* Save current settings of IT on IT->stack. Called, for example,
2877 before setting up IT for an overlay string, to be able to restore
2878 IT's settings to what they were after the overlay string has been
2879 processed. */
2880
2881 static void
2882 push_it (it)
2883 struct it *it;
2884 {
2885 struct iterator_stack_entry *p;
2886
2887 xassert (it->sp < 2);
2888 p = it->stack + it->sp;
2889
2890 p->stop_charpos = it->stop_charpos;
2891 xassert (it->face_id >= 0);
2892 p->face_id = it->face_id;
2893 p->string = it->string;
2894 p->pos = it->current;
2895 p->end_charpos = it->end_charpos;
2896 p->string_nchars = it->string_nchars;
2897 p->area = it->area;
2898 p->multibyte_p = it->multibyte_p;
2899 p->space_width = it->space_width;
2900 p->font_height = it->font_height;
2901 p->voffset = it->voffset;
2902 p->string_from_display_prop_p = it->string_from_display_prop_p;
2903 ++it->sp;
2904 }
2905
2906
2907 /* Restore IT's settings from IT->stack. Called, for example, when no
2908 more overlay strings must be processed, and we return to delivering
2909 display elements from a buffer, or when the end of a string from a
2910 `display' property is reached and we return to delivering display
2911 elements from an overlay string, or from a buffer. */
2912
2913 static void
2914 pop_it (it)
2915 struct it *it;
2916 {
2917 struct iterator_stack_entry *p;
2918
2919 xassert (it->sp > 0);
2920 --it->sp;
2921 p = it->stack + it->sp;
2922 it->stop_charpos = p->stop_charpos;
2923 it->face_id = p->face_id;
2924 it->string = p->string;
2925 it->current = p->pos;
2926 it->end_charpos = p->end_charpos;
2927 it->string_nchars = p->string_nchars;
2928 it->area = p->area;
2929 it->multibyte_p = p->multibyte_p;
2930 it->space_width = p->space_width;
2931 it->font_height = p->font_height;
2932 it->voffset = p->voffset;
2933 it->string_from_display_prop_p = p->string_from_display_prop_p;
2934 }
2935
2936
2937 \f
2938 /***********************************************************************
2939 Moving over lines
2940 ***********************************************************************/
2941
2942 /* Set IT's current position to the previous line start. */
2943
2944 static void
2945 back_to_previous_line_start (it)
2946 struct it *it;
2947 {
2948 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
2949 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
2950 }
2951
2952
2953 /* Set IT's current position to the next line start. */
2954
2955 static void
2956 forward_to_next_line_start (it)
2957 struct it *it;
2958 {
2959 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), 1);
2960 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
2961 }
2962
2963
2964 /* Set IT's current position to the previous visible line start. Skip
2965 invisible text that is so either due to text properties or due to
2966 selective display. Caution: this does not change IT->current_x and
2967 IT->hpos. */
2968
2969 static void
2970 back_to_previous_visible_line_start (it)
2971 struct it *it;
2972 {
2973 int visible_p = 0;
2974
2975 /* Go back one newline if not on BEGV already. */
2976 if (IT_CHARPOS (*it) > BEGV)
2977 back_to_previous_line_start (it);
2978
2979 /* Move over lines that are invisible because of selective display
2980 or text properties. */
2981 while (IT_CHARPOS (*it) > BEGV
2982 && !visible_p)
2983 {
2984 visible_p = 1;
2985
2986 /* If selective > 0, then lines indented more than that values
2987 are invisible. */
2988 if (it->selective > 0
2989 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
2990 it->selective))
2991 visible_p = 0;
2992 #ifdef USE_TEXT_PROPERTIES
2993 else
2994 {
2995 Lisp_Object prop;
2996
2997 prop = Fget_char_property (IT_CHARPOS (*it), Qinvisible, it->window);
2998 if (TEXT_PROP_MEANS_INVISIBLE (prop))
2999 visible_p = 0;
3000 }
3001 #endif /* USE_TEXT_PROPERTIES */
3002
3003 /* Back one more newline if the current one is invisible. */
3004 if (!visible_p)
3005 back_to_previous_line_start (it);
3006 }
3007
3008 xassert (IT_CHARPOS (*it) >= BEGV);
3009 xassert (IT_CHARPOS (*it) == BEGV
3010 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3011 CHECK_IT (it);
3012 }
3013
3014
3015 /* Reseat iterator IT at the previous visible line start. Skip
3016 invisible text that is so either due to text properties or due to
3017 selective display. At the end, update IT's overlay information,
3018 face information etc. */
3019
3020 static void
3021 reseat_at_previous_visible_line_start (it)
3022 struct it *it;
3023 {
3024 back_to_previous_visible_line_start (it);
3025 reseat (it, it->current.pos, 1);
3026 CHECK_IT (it);
3027 }
3028
3029
3030 /* Reseat iterator IT on the next visible line start in the current
3031 buffer. Skip over invisible text that is so because of selective
3032 display. Compute faces, overlays etc at the new position. Note
3033 that this function does not skip over text that is invisible
3034 because of text properties. */
3035
3036 static void
3037 reseat_at_next_visible_line_start (it)
3038 struct it *it;
3039 {
3040 /* Restore the buffer position when currently not delivering display
3041 elements from the current buffer. This is the case, for example,
3042 when called at the end of a truncated overlay string. */
3043 while (it->sp)
3044 pop_it (it);
3045 it->method = next_element_from_buffer;
3046
3047 /* Otherwise, scan_buffer would not work. */
3048 if (IT_CHARPOS (*it) < ZV)
3049 {
3050 /* If on a newline, advance past it. Otherwise, find the next
3051 newline which automatically gives us the position following
3052 the newline. */
3053 if (FETCH_BYTE (IT_BYTEPOS (*it)) == '\n')
3054 {
3055 ++IT_CHARPOS (*it);
3056 ++IT_BYTEPOS (*it);
3057 }
3058 else
3059 forward_to_next_line_start (it);
3060
3061 /* We must either have reached the end of the buffer or end up
3062 after a newline. */
3063 xassert (IT_CHARPOS (*it) == ZV
3064 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3065
3066 /* Skip over lines that are invisible because they are indented
3067 more than the value of IT->selective. */
3068 if (it->selective > 0)
3069 while (IT_CHARPOS (*it) < ZV
3070 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3071 it->selective))
3072 forward_to_next_line_start (it);
3073
3074 /* Set the iterator there. The 0 as the last parameter of
3075 reseat means don't force a text property lookup. The lookup
3076 is then only done if we've skipped past the iterator's
3077 check_charpos'es. This optimization is important because
3078 text property lookups tend to be expensive. */
3079 reseat (it, it->current.pos, 0);
3080 }
3081
3082 CHECK_IT (it);
3083 }
3084
3085
3086 \f
3087 /***********************************************************************
3088 Changing an iterator's position
3089 ***********************************************************************/
3090
3091 /* Change IT's current position to POS in current_buffer. If FORCE_P
3092 is non-zero, always check for text properties at the new position.
3093 Otherwise, text properties are only looked up if POS >=
3094 IT->check_charpos of a property. */
3095
3096 static void
3097 reseat (it, pos, force_p)
3098 struct it *it;
3099 struct text_pos pos;
3100 int force_p;
3101 {
3102 int original_pos = IT_CHARPOS (*it);
3103
3104 reseat_1 (it, pos, 0);
3105
3106 /* Determine where to check text properties. Avoid doing it
3107 where possible because text property lookup is very expensive. */
3108 if (force_p
3109 || CHARPOS (pos) > it->stop_charpos
3110 || CHARPOS (pos) < original_pos)
3111 handle_stop (it);
3112
3113 CHECK_IT (it);
3114 }
3115
3116
3117 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3118 IT->stop_pos to POS, also. */
3119
3120 static void
3121 reseat_1 (it, pos, set_stop_p)
3122 struct it *it;
3123 struct text_pos pos;
3124 int set_stop_p;
3125 {
3126 /* Don't call this function when scanning a C string. */
3127 xassert (it->s == NULL);
3128
3129 /* POS must be a reasonable value. */
3130 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3131
3132 it->current.pos = it->position = pos;
3133 XSETBUFFER (it->object, current_buffer);
3134 it->dpvec = NULL;
3135 it->current.dpvec_index = -1;
3136 it->current.overlay_string_index = -1;
3137 IT_STRING_CHARPOS (*it) = -1;
3138 IT_STRING_BYTEPOS (*it) = -1;
3139 it->string = Qnil;
3140 it->method = next_element_from_buffer;
3141 it->sp = 0;
3142
3143 if (set_stop_p)
3144 it->stop_charpos = CHARPOS (pos);
3145 }
3146
3147
3148 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3149 If S is non-null, it is a C string to iterate over. Otherwise,
3150 STRING gives a Lisp string to iterate over.
3151
3152 If PRECISION > 0, don't return more then PRECISION number of
3153 characters from the string.
3154
3155 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3156 characters have been returned. FIELD_WIDTH < 0 means an infinite
3157 field width.
3158
3159 MULTIBYTE = 0 means disable processing of multibyte characters,
3160 MULTIBYTE > 0 means enable it,
3161 MULTIBYTE < 0 means use IT->multibyte_p.
3162
3163 IT must be initialized via a prior call to init_iterator before
3164 calling this function. */
3165
3166 static void
3167 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3168 struct it *it;
3169 unsigned char *s;
3170 Lisp_Object string;
3171 int charpos;
3172 int precision, field_width, multibyte;
3173 {
3174 /* No region in strings. */
3175 it->region_beg_charpos = it->region_end_charpos = -1;
3176
3177 /* No text property checks performed by default, but see below. */
3178 it->stop_charpos = -1;
3179
3180 /* Set iterator position and end position. */
3181 bzero (&it->current, sizeof it->current);
3182 it->current.overlay_string_index = -1;
3183 it->current.dpvec_index = -1;
3184 it->charset = CHARSET_ASCII;
3185 xassert (charpos >= 0);
3186
3187 /* Use the setting of MULTIBYTE if specified. */
3188 if (multibyte >= 0)
3189 it->multibyte_p = multibyte > 0;
3190
3191 if (s == NULL)
3192 {
3193 xassert (STRINGP (string));
3194 it->string = string;
3195 it->s = NULL;
3196 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3197 it->method = next_element_from_string;
3198 it->current.string_pos = string_pos (charpos, string);
3199 }
3200 else
3201 {
3202 it->s = s;
3203 it->string = Qnil;
3204
3205 /* Note that we use IT->current.pos, not it->current.string_pos,
3206 for displaying C strings. */
3207 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3208 if (it->multibyte_p)
3209 {
3210 it->current.pos = c_string_pos (charpos, s, 1);
3211 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3212 }
3213 else
3214 {
3215 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3216 it->end_charpos = it->string_nchars = strlen (s);
3217 }
3218
3219 it->method = next_element_from_c_string;
3220 }
3221
3222 /* PRECISION > 0 means don't return more than PRECISION characters
3223 from the string. */
3224 if (precision > 0 && it->end_charpos - charpos > precision)
3225 it->end_charpos = it->string_nchars = charpos + precision;
3226
3227 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3228 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3229 FIELD_WIDTH < 0 means infinite field width. This is useful for
3230 padding with `-' at the end of a mode line. */
3231 if (field_width < 0)
3232 field_width = INFINITY;
3233 if (field_width > it->end_charpos - charpos)
3234 it->end_charpos = charpos + field_width;
3235
3236 /* Use the standard display table for displaying strings. */
3237 if (DISP_TABLE_P (Vstandard_display_table))
3238 it->dp = XCHAR_TABLE (Vstandard_display_table);
3239
3240 it->stop_charpos = charpos;
3241 CHECK_IT (it);
3242 }
3243
3244
3245 \f
3246 /***********************************************************************
3247 Iteration
3248 ***********************************************************************/
3249
3250 /* Load IT's display element fields with information about the next
3251 display element from the current position of IT. Value is zero if
3252 end of buffer (or C string) is reached. */
3253
3254 int
3255 get_next_display_element (it)
3256 struct it *it;
3257 {
3258 /* Non-zero means that we found an display element. Zero means that
3259 we hit the end of what we iterate over. Performance note: the
3260 function pointer `method' used here turns out to be faster than
3261 using a sequence of if-statements. */
3262 int success_p = (*it->method) (it);
3263 int charset;
3264
3265 if (it->what == IT_CHARACTER)
3266 {
3267 /* Map via display table or translate control characters.
3268 IT->c, IT->len etc. have been set to the next character by
3269 the function call above. If we have a display table, and it
3270 contains an entry for IT->c, translate it. Don't do this if
3271 IT->c itself comes from a display table, otherwise we could
3272 end up in an infinite recursion. (An alternative could be to
3273 count the recursion depth of this function and signal an
3274 error when a certain maximum depth is reached.) Is it worth
3275 it? */
3276 if (success_p && it->dpvec == NULL)
3277 {
3278 Lisp_Object dv;
3279
3280 if (it->dp
3281 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3282 VECTORP (dv)))
3283 {
3284 struct Lisp_Vector *v = XVECTOR (dv);
3285
3286 /* Return the first character from the display table
3287 entry, if not empty. If empty, don't display the
3288 current character. */
3289 if (v->size)
3290 {
3291 it->dpvec_char_len = it->len;
3292 it->dpvec = v->contents;
3293 it->dpend = v->contents + v->size;
3294 it->current.dpvec_index = 0;
3295 it->method = next_element_from_display_vector;
3296 }
3297
3298 success_p = get_next_display_element (it);
3299 }
3300
3301 /* Translate control characters into `\003' or `^C' form.
3302 Control characters coming from a display table entry are
3303 currently not translated because we use IT->dpvec to hold
3304 the translation. This could easily be changed but I
3305 don't believe that it is worth doing. */
3306 else if ((it->c < ' '
3307 && (it->area != TEXT_AREA
3308 || (it->c != '\n'
3309 && it->c != '\t'
3310 && it->c != '\r')))
3311 || (it->c >= 127
3312 && it->len == 1))
3313 {
3314 /* IT->c is a control character which must be displayed
3315 either as '\003' or as `^C' where the '\\' and '^'
3316 can be defined in the display table. Fill
3317 IT->ctl_chars with glyphs for what we have to
3318 display. Then, set IT->dpvec to these glyphs. */
3319 GLYPH g;
3320
3321 if (it->c < 128 && it->ctl_arrow_p)
3322 {
3323 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3324 if (it->dp
3325 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3326 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3327 g = XINT (DISP_CTRL_GLYPH (it->dp));
3328 else
3329 g = FAST_MAKE_GLYPH ('^', 0);
3330 XSETINT (it->ctl_chars[0], g);
3331
3332 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3333 XSETINT (it->ctl_chars[1], g);
3334
3335 /* Set up IT->dpvec and return first character from it. */
3336 it->dpvec_char_len = it->len;
3337 it->dpvec = it->ctl_chars;
3338 it->dpend = it->dpvec + 2;
3339 it->current.dpvec_index = 0;
3340 it->method = next_element_from_display_vector;
3341 get_next_display_element (it);
3342 }
3343 else
3344 {
3345 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3346 if (it->dp
3347 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3348 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3349 g = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3350 else
3351 g = FAST_MAKE_GLYPH ('\\', 0);
3352 XSETINT (it->ctl_chars[0], g);
3353
3354 /* Insert three more glyphs into IT->ctl_chars for
3355 the octal display of the character. */
3356 g = FAST_MAKE_GLYPH (((it->c >> 6) & 7) + '0', 0);
3357 XSETINT (it->ctl_chars[1], g);
3358 g = FAST_MAKE_GLYPH (((it->c >> 3) & 7) + '0', 0);
3359 XSETINT (it->ctl_chars[2], g);
3360 g = FAST_MAKE_GLYPH ((it->c & 7) + '0', 0);
3361 XSETINT (it->ctl_chars[3], g);
3362
3363 /* Set up IT->dpvec and return the first character
3364 from it. */
3365 it->dpvec_char_len = it->len;
3366 it->dpvec = it->ctl_chars;
3367 it->dpend = it->dpvec + 4;
3368 it->current.dpvec_index = 0;
3369 it->method = next_element_from_display_vector;
3370 get_next_display_element (it);
3371 }
3372 }
3373 }
3374
3375 /* Adjust face id if charset changes. There are no charset
3376 changes in unibyte text because Emacs' charsets are not
3377 applicable there. */
3378 if (it->multibyte_p
3379 && success_p
3380 && (charset = CHAR_CHARSET (it->c),
3381 charset != it->charset))
3382 {
3383 it->charset = charset;
3384 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, charset);
3385 }
3386 }
3387
3388 /* Is this character the last one of a run of characters with
3389 box? If yes, set IT->end_of_box_run_p to 1. */
3390 if (it->face_box_p
3391 && it->s == NULL)
3392 {
3393 int face_id;
3394 struct face *face;
3395
3396 it->end_of_box_run_p
3397 = ((face_id = face_after_it_pos (it),
3398 face_id != it->face_id)
3399 && (face = FACE_FROM_ID (it->f, face_id),
3400 face->box == FACE_NO_BOX));
3401 }
3402
3403 /* Value is 0 if end of buffer or string reached. */
3404 return success_p;
3405 }
3406
3407
3408 /* Move IT to the next display element.
3409
3410 Functions get_next_display_element and set_iterator_to_next are
3411 separate because I find this arrangement easier to handle than a
3412 get_next_display_element function that also increments IT's
3413 position. The way it is we can first look at an iterator's current
3414 display element, decide whether it fits on a line, and if it does,
3415 increment the iterator position. The other way around we probably
3416 would either need a flag indicating whether the iterator has to be
3417 incremented the next time, or we would have to implement a
3418 decrement position function which would not be easy to write. */
3419
3420 void
3421 set_iterator_to_next (it)
3422 struct it *it;
3423 {
3424 if (it->method == next_element_from_buffer)
3425 {
3426 /* The current display element of IT is a character from
3427 current_buffer. Advance in the buffer, and maybe skip over
3428 invisible lines that are so because of selective display. */
3429 if (ITERATOR_AT_END_OF_LINE_P (it))
3430 reseat_at_next_visible_line_start (it);
3431 else
3432 {
3433 xassert (it->len != 0);
3434 IT_BYTEPOS (*it) += it->len;
3435 IT_CHARPOS (*it) += 1;
3436 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3437 }
3438 }
3439 else if (it->method == next_element_from_c_string)
3440 {
3441 /* Current display element of IT is from a C string. */
3442 IT_BYTEPOS (*it) += it->len;
3443 IT_CHARPOS (*it) += 1;
3444 }
3445 else if (it->method == next_element_from_display_vector)
3446 {
3447 /* Current display element of IT is from a display table entry.
3448 Advance in the display table definition. Reset it to null if
3449 end reached, and continue with characters from buffers/
3450 strings. */
3451 ++it->current.dpvec_index;
3452 it->face_id = it->saved_face_id;
3453 if (it->dpvec + it->current.dpvec_index == it->dpend)
3454 {
3455 if (it->s)
3456 it->method = next_element_from_c_string;
3457 else if (STRINGP (it->string))
3458 it->method = next_element_from_string;
3459 else
3460 it->method = next_element_from_buffer;
3461
3462 it->dpvec = NULL;
3463 it->current.dpvec_index = -1;
3464
3465 /* Consume the character which was displayed via IT->dpvec. */
3466 if (it->dpvec_char_len)
3467 {
3468 it->len = it->dpvec_char_len;
3469 set_iterator_to_next (it);
3470 }
3471 }
3472 }
3473 else if (it->method == next_element_from_string)
3474 {
3475 /* Current display element is a character from a Lisp string. */
3476 xassert (it->s == NULL && STRINGP (it->string));
3477 IT_STRING_BYTEPOS (*it) += it->len;
3478 IT_STRING_CHARPOS (*it) += 1;
3479
3480 consider_string_end:
3481
3482 if (it->current.overlay_string_index >= 0)
3483 {
3484 /* IT->string is an overlay string. Advance to the
3485 next, if there is one. */
3486 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3487 next_overlay_string (it);
3488 }
3489 else
3490 {
3491 /* IT->string is not an overlay string. If we reached
3492 its end, and there is something on IT->stack, proceed
3493 with what is on the stack. This can be either another
3494 string, this time an overlay string, or a buffer. */
3495 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
3496 && it->sp > 0)
3497 {
3498 pop_it (it);
3499 if (!STRINGP (it->string))
3500 it->method = next_element_from_buffer;
3501 }
3502 }
3503 }
3504 else if (it->method == next_element_from_image
3505 || it->method == next_element_from_stretch)
3506 {
3507 /* The position etc with which we have to proceed are on
3508 the stack. The position may be at the end of a string,
3509 if the `display' property takes up the whole string. */
3510 pop_it (it);
3511 it->image_id = 0;
3512 if (STRINGP (it->string))
3513 {
3514 it->method = next_element_from_string;
3515 goto consider_string_end;
3516 }
3517 else
3518 it->method = next_element_from_buffer;
3519 }
3520 else
3521 /* There are no other methods defined, so this should be a bug. */
3522 abort ();
3523
3524 /* Reset flags indicating start and end of a sequence of
3525 characters with box. */
3526 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3527
3528 xassert (it->method != next_element_from_string
3529 || (STRINGP (it->string)
3530 && IT_STRING_CHARPOS (*it) >= 0));
3531 }
3532
3533
3534 /* Load IT's display element fields with information about the next
3535 display element which comes from a display table entry or from the
3536 result of translating a control character to one of the forms `^C'
3537 or `\003'. IT->dpvec holds the glyphs to return as characters. */
3538
3539 static int
3540 next_element_from_display_vector (it)
3541 struct it *it;
3542 {
3543 /* Precondition. */
3544 xassert (it->dpvec && it->current.dpvec_index >= 0);
3545
3546 /* Remember the current face id in case glyphs specify faces.
3547 IT's face is restored in set_iterator_to_next. */
3548 it->saved_face_id = it->face_id;
3549
3550 if (INTEGERP (*it->dpvec)
3551 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
3552 {
3553 int lface_id;
3554 GLYPH g;
3555
3556 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
3557 it->c = FAST_GLYPH_CHAR (g);
3558 it->len = CHAR_LEN (it->c);
3559
3560 /* The entry may contain a face id to use. Such a face id is
3561 the id of a Lisp face, not a realized face. A face id of
3562 zero means no face. */
3563 lface_id = FAST_GLYPH_FACE (g);
3564 if (lface_id)
3565 {
3566 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
3567 if (face_id >= 0)
3568 {
3569 it->face_id = face_id;
3570 it->charset = CHARSET_ASCII;
3571 }
3572 }
3573 }
3574 else
3575 /* Display table entry is invalid. Return a space. */
3576 it->c = ' ', it->len = 1;
3577
3578 /* Don't change position and object of the iterator here. They are
3579 still the values of the character that had this display table
3580 entry or was translated, and that's what we want. */
3581 it->what = IT_CHARACTER;
3582 return 1;
3583 }
3584
3585
3586 /* Load IT with the next display element from Lisp string IT->string.
3587 IT->current.string_pos is the current position within the string.
3588 If IT->current.overlay_string_index >= 0, the Lisp string is an
3589 overlay string. */
3590
3591 static int
3592 next_element_from_string (it)
3593 struct it *it;
3594 {
3595 struct text_pos position;
3596
3597 xassert (STRINGP (it->string));
3598 xassert (IT_STRING_CHARPOS (*it) >= 0);
3599 position = it->current.string_pos;
3600
3601 /* Time to check for invisible text? */
3602 if (IT_STRING_CHARPOS (*it) < it->end_charpos
3603 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
3604 {
3605 handle_stop (it);
3606
3607 /* Since a handler may have changed IT->method, we must
3608 recurse here. */
3609 return get_next_display_element (it);
3610 }
3611
3612 if (it->current.overlay_string_index >= 0)
3613 {
3614 /* Get the next character from an overlay string. In overlay
3615 strings, There is no field width or padding with spaces to
3616 do. */
3617 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3618 {
3619 it->what = IT_EOB;
3620 return 0;
3621 }
3622 else if (STRING_MULTIBYTE (it->string))
3623 {
3624 int remaining = (STRING_BYTES (XSTRING (it->string))
3625 - IT_STRING_BYTEPOS (*it));
3626 unsigned char *s = (XSTRING (it->string)->data
3627 + IT_STRING_BYTEPOS (*it));
3628 it->c = string_char_and_length (s, remaining, &it->len);
3629 }
3630 else
3631 {
3632 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3633 it->len = 1;
3634 }
3635 }
3636 else
3637 {
3638 /* Get the next character from a Lisp string that is not an
3639 overlay string. Such strings come from the mode line, for
3640 example. We may have to pad with spaces, or truncate the
3641 string. See also next_element_from_c_string. */
3642 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
3643 {
3644 it->what = IT_EOB;
3645 return 0;
3646 }
3647 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
3648 {
3649 /* Pad with spaces. */
3650 it->c = ' ', it->len = 1;
3651 CHARPOS (position) = BYTEPOS (position) = -1;
3652 }
3653 else if (STRING_MULTIBYTE (it->string))
3654 {
3655 int maxlen = (STRING_BYTES (XSTRING (it->string))
3656 - IT_STRING_BYTEPOS (*it));
3657 unsigned char *s = (XSTRING (it->string)->data
3658 + IT_STRING_BYTEPOS (*it));
3659 it->c = string_char_and_length (s, maxlen, &it->len);
3660 }
3661 else
3662 {
3663 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3664 it->len = 1;
3665 }
3666 }
3667
3668 /* Record what we have and where it came from. Note that we store a
3669 buffer position in IT->position although it could arguably be a
3670 string position. */
3671 it->what = IT_CHARACTER;
3672 it->object = it->string;
3673 it->position = position;
3674 return 1;
3675 }
3676
3677
3678 /* Load IT with next display element from C string IT->s.
3679 IT->string_nchars is the maximum number of characters to return
3680 from the string. IT->end_charpos may be greater than
3681 IT->string_nchars when this function is called, in which case we
3682 may have to return padding spaces. Value is zero if end of string
3683 reached, including padding spaces. */
3684
3685 static int
3686 next_element_from_c_string (it)
3687 struct it *it;
3688 {
3689 int success_p = 1;
3690
3691 xassert (it->s);
3692 it->what = IT_CHARACTER;
3693 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
3694 it->object = Qnil;
3695
3696 /* IT's position can be greater IT->string_nchars in case a field
3697 width or precision has been specified when the iterator was
3698 initialized. */
3699 if (IT_CHARPOS (*it) >= it->end_charpos)
3700 {
3701 /* End of the game. */
3702 it->what = IT_EOB;
3703 success_p = 0;
3704 }
3705 else if (IT_CHARPOS (*it) >= it->string_nchars)
3706 {
3707 /* Pad with spaces. */
3708 it->c = ' ', it->len = 1;
3709 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
3710 }
3711 else if (it->multibyte_p)
3712 {
3713 /* Implementation note: The calls to strlen apparently aren't a
3714 performance problem because there is no noticeable performance
3715 difference between Emacs running in unibyte or multibyte mode. */
3716 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
3717 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
3718 maxlen, &it->len);
3719 }
3720 else
3721 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
3722
3723 return success_p;
3724 }
3725
3726
3727 /* Set up IT to return characters from an ellipsis, if appropriate.
3728 The definition of the ellipsis glyphs may come from a display table
3729 entry. This function Fills IT with the first glyph from the
3730 ellipsis if an ellipsis is to be displayed. */
3731
3732 static void
3733 next_element_from_ellipsis (it)
3734 struct it *it;
3735 {
3736 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3737 {
3738 /* Use the display table definition for `...'. Invalid glyphs
3739 will be handled by the method returning elements from dpvec. */
3740 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3741 it->dpvec_char_len = it->len;
3742 it->dpvec = v->contents;
3743 it->dpend = v->contents + v->size;
3744 it->current.dpvec_index = 0;
3745 it->method = next_element_from_display_vector;
3746 get_next_display_element (it);
3747 }
3748 else if (it->selective_display_ellipsis_p)
3749 {
3750 /* Use default `...' which is stored in default_invis_vector. */
3751 it->dpvec_char_len = it->len;
3752 it->dpvec = default_invis_vector;
3753 it->dpend = default_invis_vector + 3;
3754 it->current.dpvec_index = 0;
3755 it->method = next_element_from_display_vector;
3756 get_next_display_element (it);
3757 }
3758 }
3759
3760
3761 /* Deliver an image display element. The iterator IT is already
3762 filled with image information (done in handle_display_prop). Value
3763 is always 1. */
3764
3765
3766 static int
3767 next_element_from_image (it)
3768 struct it *it;
3769 {
3770 it->what = IT_IMAGE;
3771 return 1;
3772 }
3773
3774
3775 /* Fill iterator IT with next display element from a stretch glyph
3776 property. IT->object is the value of the text property. Value is
3777 always 1. */
3778
3779 static int
3780 next_element_from_stretch (it)
3781 struct it *it;
3782 {
3783 it->what = IT_STRETCH;
3784 return 1;
3785 }
3786
3787
3788 /* Load IT with the next display element from current_buffer. Value
3789 is zero if end of buffer reached. IT->stop_charpos is the next
3790 position at which to stop and check for text properties or buffer
3791 end. */
3792
3793 static int
3794 next_element_from_buffer (it)
3795 struct it *it;
3796 {
3797 int success_p = 1;
3798
3799 /* Check this assumption, otherwise, we would never enter the
3800 if-statement, below. */
3801 xassert (IT_CHARPOS (*it) >= BEGV
3802 && IT_CHARPOS (*it) <= it->stop_charpos);
3803
3804 if (IT_CHARPOS (*it) >= it->stop_charpos)
3805 {
3806 if (IT_CHARPOS (*it) >= it->end_charpos)
3807 {
3808 int overlay_strings_follow_p;
3809
3810 /* End of the game, except when overlay strings follow that
3811 haven't been returned yet. */
3812 if (it->overlay_strings_at_end_processed_p)
3813 overlay_strings_follow_p = 0;
3814 else
3815 {
3816 it->overlay_strings_at_end_processed_p = 1;
3817 overlay_strings_follow_p
3818 = get_overlay_strings (it);
3819 }
3820
3821 if (overlay_strings_follow_p)
3822 success_p = get_next_display_element (it);
3823 else
3824 {
3825 it->what = IT_EOB;
3826 it->position = it->current.pos;
3827 success_p = 0;
3828 }
3829 }
3830 else
3831 {
3832 handle_stop (it);
3833 return get_next_display_element (it);
3834 }
3835 }
3836 else
3837 {
3838 /* No face changes, overlays etc. in sight, so just return a
3839 character from current_buffer. */
3840 unsigned char *p;
3841
3842 /* Maybe run the redisplay end trigger hook. Performance note:
3843 This doesn't seem to cost measurable time. */
3844 if (it->redisplay_end_trigger_charpos
3845 && it->glyph_row
3846 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
3847 run_redisplay_end_trigger_hook (it);
3848
3849 /* Get the next character, maybe multibyte. */
3850 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
3851 if (it->multibyte_p)
3852 {
3853 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
3854 - IT_BYTEPOS (*it));
3855 it->c = string_char_and_length (p, maxlen, &it->len);
3856 }
3857 else
3858 it->c = *p, it->len = 1;
3859
3860 /* Record what we have and where it came from. */
3861 it->what = IT_CHARACTER;;
3862 it->object = it->w->buffer;
3863 it->position = it->current.pos;
3864
3865 /* Normally we return the character found above, except when we
3866 really want to return an ellipsis for selective display. */
3867 if (it->selective)
3868 {
3869 if (it->c == '\n')
3870 {
3871 /* A value of selective > 0 means hide lines indented more
3872 than that number of columns. */
3873 if (it->selective > 0
3874 && IT_CHARPOS (*it) + 1 < ZV
3875 && indented_beyond_p (IT_CHARPOS (*it) + 1,
3876 IT_BYTEPOS (*it) + 1,
3877 it->selective))
3878 next_element_from_ellipsis (it);
3879 }
3880 else if (it->c == '\r' && it->selective == -1)
3881 {
3882 /* A value of selective == -1 means that everything from the
3883 CR to the end of the line is invisible, with maybe an
3884 ellipsis displayed for it. */
3885 next_element_from_ellipsis (it);
3886 }
3887 }
3888 }
3889
3890 /* Value is zero if end of buffer reached. */
3891 xassert (!success_p || it->len > 0);
3892 return success_p;
3893 }
3894
3895
3896 /* Run the redisplay end trigger hook for IT. */
3897
3898 static void
3899 run_redisplay_end_trigger_hook (it)
3900 struct it *it;
3901 {
3902 Lisp_Object args[3];
3903
3904 /* IT->glyph_row should be non-null, i.e. we should be actually
3905 displaying something, or otherwise we should not run the hook. */
3906 xassert (it->glyph_row);
3907
3908 /* Set up hook arguments. */
3909 args[0] = Qredisplay_end_trigger_functions;
3910 args[1] = it->window;
3911 XSETINT (args[2], it->redisplay_end_trigger_charpos);
3912 it->redisplay_end_trigger_charpos = 0;
3913
3914 /* Since we are *trying* to run these functions, don't try to run
3915 them again, even if they get an error. */
3916 it->w->redisplay_end_trigger = Qnil;
3917 Frun_hook_with_args (3, args);
3918
3919 /* Notice if it changed the face of the character we are on. */
3920 handle_face_prop (it);
3921 }
3922
3923
3924 \f
3925 /***********************************************************************
3926 Moving an iterator without producing glyphs
3927 ***********************************************************************/
3928
3929 /* Move iterator IT to a specified buffer or X position within one
3930 line on the display without producing glyphs.
3931
3932 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
3933 whichever is reached first.
3934
3935 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
3936
3937 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
3938 0 <= TO_X <= IT->last_visible_x. This means in particular, that
3939 TO_X includes the amount by which a window is horizontally
3940 scrolled.
3941
3942 Value is
3943
3944 MOVE_POS_MATCH_OR_ZV
3945 - when TO_POS or ZV was reached.
3946
3947 MOVE_X_REACHED
3948 -when TO_X was reached before TO_POS or ZV were reached.
3949
3950 MOVE_LINE_CONTINUED
3951 - when we reached the end of the display area and the line must
3952 be continued.
3953
3954 MOVE_LINE_TRUNCATED
3955 - when we reached the end of the display area and the line is
3956 truncated.
3957
3958 MOVE_NEWLINE_OR_CR
3959 - when we stopped at a line end, i.e. a newline or a CR and selective
3960 display is on. */
3961
3962 enum move_it_result
3963 move_it_in_display_line_to (it, to_charpos, to_x, op)
3964 struct it *it;
3965 int to_charpos, to_x, op;
3966 {
3967 enum move_it_result result = MOVE_UNDEFINED;
3968 struct glyph_row *saved_glyph_row;
3969
3970 /* Don't produce glyphs in produce_glyphs. */
3971 saved_glyph_row = it->glyph_row;
3972 it->glyph_row = NULL;
3973
3974 #if NO_PROMPT_IN_BUFFER
3975 /* Take a mini-buffer prompt into account. */
3976 if (MINI_WINDOW_P (it->w)
3977 && IT_CHARPOS (*it) == BEGV)
3978 {
3979 it->current_x = minibuf_prompt_pixel_width;
3980 it->hpos = minibuf_prompt_width;
3981 }
3982 #endif
3983
3984 while (1)
3985 {
3986 int x, i;
3987
3988 /* Stop when ZV or TO_CHARPOS reached. */
3989 if (!get_next_display_element (it)
3990 || ((op & MOVE_TO_POS) != 0
3991 && BUFFERP (it->object)
3992 && IT_CHARPOS (*it) >= to_charpos))
3993 {
3994 result = MOVE_POS_MATCH_OR_ZV;
3995 break;
3996 }
3997
3998 /* The call to produce_glyphs will get the metrics of the
3999 display element IT is loaded with. We record in x the
4000 x-position before this display element in case it does not
4001 fit on the line. */
4002 x = it->current_x;
4003 PRODUCE_GLYPHS (it);
4004
4005 if (it->area != TEXT_AREA)
4006 {
4007 set_iterator_to_next (it);
4008 continue;
4009 }
4010
4011 /* The number of glyphs we get back in IT->nglyphs will normally
4012 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4013 character on a terminal frame, or (iii) a line end. For the
4014 second case, IT->nglyphs - 1 padding glyphs will be present
4015 (on X frames, there is only one glyph produced for a
4016 composite character.
4017
4018 The behavior implemented below means, for continuation lines,
4019 that as many spaces of a TAB as fit on the current line are
4020 displayed there. For terminal frames, as many glyphs of a
4021 multi-glyph character are displayed in the current line, too.
4022 This is what the old redisplay code did, and we keep it that
4023 way. Under X, the whole shape of a complex character must
4024 fit on the line or it will be completely displayed in the
4025 next line.
4026
4027 Note that both for tabs and padding glyphs, all glyphs have
4028 the same width. */
4029 if (it->nglyphs)
4030 {
4031 /* More than one glyph or glyph doesn't fit on line. All
4032 glyphs have the same width. */
4033 int single_glyph_width = it->pixel_width / it->nglyphs;
4034 int new_x;
4035
4036 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4037 {
4038 new_x = x + single_glyph_width;
4039
4040 /* We want to leave anything reaching TO_X to the caller. */
4041 if ((op & MOVE_TO_X) && new_x > to_x)
4042 {
4043 it->current_x = x;
4044 result = MOVE_X_REACHED;
4045 break;
4046 }
4047 else if (/* Lines are continued. */
4048 !it->truncate_lines_p
4049 && (/* And glyph doesn't fit on the line. */
4050 new_x > it->last_visible_x
4051 /* Or it fits exactly and we're on a window
4052 system frame. */
4053 || (new_x == it->last_visible_x
4054 && FRAME_WINDOW_P (it->f))))
4055 {
4056 if (/* IT->hpos == 0 means the very first glyph
4057 doesn't fit on the line, e.g. a wide image. */
4058 it->hpos == 0
4059 || (new_x == it->last_visible_x
4060 && FRAME_WINDOW_P (it->f)))
4061 {
4062 ++it->hpos;
4063 it->current_x = new_x;
4064 if (i == it->nglyphs - 1)
4065 set_iterator_to_next (it);
4066 }
4067 else
4068 it->current_x = x;
4069
4070 result = MOVE_LINE_CONTINUED;
4071 break;
4072 }
4073 else if (new_x > it->first_visible_x)
4074 {
4075 /* Glyph is visible. Increment number of glyphs that
4076 would be displayed. */
4077 ++it->hpos;
4078 }
4079 else
4080 {
4081 /* Glyph is completely off the left margin of the display
4082 area. Nothing to do. */
4083 }
4084 }
4085
4086 if (result != MOVE_UNDEFINED)
4087 break;
4088 }
4089 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4090 {
4091 /* Stop when TO_X specified and reached. This check is
4092 necessary here because of lines consisting of a line end,
4093 only. The line end will not produce any glyphs and we
4094 would never get MOVE_X_REACHED. */
4095 xassert (it->nglyphs == 0);
4096 result = MOVE_X_REACHED;
4097 break;
4098 }
4099
4100 /* Is this a line end? If yes, we're done. */
4101 if (ITERATOR_AT_END_OF_LINE_P (it))
4102 {
4103 result = MOVE_NEWLINE_OR_CR;
4104 break;
4105 }
4106
4107 /* The current display element has been consumed. Advance
4108 to the next. */
4109 set_iterator_to_next (it);
4110
4111 /* Stop if lines are truncated and IT's current x-position is
4112 past the right edge of the window now. */
4113 if (it->truncate_lines_p
4114 && it->current_x >= it->last_visible_x)
4115 {
4116 result = MOVE_LINE_TRUNCATED;
4117 break;
4118 }
4119 }
4120
4121 /* Restore the iterator settings altered at the beginning of this
4122 function. */
4123 it->glyph_row = saved_glyph_row;
4124 return result;
4125 }
4126
4127
4128 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4129 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4130 the description of enum move_operation_enum.
4131
4132 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4133 screen line, this function will set IT to the next position >
4134 TO_CHARPOS. */
4135
4136 void
4137 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4138 struct it *it;
4139 int to_charpos, to_x, to_y, to_vpos;
4140 int op;
4141 {
4142 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4143 int line_height;
4144
4145 xassert (XBUFFER (it->w->buffer) == current_buffer);
4146
4147 while (1)
4148 {
4149 if (op & MOVE_TO_VPOS)
4150 {
4151 /* If no TO_CHARPOS and no TO_X specified, stop at the
4152 start of the line TO_VPOS. */
4153 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4154 {
4155 if (it->vpos == to_vpos)
4156 break;
4157 skip = move_it_in_display_line_to (it, -1, -1, 0);
4158 }
4159 else
4160 {
4161 /* TO_VPOS >= 0 means stop at TO_X in the line at
4162 TO_VPOS, or at TO_POS, whichever comes first. */
4163 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4164
4165 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4166 break;
4167 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4168 {
4169 /* We have reached TO_X but not in the line we want. */
4170 skip = move_it_in_display_line_to (it, to_charpos,
4171 -1, MOVE_TO_POS);
4172 if (skip == MOVE_POS_MATCH_OR_ZV)
4173 break;
4174 }
4175 }
4176 }
4177 else if (op & MOVE_TO_Y)
4178 {
4179 struct it it_backup;
4180 int done_p;
4181
4182 /* TO_Y specified means stop at TO_X in the line containing
4183 TO_Y---or at TO_CHARPOS if this is reached first. The
4184 problem is that we can't really tell whether the line
4185 contains TO_Y before we have completely scanned it, and
4186 this may skip past TO_X. What we do is to first scan to
4187 TO_X.
4188
4189 If TO_X is not specified, use a TO_X of zero. The reason
4190 is to make the outcome of this function more predictable.
4191 If we didn't use TO_X == 0, we would stop at the end of
4192 the line which is probably not what a caller would expect
4193 to happen. */
4194 skip = move_it_in_display_line_to (it, to_charpos,
4195 ((op & MOVE_TO_X)
4196 ? to_x : 0),
4197 (MOVE_TO_X
4198 | (op & MOVE_TO_POS)));
4199
4200 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4201 if (skip == MOVE_POS_MATCH_OR_ZV)
4202 break;
4203
4204 /* If TO_X was reached, we would like to know whether TO_Y
4205 is in the line. This can only be said if we know the
4206 total line height which requires us to scan the rest of
4207 the line. */
4208 done_p = 0;
4209 if (skip == MOVE_X_REACHED)
4210 {
4211 it_backup = *it;
4212 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4213 op & MOVE_TO_POS);
4214 }
4215
4216 /* Now, decide whether TO_Y is in this line. */
4217 line_height = it->max_ascent + it->max_descent;
4218
4219 if (to_y >= it->current_y
4220 && to_y < it->current_y + line_height)
4221 {
4222 if (skip == MOVE_X_REACHED)
4223 /* If TO_Y is in this line and TO_X was reached above,
4224 we scanned too far. We have to restore IT's settings
4225 to the ones before skipping. */
4226 *it = it_backup;
4227 done_p = 1;
4228 }
4229 else if (skip == MOVE_X_REACHED)
4230 {
4231 skip = skip2;
4232 if (skip == MOVE_POS_MATCH_OR_ZV)
4233 done_p = 1;
4234 }
4235
4236 if (done_p)
4237 break;
4238 }
4239 else
4240 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4241
4242 switch (skip)
4243 {
4244 case MOVE_POS_MATCH_OR_ZV:
4245 return;
4246
4247 case MOVE_NEWLINE_OR_CR:
4248 set_iterator_to_next (it);
4249 it->continuation_lines_width = 0;
4250 break;
4251
4252 case MOVE_LINE_TRUNCATED:
4253 it->continuation_lines_width = 0;
4254 reseat_at_next_visible_line_start (it);
4255 if ((op & MOVE_TO_POS) != 0
4256 && IT_CHARPOS (*it) > to_charpos)
4257 goto out;
4258 break;
4259
4260 case MOVE_LINE_CONTINUED:
4261 it->continuation_lines_width += it->current_x;
4262 break;
4263
4264 default:
4265 abort ();
4266 }
4267
4268 /* Reset/increment for the next run. */
4269 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4270 it->current_x = it->hpos = 0;
4271 it->current_y += it->max_ascent + it->max_descent;
4272 ++it->vpos;
4273 last_height = it->max_ascent + it->max_descent;
4274 last_max_ascent = it->max_ascent;
4275 it->max_ascent = it->max_descent = 0;
4276 }
4277 out:;
4278 }
4279
4280
4281 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4282
4283 If DY > 0, move IT backward at least that many pixels. DY = 0
4284 means move IT backward to the preceding line start or BEGV. This
4285 function may move over more than DY pixels if IT->current_y - DY
4286 ends up in the middle of a line; in this case IT->current_y will be
4287 set to the top of the line moved to. */
4288
4289 void
4290 move_it_vertically_backward (it, dy)
4291 struct it *it;
4292 int dy;
4293 {
4294 int nlines, h, line_height;
4295 struct it it2;
4296 int start_pos = IT_CHARPOS (*it);
4297
4298 xassert (dy >= 0);
4299
4300 /* Estimate how many newlines we must move back. */
4301 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4302
4303 /* Set the iterator's position that many lines back. */
4304 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4305 back_to_previous_visible_line_start (it);
4306
4307 /* Reseat the iterator here. When moving backward, we don't want
4308 reseat to skip forward over invisible text, set up the iterator
4309 to deliver from overlay strings at the new position etc. So,
4310 use reseat_1 here. */
4311 reseat_1 (it, it->current.pos, 1);
4312
4313 /* We are now surely at a line start. */
4314 it->current_x = it->hpos = 0;
4315
4316 /* Move forward and see what y-distance we moved. First move to the
4317 start of the next line so that we get its height. We need this
4318 height to be able to tell whether we reached the specified
4319 y-distance. */
4320 it2 = *it;
4321 it2.max_ascent = it2.max_descent = 0;
4322 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4323 MOVE_TO_POS | MOVE_TO_VPOS);
4324 xassert (IT_CHARPOS (*it) >= BEGV);
4325 line_height = it2.max_ascent + it2.max_descent;
4326 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4327 xassert (IT_CHARPOS (*it) >= BEGV);
4328 h = it2.current_y - it->current_y;
4329 nlines = it2.vpos - it->vpos;
4330
4331 /* Correct IT's y and vpos position. */
4332 it->vpos -= nlines;
4333 it->current_y -= h;
4334
4335 if (dy == 0)
4336 {
4337 /* DY == 0 means move to the start of the screen line. The
4338 value of nlines is > 0 if continuation lines were involved. */
4339 if (nlines > 0)
4340 move_it_by_lines (it, nlines, 1);
4341 xassert (IT_CHARPOS (*it) <= start_pos);
4342 }
4343 else if (nlines)
4344 {
4345 /* The y-position we try to reach. Note that h has been
4346 subtracted in front of the if-statement. */
4347 int target_y = it->current_y + h - dy;
4348
4349 /* If we did not reach target_y, try to move further backward if
4350 we can. If we moved too far backward, try to move forward. */
4351 if (target_y < it->current_y
4352 && IT_CHARPOS (*it) > BEGV)
4353 {
4354 move_it_vertically (it, target_y - it->current_y);
4355 xassert (IT_CHARPOS (*it) >= BEGV);
4356 }
4357 else if (target_y >= it->current_y + line_height
4358 && IT_CHARPOS (*it) < ZV)
4359 {
4360 move_it_vertically (it, target_y - (it->current_y + line_height));
4361 xassert (IT_CHARPOS (*it) >= BEGV);
4362 }
4363 }
4364 }
4365
4366
4367 /* Move IT by a specified amount of pixel lines DY. DY negative means
4368 move backwards. DY = 0 means move to start of screen line. At the
4369 end, IT will be on the start of a screen line. */
4370
4371 void
4372 move_it_vertically (it, dy)
4373 struct it *it;
4374 int dy;
4375 {
4376 if (dy <= 0)
4377 move_it_vertically_backward (it, -dy);
4378 else if (dy > 0)
4379 {
4380 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4381 MOVE_TO_POS | MOVE_TO_Y);
4382
4383 /* If buffer ends in ZV without a newline, move to the start of
4384 the line to satisfy the post-condition. */
4385 if (IT_CHARPOS (*it) == ZV
4386 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4387 move_it_by_lines (it, 0, 0);
4388 }
4389 }
4390
4391
4392 /* Return non-zero if some text between buffer positions START_CHARPOS
4393 and END_CHARPOS is invisible. IT->window is the window for text
4394 property lookup. */
4395
4396 static int
4397 invisible_text_between_p (it, start_charpos, end_charpos)
4398 struct it *it;
4399 int start_charpos, end_charpos;
4400 {
4401 #ifdef USE_TEXT_PROPERTIES
4402 Lisp_Object prop, limit;
4403 int invisible_found_p;
4404
4405 xassert (it != NULL && start_charpos <= end_charpos);
4406
4407 /* Is text at START invisible? */
4408 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4409 it->window);
4410 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4411 invisible_found_p = 1;
4412 else
4413 {
4414 limit = Fnext_single_property_change (make_number (start_charpos),
4415 Qinvisible,
4416 Fcurrent_buffer (),
4417 make_number (end_charpos));
4418 invisible_found_p = XFASTINT (limit) < end_charpos;
4419 }
4420
4421 return invisible_found_p;
4422
4423 #else /* not USE_TEXT_PROPERTIES */
4424 return 0;
4425 #endif /* not USE_TEXT_PROPERTIES */
4426 }
4427
4428
4429 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
4430 negative means move up. DVPOS == 0 means move to the start of the
4431 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4432 NEED_Y_P is zero, IT->current_y will be left unchanged.
4433
4434 Further optimization ideas: If we would know that IT->f doesn't use
4435 a face with proportional font, we could be faster for
4436 truncate-lines nil. */
4437
4438 void
4439 move_it_by_lines (it, dvpos, need_y_p)
4440 struct it *it;
4441 int dvpos, need_y_p;
4442 {
4443 struct position pos;
4444
4445 if (!FRAME_WINDOW_P (it->f))
4446 {
4447 struct text_pos textpos;
4448
4449 /* We can use vmotion on frames without proportional fonts. */
4450 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
4451 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
4452 reseat (it, textpos, 1);
4453 it->vpos += pos.vpos;
4454 it->current_y += pos.vpos;
4455 }
4456 else if (dvpos == 0)
4457 {
4458 /* DVPOS == 0 means move to the start of the screen line. */
4459 move_it_vertically_backward (it, 0);
4460 xassert (it->current_x == 0 && it->hpos == 0);
4461 }
4462 else if (dvpos > 0)
4463 {
4464 /* If there are no continuation lines, and if there is no
4465 selective display, try the simple method of moving forward
4466 DVPOS newlines, then see where we are. */
4467 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4468 {
4469 int shortage = 0, charpos;
4470
4471 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
4472 charpos = IT_CHARPOS (*it) + 1;
4473 else
4474 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
4475 &shortage, 0);
4476
4477 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
4478 {
4479 struct text_pos pos;
4480 CHARPOS (pos) = charpos;
4481 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4482 reseat (it, pos, 1);
4483 it->vpos += dvpos - shortage;
4484 it->hpos = it->current_x = 0;
4485 return;
4486 }
4487 }
4488
4489 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
4490 }
4491 else
4492 {
4493 struct it it2;
4494 int start_charpos, i;
4495
4496 /* If there are no continuation lines, and if there is no
4497 selective display, try the simple method of moving backward
4498 -DVPOS newlines. */
4499 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4500 {
4501 int shortage;
4502 int charpos = IT_CHARPOS (*it);
4503 int bytepos = IT_BYTEPOS (*it);
4504
4505 /* If in the middle of a line, go to its start. */
4506 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
4507 {
4508 charpos = find_next_newline_no_quit (charpos, -1);
4509 bytepos = CHAR_TO_BYTE (charpos);
4510 }
4511
4512 if (charpos == BEGV)
4513 {
4514 struct text_pos pos;
4515 CHARPOS (pos) = charpos;
4516 BYTEPOS (pos) = bytepos;
4517 reseat (it, pos, 1);
4518 it->hpos = it->current_x = 0;
4519 return;
4520 }
4521 else
4522 {
4523 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
4524 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
4525 {
4526 struct text_pos pos;
4527 CHARPOS (pos) = charpos;
4528 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4529 reseat (it, pos, 1);
4530 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
4531 it->hpos = it->current_x = 0;
4532 return;
4533 }
4534 }
4535 }
4536
4537 /* Go back -DVPOS visible lines and reseat the iterator there. */
4538 start_charpos = IT_CHARPOS (*it);
4539 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
4540 back_to_previous_visible_line_start (it);
4541 reseat (it, it->current.pos, 1);
4542 it->current_x = it->hpos = 0;
4543
4544 /* Above call may have moved too far if continuation lines
4545 are involved. Scan forward and see if it did. */
4546 it2 = *it;
4547 it2.vpos = it2.current_y = 0;
4548 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
4549 it->vpos -= it2.vpos;
4550 it->current_y -= it2.current_y;
4551 it->current_x = it->hpos = 0;
4552
4553 /* If we moved too far, move IT some lines forward. */
4554 if (it2.vpos > -dvpos)
4555 {
4556 int delta = it2.vpos + dvpos;
4557 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
4558 }
4559 }
4560 }
4561
4562
4563 \f
4564 /***********************************************************************
4565 Messages
4566 ***********************************************************************/
4567
4568
4569 /* Output a newline in the *Messages* buffer if "needs" one. */
4570
4571 void
4572 message_log_maybe_newline ()
4573 {
4574 if (message_log_need_newline)
4575 message_dolog ("", 0, 1, 0);
4576 }
4577
4578
4579 /* Add a string M of length LEN to the message log, optionally
4580 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
4581 nonzero, means interpret the contents of M as multibyte. This
4582 function calls low-level routines in order to bypass text property
4583 hooks, etc. which might not be safe to run. */
4584
4585 void
4586 message_dolog (m, len, nlflag, multibyte)
4587 char *m;
4588 int len, nlflag, multibyte;
4589 {
4590 if (!NILP (Vmessage_log_max))
4591 {
4592 struct buffer *oldbuf;
4593 Lisp_Object oldpoint, oldbegv, oldzv;
4594 int old_windows_or_buffers_changed = windows_or_buffers_changed;
4595 int point_at_end = 0;
4596 int zv_at_end = 0;
4597 Lisp_Object old_deactivate_mark, tem;
4598 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4599
4600 old_deactivate_mark = Vdeactivate_mark;
4601 oldbuf = current_buffer;
4602 Fset_buffer (Fget_buffer_create (build_string ("*Messages*")));
4603 current_buffer->undo_list = Qt;
4604
4605 oldpoint = Fpoint_marker ();
4606 oldbegv = Fpoint_min_marker ();
4607 oldzv = Fpoint_max_marker ();
4608 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
4609
4610 if (PT == Z)
4611 point_at_end = 1;
4612 if (ZV == Z)
4613 zv_at_end = 1;
4614
4615 BEGV = BEG;
4616 BEGV_BYTE = BEG_BYTE;
4617 ZV = Z;
4618 ZV_BYTE = Z_BYTE;
4619 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4620
4621 /* Insert the string--maybe converting multibyte to single byte
4622 or vice versa, so that all the text fits the buffer. */
4623 if (multibyte
4624 && NILP (current_buffer->enable_multibyte_characters))
4625 {
4626 int i, c, nbytes;
4627 unsigned char work[1];
4628
4629 /* Convert a multibyte string to single-byte
4630 for the *Message* buffer. */
4631 for (i = 0; i < len; i += nbytes)
4632 {
4633 c = string_char_and_length (m + i, len - i, &nbytes);
4634 work[0] = (SINGLE_BYTE_CHAR_P (c)
4635 ? c
4636 : multibyte_char_to_unibyte (c, Qnil));
4637 insert_1_both (work, 1, 1, 1, 0, 0);
4638 }
4639 }
4640 else if (! multibyte
4641 && ! NILP (current_buffer->enable_multibyte_characters))
4642 {
4643 int i, c, nbytes;
4644 unsigned char *msg = (unsigned char *) m;
4645 unsigned char *str, work[4];
4646 /* Convert a single-byte string to multibyte
4647 for the *Message* buffer. */
4648 for (i = 0; i < len; i++)
4649 {
4650 c = unibyte_char_to_multibyte (msg[i]);
4651 nbytes = CHAR_STRING (c, work, str);
4652 insert_1_both (work, 1, nbytes, 1, 0, 0);
4653 }
4654 }
4655 else if (len)
4656 insert_1 (m, len, 1, 0, 0);
4657
4658 if (nlflag)
4659 {
4660 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
4661 insert_1 ("\n", 1, 1, 0, 0);
4662
4663 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
4664 this_bol = PT;
4665 this_bol_byte = PT_BYTE;
4666
4667 if (this_bol > BEG)
4668 {
4669 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
4670 prev_bol = PT;
4671 prev_bol_byte = PT_BYTE;
4672
4673 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
4674 this_bol, this_bol_byte);
4675 if (dup)
4676 {
4677 del_range_both (prev_bol, prev_bol_byte,
4678 this_bol, this_bol_byte, 0);
4679 if (dup > 1)
4680 {
4681 char dupstr[40];
4682 int duplen;
4683
4684 /* If you change this format, don't forget to also
4685 change message_log_check_duplicate. */
4686 sprintf (dupstr, " [%d times]", dup);
4687 duplen = strlen (dupstr);
4688 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
4689 insert_1 (dupstr, duplen, 1, 0, 1);
4690 }
4691 }
4692 }
4693
4694 if (NATNUMP (Vmessage_log_max))
4695 {
4696 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
4697 -XFASTINT (Vmessage_log_max) - 1, 0);
4698 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
4699 }
4700 }
4701 BEGV = XMARKER (oldbegv)->charpos;
4702 BEGV_BYTE = marker_byte_position (oldbegv);
4703
4704 if (zv_at_end)
4705 {
4706 ZV = Z;
4707 ZV_BYTE = Z_BYTE;
4708 }
4709 else
4710 {
4711 ZV = XMARKER (oldzv)->charpos;
4712 ZV_BYTE = marker_byte_position (oldzv);
4713 }
4714
4715 if (point_at_end)
4716 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4717 else
4718 /* We can't do Fgoto_char (oldpoint) because it will run some
4719 Lisp code. */
4720 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
4721 XMARKER (oldpoint)->bytepos);
4722
4723 UNGCPRO;
4724 free_marker (oldpoint);
4725 free_marker (oldbegv);
4726 free_marker (oldzv);
4727
4728 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
4729 set_buffer_internal (oldbuf);
4730 if (NILP (tem))
4731 windows_or_buffers_changed = old_windows_or_buffers_changed;
4732 message_log_need_newline = !nlflag;
4733 Vdeactivate_mark = old_deactivate_mark;
4734 }
4735 }
4736
4737
4738 /* We are at the end of the buffer after just having inserted a newline.
4739 (Note: We depend on the fact we won't be crossing the gap.)
4740 Check to see if the most recent message looks a lot like the previous one.
4741 Return 0 if different, 1 if the new one should just replace it, or a
4742 value N > 1 if we should also append " [N times]". */
4743
4744 static int
4745 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
4746 int prev_bol, this_bol;
4747 int prev_bol_byte, this_bol_byte;
4748 {
4749 int i;
4750 int len = Z_BYTE - 1 - this_bol_byte;
4751 int seen_dots = 0;
4752 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
4753 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
4754
4755 for (i = 0; i < len; i++)
4756 {
4757 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
4758 && p1[i] != '\n')
4759 seen_dots = 1;
4760 if (p1[i] != p2[i])
4761 return seen_dots;
4762 }
4763 p1 += len;
4764 if (*p1 == '\n')
4765 return 2;
4766 if (*p1++ == ' ' && *p1++ == '[')
4767 {
4768 int n = 0;
4769 while (*p1 >= '0' && *p1 <= '9')
4770 n = n * 10 + *p1++ - '0';
4771 if (strncmp (p1, " times]\n", 8) == 0)
4772 return n+1;
4773 }
4774 return 0;
4775 }
4776
4777
4778 /* Display an echo area message M with a specified length of LEN
4779 chars. The string may include null characters. If M is 0, clear
4780 out any existing message, and let the mini-buffer text show through.
4781
4782 The buffer M must continue to exist until after the echo area gets
4783 cleared or some other message gets displayed there. This means do
4784 not pass text that is stored in a Lisp string; do not pass text in
4785 a buffer that was alloca'd. */
4786
4787 void
4788 message2 (m, len, multibyte)
4789 char *m;
4790 int len;
4791 int multibyte;
4792 {
4793 /* First flush out any partial line written with print. */
4794 message_log_maybe_newline ();
4795 if (m)
4796 message_dolog (m, len, 1, multibyte);
4797 message2_nolog (m, len, multibyte);
4798 }
4799
4800
4801 /* The non-logging counterpart of message2. */
4802
4803 void
4804 message2_nolog (m, len, multibyte)
4805 char *m;
4806 int len;
4807 {
4808 message_enable_multibyte = multibyte;
4809
4810 if (noninteractive)
4811 {
4812 if (noninteractive_need_newline)
4813 putc ('\n', stderr);
4814 noninteractive_need_newline = 0;
4815 if (m)
4816 fwrite (m, len, 1, stderr);
4817 if (cursor_in_echo_area == 0)
4818 fprintf (stderr, "\n");
4819 fflush (stderr);
4820 }
4821 /* A null message buffer means that the frame hasn't really been
4822 initialized yet. Error messages get reported properly by
4823 cmd_error, so this must be just an informative message; toss it. */
4824 else if (INTERACTIVE
4825 && selected_frame->glyphs_initialized_p
4826 && FRAME_MESSAGE_BUF (selected_frame))
4827 {
4828 Lisp_Object mini_window;
4829 struct frame *f;
4830
4831 /* Get the frame containing the mini-buffer
4832 that the selected frame is using. */
4833 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
4834 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
4835
4836 FRAME_SAMPLE_VISIBILITY (f);
4837 if (FRAME_VISIBLE_P (selected_frame)
4838 && ! FRAME_VISIBLE_P (f))
4839 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
4840
4841 if (m)
4842 {
4843 echo_area_glyphs = m;
4844 echo_area_glyphs_length = len;
4845 echo_area_message = Qnil;
4846
4847 if (minibuffer_auto_raise)
4848 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
4849 }
4850 else
4851 {
4852 echo_area_glyphs = previous_echo_glyphs = NULL;
4853 echo_area_message = previous_echo_area_message = Qnil;
4854 }
4855
4856 do_pending_window_change ();
4857 echo_area_display (1);
4858 do_pending_window_change ();
4859 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
4860 (*frame_up_to_date_hook) (f);
4861 }
4862 }
4863
4864
4865 /* Display an echo area message M with a specified length of LEN
4866 chars. The string may include null characters. If M is not a
4867 string, clear out any existing message, and let the mini-buffer
4868 text show through. */
4869
4870 void
4871 message3 (m, len, multibyte)
4872 Lisp_Object m;
4873 int len;
4874 int multibyte;
4875 {
4876 struct gcpro gcpro1;
4877
4878 GCPRO1 (m);
4879
4880 /* First flush out any partial line written with print. */
4881 message_log_maybe_newline ();
4882 if (STRINGP (m))
4883 message_dolog (XSTRING (m)->data, len, 1, multibyte);
4884 message3_nolog (m, len, multibyte);
4885
4886 UNGCPRO;
4887 }
4888
4889
4890 /* The non-logging version of message3. */
4891
4892 void
4893 message3_nolog (m, len, multibyte)
4894 Lisp_Object m;
4895 int len, multibyte;
4896 {
4897 message_enable_multibyte = multibyte;
4898
4899 if (noninteractive)
4900 {
4901 if (noninteractive_need_newline)
4902 putc ('\n', stderr);
4903 noninteractive_need_newline = 0;
4904 if (STRINGP (m))
4905 fwrite (XSTRING (m)->data, len, 1, stderr);
4906 if (cursor_in_echo_area == 0)
4907 fprintf (stderr, "\n");
4908 fflush (stderr);
4909 }
4910 /* A null message buffer means that the frame hasn't really been
4911 initialized yet. Error messages get reported properly by
4912 cmd_error, so this must be just an informative message; toss it. */
4913 else if (INTERACTIVE
4914 && selected_frame->glyphs_initialized_p
4915 && FRAME_MESSAGE_BUF (selected_frame))
4916 {
4917 Lisp_Object mini_window;
4918 struct frame *f;
4919
4920 /* Get the frame containing the mini-buffer
4921 that the selected frame is using. */
4922 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
4923 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
4924
4925 FRAME_SAMPLE_VISIBILITY (f);
4926 if (FRAME_VISIBLE_P (selected_frame)
4927 && ! FRAME_VISIBLE_P (f))
4928 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
4929
4930 if (STRINGP (m))
4931 {
4932 echo_area_glyphs = NULL;
4933 echo_area_message = m;
4934 echo_area_glyphs_length = len;
4935
4936 if (minibuffer_auto_raise)
4937 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
4938 }
4939 else
4940 {
4941 echo_area_glyphs = previous_echo_glyphs = NULL;
4942 echo_area_message = previous_echo_area_message = Qnil;
4943 }
4944
4945 do_pending_window_change ();
4946 echo_area_display (1);
4947 do_pending_window_change ();
4948 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
4949 (*frame_up_to_date_hook) (f);
4950 }
4951 }
4952
4953
4954 /* Display a null-terminated echo area message M. If M is 0, clear
4955 out any existing message, and let the mini-buffer text show through.
4956
4957 The buffer M must continue to exist until after the echo area gets
4958 cleared or some other message gets displayed there. Do not pass
4959 text that is stored in a Lisp string. Do not pass text in a buffer
4960 that was alloca'd. */
4961
4962 void
4963 message1 (m)
4964 char *m;
4965 {
4966 message2 (m, (m ? strlen (m) : 0), 0);
4967 }
4968
4969
4970 /* The non-logging counterpart of message1. */
4971
4972 void
4973 message1_nolog (m)
4974 char *m;
4975 {
4976 message2_nolog (m, (m ? strlen (m) : 0), 0);
4977 }
4978
4979 /* Display a message M which contains a single %s
4980 which gets replaced with STRING. */
4981
4982 void
4983 message_with_string (m, string, log)
4984 char *m;
4985 Lisp_Object string;
4986 int log;
4987 {
4988 if (noninteractive)
4989 {
4990 if (m)
4991 {
4992 if (noninteractive_need_newline)
4993 putc ('\n', stderr);
4994 noninteractive_need_newline = 0;
4995 fprintf (stderr, m, XSTRING (string)->data);
4996 if (cursor_in_echo_area == 0)
4997 fprintf (stderr, "\n");
4998 fflush (stderr);
4999 }
5000 }
5001 else if (INTERACTIVE)
5002 {
5003 /* The frame whose minibuffer we're going to display the message on.
5004 It may be larger than the selected frame, so we need
5005 to use its buffer, not the selected frame's buffer. */
5006 Lisp_Object mini_window;
5007 FRAME_PTR f;
5008
5009 /* Get the frame containing the minibuffer
5010 that the selected frame is using. */
5011 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
5012 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5013
5014 /* A null message buffer means that the frame hasn't really been
5015 initialized yet. Error messages get reported properly by
5016 cmd_error, so this must be just an informative message; toss it. */
5017 if (FRAME_MESSAGE_BUF (f))
5018 {
5019 int len;
5020 char *a[1];
5021 a[0] = (char *) XSTRING (string)->data;
5022
5023 len = doprnt (FRAME_MESSAGE_BUF (f),
5024 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5025
5026 if (log)
5027 message2 (FRAME_MESSAGE_BUF (f), len,
5028 STRING_MULTIBYTE (string));
5029 else
5030 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5031 STRING_MULTIBYTE (string));
5032
5033 /* Print should start at the beginning of the message
5034 buffer next time. */
5035 message_buf_print = 0;
5036 }
5037 }
5038 }
5039
5040
5041 /* Truncate what will be displayed in the echo area
5042 the next time we display it--but don't redisplay it now. */
5043
5044 void
5045 truncate_echo_area (len)
5046 int len;
5047 {
5048 /* A null message buffer means that the frame hasn't really been
5049 initialized yet. Error messages get reported properly by
5050 cmd_error, so this must be just an informative message; toss it. */
5051 if (!noninteractive && INTERACTIVE && FRAME_MESSAGE_BUF (selected_frame))
5052 echo_area_glyphs_length = len;
5053 }
5054
5055
5056 /* Nonzero if FRAME_MESSAGE_BUF (selected_frame) is being used by
5057 print; zero if being used by message. */
5058
5059 int message_buf_print;
5060
5061
5062 /* Dump an informative message to the minibuf. If M is 0, clear out
5063 any existing message, and let the mini-buffer text show through. */
5064
5065 /* VARARGS 1 */
5066 void
5067 message (m, a1, a2, a3)
5068 char *m;
5069 EMACS_INT a1, a2, a3;
5070 {
5071 if (noninteractive)
5072 {
5073 if (m)
5074 {
5075 if (noninteractive_need_newline)
5076 putc ('\n', stderr);
5077 noninteractive_need_newline = 0;
5078 fprintf (stderr, m, a1, a2, a3);
5079 if (cursor_in_echo_area == 0)
5080 fprintf (stderr, "\n");
5081 fflush (stderr);
5082 }
5083 }
5084 else if (INTERACTIVE)
5085 {
5086 /* The frame whose mini-buffer we're going to display the message
5087 on. It may be larger than the selected frame, so we need to
5088 use its buffer, not the selected frame's buffer. */
5089 Lisp_Object mini_window;
5090 struct frame *f;
5091
5092 /* Get the frame containing the mini-buffer
5093 that the selected frame is using. */
5094 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
5095 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5096
5097 /* A null message buffer means that the frame hasn't really been
5098 initialized yet. Error messages get reported properly by
5099 cmd_error, so this must be just an informative message; toss
5100 it. */
5101 if (FRAME_MESSAGE_BUF (f))
5102 {
5103 if (m)
5104 {
5105 int len;
5106 #ifdef NO_ARG_ARRAY
5107 char *a[3];
5108 a[0] = (char *) a1;
5109 a[1] = (char *) a2;
5110 a[2] = (char *) a3;
5111
5112 len = doprnt (FRAME_MESSAGE_BUF (f),
5113 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5114 #else
5115 len = doprnt (FRAME_MESSAGE_BUF (f),
5116 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5117 (char **) &a1);
5118 #endif /* NO_ARG_ARRAY */
5119
5120 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5121 }
5122 else
5123 message1 (0);
5124
5125 /* Print should start at the beginning of the message
5126 buffer next time. */
5127 message_buf_print = 0;
5128 }
5129 }
5130 }
5131
5132
5133 /* The non-logging version of message. */
5134
5135 void
5136 message_nolog (m, a1, a2, a3)
5137 char *m;
5138 EMACS_INT a1, a2, a3;
5139 {
5140 Lisp_Object old_log_max;
5141 old_log_max = Vmessage_log_max;
5142 Vmessage_log_max = Qnil;
5143 message (m, a1, a2, a3);
5144 Vmessage_log_max = old_log_max;
5145 }
5146
5147
5148 /* Display echo_area_message or echo_area_glyphs in the current
5149 mini-buffer. */
5150
5151 void
5152 update_echo_area ()
5153 {
5154 if (STRINGP (echo_area_message))
5155 message3 (echo_area_message, echo_area_glyphs_length,
5156 !NILP (current_buffer->enable_multibyte_characters));
5157 else
5158 message2 (echo_area_glyphs, echo_area_glyphs_length,
5159 !NILP (current_buffer->enable_multibyte_characters));
5160 }
5161
5162
5163 /* Redisplay the echo area of selected_frame. If UPDATE_FRAME_P is
5164 non-zero update selected_frame. */
5165
5166 static void
5167 echo_area_display (update_frame_p)
5168 int update_frame_p;
5169 {
5170 Lisp_Object mini_window;
5171 struct window *w;
5172 struct frame *f;
5173
5174 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
5175 w = XWINDOW (mini_window);
5176 f = XFRAME (WINDOW_FRAME (w));
5177
5178 /* Don't display if frame is invisible or not yet initialized. */
5179 if (!FRAME_VISIBLE_P (f)
5180 || !f->glyphs_initialized_p)
5181 return;
5182
5183 /* When Emacs starts, selected_frame may be a visible terminal
5184 frame, even if we run under a window system. If we let this
5185 through, a message would be displayed on the terminal. */
5186 #ifdef HAVE_WINDOW_SYSTEM
5187 if (!inhibit_window_system && !FRAME_WINDOW_P (selected_frame))
5188 return;
5189 #endif /* HAVE_WINDOW_SYSTEM */
5190
5191 /* Redraw garbaged frames. */
5192 if (frame_garbaged)
5193 {
5194 /* Old redisplay called redraw_garbaged_frames here which in
5195 turn called redraw_frame which in turn called clear_frame.
5196 The call to clear_frame is a source of flickering. After
5197 checking the places where SET_FRAME_GARBAGED is called, I
5198 believe a clear_frame is not necessary. It should suffice in
5199 the new redisplay to invalidate all current matrices, and
5200 ensure a complete redisplay of all windows. */
5201 Lisp_Object tail, frame;
5202
5203 FOR_EACH_FRAME (tail, frame)
5204 {
5205 struct frame *f = XFRAME (frame);
5206
5207 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
5208 {
5209 clear_current_matrices (f);
5210 f->garbaged = 0;
5211 }
5212 }
5213
5214 frame_garbaged = 0;
5215 ++windows_or_buffers_changed;
5216 }
5217
5218 if (echo_area_glyphs
5219 || STRINGP (echo_area_message)
5220 || minibuf_level == 0)
5221 {
5222 struct it it;
5223
5224 echo_area_window = mini_window;
5225 clear_glyph_matrix (w->desired_matrix);
5226 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, DEFAULT_FACE_ID);
5227
5228 if (STRINGP (echo_area_message)
5229 && echo_area_glyphs_length)
5230 {
5231 prepare_desired_row (it.glyph_row);
5232 display_string (NULL, echo_area_message, Qnil, 0, 0,
5233 &it, -1, echo_area_glyphs_length, 0,
5234 message_enable_multibyte);
5235 it.glyph_row->truncated_on_right_p = 0;
5236 compute_line_metrics (&it);
5237 }
5238 else if (echo_area_glyphs
5239 && echo_area_glyphs_length)
5240 {
5241 prepare_desired_row (it.glyph_row);
5242 display_string (echo_area_glyphs, Qnil, Qnil, 0, 0, &it,
5243 -1, echo_area_glyphs_length, 0,
5244 message_enable_multibyte);
5245 it.glyph_row->truncated_on_right_p = 0;
5246 compute_line_metrics (&it);
5247 }
5248 else
5249 blank_row (w, it.glyph_row, 0);
5250
5251 it.glyph_row->y = it.current_y;
5252 it.current_y += it.glyph_row->height;
5253
5254 /* Clear the rest of the lines. */
5255 while (it.current_y < it.last_visible_y)
5256 {
5257 ++it.glyph_row;
5258 blank_row (w, it.glyph_row, it.current_y);
5259 it.current_y += it.glyph_row->height;
5260 }
5261
5262 w->must_be_updated_p = 1;
5263 if (update_frame_p)
5264 {
5265 /* Calling update_single_window is faster when we can use
5266 window-based redisplay. */
5267 if (FRAME_WINDOW_P (f))
5268 {
5269 update_single_window (w, 1);
5270 rif->flush_display (f);
5271 }
5272 else
5273 update_frame (f, 1, 1);
5274 }
5275 }
5276 else if (!EQ (mini_window, selected_window))
5277 windows_or_buffers_changed++;
5278
5279 /* Prevent redisplay optimization in redisplay_internal by resetting
5280 this_line_start_pos. This is done because the mini-buffer now
5281 displays the message instead of its buffer text. */
5282 if (EQ (mini_window, selected_window))
5283 CHARPOS (this_line_start_pos) = 0;
5284
5285 previous_echo_glyphs = echo_area_glyphs;
5286 previous_echo_area_message = echo_area_message;
5287 previous_echo_glyphs_length = echo_area_glyphs_length;
5288 }
5289
5290
5291 \f
5292 /***********************************************************************
5293 Frame Titles
5294 ***********************************************************************/
5295
5296
5297 #ifdef HAVE_WINDOW_SYSTEM
5298
5299 /* A buffer for constructing frame titles in it; allocated from the
5300 heap in init_xdisp and resized as needed in store_frame_title_char. */
5301
5302 static char *frame_title_buf;
5303
5304 /* The buffer's end, and a current output position in it. */
5305
5306 static char *frame_title_buf_end;
5307 static char *frame_title_ptr;
5308
5309
5310 /* Store a single character C for the frame title in frame_title_buf.
5311 Re-allocate frame_title_buf if necessary. */
5312
5313 static void
5314 store_frame_title_char (c)
5315 char c;
5316 {
5317 /* If output position has reached the end of the allocated buffer,
5318 double the buffer's size. */
5319 if (frame_title_ptr == frame_title_buf_end)
5320 {
5321 int len = frame_title_ptr - frame_title_buf;
5322 int new_size = 2 * len * sizeof *frame_title_buf;
5323 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
5324 frame_title_buf_end = frame_title_buf + new_size;
5325 frame_title_ptr = frame_title_buf + len;
5326 }
5327
5328 *frame_title_ptr++ = c;
5329 }
5330
5331
5332 /* Store part of a frame title in frame_title_buf, beginning at
5333 frame_title_ptr. STR is the string to store. Do not copy more
5334 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
5335 the whole string. Pad with spaces until FIELD_WIDTH number of
5336 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
5337 Called from display_mode_element when it is used to build a frame
5338 title. */
5339
5340 static int
5341 store_frame_title (str, field_width, precision)
5342 unsigned char *str;
5343 int field_width, precision;
5344 {
5345 int n = 0;
5346
5347 /* Copy at most PRECISION chars from STR. */
5348 while ((precision <= 0 || n < precision)
5349 && *str)
5350 {
5351 store_frame_title_char (*str++);
5352 ++n;
5353 }
5354
5355 /* Fill up with spaces until FIELD_WIDTH reached. */
5356 while (field_width > 0
5357 && n < field_width)
5358 {
5359 store_frame_title_char (' ');
5360 ++n;
5361 }
5362
5363 return n;
5364 }
5365
5366
5367 /* Set the title of FRAME, if it has changed. The title format is
5368 Vicon_title_format if FRAME is iconified, otherwise it is
5369 frame_title_format. */
5370
5371 static void
5372 x_consider_frame_title (frame)
5373 Lisp_Object frame;
5374 {
5375 struct frame *f = XFRAME (frame);
5376
5377 if (FRAME_WINDOW_P (f)
5378 || FRAME_MINIBUF_ONLY_P (f)
5379 || f->explicit_name)
5380 {
5381 /* Do we have more than one visible frame on this X display? */
5382 Lisp_Object tail;
5383 Lisp_Object fmt;
5384 struct buffer *obuf;
5385 int len;
5386 struct it it;
5387
5388 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
5389 {
5390 struct frame *tf = XFRAME (XCONS (tail)->car);
5391
5392 if (tf != f
5393 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
5394 && !FRAME_MINIBUF_ONLY_P (tf)
5395 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
5396 break;
5397 }
5398
5399 /* Set global variable indicating that multiple frames exist. */
5400 multiple_frames = CONSP (tail);
5401
5402 /* Switch to the buffer of selected window of the frame. Set up
5403 frame_title_ptr so that display_mode_element will output into it;
5404 then display the title. */
5405 obuf = current_buffer;
5406 Fset_buffer (XWINDOW (f->selected_window)->buffer);
5407 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
5408 frame_title_ptr = frame_title_buf;
5409 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
5410 NULL, DEFAULT_FACE_ID);
5411 len = display_mode_element (&it, 0, -1, -1, fmt);
5412 frame_title_ptr = NULL;
5413 set_buffer_internal (obuf);
5414
5415 /* Set the title only if it's changed. This avoids consing in
5416 the common case where it hasn't. (If it turns out that we've
5417 already wasted too much time by walking through the list with
5418 display_mode_element, then we might need to optimize at a
5419 higher level than this.) */
5420 if (! STRINGP (f->name)
5421 || STRING_BYTES (XSTRING (f->name)) != len
5422 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
5423 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
5424 }
5425 }
5426
5427 #else /* not HAVE_WINDOW_SYSTEM */
5428
5429 #define frame_title_ptr ((char *)0)
5430 #define store_frame_title(str, mincol, maxcol) 0
5431
5432 #endif /* not HAVE_WINDOW_SYSTEM */
5433
5434
5435
5436 \f
5437 /***********************************************************************
5438 Menu Bars
5439 ***********************************************************************/
5440
5441
5442 /* Prepare for redisplay by updating menu-bar item lists when
5443 appropriate. This can call eval. */
5444
5445 void
5446 prepare_menu_bars ()
5447 {
5448 int all_windows;
5449 struct gcpro gcpro1, gcpro2;
5450 struct frame *f;
5451 struct frame *tooltip_frame;
5452
5453 #ifdef HAVE_X_WINDOWS
5454 tooltip_frame = tip_frame;
5455 #else
5456 tooltip_frame = NULL;
5457 #endif
5458
5459 /* Update all frame titles based on their buffer names, etc. We do
5460 this before the menu bars so that the buffer-menu will show the
5461 up-to-date frame titles. */
5462 #ifdef HAVE_WINDOW_SYSTEM
5463 if (windows_or_buffers_changed || update_mode_lines)
5464 {
5465 Lisp_Object tail, frame;
5466
5467 FOR_EACH_FRAME (tail, frame)
5468 {
5469 f = XFRAME (frame);
5470 if (f != tooltip_frame
5471 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
5472 x_consider_frame_title (frame);
5473 }
5474 }
5475 #endif /* HAVE_WINDOW_SYSTEM */
5476
5477 /* Update the menu bar item lists, if appropriate. This has to be
5478 done before any actual redisplay or generation of display lines. */
5479 all_windows = (update_mode_lines
5480 || buffer_shared > 1
5481 || windows_or_buffers_changed);
5482 if (all_windows)
5483 {
5484 Lisp_Object tail, frame;
5485 int count = specpdl_ptr - specpdl;
5486
5487 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
5488
5489 FOR_EACH_FRAME (tail, frame)
5490 {
5491 f = XFRAME (frame);
5492
5493 /* Ignore tooltip frame. */
5494 if (f == tooltip_frame)
5495 continue;
5496
5497 /* If a window on this frame changed size, report that to
5498 the user and clear the size-change flag. */
5499 if (FRAME_WINDOW_SIZES_CHANGED (f))
5500 {
5501 Lisp_Object functions;
5502
5503 /* Clear flag first in case we get an error below. */
5504 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
5505 functions = Vwindow_size_change_functions;
5506 GCPRO2 (tail, functions);
5507
5508 while (CONSP (functions))
5509 {
5510 call1 (XCAR (functions), frame);
5511 functions = XCDR (functions);
5512 }
5513 UNGCPRO;
5514 }
5515
5516 GCPRO1 (tail);
5517 update_menu_bar (f, 0);
5518 #ifdef HAVE_WINDOW_SYSTEM
5519 update_toolbar (f, 0);
5520 #endif
5521 UNGCPRO;
5522 }
5523
5524 unbind_to (count, Qnil);
5525 }
5526 else
5527 {
5528 update_menu_bar (selected_frame, 1);
5529 #ifdef HAVE_WINDOW_SYSTEM
5530 update_toolbar (selected_frame, 1);
5531 #endif
5532 }
5533
5534 /* Motif needs this. See comment in xmenu.c. Turn it off when
5535 pending_menu_activation is not defined. */
5536 #ifdef USE_X_TOOLKIT
5537 pending_menu_activation = 0;
5538 #endif
5539 }
5540
5541
5542 /* Update the menu bar item list for frame F. This has to be done
5543 before we start to fill in any display lines, because it can call
5544 eval.
5545
5546 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
5547
5548 static void
5549 update_menu_bar (f, save_match_data)
5550 struct frame *f;
5551 int save_match_data;
5552 {
5553 Lisp_Object window;
5554 register struct window *w;
5555
5556 window = FRAME_SELECTED_WINDOW (f);
5557 w = XWINDOW (window);
5558
5559 if (update_mode_lines)
5560 w->update_mode_line = Qt;
5561
5562 if (FRAME_WINDOW_P (f)
5563 ?
5564 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
5565 FRAME_EXTERNAL_MENU_BAR (f)
5566 #else
5567 FRAME_MENU_BAR_LINES (f) > 0
5568 #endif
5569 : FRAME_MENU_BAR_LINES (f) > 0)
5570 {
5571 /* If the user has switched buffers or windows, we need to
5572 recompute to reflect the new bindings. But we'll
5573 recompute when update_mode_lines is set too; that means
5574 that people can use force-mode-line-update to request
5575 that the menu bar be recomputed. The adverse effect on
5576 the rest of the redisplay algorithm is about the same as
5577 windows_or_buffers_changed anyway. */
5578 if (windows_or_buffers_changed
5579 || !NILP (w->update_mode_line)
5580 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
5581 < BUF_MODIFF (XBUFFER (w->buffer)))
5582 != !NILP (w->last_had_star))
5583 || ((!NILP (Vtransient_mark_mode)
5584 && !NILP (XBUFFER (w->buffer)->mark_active))
5585 != !NILP (w->region_showing)))
5586 {
5587 struct buffer *prev = current_buffer;
5588 int count = specpdl_ptr - specpdl;
5589
5590 set_buffer_internal_1 (XBUFFER (w->buffer));
5591 if (save_match_data)
5592 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
5593 if (NILP (Voverriding_local_map_menu_flag))
5594 {
5595 specbind (Qoverriding_terminal_local_map, Qnil);
5596 specbind (Qoverriding_local_map, Qnil);
5597 }
5598
5599 /* Run the Lucid hook. */
5600 call1 (Vrun_hooks, Qactivate_menubar_hook);
5601
5602 /* If it has changed current-menubar from previous value,
5603 really recompute the menu-bar from the value. */
5604 if (! NILP (Vlucid_menu_bar_dirty_flag))
5605 call0 (Qrecompute_lucid_menubar);
5606
5607 safe_run_hooks (Qmenu_bar_update_hook);
5608 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
5609
5610 /* Redisplay the menu bar in case we changed it. */
5611 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
5612 if (FRAME_WINDOW_P (f))
5613 set_frame_menubar (f, 0, 0);
5614 else
5615 /* On a terminal screen, the menu bar is an ordinary screen
5616 line, and this makes it get updated. */
5617 w->update_mode_line = Qt;
5618 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
5619 /* In the non-toolkit version, the menu bar is an ordinary screen
5620 line, and this makes it get updated. */
5621 w->update_mode_line = Qt;
5622 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
5623
5624 unbind_to (count, Qnil);
5625 set_buffer_internal_1 (prev);
5626 }
5627 }
5628 }
5629
5630
5631 \f
5632 /***********************************************************************
5633 Toolbars
5634 ***********************************************************************/
5635
5636 #ifdef HAVE_WINDOW_SYSTEM
5637
5638 /* Update the toolbar item list for frame F. This has to be done
5639 before we start to fill in any display lines. Called from
5640 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
5641 and restore it here. */
5642
5643 static void
5644 update_toolbar (f, save_match_data)
5645 struct frame *f;
5646 int save_match_data;
5647 {
5648 if (WINDOWP (f->toolbar_window)
5649 && XFASTINT (XWINDOW (f->toolbar_window)->height) > 0)
5650 {
5651 Lisp_Object window;
5652 struct window *w;
5653
5654 window = FRAME_SELECTED_WINDOW (f);
5655 w = XWINDOW (window);
5656
5657 /* If the user has switched buffers or windows, we need to
5658 recompute to reflect the new bindings. But we'll
5659 recompute when update_mode_lines is set too; that means
5660 that people can use force-mode-line-update to request
5661 that the menu bar be recomputed. The adverse effect on
5662 the rest of the redisplay algorithm is about the same as
5663 windows_or_buffers_changed anyway. */
5664 if (windows_or_buffers_changed
5665 || !NILP (w->update_mode_line)
5666 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
5667 < BUF_MODIFF (XBUFFER (w->buffer)))
5668 != !NILP (w->last_had_star))
5669 || ((!NILP (Vtransient_mark_mode)
5670 && !NILP (XBUFFER (w->buffer)->mark_active))
5671 != !NILP (w->region_showing)))
5672 {
5673 struct buffer *prev = current_buffer;
5674 int count = specpdl_ptr - specpdl;
5675
5676 /* Set current_buffer to the buffer of the selected
5677 window of the frame, so that we get the right local
5678 keymaps. */
5679 set_buffer_internal_1 (XBUFFER (w->buffer));
5680
5681 /* Save match data, if we must. */
5682 if (save_match_data)
5683 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
5684
5685 /* Make sure that we don't accidentally use bogus keymaps. */
5686 if (NILP (Voverriding_local_map_menu_flag))
5687 {
5688 specbind (Qoverriding_terminal_local_map, Qnil);
5689 specbind (Qoverriding_local_map, Qnil);
5690 }
5691
5692 /* Build desired toolbar items from keymaps. */
5693 f->desired_toolbar_items
5694 = toolbar_items (f->desired_toolbar_items,
5695 &f->n_desired_toolbar_items);
5696
5697 /* Redisplay the toolbar in case we changed it. */
5698 w->update_mode_line = Qt;
5699
5700 unbind_to (count, Qnil);
5701 set_buffer_internal_1 (prev);
5702 }
5703 }
5704 }
5705
5706
5707 /* Set F->desired_toolbar_string to a Lisp string representing frame
5708 F's desired toolbar contents. F->desired_toolbar_items must have
5709 been set up previously by calling prepare_menu_bars. */
5710
5711 static void
5712 build_desired_toolbar_string (f)
5713 struct frame *f;
5714 {
5715 int i, size, size_needed, string_idx;
5716 struct gcpro gcpro1, gcpro2, gcpro3;
5717 Lisp_Object image, plist, props;
5718
5719 image = plist = props = Qnil;
5720 GCPRO3 (image, plist, props);
5721
5722 /* Prepare F->desired_toolbar_string. If we can reuse it, do so.
5723 Otherwise, make a new string. */
5724
5725 /* The size of the string we might be able to reuse. */
5726 size = (STRINGP (f->desired_toolbar_string)
5727 ? XSTRING (f->desired_toolbar_string)->size
5728 : 0);
5729
5730 /* Each image in the string we build is preceded by a space,
5731 and there is a space at the end. */
5732 size_needed = f->n_desired_toolbar_items + 1;
5733
5734 /* Reuse f->desired_toolbar_string, if possible. */
5735 if (size < size_needed)
5736 f->desired_toolbar_string = Fmake_string (make_number (size_needed), ' ');
5737 else
5738 {
5739 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
5740 Fremove_text_properties (make_number (0), make_number (size),
5741 props, f->desired_toolbar_string);
5742 }
5743
5744 /* Put a `display' property on the string for the images to display,
5745 put a `menu_item' property on toolbar items with a value that
5746 is the index of the item in F's toolbar item vector. */
5747 for (i = 0, string_idx = 0;
5748 i < f->n_desired_toolbar_items;
5749 ++i, string_idx += 1)
5750 {
5751 #define PROP(IDX) \
5752 (XVECTOR (f->desired_toolbar_items) \
5753 ->contents[i * TOOLBAR_ITEM_NSLOTS + (IDX)])
5754
5755 int enabled_p = !NILP (PROP (TOOLBAR_ITEM_ENABLED_P));
5756 int selected_p = !NILP (PROP (TOOLBAR_ITEM_SELECTED_P));
5757 int margin, relief;
5758 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
5759 extern Lisp_Object Qlaplace;
5760
5761 /* If image is a vector, choose the image according to the
5762 button state. */
5763 image = PROP (TOOLBAR_ITEM_IMAGES);
5764 if (VECTORP (image))
5765 {
5766 enum toolbar_item_image idx;
5767
5768 if (enabled_p)
5769 idx = (selected_p
5770 ? TOOLBAR_IMAGE_ENABLED_SELECTED
5771 : TOOLBAR_IMAGE_ENABLED_DESELECTED);
5772 else
5773 idx = (selected_p
5774 ? TOOLBAR_IMAGE_DISABLED_SELECTED
5775 : TOOLBAR_IMAGE_DISABLED_DESELECTED);
5776
5777 xassert (XVECTOR (image)->size >= idx);
5778 image = XVECTOR (image)->contents[idx];
5779 }
5780
5781 /* Ignore invalid image specifications. */
5782 if (!valid_image_p (image))
5783 continue;
5784
5785 /* Display the toolbar button pressed, or depressed. */
5786 plist = Fcopy_sequence (XCDR (image));
5787
5788 /* Compute margin and relief to draw. */
5789 relief = toolbar_button_relief > 0 ? toolbar_button_relief : 3;
5790 margin = relief + max (0, toolbar_button_margin);
5791
5792 if (auto_raise_toolbar_buttons_p)
5793 {
5794 /* Add a `:relief' property to the image spec if the item is
5795 selected. */
5796 if (selected_p)
5797 {
5798 plist = Fplist_put (plist, QCrelief, make_number (-relief));
5799 margin -= relief;
5800 }
5801 }
5802 else
5803 {
5804 /* If image is selected, display it pressed, i.e. with a
5805 negative relief. If it's not selected, display it with a
5806 raised relief. */
5807 plist = Fplist_put (plist, QCrelief,
5808 (selected_p
5809 ? make_number (-relief)
5810 : make_number (relief)));
5811 margin -= relief;
5812 }
5813
5814 /* Put a margin around the image. */
5815 if (margin)
5816 plist = Fplist_put (plist, QCmargin, make_number (margin));
5817
5818 /* If button is not enabled, make the image appear disabled by
5819 applying an appropriate algorithm to it. */
5820 if (!enabled_p)
5821 plist = Fplist_put (plist, QCalgorithm, Qlaplace);
5822
5823 /* Put a `display' text property on the string for the image to
5824 display. Put a `menu-item' property on the string that gives
5825 the start of this item's properties in the toolbar items
5826 vector. */
5827 image = Fcons (Qimage, plist);
5828 props = list4 (Qdisplay, image,
5829 Qmenu_item, make_number (i * TOOLBAR_ITEM_NSLOTS)),
5830 Fadd_text_properties (make_number (string_idx),
5831 make_number (string_idx + 1),
5832 props, f->desired_toolbar_string);
5833 #undef PROP
5834 }
5835
5836 UNGCPRO;
5837 }
5838
5839
5840 /* Display one line of the toolbar of frame IT->f. */
5841
5842 static void
5843 display_toolbar_line (it)
5844 struct it *it;
5845 {
5846 struct glyph_row *row = it->glyph_row;
5847 int max_x = it->last_visible_x;
5848 struct glyph *last;
5849
5850 prepare_desired_row (row);
5851 row->y = it->current_y;
5852
5853 while (it->current_x < max_x)
5854 {
5855 int x_before, x, n_glyphs_before, i, nglyphs;
5856
5857 /* Get the next display element. */
5858 if (!get_next_display_element (it))
5859 break;
5860
5861 /* Produce glyphs. */
5862 x_before = it->current_x;
5863 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
5864 PRODUCE_GLYPHS (it);
5865
5866 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
5867 i = 0;
5868 x = x_before;
5869 while (i < nglyphs)
5870 {
5871 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
5872
5873 if (x + glyph->pixel_width > max_x)
5874 {
5875 /* Glyph doesn't fit on line. */
5876 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
5877 it->current_x = x;
5878 goto out;
5879 }
5880
5881 ++it->hpos;
5882 x += glyph->pixel_width;
5883 ++i;
5884 }
5885
5886 /* Stop at line ends. */
5887 if (ITERATOR_AT_END_OF_LINE_P (it))
5888 break;
5889
5890 set_iterator_to_next (it);
5891 }
5892
5893 out:;
5894
5895 row->displays_text_p = row->used[TEXT_AREA] != 0;
5896 extend_face_to_end_of_line (it);
5897 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
5898 last->right_box_line_p = 1;
5899 compute_line_metrics (it);
5900
5901 /* If line is empty, make it occupy the rest of the toolbar. */
5902 if (!row->displays_text_p)
5903 {
5904 row->height = it->last_visible_y - row->y;
5905 row->ascent = 0;
5906 }
5907
5908 row->full_width_p = 1;
5909 row->continued_p = 0;
5910 row->truncated_on_left_p = 0;
5911 row->truncated_on_right_p = 0;
5912
5913 it->current_x = it->hpos = 0;
5914 it->current_y += row->height;
5915 ++it->vpos;
5916 ++it->glyph_row;
5917 }
5918
5919
5920 /* Value is the number of screen lines needed to make all toolbar
5921 items of frame F visible. */
5922
5923 static int
5924 toolbar_lines_needed (f)
5925 struct frame *f;
5926 {
5927 struct window *w = XWINDOW (f->toolbar_window);
5928 struct it it;
5929
5930 /* Initialize an iterator for iteration over F->desired_toolbar_string
5931 in the toolbar window of frame F. */
5932 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOLBAR_FACE_ID);
5933 it.first_visible_x = 0;
5934 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
5935 reseat_to_string (&it, NULL, f->desired_toolbar_string, 0, 0, 0, -1);
5936
5937 while (!ITERATOR_AT_END_P (&it))
5938 {
5939 it.glyph_row = w->desired_matrix->rows;
5940 clear_glyph_row (it.glyph_row);
5941 display_toolbar_line (&it);
5942 }
5943
5944 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
5945 }
5946
5947
5948 /* Display the toolbar of frame F. Value is non-zero if toolbar's
5949 height should be changed. */
5950
5951 static int
5952 redisplay_toolbar (f)
5953 struct frame *f;
5954 {
5955 struct window *w;
5956 struct it it;
5957 struct glyph_row *row;
5958 int change_height_p = 0;
5959
5960 /* If frame hasn't a toolbar window or if it is zero-height, don't
5961 do anything. This means you must start with toolbar-lines
5962 non-zero to get the auto-sizing effect. Or in other words, you
5963 can turn off toolbars by specifying toolbar-lines zero. */
5964 if (!WINDOWP (f->toolbar_window)
5965 || (w = XWINDOW (f->toolbar_window),
5966 XFASTINT (w->height) == 0))
5967 return 0;
5968
5969 /* Set up an iterator for the toolbar window. */
5970 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOLBAR_FACE_ID);
5971 it.first_visible_x = 0;
5972 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
5973 row = it.glyph_row;
5974
5975 /* Build a string that represents the contents of the toolbar. */
5976 build_desired_toolbar_string (f);
5977 reseat_to_string (&it, NULL, f->desired_toolbar_string, 0, 0, 0, -1);
5978
5979 /* Display as many lines as needed to display all toolbar items. */
5980 while (it.current_y < it.last_visible_y)
5981 display_toolbar_line (&it);
5982
5983 /* It doesn't make much sense to try scrolling in the toolbar
5984 window, so don't do it. */
5985 w->desired_matrix->no_scrolling_p = 1;
5986 w->must_be_updated_p = 1;
5987
5988 if (auto_resize_toolbars_p)
5989 {
5990 int nlines;
5991
5992 /* If there are blank lines at the end, except for a partially
5993 visible blank line at the end that is smaller than
5994 CANON_Y_UNIT, change the toolbar's height. */
5995 row = it.glyph_row - 1;
5996 if (!row->displays_text_p
5997 && row->height >= CANON_Y_UNIT (f))
5998 change_height_p = 1;
5999
6000 /* If row displays toolbar items, but is partially visible,
6001 change the toolbar's height. */
6002 if (row->displays_text_p
6003 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
6004 change_height_p = 1;
6005
6006 /* Resize windows as needed by changing the `toolbar-lines'
6007 frame parameter. */
6008 if (change_height_p
6009 && (nlines = toolbar_lines_needed (f),
6010 nlines != XFASTINT (w->height)))
6011 {
6012 extern Lisp_Object Qtoolbar_lines;
6013 Lisp_Object frame;
6014
6015 XSETFRAME (frame, f);
6016 clear_glyph_matrix (w->desired_matrix);
6017 Fmodify_frame_parameters (frame,
6018 Fcons (Fcons (Qtoolbar_lines,
6019 make_number (nlines)),
6020 Qnil));
6021 fonts_changed_p = 1;
6022 }
6023 }
6024
6025 return change_height_p;
6026 }
6027
6028
6029 /* Get information about the toolbar item which is displayed in GLYPH
6030 on frame F. Return in *PROP_IDX the index where toolbar item
6031 properties start in F->current_toolbar_items. Value is zero if
6032 GLYPH doesn't display a toolbar item. */
6033
6034 int
6035 toolbar_item_info (f, glyph, prop_idx)
6036 struct frame *f;
6037 struct glyph *glyph;
6038 int *prop_idx;
6039 {
6040 Lisp_Object prop;
6041 int success_p;
6042
6043 /* Get the text property `menu-item' at pos. The value of that
6044 property is the start index of this item's properties in
6045 F->current_toolbar_items. */
6046 prop = Fget_text_property (make_number (glyph->charpos),
6047 Qmenu_item, f->current_toolbar_string);
6048 if (INTEGERP (prop))
6049 {
6050 *prop_idx = XINT (prop);
6051 success_p = 1;
6052 }
6053 else
6054 success_p = 0;
6055
6056 return success_p;
6057 }
6058
6059 #endif /* HAVE_WINDOW_SYSTEM */
6060
6061
6062 \f
6063 /************************************************************************
6064 Horizontal scrolling
6065 ************************************************************************/
6066
6067 static int hscroll_window_tree P_ ((Lisp_Object));
6068 static int hscroll_windows P_ ((Lisp_Object));
6069
6070 /* For all leaf windows in the window tree rooted at WINDOW, set their
6071 hscroll value so that PT is (i) visible in the window, and (ii) so
6072 that it is not within a certain margin at the window's left and
6073 right border. Value is non-zero if any window's hscroll has been
6074 changed. */
6075
6076 static int
6077 hscroll_window_tree (window)
6078 Lisp_Object window;
6079 {
6080 int hscrolled_p = 0;
6081
6082 while (WINDOWP (window))
6083 {
6084 struct window *w = XWINDOW (window);
6085
6086 if (WINDOWP (w->hchild))
6087 hscrolled_p |= hscroll_window_tree (w->hchild);
6088 else if (WINDOWP (w->vchild))
6089 hscrolled_p |= hscroll_window_tree (w->vchild);
6090 else if (w->cursor.vpos >= 0)
6091 {
6092 int hscroll_margin, text_area_x, text_area_y;
6093 int text_area_width, text_area_height;
6094 struct glyph_row *cursor_row = MATRIX_ROW (w->current_matrix,
6095 w->cursor.vpos);
6096
6097 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
6098 &text_area_width, &text_area_height);
6099
6100 /* Scroll when cursor is inside this scroll margin. */
6101 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
6102
6103 if ((XFASTINT (w->hscroll)
6104 && w->cursor.x < hscroll_margin)
6105 || (cursor_row->truncated_on_right_p
6106 && (w->cursor.x > text_area_width - hscroll_margin)))
6107 {
6108 struct it it;
6109 int hscroll;
6110 struct buffer *saved_current_buffer;
6111 int pt;
6112
6113 /* Find point in a display of infinite width. */
6114 saved_current_buffer = current_buffer;
6115 current_buffer = XBUFFER (w->buffer);
6116
6117 if (w == XWINDOW (selected_window))
6118 pt = BUF_PT (current_buffer);
6119 else
6120 {
6121 pt = marker_position (w->pointm);
6122 pt = max (BEGV, pt);
6123 pt = min (ZV, pt);
6124 }
6125
6126 /* Move iterator to pt starting at cursor_row->start in
6127 a line with infinite width. */
6128 init_to_row_start (&it, w, cursor_row);
6129 it.last_visible_x = INFINITY;
6130 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
6131 current_buffer = saved_current_buffer;
6132
6133 /* Center cursor in window. */
6134 hscroll = (max (0, it.current_x - text_area_width / 2)
6135 / CANON_X_UNIT (it.f));
6136
6137 /* Don't call Fset_window_hscroll if value hasn't
6138 changed because it will prevent redisplay
6139 optimizations. */
6140 if (XFASTINT (w->hscroll) != hscroll)
6141 {
6142 Fset_window_hscroll (window, make_number (hscroll));
6143 hscrolled_p = 1;
6144 }
6145 }
6146 }
6147
6148 window = w->next;
6149 }
6150
6151 /* Value is non-zero if hscroll of any leaf window has been changed. */
6152 return hscrolled_p;
6153 }
6154
6155
6156 /* Set hscroll so that cursor is visible and not inside horizontal
6157 scroll margins for all windows in the tree rooted at WINDOW. See
6158 also hscroll_window_tree above. Value is non-zero if any window's
6159 hscroll has been changed. If it has, desired matrices on the frame
6160 of WINDOW are cleared. */
6161
6162 static int
6163 hscroll_windows (window)
6164 Lisp_Object window;
6165 {
6166 int hscrolled_p = hscroll_window_tree (window);
6167 if (hscrolled_p)
6168 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
6169 return hscrolled_p;
6170 }
6171
6172
6173 \f
6174 /************************************************************************
6175 Redisplay
6176 ************************************************************************/
6177
6178 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
6179 to a non-zero value. This is sometimes handy to have in a debugger
6180 session. */
6181
6182 #if GLYPH_DEBUG
6183
6184 /* Values of beg_unchanged and end_unchanged as of last call to
6185 try_window_id. */
6186
6187 int debug_beg_unchanged, debug_end_unchanged;
6188
6189 /* First and last unchanged row for try_window_id. */
6190
6191 int debug_first_unchanged_at_end_vpos;
6192 int debug_last_unchanged_at_beg_vpos;
6193
6194 /* Delta vpos and y. */
6195
6196 int debug_dvpos, debug_dy;
6197
6198 /* Delta in characters and bytes for try_window_id. */
6199
6200 int debug_delta, debug_delta_bytes;
6201
6202 /* Values of window_end_pos and window_end_vpos at the end of
6203 try_window_id. */
6204
6205 int debug_end_pos, debug_end_vpos;
6206
6207 /* Append a string to W->desired_matrix->method. FMT is a printf
6208 format string. A1...A9 are a supplement for a variable-length
6209 argument list. If trace_redisplay_p is non-zero also printf the
6210 resulting string to stderr. */
6211
6212 static void
6213 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
6214 struct window *w;
6215 char *fmt;
6216 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
6217 {
6218 char buffer[512];
6219 char *method = w->desired_matrix->method;
6220 int len = strlen (method);
6221 int size = sizeof w->desired_matrix->method;
6222 int remaining = size - len - 1;
6223
6224 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
6225 if (len && remaining)
6226 {
6227 method[len] = '|';
6228 --remaining, ++len;
6229 }
6230
6231 strncpy (method + len, buffer, remaining);
6232
6233 if (trace_redisplay_p)
6234 fprintf (stderr, "%p (%s): %s\n",
6235 w,
6236 ((BUFFERP (w->buffer)
6237 && STRINGP (XBUFFER (w->buffer)->name))
6238 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
6239 : "no buffer"),
6240 buffer);
6241 }
6242
6243 #endif /* GLYPH_DEBUG */
6244
6245
6246 /* This counter is used to clear the face cache every once in a while
6247 in redisplay_internal. It is incremented for each redisplay.
6248 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
6249 cleared. */
6250
6251 #define CLEAR_FACE_CACHE_COUNT 10000
6252 static int clear_face_cache_count;
6253
6254 /* Record the previous terminal frame we displayed. */
6255
6256 static struct frame *previous_terminal_frame;
6257
6258 /* Non-zero while redisplay_internal is in progress. */
6259
6260 int redisplaying_p;
6261
6262
6263 /* Value is non-zero if all changes in window W, which displays
6264 current_buffer, are in the text between START and END. START is a
6265 buffer position, END is given as a distance from Z. Used in
6266 redisplay_internal for display optimization. */
6267
6268 static INLINE int
6269 text_outside_line_unchanged_p (w, start, end)
6270 struct window *w;
6271 int start, end;
6272 {
6273 int unchanged_p = 1;
6274
6275 /* If text or overlays have changed, see where. */
6276 if (XFASTINT (w->last_modified) < MODIFF
6277 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
6278 {
6279 /* Gap in the line? */
6280 if (GPT < start || Z - GPT < end)
6281 unchanged_p = 0;
6282
6283 /* Changes start in front of the line, or end after it? */
6284 if (unchanged_p
6285 && (beg_unchanged < start - 1
6286 || end_unchanged < end))
6287 unchanged_p = 0;
6288
6289 /* If selective display, can't optimize if changes start at the
6290 beginning of the line. */
6291 if (unchanged_p
6292 && INTEGERP (current_buffer->selective_display)
6293 && XINT (current_buffer->selective_display) > 0
6294 && (beg_unchanged < start || GPT <= start))
6295 unchanged_p = 0;
6296 }
6297
6298 return unchanged_p;
6299 }
6300
6301
6302 /* Do a frame update, taking possible shortcuts into account. This is
6303 the main external entry point for redisplay.
6304
6305 If the last redisplay displayed an echo area message and that message
6306 is no longer requested, we clear the echo area or bring back the
6307 mini-buffer if that is in use. */
6308
6309 void
6310 redisplay ()
6311 {
6312 redisplay_internal (0);
6313 }
6314
6315
6316 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
6317 response to any user action; therefore, we should preserve the echo
6318 area. (Actually, our caller does that job.) Perhaps in the future
6319 avoid recentering windows if it is not necessary; currently that
6320 causes some problems. */
6321
6322 static void
6323 redisplay_internal (preserve_echo_area)
6324 int preserve_echo_area;
6325 {
6326 struct window *w = XWINDOW (selected_window);
6327 struct frame *f = XFRAME (w->frame);
6328 int pause;
6329 int must_finish = 0;
6330 struct text_pos tlbufpos, tlendpos;
6331 int number_of_visible_frames;
6332
6333 /* Non-zero means redisplay has to consider all windows on all
6334 frames. Zero means, only selected_window is considered. */
6335 int consider_all_windows_p;
6336
6337 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
6338
6339 /* No redisplay if running in batch mode or frame is not yet fully
6340 initialized, or redisplay is explicitly turned off by setting
6341 Vinhibit_redisplay. */
6342 if (noninteractive
6343 || !NILP (Vinhibit_redisplay)
6344 || !f->glyphs_initialized_p)
6345 return;
6346
6347 /* The flag redisplay_performed_directly_p is set by
6348 direct_output_for_insert when it already did the whole screen
6349 update necessary. */
6350 if (redisplay_performed_directly_p)
6351 {
6352 redisplay_performed_directly_p = 0;
6353 if (!hscroll_windows (selected_window))
6354 return;
6355 }
6356
6357 #ifdef USE_X_TOOLKIT
6358 if (popup_activated ())
6359 return;
6360 #endif
6361
6362 if (redisplaying_p)
6363 return;
6364 ++redisplaying_p;
6365
6366 retry:
6367
6368 /* If new fonts have been loaded that make a glyph matrix adjustment
6369 necessary, do it. */
6370 if (fonts_changed_p)
6371 {
6372 adjust_glyphs (NULL);
6373 ++windows_or_buffers_changed;
6374 fonts_changed_p = 0;
6375 }
6376
6377 if (! FRAME_WINDOW_P (selected_frame)
6378 && previous_terminal_frame != selected_frame)
6379 {
6380 /* Since frames on an ASCII terminal share the same display
6381 area, displaying a different frame means redisplay the whole
6382 thing. */
6383 windows_or_buffers_changed++;
6384 SET_FRAME_GARBAGED (selected_frame);
6385 XSETFRAME (Vterminal_frame, selected_frame);
6386 }
6387 previous_terminal_frame = selected_frame;
6388
6389 /* Set the visible flags for all frames. Do this before checking
6390 for resized or garbaged frames; they want to know if their frames
6391 are visible. See the comment in frame.h for
6392 FRAME_SAMPLE_VISIBILITY. */
6393 {
6394 Lisp_Object tail, frame;
6395
6396 number_of_visible_frames = 0;
6397
6398 FOR_EACH_FRAME (tail, frame)
6399 {
6400 struct frame *f = XFRAME (frame);
6401
6402 FRAME_SAMPLE_VISIBILITY (f);
6403 if (FRAME_VISIBLE_P (f))
6404 ++number_of_visible_frames;
6405 clear_desired_matrices (f);
6406 }
6407 }
6408
6409 /* Notice any pending interrupt request to change frame size. */
6410 do_pending_window_change ();
6411
6412 /* Clear frames marked as garbaged. */
6413 if (frame_garbaged)
6414 {
6415 /* Old redisplay called redraw_garbaged_frames here which in
6416 turn called redraw_frame which in turn called clear_frame.
6417 The call to clear_frame is a source of flickering. After
6418 checking the places where SET_FRAME_GARBAGED is called, I
6419 believe a clear_frame is not necessary. It should suffice in
6420 the new redisplay to invalidate all current matrices, and
6421 ensure a complete redisplay of all windows. */
6422 Lisp_Object tail, frame;
6423
6424 FOR_EACH_FRAME (tail, frame)
6425 {
6426 struct frame *f = XFRAME (frame);
6427
6428 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6429 {
6430 clear_current_matrices (f);
6431 f->garbaged = 0;
6432 }
6433 }
6434
6435 frame_garbaged = 0;
6436 ++windows_or_buffers_changed;
6437 }
6438
6439 /* Build menubar and toolbar items. */
6440 prepare_menu_bars ();
6441
6442 if (windows_or_buffers_changed)
6443 update_mode_lines++;
6444
6445 /* Detect case that we need to write or remove a star in the mode line. */
6446 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
6447 {
6448 w->update_mode_line = Qt;
6449 if (buffer_shared > 1)
6450 update_mode_lines++;
6451 }
6452
6453 /* If %c is in the mode line, update it if needed. */
6454 if (!NILP (w->column_number_displayed)
6455 /* This alternative quickly identifies a common case
6456 where no change is needed. */
6457 && !(PT == XFASTINT (w->last_point)
6458 && XFASTINT (w->last_modified) >= MODIFF
6459 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
6460 && XFASTINT (w->column_number_displayed) != current_column ())
6461 w->update_mode_line = Qt;
6462
6463 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
6464
6465 /* The variable buffer_shared is set in redisplay_window and
6466 indicates that we redisplay a buffer in different windows. See
6467 there. */
6468 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
6469
6470 /* If specs for an arrow have changed, do thorough redisplay
6471 to ensure we remove any arrow that should no longer exist. */
6472 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
6473 || ! EQ (Voverlay_arrow_string, last_arrow_string))
6474 consider_all_windows_p = windows_or_buffers_changed = 1;
6475
6476 /* Normally the message* functions will have already displayed and
6477 updated the echo area, but the frame may have been trashed, or
6478 the update may have been preempted, so display the echo area
6479 again here. */
6480 if (echo_area_glyphs
6481 || STRINGP (echo_area_message)
6482 || previous_echo_glyphs
6483 || STRINGP (previous_echo_area_message))
6484 {
6485 echo_area_display (0);
6486 must_finish = 1;
6487 }
6488
6489 /* If showing the region, and mark has changed, we must redisplay
6490 the whole window. The assignment to this_line_start_pos prevents
6491 the optimization directly below this if-statement. */
6492 if (((!NILP (Vtransient_mark_mode)
6493 && !NILP (XBUFFER (w->buffer)->mark_active))
6494 != !NILP (w->region_showing))
6495 || (!NILP (w->region_showing)
6496 && !EQ (w->region_showing,
6497 Fmarker_position (XBUFFER (w->buffer)->mark))))
6498 CHARPOS (this_line_start_pos) = 0;
6499
6500 /* Optimize the case that only the line containing the cursor in the
6501 selected window has changed. Variables starting with this_ are
6502 set in display_line and record information about the line
6503 containing the cursor. */
6504 tlbufpos = this_line_start_pos;
6505 tlendpos = this_line_end_pos;
6506 if (!consider_all_windows_p
6507 && CHARPOS (tlbufpos) > 0
6508 && NILP (w->update_mode_line)
6509 && !current_buffer->clip_changed
6510 && FRAME_VISIBLE_P (XFRAME (w->frame))
6511 && !FRAME_OBSCURED_P (XFRAME (w->frame))
6512 /* Make sure recorded data applies to current buffer, etc. */
6513 && this_line_buffer == current_buffer
6514 && current_buffer == XBUFFER (w->buffer)
6515 && NILP (w->force_start)
6516 /* Point must be on the line that we have info recorded about. */
6517 && PT >= CHARPOS (tlbufpos)
6518 && PT <= Z - CHARPOS (tlendpos)
6519 /* All text outside that line, including its final newline,
6520 must be unchanged */
6521 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
6522 CHARPOS (tlendpos)))
6523 {
6524 if (CHARPOS (tlbufpos) > BEGV
6525 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
6526 && (CHARPOS (tlbufpos) == ZV
6527 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
6528 /* Former continuation line has disappeared by becoming empty */
6529 goto cancel;
6530 else if (XFASTINT (w->last_modified) < MODIFF
6531 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
6532 || MINI_WINDOW_P (w))
6533 {
6534 /* We have to handle the case of continuation around a
6535 wide-column character (See the comment in indent.c around
6536 line 885).
6537
6538 For instance, in the following case:
6539
6540 -------- Insert --------
6541 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
6542 J_I_ ==> J_I_ `^^' are cursors.
6543 ^^ ^^
6544 -------- --------
6545
6546 As we have to redraw the line above, we should goto cancel. */
6547
6548 struct it it;
6549 int line_height_before = this_line_pixel_height;
6550
6551 /* Note that start_display will handle the case that the
6552 line starting at tlbufpos is a continuation lines. */
6553 start_display (&it, w, tlbufpos);
6554
6555 /* Implementation note: It this still necessary? */
6556 if (it.current_x != this_line_start_x)
6557 goto cancel;
6558
6559 TRACE ((stderr, "trying display optimization 1\n"));
6560 w->cursor.vpos = -1;
6561 overlay_arrow_seen = 0;
6562 it.vpos = this_line_vpos;
6563 it.current_y = this_line_y;
6564 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
6565 display_line (&it);
6566
6567 /* If line contains point, is not continued,
6568 and ends at same distance from eob as before, we win */
6569 if (w->cursor.vpos >= 0
6570 /* Line is not continued, otherwise this_line_start_pos
6571 would have been set to 0 in display_line. */
6572 && CHARPOS (this_line_start_pos)
6573 /* Line ends as before. */
6574 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
6575 /* Line has same height as before. Otherwise other lines
6576 would have to be shifted up or down. */
6577 && this_line_pixel_height == line_height_before)
6578 {
6579 /* If this is not the window's last line, we must adjust
6580 the charstarts of the lines below. */
6581 if (it.current_y < it.last_visible_y)
6582 {
6583 struct glyph_row *row
6584 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
6585 int delta, delta_bytes;
6586
6587 if (Z - CHARPOS (tlendpos) == ZV)
6588 {
6589 /* This line ends at end of (accessible part of)
6590 buffer. There is no newline to count. */
6591 delta = (Z
6592 - CHARPOS (tlendpos)
6593 - MATRIX_ROW_START_CHARPOS (row));
6594 delta_bytes = (Z_BYTE
6595 - BYTEPOS (tlendpos)
6596 - MATRIX_ROW_START_BYTEPOS (row));
6597 }
6598 else
6599 {
6600 /* This line ends in a newline. Must take
6601 account of the newline and the rest of the
6602 text that follows. */
6603 delta = (Z
6604 - CHARPOS (tlendpos)
6605 - MATRIX_ROW_START_CHARPOS (row));
6606 delta_bytes = (Z_BYTE
6607 - BYTEPOS (tlendpos)
6608 - MATRIX_ROW_START_BYTEPOS (row));
6609 }
6610
6611 increment_glyph_matrix_buffer_positions (w->current_matrix,
6612 this_line_vpos + 1,
6613 w->current_matrix->nrows,
6614 delta, delta_bytes);
6615 }
6616
6617 /* If this row displays text now but previously didn't,
6618 or vice versa, w->window_end_vpos may have to be
6619 adjusted. */
6620 if ((it.glyph_row - 1)->displays_text_p)
6621 {
6622 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
6623 XSETINT (w->window_end_vpos, this_line_vpos);
6624 }
6625 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
6626 && this_line_vpos > 0)
6627 XSETINT (w->window_end_vpos, this_line_vpos - 1);
6628 w->window_end_valid = Qnil;
6629
6630 /* Update hint: No need to try to scroll in update_window. */
6631 w->desired_matrix->no_scrolling_p = 1;
6632
6633 #if GLYPH_DEBUG
6634 *w->desired_matrix->method = 0;
6635 debug_method_add (w, "optimization 1");
6636 #endif
6637 goto update;
6638 }
6639 else
6640 goto cancel;
6641 }
6642 else if (/* Cursor position hasn't changed. */
6643 PT == XFASTINT (w->last_point)
6644 /* Make sure the cursor was last displayed
6645 in this window. Otherwise we have to reposition it. */
6646 && 0 <= w->cursor.vpos
6647 && XINT (w->height) > w->cursor.vpos)
6648 {
6649 if (!must_finish)
6650 {
6651 do_pending_window_change ();
6652
6653 /* We used to always goto end_of_redisplay here, but this
6654 isn't enough if we have a blinking cursor. */
6655 if (w->cursor_off_p == w->last_cursor_off_p)
6656 goto end_of_redisplay;
6657 }
6658 goto update;
6659 }
6660 /* If highlighting the region, or if the cursor is in the echo area,
6661 then we can't just move the cursor. */
6662 else if (! (!NILP (Vtransient_mark_mode)
6663 && !NILP (current_buffer->mark_active))
6664 && (w == XWINDOW (current_buffer->last_selected_window)
6665 || highlight_nonselected_windows)
6666 && NILP (w->region_showing)
6667 && !cursor_in_echo_area)
6668 {
6669 struct it it;
6670 struct glyph_row *row;
6671
6672 /* Skip from tlbufpos to PT and see where it is. Note that
6673 PT may be in invisible text. If so, we will end at the
6674 next visible position. */
6675 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
6676 NULL, DEFAULT_FACE_ID);
6677 it.current_x = this_line_start_x;
6678 it.current_y = this_line_y;
6679 it.vpos = this_line_vpos;
6680
6681 /* The call to move_it_to stops in front of PT, but
6682 moves over before-strings. */
6683 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
6684
6685 if (it.vpos == this_line_vpos
6686 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
6687 row->enabled_p))
6688 {
6689 xassert (this_line_vpos == it.vpos);
6690 xassert (this_line_y == it.current_y);
6691 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
6692 goto update;
6693 }
6694 else
6695 goto cancel;
6696 }
6697
6698 cancel:
6699 /* Text changed drastically or point moved off of line. */
6700 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
6701 }
6702
6703 CHARPOS (this_line_start_pos) = 0;
6704 consider_all_windows_p |= buffer_shared > 1;
6705 ++clear_face_cache_count;
6706
6707
6708 /* Build desired matrices. If consider_all_windows_p is non-zero,
6709 do it for all windows on all frames. Otherwise do it for
6710 selected_window, only. */
6711
6712 if (consider_all_windows_p)
6713 {
6714 Lisp_Object tail, frame;
6715
6716 /* Clear the face cache eventually. */
6717 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
6718 {
6719 clear_face_cache (0);
6720 clear_face_cache_count = 0;
6721 }
6722
6723 /* Recompute # windows showing selected buffer. This will be
6724 incremented each time such a window is displayed. */
6725 buffer_shared = 0;
6726
6727 FOR_EACH_FRAME (tail, frame)
6728 {
6729 struct frame *f = XFRAME (frame);
6730 if (FRAME_WINDOW_P (f) || f == selected_frame)
6731 {
6732 /* Mark all the scroll bars to be removed; we'll redeem
6733 the ones we want when we redisplay their windows. */
6734 if (condemn_scroll_bars_hook)
6735 (*condemn_scroll_bars_hook) (f);
6736
6737 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
6738 redisplay_windows (FRAME_ROOT_WINDOW (f));
6739
6740 /* Any scroll bars which redisplay_windows should have
6741 nuked should now go away. */
6742 if (judge_scroll_bars_hook)
6743 (*judge_scroll_bars_hook) (f);
6744 }
6745 }
6746 }
6747 else if (FRAME_VISIBLE_P (selected_frame)
6748 && !FRAME_OBSCURED_P (selected_frame))
6749 redisplay_window (selected_window, 1);
6750
6751
6752 /* Compare desired and current matrices, perform output. */
6753
6754 update:
6755
6756 /* If fonts changed, display again. */
6757 if (fonts_changed_p)
6758 goto retry;
6759
6760 /* Prevent various kinds of signals during display update.
6761 stdio is not robust about handling signals,
6762 which can cause an apparent I/O error. */
6763 if (interrupt_input)
6764 unrequest_sigio ();
6765 stop_polling ();
6766
6767 if (consider_all_windows_p)
6768 {
6769 Lisp_Object tail;
6770
6771 pause = 0;
6772
6773 for (tail = Vframe_list; CONSP (tail); tail = XCONS (tail)->cdr)
6774 {
6775 struct frame *f;
6776
6777 if (!FRAMEP (XCONS (tail)->car))
6778 continue;
6779
6780 f = XFRAME (XCONS (tail)->car);
6781
6782 if ((FRAME_WINDOW_P (f) || f == selected_frame)
6783 && FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
6784 {
6785 /* Mark all windows as to be updated. */
6786 set_window_update_flags (XWINDOW (f->root_window), 1);
6787 pause |= update_frame (f, 0, 0);
6788 if (!pause)
6789 {
6790 if (hscroll_windows (f->root_window))
6791 goto retry;
6792
6793 mark_window_display_accurate (f->root_window, 1);
6794 if (frame_up_to_date_hook != 0)
6795 (*frame_up_to_date_hook) (f);
6796 }
6797 }
6798 }
6799 }
6800 else
6801 {
6802 if (FRAME_VISIBLE_P (selected_frame)
6803 && !FRAME_OBSCURED_P (selected_frame))
6804 {
6805 XWINDOW (selected_window)->must_be_updated_p = 1;
6806 pause = update_frame (selected_frame, 0, 0);
6807 if (!pause && hscroll_windows (selected_window))
6808 goto retry;
6809 }
6810 else
6811 pause = 0;
6812
6813 /* We may have called echo_area_display at the top of this
6814 function. If the echo area is on another frame, that may
6815 have put text on a frame other than the selected one, so the
6816 above call to update_frame would not have caught it. Catch
6817 it here. */
6818 {
6819 Lisp_Object mini_window;
6820 struct frame *mini_frame;
6821
6822 mini_window = FRAME_MINIBUF_WINDOW (selected_frame);
6823 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6824
6825 if (mini_frame != selected_frame && FRAME_WINDOW_P (mini_frame))
6826 {
6827 XWINDOW (mini_window)->must_be_updated_p = 1;
6828 pause |= update_frame (mini_frame, 0, 0);
6829 if (!pause && hscroll_windows (mini_window))
6830 goto retry;
6831 }
6832 }
6833 }
6834
6835 /* If display was paused because of pending input, make sure we do a
6836 thorough update the next time. */
6837 if (pause)
6838 {
6839 /* Prevent the optimization at the beginning of
6840 redisplay_internal that tries a single-line update of the
6841 line containing the cursor in the selected window. */
6842 CHARPOS (this_line_start_pos) = 0;
6843
6844 /* Let the overlay arrow be updated the next time. */
6845 if (!NILP (last_arrow_position))
6846 {
6847 last_arrow_position = Qt;
6848 last_arrow_string = Qt;
6849 }
6850
6851 /* If we pause after scrolling, some rows in the current
6852 matrices of some windows are not valid. */
6853 if (!WINDOW_FULL_WIDTH_P (w)
6854 && !FRAME_WINDOW_P (XFRAME (w->frame)))
6855 update_mode_lines = 1;
6856 }
6857
6858 /* Now text on frame agrees with windows, so put info into the
6859 windows for partial redisplay to follow. */
6860 if (!pause)
6861 {
6862 register struct buffer *b = XBUFFER (w->buffer);
6863
6864 unchanged_modified = BUF_MODIFF (b);
6865 overlay_unchanged_modified = BUF_OVERLAY_MODIFF (b);
6866 beg_unchanged = BUF_GPT (b) - BUF_BEG (b);
6867 end_unchanged = BUF_Z (b) - BUF_GPT (b);
6868
6869 if (consider_all_windows_p)
6870 mark_window_display_accurate (FRAME_ROOT_WINDOW (selected_frame), 1);
6871 else
6872 {
6873 XSETFASTINT (w->last_point, BUF_PT (b));
6874 w->last_cursor = w->cursor;
6875 w->last_cursor_off_p = w->cursor_off_p;
6876
6877 b->clip_changed = 0;
6878 w->update_mode_line = Qnil;
6879 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
6880 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
6881 w->last_had_star
6882 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6883 ? Qt : Qnil);
6884
6885 /* Record if we are showing a region, so can make sure to
6886 update it fully at next redisplay. */
6887 w->region_showing = (!NILP (Vtransient_mark_mode)
6888 && (w == XWINDOW (current_buffer->last_selected_window)
6889 || highlight_nonselected_windows)
6890 && !NILP (XBUFFER (w->buffer)->mark_active)
6891 ? Fmarker_position (XBUFFER (w->buffer)->mark)
6892 : Qnil);
6893
6894 w->window_end_valid = w->buffer;
6895 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
6896 last_arrow_string = Voverlay_arrow_string;
6897 if (frame_up_to_date_hook != 0)
6898 (*frame_up_to_date_hook) (selected_frame);
6899 }
6900
6901 update_mode_lines = 0;
6902 windows_or_buffers_changed = 0;
6903 }
6904
6905 /* Start SIGIO interrupts coming again. Having them off during the
6906 code above makes it less likely one will discard output, but not
6907 impossible, since there might be stuff in the system buffer here.
6908 But it is much hairier to try to do anything about that. */
6909 if (interrupt_input)
6910 request_sigio ();
6911 start_polling ();
6912
6913 /* If a frame has become visible which was not before, redisplay
6914 again, so that we display it. Expose events for such a frame
6915 (which it gets when becoming visible) don't call the parts of
6916 redisplay constructing glyphs, so simply exposing a frame won't
6917 display anything in this case. So, we have to display these
6918 frames here explicitly. */
6919 if (!pause)
6920 {
6921 Lisp_Object tail, frame;
6922 int new_count = 0;
6923
6924 FOR_EACH_FRAME (tail, frame)
6925 {
6926 int this_is_visible = 0;
6927
6928 if (XFRAME (frame)->visible)
6929 this_is_visible = 1;
6930 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
6931 if (XFRAME (frame)->visible)
6932 this_is_visible = 1;
6933
6934 if (this_is_visible)
6935 new_count++;
6936 }
6937
6938 if (new_count != number_of_visible_frames)
6939 windows_or_buffers_changed++;
6940 }
6941
6942 /* Change frame size now if a change is pending. */
6943 do_pending_window_change ();
6944
6945 /* If we just did a pending size change, or have additional
6946 visible frames, redisplay again. */
6947 if (windows_or_buffers_changed && !pause)
6948 goto retry;
6949
6950 end_of_redisplay:;
6951
6952 if (--redisplaying_p < 0)
6953 redisplaying_p = 0;
6954 }
6955
6956
6957 /* Redisplay, but leave alone any recent echo area message unless
6958 another message has been requested in its place.
6959
6960 This is useful in situations where you need to redisplay but no
6961 user action has occurred, making it inappropriate for the message
6962 area to be cleared. See tracking_off and
6963 wait_reading_process_input for examples of these situations. */
6964
6965 void
6966 redisplay_preserve_echo_area ()
6967 {
6968 if (!echo_area_glyphs
6969 && !STRINGP (echo_area_message)
6970 && (previous_echo_glyphs
6971 || STRINGP (previous_echo_area_message)))
6972 {
6973 echo_area_glyphs = previous_echo_glyphs;
6974 echo_area_message = previous_echo_area_message;
6975 echo_area_glyphs_length = previous_echo_glyphs_length;
6976 redisplay_internal (1);
6977 echo_area_glyphs = NULL;
6978 echo_area_message = Qnil;
6979 }
6980 else
6981 redisplay_internal (1);
6982 }
6983
6984
6985 /* Mark the display of windows in the window tree rooted at WINDOW as
6986 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
6987 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
6988 the next time redisplay_internal is called. */
6989
6990 void
6991 mark_window_display_accurate (window, accurate_p)
6992 Lisp_Object window;
6993 int accurate_p;
6994 {
6995 struct window *w;
6996
6997 for (; !NILP (window); window = w->next)
6998 {
6999 w = XWINDOW (window);
7000
7001 if (BUFFERP (w->buffer))
7002 {
7003 struct buffer *b = XBUFFER (w->buffer);
7004
7005 XSETFASTINT (w->last_modified,
7006 accurate_p ? BUF_MODIFF (b) : 0);
7007 XSETFASTINT (w->last_overlay_modified,
7008 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
7009 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
7010 ? Qt : Qnil);
7011
7012 #if 0 /* I don't think this is necessary because display_line does it.
7013 Let's check it. */
7014 /* Record if we are showing a region, so can make sure to
7015 update it fully at next redisplay. */
7016 w->region_showing
7017 = (!NILP (Vtransient_mark_mode)
7018 && (w == XWINDOW (current_buffer->last_selected_window)
7019 || highlight_nonselected_windows)
7020 && (!NILP (b->mark_active)
7021 ? Fmarker_position (b->mark)
7022 : Qnil));
7023 #endif
7024
7025 if (accurate_p)
7026 {
7027 b->clip_changed = 0;
7028 w->last_cursor = w->cursor;
7029 w->last_cursor_off_p = w->cursor_off_p;
7030 if (w == XWINDOW (selected_window))
7031 w->last_point = BUF_PT (b);
7032 else
7033 w->last_point = XMARKER (w->pointm)->charpos;
7034 }
7035 }
7036
7037 w->window_end_valid = w->buffer;
7038 w->update_mode_line = Qnil;
7039
7040 if (!NILP (w->vchild))
7041 mark_window_display_accurate (w->vchild, accurate_p);
7042 if (!NILP (w->hchild))
7043 mark_window_display_accurate (w->hchild, accurate_p);
7044 }
7045
7046 if (accurate_p)
7047 {
7048 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
7049 last_arrow_string = Voverlay_arrow_string;
7050 }
7051 else
7052 {
7053 /* Force a thorough redisplay the next time by setting
7054 last_arrow_position and last_arrow_string to t, which is
7055 unequal to any useful value of Voverlay_arrow_... */
7056 last_arrow_position = Qt;
7057 last_arrow_string = Qt;
7058 }
7059 }
7060
7061
7062 /* Return value in display table DP (Lisp_Char_Table *) for character
7063 C. Since a display table doesn't have any parent, we don't have to
7064 follow parent. Do not call this function directly but use the
7065 macro DISP_CHAR_VECTOR. */
7066
7067 Lisp_Object
7068 disp_char_vector (dp, c)
7069 struct Lisp_Char_Table *dp;
7070 int c;
7071 {
7072 int code[4], i;
7073 Lisp_Object val;
7074
7075 if (SINGLE_BYTE_CHAR_P (c))
7076 return (dp->contents[c]);
7077
7078 SPLIT_NON_ASCII_CHAR (c, code[0], code[1], code[2]);
7079 if (code[0] != CHARSET_COMPOSITION)
7080 {
7081 if (code[1] < 32)
7082 code[1] = -1;
7083 else if (code[2] < 32)
7084 code[2] = -1;
7085 }
7086
7087 /* Here, the possible range of code[0] (== charset ID) is
7088 128..max_charset. Since the top level char table contains data
7089 for multibyte characters after 256th element, we must increment
7090 code[0] by 128 to get a correct index. */
7091 code[0] += 128;
7092 code[3] = -1; /* anchor */
7093
7094 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
7095 {
7096 val = dp->contents[code[i]];
7097 if (!SUB_CHAR_TABLE_P (val))
7098 return (NILP (val) ? dp->defalt : val);
7099 }
7100
7101 /* Here, val is a sub char table. We return the default value of
7102 it. */
7103 return (dp->defalt);
7104 }
7105
7106
7107 \f
7108 /***********************************************************************
7109 Window Redisplay
7110 ***********************************************************************/
7111
7112 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
7113
7114 static void
7115 redisplay_windows (window)
7116 Lisp_Object window;
7117 {
7118 while (!NILP (window))
7119 {
7120 struct window *w = XWINDOW (window);
7121
7122 if (!NILP (w->hchild))
7123 redisplay_windows (w->hchild);
7124 else if (!NILP (w->vchild))
7125 redisplay_windows (w->vchild);
7126 else
7127 redisplay_window (window, 0);
7128
7129 window = w->next;
7130 }
7131 }
7132
7133
7134 /* Set cursor position of W. PT is assumed to be displayed in ROW.
7135 DELTA is the number of bytes by which positions recorded in ROW
7136 differ from current buffer positions. */
7137
7138 void
7139 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
7140 struct window *w;
7141 struct glyph_row *row;
7142 struct glyph_matrix *matrix;
7143 int delta, delta_bytes, dy, dvpos;
7144 {
7145 struct glyph *glyph = row->glyphs[TEXT_AREA];
7146 struct glyph *end = glyph + row->used[TEXT_AREA];
7147 int x = row->x;
7148 int pt_old = PT - delta;
7149
7150 /* Skip over glyphs not having an object at the start of the row.
7151 These are special glyphs like truncation marks on terminal
7152 frames. */
7153 if (row->displays_text_p)
7154 while (glyph < end
7155 && !glyph->object
7156 && glyph->charpos < 0)
7157 {
7158 x += glyph->pixel_width;
7159 ++glyph;
7160 }
7161
7162 while (glyph < end
7163 && glyph->object
7164 && (!BUFFERP (glyph->object)
7165 || glyph->charpos < pt_old))
7166 {
7167 x += glyph->pixel_width;
7168 ++glyph;
7169 }
7170
7171 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
7172 w->cursor.x = x;
7173 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
7174 w->cursor.y = row->y + dy;
7175
7176 if (w == XWINDOW (selected_window))
7177 {
7178 if (!row->continued_p
7179 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
7180 && row->x == 0)
7181 {
7182 this_line_buffer = XBUFFER (w->buffer);
7183
7184 CHARPOS (this_line_start_pos)
7185 = MATRIX_ROW_START_CHARPOS (row) + delta;
7186 BYTEPOS (this_line_start_pos)
7187 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
7188
7189 CHARPOS (this_line_end_pos)
7190 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
7191 BYTEPOS (this_line_end_pos)
7192 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
7193
7194 this_line_y = w->cursor.y;
7195 this_line_pixel_height = row->height;
7196 this_line_vpos = w->cursor.vpos;
7197 this_line_start_x = row->x;
7198 }
7199 else
7200 CHARPOS (this_line_start_pos) = 0;
7201 }
7202 }
7203
7204
7205 /* Run window scroll functions, if any, for WINDOW with new window
7206 start STARTP. Sets the window start of WINDOW to that position. */
7207
7208 static INLINE struct text_pos
7209 run_window_scroll_functions (window, startp)
7210 Lisp_Object window;
7211 struct text_pos startp;
7212 {
7213 struct window *w = XWINDOW (window);
7214 SET_MARKER_FROM_TEXT_POS (w->start, startp);
7215
7216 if (!NILP (Vwindow_scroll_functions))
7217 {
7218 run_hook_with_args_2 (Qwindow_scroll_functions, window,
7219 make_number (CHARPOS (startp)));
7220 SET_TEXT_POS_FROM_MARKER (startp, w->start);
7221 }
7222
7223 return startp;
7224 }
7225
7226
7227 /* Modify the desired matrix of window W and W->vscroll so that the
7228 line containing the cursor is fully visible. */
7229
7230 static void
7231 make_cursor_line_fully_visible (w)
7232 struct window *w;
7233 {
7234 struct glyph_matrix *matrix;
7235 struct glyph_row *row;
7236 int top_line_height;
7237
7238 /* It's not always possible to find the cursor, e.g, when a window
7239 is full of overlay strings. Don't do anything in that case. */
7240 if (w->cursor.vpos < 0)
7241 return;
7242
7243 matrix = w->desired_matrix;
7244 row = MATRIX_ROW (matrix, w->cursor.vpos);
7245
7246 /* If row->y == top y of window display area, the window isn't tall
7247 enough to display a single line. There is nothing we can do
7248 about it. */
7249 top_line_height = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
7250 if (row->y == top_line_height)
7251 return;
7252
7253 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
7254 {
7255 int dy = row->height - row->visible_height;
7256 w->vscroll = 0;
7257 w->cursor.y += dy;
7258 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
7259 }
7260 else if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row))
7261 {
7262 int dy = - (row->height - row->visible_height);
7263 w->vscroll = dy;
7264 w->cursor.y += dy;
7265 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
7266 }
7267
7268 /* When we change the cursor y-position of the selected window,
7269 change this_line_y as well so that the display optimization for
7270 the cursor line of the selected window in redisplay_internal uses
7271 the correct y-position. */
7272 if (w == XWINDOW (selected_window))
7273 this_line_y = w->cursor.y;
7274 }
7275
7276
7277 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
7278 non-zero means only WINDOW is redisplayed in redisplay_internal.
7279 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
7280 in redisplay_window to bring a partially visible line into view in
7281 the case that only the cursor has moved.
7282
7283 Value is
7284
7285 1 if scrolling succeeded
7286
7287 0 if scrolling didn't find point.
7288
7289 -1 if new fonts have been loaded so that we must interrupt
7290 redisplay, adjust glyph matrices, and try again. */
7291
7292 static int
7293 try_scrolling (window, just_this_one_p, scroll_conservatively,
7294 scroll_step, temp_scroll_step)
7295 Lisp_Object window;
7296 int just_this_one_p;
7297 int scroll_conservatively, scroll_step;
7298 int temp_scroll_step;
7299 {
7300 struct window *w = XWINDOW (window);
7301 struct frame *f = XFRAME (w->frame);
7302 struct text_pos scroll_margin_pos;
7303 struct text_pos pos;
7304 struct text_pos startp;
7305 struct it it;
7306 Lisp_Object window_end;
7307 int this_scroll_margin;
7308 int dy = 0;
7309 int scroll_max;
7310 int line_height, rc;
7311 int amount_to_scroll = 0;
7312 Lisp_Object aggressive;
7313 int height;
7314
7315 #if GLYPH_DEBUG
7316 debug_method_add (w, "try_scrolling");
7317 #endif
7318
7319 SET_TEXT_POS_FROM_MARKER (startp, w->start);
7320
7321 /* Compute scroll margin height in pixels. We scroll when point is
7322 within this distance from the top or bottom of the window. */
7323 if (scroll_margin > 0)
7324 {
7325 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
7326 this_scroll_margin *= CANON_Y_UNIT (f);
7327 }
7328 else
7329 this_scroll_margin = 0;
7330
7331 /* Compute how much we should try to scroll maximally to bring point
7332 into view. */
7333 if (scroll_step)
7334 scroll_max = scroll_step;
7335 else if (scroll_conservatively)
7336 scroll_max = scroll_conservatively;
7337 else if (temp_scroll_step)
7338 scroll_max = temp_scroll_step;
7339 else if (NUMBERP (current_buffer->scroll_down_aggressively)
7340 || NUMBERP (current_buffer->scroll_up_aggressively))
7341 /* We're trying to scroll because of aggressive scrolling
7342 but no scroll_step is set. Choose an arbitrary one. Maybe
7343 there should be a variable for this. */
7344 scroll_max = 10;
7345 else
7346 scroll_max = 0;
7347 scroll_max *= CANON_Y_UNIT (f);
7348
7349 /* Decide whether we have to scroll down. Start at the window end
7350 and move this_scroll_margin up to find the position of the scroll
7351 margin. */
7352 window_end = Fwindow_end (window, Qt);
7353 CHARPOS (scroll_margin_pos) = XINT (window_end);
7354 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
7355 if (this_scroll_margin)
7356 {
7357 start_display (&it, w, scroll_margin_pos);
7358 move_it_vertically (&it, - this_scroll_margin);
7359 scroll_margin_pos = it.current.pos;
7360 }
7361
7362 if (PT >= CHARPOS (scroll_margin_pos))
7363 {
7364 int y0;
7365
7366 /* Point is in the scroll margin at the bottom of the window, or
7367 below. Compute a new window start that makes point visible. */
7368
7369 /* Compute the distance from the scroll margin to PT.
7370 Give up if the distance is greater than scroll_max. */
7371 start_display (&it, w, scroll_margin_pos);
7372 y0 = it.current_y;
7373 move_it_to (&it, PT, 0, it.last_visible_y, -1,
7374 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
7375 line_height = (it.max_ascent + it.max_descent
7376 ? it.max_ascent + it.max_descent
7377 : last_height);
7378 dy = it.current_y + line_height - y0;
7379 if (dy > scroll_max)
7380 return 0;
7381
7382 /* Move the window start down. If scrolling conservatively,
7383 move it just enough down to make point visible. If
7384 scroll_step is set, move it down by scroll_step. */
7385 start_display (&it, w, startp);
7386
7387 if (scroll_conservatively)
7388 amount_to_scroll = dy;
7389 else if (scroll_step || temp_scroll_step)
7390 amount_to_scroll = scroll_max;
7391 else
7392 {
7393 aggressive = current_buffer->scroll_down_aggressively;
7394 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
7395 - WINDOW_DISPLAY_TOP_LINE_HEIGHT (w));
7396 if (NUMBERP (aggressive))
7397 amount_to_scroll = XFLOATINT (aggressive) * height;
7398 }
7399
7400 if (amount_to_scroll <= 0)
7401 return 0;
7402
7403 move_it_vertically (&it, amount_to_scroll);
7404 startp = it.current.pos;
7405 }
7406 else
7407 {
7408 /* See if point is inside the scroll margin at the top of the
7409 window. */
7410 scroll_margin_pos = startp;
7411 if (this_scroll_margin)
7412 {
7413 start_display (&it, w, startp);
7414 move_it_vertically (&it, this_scroll_margin);
7415 scroll_margin_pos = it.current.pos;
7416 }
7417
7418 if (PT < CHARPOS (scroll_margin_pos))
7419 {
7420 /* Point is in the scroll margin at the top of the window or
7421 above what is displayed in the window. */
7422 int y0;
7423
7424 /* Compute the vertical distance from PT to the scroll
7425 margin position. Give up if distance is greater than
7426 scroll_max. */
7427 SET_TEXT_POS (pos, PT, PT_BYTE);
7428 start_display (&it, w, pos);
7429 y0 = it.current_y;
7430 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
7431 it.last_visible_y, -1,
7432 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
7433 dy = it.current_y - y0;
7434 if (dy > scroll_max)
7435 return 0;
7436
7437 /* Compute new window start. */
7438 start_display (&it, w, startp);
7439
7440 if (scroll_conservatively)
7441 amount_to_scroll = dy;
7442 else if (scroll_step || temp_scroll_step)
7443 amount_to_scroll = scroll_max;
7444 else
7445 {
7446 aggressive = current_buffer->scroll_up_aggressively;
7447 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
7448 - WINDOW_DISPLAY_TOP_LINE_HEIGHT (w));
7449 if (NUMBERP (aggressive))
7450 amount_to_scroll = XFLOATINT (aggressive) * height;
7451 }
7452
7453 if (amount_to_scroll <= 0)
7454 return 0;
7455
7456 move_it_vertically (&it, - amount_to_scroll);
7457 startp = it.current.pos;
7458 }
7459 }
7460
7461 /* Run window scroll functions. */
7462 startp = run_window_scroll_functions (window, startp);
7463
7464 /* Display the window. Give up if new fonts are loaded, or if point
7465 doesn't appear. */
7466 if (!try_window (window, startp))
7467 rc = -1;
7468 else if (w->cursor.vpos < 0)
7469 {
7470 clear_glyph_matrix (w->desired_matrix);
7471 rc = 0;
7472 }
7473 else
7474 {
7475 /* Maybe forget recorded base line for line number display. */
7476 if (!just_this_one_p
7477 || current_buffer->clip_changed
7478 || beg_unchanged < CHARPOS (startp))
7479 w->base_line_number = Qnil;
7480
7481 /* If cursor ends up on a partially visible line, shift display
7482 lines up or down. */
7483 make_cursor_line_fully_visible (w);
7484 rc = 1;
7485 }
7486
7487 return rc;
7488 }
7489
7490
7491 /* Compute a suitable window start for window W if display of W starts
7492 on a continuation line. Value is non-zero if a new window start
7493 was computed.
7494
7495 The new window start will be computed, based on W's width, starting
7496 from the start of the continued line. It is the start of the
7497 screen line with the minimum distance from the old start W->start. */
7498
7499 static int
7500 compute_window_start_on_continuation_line (w)
7501 struct window *w;
7502 {
7503 struct text_pos pos, start_pos;
7504 int window_start_changed_p = 0;
7505
7506 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
7507
7508 /* If window start is on a continuation line... Window start may be
7509 < BEGV in case there's invisible text at the start of the
7510 buffer (M-x rmail, for example). */
7511 if (CHARPOS (start_pos) > BEGV
7512 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
7513 {
7514 struct it it;
7515 struct glyph_row *row;
7516
7517 /* Find the start of the continued line. This should be fast
7518 because scan_buffer is fast (newline cache). */
7519 row = w->desired_matrix->rows + (WINDOW_WANTS_TOP_LINE_P (w) ? 1 : 0);
7520 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
7521 row, DEFAULT_FACE_ID);
7522 reseat_at_previous_visible_line_start (&it);
7523
7524 /* If the line start is "too far" away from the window start,
7525 say it takes too much time to compute a new window start. */
7526 if (CHARPOS (start_pos) - IT_CHARPOS (it)
7527 < XFASTINT (w->height) * XFASTINT (w->width))
7528 {
7529 int min_distance, distance;
7530
7531 /* Move forward by display lines to find the new window
7532 start. If window width was enlarged, the new start can
7533 be expected to be > the old start. If window width was
7534 decreased, the new window start will be < the old start.
7535 So, we're looking for the display line start with the
7536 minimum distance from the old window start. */
7537 pos = it.current.pos;
7538 min_distance = INFINITY;
7539 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
7540 distance < min_distance)
7541 {
7542 min_distance = distance;
7543 pos = it.current.pos;
7544 move_it_by_lines (&it, 1, 0);
7545 }
7546
7547 /* Set the window start there. */
7548 SET_MARKER_FROM_TEXT_POS (w->start, pos);
7549 window_start_changed_p = 1;
7550 }
7551 }
7552
7553 return window_start_changed_p;
7554 }
7555
7556
7557 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
7558 selected_window is redisplayed. */
7559
7560 static void
7561 redisplay_window (window, just_this_one_p)
7562 Lisp_Object window;
7563 int just_this_one_p;
7564 {
7565 struct window *w = XWINDOW (window);
7566 struct frame *f = XFRAME (w->frame);
7567 struct buffer *buffer = XBUFFER (w->buffer);
7568 struct buffer *old = current_buffer;
7569 struct text_pos lpoint, opoint, startp;
7570 int update_mode_line;
7571 int tem;
7572 struct it it;
7573 /* Record it now because it's overwritten. */
7574 int current_matrix_up_to_date_p = 0;
7575 int really_switched_buffer = 0;
7576 int temp_scroll_step = 0;
7577 int count = specpdl_ptr - specpdl;
7578
7579 SET_TEXT_POS (lpoint, PT, PT_BYTE);
7580 opoint = lpoint;
7581
7582 /* W must be a leaf window here. */
7583 xassert (!NILP (w->buffer));
7584 #if GLYPH_DEBUG
7585 *w->desired_matrix->method = 0;
7586 #endif
7587
7588 specbind (Qinhibit_point_motion_hooks, Qt);
7589
7590 /* Has the mode line to be updated? */
7591 update_mode_line = (!NILP (w->update_mode_line)
7592 || update_mode_lines
7593 || buffer->clip_changed);
7594
7595 if (MINI_WINDOW_P (w))
7596 {
7597 if (w == XWINDOW (echo_area_window)
7598 && (echo_area_glyphs
7599 || STRINGP (echo_area_message)))
7600 {
7601 if (update_mode_line)
7602 /* We may have to update a tty frame's menu bar or a
7603 toolbar. Example `M-x C-h C-h C-g'. */
7604 goto finish_menu_bars;
7605 else
7606 /* We've already displayed the echo area glyphs in this window. */
7607 goto finish_scroll_bars;
7608 }
7609 else if (w != XWINDOW (minibuf_window))
7610 {
7611 /* W is a mini-buffer window, but it's not the currently
7612 active one, so clear it. */
7613 int yb = window_text_bottom_y (w);
7614 struct glyph_row *row;
7615 int y;
7616
7617 for (y = 0, row = w->desired_matrix->rows;
7618 y < yb;
7619 y += row->height, ++row)
7620 blank_row (w, row, y);
7621 goto finish_scroll_bars;
7622 }
7623 }
7624
7625 /* Otherwise set up data on this window; select its buffer and point
7626 value. */
7627 if (update_mode_line)
7628 {
7629 /* Really select the buffer, for the sake of buffer-local
7630 variables. */
7631 set_buffer_internal_1 (XBUFFER (w->buffer));
7632 really_switched_buffer = 1;
7633 }
7634 else
7635 set_buffer_temp (XBUFFER (w->buffer));
7636 SET_TEXT_POS (opoint, PT, PT_BYTE);
7637
7638 current_matrix_up_to_date_p
7639 = (!NILP (w->window_end_valid)
7640 && !current_buffer->clip_changed
7641 && XFASTINT (w->last_modified) >= MODIFF
7642 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
7643
7644 /* When windows_or_buffers_changed is non-zero, we can't rely on
7645 the window end being valid, so set it to nil there. */
7646 if (windows_or_buffers_changed)
7647 {
7648 /* If window starts on a continuation line, maybe adjust the
7649 window start in case the window's width changed. */
7650 if (XMARKER (w->start)->buffer == current_buffer)
7651 compute_window_start_on_continuation_line (w);
7652
7653 w->window_end_valid = Qnil;
7654 }
7655
7656 /* Some sanity checks. */
7657 CHECK_WINDOW_END (w);
7658 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
7659 abort ();
7660 if (BYTEPOS (opoint) < CHARPOS (opoint))
7661 abort ();
7662
7663 /* If %c is in mode line, update it if needed. */
7664 if (!NILP (w->column_number_displayed)
7665 /* This alternative quickly identifies a common case
7666 where no change is needed. */
7667 && !(PT == XFASTINT (w->last_point)
7668 && XFASTINT (w->last_modified) >= MODIFF
7669 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
7670 && XFASTINT (w->column_number_displayed) != current_column ())
7671 update_mode_line = 1;
7672
7673 /* Count number of windows showing the selected buffer. An indirect
7674 buffer counts as its base buffer. */
7675 if (!just_this_one_p)
7676 {
7677 struct buffer *current_base, *window_base;
7678 current_base = current_buffer;
7679 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
7680 if (current_base->base_buffer)
7681 current_base = current_base->base_buffer;
7682 if (window_base->base_buffer)
7683 window_base = window_base->base_buffer;
7684 if (current_base == window_base)
7685 buffer_shared++;
7686 }
7687
7688 /* Point refers normally to the selected window. For any other
7689 window, set up appropriate value. */
7690 if (!EQ (window, selected_window))
7691 {
7692 int new_pt = XMARKER (w->pointm)->charpos;
7693 int new_pt_byte = marker_byte_position (w->pointm);
7694 if (new_pt < BEGV)
7695 {
7696 new_pt = BEGV;
7697 new_pt_byte = BEGV_BYTE;
7698 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
7699 }
7700 else if (new_pt > (ZV - 1))
7701 {
7702 new_pt = ZV;
7703 new_pt_byte = ZV_BYTE;
7704 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
7705 }
7706
7707 /* We don't use SET_PT so that the point-motion hooks don't run. */
7708 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
7709 }
7710
7711 /* If any of the character widths specified in the display table
7712 have changed, invalidate the width run cache. It's true that
7713 this may be a bit late to catch such changes, but the rest of
7714 redisplay goes (non-fatally) haywire when the display table is
7715 changed, so why should we worry about doing any better? */
7716 if (current_buffer->width_run_cache)
7717 {
7718 struct Lisp_Char_Table *disptab = buffer_display_table ();
7719
7720 if (! disptab_matches_widthtab (disptab,
7721 XVECTOR (current_buffer->width_table)))
7722 {
7723 invalidate_region_cache (current_buffer,
7724 current_buffer->width_run_cache,
7725 BEG, Z);
7726 recompute_width_table (current_buffer, disptab);
7727 }
7728 }
7729
7730 /* If window-start is screwed up, choose a new one. */
7731 if (XMARKER (w->start)->buffer != current_buffer)
7732 goto recenter;
7733
7734 SET_TEXT_POS_FROM_MARKER (startp, w->start);
7735
7736 /* If someone specified a new starting point but did not insist,
7737 check whether it can be used. */
7738 if (!NILP (w->optional_new_start))
7739 {
7740 w->optional_new_start = Qnil;
7741 /* This takes a mini-buffer prompt into account. */
7742 start_display (&it, w, startp);
7743 move_it_to (&it, PT, 0, it.last_visible_y, -1,
7744 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
7745 if (IT_CHARPOS (it) == PT)
7746 w->force_start = Qt;
7747 }
7748
7749 /* Handle case where place to start displaying has been specified,
7750 unless the specified location is outside the accessible range. */
7751 if (!NILP (w->force_start))
7752 {
7753 w->force_start = Qnil;
7754 w->vscroll = 0;
7755 w->window_end_valid = Qnil;
7756
7757 /* Forget any recorded base line for line number display. */
7758 if (!current_matrix_up_to_date_p
7759 || current_buffer->clip_changed)
7760 w->base_line_number = Qnil;
7761
7762 /* Redisplay the mode line. Select the buffer properly for that.
7763 Also, run the hook window-scroll-functions
7764 because we have scrolled. */
7765 /* Note, we do this after clearing force_start because
7766 if there's an error, it is better to forget about force_start
7767 than to get into an infinite loop calling the hook functions
7768 and having them get more errors. */
7769 if (!update_mode_line
7770 || ! NILP (Vwindow_scroll_functions))
7771 {
7772 if (!really_switched_buffer)
7773 {
7774 set_buffer_temp (old);
7775 set_buffer_internal_1 (XBUFFER (w->buffer));
7776 really_switched_buffer = 1;
7777 }
7778
7779 update_mode_line = 1;
7780 w->update_mode_line = Qt;
7781 startp = run_window_scroll_functions (window, startp);
7782 }
7783
7784 XSETFASTINT (w->last_modified, 0);
7785 XSETFASTINT (w->last_overlay_modified, 0);
7786 if (CHARPOS (startp) < BEGV)
7787 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
7788 else if (CHARPOS (startp) > ZV)
7789 SET_TEXT_POS (startp, ZV, ZV_BYTE);
7790
7791 /* Redisplay, then check if cursor has been set during the
7792 redisplay. Give up if new fonts were loaded. */
7793 if (!try_window (window, startp))
7794 {
7795 w->force_start = Qt;
7796 clear_glyph_matrix (w->desired_matrix);
7797 goto restore_buffers;
7798 }
7799
7800 if (w->cursor.vpos < 0)
7801 {
7802 /* If point does not appear, or on a line that is not fully
7803 visible, move point so it does appear. The desired
7804 matrix has been built above, so we can use it. */
7805 int height = window_box_height (w) / 2;
7806 struct glyph_row *row = MATRIX_ROW (w->desired_matrix, 0);
7807
7808 while (row->y < height)
7809 ++row;
7810
7811 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
7812 MATRIX_ROW_START_BYTEPOS (row));
7813
7814 if (w != XWINDOW (selected_window))
7815 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
7816 else if (current_buffer == old)
7817 SET_TEXT_POS (lpoint, PT, PT_BYTE);
7818
7819 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
7820
7821 /* If we are highlighting the region, then we just changed
7822 the region, so redisplay to show it. */
7823 if (!NILP (Vtransient_mark_mode)
7824 && !NILP (current_buffer->mark_active))
7825 {
7826 clear_glyph_matrix (w->desired_matrix);
7827 if (!try_window (window, startp))
7828 goto restore_buffers;
7829 }
7830 }
7831
7832 make_cursor_line_fully_visible (w);
7833 #if GLYPH_DEBUG
7834 debug_method_add (w, "forced window start");
7835 #endif
7836 goto done;
7837 }
7838
7839 /* Handle case where text has not changed, only point, and it has
7840 not moved off the frame. */
7841 if (current_matrix_up_to_date_p
7842 /* Point may be in this window. */
7843 && PT >= CHARPOS (startp)
7844 /* If we don't check this, we are called to move the cursor in a
7845 horizontally split window with a current matrix that doesn't
7846 fit the display. */
7847 && !windows_or_buffers_changed
7848 /* Selective display hasn't changed. */
7849 && !current_buffer->clip_changed
7850 /* If force-mode-line-update was called, really redisplay;
7851 that's how redisplay is forced after e.g. changing
7852 buffer-invisibility-spec. */
7853 && NILP (w->update_mode_line)
7854 /* Can't use this case if highlighting a region. When a
7855 region exists, cursor movement has to do more than just
7856 set the cursor. */
7857 && !(!NILP (Vtransient_mark_mode)
7858 && !NILP (current_buffer->mark_active))
7859 && NILP (w->region_showing)
7860 /* Right after splitting windows, last_point may be nil. */
7861 && INTEGERP (w->last_point)
7862 /* This code is not used for mini-buffer for the sake of the case
7863 of redisplaying to replace an echo area message; since in
7864 that case the mini-buffer contents per se are usually
7865 unchanged. This code is of no real use in the mini-buffer
7866 since the handling of this_line_start_pos, etc., in redisplay
7867 handles the same cases. */
7868 && !EQ (window, minibuf_window)
7869 /* When splitting windows or for new windows, it happens that
7870 redisplay is called with a nil window_end_vpos or one being
7871 larger than the window. This should really be fixed in
7872 window.c. I don't have this on my list, now, so we do
7873 approximately the same as the old redisplay code. --gerd. */
7874 && INTEGERP (w->window_end_vpos)
7875 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
7876 && (FRAME_WINDOW_P (f)
7877 || !MARKERP (Voverlay_arrow_position)
7878 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
7879 {
7880 int this_scroll_margin;
7881 struct glyph_row *row;
7882 int scroll_p;
7883
7884 #if GLYPH_DEBUG
7885 debug_method_add (w, "cursor movement");
7886 #endif
7887
7888 /* Scroll if point within this distance from the top or bottom
7889 of the window. This is a pixel value. */
7890 this_scroll_margin = max (0, scroll_margin);
7891 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
7892 this_scroll_margin *= CANON_Y_UNIT (f);
7893
7894 /* Start with the row the cursor was displayed during the last
7895 not paused redisplay. Give up if that row is not valid. */
7896 if (w->last_cursor.vpos >= w->current_matrix->nrows)
7897 goto try_to_scroll;
7898 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
7899 if (row->mode_line_p)
7900 ++row;
7901 if (!row->enabled_p)
7902 goto try_to_scroll;
7903
7904 scroll_p = 0;
7905 if (PT > XFASTINT (w->last_point))
7906 {
7907 /* Point has moved forward. */
7908 int last_y = window_text_bottom_y (w) - this_scroll_margin;
7909
7910 while ((MATRIX_ROW_END_CHARPOS (row) < PT
7911 /* The end position of a row equals the start
7912 position of the next row. If PT is there, we
7913 would rather display it in the next line, except
7914 when this line ends in ZV. */
7915 || (MATRIX_ROW_END_CHARPOS (row) == PT
7916 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
7917 || !row->ends_at_zv_p)))
7918 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
7919 {
7920 xassert (row->enabled_p);
7921 ++row;
7922 }
7923
7924 /* If within the scroll margin, scroll. Note that
7925 MATRIX_ROW_BOTTOM_Y gives the pixel position at which the
7926 next line would be drawn, and that this_scroll_margin can
7927 be zero. */
7928 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
7929 || PT > MATRIX_ROW_END_CHARPOS (row)
7930 /* Line is completely visible last line in window and PT
7931 is to be set in the next line. */
7932 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
7933 && PT == MATRIX_ROW_END_CHARPOS (row)
7934 && !row->ends_at_zv_p
7935 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
7936 scroll_p = 1;
7937 }
7938 else if (PT < XFASTINT (w->last_point))
7939 {
7940 /* Cursor has to be moved backward. Note that PT >=
7941 CHARPOS (startp) because of the outer if-statement. */
7942 while (!row->mode_line_p
7943 && (MATRIX_ROW_START_CHARPOS (row) > PT
7944 || (MATRIX_ROW_START_CHARPOS (row) == PT
7945 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
7946 && (row->y > this_scroll_margin
7947 || CHARPOS (startp) == BEGV))
7948 {
7949 xassert (row->enabled_p);
7950 --row;
7951 }
7952
7953 /* Consider the following case: Window starts at BEGV, there
7954 is invisible, intangible text at BEGV, so that display
7955 starts at some point START > BEGV. It can happen that
7956 we are called with PT somewhere between BEGV and START.
7957 Try to handle that case. */
7958 if (row < w->current_matrix->rows
7959 || row->mode_line_p)
7960 {
7961 row = w->current_matrix->rows;
7962 if (row->mode_line_p)
7963 ++row;
7964 }
7965
7966 /* Due to newlines in overlay strings, we may have to skip
7967 forward over overlay strings. */
7968 while (MATRIX_ROW_END_CHARPOS (row) == PT
7969 && MATRIX_ROW_ENDS_IN_OVERLAY_STRING_P (row)
7970 && !row->ends_at_zv_p)
7971 ++row;
7972
7973 /* If within the scroll margin, scroll. */
7974 if (row->y < this_scroll_margin
7975 && CHARPOS (startp) != BEGV)
7976 scroll_p = 1;
7977 }
7978
7979 /* if PT is not in the glyph row, give up. */
7980 if (PT < MATRIX_ROW_START_CHARPOS (row)
7981 || PT > MATRIX_ROW_END_CHARPOS (row))
7982 goto try_to_scroll;
7983
7984 /* If we end up in a partially visible line, let's make it fully
7985 visible. This can be done most easily by using the existing
7986 scrolling code. */
7987 if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
7988 {
7989 temp_scroll_step = 1;
7990 goto try_to_scroll;
7991 }
7992 else if (scroll_p)
7993 goto try_to_scroll;
7994
7995 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
7996 goto done;
7997 }
7998
7999 /* If current starting point was originally the beginning of a line
8000 but no longer is, find a new starting point. */
8001 else if (!NILP (w->start_at_line_beg)
8002 && !(CHARPOS (startp) <= BEGV
8003 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
8004 {
8005 #if GLYPH_DEBUG
8006 debug_method_add (w, "recenter 1");
8007 #endif
8008 goto recenter;
8009 }
8010
8011 /* Try scrolling with try_window_id. */
8012 else if (!windows_or_buffers_changed
8013 /* Window must be either use window-based redisplay or
8014 be full width. */
8015 && (FRAME_WINDOW_P (f)
8016 || ((line_ins_del_ok && WINDOW_FULL_WIDTH_P (w))
8017 && just_this_one_p))
8018 && !MINI_WINDOW_P (w)
8019 /* Point is not known NOT to appear in window. */
8020 && PT >= CHARPOS (startp)
8021 && XFASTINT (w->last_modified)
8022 /* Window is not hscrolled. */
8023 && XFASTINT (w->hscroll) == 0
8024 /* Selective display has not changed. */
8025 && !current_buffer->clip_changed
8026 /* Current matrix is up to date. */
8027 && !NILP (w->window_end_valid)
8028 /* Can't use this case if highlighting a region because
8029 a cursor movement will do more than just set the cursor. */
8030 && !(!NILP (Vtransient_mark_mode)
8031 && !NILP (current_buffer->mark_active))
8032 && NILP (w->region_showing)
8033 /* Overlay arrow position and string not changed. */
8034 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
8035 && EQ (last_arrow_string, Voverlay_arrow_string)
8036 /* Value is > 0 if update has been done, it is -1 if we
8037 know that the same window start will not work. It is 0
8038 if unsuccessful for some other reason. */
8039 && (tem = try_window_id (w)) != 0)
8040 {
8041 #if GLYPH_DEBUG
8042 debug_method_add (w, "try_window_id");
8043 #endif
8044
8045 if (fonts_changed_p)
8046 goto restore_buffers;
8047 if (tem > 0)
8048 goto done;
8049 /* Otherwise try_window_id has returned -1 which means that we
8050 don't want the alternative below this comment to execute. */
8051 }
8052 else if (CHARPOS (startp) >= BEGV
8053 && CHARPOS (startp) <= ZV
8054 && PT >= CHARPOS (startp)
8055 && (CHARPOS (startp) < ZV
8056 /* Avoid starting at end of buffer. */
8057 || CHARPOS (startp) == BEGV
8058 || (XFASTINT (w->last_modified) >= MODIFF
8059 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
8060 {
8061 #if GLYPH_DEBUG
8062 debug_method_add (w, "same window start");
8063 #endif
8064
8065 /* Try to redisplay starting at same place as before.
8066 If point has not moved off frame, accept the results. */
8067 if (!current_matrix_up_to_date_p
8068 /* Don't use try_window_reusing_current_matrix in this case
8069 because it can have changed the buffer. */
8070 || !NILP (Vwindow_scroll_functions)
8071 || MINI_WINDOW_P (w)
8072 || !try_window_reusing_current_matrix (w))
8073 {
8074 IF_DEBUG (debug_method_add (w, "1"));
8075 try_window (window, startp);
8076 }
8077
8078 if (fonts_changed_p)
8079 goto restore_buffers;
8080
8081 if (w->cursor.vpos >= 0)
8082 {
8083 if (!just_this_one_p
8084 || current_buffer->clip_changed
8085 || beg_unchanged < CHARPOS (startp))
8086 /* Forget any recorded base line for line number display. */
8087 w->base_line_number = Qnil;
8088
8089 make_cursor_line_fully_visible (w);
8090 goto done;
8091 }
8092 else
8093 clear_glyph_matrix (w->desired_matrix);
8094 }
8095
8096 try_to_scroll:
8097
8098 XSETFASTINT (w->last_modified, 0);
8099 XSETFASTINT (w->last_overlay_modified, 0);
8100
8101 /* Redisplay the mode line. Select the buffer properly for that. */
8102 if (!update_mode_line)
8103 {
8104 if (!really_switched_buffer)
8105 {
8106 set_buffer_temp (old);
8107 set_buffer_internal_1 (XBUFFER (w->buffer));
8108 really_switched_buffer = 1;
8109 }
8110 update_mode_line = 1;
8111 w->update_mode_line = Qt;
8112 }
8113
8114 /* Try to scroll by specified few lines. */
8115 if ((scroll_conservatively
8116 || scroll_step
8117 || temp_scroll_step
8118 || NUMBERP (current_buffer->scroll_up_aggressively)
8119 || NUMBERP (current_buffer->scroll_down_aggressively))
8120 && !current_buffer->clip_changed
8121 && CHARPOS (startp) >= BEGV
8122 && CHARPOS (startp) <= ZV)
8123 {
8124 /* The function returns -1 if new fonts were loaded, 1 if
8125 successful, 0 if not successful. */
8126 int rc = try_scrolling (window, just_this_one_p,
8127 scroll_conservatively,
8128 scroll_step,
8129 temp_scroll_step);
8130 if (rc > 0)
8131 goto done;
8132 else if (rc < 0)
8133 goto restore_buffers;
8134 }
8135
8136 /* Finally, just choose place to start which centers point */
8137
8138 recenter:
8139
8140 #if GLYPH_DEBUG
8141 debug_method_add (w, "recenter");
8142 #endif
8143
8144 /* w->vscroll = 0; */
8145
8146 /* Forget any previously recorded base line for line number display. */
8147 if (!current_matrix_up_to_date_p
8148 || current_buffer->clip_changed)
8149 w->base_line_number = Qnil;
8150
8151 /* Move backward half the height of the window. */
8152 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
8153 it.current_y = it.last_visible_y;
8154 move_it_vertically_backward (&it, it.last_visible_y / 2);
8155 xassert (IT_CHARPOS (it) >= BEGV);
8156
8157 /* The function move_it_vertically_backward may move over more
8158 than the specified y-distance. If it->w is small, e.g. a
8159 mini-buffer window, we may end up in front of the window's
8160 display area. Start displaying at the start of the line
8161 containing PT in this case. */
8162 if (it.current_y <= 0)
8163 {
8164 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
8165 move_it_vertically (&it, 0);
8166 xassert (IT_CHARPOS (it) <= PT);
8167 it.current_y = 0;
8168 }
8169
8170 it.current_x = it.hpos = 0;
8171
8172 /* Set startp here explicitly in case that helps avoid an infinite loop
8173 in case the window-scroll-functions functions get errors. */
8174 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
8175
8176 /* Run scroll hooks. */
8177 startp = run_window_scroll_functions (window, it.current.pos);
8178
8179 /* Redisplay the window. */
8180 if (!current_matrix_up_to_date_p
8181 || windows_or_buffers_changed
8182 /* Don't use try_window_reusing_current_matrix in this case
8183 because it can have changed the buffer. */
8184 || !NILP (Vwindow_scroll_functions)
8185 || !just_this_one_p
8186 || MINI_WINDOW_P (w)
8187 || !try_window_reusing_current_matrix (w))
8188 try_window (window, startp);
8189
8190 /* If new fonts have been loaded (due to fontsets), give up. We
8191 have to start a new redisplay since we need to re-adjust glyph
8192 matrices. */
8193 if (fonts_changed_p)
8194 goto restore_buffers;
8195
8196 /* If cursor did not appear assume that the middle of the window is
8197 in the first line of the window. Do it again with the next line.
8198 (Imagine a window of height 100, displaying two lines of height
8199 60. Moving back 50 from it->last_visible_y will end in the first
8200 line.) */
8201 if (w->cursor.vpos < 0)
8202 {
8203 if (!NILP (w->window_end_valid)
8204 && PT >= Z - XFASTINT (w->window_end_pos))
8205 {
8206 clear_glyph_matrix (w->desired_matrix);
8207 move_it_by_lines (&it, 1, 0);
8208 try_window (window, it.current.pos);
8209 }
8210 else if (PT < IT_CHARPOS (it))
8211 {
8212 clear_glyph_matrix (w->desired_matrix);
8213 move_it_by_lines (&it, -1, 0);
8214 try_window (window, it.current.pos);
8215 }
8216 else
8217 {
8218 /* Not much we can do about it. */
8219 }
8220 }
8221
8222 /* Consider the following case: Window starts at BEGV, there is
8223 invisible, intangible text at BEGV, so that display starts at
8224 some point START > BEGV. It can happen that we are called with
8225 PT somewhere between BEGV and START. Try to handle that case. */
8226 if (w->cursor.vpos < 0)
8227 {
8228 struct glyph_row *row = w->current_matrix->rows;
8229 if (row->mode_line_p)
8230 ++row;
8231 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8232 }
8233
8234 make_cursor_line_fully_visible (w);
8235
8236 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8237 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
8238 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
8239 ? Qt : Qnil);
8240
8241 done:
8242
8243 /* Display the mode line, if we must. */
8244 if ((update_mode_line
8245 /* If window not full width, must redo its mode line
8246 if (a) the window to its side is being redone and
8247 (b) we do a frame-based redisplay. This is a consequence
8248 of how inverted lines are drawn in frame-based redisplay. */
8249 || (!just_this_one_p
8250 && !FRAME_WINDOW_P (f)
8251 && !WINDOW_FULL_WIDTH_P (w))
8252 /* Line number to display. */
8253 || INTEGERP (w->base_line_pos)
8254 /* Column number is displayed and different from the one displayed. */
8255 || (!NILP (w->column_number_displayed)
8256 && XFASTINT (w->column_number_displayed) != current_column ()))
8257 /* This means that the window has a mode line. */
8258 && (WINDOW_WANTS_MODELINE_P (w)
8259 || WINDOW_WANTS_TOP_LINE_P (w)))
8260 {
8261 display_mode_lines (w);
8262
8263 /* If mode line height has changed, arrange for a thorough
8264 immediate redisplay using the correct mode line height. */
8265 if (WINDOW_WANTS_MODELINE_P (w)
8266 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
8267 {
8268 fonts_changed_p = 1;
8269 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
8270 = DESIRED_MODE_LINE_HEIGHT (w);
8271 }
8272
8273 /* If top line height has changed, arrange for a thorough
8274 immediate redisplay using the correct mode line height. */
8275 if (WINDOW_WANTS_TOP_LINE_P (w)
8276 && CURRENT_TOP_LINE_HEIGHT (w) != DESIRED_TOP_LINE_HEIGHT (w))
8277 {
8278 fonts_changed_p = 1;
8279 MATRIX_TOP_LINE_ROW (w->current_matrix)->height
8280 = DESIRED_TOP_LINE_HEIGHT (w);
8281 }
8282
8283 if (fonts_changed_p)
8284 goto restore_buffers;
8285 }
8286
8287 if (!line_number_displayed
8288 && !BUFFERP (w->base_line_pos))
8289 {
8290 w->base_line_pos = Qnil;
8291 w->base_line_number = Qnil;
8292 }
8293
8294 finish_menu_bars:
8295
8296 /* When we reach a frame's selected window, redo the frame's menu bar. */
8297 if (update_mode_line
8298 && EQ (FRAME_SELECTED_WINDOW (f), window))
8299 {
8300 int redisplay_menu_p = 0;
8301
8302 if (FRAME_WINDOW_P (f))
8303 {
8304 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
8305 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
8306 #else
8307 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
8308 #endif
8309 }
8310 else
8311 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
8312
8313 if (redisplay_menu_p)
8314 display_menu_bar (w);
8315
8316 #ifdef HAVE_WINDOW_SYSTEM
8317 if (WINDOWP (f->toolbar_window)
8318 && (FRAME_TOOLBAR_LINES (f) > 0
8319 || auto_resize_toolbars_p))
8320 redisplay_toolbar (f);
8321 #endif
8322 }
8323
8324 finish_scroll_bars:
8325
8326 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8327 {
8328 int start, end, whole;
8329
8330 /* Calculate the start and end positions for the current window.
8331 At some point, it would be nice to choose between scrollbars
8332 which reflect the whole buffer size, with special markers
8333 indicating narrowing, and scrollbars which reflect only the
8334 visible region.
8335
8336 Note that mini-buffers sometimes aren't displaying any text. */
8337 if (! MINI_WINDOW_P (w)
8338 || (w == XWINDOW (minibuf_window)
8339 && !echo_area_glyphs
8340 && !STRINGP (echo_area_message)))
8341 {
8342 whole = ZV - BEGV;
8343 start = marker_position (w->start) - BEGV;
8344 /* I don't think this is guaranteed to be right. For the
8345 moment, we'll pretend it is. */
8346 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
8347
8348 if (end < start)
8349 end = start;
8350 if (whole < (end - start))
8351 whole = end - start;
8352 }
8353 else
8354 start = end = whole = 0;
8355
8356 /* Indicate what this scroll bar ought to be displaying now. */
8357 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
8358
8359 /* Note that we actually used the scroll bar attached to this
8360 window, so it shouldn't be deleted at the end of redisplay. */
8361 (*redeem_scroll_bar_hook) (w);
8362 }
8363
8364 restore_buffers:
8365
8366 /* Restore current_buffer and value of point in it. */
8367 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
8368 if (really_switched_buffer)
8369 set_buffer_internal_1 (old);
8370 else
8371 set_buffer_temp (old);
8372 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
8373
8374 unbind_to (count, Qnil);
8375 }
8376
8377
8378 /* Build the complete desired matrix of WINDOW with a window start
8379 buffer position POS. Value is non-zero if successful. It is zero
8380 if fonts were loaded during redisplay which makes re-adjusting
8381 glyph matrices necessary. */
8382
8383 int
8384 try_window (window, pos)
8385 Lisp_Object window;
8386 struct text_pos pos;
8387 {
8388 struct window *w = XWINDOW (window);
8389 struct it it;
8390 struct glyph_row *last_text_row = NULL;
8391
8392 /* Make POS the new window start. */
8393 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
8394
8395 /* Mark cursor position as unknown. No overlay arrow seen. */
8396 w->cursor.vpos = -1;
8397 overlay_arrow_seen = 0;
8398
8399 /* Initialize iterator and info to start at POS. */
8400 start_display (&it, w, pos);
8401
8402 /* Display all lines of W. */
8403 while (it.current_y < it.last_visible_y)
8404 {
8405 if (display_line (&it))
8406 last_text_row = it.glyph_row - 1;
8407 if (fonts_changed_p)
8408 return 0;
8409 }
8410
8411 /* If bottom moved off end of frame, change mode line percentage. */
8412 if (XFASTINT (w->window_end_pos) <= 0
8413 && Z != IT_CHARPOS (it))
8414 w->update_mode_line = Qt;
8415
8416 /* Set window_end_pos to the offset of the last character displayed
8417 on the window from the end of current_buffer. Set
8418 window_end_vpos to its row number. */
8419 if (last_text_row)
8420 {
8421 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
8422 w->window_end_bytepos
8423 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
8424 XSETFASTINT (w->window_end_pos,
8425 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
8426 XSETFASTINT (w->window_end_vpos,
8427 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
8428 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
8429 ->displays_text_p);
8430 }
8431 else
8432 {
8433 w->window_end_bytepos = 0;
8434 XSETFASTINT (w->window_end_pos, 0);
8435 XSETFASTINT (w->window_end_vpos, 0);
8436 }
8437
8438 /* But that is not valid info until redisplay finishes. */
8439 w->window_end_valid = Qnil;
8440 return 1;
8441 }
8442
8443
8444 \f
8445 /************************************************************************
8446 Window redisplay reusing current matrix when buffer has not changed
8447 ************************************************************************/
8448
8449 /* Try redisplay of window W showing an unchanged buffer with a
8450 different window start than the last time it was displayed by
8451 reusing its current matrix. Value is non-zero if successful.
8452 W->start is the new window start. */
8453
8454 static int
8455 try_window_reusing_current_matrix (w)
8456 struct window *w;
8457 {
8458 struct frame *f = XFRAME (w->frame);
8459 struct glyph_row *row, *bottom_row;
8460 struct it it;
8461 struct run run;
8462 struct text_pos start, new_start;
8463 int nrows_scrolled, i;
8464 struct glyph_row *last_text_row;
8465 struct glyph_row *last_reused_text_row;
8466 struct glyph_row *start_row;
8467 int start_vpos, min_y, max_y;
8468
8469 /* Right now this function doesn't handle terminal frames. */
8470 if (!FRAME_WINDOW_P (f))
8471 return 0;
8472
8473 /* Can't do this if region may have changed. */
8474 if ((!NILP (Vtransient_mark_mode)
8475 && !NILP (current_buffer->mark_active))
8476 || !NILP (w->region_showing))
8477 return 0;
8478
8479 /* If top-line visibility has changed, give up. */
8480 if (WINDOW_WANTS_TOP_LINE_P (w)
8481 != MATRIX_TOP_LINE_ROW (w->current_matrix)->mode_line_p)
8482 return 0;
8483
8484 /* Give up if old or new display is scrolled vertically. We could
8485 make this function handle this, but right now it doesn't. */
8486 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
8487 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
8488 return 0;
8489
8490 /* The variable new_start now holds the new window start. The old
8491 start `start' can be determined from the current matrix. */
8492 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
8493 start = start_row->start.pos;
8494 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
8495
8496 /* Clear the desired matrix for the display below. */
8497 clear_glyph_matrix (w->desired_matrix);
8498
8499 if (CHARPOS (new_start) <= CHARPOS (start))
8500 {
8501 int first_row_y;
8502
8503 IF_DEBUG (debug_method_add (w, "twu1"));
8504
8505 /* Display up to a row that can be reused. The variable
8506 last_text_row is set to the last row displayed that displays
8507 text. */
8508 start_display (&it, w, new_start);
8509 first_row_y = it.current_y;
8510 w->cursor.vpos = -1;
8511 last_text_row = last_reused_text_row = NULL;
8512 while (it.current_y < it.last_visible_y
8513 && IT_CHARPOS (it) < CHARPOS (start)
8514 && !fonts_changed_p)
8515 if (display_line (&it))
8516 last_text_row = it.glyph_row - 1;
8517
8518 /* A value of current_y < last_visible_y means that we stopped
8519 at the previous window start, which in turn means that we
8520 have at least one reusable row. */
8521 if (it.current_y < it.last_visible_y)
8522 {
8523 nrows_scrolled = it.vpos;
8524
8525 /* Find PT if not already found in the lines displayed. */
8526 if (w->cursor.vpos < 0)
8527 {
8528 int dy = it.current_y - first_row_y;
8529
8530 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
8531 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
8532 {
8533 if (PT >= MATRIX_ROW_START_CHARPOS (row)
8534 && PT < MATRIX_ROW_END_CHARPOS (row))
8535 {
8536 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
8537 dy, nrows_scrolled);
8538 break;
8539 }
8540
8541 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
8542 break;
8543
8544 ++row;
8545 }
8546
8547 /* Give up if point was not found. This shouldn't
8548 happen often; not more often than with try_window
8549 itself. */
8550 if (w->cursor.vpos < 0)
8551 {
8552 clear_glyph_matrix (w->desired_matrix);
8553 return 0;
8554 }
8555 }
8556
8557 /* Scroll the display. Do it before the current matrix is
8558 changed. The problem here is that update has not yet
8559 run, i.e. part of the current matrix is not up to date.
8560 scroll_run_hook will clear the cursor, and use the
8561 current matrix to get the height of the row the cursor is
8562 in. */
8563 run.current_y = first_row_y;
8564 run.desired_y = it.current_y;
8565 run.height = it.last_visible_y - it.current_y;
8566 if (run.height > 0)
8567 {
8568 update_begin (f);
8569 rif->update_window_begin_hook (w);
8570 rif->scroll_run_hook (w, &run);
8571 rif->update_window_end_hook (w, 0);
8572 update_end (f);
8573 }
8574
8575 /* Shift current matrix down by nrows_scrolled lines. */
8576 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
8577 rotate_matrix (w->current_matrix,
8578 start_vpos,
8579 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
8580 nrows_scrolled);
8581
8582 /* Disable lines not reused. */
8583 for (i = 0; i < it.vpos; ++i)
8584 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
8585
8586 /* Re-compute Y positions. */
8587 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix) + nrows_scrolled;
8588 min_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
8589 max_y = it.last_visible_y;
8590 while (row < bottom_row)
8591 {
8592 row->y = it.current_y;
8593
8594 if (row->y < min_y)
8595 row->visible_height = row->height - (min_y - row->y);
8596 else if (row->y + row->height > max_y)
8597 row->visible_height
8598 = row->height - (row->y + row->height - max_y);
8599 else
8600 row->visible_height = row->height;
8601
8602 it.current_y += row->height;
8603 ++it.vpos;
8604
8605 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
8606 last_reused_text_row = row;
8607 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
8608 break;
8609 ++row;
8610 }
8611 }
8612
8613 /* Update window_end_pos etc.; last_reused_text_row is the last
8614 reused row from the current matrix containing text, if any.
8615 The value of last_text_row is the last displayed line
8616 containing text. */
8617 if (last_reused_text_row)
8618 {
8619 w->window_end_bytepos
8620 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
8621 XSETFASTINT (w->window_end_pos,
8622 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
8623 XSETFASTINT (w->window_end_vpos,
8624 MATRIX_ROW_VPOS (last_reused_text_row,
8625 w->current_matrix));
8626 }
8627 else if (last_text_row)
8628 {
8629 w->window_end_bytepos
8630 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
8631 XSETFASTINT (w->window_end_pos,
8632 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
8633 XSETFASTINT (w->window_end_vpos,
8634 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
8635 }
8636 else
8637 {
8638 /* This window must be completely empty. */
8639 w->window_end_bytepos = 0;
8640 XSETFASTINT (w->window_end_pos, 0);
8641 XSETFASTINT (w->window_end_vpos, 0);
8642 }
8643 w->window_end_valid = Qnil;
8644
8645 /* Update hint: don't try scrolling again in update_window. */
8646 w->desired_matrix->no_scrolling_p = 1;
8647
8648 #if GLYPH_DEBUG
8649 debug_method_add (w, "try_window_reusing_current_matrix 1");
8650 #endif
8651 return 1;
8652 }
8653 else if (CHARPOS (new_start) > CHARPOS (start))
8654 {
8655 struct glyph_row *pt_row, *row;
8656 struct glyph_row *first_reusable_row;
8657 struct glyph_row *first_row_to_display;
8658 int dy;
8659 int yb = window_text_bottom_y (w);
8660
8661 IF_DEBUG (debug_method_add (w, "twu2"));
8662
8663 /* Find the row starting at new_start, if there is one. Don't
8664 reuse a partially visible line at the end. */
8665 first_reusable_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
8666 while (first_reusable_row->enabled_p
8667 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
8668 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
8669 < CHARPOS (new_start)))
8670 ++first_reusable_row;
8671
8672 /* Give up if there is no row to reuse. */
8673 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
8674 || !first_reusable_row->enabled_p)
8675 return 0;
8676
8677 /* The row we found must start at new_start, or else something
8678 is broken. */
8679 xassert (MATRIX_ROW_START_CHARPOS (first_reusable_row)
8680 == CHARPOS (new_start));
8681
8682 /* We can reuse fully visible rows beginning with
8683 first_reusable_row to the end of the window. Set
8684 first_row_to_display to the first row that cannot be reused.
8685 Set pt_row to the row containing point, if there is any. */
8686 first_row_to_display = first_reusable_row;
8687 pt_row = NULL;
8688 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
8689 {
8690 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
8691 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
8692 pt_row = first_row_to_display;
8693
8694 ++first_row_to_display;
8695 }
8696
8697 /* Start displaying at the start of first_row_to_display. */
8698 xassert (first_row_to_display->y < yb);
8699 init_to_row_start (&it, w, first_row_to_display);
8700 nrows_scrolled = MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix);
8701 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
8702 - nrows_scrolled);
8703 it.current_y = first_row_to_display->y - first_reusable_row->y;
8704
8705 /* Display lines beginning with first_row_to_display in the
8706 desired matrix. Set last_text_row to the last row displayed
8707 that displays text. */
8708 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
8709 if (pt_row == NULL)
8710 w->cursor.vpos = -1;
8711 last_text_row = NULL;
8712 while (it.current_y < it.last_visible_y && !fonts_changed_p)
8713 if (display_line (&it))
8714 last_text_row = it.glyph_row - 1;
8715
8716 /* Give up If point isn't in a row displayed or reused. */
8717 if (w->cursor.vpos < 0)
8718 {
8719 clear_glyph_matrix (w->desired_matrix);
8720 return 0;
8721 }
8722
8723 /* If point is in a reused row, adjust y and vpos of the cursor
8724 position. */
8725 if (pt_row)
8726 {
8727 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
8728 w->current_matrix);
8729 w->cursor.y -= first_reusable_row->y;
8730 }
8731
8732 /* Scroll the display. */
8733 run.current_y = first_reusable_row->y;
8734 run.desired_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
8735 run.height = it.last_visible_y - run.current_y;
8736 if (run.height)
8737 {
8738 struct frame *f = XFRAME (WINDOW_FRAME (w));
8739 update_begin (f);
8740 rif->update_window_begin_hook (w);
8741 rif->scroll_run_hook (w, &run);
8742 rif->update_window_end_hook (w, 0);
8743 update_end (f);
8744 }
8745
8746 /* Adjust Y positions of reused rows. */
8747 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
8748 row = first_reusable_row;
8749 dy = first_reusable_row->y;
8750 min_y = WINDOW_DISPLAY_TOP_LINE_HEIGHT (w);
8751 max_y = it.last_visible_y;
8752 while (row < first_row_to_display)
8753 {
8754 row->y -= dy;
8755 if (row->y < min_y)
8756 row->visible_height = row->height - (min_y - row->y);
8757 else if (row->y + row->height > max_y)
8758 row->visible_height
8759 = row->height - (row->y + row->height - max_y);
8760 else
8761 row->visible_height = row->height;
8762 ++row;
8763 }
8764
8765 /* Disable rows not reused. */
8766 while (row < bottom_row)
8767 {
8768 row->enabled_p = 0;
8769 ++row;
8770 }
8771
8772 /* Scroll the current matrix. */
8773 xassert (nrows_scrolled > 0);
8774 rotate_matrix (w->current_matrix,
8775 start_vpos,
8776 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
8777 -nrows_scrolled);
8778
8779 /* Adjust window end. A null value of last_text_row means that
8780 the window end is in reused rows which in turn means that
8781 only its vpos can have changed. */
8782 if (last_text_row)
8783 {
8784 w->window_end_bytepos
8785 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
8786 XSETFASTINT (w->window_end_pos,
8787 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
8788 XSETFASTINT (w->window_end_vpos,
8789 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
8790 }
8791 else
8792 {
8793 XSETFASTINT (w->window_end_vpos,
8794 XFASTINT (w->window_end_vpos) - nrows_scrolled);
8795 }
8796
8797 w->window_end_valid = Qnil;
8798 w->desired_matrix->no_scrolling_p = 1;
8799
8800 #if GLYPH_DEBUG
8801 debug_method_add (w, "try_window_reusing_current_matrix 2");
8802 #endif
8803 return 1;
8804 }
8805
8806 return 0;
8807 }
8808
8809
8810 \f
8811 /************************************************************************
8812 Window redisplay reusing current matrix when buffer has changed
8813 ************************************************************************/
8814
8815 static struct glyph_row *get_last_unchanged_at_beg_row P_ ((struct window *));
8816 static struct glyph_row *get_first_unchanged_at_end_row P_ ((struct window *,
8817 int *, int *));
8818 static struct glyph_row *
8819 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
8820 struct glyph_row *));
8821
8822
8823 /* Return the last row in MATRIX displaying text. If row START is
8824 non-null, start searching with that row. IT gives the dimensions
8825 of the display. Value is null if matrix is empty; otherwise it is
8826 a pointer to the row found. */
8827
8828 static struct glyph_row *
8829 find_last_row_displaying_text (matrix, it, start)
8830 struct glyph_matrix *matrix;
8831 struct it *it;
8832 struct glyph_row *start;
8833 {
8834 struct glyph_row *row, *row_found;
8835
8836 /* Set row_found to the last row in IT->w's current matrix
8837 displaying text. The loop looks funny but think of partially
8838 visible lines. */
8839 row_found = NULL;
8840 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
8841 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
8842 {
8843 xassert (row->enabled_p);
8844 row_found = row;
8845 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
8846 break;
8847 ++row;
8848 }
8849
8850 return row_found;
8851 }
8852
8853
8854 /* Return the last row in the current matrix of W that is not affected
8855 by changes at the start of current_buffer that occurred since the
8856 last time W was redisplayed. Value is null if no such row exists.
8857
8858 The global variable beg_unchanged has to contain the number of
8859 bytes unchanged at the start of current_buffer. BEG +
8860 beg_unchanged is the buffer position of the first changed byte in
8861 current_buffer. Characters at positions < BEG + beg_unchanged are
8862 at the same buffer positions as they were when the current matrix
8863 was built. */
8864
8865 static struct glyph_row *
8866 get_last_unchanged_at_beg_row (w)
8867 struct window *w;
8868 {
8869 int first_changed_pos = BEG + beg_unchanged;
8870 struct glyph_row *row;
8871 struct glyph_row *row_found = NULL;
8872 int yb = window_text_bottom_y (w);
8873
8874 /* Find the last row displaying unchanged text. */
8875 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
8876 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
8877 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
8878 {
8879 if (/* If row ends before first_changed_pos, it is unchanged,
8880 except in some case. */
8881 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
8882 /* When row ends in ZV and we write at ZV it is not
8883 unchanged. */
8884 && !row->ends_at_zv_p
8885 /* When first_changed_pos is the end of a continued line,
8886 row is not unchanged because it may be no longer
8887 continued. */
8888 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
8889 && row->continued_p))
8890 row_found = row;
8891
8892 /* Stop if last visible row. */
8893 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
8894 break;
8895
8896 ++row;
8897 }
8898
8899 return row_found;
8900 }
8901
8902
8903 /* Find the first glyph row in the current matrix of W that is not
8904 affected by changes at the end of current_buffer since the last
8905 time the window was redisplayed. Return in *DELTA the number of
8906 bytes by which buffer positions in unchanged text at the end of
8907 current_buffer must be adjusted. Value is null if no such row
8908 exists, i.e. all rows are affected by changes.
8909
8910 The global variable end_unchanged is assumed to contain the number
8911 of unchanged bytes at the end of current_buffer. The buffer
8912 position of the last changed byte in current_buffer is then Z -
8913 end_unchanged. */
8914
8915 static struct glyph_row *
8916 get_first_unchanged_at_end_row (w, delta, delta_bytes)
8917 struct window *w;
8918 int *delta, *delta_bytes;
8919 {
8920 struct glyph_row *row;
8921 struct glyph_row *row_found = NULL;
8922
8923 *delta = *delta_bytes = 0;
8924
8925 /* A value of window_end_pos >= end_unchanged means that the window
8926 end is in the range of changed text. If so, there is no
8927 unchanged row at the end of W's current matrix. */
8928 xassert (!NILP (w->window_end_valid));
8929 if (XFASTINT (w->window_end_pos) >= end_unchanged)
8930 return NULL;
8931
8932 /* Set row to the last row in W's current matrix displaying text. */
8933 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
8934
8935 /* End vpos should always be on text, except in an entirely empty
8936 matrix. */
8937 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row)
8938 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0);
8939
8940 /* If matrix is entirely empty, no unchanged row exists. */
8941 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
8942 {
8943 /* The value of row is the last glyph row in the matrix having a
8944 meaningful buffer position in it. The end position of row
8945 corresponds to window_end_pos. This allows us to translate
8946 buffer positions in the current matrix to current buffer
8947 positions for characters not in changed text. */
8948 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
8949 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
8950 int last_unchanged_pos, last_unchanged_pos_old;
8951 struct glyph_row *first_text_row
8952 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
8953
8954 *delta = Z - Z_old;
8955 *delta_bytes = Z_BYTE - Z_BYTE_old;
8956
8957 /* Set last_unchanged_pos to the buffer position of the last
8958 character in the buffer that has not been changed. Z is the
8959 index + 1 of the last byte in current_buffer, i.e. by
8960 subtracting end_unchanged we get the index of the last
8961 unchanged character, and we have to add BEG to get its buffer
8962 position. */
8963 last_unchanged_pos = Z - end_unchanged + BEG;
8964 last_unchanged_pos_old = last_unchanged_pos - *delta;
8965
8966 /* Search backward from ROW for a row displaying a line that
8967 starts at a minimum position >= last_unchanged_pos_old. */
8968 while (row >= first_text_row)
8969 {
8970 xassert (row->enabled_p);
8971 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row));
8972
8973 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
8974 row_found = row;
8975 --row;
8976 }
8977 }
8978
8979 xassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
8980 return row_found;
8981 }
8982
8983
8984 /* Make sure that glyph rows in the current matrix of window W
8985 reference the same glyph memory as corresponding rows in the
8986 frame's frame matrix. This function is called after scrolling W's
8987 current matrix on a terminal frame in try_window_id and
8988 try_window_reusing_current_matrix. */
8989
8990 static void
8991 sync_frame_with_window_matrix_rows (w)
8992 struct window *w;
8993 {
8994 struct frame *f = XFRAME (w->frame);
8995 struct glyph_row *window_row, *window_row_end, *frame_row;
8996
8997 /* Preconditions: W must be a leaf window and full-width. Its frame
8998 must have a frame matrix. */
8999 xassert (NILP (w->hchild) && NILP (w->vchild));
9000 xassert (WINDOW_FULL_WIDTH_P (w));
9001 xassert (!FRAME_WINDOW_P (f));
9002
9003 /* If W is a full-width window, glyph pointers in W's current matrix
9004 have, by definition, to be the same as glyph pointers in the
9005 corresponding frame matrix. */
9006 window_row = w->current_matrix->rows;
9007 window_row_end = window_row + w->current_matrix->nrows;
9008 frame_row = f->current_matrix->rows + XFASTINT (w->top);
9009 while (window_row < window_row_end)
9010 {
9011 int area;
9012 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
9013 frame_row->glyphs[area] = window_row->glyphs[area];
9014 ++window_row, ++frame_row;
9015 }
9016 }
9017
9018
9019 /* Try to redisplay window W by reusing its existing display. W's
9020 current matrix must be up to date when this function is called,
9021 i.e. window_end_valid must not be nil.
9022
9023 Value is
9024
9025 1 if display has been updated
9026 0 if otherwise unsuccessful
9027 -1 if redisplay with same window start is known not to succeed
9028
9029 The following steps are performed:
9030
9031 1. Find the last row in the current matrix of W that is not
9032 affected by changes at the start of current_buffer. If no such row
9033 is found, give up.
9034
9035 2. Find the first row in W's current matrix that is not affected by
9036 changes at the end of current_buffer. Maybe there is no such row.
9037
9038 3. Display lines beginning with the row + 1 found in step 1 to the
9039 row found in step 2 or, if step 2 didn't find a row, to the end of
9040 the window.
9041
9042 4. If cursor is not known to appear on the window, give up.
9043
9044 5. If display stopped at the row found in step 2, scroll the
9045 display and current matrix as needed.
9046
9047 6. Maybe display some lines at the end of W, if we must. This can
9048 happen under various circumstances, like a partially visible line
9049 becoming fully visible, or because newly displayed lines are displayed
9050 in smaller font sizes.
9051
9052 7. Update W's window end information. */
9053
9054 /* Check that window end is what we expect it to be. */
9055
9056 static int
9057 try_window_id (w)
9058 struct window *w;
9059 {
9060 struct frame *f = XFRAME (w->frame);
9061 struct glyph_matrix *current_matrix = w->current_matrix;
9062 struct glyph_matrix *desired_matrix = w->desired_matrix;
9063 struct glyph_row *last_unchanged_at_beg_row;
9064 struct glyph_row *first_unchanged_at_end_row;
9065 struct glyph_row *row;
9066 struct glyph_row *bottom_row;
9067 int bottom_vpos;
9068 struct it it;
9069 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
9070 struct text_pos start_pos;
9071 struct run run;
9072 int first_unchanged_at_end_vpos = 0;
9073 struct glyph_row *last_text_row, *last_text_row_at_end;
9074 struct text_pos start;
9075
9076 SET_TEXT_POS_FROM_MARKER (start, w->start);
9077
9078 /* Check pre-conditions. Window end must be valid, otherwise
9079 the current matrix would not be up to date. */
9080 xassert (!NILP (w->window_end_valid));
9081 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
9082 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
9083
9084 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
9085 only if buffer has really changed. The reason is that the gap is
9086 initially at Z for freshly visited files. The code below would
9087 set end_unchanged to 0 in that case. */
9088 if (MODIFF > SAVE_MODIFF)
9089 {
9090 if (GPT - BEG < beg_unchanged)
9091 beg_unchanged = GPT - BEG;
9092 if (Z - GPT < end_unchanged)
9093 end_unchanged = Z - GPT;
9094 }
9095
9096 /* If window starts after a line end, and the last change is in
9097 front of that newline, then changes don't affect the display.
9098 This case happens with stealth-fontification. */
9099 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
9100 if (CHARPOS (start) > BEGV
9101 && Z - end_unchanged < CHARPOS (start) - 1
9102 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
9103 && PT < MATRIX_ROW_END_CHARPOS (row))
9104 {
9105 /* We have to update window end positions because the buffer's
9106 size has changed. */
9107 w->window_end_pos
9108 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
9109 w->window_end_bytepos
9110 = make_number (Z_BYTE - MATRIX_ROW_END_BYTEPOS (row));
9111 return 1;
9112 }
9113
9114 /* Return quickly if changes are all below what is displayed in the
9115 window, and if PT is in the window. */
9116 if (beg_unchanged > MATRIX_ROW_END_CHARPOS (row)
9117 && PT < MATRIX_ROW_END_CHARPOS (row))
9118 {
9119 /* We have to update window end positions because the buffer's
9120 size has changed. */
9121 w->window_end_pos
9122 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
9123 w->window_end_bytepos
9124 = make_number (Z_BYTE - MATRIX_ROW_END_BYTEPOS (row));
9125 return 1;
9126 }
9127
9128 /* Check that window start agrees with the start of the first glyph
9129 row in its current matrix. Check this after we know the window
9130 start is not in changed text, otherwise positions would not be
9131 comparable. */
9132 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9133 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
9134 return 0;
9135
9136 /* Remember beg_unchanged and end_unchanged for debugging purposes. */
9137 IF_DEBUG (debug_beg_unchanged = beg_unchanged;
9138 debug_end_unchanged = end_unchanged);
9139
9140 /* Compute the position at which we have to start displaying new
9141 lines. Some of the lines at the top of the window might be
9142 reusable because they are not displaying changed text. Find the
9143 last row in W's current matrix not affected by changes at the
9144 start of current_buffer. Value is null if changes start in the
9145 first line of window. */
9146 last_unchanged_at_beg_row = get_last_unchanged_at_beg_row (w);
9147 if (last_unchanged_at_beg_row)
9148 {
9149 init_to_row_end (&it, w, last_unchanged_at_beg_row);
9150 start_pos = it.current.pos;
9151
9152 /* Start displaying new lines in the desired matrix at the same
9153 vpos we would use in the current matrix, i.e. below
9154 last_unchanged_at_beg_row. */
9155 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
9156 current_matrix);
9157 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
9158 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
9159
9160 xassert (it.hpos == 0 && it.current_x == 0);
9161 }
9162 else
9163 {
9164 /* There are no reusable lines at the start of the window.
9165 Start displaying in the first line. */
9166 start_display (&it, w, start);
9167 start_pos = it.current.pos;
9168 }
9169
9170 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
9171 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
9172
9173 /* Find the first row that is not affected by changes at the end of
9174 the buffer. Value will be null if there is no unchanged row, in
9175 which case we must redisplay to the end of the window. delta
9176 will be set to the value by which buffer positions beginning with
9177 first_unchanged_at_end_row have to be adjusted due to text
9178 changes. */
9179 first_unchanged_at_end_row
9180 = get_first_unchanged_at_end_row (w, &delta, &delta_bytes);
9181 IF_DEBUG (debug_delta = delta);
9182 IF_DEBUG (debug_delta_bytes = delta_bytes);
9183
9184 /* Set stop_pos to the buffer position up to which we will have to
9185 display new lines. If first_unchanged_at_end_row != NULL, this
9186 is the buffer position of the start of the line displayed in that
9187 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
9188 that we don't stop at a buffer position. */
9189 stop_pos = 0;
9190 if (first_unchanged_at_end_row)
9191 {
9192 xassert (last_unchanged_at_beg_row == NULL
9193 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
9194
9195 /* If this is a continuation line, move forward to the next one
9196 that isn't. Changes in lines above affect this line.
9197 Caution: this may move first_unchanged_at_end_row to a row
9198 not displaying text. */
9199 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
9200 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
9201 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
9202 < it.last_visible_y))
9203 ++first_unchanged_at_end_row;
9204
9205 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
9206 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
9207 >= it.last_visible_y))
9208 first_unchanged_at_end_row = NULL;
9209 else
9210 {
9211 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
9212 + delta);
9213 first_unchanged_at_end_vpos
9214 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
9215 xassert (stop_pos >= Z - end_unchanged);
9216 }
9217 }
9218 else if (last_unchanged_at_beg_row == NULL)
9219 return 0;
9220
9221
9222 #if GLYPH_DEBUG
9223
9224 /* Either there is no unchanged row at the end, or the one we have
9225 now displays text. This is a necessary condition for the window
9226 end pos calculation at the end of this function. */
9227 xassert (first_unchanged_at_end_row == NULL
9228 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
9229
9230 debug_last_unchanged_at_beg_vpos
9231 = (last_unchanged_at_beg_row
9232 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
9233 : -1);
9234 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
9235
9236 #endif /* GLYPH_DEBUG != 0 */
9237
9238
9239 /* Display new lines. Set last_text_row to the last new line
9240 displayed which has text on it, i.e. might end up as being the
9241 line where the window_end_vpos is. */
9242 w->cursor.vpos = -1;
9243 last_text_row = NULL;
9244 overlay_arrow_seen = 0;
9245 while (it.current_y < it.last_visible_y
9246 && !fonts_changed_p
9247 && (first_unchanged_at_end_row == NULL
9248 || IT_CHARPOS (it) < stop_pos))
9249 {
9250 if (display_line (&it))
9251 last_text_row = it.glyph_row - 1;
9252 }
9253
9254 if (fonts_changed_p)
9255 return -1;
9256
9257
9258 /* Compute differences in buffer positions, y-positions etc. for
9259 lines reused at the bottom of the window. Compute what we can
9260 scroll. */
9261 if (first_unchanged_at_end_row)
9262 {
9263 dvpos = (it.vpos
9264 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
9265 current_matrix));
9266 dy = it.current_y - first_unchanged_at_end_row->y;
9267 run.current_y = first_unchanged_at_end_row->y;
9268 run.desired_y = run.current_y + dy;
9269 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
9270 }
9271 else
9272 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
9273 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
9274
9275
9276 /* Find the cursor if not already found. We have to decide whether
9277 PT will appear on this window (it sometimes doesn't, but this is
9278 not a very frequent case.) This decision has to be made before
9279 the current matrix is altered. A value of cursor.vpos < 0 means
9280 that PT is either in one of the lines beginning at
9281 first_unchanged_at_end_row or below the window. Don't care for
9282 lines that might be displayed later at the window end; as
9283 mentioned, this is not a frequent case. */
9284 if (w->cursor.vpos < 0)
9285 {
9286 int last_y = min (it.last_visible_y, it.last_visible_y + dy);
9287
9288 /* Cursor in unchanged rows at the top? */
9289 if (PT < CHARPOS (start_pos)
9290 && last_unchanged_at_beg_row)
9291 {
9292 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9293 while (row <= last_unchanged_at_beg_row
9294 && MATRIX_ROW_END_CHARPOS (row) <= PT)
9295 ++row;
9296 xassert (row <= last_unchanged_at_beg_row);
9297 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9298 }
9299
9300 /* Start from first_unchanged_at_end_row looking for PT. */
9301 else if (first_unchanged_at_end_row)
9302 {
9303 row = first_unchanged_at_end_row;
9304 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9305 {
9306 if (PT - delta >= MATRIX_ROW_START_CHARPOS (row)
9307 && PT - delta < MATRIX_ROW_END_CHARPOS (row))
9308 {
9309 set_cursor_from_row (w, row, w->current_matrix, delta,
9310 delta_bytes, dy, dvpos);
9311 break;
9312 }
9313 else if (MATRIX_ROW_BOTTOM_Y (row) >= last_y)
9314 break;
9315 ++row;
9316 }
9317 }
9318
9319 /* Give up if cursor was not found. */
9320 if (w->cursor.vpos < 0)
9321 {
9322 clear_glyph_matrix (w->desired_matrix);
9323 return -1;
9324 }
9325 }
9326
9327 /* Don't let the cursor end in the scroll margins. */
9328 {
9329 int this_scroll_margin, cursor_height;
9330
9331 this_scroll_margin = max (0, scroll_margin);
9332 this_scroll_margin = min (this_scroll_margin,
9333 XFASTINT (w->height) / 4);
9334 this_scroll_margin *= CANON_Y_UNIT (it.f);
9335 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
9336
9337 if ((w->cursor.y < this_scroll_margin
9338 && CHARPOS (start) > BEGV)
9339 /* Don't take scroll margin into account at the bottom because
9340 old redisplay didn't do it either. */
9341 || w->cursor.y + cursor_height > it.last_visible_y)
9342 {
9343 w->cursor.vpos = -1;
9344 clear_glyph_matrix (w->desired_matrix);
9345 return -1;
9346 }
9347 }
9348
9349 /* Scroll the display. Do it before changing the current matrix so
9350 that xterm.c doesn't get confused about where the cursor glyph is
9351 found. */
9352 if (dy)
9353 {
9354 update_begin (f);
9355
9356 if (FRAME_WINDOW_P (f))
9357 {
9358 rif->update_window_begin_hook (w);
9359 rif->scroll_run_hook (w, &run);
9360 rif->update_window_end_hook (w, 0);
9361 }
9362 else
9363 {
9364 /* Terminal frame. In this case, dvpos gives the number of
9365 lines to scroll by; dvpos < 0 means scroll up. */
9366 int first_unchanged_at_end_vpos
9367 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
9368 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
9369 int end = XFASTINT (w->top) + window_internal_height (w);
9370
9371 /* Perform the operation on the screen. */
9372 if (dvpos > 0)
9373 {
9374 /* Scroll last_unchanged_at_beg_row to the end of the
9375 window down dvpos lines. */
9376 set_terminal_window (end);
9377
9378 /* On dumb terminals delete dvpos lines at the end
9379 before inserting dvpos empty lines. */
9380 if (!scroll_region_ok)
9381 ins_del_lines (end - dvpos, -dvpos);
9382
9383 /* Insert dvpos empty lines in front of
9384 last_unchanged_at_beg_row. */
9385 ins_del_lines (from, dvpos);
9386 }
9387 else if (dvpos < 0)
9388 {
9389 /* Scroll up last_unchanged_at_beg_vpos to the end of
9390 the window to last_unchanged_at_beg_vpos - |dvpos|. */
9391 set_terminal_window (end);
9392
9393 /* Delete dvpos lines in front of
9394 last_unchanged_at_beg_vpos. ins_del_lines will set
9395 the cursor to the given vpos and emit |dvpos| delete
9396 line sequences. */
9397 ins_del_lines (from + dvpos, dvpos);
9398
9399 /* On a dumb terminal insert dvpos empty lines at the
9400 end. */
9401 if (!scroll_region_ok)
9402 ins_del_lines (end + dvpos, -dvpos);
9403 }
9404
9405 set_terminal_window (0);
9406 }
9407
9408 update_end (f);
9409 }
9410
9411 /* Shift reused rows of the current matrix to the right position. */
9412 if (dvpos < 0)
9413 {
9414 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
9415 bottom_vpos, dvpos);
9416 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
9417 bottom_vpos, 0);
9418 }
9419 else if (dvpos > 0)
9420 {
9421 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
9422 bottom_vpos, dvpos);
9423 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
9424 first_unchanged_at_end_vpos + dvpos, 0);
9425 }
9426
9427 /* For frame-based redisplay, make sure that current frame and window
9428 matrix are in sync with respect to glyph memory. */
9429 if (!FRAME_WINDOW_P (f))
9430 sync_frame_with_window_matrix_rows (w);
9431
9432 /* Adjust buffer positions in reused rows. */
9433 if (delta)
9434 increment_glyph_matrix_buffer_positions (current_matrix,
9435 first_unchanged_at_end_vpos + dvpos,
9436 bottom_vpos, delta, delta_bytes);
9437
9438 /* Adjust Y positions. */
9439 if (dy)
9440 shift_glyph_matrix (w, current_matrix,
9441 first_unchanged_at_end_vpos + dvpos,
9442 bottom_vpos, dy);
9443
9444 if (first_unchanged_at_end_row)
9445 first_unchanged_at_end_row += dvpos;
9446
9447 /* If scrolling up, there may be some lines to display at the end of
9448 the window. */
9449 last_text_row_at_end = NULL;
9450 if (dy < 0)
9451 {
9452 /* Set last_row to the glyph row in the current matrix where the
9453 window end line is found. It has been moved up or down in
9454 the matrix by dvpos. */
9455 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
9456 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
9457
9458 /* If last_row is the window end line, it should display text. */
9459 xassert (last_row->displays_text_p);
9460
9461 /* If window end line was partially visible before, begin
9462 displaying at that line. Otherwise begin displaying with the
9463 line following it. */
9464 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
9465 {
9466 init_to_row_start (&it, w, last_row);
9467 it.vpos = last_vpos;
9468 it.current_y = last_row->y;
9469 }
9470 else
9471 {
9472 init_to_row_end (&it, w, last_row);
9473 it.vpos = 1 + last_vpos;
9474 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
9475 ++last_row;
9476 }
9477
9478 /* We may start in a continuation line. If so, we have to get
9479 the right continuation_lines_width and current_x. */
9480 it.continuation_lines_width = last_row->continuation_lines_width;
9481 it.hpos = it.current_x = 0;
9482
9483 /* Display the rest of the lines at the window end. */
9484 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
9485 while (it.current_y < it.last_visible_y
9486 && !fonts_changed_p)
9487 {
9488 /* Is it always sure that the display agrees with lines in
9489 the current matrix? I don't think so, so we mark rows
9490 displayed invalid in the current matrix by setting their
9491 enabled_p flag to zero. */
9492 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
9493 if (display_line (&it))
9494 last_text_row_at_end = it.glyph_row - 1;
9495 }
9496 }
9497
9498 /* Update window_end_pos and window_end_vpos. */
9499 if (first_unchanged_at_end_row
9500 && first_unchanged_at_end_row->y < it.last_visible_y
9501 && !last_text_row_at_end)
9502 {
9503 /* Window end line if one of the preserved rows from the current
9504 matrix. Set row to the last row displaying text in current
9505 matrix starting at first_unchanged_at_end_row, after
9506 scrolling. */
9507 xassert (first_unchanged_at_end_row->displays_text_p);
9508 row = find_last_row_displaying_text (w->current_matrix, &it,
9509 first_unchanged_at_end_row);
9510 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
9511
9512 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
9513 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
9514 XSETFASTINT (w->window_end_vpos,
9515 MATRIX_ROW_VPOS (row, w->current_matrix));
9516 }
9517 else if (last_text_row_at_end)
9518 {
9519 XSETFASTINT (w->window_end_pos,
9520 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
9521 w->window_end_bytepos
9522 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
9523 XSETFASTINT (w->window_end_vpos,
9524 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
9525 }
9526 else if (last_text_row)
9527 {
9528 /* We have displayed either to the end of the window or at the
9529 end of the window, i.e. the last row with text is to be found
9530 in the desired matrix. */
9531 XSETFASTINT (w->window_end_pos,
9532 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9533 w->window_end_bytepos
9534 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9535 XSETFASTINT (w->window_end_vpos,
9536 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
9537 }
9538 else if (first_unchanged_at_end_row == NULL
9539 && last_text_row == NULL
9540 && last_text_row_at_end == NULL)
9541 {
9542 /* Displayed to end of window, but no line containing text was
9543 displayed. Lines were deleted at the end of the window. */
9544 int vpos;
9545 int top_line_p = WINDOW_WANTS_TOP_LINE_P (w) ? 1 : 0;
9546
9547 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
9548 if ((w->desired_matrix->rows[vpos + top_line_p].enabled_p
9549 && w->desired_matrix->rows[vpos + top_line_p].displays_text_p)
9550 || (!w->desired_matrix->rows[vpos + top_line_p].enabled_p
9551 && w->current_matrix->rows[vpos + top_line_p].displays_text_p))
9552 break;
9553
9554 w->window_end_vpos = make_number (vpos);
9555 }
9556 else
9557 abort ();
9558
9559 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
9560 debug_end_vpos = XFASTINT (w->window_end_vpos));
9561
9562 /* Record that display has not been completed. */
9563 w->window_end_valid = Qnil;
9564 w->desired_matrix->no_scrolling_p = 1;
9565 return 1;
9566 }
9567
9568
9569 \f
9570 /***********************************************************************
9571 More debugging support
9572 ***********************************************************************/
9573
9574 #if GLYPH_DEBUG
9575
9576 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
9577 static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
9578
9579
9580 /* Dump the contents of glyph matrix MATRIX on stderr. If
9581 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
9582
9583 void
9584 dump_glyph_matrix (matrix, with_glyphs_p)
9585 struct glyph_matrix *matrix;
9586 int with_glyphs_p;
9587 {
9588 int i;
9589 for (i = 0; i < matrix->nrows; ++i)
9590 dump_glyph_row (matrix, i, with_glyphs_p);
9591 }
9592
9593
9594 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
9595 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
9596
9597 void
9598 dump_glyph_row (matrix, vpos, with_glyphs_p)
9599 struct glyph_matrix *matrix;
9600 int vpos, with_glyphs_p;
9601 {
9602 struct glyph_row *row;
9603
9604 if (vpos < 0 || vpos >= matrix->nrows)
9605 return;
9606
9607 row = MATRIX_ROW (matrix, vpos);
9608
9609 fprintf (stderr, "Row Start End Used oEI><O\\CTZF X Y W\n");
9610 fprintf (stderr, "=============================================\n");
9611
9612 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",
9613 row - matrix->rows,
9614 MATRIX_ROW_START_CHARPOS (row),
9615 MATRIX_ROW_END_CHARPOS (row),
9616 row->used[TEXT_AREA],
9617 row->contains_overlapping_glyphs_p,
9618 row->enabled_p,
9619 row->inverse_p,
9620 row->truncated_on_left_p,
9621 row->truncated_on_right_p,
9622 row->overlay_arrow_p,
9623 row->continued_p,
9624 MATRIX_ROW_CONTINUATION_LINE_P (row),
9625 row->displays_text_p,
9626 row->ends_at_zv_p,
9627 row->fill_line_p,
9628 row->x,
9629 row->y,
9630 row->pixel_width);
9631 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
9632 row->end.overlay_string_index);
9633 fprintf (stderr, "%9d %5d\n",
9634 CHARPOS (row->start.string_pos),
9635 CHARPOS (row->end.string_pos));
9636 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
9637 row->end.dpvec_index);
9638
9639 if (with_glyphs_p)
9640 {
9641 struct glyph *glyph, *glyph_end;
9642 int prev_had_glyphs_p;
9643
9644 glyph = row->glyphs[TEXT_AREA];
9645 glyph_end = glyph + row->used[TEXT_AREA];
9646
9647 /* Glyph for a line end in text. */
9648 if (glyph == glyph_end && glyph->charpos > 0)
9649 ++glyph_end;
9650
9651 if (glyph < glyph_end)
9652 {
9653 fprintf (stderr, " Glyph Type Pos W Code C Face LR\n");
9654 prev_had_glyphs_p = 1;
9655 }
9656 else
9657 prev_had_glyphs_p = 0;
9658
9659 while (glyph < glyph_end)
9660 {
9661 if (glyph->type == CHAR_GLYPH)
9662 {
9663 fprintf (stderr,
9664 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
9665 glyph - row->glyphs[TEXT_AREA],
9666 'C',
9667 glyph->charpos,
9668 glyph->pixel_width,
9669 glyph->u.ch.code,
9670 (glyph->u.ch.code < 0x80 && glyph->u.ch.code >= ' '
9671 ? glyph->u.ch.code
9672 : '.'),
9673 glyph->u.ch.face_id,
9674 glyph->left_box_line_p,
9675 glyph->right_box_line_p);
9676 }
9677 else if (glyph->type == STRETCH_GLYPH)
9678 {
9679 fprintf (stderr,
9680 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
9681 glyph - row->glyphs[TEXT_AREA],
9682 'S',
9683 glyph->charpos,
9684 glyph->pixel_width,
9685 0,
9686 '.',
9687 glyph->u.stretch.face_id,
9688 glyph->left_box_line_p,
9689 glyph->right_box_line_p);
9690 }
9691 else if (glyph->type == IMAGE_GLYPH)
9692 {
9693 fprintf (stderr,
9694 " %5d %4c %6d %3d 0x%05x %c %4d %1.1d%1.1d\n",
9695 glyph - row->glyphs[TEXT_AREA],
9696 'I',
9697 glyph->charpos,
9698 glyph->pixel_width,
9699 glyph->u.img.id,
9700 '.',
9701 glyph->u.img.face_id,
9702 glyph->left_box_line_p,
9703 glyph->right_box_line_p);
9704 }
9705 ++glyph;
9706 }
9707 }
9708 }
9709
9710
9711 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
9712 Sdump_glyph_matrix, 0, 1, "p",
9713 "Dump the current matrix of the selected window to stderr.\n\
9714 Shows contents of glyph row structures. With non-nil optional\n\
9715 parameter WITH-GLYPHS-P, dump glyphs as well.")
9716 (with_glyphs_p)
9717 {
9718 struct window *w = XWINDOW (selected_window);
9719 struct buffer *buffer = XBUFFER (w->buffer);
9720
9721 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
9722 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
9723 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
9724 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
9725 fprintf (stderr, "=============================================\n");
9726 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
9727 return Qnil;
9728 }
9729
9730
9731 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
9732 "Dump glyph row ROW to stderr.")
9733 (row)
9734 Lisp_Object row;
9735 {
9736 CHECK_NUMBER (row, 0);
9737 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
9738 return Qnil;
9739 }
9740
9741
9742 DEFUN ("dump-toolbar-row", Fdump_toolbar_row, Sdump_toolbar_row,
9743 0, 0, "", "")
9744 ()
9745 {
9746 struct glyph_matrix *m = (XWINDOW (selected_frame->toolbar_window)
9747 ->current_matrix);
9748 dump_glyph_row (m, 0, 1);
9749 return Qnil;
9750 }
9751
9752
9753 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
9754 Strace_redisplay_toggle, 0, 0, "",
9755 "Toggle tracing of redisplay.")
9756 ()
9757 {
9758 trace_redisplay_p = !trace_redisplay_p;
9759 return Qnil;
9760 }
9761
9762
9763 #endif /* GLYPH_DEBUG */
9764
9765
9766 \f
9767 /***********************************************************************
9768 Building Desired Matrix Rows
9769 ***********************************************************************/
9770
9771 /* Return a temporary glyph row holding the glyphs of an overlay
9772 arrow. Only used for non-window-redisplay windows. */
9773
9774 static struct glyph_row *
9775 get_overlay_arrow_glyph_row (w)
9776 struct window *w;
9777 {
9778 struct frame *f = XFRAME (WINDOW_FRAME (w));
9779 struct buffer *buffer = XBUFFER (w->buffer);
9780 struct buffer *old = current_buffer;
9781 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
9782 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
9783 unsigned char *arrow_end = arrow_string + arrow_len;
9784 unsigned char *p;
9785 struct it it;
9786 int multibyte_p;
9787 int n_glyphs_before;
9788
9789 set_buffer_temp (buffer);
9790 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
9791 it.glyph_row->used[TEXT_AREA] = 0;
9792 SET_TEXT_POS (it.position, 0, 0);
9793
9794 multibyte_p = !NILP (buffer->enable_multibyte_characters);
9795 p = arrow_string;
9796 while (p < arrow_end)
9797 {
9798 Lisp_Object face, ilisp;
9799
9800 /* Get the next character. */
9801 if (multibyte_p)
9802 it.c = string_char_and_length (p, arrow_len, &it.len);
9803 else
9804 it.c = *p, it.len = 1;
9805 p += it.len;
9806
9807 /* Get its face. */
9808 XSETFASTINT (ilisp, p - arrow_string);
9809 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
9810 it.face_id = compute_char_face (f, it.c, face);
9811
9812 /* Compute its width, get its glyphs. */
9813 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
9814 PRODUCE_GLYPHS (&it);
9815
9816 /* If this character doesn't fit any more in the line, we have
9817 to remove some glyphs. */
9818 if (it.current_x > it.last_visible_x)
9819 {
9820 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
9821 break;
9822 }
9823 }
9824
9825 set_buffer_temp (old);
9826 return it.glyph_row;
9827 }
9828
9829
9830 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
9831 glyphs are only inserted for terminal frames since we can't really
9832 win with truncation glyphs when partially visible glyphs are
9833 involved. Which glyphs to insert is determined by
9834 produce_special_glyphs. */
9835
9836 static void
9837 insert_left_trunc_glyphs (it)
9838 struct it *it;
9839 {
9840 struct it truncate_it;
9841 struct glyph *from, *end, *to, *toend;
9842
9843 xassert (!FRAME_WINDOW_P (it->f));
9844
9845 /* Get the truncation glyphs. */
9846 truncate_it = *it;
9847 truncate_it.charset = -1;
9848 truncate_it.current_x = 0;
9849 truncate_it.face_id = DEFAULT_FACE_ID;
9850 truncate_it.glyph_row = &scratch_glyph_row;
9851 truncate_it.glyph_row->used[TEXT_AREA] = 0;
9852 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
9853 truncate_it.object = 0;
9854 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
9855
9856 /* Overwrite glyphs from IT with truncation glyphs. */
9857 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
9858 end = from + truncate_it.glyph_row->used[TEXT_AREA];
9859 to = it->glyph_row->glyphs[TEXT_AREA];
9860 toend = to + it->glyph_row->used[TEXT_AREA];
9861
9862 while (from < end)
9863 *to++ = *from++;
9864
9865 /* There may be padding glyphs left over. Remove them. */
9866 from = to;
9867 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
9868 ++from;
9869 while (from < toend)
9870 *to++ = *from++;
9871
9872 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
9873 }
9874
9875
9876 /* Compute the pixel height and width of IT->glyph_row.
9877
9878 Most of the time, ascent and height of a display line will be equal
9879 to the max_ascent and max_height values of the display iterator
9880 structure. This is not the case if
9881
9882 1. We hit ZV without displaying anything. In this case, max_ascent
9883 and max_height will be zero.
9884
9885 2. We have some glyphs that don't contribute to the line height.
9886 (The glyph row flag contributes_to_line_height_p is for future
9887 pixmap extensions).
9888
9889 The first case is easily covered by using default values because in
9890 these cases, the line height does not really matter, except that it
9891 must not be zero. */
9892
9893 static void
9894 compute_line_metrics (it)
9895 struct it *it;
9896 {
9897 struct glyph_row *row = it->glyph_row;
9898 int area, i;
9899
9900 if (FRAME_WINDOW_P (it->f))
9901 {
9902 int i, top_line_height;
9903
9904 /* The line may consist of one space only, that was added to
9905 place the cursor on it. If so, the row's height hasn't been
9906 computed yet. */
9907 if (row->height == 0)
9908 {
9909 if (it->max_ascent + it->max_descent == 0)
9910 it->max_descent = CANON_Y_UNIT (it->f);
9911 row->ascent = it->max_ascent;
9912 row->height = it->max_ascent + it->max_descent;
9913 }
9914
9915 /* Compute the width of this line. */
9916 row->pixel_width = row->x;
9917 for (i = 0; i < row->used[TEXT_AREA]; ++i)
9918 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
9919
9920 xassert (row->pixel_width >= 0);
9921 xassert (row->ascent >= 0 && row->height > 0);
9922
9923 /* Compute how much of the line is visible. */
9924 row->visible_height = row->height;
9925
9926 top_line_height = WINDOW_DISPLAY_TOP_LINE_HEIGHT (it->w);
9927 if (row->y < top_line_height)
9928 row->visible_height -= top_line_height - row->y;
9929 else
9930 {
9931 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
9932 if (row->y + row->height > max_y)
9933 row->visible_height -= row->y + row->height - max_y;
9934 }
9935 }
9936 else
9937 {
9938 row->pixel_width = row->used[TEXT_AREA];
9939 row->ascent = 0;
9940 row->height = row->visible_height = 1;
9941 }
9942
9943 /* Compute a hash code for this row. */
9944 row->hash = 0;
9945 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
9946 for (i = 0; i < row->used[area]; ++i)
9947 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
9948 + row->glyphs[area][i].u.val
9949 + (row->glyphs[area][i].type << 2));
9950
9951 it->max_ascent = it->max_descent = 0;
9952 }
9953
9954
9955 /* Append one space to the glyph row of iterator IT if doing a
9956 window-based redisplay. DEFAULT_FACE_P non-zero means let the
9957 space have the default face, otherwise let it have the same face as
9958 IT->face_id. This function is called to make sure that there is
9959 always one glyph at the end of a glyph row that the cursor can be
9960 set on under window-systems. (If there weren't such a glyph we
9961 would not know how wide and tall the cursor should be displayed). */
9962
9963 static void
9964 append_space (it, default_face_p)
9965 struct it *it;
9966 int default_face_p;
9967 {
9968 if (FRAME_WINDOW_P (it->f))
9969 {
9970 int n = it->glyph_row->used[TEXT_AREA];
9971
9972 if (it->glyph_row->glyphs[TEXT_AREA] + n
9973 < it->glyph_row->glyphs[1 + TEXT_AREA])
9974 {
9975 /* Save some values that must not be changed. */
9976 int saved_x = it->current_x;
9977 struct text_pos saved_pos;
9978 int saved_what = it->what;
9979 int saved_face_id = it->face_id;
9980 int saved_charset = it->charset;
9981 Lisp_Object saved_object;
9982
9983 saved_object = it->object;
9984 saved_pos = it->position;
9985
9986 it->what = IT_CHARACTER;
9987 bzero (&it->position, sizeof it->position);
9988 it->object = 0;
9989 it->c = ' ';
9990 it->len = 1;
9991 it->charset = CHARSET_ASCII;
9992
9993 if (default_face_p)
9994 it->face_id = DEFAULT_FACE_ID;
9995 if (it->multibyte_p)
9996 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, CHARSET_ASCII);
9997 else
9998 it->face_id = FACE_FOR_CHARSET (it->f, it->face_id, -1);
9999
10000 PRODUCE_GLYPHS (it);
10001
10002 it->current_x = saved_x;
10003 it->object = saved_object;
10004 it->position = saved_pos;
10005 it->what = saved_what;
10006 it->face_id = saved_face_id;
10007 it->charset = saved_charset;
10008 }
10009 }
10010 }
10011
10012
10013 /* Extend the face of the last glyph in the text area of IT->glyph_row
10014 to the end of the display line. Called from display_line.
10015 If the glyph row is empty, add a space glyph to it so that we
10016 know the face to draw. Set the glyph row flag fill_line_p. */
10017
10018 static void
10019 extend_face_to_end_of_line (it)
10020 struct it *it;
10021 {
10022 struct face *face;
10023 struct frame *f = it->f;
10024
10025 /* If line is already filled, do nothing. */
10026 if (it->current_x >= it->last_visible_x)
10027 return;
10028
10029 /* Face extension extends the background and box of IT->face_id
10030 to the end of the line. If the background equals the background
10031 of the frame, we haven't to do anything. */
10032 face = FACE_FROM_ID (f, it->face_id);
10033 if (FRAME_WINDOW_P (f)
10034 && face->box == FACE_NO_BOX
10035 && face->background == FRAME_BACKGROUND_PIXEL (f)
10036 && !face->stipple)
10037 return;
10038
10039 /* Set the glyph row flag indicating that the face of the last glyph
10040 in the text area has to be drawn to the end of the text area. */
10041 it->glyph_row->fill_line_p = 1;
10042
10043 /* If current charset of IT is not ASCII, make sure we have the
10044 ASCII face. This will be automatically undone the next time
10045 get_next_display_element returns a character from a different
10046 charset. Note that the charset will always be ASCII in unibyte
10047 text. */
10048 if (it->charset != CHARSET_ASCII)
10049 {
10050 it->charset = CHARSET_ASCII;
10051 it->face_id = FACE_FOR_CHARSET (f, it->face_id, CHARSET_ASCII);
10052 }
10053
10054 if (FRAME_WINDOW_P (f))
10055 {
10056 /* If the row is empty, add a space with the current face of IT,
10057 so that we know which face to draw. */
10058 if (it->glyph_row->used[TEXT_AREA] == 0)
10059 {
10060 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
10061 it->glyph_row->glyphs[TEXT_AREA][0].u.ch.face_id = it->face_id;
10062 it->glyph_row->used[TEXT_AREA] = 1;
10063 }
10064 }
10065 else
10066 {
10067 /* Save some values that must not be changed. */
10068 int saved_x = it->current_x;
10069 struct text_pos saved_pos;
10070 Lisp_Object saved_object;
10071 int saved_what = it->what;
10072
10073 saved_object = it->object;
10074 saved_pos = it->position;
10075
10076 it->what = IT_CHARACTER;
10077 bzero (&it->position, sizeof it->position);
10078 it->object = 0;
10079 it->c = ' ';
10080 it->len = 1;
10081
10082 PRODUCE_GLYPHS (it);
10083
10084 while (it->current_x <= it->last_visible_x)
10085 PRODUCE_GLYPHS (it);
10086
10087 /* Don't count these blanks really. It would let us insert a left
10088 truncation glyph below and make us set the cursor on them, maybe. */
10089 it->current_x = saved_x;
10090 it->object = saved_object;
10091 it->position = saved_pos;
10092 it->what = saved_what;
10093 }
10094 }
10095
10096
10097 /* Value is non-zero if text starting at CHARPOS in current_buffer is
10098 trailing whitespace. */
10099
10100 static int
10101 trailing_whitespace_p (charpos)
10102 int charpos;
10103 {
10104 int bytepos = CHAR_TO_BYTE (charpos);
10105 int c = 0;
10106
10107 while (bytepos < ZV_BYTE
10108 && (c = FETCH_CHAR (bytepos),
10109 c == ' ' || c == '\t'))
10110 ++bytepos;
10111
10112 return bytepos >= ZV_BYTE || c == '\n' || c == '\r';
10113 }
10114
10115
10116 /* Highlight trailing whitespace, if any, in ROW. */
10117
10118 void
10119 highlight_trailing_whitespace (f, row)
10120 struct frame *f;
10121 struct glyph_row *row;
10122 {
10123 int used = row->used[TEXT_AREA];
10124
10125 if (used)
10126 {
10127 struct glyph *start = row->glyphs[TEXT_AREA];
10128 struct glyph *glyph = start + used - 1;
10129
10130 /* Skip over the space glyph inserted to display the
10131 cursor at the end of a line. */
10132 if (glyph->type == CHAR_GLYPH
10133 && glyph->u.ch.code == ' '
10134 && glyph->object == 0)
10135 --glyph;
10136
10137 /* If last glyph is a space or stretch, and it's trailing
10138 whitespace, set the face of all trailing whitespace glyphs in
10139 IT->glyph_row to `trailing-whitespace'. */
10140 if (glyph >= start
10141 && BUFFERP (glyph->object)
10142 && (glyph->type == STRETCH_GLYPH
10143 || (glyph->type == CHAR_GLYPH
10144 && glyph->u.ch.code == ' '))
10145 && trailing_whitespace_p (glyph->charpos))
10146 {
10147 int face_id = lookup_named_face (f, Qtrailing_whitespace,
10148 CHARSET_ASCII);
10149
10150 while (glyph >= start
10151 && BUFFERP (glyph->object)
10152 && (glyph->type == STRETCH_GLYPH
10153 || (glyph->type == CHAR_GLYPH
10154 && glyph->u.ch.code == ' ')))
10155 {
10156 if (glyph->type == STRETCH_GLYPH)
10157 glyph->u.stretch.face_id = face_id;
10158 else
10159 glyph->u.ch.face_id = face_id;
10160 --glyph;
10161 }
10162 }
10163 }
10164 }
10165
10166
10167
10168 /* Construct the glyph row IT->glyph_row in the desired matrix of
10169 IT->w from text at the current position of IT. See dispextern.h
10170 for an overview of struct it. Value is non-zero if
10171 IT->glyph_row displays text, as opposed to a line displaying ZV
10172 only. */
10173
10174 static int
10175 display_line (it)
10176 struct it *it;
10177 {
10178 struct glyph_row *row = it->glyph_row;
10179
10180 /* We always start displaying at hpos zero even if hscrolled. */
10181 xassert (it->hpos == 0 && it->current_x == 0);
10182
10183 /* We must not display in a row that's not a text row. */
10184 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
10185 < it->w->desired_matrix->nrows);
10186
10187 /* Is IT->w showing the region? */
10188 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
10189
10190 /* Clear the result glyph row and enable it. */
10191 prepare_desired_row (row);
10192
10193 row->y = it->current_y;
10194 row->start = it->current;
10195 row->continuation_lines_width = it->continuation_lines_width;
10196 row->displays_text_p = 1;
10197
10198 /* Arrange the overlays nicely for our purposes. Usually, we call
10199 display_line on only one line at a time, in which case this
10200 can't really hurt too much, or we call it on lines which appear
10201 one after another in the buffer, in which case all calls to
10202 recenter_overlay_lists but the first will be pretty cheap. */
10203 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
10204
10205 #if NO_PROMPT_IN_BUFFER
10206 /* Show mini-buffer prompt, if at the beginning of a mini-buffer
10207 window. */
10208 if (MINI_WINDOW_P (it->w)
10209 && MATRIX_ROW_START_CHARPOS (row) == BEG
10210 && it->vpos == 0)
10211 {
10212 if (NILP (minibuf_prompt))
10213 minibuf_prompt_width = minibuf_prompt_pixel_width = 0;
10214 else
10215 {
10216 /* We would like to truncate the prompt a little bit before
10217 the right margin of the window, so that user input can
10218 start on the first line. Set max_x to this position. */
10219 int max_x = (it->last_visible_x - 4 * CANON_X_UNIT (it->f));
10220
10221 /* We use a temporary iterator different from IT so that
10222 IT's settings are not overwritten when displaying
10223 the prompt. */
10224 struct it ti;
10225
10226 ti = *it;
10227
10228 /* Display the prompt. Set minibuf_prompt_width to the
10229 number of glyphs generated for the prompt, set
10230 minibuf_prompt_pixel_width to its width in pixels. */
10231 xassert (it->current_x == 0);
10232 display_string (NULL, minibuf_prompt, Qnil, 0, 0, &ti,
10233 0, 0, max_x, -1);
10234 minibuf_prompt_width = ti.hpos;
10235 minibuf_prompt_pixel_width = ti.current_x;
10236
10237 /* Transfer pixel and hpos information to IT. */
10238 it->hpos = ti.hpos;
10239 it->current_x = ti.current_x;
10240 }
10241 }
10242 #endif /* NO_PROMPT_IN_BUFFER */
10243
10244 /* Move over display elements that are not visible because we are
10245 hscrolled. This may stop at an x-position < IT->first_visible_x
10246 if the first glyph is partially visible or if we hit a line end. */
10247 if (it->current_x < it->first_visible_x)
10248 move_it_in_display_line_to (it, ZV, it->first_visible_x,
10249 MOVE_TO_POS | MOVE_TO_X);
10250
10251 /* Get the initial row height. This is either the height of the
10252 text hscrolled, if there is any, or zero. */
10253 row->ascent = it->max_ascent;
10254 row->height = it->max_ascent + it->max_descent;
10255
10256 /* Loop generating characters. The loop is left with IT on the next
10257 character to display. */
10258 while (1)
10259 {
10260 int n_glyphs_before, hpos_before, x_before;
10261 int x, i, nglyphs;
10262
10263 /* Retrieve the next thing to display. Value is zero if end of
10264 buffer reached. */
10265 if (!get_next_display_element (it))
10266 {
10267 /* Maybe add a space at the end of this line that is used to
10268 display the cursor there under X. */
10269 append_space (it, 1);
10270
10271 /* The position -1 below indicates a blank line not
10272 corresponding to any text, as opposed to an empty line
10273 corresponding to a line end. */
10274 if (row->used[TEXT_AREA] <= 1)
10275 {
10276 row->glyphs[TEXT_AREA]->charpos = -1;
10277 row->displays_text_p = 0;
10278
10279 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
10280 row->indicate_empty_line_p = 1;
10281 }
10282
10283 it->continuation_lines_width = 0;
10284 row->ends_at_zv_p = 1;
10285 break;
10286 }
10287
10288 /* Now, get the metrics of what we want to display. This also
10289 generates glyphs in `row' (which is IT->glyph_row). */
10290 n_glyphs_before = row->used[TEXT_AREA];
10291 x = it->current_x;
10292 PRODUCE_GLYPHS (it);
10293
10294 /* If this display element was in marginal areas, continue with
10295 the next one. */
10296 if (it->area != TEXT_AREA)
10297 {
10298 row->ascent = max (row->ascent, it->max_ascent);
10299 row->height = max (row->height, it->max_ascent + it->max_descent);
10300 set_iterator_to_next (it);
10301 continue;
10302 }
10303
10304 /* Does the display element fit on the line? If we truncate
10305 lines, we should draw past the right edge of the window. If
10306 we don't truncate, we want to stop so that we can display the
10307 continuation glyph before the right margin. If lines are
10308 continued, there are two possible strategies for characters
10309 resulting in more than 1 glyph (e.g. tabs): Display as many
10310 glyphs as possible in this line and leave the rest for the
10311 continuation line, or display the whole element in the next
10312 line. Original redisplay did the former, so we do it also. */
10313 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10314 hpos_before = it->hpos;
10315 x_before = x;
10316
10317 if (nglyphs == 1
10318 && it->current_x < it->last_visible_x)
10319 {
10320 ++it->hpos;
10321 row->ascent = max (row->ascent, it->max_ascent);
10322 row->height = max (row->height, it->max_ascent + it->max_descent);
10323 if (it->current_x - it->pixel_width < it->first_visible_x)
10324 row->x = x - it->first_visible_x;
10325 }
10326 else
10327 {
10328 int new_x;
10329 struct glyph *glyph;
10330
10331 for (i = 0; i < nglyphs; ++i, x = new_x)
10332 {
10333 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10334 new_x = x + glyph->pixel_width;
10335
10336 if (/* Lines are continued. */
10337 !it->truncate_lines_p
10338 && (/* Glyph doesn't fit on the line. */
10339 new_x > it->last_visible_x
10340 /* Or it fits exactly on a window system frame. */
10341 || (new_x == it->last_visible_x
10342 && FRAME_WINDOW_P (it->f))))
10343 {
10344 /* End of a continued line. */
10345
10346 if (it->hpos == 0
10347 || (new_x == it->last_visible_x
10348 && FRAME_WINDOW_P (it->f)))
10349 {
10350 /* Current glyph fits exactly on the line. We
10351 must continue the line because we can't draw
10352 the cursor after the glyph. */
10353 row->continued_p = 1;
10354 it->current_x = new_x;
10355 it->continuation_lines_width += new_x;
10356 ++it->hpos;
10357 if (i == nglyphs - 1)
10358 set_iterator_to_next (it);
10359 }
10360 else
10361 {
10362 /* Display element draws past the right edge of
10363 the window. Restore positions to values
10364 before the element. The next line starts
10365 with current_x before the glyph that could
10366 not be displayed, so that TAB works right. */
10367 row->used[TEXT_AREA] = n_glyphs_before + i;
10368
10369 /* Display continuation glyphs. */
10370 if (!FRAME_WINDOW_P (it->f))
10371 produce_special_glyphs (it, IT_CONTINUATION);
10372 row->continued_p = 1;
10373
10374 it->current_x = x;
10375 it->continuation_lines_width += x;
10376 }
10377 break;
10378 }
10379 else if (new_x > it->first_visible_x)
10380 {
10381 /* Increment number of glyphs actually displayed. */
10382 ++it->hpos;
10383
10384 if (x < it->first_visible_x)
10385 /* Glyph is partially visible, i.e. row starts at
10386 negative X position. */
10387 row->x = x - it->first_visible_x;
10388 }
10389 else
10390 {
10391 /* Glyph is completely off the left margin of the
10392 window. This should not happen because of the
10393 move_it_in_display_line at the start of
10394 this function. */
10395 abort ();
10396 }
10397 }
10398
10399 row->ascent = max (row->ascent, it->max_ascent);
10400 row->height = max (row->height, it->max_ascent + it->max_descent);
10401
10402 /* End of this display line if row is continued. */
10403 if (row->continued_p)
10404 break;
10405 }
10406
10407 /* Is this a line end? If yes, we're also done, after making
10408 sure that a non-default face is extended up to the right
10409 margin of the window. */
10410 if (ITERATOR_AT_END_OF_LINE_P (it))
10411 {
10412 int used_before = row->used[TEXT_AREA];
10413
10414 /* Add a space at the end of the line that is used to
10415 display the cursor there. */
10416 append_space (it, 0);
10417
10418 /* Extend the face to the end of the line. */
10419 extend_face_to_end_of_line (it);
10420
10421 /* Make sure we have the position. */
10422 if (used_before == 0)
10423 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
10424
10425 /* Consume the line end. This skips over invisible lines. */
10426 set_iterator_to_next (it);
10427 it->continuation_lines_width = 0;
10428 break;
10429 }
10430
10431 /* Proceed with next display element. Note that this skips
10432 over lines invisible because of selective display. */
10433 set_iterator_to_next (it);
10434
10435 /* If we truncate lines, we are done when the last displayed
10436 glyphs reach past the right margin of the window. */
10437 if (it->truncate_lines_p
10438 && (FRAME_WINDOW_P (it->f)
10439 ? (it->current_x >= it->last_visible_x)
10440 : (it->current_x > it->last_visible_x)))
10441 {
10442 /* Maybe add truncation glyphs. */
10443 if (!FRAME_WINDOW_P (it->f))
10444 {
10445 --it->glyph_row->used[TEXT_AREA];
10446 produce_special_glyphs (it, IT_TRUNCATION);
10447 }
10448
10449 row->truncated_on_right_p = 1;
10450 it->continuation_lines_width = 0;
10451 reseat_at_next_visible_line_start (it);
10452 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
10453 it->hpos = hpos_before;
10454 it->current_x = x_before;
10455 break;
10456 }
10457 }
10458
10459 /* If line is not empty and hscrolled, maybe insert truncation glyphs
10460 at the left window margin. */
10461 if (it->first_visible_x
10462 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
10463 {
10464 if (!FRAME_WINDOW_P (it->f))
10465 insert_left_trunc_glyphs (it);
10466 row->truncated_on_left_p = 1;
10467 }
10468
10469 /* If the start of this line is the overlay arrow-position, then
10470 mark this glyph row as the one containing the overlay arrow.
10471 This is clearly a mess with variable size fonts. It would be
10472 better to let it be displayed like cursors under X. */
10473 if (MARKERP (Voverlay_arrow_position)
10474 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
10475 && (MATRIX_ROW_START_CHARPOS (row)
10476 == marker_position (Voverlay_arrow_position))
10477 && STRINGP (Voverlay_arrow_string)
10478 && ! overlay_arrow_seen)
10479 {
10480 /* Overlay arrow in window redisplay is a bitmap. */
10481 if (!FRAME_WINDOW_P (it->f))
10482 {
10483 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
10484 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
10485 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
10486 struct glyph *p = row->glyphs[TEXT_AREA];
10487 struct glyph *p2, *end;
10488
10489 /* Copy the arrow glyphs. */
10490 while (glyph < arrow_end)
10491 *p++ = *glyph++;
10492
10493 /* Throw away padding glyphs. */
10494 p2 = p;
10495 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
10496 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
10497 ++p2;
10498 if (p2 > p)
10499 {
10500 while (p2 < end)
10501 *p++ = *p2++;
10502 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
10503 }
10504 }
10505
10506 overlay_arrow_seen = 1;
10507 row->overlay_arrow_p = 1;
10508 }
10509
10510 /* Compute pixel dimensions of this line. */
10511 compute_line_metrics (it);
10512
10513 /* Remember the position at which this line ends. */
10514 row->end = it->current;
10515
10516 /* Maybe set the cursor. If you change this, it's probably a good
10517 idea to also change the code in redisplay_window for cursor
10518 movement in an unchanged window. */
10519 if (it->w->cursor.vpos < 0
10520 && PT >= MATRIX_ROW_START_CHARPOS (row)
10521 && MATRIX_ROW_END_CHARPOS (row) >= PT
10522 && !(MATRIX_ROW_END_CHARPOS (row) == PT
10523 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
10524 || !row->ends_at_zv_p)))
10525 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
10526
10527 /* Highlight trailing whitespace. */
10528 if (it->show_trailing_whitespace_p)
10529 highlight_trailing_whitespace (it->f, it->glyph_row);
10530
10531 /* Prepare for the next line. This line starts horizontally at (X
10532 HPOS) = (0 0). Vertical positions are incremented. As a
10533 convenience for the caller, IT->glyph_row is set to the next
10534 row to be used. */
10535 it->current_x = it->hpos = 0;
10536 it->current_y += row->height;
10537 ++it->vpos;
10538 ++it->glyph_row;
10539 return row->displays_text_p;
10540 }
10541
10542
10543 \f
10544 /***********************************************************************
10545 Menu Bar
10546 ***********************************************************************/
10547
10548 /* Redisplay the menu bar in the frame for window W.
10549
10550 The menu bar of X frames that don't have X toolkit support is
10551 displayed in a special window W->frame->menu_bar_window.
10552
10553 The menu bar of terminal frames is treated specially as far as
10554 glyph matrices are concerned. Menu bar lines are not part of
10555 windows, so the update is done directly on the frame matrix rows
10556 for the menu bar. */
10557
10558 static void
10559 display_menu_bar (w)
10560 struct window *w;
10561 {
10562 struct frame *f = XFRAME (WINDOW_FRAME (w));
10563 struct it it;
10564 Lisp_Object items;
10565 int i;
10566
10567 /* Don't do all this for graphical frames. */
10568 #ifdef HAVE_NTGUI
10569 if (!NILP (Vwindow_system))
10570 return;
10571 #endif
10572 #ifdef USE_X_TOOLKIT
10573 if (FRAME_X_P (f))
10574 return;
10575 #endif
10576
10577 #ifdef USE_X_TOOLKIT
10578 xassert (!FRAME_WINDOW_P (f));
10579 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MODE_LINE_FACE_ID);
10580 it.first_visible_x = 0;
10581 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
10582 #else /* not USE_X_TOOLKIT */
10583 if (FRAME_WINDOW_P (f))
10584 {
10585 /* Menu bar lines are displayed in the desired matrix of the
10586 dummy window menu_bar_window. */
10587 struct window *menu_w;
10588 xassert (WINDOWP (f->menu_bar_window));
10589 menu_w = XWINDOW (f->menu_bar_window);
10590 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
10591 MODE_LINE_FACE_ID);
10592 it.first_visible_x = 0;
10593 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
10594 }
10595 else
10596 {
10597 /* This is a TTY frame, i.e. character hpos/vpos are used as
10598 pixel x/y. */
10599 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
10600 MODE_LINE_FACE_ID);
10601 it.first_visible_x = 0;
10602 it.last_visible_x = FRAME_WIDTH (f);
10603 }
10604 #endif /* not USE_X_TOOLKIT */
10605
10606 /* Clear all rows of the menu bar. */
10607 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
10608 {
10609 struct glyph_row *row = it.glyph_row + i;
10610 clear_glyph_row (row);
10611 row->enabled_p = 1;
10612 row->full_width_p = 1;
10613 }
10614
10615 /* Make the first line of the menu bar appear in reverse video. */
10616 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
10617
10618 /* Display all items of the menu bar. */
10619 items = FRAME_MENU_BAR_ITEMS (it.f);
10620 for (i = 0; i < XVECTOR (items)->size; i += 4)
10621 {
10622 Lisp_Object string;
10623
10624 /* Stop at nil string. */
10625 string = XVECTOR (items)->contents[i + 1];
10626 if (NILP (string))
10627 break;
10628
10629 /* Remember where item was displayed. */
10630 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
10631
10632 /* Display the item, pad with one space. */
10633 if (it.current_x < it.last_visible_x)
10634 display_string (NULL, string, Qnil, 0, 0, &it,
10635 XSTRING (string)->size + 1, 0, 0, -1);
10636 }
10637
10638 /* Fill out the line with spaces. */
10639 if (it.current_x < it.last_visible_x)
10640 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
10641
10642 /* Compute the total height of the lines. */
10643 compute_line_metrics (&it);
10644 }
10645
10646
10647 \f
10648 /***********************************************************************
10649 Mode Line
10650 ***********************************************************************/
10651
10652 /* Display the mode and/or top line of window W. */
10653
10654 static void
10655 display_mode_lines (w)
10656 struct window *w;
10657 {
10658 /* These will be set while the mode line specs are processed. */
10659 line_number_displayed = 0;
10660 w->column_number_displayed = Qnil;
10661
10662 if (WINDOW_WANTS_MODELINE_P (w))
10663 display_mode_line (w, MODE_LINE_FACE_ID, current_buffer->mode_line_format);
10664
10665 if (WINDOW_WANTS_TOP_LINE_P (w))
10666 display_mode_line (w, TOP_LINE_FACE_ID, current_buffer->top_line_format);
10667 }
10668
10669
10670 /* Display mode or top line of window W. FACE_ID specifies which line
10671 to display; it is either MODE_LINE_FACE_ID or TOP_LINE_FACE_ID.
10672 FORMAT is the mode line format to display. */
10673
10674 static void
10675 display_mode_line (w, face_id, format)
10676 struct window *w;
10677 enum face_id face_id;
10678 Lisp_Object format;
10679 {
10680 struct it it;
10681 struct face *face;
10682
10683 init_iterator (&it, w, -1, -1, NULL, face_id);
10684 prepare_desired_row (it.glyph_row);
10685
10686 /* Temporarily make frame's keyboard the current kboard so that
10687 kboard-local variables in the mode_line_format will get the right
10688 values. */
10689 push_frame_kboard (it.f);
10690 display_mode_element (&it, 0, 0, 0, format);
10691 pop_frame_kboard ();
10692
10693 /* Fill up with spaces. */
10694 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
10695
10696 compute_line_metrics (&it);
10697 it.glyph_row->full_width_p = 1;
10698 it.glyph_row->mode_line_p = 1;
10699 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
10700 it.glyph_row->continued_p = 0;
10701 it.glyph_row->truncated_on_left_p = 0;
10702 it.glyph_row->truncated_on_right_p = 0;
10703
10704 /* Make a 3D mode-line have a shadow at its right end. */
10705 face = FACE_FROM_ID (it.f, face_id);
10706 extend_face_to_end_of_line (&it);
10707 if (face->box != FACE_NO_BOX)
10708 {
10709 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
10710 + it.glyph_row->used[TEXT_AREA] - 1);
10711 last->right_box_line_p = 1;
10712 }
10713 }
10714
10715
10716 /* Contribute ELT to the mode line for window IT->w. How it
10717 translates into text depends on its data type.
10718
10719 IT describes the display environment in which we display, as usual.
10720
10721 DEPTH is the depth in recursion. It is used to prevent
10722 infinite recursion here.
10723
10724 FIELD_WIDTH is the number of characters the display of ELT should
10725 occupy in the mode line, and PRECISION is the maximum number of
10726 characters to display from ELT's representation. See
10727 display_string for details. *
10728
10729 Returns the hpos of the end of the text generated by ELT. */
10730
10731 static int
10732 display_mode_element (it, depth, field_width, precision, elt)
10733 struct it *it;
10734 int depth;
10735 int field_width, precision;
10736 Lisp_Object elt;
10737 {
10738 int n = 0, field, prec;
10739
10740 tail_recurse:
10741 if (depth > 10)
10742 goto invalid;
10743
10744 depth++;
10745
10746 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
10747 {
10748 case Lisp_String:
10749 {
10750 /* A string: output it and check for %-constructs within it. */
10751 unsigned char c;
10752 unsigned char *this = XSTRING (elt)->data;
10753 unsigned char *lisp_string = this;
10754
10755 while ((precision <= 0 || n < precision)
10756 && *this
10757 && (frame_title_ptr
10758 || it->current_x < it->last_visible_x))
10759 {
10760 unsigned char *last = this;
10761
10762 /* Advance to end of string or next format specifier. */
10763 while ((c = *this++) != '\0' && c != '%')
10764 ;
10765
10766 if (this - 1 != last)
10767 {
10768 /* Output to end of string or up to '%'. Field width
10769 is length of string. Don't output more than
10770 PRECISION allows us. */
10771 prec = --this - last;
10772 if (precision > 0 && prec > precision - n)
10773 prec = precision - n;
10774
10775 if (frame_title_ptr)
10776 n += store_frame_title (last, prec, prec);
10777 else
10778 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
10779 it, 0, prec, 0, -1);
10780 }
10781 else /* c == '%' */
10782 {
10783 unsigned char *percent_position = this;
10784
10785 /* Get the specified minimum width. Zero means
10786 don't pad. */
10787 field = 0;
10788 while ((c = *this++) >= '0' && c <= '9')
10789 field = field * 10 + c - '0';
10790
10791 /* Don't pad beyond the total padding allowed. */
10792 if (field_width - n > 0 && field > field_width - n)
10793 field = field_width - n;
10794
10795 /* Note that either PRECISION <= 0 or N < PRECISION. */
10796 prec = precision - n;
10797
10798 if (c == 'M')
10799 n += display_mode_element (it, depth, field, prec,
10800 Vglobal_mode_string);
10801 else if (c != 0)
10802 {
10803 unsigned char *spec
10804 = decode_mode_spec (it->w, c, field, prec);
10805
10806 if (frame_title_ptr)
10807 n += store_frame_title (spec, field, prec);
10808 else
10809 {
10810 int nglyphs_before
10811 = it->glyph_row->used[TEXT_AREA];
10812 int charpos
10813 = percent_position - XSTRING (elt)->data;
10814 int nwritten
10815 = display_string (spec, Qnil, elt, charpos, 0, it,
10816 field, prec, 0, -1);
10817
10818 /* Assign to the glyphs written above the
10819 string where the `%x' came from, position
10820 of the `%'. */
10821 if (nwritten > 0)
10822 {
10823 struct glyph *glyph
10824 = (it->glyph_row->glyphs[TEXT_AREA]
10825 + nglyphs_before);
10826 int i;
10827
10828 for (i = 0; i < nwritten; ++i)
10829 {
10830 glyph[i].object = elt;
10831 glyph[i].charpos = charpos;
10832 }
10833
10834 n += nwritten;
10835 }
10836 }
10837 }
10838 }
10839 }
10840 }
10841 break;
10842
10843 case Lisp_Symbol:
10844 /* A symbol: process the value of the symbol recursively
10845 as if it appeared here directly. Avoid error if symbol void.
10846 Special case: if value of symbol is a string, output the string
10847 literally. */
10848 {
10849 register Lisp_Object tem;
10850 tem = Fboundp (elt);
10851 if (!NILP (tem))
10852 {
10853 tem = Fsymbol_value (elt);
10854 /* If value is a string, output that string literally:
10855 don't check for % within it. */
10856 if (STRINGP (tem))
10857 {
10858 prec = XSTRING (tem)->size;
10859 if (precision > 0 && prec > precision - n)
10860 prec = precision - n;
10861 if (frame_title_ptr)
10862 n += store_frame_title (XSTRING (tem)->data, -1, prec);
10863 else
10864 n += display_string (NULL, tem, Qnil, 0, 0, it,
10865 0, prec, 0, -1);
10866 }
10867 else if (!EQ (tem, elt))
10868 {
10869 /* Give up right away for nil or t. */
10870 elt = tem;
10871 goto tail_recurse;
10872 }
10873 }
10874 }
10875 break;
10876
10877 case Lisp_Cons:
10878 {
10879 register Lisp_Object car, tem;
10880
10881 /* A cons cell: three distinct cases.
10882 If first element is a string or a cons, process all the elements
10883 and effectively concatenate them.
10884 If first element is a negative number, truncate displaying cdr to
10885 at most that many characters. If positive, pad (with spaces)
10886 to at least that many characters.
10887 If first element is a symbol, process the cadr or caddr recursively
10888 according to whether the symbol's value is non-nil or nil. */
10889 car = XCONS (elt)->car;
10890 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
10891 {
10892 /* An element of the form (:eval FORM) means evaluate FORM
10893 and use the result as mode line elements. */
10894 struct gcpro gcpro1;
10895 Lisp_Object spec;
10896
10897 spec = eval_form (XCAR (XCDR (elt)));
10898 GCPRO1 (spec);
10899 n += display_mode_element (it, depth, field_width - n,
10900 precision - n, spec);
10901 UNGCPRO;
10902 }
10903 else if (SYMBOLP (car))
10904 {
10905 tem = Fboundp (car);
10906 elt = XCONS (elt)->cdr;
10907 if (!CONSP (elt))
10908 goto invalid;
10909 /* elt is now the cdr, and we know it is a cons cell.
10910 Use its car if CAR has a non-nil value. */
10911 if (!NILP (tem))
10912 {
10913 tem = Fsymbol_value (car);
10914 if (!NILP (tem))
10915 { elt = XCONS (elt)->car; goto tail_recurse; }
10916 }
10917 /* Symbol's value is nil (or symbol is unbound)
10918 Get the cddr of the original list
10919 and if possible find the caddr and use that. */
10920 elt = XCONS (elt)->cdr;
10921 if (NILP (elt))
10922 break;
10923 else if (!CONSP (elt))
10924 goto invalid;
10925 elt = XCONS (elt)->car;
10926 goto tail_recurse;
10927 }
10928 else if (INTEGERP (car))
10929 {
10930 register int lim = XINT (car);
10931 elt = XCONS (elt)->cdr;
10932 if (lim < 0)
10933 {
10934 /* Negative int means reduce maximum width. */
10935 if (precision <= 0)
10936 precision = -lim;
10937 else
10938 precision = min (precision, -lim);
10939 }
10940 else if (lim > 0)
10941 {
10942 /* Padding specified. Don't let it be more than
10943 current maximum. */
10944 if (precision > 0)
10945 lim = min (precision, lim);
10946
10947 /* If that's more padding than already wanted, queue it.
10948 But don't reduce padding already specified even if
10949 that is beyond the current truncation point. */
10950 field_width = max (lim, field_width);
10951 }
10952 goto tail_recurse;
10953 }
10954 else if (STRINGP (car) || CONSP (car))
10955 {
10956 register int limit = 50;
10957 /* Limit is to protect against circular lists. */
10958 while (CONSP (elt)
10959 && --limit > 0
10960 && (precision <= 0 || n < precision))
10961 {
10962 n += display_mode_element (it, depth, field_width - n,
10963 precision - n, XCONS (elt)->car);
10964 elt = XCONS (elt)->cdr;
10965 }
10966 }
10967 }
10968 break;
10969
10970 default:
10971 invalid:
10972 if (frame_title_ptr)
10973 n += store_frame_title ("*invalid*", 0, precision - n);
10974 else
10975 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
10976 precision - n, 0, 0);
10977 return n;
10978 }
10979
10980 /* Pad to FIELD_WIDTH. */
10981 if (field_width > 0 && n < field_width)
10982 {
10983 if (frame_title_ptr)
10984 n += store_frame_title ("", field_width - n, 0);
10985 else
10986 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
10987 0, 0, 0);
10988 }
10989
10990 return n;
10991 }
10992
10993
10994 /* Write a null-terminated, right justified decimal representation of
10995 the positive integer D to BUF using a minimal field width WIDTH. */
10996
10997 static void
10998 pint2str (buf, width, d)
10999 register char *buf;
11000 register int width;
11001 register int d;
11002 {
11003 register char *p = buf;
11004
11005 if (d <= 0)
11006 *p++ = '0';
11007 else
11008 {
11009 while (d > 0)
11010 {
11011 *p++ = d % 10 + '0';
11012 d /= 10;
11013 }
11014 }
11015
11016 for (width -= (int) (p - buf); width > 0; --width)
11017 *p++ = ' ';
11018 *p-- = '\0';
11019 while (p > buf)
11020 {
11021 d = *buf;
11022 *buf++ = *p;
11023 *p-- = d;
11024 }
11025 }
11026
11027 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
11028 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
11029 type of CODING_SYSTEM. Return updated pointer into BUF. */
11030
11031 static unsigned char invalid_eol_type[] = "(*invalid*)";
11032
11033 static char *
11034 decode_mode_spec_coding (coding_system, buf, eol_flag)
11035 Lisp_Object coding_system;
11036 register char *buf;
11037 int eol_flag;
11038 {
11039 Lisp_Object val;
11040 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
11041 unsigned char *eol_str;
11042 int eol_str_len;
11043 /* The EOL conversion we are using. */
11044 Lisp_Object eoltype;
11045
11046 val = coding_system;
11047
11048 if (NILP (val)) /* Not yet decided. */
11049 {
11050 if (multibyte)
11051 *buf++ = '-';
11052 if (eol_flag)
11053 eoltype = eol_mnemonic_undecided;
11054 /* Don't mention EOL conversion if it isn't decided. */
11055 }
11056 else
11057 {
11058 Lisp_Object eolvalue;
11059
11060 eolvalue = Fget (coding_system, Qeol_type);
11061
11062 while (!NILP (val) && SYMBOLP (val))
11063 {
11064 val = Fget (val, Qcoding_system);
11065 if (NILP (eolvalue))
11066 eolvalue = Fget (val, Qeol_type);
11067 }
11068
11069 if (multibyte)
11070 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
11071
11072 if (eol_flag)
11073 {
11074 /* The EOL conversion that is normal on this system. */
11075
11076 if (NILP (eolvalue)) /* Not yet decided. */
11077 eoltype = eol_mnemonic_undecided;
11078 else if (VECTORP (eolvalue)) /* Not yet decided. */
11079 eoltype = eol_mnemonic_undecided;
11080 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
11081 eoltype = (XFASTINT (eolvalue) == 0
11082 ? eol_mnemonic_unix
11083 : (XFASTINT (eolvalue) == 1
11084 ? eol_mnemonic_dos : eol_mnemonic_mac));
11085 }
11086 }
11087
11088 if (eol_flag)
11089 {
11090 /* Mention the EOL conversion if it is not the usual one. */
11091 if (STRINGP (eoltype))
11092 {
11093 eol_str = XSTRING (eoltype)->data;
11094 eol_str_len = XSTRING (eoltype)->size;
11095 }
11096 else if (INTEGERP (eoltype)
11097 && CHAR_VALID_P (XINT (eoltype), 0))
11098 {
11099 int c = XINT (eoltype);
11100 unsigned char work[4];
11101
11102 eol_str_len = CHAR_STRING (XINT (eoltype), work, eol_str);
11103 }
11104 else
11105 {
11106 eol_str = invalid_eol_type;
11107 eol_str_len = sizeof (invalid_eol_type) - 1;
11108 }
11109 bcopy (eol_str, buf, eol_str_len);
11110 buf += eol_str_len;
11111 }
11112
11113 return buf;
11114 }
11115
11116 /* Return a string for the output of a mode line %-spec for window W,
11117 generated by character C. PRECISION >= 0 means don't return a
11118 string longer than that value. FIELD_WIDTH > 0 means pad the
11119 string returned with spaces to that value. */
11120
11121 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
11122
11123 static char *
11124 decode_mode_spec (w, c, field_width, precision)
11125 struct window *w;
11126 register char c;
11127 int field_width, precision;
11128 {
11129 Lisp_Object obj;
11130 struct frame *f = XFRAME (WINDOW_FRAME (w));
11131 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
11132 struct buffer *b = XBUFFER (w->buffer);
11133
11134 obj = Qnil;
11135
11136 switch (c)
11137 {
11138 case '*':
11139 if (!NILP (b->read_only))
11140 return "%";
11141 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
11142 return "*";
11143 return "-";
11144
11145 case '+':
11146 /* This differs from %* only for a modified read-only buffer. */
11147 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
11148 return "*";
11149 if (!NILP (b->read_only))
11150 return "%";
11151 return "-";
11152
11153 case '&':
11154 /* This differs from %* in ignoring read-only-ness. */
11155 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
11156 return "*";
11157 return "-";
11158
11159 case '%':
11160 return "%";
11161
11162 case '[':
11163 {
11164 int i;
11165 char *p;
11166
11167 if (command_loop_level > 5)
11168 return "[[[... ";
11169 p = decode_mode_spec_buf;
11170 for (i = 0; i < command_loop_level; i++)
11171 *p++ = '[';
11172 *p = 0;
11173 return decode_mode_spec_buf;
11174 }
11175
11176 case ']':
11177 {
11178 int i;
11179 char *p;
11180
11181 if (command_loop_level > 5)
11182 return " ...]]]";
11183 p = decode_mode_spec_buf;
11184 for (i = 0; i < command_loop_level; i++)
11185 *p++ = ']';
11186 *p = 0;
11187 return decode_mode_spec_buf;
11188 }
11189
11190 case '-':
11191 {
11192 register int i;
11193
11194 /* Let lots_of_dashes be a string of infinite length. */
11195 if (field_width <= 0
11196 || field_width > sizeof (lots_of_dashes))
11197 {
11198 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
11199 decode_mode_spec_buf[i] = '-';
11200 decode_mode_spec_buf[i] = '\0';
11201 return decode_mode_spec_buf;
11202 }
11203 else
11204 return lots_of_dashes;
11205 }
11206
11207 case 'b':
11208 obj = b->name;
11209 break;
11210
11211 case 'c':
11212 {
11213 int col = current_column ();
11214 XSETFASTINT (w->column_number_displayed, col);
11215 pint2str (decode_mode_spec_buf, field_width, col);
11216 return decode_mode_spec_buf;
11217 }
11218
11219 case 'F':
11220 /* %F displays the frame name. */
11221 if (!NILP (f->title))
11222 return (char *) XSTRING (f->title)->data;
11223 if (f->explicit_name || ! FRAME_WINDOW_P (f))
11224 return (char *) XSTRING (f->name)->data;
11225 return "Emacs";
11226
11227 case 'f':
11228 obj = b->filename;
11229 break;
11230
11231 case 'l':
11232 {
11233 int startpos = XMARKER (w->start)->charpos;
11234 int startpos_byte = marker_byte_position (w->start);
11235 int line, linepos, linepos_byte, topline;
11236 int nlines, junk;
11237 int height = XFASTINT (w->height);
11238
11239 /* If we decided that this buffer isn't suitable for line numbers,
11240 don't forget that too fast. */
11241 if (EQ (w->base_line_pos, w->buffer))
11242 goto no_value;
11243 /* But do forget it, if the window shows a different buffer now. */
11244 else if (BUFFERP (w->base_line_pos))
11245 w->base_line_pos = Qnil;
11246
11247 /* If the buffer is very big, don't waste time. */
11248 if (BUF_ZV (b) - BUF_BEGV (b) > line_number_display_limit)
11249 {
11250 w->base_line_pos = Qnil;
11251 w->base_line_number = Qnil;
11252 goto no_value;
11253 }
11254
11255 if (!NILP (w->base_line_number)
11256 && !NILP (w->base_line_pos)
11257 && XFASTINT (w->base_line_pos) <= startpos)
11258 {
11259 line = XFASTINT (w->base_line_number);
11260 linepos = XFASTINT (w->base_line_pos);
11261 linepos_byte = buf_charpos_to_bytepos (b, linepos);
11262 }
11263 else
11264 {
11265 line = 1;
11266 linepos = BUF_BEGV (b);
11267 linepos_byte = BUF_BEGV_BYTE (b);
11268 }
11269
11270 /* Count lines from base line to window start position. */
11271 nlines = display_count_lines (linepos, linepos_byte,
11272 startpos_byte,
11273 startpos, &junk);
11274
11275 topline = nlines + line;
11276
11277 /* Determine a new base line, if the old one is too close
11278 or too far away, or if we did not have one.
11279 "Too close" means it's plausible a scroll-down would
11280 go back past it. */
11281 if (startpos == BUF_BEGV (b))
11282 {
11283 XSETFASTINT (w->base_line_number, topline);
11284 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
11285 }
11286 else if (nlines < height + 25 || nlines > height * 3 + 50
11287 || linepos == BUF_BEGV (b))
11288 {
11289 int limit = BUF_BEGV (b);
11290 int limit_byte = BUF_BEGV_BYTE (b);
11291 int position;
11292 int distance = (height * 2 + 30) * 200;
11293
11294 if (startpos - distance > limit)
11295 {
11296 limit = startpos - distance;
11297 limit_byte = CHAR_TO_BYTE (limit);
11298 }
11299
11300 nlines = display_count_lines (startpos, startpos_byte,
11301 limit_byte,
11302 - (height * 2 + 30),
11303 &position);
11304 /* If we couldn't find the lines we wanted within
11305 200 chars per line,
11306 give up on line numbers for this window. */
11307 if (position == limit_byte && limit == startpos - distance)
11308 {
11309 w->base_line_pos = w->buffer;
11310 w->base_line_number = Qnil;
11311 goto no_value;
11312 }
11313
11314 XSETFASTINT (w->base_line_number, topline - nlines);
11315 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
11316 }
11317
11318 /* Now count lines from the start pos to point. */
11319 nlines = display_count_lines (startpos, startpos_byte,
11320 PT_BYTE, PT, &junk);
11321
11322 /* Record that we did display the line number. */
11323 line_number_displayed = 1;
11324
11325 /* Make the string to show. */
11326 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
11327 return decode_mode_spec_buf;
11328 no_value:
11329 {
11330 char* p = decode_mode_spec_buf;
11331 int pad = field_width - 2;
11332 while (pad-- > 0)
11333 *p++ = ' ';
11334 *p++ = '?';
11335 *p = '?';
11336 return decode_mode_spec_buf;
11337 }
11338 }
11339 break;
11340
11341 case 'm':
11342 obj = b->mode_name;
11343 break;
11344
11345 case 'n':
11346 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
11347 return " Narrow";
11348 break;
11349
11350 case 'p':
11351 {
11352 int pos = marker_position (w->start);
11353 int total = BUF_ZV (b) - BUF_BEGV (b);
11354
11355 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
11356 {
11357 if (pos <= BUF_BEGV (b))
11358 return "All";
11359 else
11360 return "Bottom";
11361 }
11362 else if (pos <= BUF_BEGV (b))
11363 return "Top";
11364 else
11365 {
11366 if (total > 1000000)
11367 /* Do it differently for a large value, to avoid overflow. */
11368 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
11369 else
11370 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
11371 /* We can't normally display a 3-digit number,
11372 so get us a 2-digit number that is close. */
11373 if (total == 100)
11374 total = 99;
11375 sprintf (decode_mode_spec_buf, "%2d%%", total);
11376 return decode_mode_spec_buf;
11377 }
11378 }
11379
11380 /* Display percentage of size above the bottom of the screen. */
11381 case 'P':
11382 {
11383 int toppos = marker_position (w->start);
11384 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
11385 int total = BUF_ZV (b) - BUF_BEGV (b);
11386
11387 if (botpos >= BUF_ZV (b))
11388 {
11389 if (toppos <= BUF_BEGV (b))
11390 return "All";
11391 else
11392 return "Bottom";
11393 }
11394 else
11395 {
11396 if (total > 1000000)
11397 /* Do it differently for a large value, to avoid overflow. */
11398 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
11399 else
11400 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
11401 /* We can't normally display a 3-digit number,
11402 so get us a 2-digit number that is close. */
11403 if (total == 100)
11404 total = 99;
11405 if (toppos <= BUF_BEGV (b))
11406 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
11407 else
11408 sprintf (decode_mode_spec_buf, "%2d%%", total);
11409 return decode_mode_spec_buf;
11410 }
11411 }
11412
11413 case 's':
11414 /* status of process */
11415 obj = Fget_buffer_process (w->buffer);
11416 if (NILP (obj))
11417 return "no process";
11418 #ifdef subprocesses
11419 obj = Fsymbol_name (Fprocess_status (obj));
11420 #endif
11421 break;
11422
11423 case 't': /* indicate TEXT or BINARY */
11424 #ifdef MODE_LINE_BINARY_TEXT
11425 return MODE_LINE_BINARY_TEXT (b);
11426 #else
11427 return "T";
11428 #endif
11429
11430 case 'z':
11431 /* coding-system (not including end-of-line format) */
11432 case 'Z':
11433 /* coding-system (including end-of-line type) */
11434 {
11435 int eol_flag = (c == 'Z');
11436 char *p = decode_mode_spec_buf;
11437
11438 if (! FRAME_WINDOW_P (f))
11439 {
11440 /* No need to mention EOL here--the terminal never needs
11441 to do EOL conversion. */
11442 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
11443 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
11444 }
11445 p = decode_mode_spec_coding (b->buffer_file_coding_system,
11446 p, eol_flag);
11447
11448 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
11449 #ifdef subprocesses
11450 obj = Fget_buffer_process (Fcurrent_buffer ());
11451 if (PROCESSP (obj))
11452 {
11453 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
11454 p, eol_flag);
11455 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
11456 p, eol_flag);
11457 }
11458 #endif /* subprocesses */
11459 #endif /* 0 */
11460 *p = 0;
11461 return decode_mode_spec_buf;
11462 }
11463 }
11464
11465 if (STRINGP (obj))
11466 return (char *) XSTRING (obj)->data;
11467 else
11468 return "";
11469 }
11470
11471
11472 /* Count up to COUNT lines starting from START / START_BYTE.
11473 But don't go beyond LIMIT_BYTE.
11474 Return the number of lines thus found (always nonnegative).
11475
11476 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
11477
11478 static int
11479 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
11480 int start, start_byte, limit_byte, count;
11481 int *byte_pos_ptr;
11482 {
11483 register unsigned char *cursor;
11484 unsigned char *base;
11485
11486 register int ceiling;
11487 register unsigned char *ceiling_addr;
11488 int orig_count = count;
11489
11490 /* If we are not in selective display mode,
11491 check only for newlines. */
11492 int selective_display = (!NILP (current_buffer->selective_display)
11493 && !INTEGERP (current_buffer->selective_display));
11494
11495 if (count > 0)
11496 {
11497 while (start_byte < limit_byte)
11498 {
11499 ceiling = BUFFER_CEILING_OF (start_byte);
11500 ceiling = min (limit_byte - 1, ceiling);
11501 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
11502 base = (cursor = BYTE_POS_ADDR (start_byte));
11503 while (1)
11504 {
11505 if (selective_display)
11506 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
11507 ;
11508 else
11509 while (*cursor != '\n' && ++cursor != ceiling_addr)
11510 ;
11511
11512 if (cursor != ceiling_addr)
11513 {
11514 if (--count == 0)
11515 {
11516 start_byte += cursor - base + 1;
11517 *byte_pos_ptr = start_byte;
11518 return orig_count;
11519 }
11520 else
11521 if (++cursor == ceiling_addr)
11522 break;
11523 }
11524 else
11525 break;
11526 }
11527 start_byte += cursor - base;
11528 }
11529 }
11530 else
11531 {
11532 while (start_byte > limit_byte)
11533 {
11534 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
11535 ceiling = max (limit_byte, ceiling);
11536 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
11537 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
11538 while (1)
11539 {
11540 if (selective_display)
11541 while (--cursor != ceiling_addr
11542 && *cursor != '\n' && *cursor != 015)
11543 ;
11544 else
11545 while (--cursor != ceiling_addr && *cursor != '\n')
11546 ;
11547
11548 if (cursor != ceiling_addr)
11549 {
11550 if (++count == 0)
11551 {
11552 start_byte += cursor - base + 1;
11553 *byte_pos_ptr = start_byte;
11554 /* When scanning backwards, we should
11555 not count the newline posterior to which we stop. */
11556 return - orig_count - 1;
11557 }
11558 }
11559 else
11560 break;
11561 }
11562 /* Here we add 1 to compensate for the last decrement
11563 of CURSOR, which took it past the valid range. */
11564 start_byte += cursor - base + 1;
11565 }
11566 }
11567
11568 *byte_pos_ptr = limit_byte;
11569
11570 if (count < 0)
11571 return - orig_count + count;
11572 return orig_count - count;
11573
11574 }
11575
11576
11577 \f
11578 /***********************************************************************
11579 Displaying strings
11580 ***********************************************************************/
11581
11582 /* Display a NUL-terminated string, starting with index START.
11583
11584 If STRING is non-null, display that C string. Otherwise, the Lisp
11585 string LISP_STRING is displayed.
11586
11587 If FACE_STRING is not nil, FACE_STRING_POS is a position in
11588 FACE_STRING. Display STRING or LISP_STRING with the face at
11589 FACE_STRING_POS in FACE_STRING:
11590
11591 Display the string in the environment given by IT, but use the
11592 standard display table, temporarily.
11593
11594 FIELD_WIDTH is the minimum number of output glyphs to produce.
11595 If STRING has fewer characters than FIELD_WIDTH, pad to the right
11596 with spaces. If STRING has more characters, more than FIELD_WIDTH
11597 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
11598
11599 PRECISION is the maximum number of characters to output from
11600 STRING. PRECISION < 0 means don't truncate the string.
11601
11602 This is roughly equivalent to printf format specifiers:
11603
11604 FIELD_WIDTH PRECISION PRINTF
11605 ----------------------------------------
11606 -1 -1 %s
11607 -1 10 %.10s
11608 10 -1 %10s
11609 20 10 %20.10s
11610
11611 MULTIBYTE zero means do not display multibyte chars, > 0 means do
11612 display them, and < 0 means obey the current buffer's value of
11613 enable_multibyte_characters.
11614
11615 Value is the number of glyphs produced. */
11616
11617 static int
11618 display_string (string, lisp_string, face_string, face_string_pos,
11619 start, it, field_width, precision, max_x, multibyte)
11620 unsigned char *string;
11621 Lisp_Object lisp_string;
11622 int start;
11623 struct it *it;
11624 int field_width, precision, max_x;
11625 int multibyte;
11626 {
11627 int hpos_at_start = it->hpos;
11628 int saved_face_id = it->face_id;
11629 struct glyph_row *row = it->glyph_row;
11630
11631 /* Initialize the iterator IT for iteration over STRING beginning
11632 with index START. We assume that IT may be modified here (which
11633 means that display_line has to do something when displaying a
11634 mini-buffer prompt, which it does). */
11635 reseat_to_string (it, string, lisp_string, start,
11636 precision, field_width, multibyte);
11637
11638 /* If displaying STRING, set up the face of the iterator
11639 from LISP_STRING, if that's given. */
11640 if (STRINGP (face_string))
11641 {
11642 int endptr;
11643 struct face *face;
11644
11645 it->face_id
11646 = face_at_string_position (it->w, face_string, face_string_pos,
11647 0, it->region_beg_charpos,
11648 it->region_end_charpos,
11649 &endptr, it->base_face_id);
11650 face = FACE_FROM_ID (it->f, it->face_id);
11651 it->face_box_p = face->box != FACE_NO_BOX;
11652 }
11653
11654 /* Set max_x to the maximum allowed X position. Don't let it go
11655 beyond the right edge of the window. */
11656 if (max_x <= 0)
11657 max_x = it->last_visible_x;
11658 else
11659 max_x = min (max_x, it->last_visible_x);
11660
11661 /* Skip over display elements that are not visible. because IT->w is
11662 hscrolled. */
11663 if (it->current_x < it->first_visible_x)
11664 move_it_in_display_line_to (it, 100000, it->first_visible_x,
11665 MOVE_TO_POS | MOVE_TO_X);
11666
11667 row->ascent = it->max_ascent;
11668 row->height = it->max_ascent + it->max_descent;
11669
11670 /* This condition is for the case that we are called with current_x
11671 past last_visible_x. */
11672 while (it->current_x < max_x)
11673 {
11674 int x_before, x, n_glyphs_before, i, nglyphs;
11675
11676 /* Get the next display element. */
11677 if (!get_next_display_element (it))
11678 break;
11679
11680 /* Produce glyphs. */
11681 x_before = it->current_x;
11682 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
11683 PRODUCE_GLYPHS (it);
11684
11685 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
11686 i = 0;
11687 x = x_before;
11688 while (i < nglyphs)
11689 {
11690 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11691
11692 if (!it->truncate_lines_p
11693 && x + glyph->pixel_width > max_x)
11694 {
11695 /* End of continued line or max_x reached. */
11696 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
11697 it->current_x = x;
11698 break;
11699 }
11700 else if (x + glyph->pixel_width > it->first_visible_x)
11701 {
11702 /* Glyph is at least partially visible. */
11703 ++it->hpos;
11704 if (x < it->first_visible_x)
11705 it->glyph_row->x = x - it->first_visible_x;
11706 }
11707 else
11708 {
11709 /* Glyph is off the left margin of the display area.
11710 Should not happen. */
11711 abort ();
11712 }
11713
11714 row->ascent = max (row->ascent, it->max_ascent);
11715 row->height = max (row->height, it->max_ascent + it->max_descent);
11716 x += glyph->pixel_width;
11717 ++i;
11718 }
11719
11720 /* Stop if max_x reached. */
11721 if (i < nglyphs)
11722 break;
11723
11724 /* Stop at line ends. */
11725 if (ITERATOR_AT_END_OF_LINE_P (it))
11726 {
11727 it->continuation_lines_width = 0;
11728 break;
11729 }
11730
11731 set_iterator_to_next (it);
11732
11733 /* Stop if truncating at the right edge. */
11734 if (it->truncate_lines_p
11735 && it->current_x >= it->last_visible_x)
11736 {
11737 /* Add truncation mark, but don't do it if the line is
11738 truncated at a padding space. */
11739 if (IT_CHARPOS (*it) < it->string_nchars)
11740 {
11741 if (!FRAME_WINDOW_P (it->f))
11742 produce_special_glyphs (it, IT_TRUNCATION);
11743 it->glyph_row->truncated_on_right_p = 1;
11744 }
11745 break;
11746 }
11747 }
11748
11749 /* Maybe insert a truncation at the left. */
11750 if (it->first_visible_x
11751 && IT_CHARPOS (*it) > 0)
11752 {
11753 if (!FRAME_WINDOW_P (it->f))
11754 insert_left_trunc_glyphs (it);
11755 it->glyph_row->truncated_on_left_p = 1;
11756 }
11757
11758 it->face_id = saved_face_id;
11759
11760 /* Value is number of columns displayed. */
11761 return it->hpos - hpos_at_start;
11762 }
11763
11764
11765 \f
11766 /* This is like a combination of memq and assq. Return 1 if PROPVAL
11767 appears as an element of LIST or as the car of an element of LIST.
11768 If PROPVAL is a list, compare each element against LIST in that
11769 way, and return 1 if any element of PROPVAL is found in LIST.
11770 Otherwise return 0. This function cannot quit. */
11771
11772 int
11773 invisible_p (propval, list)
11774 register Lisp_Object propval;
11775 Lisp_Object list;
11776 {
11777 register Lisp_Object tail, proptail;
11778 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
11779 {
11780 register Lisp_Object tem;
11781 tem = XCONS (tail)->car;
11782 if (EQ (propval, tem))
11783 return 1;
11784 if (CONSP (tem) && EQ (propval, XCONS (tem)->car))
11785 return 1;
11786 }
11787 if (CONSP (propval))
11788 for (proptail = propval; CONSP (proptail);
11789 proptail = XCONS (proptail)->cdr)
11790 {
11791 Lisp_Object propelt;
11792 propelt = XCONS (proptail)->car;
11793 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
11794 {
11795 register Lisp_Object tem;
11796 tem = XCONS (tail)->car;
11797 if (EQ (propelt, tem))
11798 return 1;
11799 if (CONSP (tem) && EQ (propelt, XCONS (tem)->car))
11800 return 1;
11801 }
11802 }
11803 return 0;
11804 }
11805
11806
11807 /* Return 1 if PROPVAL appears as the car of an element of LIST and
11808 the cdr of that element is non-nil. If PROPVAL is a list, check
11809 each element of PROPVAL in that way, and the first time some
11810 element is found, return 1 if the cdr of that element is non-nil.
11811 Otherwise return 0. This function cannot quit. */
11812
11813 int
11814 invisible_ellipsis_p (propval, list)
11815 register Lisp_Object propval;
11816 Lisp_Object list;
11817 {
11818 register Lisp_Object tail, proptail;
11819 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
11820 {
11821 register Lisp_Object tem;
11822 tem = XCONS (tail)->car;
11823 if (CONSP (tem) && EQ (propval, XCONS (tem)->car))
11824 return ! NILP (XCONS (tem)->cdr);
11825 }
11826 if (CONSP (propval))
11827 for (proptail = propval; CONSP (proptail);
11828 proptail = XCONS (proptail)->cdr)
11829 {
11830 Lisp_Object propelt;
11831 propelt = XCONS (proptail)->car;
11832 for (tail = list; CONSP (tail); tail = XCONS (tail)->cdr)
11833 {
11834 register Lisp_Object tem;
11835 tem = XCONS (tail)->car;
11836 if (CONSP (tem) && EQ (propelt, XCONS (tem)->car))
11837 return ! NILP (XCONS (tem)->cdr);
11838 }
11839 }
11840 return 0;
11841 }
11842
11843
11844 \f
11845 /***********************************************************************
11846 Initialization
11847 ***********************************************************************/
11848
11849 void
11850 syms_of_xdisp ()
11851 {
11852 echo_area_message = previous_echo_area_message = Qnil;
11853 staticpro (&echo_area_message);
11854 staticpro (&previous_echo_area_message);
11855
11856 staticpro (&Qinhibit_redisplay);
11857 Qinhibit_redisplay = intern ("inhibit-redisplay");
11858
11859 #if GLYPH_DEBUG
11860 defsubr (&Sdump_glyph_matrix);
11861 defsubr (&Sdump_glyph_row);
11862 defsubr (&Sdump_toolbar_row);
11863 defsubr (&Strace_redisplay_toggle);
11864 #endif
11865
11866 staticpro (&Qmenu_bar_update_hook);
11867 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
11868
11869 staticpro (&Qoverriding_terminal_local_map);
11870 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
11871
11872 staticpro (&Qoverriding_local_map);
11873 Qoverriding_local_map = intern ("overriding-local-map");
11874
11875 staticpro (&Qwindow_scroll_functions);
11876 Qwindow_scroll_functions = intern ("window-scroll-functions");
11877
11878 staticpro (&Qredisplay_end_trigger_functions);
11879 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
11880
11881 staticpro (&Qinhibit_point_motion_hooks);
11882 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
11883
11884 staticpro (&Qdisplay);
11885 Qdisplay = intern ("display");
11886 staticpro (&Qleft_margin);
11887 Qspace_width = intern ("space-width");
11888 staticpro (&Qspace_width);
11889 Qheight = intern ("height");
11890 staticpro (&Qheight);
11891 Qraise = intern ("raise");
11892 staticpro (&Qraise);
11893 Qspace = intern ("space");
11894 staticpro (&Qspace);
11895 Qleft_margin = intern ("left-margin");
11896 staticpro (&Qright_margin);
11897 Qright_margin = intern ("right-margin");
11898 Qalign_to = intern ("align-to");
11899 staticpro (&Qalign_to);
11900 QCalign_to = intern (":align-to");
11901 staticpro (&QCalign_to);
11902 Qwidth = intern ("width");
11903 staticpro (&Qwidth);
11904 Qrelative_width = intern ("relative-width");
11905 staticpro (&Qrelative_width);
11906 QCrelative_width = intern (":relative-width");
11907 staticpro (&QCrelative_width);
11908 QCrelative_height = intern (":relative-height");
11909 staticpro (&QCrelative_height);
11910 QCeval = intern (":eval");
11911 staticpro (&QCeval);
11912 QCwhen = intern (":when");
11913 staticpro (&QCwhen);
11914 Qfontified = intern ("fontified");
11915 staticpro (&Qfontified);
11916 Qfontification_functions = intern ("fontification-functions");
11917 staticpro (&Qfontification_functions);
11918 Qshow_trailing_whitespace = intern ("show-trailing-whitespace");
11919 staticpro (&Qshow_trailing_whitespace);
11920 Qtrailing_whitespace = intern ("trailing-whitespace");
11921 staticpro (&Qtrailing_whitespace);
11922 Qimage = intern ("image");
11923 staticpro (&Qimage);
11924
11925 staticpro (&last_arrow_position);
11926 staticpro (&last_arrow_string);
11927 last_arrow_position = Qnil;
11928 last_arrow_string = Qnil;
11929
11930 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
11931 "Non-nil means don't actually do any redisplay.\n\
11932 This is used for internal purposes.");
11933 Vinhibit_redisplay = Qnil;
11934
11935 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
11936 "String (or mode line construct) included (normally) in `mode-line-format'.");
11937 Vglobal_mode_string = Qnil;
11938
11939 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
11940 "Marker for where to display an arrow on top of the buffer text.\n\
11941 This must be the beginning of a line in order to work.\n\
11942 See also `overlay-arrow-string'.");
11943 Voverlay_arrow_position = Qnil;
11944
11945 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
11946 "String to display as an arrow. See also `overlay-arrow-position'.");
11947 Voverlay_arrow_string = Qnil;
11948
11949 DEFVAR_INT ("scroll-step", &scroll_step,
11950 "*The number of lines to try scrolling a window by when point moves out.\n\
11951 If that fails to bring point back on frame, point is centered instead.\n\
11952 If this is zero, point is always centered after it moves off frame.");
11953
11954 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
11955 "*Scroll up to this many lines, to bring point back on screen.");
11956 scroll_conservatively = 0;
11957
11958 DEFVAR_INT ("scroll-margin", &scroll_margin,
11959 "*Number of lines of margin at the top and bottom of a window.\n\
11960 Recenter the window whenever point gets within this many lines\n\
11961 of the top or bottom of the window.");
11962 scroll_margin = 0;
11963
11964 #if GLYPH_DEBUG
11965 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
11966 #endif
11967
11968 DEFVAR_BOOL ("truncate-partial-width-windows",
11969 &truncate_partial_width_windows,
11970 "*Non-nil means truncate lines in all windows less than full frame wide.");
11971 truncate_partial_width_windows = 1;
11972
11973 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
11974 "*Non-nil means use inverse video for the mode line.");
11975 mode_line_inverse_video = 1;
11976
11977 DEFVAR_INT ("line-number-display-limit", &line_number_display_limit,
11978 "*Maximum buffer size for which line number should be displayed.\n\
11979 If the buffer is bigger than this, the line number does not appear\n\
11980 in the mode line.");
11981 line_number_display_limit = 1000000;
11982
11983 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
11984 "*Non-nil means highlight region even in nonselected windows.");
11985 highlight_nonselected_windows = 0;
11986
11987 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
11988 "Non-nil if more than one frame is visible on this display.\n\
11989 Minibuffer-only frames don't count, but iconified frames do.\n\
11990 This variable is not guaranteed to be accurate except while processing\n\
11991 `frame-title-format' and `icon-title-format'.");
11992
11993 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
11994 "Template for displaying the title bar of visible frames.\n\
11995 \(Assuming the window manager supports this feature.)\n\
11996 This variable has the same structure as `mode-line-format' (which see),\n\
11997 and is used only on frames for which no explicit name has been set\n\
11998 \(see `modify-frame-parameters').");
11999 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
12000 "Template for displaying the title bar of an iconified frame.\n\
12001 \(Assuming the window manager supports this feature.)\n\
12002 This variable has the same structure as `mode-line-format' (which see),\n\
12003 and is used only on frames for which no explicit name has been set\n\
12004 \(see `modify-frame-parameters').");
12005 Vicon_title_format
12006 = Vframe_title_format
12007 = Fcons (intern ("multiple-frames"),
12008 Fcons (build_string ("%b"),
12009 Fcons (Fcons (build_string (""),
12010 Fcons (intern ("invocation-name"),
12011 Fcons (build_string ("@"),
12012 Fcons (intern ("system-name"),
12013 Qnil)))),
12014 Qnil)));
12015
12016 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
12017 "Maximum number of lines to keep in the message log buffer.\n\
12018 If nil, disable message logging. If t, log messages but don't truncate\n\
12019 the buffer when it becomes large.");
12020 XSETFASTINT (Vmessage_log_max, 50);
12021
12022 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
12023 "Functions called before redisplay, if window sizes have changed.\n\
12024 The value should be a list of functions that take one argument.\n\
12025 Just before redisplay, for each frame, if any of its windows have changed\n\
12026 size since the last redisplay, or have been split or deleted,\n\
12027 all the functions in the list are called, with the frame as argument.");
12028 Vwindow_size_change_functions = Qnil;
12029
12030 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
12031 "List of Functions to call before redisplaying a window with scrolling.\n\
12032 Each function is called with two arguments, the window\n\
12033 and its new display-start position. Note that the value of `window-end'\n\
12034 is not valid when these functions are called.");
12035 Vwindow_scroll_functions = Qnil;
12036
12037 DEFVAR_BOOL ("auto-resize-toolbars", &auto_resize_toolbars_p,
12038 "*Non-nil means automatically resize toolbars.\n\
12039 This increases a toolbar's height if not all toolbar items are visible.\n\
12040 It decreases a toolbar's height when it would display blank lines\n\
12041 otherwise.");
12042 auto_resize_toolbars_p = 1;
12043
12044 DEFVAR_BOOL ("auto-raise-toolbar-buttons", &auto_raise_toolbar_buttons_p,
12045 "*Non-nil means raise toolbar buttons when the mouse moves over them.");
12046 auto_raise_toolbar_buttons_p = 1;
12047
12048 DEFVAR_INT ("toolbar-button-margin", &toolbar_button_margin,
12049 "*Margin around toolbar buttons in pixels.");
12050 toolbar_button_margin = 1;
12051
12052 DEFVAR_INT ("toolbar-button-relief", &toolbar_button_relief,
12053 "Relief thickness of toolbar buttons.");
12054 toolbar_button_relief = 3;
12055
12056 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
12057 "List of functions to call to fontify regions of text.\n\
12058 Each function is called with one argument POS. Functions must\n\
12059 fontify a region starting at POS in the current buffer, and give\n\
12060 fontified regions the property `fontified'.\n\
12061 This variable automatically becomes buffer-local when set.");
12062 Vfontification_functions = Qnil;
12063 Fmake_local_variable (Qfontification_functions);
12064
12065 DEFVAR_BOOL ("unibyte-display-via-language-environment",
12066 &unibyte_display_via_language_environment,
12067 "*Non-nil means display unibyte text according to language environment.\n\
12068 Specifically this means that unibyte non-ASCII characters\n\
12069 are displayed by converting them to the equivalent multibyte characters\n\
12070 according to the current language environment. As a result, they are\n\
12071 displayed according to the current fontset.");
12072 unibyte_display_via_language_environment = 0;
12073 }
12074
12075
12076 /* Initialize this module when Emacs starts. */
12077
12078 void
12079 init_xdisp ()
12080 {
12081 Lisp_Object root_window;
12082 struct window *mini_w;
12083
12084 CHARPOS (this_line_start_pos) = 0;
12085
12086 mini_w = XWINDOW (minibuf_window);
12087 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
12088
12089 echo_area_glyphs = 0;
12090 previous_echo_glyphs = 0;
12091 echo_area_message = previous_echo_area_message = Qnil;
12092
12093 if (!noninteractive)
12094 {
12095 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
12096 int i;
12097
12098 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
12099 set_window_height (root_window,
12100 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
12101 0);
12102 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
12103 set_window_height (minibuf_window, 1, 0);
12104
12105 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
12106 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
12107
12108 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
12109 scratch_glyph_row.glyphs[TEXT_AREA + 1]
12110 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
12111
12112 /* The default ellipsis glyphs `...'. */
12113 for (i = 0; i < 3; ++i)
12114 XSETFASTINT (default_invis_vector[i], '.');
12115 }
12116
12117 #ifdef HAVE_WINDOW_SYSTEM
12118 {
12119 /* Allocate the buffer for frame titles. */
12120 int size = 100;
12121 frame_title_buf = (char *) xmalloc (size);
12122 frame_title_buf_end = frame_title_buf + size;
12123 frame_title_ptr = NULL;
12124 }
12125 #endif /* HAVE_WINDOW_SYSTEM */
12126 }
12127
12128