]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(get_next_display_element): Use CHAR_STRING_NO_SIGNAL
[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, 2000, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the the description of direct update
37 operations, below.).
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay() +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
125
126 Iteration over things to be be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator
128 (or init_string_iterator for that matter). Calls to
129 get_next_display_element fill the iterator structure with relevant
130 information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "macros.h"
183 #include "disptab.h"
184 #include "termhooks.h"
185 #include "intervals.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
189 #include "fontset.h"
190
191 #ifdef HAVE_X_WINDOWS
192 #include "xterm.h"
193 #endif
194 #ifdef WINDOWSNT
195 #include "w32term.h"
196 #endif
197 #ifdef macintosh
198 #include "macterm.h"
199 #endif
200
201 #define min(a, b) ((a) < (b) ? (a) : (b))
202 #define max(a, b) ((a) > (b) ? (a) : (b))
203
204 #define INFINITY 10000000
205
206 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
207 extern void set_frame_menubar P_ ((struct frame *f, int, int));
208 extern int pending_menu_activation;
209 #endif
210
211 extern int interrupt_input;
212 extern int command_loop_level;
213
214 extern int minibuffer_auto_raise;
215
216 extern Lisp_Object Qface;
217
218 extern Lisp_Object Voverriding_local_map;
219 extern Lisp_Object Voverriding_local_map_menu_flag;
220 extern Lisp_Object Qmenu_item;
221
222 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
223 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
224 Lisp_Object Qredisplay_end_trigger_functions;
225 Lisp_Object Qinhibit_point_motion_hooks;
226 Lisp_Object QCeval, Qwhen, QCfile, QCdata;
227 Lisp_Object Qfontified;
228 Lisp_Object Qgrow_only;
229 Lisp_Object Qinhibit_eval_during_redisplay;
230
231 /* Functions called to fontify regions of text. */
232
233 Lisp_Object Vfontification_functions;
234 Lisp_Object Qfontification_functions;
235
236 /* Non-zero means draw tool bar buttons raised when the mouse moves
237 over them. */
238
239 int auto_raise_tool_bar_buttons_p;
240
241 /* Margin around tool bar buttons in pixels. */
242
243 Lisp_Object Vtool_bar_button_margin;
244
245 /* Thickness of shadow to draw around tool bar buttons. */
246
247 int tool_bar_button_relief;
248
249 /* Non-zero means automatically resize tool-bars so that all tool-bar
250 items are visible, and no blank lines remain. */
251
252 int auto_resize_tool_bars_p;
253
254 /* Non-nil means don't actually do any redisplay. */
255
256 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
257
258 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
259
260 int inhibit_eval_during_redisplay;
261
262 /* Names of text properties relevant for redisplay. */
263
264 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
265 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
266
267 /* Symbols used in text property values. */
268
269 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
270 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
271 Lisp_Object Qmargin;
272 extern Lisp_Object Qheight;
273
274 /* Non-nil means highlight trailing whitespace. */
275
276 Lisp_Object Vshow_trailing_whitespace;
277
278 /* Name of the face used to highlight trailing whitespace. */
279
280 Lisp_Object Qtrailing_whitespace;
281
282 /* The symbol `image' which is the car of the lists used to represent
283 images in Lisp. */
284
285 Lisp_Object Qimage;
286
287 /* Non-zero means print newline to stdout before next mini-buffer
288 message. */
289
290 int noninteractive_need_newline;
291
292 /* Non-zero means print newline to message log before next message. */
293
294 static int message_log_need_newline;
295
296 \f
297 /* The buffer position of the first character appearing entirely or
298 partially on the line of the selected window which contains the
299 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
300 redisplay optimization in redisplay_internal. */
301
302 static struct text_pos this_line_start_pos;
303
304 /* Number of characters past the end of the line above, including the
305 terminating newline. */
306
307 static struct text_pos this_line_end_pos;
308
309 /* The vertical positions and the height of this line. */
310
311 static int this_line_vpos;
312 static int this_line_y;
313 static int this_line_pixel_height;
314
315 /* X position at which this display line starts. Usually zero;
316 negative if first character is partially visible. */
317
318 static int this_line_start_x;
319
320 /* Buffer that this_line_.* variables are referring to. */
321
322 static struct buffer *this_line_buffer;
323
324 /* Nonzero means truncate lines in all windows less wide than the
325 frame. */
326
327 int truncate_partial_width_windows;
328
329 /* A flag to control how to display unibyte 8-bit character. */
330
331 int unibyte_display_via_language_environment;
332
333 /* Nonzero means we have more than one non-mini-buffer-only frame.
334 Not guaranteed to be accurate except while parsing
335 frame-title-format. */
336
337 int multiple_frames;
338
339 Lisp_Object Vglobal_mode_string;
340
341 /* Marker for where to display an arrow on top of the buffer text. */
342
343 Lisp_Object Voverlay_arrow_position;
344
345 /* String to display for the arrow. Only used on terminal frames. */
346
347 Lisp_Object Voverlay_arrow_string;
348
349 /* Values of those variables at last redisplay. However, if
350 Voverlay_arrow_position is a marker, last_arrow_position is its
351 numerical position. */
352
353 static Lisp_Object last_arrow_position, last_arrow_string;
354
355 /* Like mode-line-format, but for the title bar on a visible frame. */
356
357 Lisp_Object Vframe_title_format;
358
359 /* Like mode-line-format, but for the title bar on an iconified frame. */
360
361 Lisp_Object Vicon_title_format;
362
363 /* List of functions to call when a window's size changes. These
364 functions get one arg, a frame on which one or more windows' sizes
365 have changed. */
366
367 static Lisp_Object Vwindow_size_change_functions;
368
369 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
370
371 /* Nonzero if overlay arrow has been displayed once in this window. */
372
373 static int overlay_arrow_seen;
374
375 /* Nonzero means highlight the region even in nonselected windows. */
376
377 int highlight_nonselected_windows;
378
379 /* If cursor motion alone moves point off frame, try scrolling this
380 many lines up or down if that will bring it back. */
381
382 static int scroll_step;
383
384 /* Non-0 means scroll just far enough to bring point back on the
385 screen, when appropriate. */
386
387 static int scroll_conservatively;
388
389 /* Recenter the window whenever point gets within this many lines of
390 the top or bottom of the window. This value is translated into a
391 pixel value by multiplying it with CANON_Y_UNIT, which means that
392 there is really a fixed pixel height scroll margin. */
393
394 int scroll_margin;
395
396 /* Number of windows showing the buffer of the selected window (or
397 another buffer with the same base buffer). keyboard.c refers to
398 this. */
399
400 int buffer_shared;
401
402 /* Vector containing glyphs for an ellipsis `...'. */
403
404 static Lisp_Object default_invis_vector[3];
405
406 /* Zero means display the mode-line/header-line/menu-bar in the default face
407 (this slightly odd definition is for compatibility with previous versions
408 of emacs), non-zero means display them using their respective faces.
409
410 This variable is deprecated. */
411
412 int mode_line_inverse_video;
413
414 /* Prompt to display in front of the mini-buffer contents. */
415
416 Lisp_Object minibuf_prompt;
417
418 /* Width of current mini-buffer prompt. Only set after display_line
419 of the line that contains the prompt. */
420
421 int minibuf_prompt_width;
422 int minibuf_prompt_pixel_width;
423
424 /* This is the window where the echo area message was displayed. It
425 is always a mini-buffer window, but it may not be the same window
426 currently active as a mini-buffer. */
427
428 Lisp_Object echo_area_window;
429
430 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
431 pushes the current message and the value of
432 message_enable_multibyte on the stack, the function restore_message
433 pops the stack and displays MESSAGE again. */
434
435 Lisp_Object Vmessage_stack;
436
437 /* Nonzero means multibyte characters were enabled when the echo area
438 message was specified. */
439
440 int message_enable_multibyte;
441
442 /* True if we should redraw the mode lines on the next redisplay. */
443
444 int update_mode_lines;
445
446 /* Nonzero if window sizes or contents have changed since last
447 redisplay that finished */
448
449 int windows_or_buffers_changed;
450
451 /* Nonzero after display_mode_line if %l was used and it displayed a
452 line number. */
453
454 int line_number_displayed;
455
456 /* Maximum buffer size for which to display line numbers. */
457
458 Lisp_Object Vline_number_display_limit;
459
460 /* line width to consider when repostioning for line number display */
461
462 static int line_number_display_limit_width;
463
464 /* Number of lines to keep in the message log buffer. t means
465 infinite. nil means don't log at all. */
466
467 Lisp_Object Vmessage_log_max;
468
469 /* The name of the *Messages* buffer, a string. */
470
471 static Lisp_Object Vmessages_buffer_name;
472
473 /* Current, index 0, and last displayed echo area message. Either
474 buffers from echo_buffers, or nil to indicate no message. */
475
476 Lisp_Object echo_area_buffer[2];
477
478 /* The buffers referenced from echo_area_buffer. */
479
480 static Lisp_Object echo_buffer[2];
481
482 /* A vector saved used in with_area_buffer to reduce consing. */
483
484 static Lisp_Object Vwith_echo_area_save_vector;
485
486 /* Non-zero means display_echo_area should display the last echo area
487 message again. Set by redisplay_preserve_echo_area. */
488
489 static int display_last_displayed_message_p;
490
491 /* Nonzero if echo area is being used by print; zero if being used by
492 message. */
493
494 int message_buf_print;
495
496 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
497
498 Lisp_Object Qinhibit_menubar_update;
499 int inhibit_menubar_update;
500
501 /* Maximum height for resizing mini-windows. Either a float
502 specifying a fraction of the available height, or an integer
503 specifying a number of lines. */
504
505 Lisp_Object Vmax_mini_window_height;
506
507 /* Non-zero means messages should be displayed with truncated
508 lines instead of being continued. */
509
510 int message_truncate_lines;
511 Lisp_Object Qmessage_truncate_lines;
512
513 /* Non-zero means we want a hollow cursor in windows that are not
514 selected. Zero means there's no cursor in such windows. */
515
516 int cursor_in_non_selected_windows;
517
518 /* A scratch glyph row with contents used for generating truncation
519 glyphs. Also used in direct_output_for_insert. */
520
521 #define MAX_SCRATCH_GLYPHS 100
522 struct glyph_row scratch_glyph_row;
523 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
524
525 /* Ascent and height of the last line processed by move_it_to. */
526
527 static int last_max_ascent, last_height;
528
529 /* Non-zero if there's a help-echo in the echo area. */
530
531 int help_echo_showing_p;
532
533 /* If >= 0, computed, exact values of mode-line and header-line height
534 to use in the macros CURRENT_MODE_LINE_HEIGHT and
535 CURRENT_HEADER_LINE_HEIGHT. */
536
537 int current_mode_line_height, current_header_line_height;
538
539 /* The maximum distance to look ahead for text properties. Values
540 that are too small let us call compute_char_face and similar
541 functions too often which is expensive. Values that are too large
542 let us call compute_char_face and alike too often because we
543 might not be interested in text properties that far away. */
544
545 #define TEXT_PROP_DISTANCE_LIMIT 100
546
547 #if GLYPH_DEBUG
548
549 /* Non-zero means print traces of redisplay if compiled with
550 GLYPH_DEBUG != 0. */
551
552 int trace_redisplay_p;
553
554 #endif /* GLYPH_DEBUG */
555
556 #ifdef DEBUG_TRACE_MOVE
557 /* Non-zero means trace with TRACE_MOVE to stderr. */
558 int trace_move;
559
560 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
561 #else
562 #define TRACE_MOVE(x) (void) 0
563 #endif
564
565 /* Non-zero means automatically scroll windows horizontally to make
566 point visible. */
567
568 int automatic_hscrolling_p;
569
570 /* A list of symbols, one for each supported image type. */
571
572 Lisp_Object Vimage_types;
573
574 /* The variable `resize-mini-windows'. If nil, don't resize
575 mini-windows. If t, always resize them to fit the text they
576 display. If `grow-only', let mini-windows grow only until they
577 become empty. */
578
579 Lisp_Object Vresize_mini_windows;
580
581 /* Value returned from text property handlers (see below). */
582
583 enum prop_handled
584 {
585 HANDLED_NORMALLY,
586 HANDLED_RECOMPUTE_PROPS,
587 HANDLED_OVERLAY_STRING_CONSUMED,
588 HANDLED_RETURN
589 };
590
591 /* A description of text properties that redisplay is interested
592 in. */
593
594 struct props
595 {
596 /* The name of the property. */
597 Lisp_Object *name;
598
599 /* A unique index for the property. */
600 enum prop_idx idx;
601
602 /* A handler function called to set up iterator IT from the property
603 at IT's current position. Value is used to steer handle_stop. */
604 enum prop_handled (*handler) P_ ((struct it *it));
605 };
606
607 static enum prop_handled handle_face_prop P_ ((struct it *));
608 static enum prop_handled handle_invisible_prop P_ ((struct it *));
609 static enum prop_handled handle_display_prop P_ ((struct it *));
610 static enum prop_handled handle_composition_prop P_ ((struct it *));
611 static enum prop_handled handle_overlay_change P_ ((struct it *));
612 static enum prop_handled handle_fontified_prop P_ ((struct it *));
613
614 /* Properties handled by iterators. */
615
616 static struct props it_props[] =
617 {
618 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
619 /* Handle `face' before `display' because some sub-properties of
620 `display' need to know the face. */
621 {&Qface, FACE_PROP_IDX, handle_face_prop},
622 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
623 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
624 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
625 {NULL, 0, NULL}
626 };
627
628 /* Value is the position described by X. If X is a marker, value is
629 the marker_position of X. Otherwise, value is X. */
630
631 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
632
633 /* Enumeration returned by some move_it_.* functions internally. */
634
635 enum move_it_result
636 {
637 /* Not used. Undefined value. */
638 MOVE_UNDEFINED,
639
640 /* Move ended at the requested buffer position or ZV. */
641 MOVE_POS_MATCH_OR_ZV,
642
643 /* Move ended at the requested X pixel position. */
644 MOVE_X_REACHED,
645
646 /* Move within a line ended at the end of a line that must be
647 continued. */
648 MOVE_LINE_CONTINUED,
649
650 /* Move within a line ended at the end of a line that would
651 be displayed truncated. */
652 MOVE_LINE_TRUNCATED,
653
654 /* Move within a line ended at a line end. */
655 MOVE_NEWLINE_OR_CR
656 };
657
658
659 \f
660 /* Function prototypes. */
661
662 static void setup_for_ellipsis P_ ((struct it *));
663 static void mark_window_display_accurate_1 P_ ((struct window *, int));
664 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
665 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
666 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
667 static int redisplay_mode_lines P_ ((Lisp_Object, int));
668 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
669 static int invisible_text_between_p P_ ((struct it *, int, int));
670 static int next_element_from_ellipsis P_ ((struct it *));
671 static void pint2str P_ ((char *, int, int));
672 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
673 struct text_pos));
674 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
675 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
676 static void store_frame_title_char P_ ((char));
677 static int store_frame_title P_ ((unsigned char *, int, int));
678 static void x_consider_frame_title P_ ((Lisp_Object));
679 static void handle_stop P_ ((struct it *));
680 static int tool_bar_lines_needed P_ ((struct frame *));
681 static int single_display_prop_intangible_p P_ ((Lisp_Object));
682 static void ensure_echo_area_buffers P_ ((void));
683 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
684 struct glyph_row *,
685 struct glyph_row *));
686 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
687 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
688 static int with_echo_area_buffer P_ ((struct window *, int,
689 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
690 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
691 static void clear_garbaged_frames P_ ((void));
692 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
693 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
694 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
695 static int display_echo_area P_ ((struct window *));
696 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
697 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
698 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
699 static int string_char_and_length P_ ((unsigned char *, int, int *));
700 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
701 struct text_pos));
702 static int compute_window_start_on_continuation_line P_ ((struct window *));
703 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
704 static void insert_left_trunc_glyphs P_ ((struct it *));
705 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
706 static void extend_face_to_end_of_line P_ ((struct it *));
707 static int append_space P_ ((struct it *, int));
708 static void make_cursor_line_fully_visible P_ ((struct window *));
709 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
710 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
711 static int trailing_whitespace_p P_ ((int));
712 static int message_log_check_duplicate P_ ((int, int, int, int));
713 int invisible_p P_ ((Lisp_Object, Lisp_Object));
714 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
715 static void push_it P_ ((struct it *));
716 static void pop_it P_ ((struct it *));
717 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
718 static void redisplay_internal P_ ((int));
719 static int echo_area_display P_ ((int));
720 static void redisplay_windows P_ ((Lisp_Object));
721 static void redisplay_window P_ ((Lisp_Object, int));
722 static void update_menu_bar P_ ((struct frame *, int));
723 static int try_window_reusing_current_matrix P_ ((struct window *));
724 static int try_window_id P_ ((struct window *));
725 static int display_line P_ ((struct it *));
726 static int display_mode_lines P_ ((struct window *));
727 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
728 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
729 static char *decode_mode_spec P_ ((struct window *, int, int, int));
730 static void display_menu_bar P_ ((struct window *));
731 static int display_count_lines P_ ((int, int, int, int, int *));
732 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
733 int, int, struct it *, int, int, int, int));
734 static void compute_line_metrics P_ ((struct it *));
735 static void run_redisplay_end_trigger_hook P_ ((struct it *));
736 static int get_overlay_strings P_ ((struct it *, int));
737 static void next_overlay_string P_ ((struct it *));
738 static void reseat P_ ((struct it *, struct text_pos, int));
739 static void reseat_1 P_ ((struct it *, struct text_pos, int));
740 static void back_to_previous_visible_line_start P_ ((struct it *));
741 static void reseat_at_previous_visible_line_start P_ ((struct it *));
742 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
743 static int next_element_from_display_vector P_ ((struct it *));
744 static int next_element_from_string P_ ((struct it *));
745 static int next_element_from_c_string P_ ((struct it *));
746 static int next_element_from_buffer P_ ((struct it *));
747 static int next_element_from_composition P_ ((struct it *));
748 static int next_element_from_image P_ ((struct it *));
749 static int next_element_from_stretch P_ ((struct it *));
750 static void load_overlay_strings P_ ((struct it *, int));
751 static void init_from_display_pos P_ ((struct it *, struct window *,
752 struct display_pos *));
753 static void reseat_to_string P_ ((struct it *, unsigned char *,
754 Lisp_Object, int, int, int, int));
755 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
756 int, int, int));
757 void move_it_vertically_backward P_ ((struct it *, int));
758 static void init_to_row_start P_ ((struct it *, struct window *,
759 struct glyph_row *));
760 static void init_to_row_end P_ ((struct it *, struct window *,
761 struct glyph_row *));
762 static void back_to_previous_line_start P_ ((struct it *));
763 static int forward_to_next_line_start P_ ((struct it *, int *));
764 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
765 Lisp_Object, int));
766 static struct text_pos string_pos P_ ((int, Lisp_Object));
767 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
768 static int number_of_chars P_ ((unsigned char *, int));
769 static void compute_stop_pos P_ ((struct it *));
770 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
771 Lisp_Object));
772 static int face_before_or_after_it_pos P_ ((struct it *, int));
773 static int next_overlay_change P_ ((int));
774 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
775 Lisp_Object, struct text_pos *,
776 int));
777 static int underlying_face_id P_ ((struct it *));
778 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
779 struct window *));
780
781 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
782 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
783
784 #ifdef HAVE_WINDOW_SYSTEM
785
786 static void update_tool_bar P_ ((struct frame *, int));
787 static void build_desired_tool_bar_string P_ ((struct frame *f));
788 static int redisplay_tool_bar P_ ((struct frame *));
789 static void display_tool_bar_line P_ ((struct it *));
790
791 #endif /* HAVE_WINDOW_SYSTEM */
792
793 \f
794 /***********************************************************************
795 Window display dimensions
796 ***********************************************************************/
797
798 /* Return the window-relative maximum y + 1 for glyph rows displaying
799 text in window W. This is the height of W minus the height of a
800 mode line, if any. */
801
802 INLINE int
803 window_text_bottom_y (w)
804 struct window *w;
805 {
806 struct frame *f = XFRAME (w->frame);
807 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
808
809 if (WINDOW_WANTS_MODELINE_P (w))
810 height -= CURRENT_MODE_LINE_HEIGHT (w);
811 return height;
812 }
813
814
815 /* Return the pixel width of display area AREA of window W. AREA < 0
816 means return the total width of W, not including bitmap areas to
817 the left and right of the window. */
818
819 INLINE int
820 window_box_width (w, area)
821 struct window *w;
822 int area;
823 {
824 struct frame *f = XFRAME (w->frame);
825 int width = XFASTINT (w->width);
826
827 if (!w->pseudo_window_p)
828 {
829 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
830
831 if (area == TEXT_AREA)
832 {
833 if (INTEGERP (w->left_margin_width))
834 width -= XFASTINT (w->left_margin_width);
835 if (INTEGERP (w->right_margin_width))
836 width -= XFASTINT (w->right_margin_width);
837 }
838 else if (area == LEFT_MARGIN_AREA)
839 width = (INTEGERP (w->left_margin_width)
840 ? XFASTINT (w->left_margin_width) : 0);
841 else if (area == RIGHT_MARGIN_AREA)
842 width = (INTEGERP (w->right_margin_width)
843 ? XFASTINT (w->right_margin_width) : 0);
844 }
845
846 return width * CANON_X_UNIT (f);
847 }
848
849
850 /* Return the pixel height of the display area of window W, not
851 including mode lines of W, if any.. */
852
853 INLINE int
854 window_box_height (w)
855 struct window *w;
856 {
857 struct frame *f = XFRAME (w->frame);
858 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
859
860 xassert (height >= 0);
861
862 /* Note: the code below that determines the mode-line/header-line
863 height is essentially the same as that contained in the macro
864 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
865 the appropriate glyph row has its `mode_line_p' flag set,
866 and if it doesn't, uses estimate_mode_line_height instead. */
867
868 if (WINDOW_WANTS_MODELINE_P (w))
869 {
870 struct glyph_row *ml_row
871 = (w->current_matrix && w->current_matrix->rows
872 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
873 : 0);
874 if (ml_row && ml_row->mode_line_p)
875 height -= ml_row->height;
876 else
877 height -= estimate_mode_line_height (f, MODE_LINE_FACE_ID);
878 }
879
880 if (WINDOW_WANTS_HEADER_LINE_P (w))
881 {
882 struct glyph_row *hl_row
883 = (w->current_matrix && w->current_matrix->rows
884 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
885 : 0);
886 if (hl_row && hl_row->mode_line_p)
887 height -= hl_row->height;
888 else
889 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
890 }
891
892 return height;
893 }
894
895
896 /* Return the frame-relative coordinate of the left edge of display
897 area AREA of window W. AREA < 0 means return the left edge of the
898 whole window, to the right of any bitmap area at the left side of
899 W. */
900
901 INLINE int
902 window_box_left (w, area)
903 struct window *w;
904 int area;
905 {
906 struct frame *f = XFRAME (w->frame);
907 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
908
909 if (!w->pseudo_window_p)
910 {
911 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
912 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
913
914 if (area == TEXT_AREA)
915 x += window_box_width (w, LEFT_MARGIN_AREA);
916 else if (area == RIGHT_MARGIN_AREA)
917 x += (window_box_width (w, LEFT_MARGIN_AREA)
918 + window_box_width (w, TEXT_AREA));
919 }
920
921 return x;
922 }
923
924
925 /* Return the frame-relative coordinate of the right edge of display
926 area AREA of window W. AREA < 0 means return the left edge of the
927 whole window, to the left of any bitmap area at the right side of
928 W. */
929
930 INLINE int
931 window_box_right (w, area)
932 struct window *w;
933 int area;
934 {
935 return window_box_left (w, area) + window_box_width (w, area);
936 }
937
938
939 /* Get the bounding box of the display area AREA of window W, without
940 mode lines, in frame-relative coordinates. AREA < 0 means the
941 whole window, not including bitmap areas to the left and right of
942 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
943 coordinates of the upper-left corner of the box. Return in
944 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
945
946 INLINE void
947 window_box (w, area, box_x, box_y, box_width, box_height)
948 struct window *w;
949 int area;
950 int *box_x, *box_y, *box_width, *box_height;
951 {
952 struct frame *f = XFRAME (w->frame);
953
954 *box_width = window_box_width (w, area);
955 *box_height = window_box_height (w);
956 *box_x = window_box_left (w, area);
957 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
958 + XFASTINT (w->top) * CANON_Y_UNIT (f));
959 if (WINDOW_WANTS_HEADER_LINE_P (w))
960 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
961 }
962
963
964 /* Get the bounding box of the display area AREA of window W, without
965 mode lines. AREA < 0 means the whole window, not including bitmap
966 areas to the left and right of the window. Return in *TOP_LEFT_X
967 and TOP_LEFT_Y the frame-relative pixel coordinates of the
968 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
969 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
970 box. */
971
972 INLINE void
973 window_box_edges (w, area, top_left_x, top_left_y,
974 bottom_right_x, bottom_right_y)
975 struct window *w;
976 int area;
977 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
978 {
979 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
980 bottom_right_y);
981 *bottom_right_x += *top_left_x;
982 *bottom_right_y += *top_left_y;
983 }
984
985
986 \f
987 /***********************************************************************
988 Utilities
989 ***********************************************************************/
990
991 /* Return the bottom y-position of the line the iterator IT is in.
992 This can modify IT's settings. */
993
994 int
995 line_bottom_y (it)
996 struct it *it;
997 {
998 int line_height = it->max_ascent + it->max_descent;
999 int line_top_y = it->current_y;
1000
1001 if (line_height == 0)
1002 {
1003 if (last_height)
1004 line_height = last_height;
1005 else if (IT_CHARPOS (*it) < ZV)
1006 {
1007 move_it_by_lines (it, 1, 1);
1008 line_height = (it->max_ascent || it->max_descent
1009 ? it->max_ascent + it->max_descent
1010 : last_height);
1011 }
1012 else
1013 {
1014 struct glyph_row *row = it->glyph_row;
1015
1016 /* Use the default character height. */
1017 it->glyph_row = NULL;
1018 it->what = IT_CHARACTER;
1019 it->c = ' ';
1020 it->len = 1;
1021 PRODUCE_GLYPHS (it);
1022 line_height = it->ascent + it->descent;
1023 it->glyph_row = row;
1024 }
1025 }
1026
1027 return line_top_y + line_height;
1028 }
1029
1030
1031 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1032 1 if POS is visible and the line containing POS is fully visible.
1033 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1034 and header-lines heights. */
1035
1036 int
1037 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1038 struct window *w;
1039 int charpos, *fully, exact_mode_line_heights_p;
1040 {
1041 struct it it;
1042 struct text_pos top;
1043 int visible_p;
1044 struct buffer *old_buffer = NULL;
1045
1046 if (XBUFFER (w->buffer) != current_buffer)
1047 {
1048 old_buffer = current_buffer;
1049 set_buffer_internal_1 (XBUFFER (w->buffer));
1050 }
1051
1052 *fully = visible_p = 0;
1053 SET_TEXT_POS_FROM_MARKER (top, w->start);
1054
1055 /* Compute exact mode line heights, if requested. */
1056 if (exact_mode_line_heights_p)
1057 {
1058 if (WINDOW_WANTS_MODELINE_P (w))
1059 current_mode_line_height
1060 = display_mode_line (w, MODE_LINE_FACE_ID,
1061 current_buffer->mode_line_format);
1062
1063 if (WINDOW_WANTS_HEADER_LINE_P (w))
1064 current_header_line_height
1065 = display_mode_line (w, HEADER_LINE_FACE_ID,
1066 current_buffer->header_line_format);
1067 }
1068
1069 start_display (&it, w, top);
1070 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1071 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1072
1073 /* Note that we may overshoot because of invisible text. */
1074 if (IT_CHARPOS (it) >= charpos)
1075 {
1076 int top_y = it.current_y;
1077 int bottom_y = line_bottom_y (&it);
1078 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1079
1080 if (top_y < window_top_y)
1081 visible_p = bottom_y > window_top_y;
1082 else if (top_y < it.last_visible_y)
1083 {
1084 visible_p = 1;
1085 *fully = bottom_y <= it.last_visible_y;
1086 }
1087 }
1088 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1089 {
1090 move_it_by_lines (&it, 1, 0);
1091 if (charpos < IT_CHARPOS (it))
1092 {
1093 visible_p = 1;
1094 *fully = 0;
1095 }
1096 }
1097
1098 if (old_buffer)
1099 set_buffer_internal_1 (old_buffer);
1100
1101 current_header_line_height = current_mode_line_height = -1;
1102 return visible_p;
1103 }
1104
1105
1106 /* Return the next character from STR which is MAXLEN bytes long.
1107 Return in *LEN the length of the character. This is like
1108 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1109 we find one, we return a `?', but with the length of the invalid
1110 character. */
1111
1112 static INLINE int
1113 string_char_and_length (str, maxlen, len)
1114 unsigned char *str;
1115 int maxlen, *len;
1116 {
1117 int c;
1118
1119 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1120 if (!CHAR_VALID_P (c, 1))
1121 /* We may not change the length here because other places in Emacs
1122 don't use this function, i.e. they silently accept invalid
1123 characters. */
1124 c = '?';
1125
1126 return c;
1127 }
1128
1129
1130
1131 /* Given a position POS containing a valid character and byte position
1132 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1133
1134 static struct text_pos
1135 string_pos_nchars_ahead (pos, string, nchars)
1136 struct text_pos pos;
1137 Lisp_Object string;
1138 int nchars;
1139 {
1140 xassert (STRINGP (string) && nchars >= 0);
1141
1142 if (STRING_MULTIBYTE (string))
1143 {
1144 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1145 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1146 int len;
1147
1148 while (nchars--)
1149 {
1150 string_char_and_length (p, rest, &len);
1151 p += len, rest -= len;
1152 xassert (rest >= 0);
1153 CHARPOS (pos) += 1;
1154 BYTEPOS (pos) += len;
1155 }
1156 }
1157 else
1158 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1159
1160 return pos;
1161 }
1162
1163
1164 /* Value is the text position, i.e. character and byte position,
1165 for character position CHARPOS in STRING. */
1166
1167 static INLINE struct text_pos
1168 string_pos (charpos, string)
1169 int charpos;
1170 Lisp_Object string;
1171 {
1172 struct text_pos pos;
1173 xassert (STRINGP (string));
1174 xassert (charpos >= 0);
1175 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1176 return pos;
1177 }
1178
1179
1180 /* Value is a text position, i.e. character and byte position, for
1181 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1182 means recognize multibyte characters. */
1183
1184 static struct text_pos
1185 c_string_pos (charpos, s, multibyte_p)
1186 int charpos;
1187 unsigned char *s;
1188 int multibyte_p;
1189 {
1190 struct text_pos pos;
1191
1192 xassert (s != NULL);
1193 xassert (charpos >= 0);
1194
1195 if (multibyte_p)
1196 {
1197 int rest = strlen (s), len;
1198
1199 SET_TEXT_POS (pos, 0, 0);
1200 while (charpos--)
1201 {
1202 string_char_and_length (s, rest, &len);
1203 s += len, rest -= len;
1204 xassert (rest >= 0);
1205 CHARPOS (pos) += 1;
1206 BYTEPOS (pos) += len;
1207 }
1208 }
1209 else
1210 SET_TEXT_POS (pos, charpos, charpos);
1211
1212 return pos;
1213 }
1214
1215
1216 /* Value is the number of characters in C string S. MULTIBYTE_P
1217 non-zero means recognize multibyte characters. */
1218
1219 static int
1220 number_of_chars (s, multibyte_p)
1221 unsigned char *s;
1222 int multibyte_p;
1223 {
1224 int nchars;
1225
1226 if (multibyte_p)
1227 {
1228 int rest = strlen (s), len;
1229 unsigned char *p = (unsigned char *) s;
1230
1231 for (nchars = 0; rest > 0; ++nchars)
1232 {
1233 string_char_and_length (p, rest, &len);
1234 rest -= len, p += len;
1235 }
1236 }
1237 else
1238 nchars = strlen (s);
1239
1240 return nchars;
1241 }
1242
1243
1244 /* Compute byte position NEWPOS->bytepos corresponding to
1245 NEWPOS->charpos. POS is a known position in string STRING.
1246 NEWPOS->charpos must be >= POS.charpos. */
1247
1248 static void
1249 compute_string_pos (newpos, pos, string)
1250 struct text_pos *newpos, pos;
1251 Lisp_Object string;
1252 {
1253 xassert (STRINGP (string));
1254 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1255
1256 if (STRING_MULTIBYTE (string))
1257 *newpos = string_pos_nchars_ahead (pos, string,
1258 CHARPOS (*newpos) - CHARPOS (pos));
1259 else
1260 BYTEPOS (*newpos) = CHARPOS (*newpos);
1261 }
1262
1263
1264 \f
1265 /***********************************************************************
1266 Lisp form evaluation
1267 ***********************************************************************/
1268
1269 /* Error handler for safe_eval and safe_call. */
1270
1271 static Lisp_Object
1272 safe_eval_handler (arg)
1273 Lisp_Object arg;
1274 {
1275 add_to_log ("Error during redisplay: %s", arg, Qnil);
1276 return Qnil;
1277 }
1278
1279
1280 /* Evaluate SEXPR and return the result, or nil if something went
1281 wrong. */
1282
1283 Lisp_Object
1284 safe_eval (sexpr)
1285 Lisp_Object sexpr;
1286 {
1287 Lisp_Object val;
1288
1289 if (inhibit_eval_during_redisplay)
1290 val = Qnil;
1291 else
1292 {
1293 int count = BINDING_STACK_SIZE ();
1294 struct gcpro gcpro1;
1295
1296 GCPRO1 (sexpr);
1297 specbind (Qinhibit_redisplay, Qt);
1298 val = internal_condition_case_1 (Feval, sexpr, Qerror,
1299 safe_eval_handler);
1300 UNGCPRO;
1301 val = unbind_to (count, val);
1302 }
1303
1304 return val;
1305 }
1306
1307
1308 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1309 Return the result, or nil if something went wrong. */
1310
1311 Lisp_Object
1312 safe_call (nargs, args)
1313 int nargs;
1314 Lisp_Object *args;
1315 {
1316 Lisp_Object val;
1317
1318 if (inhibit_eval_during_redisplay)
1319 val = Qnil;
1320 else
1321 {
1322 int count = BINDING_STACK_SIZE ();
1323 struct gcpro gcpro1;
1324
1325 GCPRO1 (args[0]);
1326 gcpro1.nvars = nargs;
1327 specbind (Qinhibit_redisplay, Qt);
1328 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1329 safe_eval_handler);
1330 UNGCPRO;
1331 val = unbind_to (count, val);
1332 }
1333
1334 return val;
1335 }
1336
1337
1338 /* Call function FN with one argument ARG.
1339 Return the result, or nil if something went wrong. */
1340
1341 Lisp_Object
1342 safe_call1 (fn, arg)
1343 Lisp_Object fn, arg;
1344 {
1345 Lisp_Object args[2];
1346 args[0] = fn;
1347 args[1] = arg;
1348 return safe_call (2, args);
1349 }
1350
1351
1352 \f
1353 /***********************************************************************
1354 Debugging
1355 ***********************************************************************/
1356
1357 #if 0
1358
1359 /* Define CHECK_IT to perform sanity checks on iterators.
1360 This is for debugging. It is too slow to do unconditionally. */
1361
1362 static void
1363 check_it (it)
1364 struct it *it;
1365 {
1366 if (it->method == next_element_from_string)
1367 {
1368 xassert (STRINGP (it->string));
1369 xassert (IT_STRING_CHARPOS (*it) >= 0);
1370 }
1371 else if (it->method == next_element_from_buffer)
1372 {
1373 /* Check that character and byte positions agree. */
1374 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1375 }
1376
1377 if (it->dpvec)
1378 xassert (it->current.dpvec_index >= 0);
1379 else
1380 xassert (it->current.dpvec_index < 0);
1381 }
1382
1383 #define CHECK_IT(IT) check_it ((IT))
1384
1385 #else /* not 0 */
1386
1387 #define CHECK_IT(IT) (void) 0
1388
1389 #endif /* not 0 */
1390
1391
1392 #if GLYPH_DEBUG
1393
1394 /* Check that the window end of window W is what we expect it
1395 to be---the last row in the current matrix displaying text. */
1396
1397 static void
1398 check_window_end (w)
1399 struct window *w;
1400 {
1401 if (!MINI_WINDOW_P (w)
1402 && !NILP (w->window_end_valid))
1403 {
1404 struct glyph_row *row;
1405 xassert ((row = MATRIX_ROW (w->current_matrix,
1406 XFASTINT (w->window_end_vpos)),
1407 !row->enabled_p
1408 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1409 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1410 }
1411 }
1412
1413 #define CHECK_WINDOW_END(W) check_window_end ((W))
1414
1415 #else /* not GLYPH_DEBUG */
1416
1417 #define CHECK_WINDOW_END(W) (void) 0
1418
1419 #endif /* not GLYPH_DEBUG */
1420
1421
1422 \f
1423 /***********************************************************************
1424 Iterator initialization
1425 ***********************************************************************/
1426
1427 /* Initialize IT for displaying current_buffer in window W, starting
1428 at character position CHARPOS. CHARPOS < 0 means that no buffer
1429 position is specified which is useful when the iterator is assigned
1430 a position later. BYTEPOS is the byte position corresponding to
1431 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1432
1433 If ROW is not null, calls to produce_glyphs with IT as parameter
1434 will produce glyphs in that row.
1435
1436 BASE_FACE_ID is the id of a base face to use. It must be one of
1437 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1438 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1439 displaying the tool-bar.
1440
1441 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1442 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1443 corresponding mode line glyph row of the desired matrix of W. */
1444
1445 void
1446 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1447 struct it *it;
1448 struct window *w;
1449 int charpos, bytepos;
1450 struct glyph_row *row;
1451 enum face_id base_face_id;
1452 {
1453 int highlight_region_p;
1454
1455 /* Some precondition checks. */
1456 xassert (w != NULL && it != NULL);
1457 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1458
1459 /* If face attributes have been changed since the last redisplay,
1460 free realized faces now because they depend on face definitions
1461 that might have changed. */
1462 if (face_change_count)
1463 {
1464 face_change_count = 0;
1465 free_all_realized_faces (Qnil);
1466 }
1467
1468 /* Use one of the mode line rows of W's desired matrix if
1469 appropriate. */
1470 if (row == NULL)
1471 {
1472 if (base_face_id == MODE_LINE_FACE_ID)
1473 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1474 else if (base_face_id == HEADER_LINE_FACE_ID)
1475 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1476 }
1477
1478 /* Clear IT. */
1479 bzero (it, sizeof *it);
1480 it->current.overlay_string_index = -1;
1481 it->current.dpvec_index = -1;
1482 it->base_face_id = base_face_id;
1483
1484 /* The window in which we iterate over current_buffer: */
1485 XSETWINDOW (it->window, w);
1486 it->w = w;
1487 it->f = XFRAME (w->frame);
1488
1489 /* Extra space between lines (on window systems only). */
1490 if (base_face_id == DEFAULT_FACE_ID
1491 && FRAME_WINDOW_P (it->f))
1492 {
1493 if (NATNUMP (current_buffer->extra_line_spacing))
1494 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1495 else if (it->f->extra_line_spacing > 0)
1496 it->extra_line_spacing = it->f->extra_line_spacing;
1497 }
1498
1499 /* If realized faces have been removed, e.g. because of face
1500 attribute changes of named faces, recompute them. When running
1501 in batch mode, the face cache of Vterminal_frame is null. If
1502 we happen to get called, make a dummy face cache. */
1503 if (
1504 #ifndef WINDOWSNT
1505 noninteractive &&
1506 #endif
1507 FRAME_FACE_CACHE (it->f) == NULL)
1508 init_frame_faces (it->f);
1509 if (FRAME_FACE_CACHE (it->f)->used == 0)
1510 recompute_basic_faces (it->f);
1511
1512 /* Current value of the `space-width', and 'height' properties. */
1513 it->space_width = Qnil;
1514 it->font_height = Qnil;
1515
1516 /* Are control characters displayed as `^C'? */
1517 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1518
1519 /* -1 means everything between a CR and the following line end
1520 is invisible. >0 means lines indented more than this value are
1521 invisible. */
1522 it->selective = (INTEGERP (current_buffer->selective_display)
1523 ? XFASTINT (current_buffer->selective_display)
1524 : (!NILP (current_buffer->selective_display)
1525 ? -1 : 0));
1526 it->selective_display_ellipsis_p
1527 = !NILP (current_buffer->selective_display_ellipses);
1528
1529 /* Display table to use. */
1530 it->dp = window_display_table (w);
1531
1532 /* Are multibyte characters enabled in current_buffer? */
1533 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1534
1535 /* Non-zero if we should highlight the region. */
1536 highlight_region_p
1537 = (!NILP (Vtransient_mark_mode)
1538 && !NILP (current_buffer->mark_active)
1539 && XMARKER (current_buffer->mark)->buffer != 0);
1540
1541 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1542 start and end of a visible region in window IT->w. Set both to
1543 -1 to indicate no region. */
1544 if (highlight_region_p
1545 /* Maybe highlight only in selected window. */
1546 && (/* Either show region everywhere. */
1547 highlight_nonselected_windows
1548 /* Or show region in the selected window. */
1549 || w == XWINDOW (selected_window)
1550 /* Or show the region if we are in the mini-buffer and W is
1551 the window the mini-buffer refers to. */
1552 || (MINI_WINDOW_P (XWINDOW (selected_window))
1553 && WINDOWP (Vminibuf_scroll_window)
1554 && w == XWINDOW (Vminibuf_scroll_window))))
1555 {
1556 int charpos = marker_position (current_buffer->mark);
1557 it->region_beg_charpos = min (PT, charpos);
1558 it->region_end_charpos = max (PT, charpos);
1559 }
1560 else
1561 it->region_beg_charpos = it->region_end_charpos = -1;
1562
1563 /* Get the position at which the redisplay_end_trigger hook should
1564 be run, if it is to be run at all. */
1565 if (MARKERP (w->redisplay_end_trigger)
1566 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1567 it->redisplay_end_trigger_charpos
1568 = marker_position (w->redisplay_end_trigger);
1569 else if (INTEGERP (w->redisplay_end_trigger))
1570 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1571
1572 /* Correct bogus values of tab_width. */
1573 it->tab_width = XINT (current_buffer->tab_width);
1574 if (it->tab_width <= 0 || it->tab_width > 1000)
1575 it->tab_width = 8;
1576
1577 /* Are lines in the display truncated? */
1578 it->truncate_lines_p
1579 = (base_face_id != DEFAULT_FACE_ID
1580 || XINT (it->w->hscroll)
1581 || (truncate_partial_width_windows
1582 && !WINDOW_FULL_WIDTH_P (it->w))
1583 || !NILP (current_buffer->truncate_lines));
1584
1585 /* Get dimensions of truncation and continuation glyphs. These are
1586 displayed as bitmaps under X, so we don't need them for such
1587 frames. */
1588 if (!FRAME_WINDOW_P (it->f))
1589 {
1590 if (it->truncate_lines_p)
1591 {
1592 /* We will need the truncation glyph. */
1593 xassert (it->glyph_row == NULL);
1594 produce_special_glyphs (it, IT_TRUNCATION);
1595 it->truncation_pixel_width = it->pixel_width;
1596 }
1597 else
1598 {
1599 /* We will need the continuation glyph. */
1600 xassert (it->glyph_row == NULL);
1601 produce_special_glyphs (it, IT_CONTINUATION);
1602 it->continuation_pixel_width = it->pixel_width;
1603 }
1604
1605 /* Reset these values to zero becaue the produce_special_glyphs
1606 above has changed them. */
1607 it->pixel_width = it->ascent = it->descent = 0;
1608 it->phys_ascent = it->phys_descent = 0;
1609 }
1610
1611 /* Set this after getting the dimensions of truncation and
1612 continuation glyphs, so that we don't produce glyphs when calling
1613 produce_special_glyphs, above. */
1614 it->glyph_row = row;
1615 it->area = TEXT_AREA;
1616
1617 /* Get the dimensions of the display area. The display area
1618 consists of the visible window area plus a horizontally scrolled
1619 part to the left of the window. All x-values are relative to the
1620 start of this total display area. */
1621 if (base_face_id != DEFAULT_FACE_ID)
1622 {
1623 /* Mode lines, menu bar in terminal frames. */
1624 it->first_visible_x = 0;
1625 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1626 }
1627 else
1628 {
1629 it->first_visible_x
1630 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1631 it->last_visible_x = (it->first_visible_x
1632 + window_box_width (w, TEXT_AREA));
1633
1634 /* If we truncate lines, leave room for the truncator glyph(s) at
1635 the right margin. Otherwise, leave room for the continuation
1636 glyph(s). Truncation and continuation glyphs are not inserted
1637 for window-based redisplay. */
1638 if (!FRAME_WINDOW_P (it->f))
1639 {
1640 if (it->truncate_lines_p)
1641 it->last_visible_x -= it->truncation_pixel_width;
1642 else
1643 it->last_visible_x -= it->continuation_pixel_width;
1644 }
1645
1646 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1647 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1648 }
1649
1650 /* Leave room for a border glyph. */
1651 if (!FRAME_WINDOW_P (it->f)
1652 && !WINDOW_RIGHTMOST_P (it->w))
1653 it->last_visible_x -= 1;
1654
1655 it->last_visible_y = window_text_bottom_y (w);
1656
1657 /* For mode lines and alike, arrange for the first glyph having a
1658 left box line if the face specifies a box. */
1659 if (base_face_id != DEFAULT_FACE_ID)
1660 {
1661 struct face *face;
1662
1663 it->face_id = base_face_id;
1664
1665 /* If we have a boxed mode line, make the first character appear
1666 with a left box line. */
1667 face = FACE_FROM_ID (it->f, base_face_id);
1668 if (face->box != FACE_NO_BOX)
1669 it->start_of_box_run_p = 1;
1670 }
1671
1672 /* If a buffer position was specified, set the iterator there,
1673 getting overlays and face properties from that position. */
1674 if (charpos > 0)
1675 {
1676 it->end_charpos = ZV;
1677 it->face_id = -1;
1678 IT_CHARPOS (*it) = charpos;
1679
1680 /* Compute byte position if not specified. */
1681 if (bytepos <= 0)
1682 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1683 else
1684 IT_BYTEPOS (*it) = bytepos;
1685
1686 /* Compute faces etc. */
1687 reseat (it, it->current.pos, 1);
1688 }
1689
1690 CHECK_IT (it);
1691 }
1692
1693
1694 /* Initialize IT for the display of window W with window start POS. */
1695
1696 void
1697 start_display (it, w, pos)
1698 struct it *it;
1699 struct window *w;
1700 struct text_pos pos;
1701 {
1702 int start_at_line_beg_p;
1703 struct glyph_row *row;
1704 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1705 int first_y;
1706
1707 row = w->desired_matrix->rows + first_vpos;
1708 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1709 first_y = it->current_y;
1710
1711 /* If window start is not at a line start, move back to the line
1712 start. This makes sure that we take continuation lines into
1713 account. */
1714 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1715 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1716 if (!start_at_line_beg_p)
1717 reseat_at_previous_visible_line_start (it);
1718
1719 /* If window start is not at a line start, skip forward to POS to
1720 get the correct continuation_lines_width and current_x. */
1721 if (!start_at_line_beg_p)
1722 {
1723 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1724
1725 /* If lines are continued, this line may end in the middle of a
1726 multi-glyph character (e.g. a control character displayed as
1727 \003, or in the middle of an overlay string). In this case
1728 move_it_to above will not have taken us to the start of
1729 the continuation line but to the end of the continued line. */
1730 if (!it->truncate_lines_p)
1731 {
1732 if (it->current_x > 0)
1733 {
1734 if (it->current.dpvec_index >= 0
1735 || it->current.overlay_string_index >= 0)
1736 {
1737 set_iterator_to_next (it, 1);
1738 move_it_in_display_line_to (it, -1, -1, 0);
1739 }
1740
1741 it->continuation_lines_width += it->current_x;
1742 }
1743
1744 /* We're starting a new display line, not affected by the
1745 height of the continued line, so clear the appropriate
1746 fields in the iterator structure. */
1747 it->max_ascent = it->max_descent = 0;
1748 it->max_phys_ascent = it->max_phys_descent = 0;
1749 }
1750
1751 it->current_y = first_y;
1752 it->vpos = 0;
1753 it->current_x = it->hpos = 0;
1754 }
1755
1756 #if 0 /* Don't assert the following because start_display is sometimes
1757 called intentionally with a window start that is not at a
1758 line start. Please leave this code in as a comment. */
1759
1760 /* Window start should be on a line start, now. */
1761 xassert (it->continuation_lines_width
1762 || IT_CHARPOS (it) == BEGV
1763 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1764 #endif /* 0 */
1765 }
1766
1767
1768 /* Return 1 if POS is a position in ellipses displayed for invisible
1769 text. W is the window we display, for text property lookup. */
1770
1771 static int
1772 in_ellipses_for_invisible_text_p (pos, w)
1773 struct display_pos *pos;
1774 struct window *w;
1775 {
1776 Lisp_Object prop, window;
1777 int ellipses_p = 0;
1778 int charpos = CHARPOS (pos->pos);
1779
1780 /* If POS specifies a position in a display vector, this might
1781 be for an ellipsis displayed for invisible text. We won't
1782 get the iterator set up for delivering that ellipsis unless
1783 we make sure that it gets aware of the invisible text. */
1784 if (pos->dpvec_index >= 0
1785 && pos->overlay_string_index < 0
1786 && CHARPOS (pos->string_pos) < 0
1787 && charpos > BEGV
1788 && (XSETWINDOW (window, w),
1789 prop = Fget_char_property (make_number (charpos),
1790 Qinvisible, window),
1791 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1792 {
1793 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1794 window);
1795 if (TEXT_PROP_MEANS_INVISIBLE (prop)
1796 && TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop))
1797 ellipses_p = 1;
1798 }
1799
1800 return ellipses_p;
1801 }
1802
1803
1804 /* Initialize IT for stepping through current_buffer in window W,
1805 starting at position POS that includes overlay string and display
1806 vector/ control character translation position information. */
1807
1808 static void
1809 init_from_display_pos (it, w, pos)
1810 struct it *it;
1811 struct window *w;
1812 struct display_pos *pos;
1813 {
1814 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1815
1816 /* If POS specifies a position in a display vector, this might
1817 be for an ellipsis displayed for invisible text. We won't
1818 get the iterator set up for delivering that ellipsis unless
1819 we make sure that it gets aware of the invisible text. */
1820 if (in_ellipses_for_invisible_text_p (pos, w))
1821 {
1822 --charpos;
1823 bytepos = 0;
1824 }
1825
1826 /* Keep in mind: the call to reseat in init_iterator skips invisible
1827 text, so we might end up at a position different from POS. This
1828 is only a problem when POS is a row start after a newline and an
1829 overlay starts there with an after-string, and the overlay has an
1830 invisible property. Since we don't skip invisible text in
1831 display_line and elsewhere immediately after consuming the
1832 newline before the row start, such a POS will not be in a string,
1833 but the call to init_iterator below will move us to the
1834 after-string. */
1835 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1836
1837 /* If position is within an overlay string, set up IT to
1838 the right overlay string. */
1839 if (pos->overlay_string_index >= 0)
1840 {
1841 int relative_index;
1842
1843 /* We already have the first chunk of overlay strings in
1844 IT->overlay_strings. Load more until the one for
1845 pos->overlay_string_index is in IT->overlay_strings. */
1846 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1847 {
1848 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1849 it->current.overlay_string_index = 0;
1850 while (n--)
1851 {
1852 load_overlay_strings (it, 0);
1853 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1854 }
1855 }
1856
1857 it->current.overlay_string_index = pos->overlay_string_index;
1858 relative_index = (it->current.overlay_string_index
1859 % OVERLAY_STRING_CHUNK_SIZE);
1860 it->string = it->overlay_strings[relative_index];
1861 xassert (STRINGP (it->string));
1862 it->current.string_pos = pos->string_pos;
1863 it->method = next_element_from_string;
1864 }
1865 else if (it->current.overlay_string_index >= 0)
1866 {
1867 /* If POS says we're already after an overlay string ending at
1868 POS, make sure to pop the iterator because it will be in
1869 front of that overlay string. When POS is ZV, we've thereby
1870 also ``processed'' overlay strings at ZV. */
1871 while (it->sp)
1872 pop_it (it);
1873 it->current.overlay_string_index = -1;
1874 it->method = next_element_from_buffer;
1875 if (CHARPOS (pos->pos) == ZV)
1876 it->overlay_strings_at_end_processed_p = 1;
1877 }
1878
1879 if (CHARPOS (pos->string_pos) >= 0)
1880 {
1881 /* Recorded position is not in an overlay string, but in another
1882 string. This can only be a string from a `display' property.
1883 IT should already be filled with that string. */
1884 it->current.string_pos = pos->string_pos;
1885 xassert (STRINGP (it->string));
1886 }
1887
1888 /* Restore position in display vector translations, control
1889 character translations or ellipses. */
1890 if (pos->dpvec_index >= 0)
1891 {
1892 if (it->dpvec == NULL)
1893 get_next_display_element (it);
1894 xassert (it->dpvec && it->current.dpvec_index == 0);
1895 it->current.dpvec_index = pos->dpvec_index;
1896 }
1897
1898 CHECK_IT (it);
1899 }
1900
1901
1902 /* Initialize IT for stepping through current_buffer in window W
1903 starting at ROW->start. */
1904
1905 static void
1906 init_to_row_start (it, w, row)
1907 struct it *it;
1908 struct window *w;
1909 struct glyph_row *row;
1910 {
1911 init_from_display_pos (it, w, &row->start);
1912 it->continuation_lines_width = row->continuation_lines_width;
1913 CHECK_IT (it);
1914 }
1915
1916
1917 /* Initialize IT for stepping through current_buffer in window W
1918 starting in the line following ROW, i.e. starting at ROW->end. */
1919
1920 static void
1921 init_to_row_end (it, w, row)
1922 struct it *it;
1923 struct window *w;
1924 struct glyph_row *row;
1925 {
1926 init_from_display_pos (it, w, &row->end);
1927
1928 if (row->continued_p)
1929 it->continuation_lines_width = (row->continuation_lines_width
1930 + row->pixel_width);
1931 CHECK_IT (it);
1932 }
1933
1934
1935
1936 \f
1937 /***********************************************************************
1938 Text properties
1939 ***********************************************************************/
1940
1941 /* Called when IT reaches IT->stop_charpos. Handle text property and
1942 overlay changes. Set IT->stop_charpos to the next position where
1943 to stop. */
1944
1945 static void
1946 handle_stop (it)
1947 struct it *it;
1948 {
1949 enum prop_handled handled;
1950 int handle_overlay_change_p = 1;
1951 struct props *p;
1952
1953 it->dpvec = NULL;
1954 it->current.dpvec_index = -1;
1955
1956 do
1957 {
1958 handled = HANDLED_NORMALLY;
1959
1960 /* Call text property handlers. */
1961 for (p = it_props; p->handler; ++p)
1962 {
1963 handled = p->handler (it);
1964
1965 if (handled == HANDLED_RECOMPUTE_PROPS)
1966 break;
1967 else if (handled == HANDLED_RETURN)
1968 return;
1969 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1970 handle_overlay_change_p = 0;
1971 }
1972
1973 if (handled != HANDLED_RECOMPUTE_PROPS)
1974 {
1975 /* Don't check for overlay strings below when set to deliver
1976 characters from a display vector. */
1977 if (it->method == next_element_from_display_vector)
1978 handle_overlay_change_p = 0;
1979
1980 /* Handle overlay changes. */
1981 if (handle_overlay_change_p)
1982 handled = handle_overlay_change (it);
1983
1984 /* Determine where to stop next. */
1985 if (handled == HANDLED_NORMALLY)
1986 compute_stop_pos (it);
1987 }
1988 }
1989 while (handled == HANDLED_RECOMPUTE_PROPS);
1990 }
1991
1992
1993 /* Compute IT->stop_charpos from text property and overlay change
1994 information for IT's current position. */
1995
1996 static void
1997 compute_stop_pos (it)
1998 struct it *it;
1999 {
2000 register INTERVAL iv, next_iv;
2001 Lisp_Object object, limit, position;
2002
2003 /* If nowhere else, stop at the end. */
2004 it->stop_charpos = it->end_charpos;
2005
2006 if (STRINGP (it->string))
2007 {
2008 /* Strings are usually short, so don't limit the search for
2009 properties. */
2010 object = it->string;
2011 limit = Qnil;
2012 position = make_number (IT_STRING_CHARPOS (*it));
2013 }
2014 else
2015 {
2016 int charpos;
2017
2018 /* If next overlay change is in front of the current stop pos
2019 (which is IT->end_charpos), stop there. Note: value of
2020 next_overlay_change is point-max if no overlay change
2021 follows. */
2022 charpos = next_overlay_change (IT_CHARPOS (*it));
2023 if (charpos < it->stop_charpos)
2024 it->stop_charpos = charpos;
2025
2026 /* If showing the region, we have to stop at the region
2027 start or end because the face might change there. */
2028 if (it->region_beg_charpos > 0)
2029 {
2030 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2031 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2032 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2033 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2034 }
2035
2036 /* Set up variables for computing the stop position from text
2037 property changes. */
2038 XSETBUFFER (object, current_buffer);
2039 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2040 position = make_number (IT_CHARPOS (*it));
2041
2042 }
2043
2044 /* Get the interval containing IT's position. Value is a null
2045 interval if there isn't such an interval. */
2046 iv = validate_interval_range (object, &position, &position, 0);
2047 if (!NULL_INTERVAL_P (iv))
2048 {
2049 Lisp_Object values_here[LAST_PROP_IDX];
2050 struct props *p;
2051
2052 /* Get properties here. */
2053 for (p = it_props; p->handler; ++p)
2054 values_here[p->idx] = textget (iv->plist, *p->name);
2055
2056 /* Look for an interval following iv that has different
2057 properties. */
2058 for (next_iv = next_interval (iv);
2059 (!NULL_INTERVAL_P (next_iv)
2060 && (NILP (limit)
2061 || XFASTINT (limit) > next_iv->position));
2062 next_iv = next_interval (next_iv))
2063 {
2064 for (p = it_props; p->handler; ++p)
2065 {
2066 Lisp_Object new_value;
2067
2068 new_value = textget (next_iv->plist, *p->name);
2069 if (!EQ (values_here[p->idx], new_value))
2070 break;
2071 }
2072
2073 if (p->handler)
2074 break;
2075 }
2076
2077 if (!NULL_INTERVAL_P (next_iv))
2078 {
2079 if (INTEGERP (limit)
2080 && next_iv->position >= XFASTINT (limit))
2081 /* No text property change up to limit. */
2082 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2083 else
2084 /* Text properties change in next_iv. */
2085 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2086 }
2087 }
2088
2089 xassert (STRINGP (it->string)
2090 || (it->stop_charpos >= BEGV
2091 && it->stop_charpos >= IT_CHARPOS (*it)));
2092 }
2093
2094
2095 /* Return the position of the next overlay change after POS in
2096 current_buffer. Value is point-max if no overlay change
2097 follows. This is like `next-overlay-change' but doesn't use
2098 xmalloc. */
2099
2100 static int
2101 next_overlay_change (pos)
2102 int pos;
2103 {
2104 int noverlays;
2105 int endpos;
2106 Lisp_Object *overlays;
2107 int len;
2108 int i;
2109
2110 /* Get all overlays at the given position. */
2111 len = 10;
2112 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2113 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2114 if (noverlays > len)
2115 {
2116 len = noverlays;
2117 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2118 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2119 }
2120
2121 /* If any of these overlays ends before endpos,
2122 use its ending point instead. */
2123 for (i = 0; i < noverlays; ++i)
2124 {
2125 Lisp_Object oend;
2126 int oendpos;
2127
2128 oend = OVERLAY_END (overlays[i]);
2129 oendpos = OVERLAY_POSITION (oend);
2130 endpos = min (endpos, oendpos);
2131 }
2132
2133 return endpos;
2134 }
2135
2136
2137 \f
2138 /***********************************************************************
2139 Fontification
2140 ***********************************************************************/
2141
2142 /* Handle changes in the `fontified' property of the current buffer by
2143 calling hook functions from Qfontification_functions to fontify
2144 regions of text. */
2145
2146 static enum prop_handled
2147 handle_fontified_prop (it)
2148 struct it *it;
2149 {
2150 Lisp_Object prop, pos;
2151 enum prop_handled handled = HANDLED_NORMALLY;
2152
2153 /* Get the value of the `fontified' property at IT's current buffer
2154 position. (The `fontified' property doesn't have a special
2155 meaning in strings.) If the value is nil, call functions from
2156 Qfontification_functions. */
2157 if (!STRINGP (it->string)
2158 && it->s == NULL
2159 && !NILP (Vfontification_functions)
2160 && !NILP (Vrun_hooks)
2161 && (pos = make_number (IT_CHARPOS (*it)),
2162 prop = Fget_char_property (pos, Qfontified, Qnil),
2163 NILP (prop)))
2164 {
2165 int count = BINDING_STACK_SIZE ();
2166 Lisp_Object val;
2167
2168 val = Vfontification_functions;
2169 specbind (Qfontification_functions, Qnil);
2170 specbind (Qafter_change_functions, Qnil);
2171
2172 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2173 safe_call1 (val, pos);
2174 else
2175 {
2176 Lisp_Object globals, fn;
2177 struct gcpro gcpro1, gcpro2;
2178
2179 globals = Qnil;
2180 GCPRO2 (val, globals);
2181
2182 for (; CONSP (val); val = XCDR (val))
2183 {
2184 fn = XCAR (val);
2185
2186 if (EQ (fn, Qt))
2187 {
2188 /* A value of t indicates this hook has a local
2189 binding; it means to run the global binding too.
2190 In a global value, t should not occur. If it
2191 does, we must ignore it to avoid an endless
2192 loop. */
2193 for (globals = Fdefault_value (Qfontification_functions);
2194 CONSP (globals);
2195 globals = XCDR (globals))
2196 {
2197 fn = XCAR (globals);
2198 if (!EQ (fn, Qt))
2199 safe_call1 (fn, pos);
2200 }
2201 }
2202 else
2203 safe_call1 (fn, pos);
2204 }
2205
2206 UNGCPRO;
2207 }
2208
2209 unbind_to (count, Qnil);
2210
2211 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2212 something. This avoids an endless loop if they failed to
2213 fontify the text for which reason ever. */
2214 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2215 handled = HANDLED_RECOMPUTE_PROPS;
2216 }
2217
2218 return handled;
2219 }
2220
2221
2222 \f
2223 /***********************************************************************
2224 Faces
2225 ***********************************************************************/
2226
2227 /* Set up iterator IT from face properties at its current position.
2228 Called from handle_stop. */
2229
2230 static enum prop_handled
2231 handle_face_prop (it)
2232 struct it *it;
2233 {
2234 int new_face_id, next_stop;
2235
2236 if (!STRINGP (it->string))
2237 {
2238 new_face_id
2239 = face_at_buffer_position (it->w,
2240 IT_CHARPOS (*it),
2241 it->region_beg_charpos,
2242 it->region_end_charpos,
2243 &next_stop,
2244 (IT_CHARPOS (*it)
2245 + TEXT_PROP_DISTANCE_LIMIT),
2246 0);
2247
2248 /* Is this a start of a run of characters with box face?
2249 Caveat: this can be called for a freshly initialized
2250 iterator; face_id is -1 is this case. We know that the new
2251 face will not change until limit, i.e. if the new face has a
2252 box, all characters up to limit will have one. But, as
2253 usual, we don't know whether limit is really the end. */
2254 if (new_face_id != it->face_id)
2255 {
2256 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2257
2258 /* If new face has a box but old face has not, this is
2259 the start of a run of characters with box, i.e. it has
2260 a shadow on the left side. The value of face_id of the
2261 iterator will be -1 if this is the initial call that gets
2262 the face. In this case, we have to look in front of IT's
2263 position and see whether there is a face != new_face_id. */
2264 it->start_of_box_run_p
2265 = (new_face->box != FACE_NO_BOX
2266 && (it->face_id >= 0
2267 || IT_CHARPOS (*it) == BEG
2268 || new_face_id != face_before_it_pos (it)));
2269 it->face_box_p = new_face->box != FACE_NO_BOX;
2270 }
2271 }
2272 else
2273 {
2274 int base_face_id, bufpos;
2275
2276 if (it->current.overlay_string_index >= 0)
2277 bufpos = IT_CHARPOS (*it);
2278 else
2279 bufpos = 0;
2280
2281 /* For strings from a buffer, i.e. overlay strings or strings
2282 from a `display' property, use the face at IT's current
2283 buffer position as the base face to merge with, so that
2284 overlay strings appear in the same face as surrounding
2285 text, unless they specify their own faces. */
2286 base_face_id = underlying_face_id (it);
2287
2288 new_face_id = face_at_string_position (it->w,
2289 it->string,
2290 IT_STRING_CHARPOS (*it),
2291 bufpos,
2292 it->region_beg_charpos,
2293 it->region_end_charpos,
2294 &next_stop,
2295 base_face_id, 0);
2296
2297 #if 0 /* This shouldn't be neccessary. Let's check it. */
2298 /* If IT is used to display a mode line we would really like to
2299 use the mode line face instead of the frame's default face. */
2300 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2301 && new_face_id == DEFAULT_FACE_ID)
2302 new_face_id = MODE_LINE_FACE_ID;
2303 #endif
2304
2305 /* Is this a start of a run of characters with box? Caveat:
2306 this can be called for a freshly allocated iterator; face_id
2307 is -1 is this case. We know that the new face will not
2308 change until the next check pos, i.e. if the new face has a
2309 box, all characters up to that position will have a
2310 box. But, as usual, we don't know whether that position
2311 is really the end. */
2312 if (new_face_id != it->face_id)
2313 {
2314 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2315 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2316
2317 /* If new face has a box but old face hasn't, this is the
2318 start of a run of characters with box, i.e. it has a
2319 shadow on the left side. */
2320 it->start_of_box_run_p
2321 = new_face->box && (old_face == NULL || !old_face->box);
2322 it->face_box_p = new_face->box != FACE_NO_BOX;
2323 }
2324 }
2325
2326 it->face_id = new_face_id;
2327 return HANDLED_NORMALLY;
2328 }
2329
2330
2331 /* Return the ID of the face ``underlying'' IT's current position,
2332 which is in a string. If the iterator is associated with a
2333 buffer, return the face at IT's current buffer position.
2334 Otherwise, use the iterator's base_face_id. */
2335
2336 static int
2337 underlying_face_id (it)
2338 struct it *it;
2339 {
2340 int face_id = it->base_face_id, i;
2341
2342 xassert (STRINGP (it->string));
2343
2344 for (i = it->sp - 1; i >= 0; --i)
2345 if (NILP (it->stack[i].string))
2346 face_id = it->stack[i].face_id;
2347
2348 return face_id;
2349 }
2350
2351
2352 /* Compute the face one character before or after the current position
2353 of IT. BEFORE_P non-zero means get the face in front of IT's
2354 position. Value is the id of the face. */
2355
2356 static int
2357 face_before_or_after_it_pos (it, before_p)
2358 struct it *it;
2359 int before_p;
2360 {
2361 int face_id, limit;
2362 int next_check_charpos;
2363 struct text_pos pos;
2364
2365 xassert (it->s == NULL);
2366
2367 if (STRINGP (it->string))
2368 {
2369 int bufpos, base_face_id;
2370
2371 /* No face change past the end of the string (for the case
2372 we are padding with spaces). No face change before the
2373 string start. */
2374 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2375 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2376 return it->face_id;
2377
2378 /* Set pos to the position before or after IT's current position. */
2379 if (before_p)
2380 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2381 else
2382 /* For composition, we must check the character after the
2383 composition. */
2384 pos = (it->what == IT_COMPOSITION
2385 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2386 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2387
2388 if (it->current.overlay_string_index >= 0)
2389 bufpos = IT_CHARPOS (*it);
2390 else
2391 bufpos = 0;
2392
2393 base_face_id = underlying_face_id (it);
2394
2395 /* Get the face for ASCII, or unibyte. */
2396 face_id = face_at_string_position (it->w,
2397 it->string,
2398 CHARPOS (pos),
2399 bufpos,
2400 it->region_beg_charpos,
2401 it->region_end_charpos,
2402 &next_check_charpos,
2403 base_face_id, 0);
2404
2405 /* Correct the face for charsets different from ASCII. Do it
2406 for the multibyte case only. The face returned above is
2407 suitable for unibyte text if IT->string is unibyte. */
2408 if (STRING_MULTIBYTE (it->string))
2409 {
2410 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2411 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2412 int c, len;
2413 struct face *face = FACE_FROM_ID (it->f, face_id);
2414
2415 c = string_char_and_length (p, rest, &len);
2416 face_id = FACE_FOR_CHAR (it->f, face, c);
2417 }
2418 }
2419 else
2420 {
2421 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2422 || (IT_CHARPOS (*it) <= BEGV && before_p))
2423 return it->face_id;
2424
2425 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2426 pos = it->current.pos;
2427
2428 if (before_p)
2429 DEC_TEXT_POS (pos, it->multibyte_p);
2430 else
2431 {
2432 if (it->what == IT_COMPOSITION)
2433 /* For composition, we must check the position after the
2434 composition. */
2435 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2436 else
2437 INC_TEXT_POS (pos, it->multibyte_p);
2438 }
2439
2440 /* Determine face for CHARSET_ASCII, or unibyte. */
2441 face_id = face_at_buffer_position (it->w,
2442 CHARPOS (pos),
2443 it->region_beg_charpos,
2444 it->region_end_charpos,
2445 &next_check_charpos,
2446 limit, 0);
2447
2448 /* Correct the face for charsets different from ASCII. Do it
2449 for the multibyte case only. The face returned above is
2450 suitable for unibyte text if current_buffer is unibyte. */
2451 if (it->multibyte_p)
2452 {
2453 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2454 struct face *face = FACE_FROM_ID (it->f, face_id);
2455 face_id = FACE_FOR_CHAR (it->f, face, c);
2456 }
2457 }
2458
2459 return face_id;
2460 }
2461
2462
2463 \f
2464 /***********************************************************************
2465 Invisible text
2466 ***********************************************************************/
2467
2468 /* Set up iterator IT from invisible properties at its current
2469 position. Called from handle_stop. */
2470
2471 static enum prop_handled
2472 handle_invisible_prop (it)
2473 struct it *it;
2474 {
2475 enum prop_handled handled = HANDLED_NORMALLY;
2476
2477 if (STRINGP (it->string))
2478 {
2479 extern Lisp_Object Qinvisible;
2480 Lisp_Object prop, end_charpos, limit, charpos;
2481
2482 /* Get the value of the invisible text property at the
2483 current position. Value will be nil if there is no such
2484 property. */
2485 charpos = make_number (IT_STRING_CHARPOS (*it));
2486 prop = Fget_text_property (charpos, Qinvisible, it->string);
2487
2488 if (!NILP (prop)
2489 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2490 {
2491 handled = HANDLED_RECOMPUTE_PROPS;
2492
2493 /* Get the position at which the next change of the
2494 invisible text property can be found in IT->string.
2495 Value will be nil if the property value is the same for
2496 all the rest of IT->string. */
2497 XSETINT (limit, XSTRING (it->string)->size);
2498 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2499 it->string, limit);
2500
2501 /* Text at current position is invisible. The next
2502 change in the property is at position end_charpos.
2503 Move IT's current position to that position. */
2504 if (INTEGERP (end_charpos)
2505 && XFASTINT (end_charpos) < XFASTINT (limit))
2506 {
2507 struct text_pos old;
2508 old = it->current.string_pos;
2509 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2510 compute_string_pos (&it->current.string_pos, old, it->string);
2511 }
2512 else
2513 {
2514 /* The rest of the string is invisible. If this is an
2515 overlay string, proceed with the next overlay string
2516 or whatever comes and return a character from there. */
2517 if (it->current.overlay_string_index >= 0)
2518 {
2519 next_overlay_string (it);
2520 /* Don't check for overlay strings when we just
2521 finished processing them. */
2522 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2523 }
2524 else
2525 {
2526 struct Lisp_String *s = XSTRING (it->string);
2527 IT_STRING_CHARPOS (*it) = s->size;
2528 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2529 }
2530 }
2531 }
2532 }
2533 else
2534 {
2535 int visible_p, newpos, next_stop, start_charpos;
2536 Lisp_Object pos, prop, overlay;
2537
2538 /* First of all, is there invisible text at this position? */
2539 start_charpos = IT_CHARPOS (*it);
2540 pos = make_number (IT_CHARPOS (*it));
2541 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2542 &overlay);
2543
2544 /* If we are on invisible text, skip over it. */
2545 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2546 && IT_CHARPOS (*it) < it->end_charpos)
2547 {
2548 /* Record whether we have to display an ellipsis for the
2549 invisible text. */
2550 int display_ellipsis_p
2551 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2552
2553 handled = HANDLED_RECOMPUTE_PROPS;
2554
2555 /* Loop skipping over invisible text. The loop is left at
2556 ZV or with IT on the first char being visible again. */
2557 do
2558 {
2559 /* Try to skip some invisible text. Return value is the
2560 position reached which can be equal to IT's position
2561 if there is nothing invisible here. This skips both
2562 over invisible text properties and overlays with
2563 invisible property. */
2564 newpos = skip_invisible (IT_CHARPOS (*it),
2565 &next_stop, ZV, it->window);
2566
2567 /* If we skipped nothing at all we weren't at invisible
2568 text in the first place. If everything to the end of
2569 the buffer was skipped, end the loop. */
2570 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2571 visible_p = 1;
2572 else
2573 {
2574 /* We skipped some characters but not necessarily
2575 all there are. Check if we ended up on visible
2576 text. Fget_char_property returns the property of
2577 the char before the given position, i.e. if we
2578 get visible_p = 1, this means that the char at
2579 newpos is visible. */
2580 pos = make_number (newpos);
2581 prop = Fget_char_property (pos, Qinvisible, it->window);
2582 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2583 }
2584
2585 /* If we ended up on invisible text, proceed to
2586 skip starting with next_stop. */
2587 if (!visible_p)
2588 IT_CHARPOS (*it) = next_stop;
2589 }
2590 while (!visible_p);
2591
2592 /* The position newpos is now either ZV or on visible text. */
2593 IT_CHARPOS (*it) = newpos;
2594 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2595
2596 /* If there are before-strings at the start of invisible
2597 text, and the text is invisible because of a text
2598 property, arrange to show before-strings because 20.x did
2599 it that way. (If the text is invisible because of an
2600 overlay property instead of a text property, this is
2601 already handled in the overlay code.) */
2602 if (NILP (overlay)
2603 && get_overlay_strings (it, start_charpos))
2604 {
2605 handled = HANDLED_RECOMPUTE_PROPS;
2606 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2607 }
2608 else if (display_ellipsis_p)
2609 setup_for_ellipsis (it);
2610 }
2611 }
2612
2613 return handled;
2614 }
2615
2616
2617 /* Make iterator IT return `...' next. */
2618
2619 static void
2620 setup_for_ellipsis (it)
2621 struct it *it;
2622 {
2623 if (it->dp
2624 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2625 {
2626 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2627 it->dpvec = v->contents;
2628 it->dpend = v->contents + v->size;
2629 }
2630 else
2631 {
2632 /* Default `...'. */
2633 it->dpvec = default_invis_vector;
2634 it->dpend = default_invis_vector + 3;
2635 }
2636
2637 /* The ellipsis display does not replace the display of the
2638 character at the new position. Indicate this by setting
2639 IT->dpvec_char_len to zero. */
2640 it->dpvec_char_len = 0;
2641
2642 it->current.dpvec_index = 0;
2643 it->method = next_element_from_display_vector;
2644 }
2645
2646
2647 \f
2648 /***********************************************************************
2649 'display' property
2650 ***********************************************************************/
2651
2652 /* Set up iterator IT from `display' property at its current position.
2653 Called from handle_stop. */
2654
2655 static enum prop_handled
2656 handle_display_prop (it)
2657 struct it *it;
2658 {
2659 Lisp_Object prop, object;
2660 struct text_pos *position;
2661 int display_replaced_p = 0;
2662
2663 if (STRINGP (it->string))
2664 {
2665 object = it->string;
2666 position = &it->current.string_pos;
2667 }
2668 else
2669 {
2670 object = it->w->buffer;
2671 position = &it->current.pos;
2672 }
2673
2674 /* Reset those iterator values set from display property values. */
2675 it->font_height = Qnil;
2676 it->space_width = Qnil;
2677 it->voffset = 0;
2678
2679 /* We don't support recursive `display' properties, i.e. string
2680 values that have a string `display' property, that have a string
2681 `display' property etc. */
2682 if (!it->string_from_display_prop_p)
2683 it->area = TEXT_AREA;
2684
2685 prop = Fget_char_property (make_number (position->charpos),
2686 Qdisplay, object);
2687 if (NILP (prop))
2688 return HANDLED_NORMALLY;
2689
2690 if (CONSP (prop)
2691 /* Simple properties. */
2692 && !EQ (XCAR (prop), Qimage)
2693 && !EQ (XCAR (prop), Qspace)
2694 && !EQ (XCAR (prop), Qwhen)
2695 && !EQ (XCAR (prop), Qspace_width)
2696 && !EQ (XCAR (prop), Qheight)
2697 && !EQ (XCAR (prop), Qraise)
2698 /* Marginal area specifications. */
2699 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2700 && !NILP (XCAR (prop)))
2701 {
2702 for (; CONSP (prop); prop = XCDR (prop))
2703 {
2704 if (handle_single_display_prop (it, XCAR (prop), object,
2705 position, display_replaced_p))
2706 display_replaced_p = 1;
2707 }
2708 }
2709 else if (VECTORP (prop))
2710 {
2711 int i;
2712 for (i = 0; i < ASIZE (prop); ++i)
2713 if (handle_single_display_prop (it, AREF (prop, i), object,
2714 position, display_replaced_p))
2715 display_replaced_p = 1;
2716 }
2717 else
2718 {
2719 if (handle_single_display_prop (it, prop, object, position, 0))
2720 display_replaced_p = 1;
2721 }
2722
2723 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2724 }
2725
2726
2727 /* Value is the position of the end of the `display' property starting
2728 at START_POS in OBJECT. */
2729
2730 static struct text_pos
2731 display_prop_end (it, object, start_pos)
2732 struct it *it;
2733 Lisp_Object object;
2734 struct text_pos start_pos;
2735 {
2736 Lisp_Object end;
2737 struct text_pos end_pos;
2738
2739 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2740 Qdisplay, object, Qnil);
2741 CHARPOS (end_pos) = XFASTINT (end);
2742 if (STRINGP (object))
2743 compute_string_pos (&end_pos, start_pos, it->string);
2744 else
2745 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2746
2747 return end_pos;
2748 }
2749
2750
2751 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2752 is the object in which the `display' property was found. *POSITION
2753 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2754 means that we previously saw a display sub-property which already
2755 replaced text display with something else, for example an image;
2756 ignore such properties after the first one has been processed.
2757
2758 If PROP is a `space' or `image' sub-property, set *POSITION to the
2759 end position of the `display' property.
2760
2761 Value is non-zero something was found which replaces the display
2762 of buffer or string text. */
2763
2764 static int
2765 handle_single_display_prop (it, prop, object, position,
2766 display_replaced_before_p)
2767 struct it *it;
2768 Lisp_Object prop;
2769 Lisp_Object object;
2770 struct text_pos *position;
2771 int display_replaced_before_p;
2772 {
2773 Lisp_Object value;
2774 int replaces_text_display_p = 0;
2775 Lisp_Object form;
2776
2777 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2778 evaluated. If the result is nil, VALUE is ignored. */
2779 form = Qt;
2780 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2781 {
2782 prop = XCDR (prop);
2783 if (!CONSP (prop))
2784 return 0;
2785 form = XCAR (prop);
2786 prop = XCDR (prop);
2787 }
2788
2789 if (!NILP (form) && !EQ (form, Qt))
2790 {
2791 struct gcpro gcpro1;
2792 struct text_pos end_pos, pt;
2793
2794 GCPRO1 (form);
2795 end_pos = display_prop_end (it, object, *position);
2796
2797 /* Temporarily set point to the end position, and then evaluate
2798 the form. This makes `(eolp)' work as FORM. */
2799 if (BUFFERP (object))
2800 {
2801 CHARPOS (pt) = PT;
2802 BYTEPOS (pt) = PT_BYTE;
2803 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2804 }
2805
2806 form = safe_eval (form);
2807
2808 if (BUFFERP (object))
2809 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2810 UNGCPRO;
2811 }
2812
2813 if (NILP (form))
2814 return 0;
2815
2816 if (CONSP (prop)
2817 && EQ (XCAR (prop), Qheight)
2818 && CONSP (XCDR (prop)))
2819 {
2820 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2821 return 0;
2822
2823 /* `(height HEIGHT)'. */
2824 it->font_height = XCAR (XCDR (prop));
2825 if (!NILP (it->font_height))
2826 {
2827 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2828 int new_height = -1;
2829
2830 if (CONSP (it->font_height)
2831 && (EQ (XCAR (it->font_height), Qplus)
2832 || EQ (XCAR (it->font_height), Qminus))
2833 && CONSP (XCDR (it->font_height))
2834 && INTEGERP (XCAR (XCDR (it->font_height))))
2835 {
2836 /* `(+ N)' or `(- N)' where N is an integer. */
2837 int steps = XINT (XCAR (XCDR (it->font_height)));
2838 if (EQ (XCAR (it->font_height), Qplus))
2839 steps = - steps;
2840 it->face_id = smaller_face (it->f, it->face_id, steps);
2841 }
2842 else if (FUNCTIONP (it->font_height))
2843 {
2844 /* Call function with current height as argument.
2845 Value is the new height. */
2846 Lisp_Object height;
2847 height = safe_call1 (it->font_height,
2848 face->lface[LFACE_HEIGHT_INDEX]);
2849 if (NUMBERP (height))
2850 new_height = XFLOATINT (height);
2851 }
2852 else if (NUMBERP (it->font_height))
2853 {
2854 /* Value is a multiple of the canonical char height. */
2855 struct face *face;
2856
2857 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2858 new_height = (XFLOATINT (it->font_height)
2859 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2860 }
2861 else
2862 {
2863 /* Evaluate IT->font_height with `height' bound to the
2864 current specified height to get the new height. */
2865 Lisp_Object value;
2866 int count = BINDING_STACK_SIZE ();
2867
2868 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2869 value = safe_eval (it->font_height);
2870 unbind_to (count, Qnil);
2871
2872 if (NUMBERP (value))
2873 new_height = XFLOATINT (value);
2874 }
2875
2876 if (new_height > 0)
2877 it->face_id = face_with_height (it->f, it->face_id, new_height);
2878 }
2879 }
2880 else if (CONSP (prop)
2881 && EQ (XCAR (prop), Qspace_width)
2882 && CONSP (XCDR (prop)))
2883 {
2884 /* `(space_width WIDTH)'. */
2885 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2886 return 0;
2887
2888 value = XCAR (XCDR (prop));
2889 if (NUMBERP (value) && XFLOATINT (value) > 0)
2890 it->space_width = value;
2891 }
2892 else if (CONSP (prop)
2893 && EQ (XCAR (prop), Qraise)
2894 && CONSP (XCDR (prop)))
2895 {
2896 /* `(raise FACTOR)'. */
2897 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2898 return 0;
2899
2900 #ifdef HAVE_WINDOW_SYSTEM
2901 value = XCAR (XCDR (prop));
2902 if (NUMBERP (value))
2903 {
2904 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2905 it->voffset = - (XFLOATINT (value)
2906 * (FONT_HEIGHT (face->font)));
2907 }
2908 #endif /* HAVE_WINDOW_SYSTEM */
2909 }
2910 else if (!it->string_from_display_prop_p)
2911 {
2912 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2913 VALUE) or `((margin nil) VALUE)' or VALUE. */
2914 Lisp_Object location, value;
2915 struct text_pos start_pos;
2916 int valid_p;
2917
2918 /* Characters having this form of property are not displayed, so
2919 we have to find the end of the property. */
2920 start_pos = *position;
2921 *position = display_prop_end (it, object, start_pos);
2922 value = Qnil;
2923
2924 /* Let's stop at the new position and assume that all
2925 text properties change there. */
2926 it->stop_charpos = position->charpos;
2927
2928 location = Qunbound;
2929 if (CONSP (prop) && CONSP (XCAR (prop)))
2930 {
2931 Lisp_Object tem;
2932
2933 value = XCDR (prop);
2934 if (CONSP (value))
2935 value = XCAR (value);
2936
2937 tem = XCAR (prop);
2938 if (EQ (XCAR (tem), Qmargin)
2939 && (tem = XCDR (tem),
2940 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2941 (NILP (tem)
2942 || EQ (tem, Qleft_margin)
2943 || EQ (tem, Qright_margin))))
2944 location = tem;
2945 }
2946
2947 if (EQ (location, Qunbound))
2948 {
2949 location = Qnil;
2950 value = prop;
2951 }
2952
2953 #ifdef HAVE_WINDOW_SYSTEM
2954 if (FRAME_TERMCAP_P (it->f))
2955 valid_p = STRINGP (value);
2956 else
2957 valid_p = (STRINGP (value)
2958 || (CONSP (value) && EQ (XCAR (value), Qspace))
2959 || valid_image_p (value));
2960 #else /* not HAVE_WINDOW_SYSTEM */
2961 valid_p = STRINGP (value);
2962 #endif /* not HAVE_WINDOW_SYSTEM */
2963
2964 if ((EQ (location, Qleft_margin)
2965 || EQ (location, Qright_margin)
2966 || NILP (location))
2967 && valid_p
2968 && !display_replaced_before_p)
2969 {
2970 replaces_text_display_p = 1;
2971
2972 /* Save current settings of IT so that we can restore them
2973 when we are finished with the glyph property value. */
2974 push_it (it);
2975
2976 if (NILP (location))
2977 it->area = TEXT_AREA;
2978 else if (EQ (location, Qleft_margin))
2979 it->area = LEFT_MARGIN_AREA;
2980 else
2981 it->area = RIGHT_MARGIN_AREA;
2982
2983 if (STRINGP (value))
2984 {
2985 it->string = value;
2986 it->multibyte_p = STRING_MULTIBYTE (it->string);
2987 it->current.overlay_string_index = -1;
2988 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2989 it->end_charpos = it->string_nchars
2990 = XSTRING (it->string)->size;
2991 it->method = next_element_from_string;
2992 it->stop_charpos = 0;
2993 it->string_from_display_prop_p = 1;
2994 /* Say that we haven't consumed the characters with
2995 `display' property yet. The call to pop_it in
2996 set_iterator_to_next will clean this up. */
2997 *position = start_pos;
2998 }
2999 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3000 {
3001 it->method = next_element_from_stretch;
3002 it->object = value;
3003 it->current.pos = it->position = start_pos;
3004 }
3005 #ifdef HAVE_WINDOW_SYSTEM
3006 else
3007 {
3008 it->what = IT_IMAGE;
3009 it->image_id = lookup_image (it->f, value);
3010 it->position = start_pos;
3011 it->object = NILP (object) ? it->w->buffer : object;
3012 it->method = next_element_from_image;
3013
3014 /* Say that we haven't consumed the characters with
3015 `display' property yet. The call to pop_it in
3016 set_iterator_to_next will clean this up. */
3017 *position = start_pos;
3018 }
3019 #endif /* HAVE_WINDOW_SYSTEM */
3020 }
3021 else
3022 /* Invalid property or property not supported. Restore
3023 the position to what it was before. */
3024 *position = start_pos;
3025 }
3026
3027 return replaces_text_display_p;
3028 }
3029
3030
3031 /* Check if PROP is a display sub-property value whose text should be
3032 treated as intangible. */
3033
3034 static int
3035 single_display_prop_intangible_p (prop)
3036 Lisp_Object prop;
3037 {
3038 /* Skip over `when FORM'. */
3039 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3040 {
3041 prop = XCDR (prop);
3042 if (!CONSP (prop))
3043 return 0;
3044 prop = XCDR (prop);
3045 }
3046
3047 if (!CONSP (prop))
3048 return 0;
3049
3050 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3051 we don't need to treat text as intangible. */
3052 if (EQ (XCAR (prop), Qmargin))
3053 {
3054 prop = XCDR (prop);
3055 if (!CONSP (prop))
3056 return 0;
3057
3058 prop = XCDR (prop);
3059 if (!CONSP (prop)
3060 || EQ (XCAR (prop), Qleft_margin)
3061 || EQ (XCAR (prop), Qright_margin))
3062 return 0;
3063 }
3064
3065 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3066 }
3067
3068
3069 /* Check if PROP is a display property value whose text should be
3070 treated as intangible. */
3071
3072 int
3073 display_prop_intangible_p (prop)
3074 Lisp_Object prop;
3075 {
3076 if (CONSP (prop)
3077 && CONSP (XCAR (prop))
3078 && !EQ (Qmargin, XCAR (XCAR (prop))))
3079 {
3080 /* A list of sub-properties. */
3081 while (CONSP (prop))
3082 {
3083 if (single_display_prop_intangible_p (XCAR (prop)))
3084 return 1;
3085 prop = XCDR (prop);
3086 }
3087 }
3088 else if (VECTORP (prop))
3089 {
3090 /* A vector of sub-properties. */
3091 int i;
3092 for (i = 0; i < ASIZE (prop); ++i)
3093 if (single_display_prop_intangible_p (AREF (prop, i)))
3094 return 1;
3095 }
3096 else
3097 return single_display_prop_intangible_p (prop);
3098
3099 return 0;
3100 }
3101
3102
3103 /* Return 1 if PROP is a display sub-property value containing STRING. */
3104
3105 static int
3106 single_display_prop_string_p (prop, string)
3107 Lisp_Object prop, string;
3108 {
3109 extern Lisp_Object Qwhen, Qmargin;
3110
3111 if (EQ (string, prop))
3112 return 1;
3113
3114 /* Skip over `when FORM'. */
3115 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3116 {
3117 prop = XCDR (prop);
3118 if (!CONSP (prop))
3119 return 0;
3120 prop = XCDR (prop);
3121 }
3122
3123 if (CONSP (prop))
3124 /* Skip over `margin LOCATION'. */
3125 if (EQ (XCAR (prop), Qmargin))
3126 {
3127 prop = XCDR (prop);
3128 if (!CONSP (prop))
3129 return 0;
3130
3131 prop = XCDR (prop);
3132 if (!CONSP (prop))
3133 return 0;
3134 }
3135
3136 return CONSP (prop) && EQ (XCAR (prop), string);
3137 }
3138
3139
3140 /* Return 1 if STRING appears in the `display' property PROP. */
3141
3142 static int
3143 display_prop_string_p (prop, string)
3144 Lisp_Object prop, string;
3145 {
3146 extern Lisp_Object Qwhen, Qmargin;
3147
3148 if (CONSP (prop)
3149 && CONSP (XCAR (prop))
3150 && !EQ (Qmargin, XCAR (XCAR (prop))))
3151 {
3152 /* A list of sub-properties. */
3153 while (CONSP (prop))
3154 {
3155 if (single_display_prop_string_p (XCAR (prop), string))
3156 return 1;
3157 prop = XCDR (prop);
3158 }
3159 }
3160 else if (VECTORP (prop))
3161 {
3162 /* A vector of sub-properties. */
3163 int i;
3164 for (i = 0; i < ASIZE (prop); ++i)
3165 if (single_display_prop_string_p (AREF (prop, i), string))
3166 return 1;
3167 }
3168 else
3169 return single_display_prop_string_p (prop, string);
3170
3171 return 0;
3172 }
3173
3174
3175 /* Determine from which buffer position in W's buffer STRING comes
3176 from. AROUND_CHARPOS is an approximate position where it could
3177 be from. Value is the buffer position or 0 if it couldn't be
3178 determined.
3179
3180 W's buffer must be current.
3181
3182 This function is necessary because we don't record buffer positions
3183 in glyphs generated from strings (to keep struct glyph small).
3184 This function may only use code that doesn't eval because it is
3185 called asynchronously from note_mouse_highlight. */
3186
3187 int
3188 string_buffer_position (w, string, around_charpos)
3189 struct window *w;
3190 Lisp_Object string;
3191 int around_charpos;
3192 {
3193 Lisp_Object around = make_number (around_charpos);
3194 Lisp_Object limit, prop, pos;
3195 const int MAX_DISTANCE = 1000;
3196 int found = 0;
3197
3198 pos = make_number (around_charpos);
3199 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3200 while (!found && !EQ (pos, limit))
3201 {
3202 prop = Fget_char_property (pos, Qdisplay, Qnil);
3203 if (!NILP (prop) && display_prop_string_p (prop, string))
3204 found = 1;
3205 else
3206 pos = Fnext_single_property_change (pos, Qdisplay, Qnil, limit);
3207 }
3208
3209 if (!found)
3210 {
3211 pos = make_number (around_charpos);
3212 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3213 while (!found && !EQ (pos, limit))
3214 {
3215 prop = Fget_char_property (pos, Qdisplay, Qnil);
3216 if (!NILP (prop) && display_prop_string_p (prop, string))
3217 found = 1;
3218 else
3219 pos = Fprevious_single_property_change (pos, Qdisplay, Qnil,
3220 limit);
3221 }
3222 }
3223
3224 return found ? XINT (pos) : 0;
3225 }
3226
3227
3228 \f
3229 /***********************************************************************
3230 `composition' property
3231 ***********************************************************************/
3232
3233 /* Set up iterator IT from `composition' property at its current
3234 position. Called from handle_stop. */
3235
3236 static enum prop_handled
3237 handle_composition_prop (it)
3238 struct it *it;
3239 {
3240 Lisp_Object prop, string;
3241 int pos, pos_byte, end;
3242 enum prop_handled handled = HANDLED_NORMALLY;
3243
3244 if (STRINGP (it->string))
3245 {
3246 pos = IT_STRING_CHARPOS (*it);
3247 pos_byte = IT_STRING_BYTEPOS (*it);
3248 string = it->string;
3249 }
3250 else
3251 {
3252 pos = IT_CHARPOS (*it);
3253 pos_byte = IT_BYTEPOS (*it);
3254 string = Qnil;
3255 }
3256
3257 /* If there's a valid composition and point is not inside of the
3258 composition (in the case that the composition is from the current
3259 buffer), draw a glyph composed from the composition components. */
3260 if (find_composition (pos, -1, &pos, &end, &prop, string)
3261 && COMPOSITION_VALID_P (pos, end, prop)
3262 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3263 {
3264 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3265
3266 if (id >= 0)
3267 {
3268 it->method = next_element_from_composition;
3269 it->cmp_id = id;
3270 it->cmp_len = COMPOSITION_LENGTH (prop);
3271 /* For a terminal, draw only the first character of the
3272 components. */
3273 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3274 it->len = (STRINGP (it->string)
3275 ? string_char_to_byte (it->string, end)
3276 : CHAR_TO_BYTE (end)) - pos_byte;
3277 it->stop_charpos = end;
3278 handled = HANDLED_RETURN;
3279 }
3280 }
3281
3282 return handled;
3283 }
3284
3285
3286 \f
3287 /***********************************************************************
3288 Overlay strings
3289 ***********************************************************************/
3290
3291 /* The following structure is used to record overlay strings for
3292 later sorting in load_overlay_strings. */
3293
3294 struct overlay_entry
3295 {
3296 Lisp_Object overlay;
3297 Lisp_Object string;
3298 int priority;
3299 int after_string_p;
3300 };
3301
3302
3303 /* Set up iterator IT from overlay strings at its current position.
3304 Called from handle_stop. */
3305
3306 static enum prop_handled
3307 handle_overlay_change (it)
3308 struct it *it;
3309 {
3310 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3311 return HANDLED_RECOMPUTE_PROPS;
3312 else
3313 return HANDLED_NORMALLY;
3314 }
3315
3316
3317 /* Set up the next overlay string for delivery by IT, if there is an
3318 overlay string to deliver. Called by set_iterator_to_next when the
3319 end of the current overlay string is reached. If there are more
3320 overlay strings to display, IT->string and
3321 IT->current.overlay_string_index are set appropriately here.
3322 Otherwise IT->string is set to nil. */
3323
3324 static void
3325 next_overlay_string (it)
3326 struct it *it;
3327 {
3328 ++it->current.overlay_string_index;
3329 if (it->current.overlay_string_index == it->n_overlay_strings)
3330 {
3331 /* No more overlay strings. Restore IT's settings to what
3332 they were before overlay strings were processed, and
3333 continue to deliver from current_buffer. */
3334 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3335
3336 pop_it (it);
3337 xassert (it->stop_charpos >= BEGV
3338 && it->stop_charpos <= it->end_charpos);
3339 it->string = Qnil;
3340 it->current.overlay_string_index = -1;
3341 SET_TEXT_POS (it->current.string_pos, -1, -1);
3342 it->n_overlay_strings = 0;
3343 it->method = next_element_from_buffer;
3344
3345 /* If we're at the end of the buffer, record that we have
3346 processed the overlay strings there already, so that
3347 next_element_from_buffer doesn't try it again. */
3348 if (IT_CHARPOS (*it) >= it->end_charpos)
3349 it->overlay_strings_at_end_processed_p = 1;
3350
3351 /* If we have to display `...' for invisible text, set
3352 the iterator up for that. */
3353 if (display_ellipsis_p)
3354 setup_for_ellipsis (it);
3355 }
3356 else
3357 {
3358 /* There are more overlay strings to process. If
3359 IT->current.overlay_string_index has advanced to a position
3360 where we must load IT->overlay_strings with more strings, do
3361 it. */
3362 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3363
3364 if (it->current.overlay_string_index && i == 0)
3365 load_overlay_strings (it, 0);
3366
3367 /* Initialize IT to deliver display elements from the overlay
3368 string. */
3369 it->string = it->overlay_strings[i];
3370 it->multibyte_p = STRING_MULTIBYTE (it->string);
3371 SET_TEXT_POS (it->current.string_pos, 0, 0);
3372 it->method = next_element_from_string;
3373 it->stop_charpos = 0;
3374 }
3375
3376 CHECK_IT (it);
3377 }
3378
3379
3380 /* Compare two overlay_entry structures E1 and E2. Used as a
3381 comparison function for qsort in load_overlay_strings. Overlay
3382 strings for the same position are sorted so that
3383
3384 1. All after-strings come in front of before-strings, except
3385 when they come from the same overlay.
3386
3387 2. Within after-strings, strings are sorted so that overlay strings
3388 from overlays with higher priorities come first.
3389
3390 2. Within before-strings, strings are sorted so that overlay
3391 strings from overlays with higher priorities come last.
3392
3393 Value is analogous to strcmp. */
3394
3395
3396 static int
3397 compare_overlay_entries (e1, e2)
3398 void *e1, *e2;
3399 {
3400 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3401 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3402 int result;
3403
3404 if (entry1->after_string_p != entry2->after_string_p)
3405 {
3406 /* Let after-strings appear in front of before-strings if
3407 they come from different overlays. */
3408 if (EQ (entry1->overlay, entry2->overlay))
3409 result = entry1->after_string_p ? 1 : -1;
3410 else
3411 result = entry1->after_string_p ? -1 : 1;
3412 }
3413 else if (entry1->after_string_p)
3414 /* After-strings sorted in order of decreasing priority. */
3415 result = entry2->priority - entry1->priority;
3416 else
3417 /* Before-strings sorted in order of increasing priority. */
3418 result = entry1->priority - entry2->priority;
3419
3420 return result;
3421 }
3422
3423
3424 /* Load the vector IT->overlay_strings with overlay strings from IT's
3425 current buffer position, or from CHARPOS if that is > 0. Set
3426 IT->n_overlays to the total number of overlay strings found.
3427
3428 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3429 a time. On entry into load_overlay_strings,
3430 IT->current.overlay_string_index gives the number of overlay
3431 strings that have already been loaded by previous calls to this
3432 function.
3433
3434 IT->add_overlay_start contains an additional overlay start
3435 position to consider for taking overlay strings from, if non-zero.
3436 This position comes into play when the overlay has an `invisible'
3437 property, and both before and after-strings. When we've skipped to
3438 the end of the overlay, because of its `invisible' property, we
3439 nevertheless want its before-string to appear.
3440 IT->add_overlay_start will contain the overlay start position
3441 in this case.
3442
3443 Overlay strings are sorted so that after-string strings come in
3444 front of before-string strings. Within before and after-strings,
3445 strings are sorted by overlay priority. See also function
3446 compare_overlay_entries. */
3447
3448 static void
3449 load_overlay_strings (it, charpos)
3450 struct it *it;
3451 int charpos;
3452 {
3453 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3454 Lisp_Object ov, overlay, window, str, invisible;
3455 int start, end;
3456 int size = 20;
3457 int n = 0, i, j, invis_p;
3458 struct overlay_entry *entries
3459 = (struct overlay_entry *) alloca (size * sizeof *entries);
3460
3461 if (charpos <= 0)
3462 charpos = IT_CHARPOS (*it);
3463
3464 /* Append the overlay string STRING of overlay OVERLAY to vector
3465 `entries' which has size `size' and currently contains `n'
3466 elements. AFTER_P non-zero means STRING is an after-string of
3467 OVERLAY. */
3468 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3469 do \
3470 { \
3471 Lisp_Object priority; \
3472 \
3473 if (n == size) \
3474 { \
3475 int new_size = 2 * size; \
3476 struct overlay_entry *old = entries; \
3477 entries = \
3478 (struct overlay_entry *) alloca (new_size \
3479 * sizeof *entries); \
3480 bcopy (old, entries, size * sizeof *entries); \
3481 size = new_size; \
3482 } \
3483 \
3484 entries[n].string = (STRING); \
3485 entries[n].overlay = (OVERLAY); \
3486 priority = Foverlay_get ((OVERLAY), Qpriority); \
3487 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3488 entries[n].after_string_p = (AFTER_P); \
3489 ++n; \
3490 } \
3491 while (0)
3492
3493 /* Process overlay before the overlay center. */
3494 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3495 {
3496 overlay = XCAR (ov);
3497 xassert (OVERLAYP (overlay));
3498 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3499 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3500
3501 if (end < charpos)
3502 break;
3503
3504 /* Skip this overlay if it doesn't start or end at IT's current
3505 position. */
3506 if (end != charpos && start != charpos)
3507 continue;
3508
3509 /* Skip this overlay if it doesn't apply to IT->w. */
3510 window = Foverlay_get (overlay, Qwindow);
3511 if (WINDOWP (window) && XWINDOW (window) != it->w)
3512 continue;
3513
3514 /* If the text ``under'' the overlay is invisible, both before-
3515 and after-strings from this overlay are visible; start and
3516 end position are indistinguishable. */
3517 invisible = Foverlay_get (overlay, Qinvisible);
3518 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3519
3520 /* If overlay has a non-empty before-string, record it. */
3521 if ((start == charpos || (end == charpos && invis_p))
3522 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3523 && XSTRING (str)->size)
3524 RECORD_OVERLAY_STRING (overlay, str, 0);
3525
3526 /* If overlay has a non-empty after-string, record it. */
3527 if ((end == charpos || (start == charpos && invis_p))
3528 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3529 && XSTRING (str)->size)
3530 RECORD_OVERLAY_STRING (overlay, str, 1);
3531 }
3532
3533 /* Process overlays after the overlay center. */
3534 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3535 {
3536 overlay = XCAR (ov);
3537 xassert (OVERLAYP (overlay));
3538 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3539 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3540
3541 if (start > charpos)
3542 break;
3543
3544 /* Skip this overlay if it doesn't start or end at IT's current
3545 position. */
3546 if (end != charpos && start != charpos)
3547 continue;
3548
3549 /* Skip this overlay if it doesn't apply to IT->w. */
3550 window = Foverlay_get (overlay, Qwindow);
3551 if (WINDOWP (window) && XWINDOW (window) != it->w)
3552 continue;
3553
3554 /* If the text ``under'' the overlay is invisible, it has a zero
3555 dimension, and both before- and after-strings apply. */
3556 invisible = Foverlay_get (overlay, Qinvisible);
3557 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3558
3559 /* If overlay has a non-empty before-string, record it. */
3560 if ((start == charpos || (end == charpos && invis_p))
3561 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3562 && XSTRING (str)->size)
3563 RECORD_OVERLAY_STRING (overlay, str, 0);
3564
3565 /* If overlay has a non-empty after-string, record it. */
3566 if ((end == charpos || (start == charpos && invis_p))
3567 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3568 && XSTRING (str)->size)
3569 RECORD_OVERLAY_STRING (overlay, str, 1);
3570 }
3571
3572 #undef RECORD_OVERLAY_STRING
3573
3574 /* Sort entries. */
3575 if (n > 1)
3576 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3577
3578 /* Record the total number of strings to process. */
3579 it->n_overlay_strings = n;
3580
3581 /* IT->current.overlay_string_index is the number of overlay strings
3582 that have already been consumed by IT. Copy some of the
3583 remaining overlay strings to IT->overlay_strings. */
3584 i = 0;
3585 j = it->current.overlay_string_index;
3586 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3587 it->overlay_strings[i++] = entries[j++].string;
3588
3589 CHECK_IT (it);
3590 }
3591
3592
3593 /* Get the first chunk of overlay strings at IT's current buffer
3594 position, or at CHARPOS if that is > 0. Value is non-zero if at
3595 least one overlay string was found. */
3596
3597 static int
3598 get_overlay_strings (it, charpos)
3599 struct it *it;
3600 int charpos;
3601 {
3602 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3603 process. This fills IT->overlay_strings with strings, and sets
3604 IT->n_overlay_strings to the total number of strings to process.
3605 IT->pos.overlay_string_index has to be set temporarily to zero
3606 because load_overlay_strings needs this; it must be set to -1
3607 when no overlay strings are found because a zero value would
3608 indicate a position in the first overlay string. */
3609 it->current.overlay_string_index = 0;
3610 load_overlay_strings (it, charpos);
3611
3612 /* If we found overlay strings, set up IT to deliver display
3613 elements from the first one. Otherwise set up IT to deliver
3614 from current_buffer. */
3615 if (it->n_overlay_strings)
3616 {
3617 /* Make sure we know settings in current_buffer, so that we can
3618 restore meaningful values when we're done with the overlay
3619 strings. */
3620 compute_stop_pos (it);
3621 xassert (it->face_id >= 0);
3622
3623 /* Save IT's settings. They are restored after all overlay
3624 strings have been processed. */
3625 xassert (it->sp == 0);
3626 push_it (it);
3627
3628 /* Set up IT to deliver display elements from the first overlay
3629 string. */
3630 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3631 it->string = it->overlay_strings[0];
3632 it->stop_charpos = 0;
3633 it->end_charpos = XSTRING (it->string)->size;
3634 it->multibyte_p = STRING_MULTIBYTE (it->string);
3635 xassert (STRINGP (it->string));
3636 it->method = next_element_from_string;
3637 }
3638 else
3639 {
3640 it->string = Qnil;
3641 it->current.overlay_string_index = -1;
3642 it->method = next_element_from_buffer;
3643 }
3644
3645 CHECK_IT (it);
3646
3647 /* Value is non-zero if we found at least one overlay string. */
3648 return STRINGP (it->string);
3649 }
3650
3651
3652 \f
3653 /***********************************************************************
3654 Saving and restoring state
3655 ***********************************************************************/
3656
3657 /* Save current settings of IT on IT->stack. Called, for example,
3658 before setting up IT for an overlay string, to be able to restore
3659 IT's settings to what they were after the overlay string has been
3660 processed. */
3661
3662 static void
3663 push_it (it)
3664 struct it *it;
3665 {
3666 struct iterator_stack_entry *p;
3667
3668 xassert (it->sp < 2);
3669 p = it->stack + it->sp;
3670
3671 p->stop_charpos = it->stop_charpos;
3672 xassert (it->face_id >= 0);
3673 p->face_id = it->face_id;
3674 p->string = it->string;
3675 p->pos = it->current;
3676 p->end_charpos = it->end_charpos;
3677 p->string_nchars = it->string_nchars;
3678 p->area = it->area;
3679 p->multibyte_p = it->multibyte_p;
3680 p->space_width = it->space_width;
3681 p->font_height = it->font_height;
3682 p->voffset = it->voffset;
3683 p->string_from_display_prop_p = it->string_from_display_prop_p;
3684 p->display_ellipsis_p = 0;
3685 ++it->sp;
3686 }
3687
3688
3689 /* Restore IT's settings from IT->stack. Called, for example, when no
3690 more overlay strings must be processed, and we return to delivering
3691 display elements from a buffer, or when the end of a string from a
3692 `display' property is reached and we return to delivering display
3693 elements from an overlay string, or from a buffer. */
3694
3695 static void
3696 pop_it (it)
3697 struct it *it;
3698 {
3699 struct iterator_stack_entry *p;
3700
3701 xassert (it->sp > 0);
3702 --it->sp;
3703 p = it->stack + it->sp;
3704 it->stop_charpos = p->stop_charpos;
3705 it->face_id = p->face_id;
3706 it->string = p->string;
3707 it->current = p->pos;
3708 it->end_charpos = p->end_charpos;
3709 it->string_nchars = p->string_nchars;
3710 it->area = p->area;
3711 it->multibyte_p = p->multibyte_p;
3712 it->space_width = p->space_width;
3713 it->font_height = p->font_height;
3714 it->voffset = p->voffset;
3715 it->string_from_display_prop_p = p->string_from_display_prop_p;
3716 }
3717
3718
3719 \f
3720 /***********************************************************************
3721 Moving over lines
3722 ***********************************************************************/
3723
3724 /* Set IT's current position to the previous line start. */
3725
3726 static void
3727 back_to_previous_line_start (it)
3728 struct it *it;
3729 {
3730 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3731 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3732 }
3733
3734
3735 /* Move IT to the next line start.
3736
3737 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3738 we skipped over part of the text (as opposed to moving the iterator
3739 continuously over the text). Otherwise, don't change the value
3740 of *SKIPPED_P.
3741
3742 Newlines may come from buffer text, overlay strings, or strings
3743 displayed via the `display' property. That's the reason we can't
3744 simply use find_next_newline_no_quit.
3745
3746 Note that this function may not skip over invisible text that is so
3747 because of text properties and immediately follows a newline. If
3748 it would, function reseat_at_next_visible_line_start, when called
3749 from set_iterator_to_next, would effectively make invisible
3750 characters following a newline part of the wrong glyph row, which
3751 leads to wrong cursor motion. */
3752
3753 static int
3754 forward_to_next_line_start (it, skipped_p)
3755 struct it *it;
3756 int *skipped_p;
3757 {
3758 int old_selective, newline_found_p, n;
3759 const int MAX_NEWLINE_DISTANCE = 500;
3760
3761 /* If already on a newline, just consume it to avoid unintended
3762 skipping over invisible text below. */
3763 if (it->what == IT_CHARACTER
3764 && it->c == '\n'
3765 && CHARPOS (it->position) == IT_CHARPOS (*it))
3766 {
3767 set_iterator_to_next (it, 0);
3768 it->c = 0;
3769 return 1;
3770 }
3771
3772 /* Don't handle selective display in the following. It's (a)
3773 unnecessary because it's done by the caller, and (b) leads to an
3774 infinite recursion because next_element_from_ellipsis indirectly
3775 calls this function. */
3776 old_selective = it->selective;
3777 it->selective = 0;
3778
3779 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3780 from buffer text. */
3781 for (n = newline_found_p = 0;
3782 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3783 n += STRINGP (it->string) ? 0 : 1)
3784 {
3785 if (!get_next_display_element (it))
3786 break;
3787 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3788 set_iterator_to_next (it, 0);
3789 }
3790
3791 /* If we didn't find a newline near enough, see if we can use a
3792 short-cut. */
3793 if (n == MAX_NEWLINE_DISTANCE)
3794 {
3795 int start = IT_CHARPOS (*it);
3796 int limit = find_next_newline_no_quit (start, 1);
3797 Lisp_Object pos;
3798
3799 xassert (!STRINGP (it->string));
3800
3801 /* If there isn't any `display' property in sight, and no
3802 overlays, we can just use the position of the newline in
3803 buffer text. */
3804 if (it->stop_charpos >= limit
3805 || ((pos = Fnext_single_property_change (make_number (start),
3806 Qdisplay,
3807 Qnil, make_number (limit)),
3808 NILP (pos))
3809 && next_overlay_change (start) == ZV))
3810 {
3811 IT_CHARPOS (*it) = limit;
3812 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3813 *skipped_p = newline_found_p = 1;
3814 }
3815 else
3816 {
3817 while (get_next_display_element (it)
3818 && !newline_found_p)
3819 {
3820 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3821 set_iterator_to_next (it, 0);
3822 }
3823 }
3824 }
3825
3826 it->selective = old_selective;
3827 return newline_found_p;
3828 }
3829
3830
3831 /* Set IT's current position to the previous visible line start. Skip
3832 invisible text that is so either due to text properties or due to
3833 selective display. Caution: this does not change IT->current_x and
3834 IT->hpos. */
3835
3836 static void
3837 back_to_previous_visible_line_start (it)
3838 struct it *it;
3839 {
3840 int visible_p = 0;
3841
3842 /* Go back one newline if not on BEGV already. */
3843 if (IT_CHARPOS (*it) > BEGV)
3844 back_to_previous_line_start (it);
3845
3846 /* Move over lines that are invisible because of selective display
3847 or text properties. */
3848 while (IT_CHARPOS (*it) > BEGV
3849 && !visible_p)
3850 {
3851 visible_p = 1;
3852
3853 /* If selective > 0, then lines indented more than that values
3854 are invisible. */
3855 if (it->selective > 0
3856 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3857 it->selective))
3858 visible_p = 0;
3859 else
3860 {
3861 Lisp_Object prop;
3862
3863 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3864 Qinvisible, it->window);
3865 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3866 visible_p = 0;
3867 }
3868
3869 /* Back one more newline if the current one is invisible. */
3870 if (!visible_p)
3871 back_to_previous_line_start (it);
3872 }
3873
3874 xassert (IT_CHARPOS (*it) >= BEGV);
3875 xassert (IT_CHARPOS (*it) == BEGV
3876 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3877 CHECK_IT (it);
3878 }
3879
3880
3881 /* Reseat iterator IT at the previous visible line start. Skip
3882 invisible text that is so either due to text properties or due to
3883 selective display. At the end, update IT's overlay information,
3884 face information etc. */
3885
3886 static void
3887 reseat_at_previous_visible_line_start (it)
3888 struct it *it;
3889 {
3890 back_to_previous_visible_line_start (it);
3891 reseat (it, it->current.pos, 1);
3892 CHECK_IT (it);
3893 }
3894
3895
3896 /* Reseat iterator IT on the next visible line start in the current
3897 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3898 preceding the line start. Skip over invisible text that is so
3899 because of selective display. Compute faces, overlays etc at the
3900 new position. Note that this function does not skip over text that
3901 is invisible because of text properties. */
3902
3903 static void
3904 reseat_at_next_visible_line_start (it, on_newline_p)
3905 struct it *it;
3906 int on_newline_p;
3907 {
3908 int newline_found_p, skipped_p = 0;
3909
3910 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3911
3912 /* Skip over lines that are invisible because they are indented
3913 more than the value of IT->selective. */
3914 if (it->selective > 0)
3915 while (IT_CHARPOS (*it) < ZV
3916 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3917 it->selective))
3918 {
3919 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3920 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3921 }
3922
3923 /* Position on the newline if that's what's requested. */
3924 if (on_newline_p && newline_found_p)
3925 {
3926 if (STRINGP (it->string))
3927 {
3928 if (IT_STRING_CHARPOS (*it) > 0)
3929 {
3930 --IT_STRING_CHARPOS (*it);
3931 --IT_STRING_BYTEPOS (*it);
3932 }
3933 }
3934 else if (IT_CHARPOS (*it) > BEGV)
3935 {
3936 --IT_CHARPOS (*it);
3937 --IT_BYTEPOS (*it);
3938 reseat (it, it->current.pos, 0);
3939 }
3940 }
3941 else if (skipped_p)
3942 reseat (it, it->current.pos, 0);
3943
3944 CHECK_IT (it);
3945 }
3946
3947
3948 \f
3949 /***********************************************************************
3950 Changing an iterator's position
3951 ***********************************************************************/
3952
3953 /* Change IT's current position to POS in current_buffer. If FORCE_P
3954 is non-zero, always check for text properties at the new position.
3955 Otherwise, text properties are only looked up if POS >=
3956 IT->check_charpos of a property. */
3957
3958 static void
3959 reseat (it, pos, force_p)
3960 struct it *it;
3961 struct text_pos pos;
3962 int force_p;
3963 {
3964 int original_pos = IT_CHARPOS (*it);
3965
3966 reseat_1 (it, pos, 0);
3967
3968 /* Determine where to check text properties. Avoid doing it
3969 where possible because text property lookup is very expensive. */
3970 if (force_p
3971 || CHARPOS (pos) > it->stop_charpos
3972 || CHARPOS (pos) < original_pos)
3973 handle_stop (it);
3974
3975 CHECK_IT (it);
3976 }
3977
3978
3979 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3980 IT->stop_pos to POS, also. */
3981
3982 static void
3983 reseat_1 (it, pos, set_stop_p)
3984 struct it *it;
3985 struct text_pos pos;
3986 int set_stop_p;
3987 {
3988 /* Don't call this function when scanning a C string. */
3989 xassert (it->s == NULL);
3990
3991 /* POS must be a reasonable value. */
3992 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3993
3994 it->current.pos = it->position = pos;
3995 XSETBUFFER (it->object, current_buffer);
3996 it->end_charpos = ZV;
3997 it->dpvec = NULL;
3998 it->current.dpvec_index = -1;
3999 it->current.overlay_string_index = -1;
4000 IT_STRING_CHARPOS (*it) = -1;
4001 IT_STRING_BYTEPOS (*it) = -1;
4002 it->string = Qnil;
4003 it->method = next_element_from_buffer;
4004 it->sp = 0;
4005 it->face_before_selective_p = 0;
4006
4007 if (set_stop_p)
4008 it->stop_charpos = CHARPOS (pos);
4009 }
4010
4011
4012 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4013 If S is non-null, it is a C string to iterate over. Otherwise,
4014 STRING gives a Lisp string to iterate over.
4015
4016 If PRECISION > 0, don't return more then PRECISION number of
4017 characters from the string.
4018
4019 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4020 characters have been returned. FIELD_WIDTH < 0 means an infinite
4021 field width.
4022
4023 MULTIBYTE = 0 means disable processing of multibyte characters,
4024 MULTIBYTE > 0 means enable it,
4025 MULTIBYTE < 0 means use IT->multibyte_p.
4026
4027 IT must be initialized via a prior call to init_iterator before
4028 calling this function. */
4029
4030 static void
4031 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4032 struct it *it;
4033 unsigned char *s;
4034 Lisp_Object string;
4035 int charpos;
4036 int precision, field_width, multibyte;
4037 {
4038 /* No region in strings. */
4039 it->region_beg_charpos = it->region_end_charpos = -1;
4040
4041 /* No text property checks performed by default, but see below. */
4042 it->stop_charpos = -1;
4043
4044 /* Set iterator position and end position. */
4045 bzero (&it->current, sizeof it->current);
4046 it->current.overlay_string_index = -1;
4047 it->current.dpvec_index = -1;
4048 xassert (charpos >= 0);
4049
4050 /* If STRING is specified, use its multibyteness, otherwise use the
4051 setting of MULTIBYTE, if specified. */
4052 if (multibyte >= 0)
4053 it->multibyte_p = multibyte > 0;
4054
4055 if (s == NULL)
4056 {
4057 xassert (STRINGP (string));
4058 it->string = string;
4059 it->s = NULL;
4060 it->end_charpos = it->string_nchars = XSTRING (string)->size;
4061 it->method = next_element_from_string;
4062 it->current.string_pos = string_pos (charpos, string);
4063 }
4064 else
4065 {
4066 it->s = s;
4067 it->string = Qnil;
4068
4069 /* Note that we use IT->current.pos, not it->current.string_pos,
4070 for displaying C strings. */
4071 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4072 if (it->multibyte_p)
4073 {
4074 it->current.pos = c_string_pos (charpos, s, 1);
4075 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4076 }
4077 else
4078 {
4079 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4080 it->end_charpos = it->string_nchars = strlen (s);
4081 }
4082
4083 it->method = next_element_from_c_string;
4084 }
4085
4086 /* PRECISION > 0 means don't return more than PRECISION characters
4087 from the string. */
4088 if (precision > 0 && it->end_charpos - charpos > precision)
4089 it->end_charpos = it->string_nchars = charpos + precision;
4090
4091 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4092 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4093 FIELD_WIDTH < 0 means infinite field width. This is useful for
4094 padding with `-' at the end of a mode line. */
4095 if (field_width < 0)
4096 field_width = INFINITY;
4097 if (field_width > it->end_charpos - charpos)
4098 it->end_charpos = charpos + field_width;
4099
4100 /* Use the standard display table for displaying strings. */
4101 if (DISP_TABLE_P (Vstandard_display_table))
4102 it->dp = XCHAR_TABLE (Vstandard_display_table);
4103
4104 it->stop_charpos = charpos;
4105 CHECK_IT (it);
4106 }
4107
4108
4109 \f
4110 /***********************************************************************
4111 Iteration
4112 ***********************************************************************/
4113
4114 /* Load IT's display element fields with information about the next
4115 display element from the current position of IT. Value is zero if
4116 end of buffer (or C string) is reached. */
4117
4118 int
4119 get_next_display_element (it)
4120 struct it *it;
4121 {
4122 /* Non-zero means that we found an display element. Zero means that
4123 we hit the end of what we iterate over. Performance note: the
4124 function pointer `method' used here turns out to be faster than
4125 using a sequence of if-statements. */
4126 int success_p = (*it->method) (it);
4127
4128 if (it->what == IT_CHARACTER)
4129 {
4130 /* Map via display table or translate control characters.
4131 IT->c, IT->len etc. have been set to the next character by
4132 the function call above. If we have a display table, and it
4133 contains an entry for IT->c, translate it. Don't do this if
4134 IT->c itself comes from a display table, otherwise we could
4135 end up in an infinite recursion. (An alternative could be to
4136 count the recursion depth of this function and signal an
4137 error when a certain maximum depth is reached.) Is it worth
4138 it? */
4139 if (success_p && it->dpvec == NULL)
4140 {
4141 Lisp_Object dv;
4142
4143 if (it->dp
4144 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4145 VECTORP (dv)))
4146 {
4147 struct Lisp_Vector *v = XVECTOR (dv);
4148
4149 /* Return the first character from the display table
4150 entry, if not empty. If empty, don't display the
4151 current character. */
4152 if (v->size)
4153 {
4154 it->dpvec_char_len = it->len;
4155 it->dpvec = v->contents;
4156 it->dpend = v->contents + v->size;
4157 it->current.dpvec_index = 0;
4158 it->method = next_element_from_display_vector;
4159 success_p = get_next_display_element (it);
4160 }
4161 else
4162 {
4163 set_iterator_to_next (it, 0);
4164 success_p = get_next_display_element (it);
4165 }
4166 }
4167
4168 /* Translate control characters into `\003' or `^C' form.
4169 Control characters coming from a display table entry are
4170 currently not translated because we use IT->dpvec to hold
4171 the translation. This could easily be changed but I
4172 don't believe that it is worth doing.
4173
4174 Non-printable multibyte characters are also translated
4175 octal form. */
4176 else if ((it->c < ' '
4177 && (it->area != TEXT_AREA
4178 || (it->c != '\n' && it->c != '\t')))
4179 || (it->c >= 127
4180 && it->len == 1)
4181 || !CHAR_PRINTABLE_P (it->c))
4182 {
4183 /* IT->c is a control character which must be displayed
4184 either as '\003' or as `^C' where the '\\' and '^'
4185 can be defined in the display table. Fill
4186 IT->ctl_chars with glyphs for what we have to
4187 display. Then, set IT->dpvec to these glyphs. */
4188 GLYPH g;
4189
4190 if (it->c < 128 && it->ctl_arrow_p)
4191 {
4192 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4193 if (it->dp
4194 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4195 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4196 g = XINT (DISP_CTRL_GLYPH (it->dp));
4197 else
4198 g = FAST_MAKE_GLYPH ('^', 0);
4199 XSETINT (it->ctl_chars[0], g);
4200
4201 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4202 XSETINT (it->ctl_chars[1], g);
4203
4204 /* Set up IT->dpvec and return first character from it. */
4205 it->dpvec_char_len = it->len;
4206 it->dpvec = it->ctl_chars;
4207 it->dpend = it->dpvec + 2;
4208 it->current.dpvec_index = 0;
4209 it->method = next_element_from_display_vector;
4210 get_next_display_element (it);
4211 }
4212 else
4213 {
4214 unsigned char str[MAX_MULTIBYTE_LENGTH];
4215 int len;
4216 int i;
4217 GLYPH escape_glyph;
4218
4219 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4220 if (it->dp
4221 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4222 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4223 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4224 else
4225 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4226
4227 if (SINGLE_BYTE_CHAR_P (it->c))
4228 str[0] = it->c, len = 1;
4229 else
4230 {
4231 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4232 if (len < 0)
4233 {
4234 /* It's an invalid character, which
4235 shouldn't happen actually, but due to
4236 bugs it may happen. Let's print the char
4237 as is, there's not much meaningful we can
4238 do with it. */
4239 str[0] = it->c;
4240 str[1] = it->c >> 8;
4241 str[2] = it->c >> 16;
4242 str[3] = it->c >> 24;
4243 len = 4;
4244 }
4245 }
4246
4247 for (i = 0; i < len; i++)
4248 {
4249 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4250 /* Insert three more glyphs into IT->ctl_chars for
4251 the octal display of the character. */
4252 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4253 XSETINT (it->ctl_chars[i * 4 + 1], g);
4254 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4255 XSETINT (it->ctl_chars[i * 4 + 2], g);
4256 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4257 XSETINT (it->ctl_chars[i * 4 + 3], g);
4258 }
4259
4260 /* Set up IT->dpvec and return the first character
4261 from it. */
4262 it->dpvec_char_len = it->len;
4263 it->dpvec = it->ctl_chars;
4264 it->dpend = it->dpvec + len * 4;
4265 it->current.dpvec_index = 0;
4266 it->method = next_element_from_display_vector;
4267 get_next_display_element (it);
4268 }
4269 }
4270 }
4271
4272 /* Adjust face id for a multibyte character. There are no
4273 multibyte character in unibyte text. */
4274 if (it->multibyte_p
4275 && success_p
4276 && FRAME_WINDOW_P (it->f))
4277 {
4278 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4279 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4280 }
4281 }
4282
4283 /* Is this character the last one of a run of characters with
4284 box? If yes, set IT->end_of_box_run_p to 1. */
4285 if (it->face_box_p
4286 && it->s == NULL)
4287 {
4288 int face_id;
4289 struct face *face;
4290
4291 it->end_of_box_run_p
4292 = ((face_id = face_after_it_pos (it),
4293 face_id != it->face_id)
4294 && (face = FACE_FROM_ID (it->f, face_id),
4295 face->box == FACE_NO_BOX));
4296 }
4297
4298 /* Value is 0 if end of buffer or string reached. */
4299 return success_p;
4300 }
4301
4302
4303 /* Move IT to the next display element.
4304
4305 RESEAT_P non-zero means if called on a newline in buffer text,
4306 skip to the next visible line start.
4307
4308 Functions get_next_display_element and set_iterator_to_next are
4309 separate because I find this arrangement easier to handle than a
4310 get_next_display_element function that also increments IT's
4311 position. The way it is we can first look at an iterator's current
4312 display element, decide whether it fits on a line, and if it does,
4313 increment the iterator position. The other way around we probably
4314 would either need a flag indicating whether the iterator has to be
4315 incremented the next time, or we would have to implement a
4316 decrement position function which would not be easy to write. */
4317
4318 void
4319 set_iterator_to_next (it, reseat_p)
4320 struct it *it;
4321 int reseat_p;
4322 {
4323 /* Reset flags indicating start and end of a sequence of characters
4324 with box. Reset them at the start of this function because
4325 moving the iterator to a new position might set them. */
4326 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4327
4328 if (it->method == next_element_from_buffer)
4329 {
4330 /* The current display element of IT is a character from
4331 current_buffer. Advance in the buffer, and maybe skip over
4332 invisible lines that are so because of selective display. */
4333 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4334 reseat_at_next_visible_line_start (it, 0);
4335 else
4336 {
4337 xassert (it->len != 0);
4338 IT_BYTEPOS (*it) += it->len;
4339 IT_CHARPOS (*it) += 1;
4340 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4341 }
4342 }
4343 else if (it->method == next_element_from_composition)
4344 {
4345 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4346 if (STRINGP (it->string))
4347 {
4348 IT_STRING_BYTEPOS (*it) += it->len;
4349 IT_STRING_CHARPOS (*it) += it->cmp_len;
4350 it->method = next_element_from_string;
4351 goto consider_string_end;
4352 }
4353 else
4354 {
4355 IT_BYTEPOS (*it) += it->len;
4356 IT_CHARPOS (*it) += it->cmp_len;
4357 it->method = next_element_from_buffer;
4358 }
4359 }
4360 else if (it->method == next_element_from_c_string)
4361 {
4362 /* Current display element of IT is from a C string. */
4363 IT_BYTEPOS (*it) += it->len;
4364 IT_CHARPOS (*it) += 1;
4365 }
4366 else if (it->method == next_element_from_display_vector)
4367 {
4368 /* Current display element of IT is from a display table entry.
4369 Advance in the display table definition. Reset it to null if
4370 end reached, and continue with characters from buffers/
4371 strings. */
4372 ++it->current.dpvec_index;
4373
4374 /* Restore face of the iterator to what they were before the
4375 display vector entry (these entries may contain faces). */
4376 it->face_id = it->saved_face_id;
4377
4378 if (it->dpvec + it->current.dpvec_index == it->dpend)
4379 {
4380 if (it->s)
4381 it->method = next_element_from_c_string;
4382 else if (STRINGP (it->string))
4383 it->method = next_element_from_string;
4384 else
4385 it->method = next_element_from_buffer;
4386
4387 it->dpvec = NULL;
4388 it->current.dpvec_index = -1;
4389
4390 /* Skip over characters which were displayed via IT->dpvec. */
4391 if (it->dpvec_char_len < 0)
4392 reseat_at_next_visible_line_start (it, 1);
4393 else if (it->dpvec_char_len > 0)
4394 {
4395 it->len = it->dpvec_char_len;
4396 set_iterator_to_next (it, reseat_p);
4397 }
4398 }
4399 }
4400 else if (it->method == next_element_from_string)
4401 {
4402 /* Current display element is a character from a Lisp string. */
4403 xassert (it->s == NULL && STRINGP (it->string));
4404 IT_STRING_BYTEPOS (*it) += it->len;
4405 IT_STRING_CHARPOS (*it) += 1;
4406
4407 consider_string_end:
4408
4409 if (it->current.overlay_string_index >= 0)
4410 {
4411 /* IT->string is an overlay string. Advance to the
4412 next, if there is one. */
4413 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4414 next_overlay_string (it);
4415 }
4416 else
4417 {
4418 /* IT->string is not an overlay string. If we reached
4419 its end, and there is something on IT->stack, proceed
4420 with what is on the stack. This can be either another
4421 string, this time an overlay string, or a buffer. */
4422 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4423 && it->sp > 0)
4424 {
4425 pop_it (it);
4426 if (!STRINGP (it->string))
4427 it->method = next_element_from_buffer;
4428 else
4429 goto consider_string_end;
4430 }
4431 }
4432 }
4433 else if (it->method == next_element_from_image
4434 || it->method == next_element_from_stretch)
4435 {
4436 /* The position etc with which we have to proceed are on
4437 the stack. The position may be at the end of a string,
4438 if the `display' property takes up the whole string. */
4439 pop_it (it);
4440 it->image_id = 0;
4441 if (STRINGP (it->string))
4442 {
4443 it->method = next_element_from_string;
4444 goto consider_string_end;
4445 }
4446 else
4447 it->method = next_element_from_buffer;
4448 }
4449 else
4450 /* There are no other methods defined, so this should be a bug. */
4451 abort ();
4452
4453 xassert (it->method != next_element_from_string
4454 || (STRINGP (it->string)
4455 && IT_STRING_CHARPOS (*it) >= 0));
4456 }
4457
4458
4459 /* Load IT's display element fields with information about the next
4460 display element which comes from a display table entry or from the
4461 result of translating a control character to one of the forms `^C'
4462 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4463
4464 static int
4465 next_element_from_display_vector (it)
4466 struct it *it;
4467 {
4468 /* Precondition. */
4469 xassert (it->dpvec && it->current.dpvec_index >= 0);
4470
4471 /* Remember the current face id in case glyphs specify faces.
4472 IT's face is restored in set_iterator_to_next. */
4473 it->saved_face_id = it->face_id;
4474
4475 if (INTEGERP (*it->dpvec)
4476 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4477 {
4478 int lface_id;
4479 GLYPH g;
4480
4481 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4482 it->c = FAST_GLYPH_CHAR (g);
4483 it->len = CHAR_BYTES (it->c);
4484
4485 /* The entry may contain a face id to use. Such a face id is
4486 the id of a Lisp face, not a realized face. A face id of
4487 zero means no face is specified. */
4488 lface_id = FAST_GLYPH_FACE (g);
4489 if (lface_id)
4490 {
4491 /* The function returns -1 if lface_id is invalid. */
4492 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4493 if (face_id >= 0)
4494 it->face_id = face_id;
4495 }
4496 }
4497 else
4498 /* Display table entry is invalid. Return a space. */
4499 it->c = ' ', it->len = 1;
4500
4501 /* Don't change position and object of the iterator here. They are
4502 still the values of the character that had this display table
4503 entry or was translated, and that's what we want. */
4504 it->what = IT_CHARACTER;
4505 return 1;
4506 }
4507
4508
4509 /* Load IT with the next display element from Lisp string IT->string.
4510 IT->current.string_pos is the current position within the string.
4511 If IT->current.overlay_string_index >= 0, the Lisp string is an
4512 overlay string. */
4513
4514 static int
4515 next_element_from_string (it)
4516 struct it *it;
4517 {
4518 struct text_pos position;
4519
4520 xassert (STRINGP (it->string));
4521 xassert (IT_STRING_CHARPOS (*it) >= 0);
4522 position = it->current.string_pos;
4523
4524 /* Time to check for invisible text? */
4525 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4526 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4527 {
4528 handle_stop (it);
4529
4530 /* Since a handler may have changed IT->method, we must
4531 recurse here. */
4532 return get_next_display_element (it);
4533 }
4534
4535 if (it->current.overlay_string_index >= 0)
4536 {
4537 /* Get the next character from an overlay string. In overlay
4538 strings, There is no field width or padding with spaces to
4539 do. */
4540 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4541 {
4542 it->what = IT_EOB;
4543 return 0;
4544 }
4545 else if (STRING_MULTIBYTE (it->string))
4546 {
4547 int remaining = (STRING_BYTES (XSTRING (it->string))
4548 - IT_STRING_BYTEPOS (*it));
4549 unsigned char *s = (XSTRING (it->string)->data
4550 + IT_STRING_BYTEPOS (*it));
4551 it->c = string_char_and_length (s, remaining, &it->len);
4552 }
4553 else
4554 {
4555 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4556 it->len = 1;
4557 }
4558 }
4559 else
4560 {
4561 /* Get the next character from a Lisp string that is not an
4562 overlay string. Such strings come from the mode line, for
4563 example. We may have to pad with spaces, or truncate the
4564 string. See also next_element_from_c_string. */
4565 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4566 {
4567 it->what = IT_EOB;
4568 return 0;
4569 }
4570 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4571 {
4572 /* Pad with spaces. */
4573 it->c = ' ', it->len = 1;
4574 CHARPOS (position) = BYTEPOS (position) = -1;
4575 }
4576 else if (STRING_MULTIBYTE (it->string))
4577 {
4578 int maxlen = (STRING_BYTES (XSTRING (it->string))
4579 - IT_STRING_BYTEPOS (*it));
4580 unsigned char *s = (XSTRING (it->string)->data
4581 + IT_STRING_BYTEPOS (*it));
4582 it->c = string_char_and_length (s, maxlen, &it->len);
4583 }
4584 else
4585 {
4586 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4587 it->len = 1;
4588 }
4589 }
4590
4591 /* Record what we have and where it came from. Note that we store a
4592 buffer position in IT->position although it could arguably be a
4593 string position. */
4594 it->what = IT_CHARACTER;
4595 it->object = it->string;
4596 it->position = position;
4597 return 1;
4598 }
4599
4600
4601 /* Load IT with next display element from C string IT->s.
4602 IT->string_nchars is the maximum number of characters to return
4603 from the string. IT->end_charpos may be greater than
4604 IT->string_nchars when this function is called, in which case we
4605 may have to return padding spaces. Value is zero if end of string
4606 reached, including padding spaces. */
4607
4608 static int
4609 next_element_from_c_string (it)
4610 struct it *it;
4611 {
4612 int success_p = 1;
4613
4614 xassert (it->s);
4615 it->what = IT_CHARACTER;
4616 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4617 it->object = Qnil;
4618
4619 /* IT's position can be greater IT->string_nchars in case a field
4620 width or precision has been specified when the iterator was
4621 initialized. */
4622 if (IT_CHARPOS (*it) >= it->end_charpos)
4623 {
4624 /* End of the game. */
4625 it->what = IT_EOB;
4626 success_p = 0;
4627 }
4628 else if (IT_CHARPOS (*it) >= it->string_nchars)
4629 {
4630 /* Pad with spaces. */
4631 it->c = ' ', it->len = 1;
4632 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4633 }
4634 else if (it->multibyte_p)
4635 {
4636 /* Implementation note: The calls to strlen apparently aren't a
4637 performance problem because there is no noticeable performance
4638 difference between Emacs running in unibyte or multibyte mode. */
4639 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4640 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4641 maxlen, &it->len);
4642 }
4643 else
4644 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4645
4646 return success_p;
4647 }
4648
4649
4650 /* Set up IT to return characters from an ellipsis, if appropriate.
4651 The definition of the ellipsis glyphs may come from a display table
4652 entry. This function Fills IT with the first glyph from the
4653 ellipsis if an ellipsis is to be displayed. */
4654
4655 static int
4656 next_element_from_ellipsis (it)
4657 struct it *it;
4658 {
4659 if (it->selective_display_ellipsis_p)
4660 {
4661 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4662 {
4663 /* Use the display table definition for `...'. Invalid glyphs
4664 will be handled by the method returning elements from dpvec. */
4665 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4666 it->dpvec_char_len = it->len;
4667 it->dpvec = v->contents;
4668 it->dpend = v->contents + v->size;
4669 it->current.dpvec_index = 0;
4670 it->method = next_element_from_display_vector;
4671 }
4672 else
4673 {
4674 /* Use default `...' which is stored in default_invis_vector. */
4675 it->dpvec_char_len = it->len;
4676 it->dpvec = default_invis_vector;
4677 it->dpend = default_invis_vector + 3;
4678 it->current.dpvec_index = 0;
4679 it->method = next_element_from_display_vector;
4680 }
4681 }
4682 else
4683 {
4684 /* The face at the current position may be different from the
4685 face we find after the invisible text. Remember what it
4686 was in IT->saved_face_id, and signal that it's there by
4687 setting face_before_selective_p. */
4688 it->saved_face_id = it->face_id;
4689 it->method = next_element_from_buffer;
4690 reseat_at_next_visible_line_start (it, 1);
4691 it->face_before_selective_p = 1;
4692 }
4693
4694 return get_next_display_element (it);
4695 }
4696
4697
4698 /* Deliver an image display element. The iterator IT is already
4699 filled with image information (done in handle_display_prop). Value
4700 is always 1. */
4701
4702
4703 static int
4704 next_element_from_image (it)
4705 struct it *it;
4706 {
4707 it->what = IT_IMAGE;
4708 return 1;
4709 }
4710
4711
4712 /* Fill iterator IT with next display element from a stretch glyph
4713 property. IT->object is the value of the text property. Value is
4714 always 1. */
4715
4716 static int
4717 next_element_from_stretch (it)
4718 struct it *it;
4719 {
4720 it->what = IT_STRETCH;
4721 return 1;
4722 }
4723
4724
4725 /* Load IT with the next display element from current_buffer. Value
4726 is zero if end of buffer reached. IT->stop_charpos is the next
4727 position at which to stop and check for text properties or buffer
4728 end. */
4729
4730 static int
4731 next_element_from_buffer (it)
4732 struct it *it;
4733 {
4734 int success_p = 1;
4735
4736 /* Check this assumption, otherwise, we would never enter the
4737 if-statement, below. */
4738 xassert (IT_CHARPOS (*it) >= BEGV
4739 && IT_CHARPOS (*it) <= it->stop_charpos);
4740
4741 if (IT_CHARPOS (*it) >= it->stop_charpos)
4742 {
4743 if (IT_CHARPOS (*it) >= it->end_charpos)
4744 {
4745 int overlay_strings_follow_p;
4746
4747 /* End of the game, except when overlay strings follow that
4748 haven't been returned yet. */
4749 if (it->overlay_strings_at_end_processed_p)
4750 overlay_strings_follow_p = 0;
4751 else
4752 {
4753 it->overlay_strings_at_end_processed_p = 1;
4754 overlay_strings_follow_p = get_overlay_strings (it, 0);
4755 }
4756
4757 if (overlay_strings_follow_p)
4758 success_p = get_next_display_element (it);
4759 else
4760 {
4761 it->what = IT_EOB;
4762 it->position = it->current.pos;
4763 success_p = 0;
4764 }
4765 }
4766 else
4767 {
4768 handle_stop (it);
4769 return get_next_display_element (it);
4770 }
4771 }
4772 else
4773 {
4774 /* No face changes, overlays etc. in sight, so just return a
4775 character from current_buffer. */
4776 unsigned char *p;
4777
4778 /* Maybe run the redisplay end trigger hook. Performance note:
4779 This doesn't seem to cost measurable time. */
4780 if (it->redisplay_end_trigger_charpos
4781 && it->glyph_row
4782 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4783 run_redisplay_end_trigger_hook (it);
4784
4785 /* Get the next character, maybe multibyte. */
4786 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4787 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4788 {
4789 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4790 - IT_BYTEPOS (*it));
4791 it->c = string_char_and_length (p, maxlen, &it->len);
4792 }
4793 else
4794 it->c = *p, it->len = 1;
4795
4796 /* Record what we have and where it came from. */
4797 it->what = IT_CHARACTER;;
4798 it->object = it->w->buffer;
4799 it->position = it->current.pos;
4800
4801 /* Normally we return the character found above, except when we
4802 really want to return an ellipsis for selective display. */
4803 if (it->selective)
4804 {
4805 if (it->c == '\n')
4806 {
4807 /* A value of selective > 0 means hide lines indented more
4808 than that number of columns. */
4809 if (it->selective > 0
4810 && IT_CHARPOS (*it) + 1 < ZV
4811 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4812 IT_BYTEPOS (*it) + 1,
4813 it->selective))
4814 {
4815 success_p = next_element_from_ellipsis (it);
4816 it->dpvec_char_len = -1;
4817 }
4818 }
4819 else if (it->c == '\r' && it->selective == -1)
4820 {
4821 /* A value of selective == -1 means that everything from the
4822 CR to the end of the line is invisible, with maybe an
4823 ellipsis displayed for it. */
4824 success_p = next_element_from_ellipsis (it);
4825 it->dpvec_char_len = -1;
4826 }
4827 }
4828 }
4829
4830 /* Value is zero if end of buffer reached. */
4831 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4832 return success_p;
4833 }
4834
4835
4836 /* Run the redisplay end trigger hook for IT. */
4837
4838 static void
4839 run_redisplay_end_trigger_hook (it)
4840 struct it *it;
4841 {
4842 Lisp_Object args[3];
4843
4844 /* IT->glyph_row should be non-null, i.e. we should be actually
4845 displaying something, or otherwise we should not run the hook. */
4846 xassert (it->glyph_row);
4847
4848 /* Set up hook arguments. */
4849 args[0] = Qredisplay_end_trigger_functions;
4850 args[1] = it->window;
4851 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4852 it->redisplay_end_trigger_charpos = 0;
4853
4854 /* Since we are *trying* to run these functions, don't try to run
4855 them again, even if they get an error. */
4856 it->w->redisplay_end_trigger = Qnil;
4857 Frun_hook_with_args (3, args);
4858
4859 /* Notice if it changed the face of the character we are on. */
4860 handle_face_prop (it);
4861 }
4862
4863
4864 /* Deliver a composition display element. The iterator IT is already
4865 filled with composition information (done in
4866 handle_composition_prop). Value is always 1. */
4867
4868 static int
4869 next_element_from_composition (it)
4870 struct it *it;
4871 {
4872 it->what = IT_COMPOSITION;
4873 it->position = (STRINGP (it->string)
4874 ? it->current.string_pos
4875 : it->current.pos);
4876 return 1;
4877 }
4878
4879
4880 \f
4881 /***********************************************************************
4882 Moving an iterator without producing glyphs
4883 ***********************************************************************/
4884
4885 /* Move iterator IT to a specified buffer or X position within one
4886 line on the display without producing glyphs.
4887
4888 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4889 whichever is reached first.
4890
4891 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4892
4893 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4894 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4895 TO_X includes the amount by which a window is horizontally
4896 scrolled.
4897
4898 Value is
4899
4900 MOVE_POS_MATCH_OR_ZV
4901 - when TO_POS or ZV was reached.
4902
4903 MOVE_X_REACHED
4904 -when TO_X was reached before TO_POS or ZV were reached.
4905
4906 MOVE_LINE_CONTINUED
4907 - when we reached the end of the display area and the line must
4908 be continued.
4909
4910 MOVE_LINE_TRUNCATED
4911 - when we reached the end of the display area and the line is
4912 truncated.
4913
4914 MOVE_NEWLINE_OR_CR
4915 - when we stopped at a line end, i.e. a newline or a CR and selective
4916 display is on. */
4917
4918 static enum move_it_result
4919 move_it_in_display_line_to (it, to_charpos, to_x, op)
4920 struct it *it;
4921 int to_charpos, to_x, op;
4922 {
4923 enum move_it_result result = MOVE_UNDEFINED;
4924 struct glyph_row *saved_glyph_row;
4925
4926 /* Don't produce glyphs in produce_glyphs. */
4927 saved_glyph_row = it->glyph_row;
4928 it->glyph_row = NULL;
4929
4930 while (1)
4931 {
4932 int x, i, ascent = 0, descent = 0;
4933
4934 /* Stop when ZV or TO_CHARPOS reached. */
4935 if (!get_next_display_element (it)
4936 || ((op & MOVE_TO_POS) != 0
4937 && BUFFERP (it->object)
4938 && IT_CHARPOS (*it) >= to_charpos))
4939 {
4940 result = MOVE_POS_MATCH_OR_ZV;
4941 break;
4942 }
4943
4944 /* The call to produce_glyphs will get the metrics of the
4945 display element IT is loaded with. We record in x the
4946 x-position before this display element in case it does not
4947 fit on the line. */
4948 x = it->current_x;
4949
4950 /* Remember the line height so far in case the next element doesn't
4951 fit on the line. */
4952 if (!it->truncate_lines_p)
4953 {
4954 ascent = it->max_ascent;
4955 descent = it->max_descent;
4956 }
4957
4958 PRODUCE_GLYPHS (it);
4959
4960 if (it->area != TEXT_AREA)
4961 {
4962 set_iterator_to_next (it, 1);
4963 continue;
4964 }
4965
4966 /* The number of glyphs we get back in IT->nglyphs will normally
4967 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4968 character on a terminal frame, or (iii) a line end. For the
4969 second case, IT->nglyphs - 1 padding glyphs will be present
4970 (on X frames, there is only one glyph produced for a
4971 composite character.
4972
4973 The behavior implemented below means, for continuation lines,
4974 that as many spaces of a TAB as fit on the current line are
4975 displayed there. For terminal frames, as many glyphs of a
4976 multi-glyph character are displayed in the current line, too.
4977 This is what the old redisplay code did, and we keep it that
4978 way. Under X, the whole shape of a complex character must
4979 fit on the line or it will be completely displayed in the
4980 next line.
4981
4982 Note that both for tabs and padding glyphs, all glyphs have
4983 the same width. */
4984 if (it->nglyphs)
4985 {
4986 /* More than one glyph or glyph doesn't fit on line. All
4987 glyphs have the same width. */
4988 int single_glyph_width = it->pixel_width / it->nglyphs;
4989 int new_x;
4990
4991 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4992 {
4993 new_x = x + single_glyph_width;
4994
4995 /* We want to leave anything reaching TO_X to the caller. */
4996 if ((op & MOVE_TO_X) && new_x > to_x)
4997 {
4998 it->current_x = x;
4999 result = MOVE_X_REACHED;
5000 break;
5001 }
5002 else if (/* Lines are continued. */
5003 !it->truncate_lines_p
5004 && (/* And glyph doesn't fit on the line. */
5005 new_x > it->last_visible_x
5006 /* Or it fits exactly and we're on a window
5007 system frame. */
5008 || (new_x == it->last_visible_x
5009 && FRAME_WINDOW_P (it->f))))
5010 {
5011 if (/* IT->hpos == 0 means the very first glyph
5012 doesn't fit on the line, e.g. a wide image. */
5013 it->hpos == 0
5014 || (new_x == it->last_visible_x
5015 && FRAME_WINDOW_P (it->f)))
5016 {
5017 ++it->hpos;
5018 it->current_x = new_x;
5019 if (i == it->nglyphs - 1)
5020 set_iterator_to_next (it, 1);
5021 }
5022 else
5023 {
5024 it->current_x = x;
5025 it->max_ascent = ascent;
5026 it->max_descent = descent;
5027 }
5028
5029 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5030 IT_CHARPOS (*it)));
5031 result = MOVE_LINE_CONTINUED;
5032 break;
5033 }
5034 else if (new_x > it->first_visible_x)
5035 {
5036 /* Glyph is visible. Increment number of glyphs that
5037 would be displayed. */
5038 ++it->hpos;
5039 }
5040 else
5041 {
5042 /* Glyph is completely off the left margin of the display
5043 area. Nothing to do. */
5044 }
5045 }
5046
5047 if (result != MOVE_UNDEFINED)
5048 break;
5049 }
5050 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5051 {
5052 /* Stop when TO_X specified and reached. This check is
5053 necessary here because of lines consisting of a line end,
5054 only. The line end will not produce any glyphs and we
5055 would never get MOVE_X_REACHED. */
5056 xassert (it->nglyphs == 0);
5057 result = MOVE_X_REACHED;
5058 break;
5059 }
5060
5061 /* Is this a line end? If yes, we're done. */
5062 if (ITERATOR_AT_END_OF_LINE_P (it))
5063 {
5064 result = MOVE_NEWLINE_OR_CR;
5065 break;
5066 }
5067
5068 /* The current display element has been consumed. Advance
5069 to the next. */
5070 set_iterator_to_next (it, 1);
5071
5072 /* Stop if lines are truncated and IT's current x-position is
5073 past the right edge of the window now. */
5074 if (it->truncate_lines_p
5075 && it->current_x >= it->last_visible_x)
5076 {
5077 result = MOVE_LINE_TRUNCATED;
5078 break;
5079 }
5080 }
5081
5082 /* Restore the iterator settings altered at the beginning of this
5083 function. */
5084 it->glyph_row = saved_glyph_row;
5085 return result;
5086 }
5087
5088
5089 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
5090 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
5091 the description of enum move_operation_enum.
5092
5093 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5094 screen line, this function will set IT to the next position >
5095 TO_CHARPOS. */
5096
5097 void
5098 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5099 struct it *it;
5100 int to_charpos, to_x, to_y, to_vpos;
5101 int op;
5102 {
5103 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5104 int line_height;
5105 int reached = 0;
5106
5107 for (;;)
5108 {
5109 if (op & MOVE_TO_VPOS)
5110 {
5111 /* If no TO_CHARPOS and no TO_X specified, stop at the
5112 start of the line TO_VPOS. */
5113 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5114 {
5115 if (it->vpos == to_vpos)
5116 {
5117 reached = 1;
5118 break;
5119 }
5120 else
5121 skip = move_it_in_display_line_to (it, -1, -1, 0);
5122 }
5123 else
5124 {
5125 /* TO_VPOS >= 0 means stop at TO_X in the line at
5126 TO_VPOS, or at TO_POS, whichever comes first. */
5127 if (it->vpos == to_vpos)
5128 {
5129 reached = 2;
5130 break;
5131 }
5132
5133 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5134
5135 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5136 {
5137 reached = 3;
5138 break;
5139 }
5140 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5141 {
5142 /* We have reached TO_X but not in the line we want. */
5143 skip = move_it_in_display_line_to (it, to_charpos,
5144 -1, MOVE_TO_POS);
5145 if (skip == MOVE_POS_MATCH_OR_ZV)
5146 {
5147 reached = 4;
5148 break;
5149 }
5150 }
5151 }
5152 }
5153 else if (op & MOVE_TO_Y)
5154 {
5155 struct it it_backup;
5156
5157 /* TO_Y specified means stop at TO_X in the line containing
5158 TO_Y---or at TO_CHARPOS if this is reached first. The
5159 problem is that we can't really tell whether the line
5160 contains TO_Y before we have completely scanned it, and
5161 this may skip past TO_X. What we do is to first scan to
5162 TO_X.
5163
5164 If TO_X is not specified, use a TO_X of zero. The reason
5165 is to make the outcome of this function more predictable.
5166 If we didn't use TO_X == 0, we would stop at the end of
5167 the line which is probably not what a caller would expect
5168 to happen. */
5169 skip = move_it_in_display_line_to (it, to_charpos,
5170 ((op & MOVE_TO_X)
5171 ? to_x : 0),
5172 (MOVE_TO_X
5173 | (op & MOVE_TO_POS)));
5174
5175 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5176 if (skip == MOVE_POS_MATCH_OR_ZV)
5177 {
5178 reached = 5;
5179 break;
5180 }
5181
5182 /* If TO_X was reached, we would like to know whether TO_Y
5183 is in the line. This can only be said if we know the
5184 total line height which requires us to scan the rest of
5185 the line. */
5186 if (skip == MOVE_X_REACHED)
5187 {
5188 it_backup = *it;
5189 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5190 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5191 op & MOVE_TO_POS);
5192 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5193 }
5194
5195 /* Now, decide whether TO_Y is in this line. */
5196 line_height = it->max_ascent + it->max_descent;
5197 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5198
5199 if (to_y >= it->current_y
5200 && to_y < it->current_y + line_height)
5201 {
5202 if (skip == MOVE_X_REACHED)
5203 /* If TO_Y is in this line and TO_X was reached above,
5204 we scanned too far. We have to restore IT's settings
5205 to the ones before skipping. */
5206 *it = it_backup;
5207 reached = 6;
5208 }
5209 else if (skip == MOVE_X_REACHED)
5210 {
5211 skip = skip2;
5212 if (skip == MOVE_POS_MATCH_OR_ZV)
5213 reached = 7;
5214 }
5215
5216 if (reached)
5217 break;
5218 }
5219 else
5220 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5221
5222 switch (skip)
5223 {
5224 case MOVE_POS_MATCH_OR_ZV:
5225 reached = 8;
5226 goto out;
5227
5228 case MOVE_NEWLINE_OR_CR:
5229 set_iterator_to_next (it, 1);
5230 it->continuation_lines_width = 0;
5231 break;
5232
5233 case MOVE_LINE_TRUNCATED:
5234 it->continuation_lines_width = 0;
5235 reseat_at_next_visible_line_start (it, 0);
5236 if ((op & MOVE_TO_POS) != 0
5237 && IT_CHARPOS (*it) > to_charpos)
5238 {
5239 reached = 9;
5240 goto out;
5241 }
5242 break;
5243
5244 case MOVE_LINE_CONTINUED:
5245 it->continuation_lines_width += it->current_x;
5246 break;
5247
5248 default:
5249 abort ();
5250 }
5251
5252 /* Reset/increment for the next run. */
5253 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5254 it->current_x = it->hpos = 0;
5255 it->current_y += it->max_ascent + it->max_descent;
5256 ++it->vpos;
5257 last_height = it->max_ascent + it->max_descent;
5258 last_max_ascent = it->max_ascent;
5259 it->max_ascent = it->max_descent = 0;
5260 }
5261
5262 out:
5263
5264 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5265 }
5266
5267
5268 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5269
5270 If DY > 0, move IT backward at least that many pixels. DY = 0
5271 means move IT backward to the preceding line start or BEGV. This
5272 function may move over more than DY pixels if IT->current_y - DY
5273 ends up in the middle of a line; in this case IT->current_y will be
5274 set to the top of the line moved to. */
5275
5276 void
5277 move_it_vertically_backward (it, dy)
5278 struct it *it;
5279 int dy;
5280 {
5281 int nlines, h, line_height;
5282 struct it it2;
5283 int start_pos = IT_CHARPOS (*it);
5284
5285 xassert (dy >= 0);
5286
5287 /* Estimate how many newlines we must move back. */
5288 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5289
5290 /* Set the iterator's position that many lines back. */
5291 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5292 back_to_previous_visible_line_start (it);
5293
5294 /* Reseat the iterator here. When moving backward, we don't want
5295 reseat to skip forward over invisible text, set up the iterator
5296 to deliver from overlay strings at the new position etc. So,
5297 use reseat_1 here. */
5298 reseat_1 (it, it->current.pos, 1);
5299
5300 /* We are now surely at a line start. */
5301 it->current_x = it->hpos = 0;
5302
5303 /* Move forward and see what y-distance we moved. First move to the
5304 start of the next line so that we get its height. We need this
5305 height to be able to tell whether we reached the specified
5306 y-distance. */
5307 it2 = *it;
5308 it2.max_ascent = it2.max_descent = 0;
5309 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5310 MOVE_TO_POS | MOVE_TO_VPOS);
5311 xassert (IT_CHARPOS (*it) >= BEGV);
5312 line_height = it2.max_ascent + it2.max_descent;
5313
5314 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5315 xassert (IT_CHARPOS (*it) >= BEGV);
5316 h = it2.current_y - it->current_y;
5317 nlines = it2.vpos - it->vpos;
5318
5319 /* Correct IT's y and vpos position. */
5320 it->vpos -= nlines;
5321 it->current_y -= h;
5322
5323 if (dy == 0)
5324 {
5325 /* DY == 0 means move to the start of the screen line. The
5326 value of nlines is > 0 if continuation lines were involved. */
5327 if (nlines > 0)
5328 move_it_by_lines (it, nlines, 1);
5329 xassert (IT_CHARPOS (*it) <= start_pos);
5330 }
5331 else if (nlines)
5332 {
5333 /* The y-position we try to reach. Note that h has been
5334 subtracted in front of the if-statement. */
5335 int target_y = it->current_y + h - dy;
5336
5337 /* If we did not reach target_y, try to move further backward if
5338 we can. If we moved too far backward, try to move forward. */
5339 if (target_y < it->current_y
5340 && IT_CHARPOS (*it) > BEGV)
5341 {
5342 move_it_vertically (it, target_y - it->current_y);
5343 xassert (IT_CHARPOS (*it) >= BEGV);
5344 }
5345 else if (target_y >= it->current_y + line_height
5346 && IT_CHARPOS (*it) < ZV)
5347 {
5348 move_it_vertically (it, target_y - (it->current_y + line_height));
5349 xassert (IT_CHARPOS (*it) >= BEGV);
5350 }
5351 }
5352 }
5353
5354
5355 /* Move IT by a specified amount of pixel lines DY. DY negative means
5356 move backwards. DY = 0 means move to start of screen line. At the
5357 end, IT will be on the start of a screen line. */
5358
5359 void
5360 move_it_vertically (it, dy)
5361 struct it *it;
5362 int dy;
5363 {
5364 if (dy <= 0)
5365 move_it_vertically_backward (it, -dy);
5366 else if (dy > 0)
5367 {
5368 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5369 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5370 MOVE_TO_POS | MOVE_TO_Y);
5371 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5372
5373 /* If buffer ends in ZV without a newline, move to the start of
5374 the line to satisfy the post-condition. */
5375 if (IT_CHARPOS (*it) == ZV
5376 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5377 move_it_by_lines (it, 0, 0);
5378 }
5379 }
5380
5381
5382 /* Move iterator IT past the end of the text line it is in. */
5383
5384 void
5385 move_it_past_eol (it)
5386 struct it *it;
5387 {
5388 enum move_it_result rc;
5389
5390 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5391 if (rc == MOVE_NEWLINE_OR_CR)
5392 set_iterator_to_next (it, 0);
5393 }
5394
5395
5396 #if 0 /* Currently not used. */
5397
5398 /* Return non-zero if some text between buffer positions START_CHARPOS
5399 and END_CHARPOS is invisible. IT->window is the window for text
5400 property lookup. */
5401
5402 static int
5403 invisible_text_between_p (it, start_charpos, end_charpos)
5404 struct it *it;
5405 int start_charpos, end_charpos;
5406 {
5407 Lisp_Object prop, limit;
5408 int invisible_found_p;
5409
5410 xassert (it != NULL && start_charpos <= end_charpos);
5411
5412 /* Is text at START invisible? */
5413 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5414 it->window);
5415 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5416 invisible_found_p = 1;
5417 else
5418 {
5419 limit = Fnext_single_char_property_change (make_number (start_charpos),
5420 Qinvisible, Qnil,
5421 make_number (end_charpos));
5422 invisible_found_p = XFASTINT (limit) < end_charpos;
5423 }
5424
5425 return invisible_found_p;
5426 }
5427
5428 #endif /* 0 */
5429
5430
5431 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5432 negative means move up. DVPOS == 0 means move to the start of the
5433 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5434 NEED_Y_P is zero, IT->current_y will be left unchanged.
5435
5436 Further optimization ideas: If we would know that IT->f doesn't use
5437 a face with proportional font, we could be faster for
5438 truncate-lines nil. */
5439
5440 void
5441 move_it_by_lines (it, dvpos, need_y_p)
5442 struct it *it;
5443 int dvpos, need_y_p;
5444 {
5445 struct position pos;
5446
5447 if (!FRAME_WINDOW_P (it->f))
5448 {
5449 struct text_pos textpos;
5450
5451 /* We can use vmotion on frames without proportional fonts. */
5452 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5453 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5454 reseat (it, textpos, 1);
5455 it->vpos += pos.vpos;
5456 it->current_y += pos.vpos;
5457 }
5458 else if (dvpos == 0)
5459 {
5460 /* DVPOS == 0 means move to the start of the screen line. */
5461 move_it_vertically_backward (it, 0);
5462 xassert (it->current_x == 0 && it->hpos == 0);
5463 }
5464 else if (dvpos > 0)
5465 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5466 else
5467 {
5468 struct it it2;
5469 int start_charpos, i;
5470
5471 /* Go back -DVPOS visible lines and reseat the iterator there. */
5472 start_charpos = IT_CHARPOS (*it);
5473 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5474 back_to_previous_visible_line_start (it);
5475 reseat (it, it->current.pos, 1);
5476 it->current_x = it->hpos = 0;
5477
5478 /* Above call may have moved too far if continuation lines
5479 are involved. Scan forward and see if it did. */
5480 it2 = *it;
5481 it2.vpos = it2.current_y = 0;
5482 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5483 it->vpos -= it2.vpos;
5484 it->current_y -= it2.current_y;
5485 it->current_x = it->hpos = 0;
5486
5487 /* If we moved too far, move IT some lines forward. */
5488 if (it2.vpos > -dvpos)
5489 {
5490 int delta = it2.vpos + dvpos;
5491 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5492 }
5493 }
5494 }
5495
5496
5497 \f
5498 /***********************************************************************
5499 Messages
5500 ***********************************************************************/
5501
5502
5503 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5504 to *Messages*. */
5505
5506 void
5507 add_to_log (format, arg1, arg2)
5508 char *format;
5509 Lisp_Object arg1, arg2;
5510 {
5511 Lisp_Object args[3];
5512 Lisp_Object msg, fmt;
5513 char *buffer;
5514 int len;
5515 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5516
5517 /* Do nothing if called asynchronously. Inserting text into
5518 a buffer may call after-change-functions and alike and
5519 that would means running Lisp asynchronously. */
5520 if (handling_signal)
5521 return;
5522
5523 fmt = msg = Qnil;
5524 GCPRO4 (fmt, msg, arg1, arg2);
5525
5526 args[0] = fmt = build_string (format);
5527 args[1] = arg1;
5528 args[2] = arg2;
5529 msg = Fformat (3, args);
5530
5531 len = STRING_BYTES (XSTRING (msg)) + 1;
5532 buffer = (char *) alloca (len);
5533 strcpy (buffer, XSTRING (msg)->data);
5534
5535 message_dolog (buffer, len - 1, 1, 0);
5536 UNGCPRO;
5537 }
5538
5539
5540 /* Output a newline in the *Messages* buffer if "needs" one. */
5541
5542 void
5543 message_log_maybe_newline ()
5544 {
5545 if (message_log_need_newline)
5546 message_dolog ("", 0, 1, 0);
5547 }
5548
5549
5550 /* Add a string M of length NBYTES to the message log, optionally
5551 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5552 nonzero, means interpret the contents of M as multibyte. This
5553 function calls low-level routines in order to bypass text property
5554 hooks, etc. which might not be safe to run. */
5555
5556 void
5557 message_dolog (m, nbytes, nlflag, multibyte)
5558 char *m;
5559 int nbytes, nlflag, multibyte;
5560 {
5561 if (!NILP (Vmessage_log_max))
5562 {
5563 struct buffer *oldbuf;
5564 Lisp_Object oldpoint, oldbegv, oldzv;
5565 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5566 int point_at_end = 0;
5567 int zv_at_end = 0;
5568 Lisp_Object old_deactivate_mark, tem;
5569 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5570
5571 old_deactivate_mark = Vdeactivate_mark;
5572 oldbuf = current_buffer;
5573 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5574 current_buffer->undo_list = Qt;
5575
5576 oldpoint = Fpoint_marker ();
5577 oldbegv = Fpoint_min_marker ();
5578 oldzv = Fpoint_max_marker ();
5579 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5580
5581 if (PT == Z)
5582 point_at_end = 1;
5583 if (ZV == Z)
5584 zv_at_end = 1;
5585
5586 BEGV = BEG;
5587 BEGV_BYTE = BEG_BYTE;
5588 ZV = Z;
5589 ZV_BYTE = Z_BYTE;
5590 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5591
5592 /* Insert the string--maybe converting multibyte to single byte
5593 or vice versa, so that all the text fits the buffer. */
5594 if (multibyte
5595 && NILP (current_buffer->enable_multibyte_characters))
5596 {
5597 int i, c, char_bytes;
5598 unsigned char work[1];
5599
5600 /* Convert a multibyte string to single-byte
5601 for the *Message* buffer. */
5602 for (i = 0; i < nbytes; i += nbytes)
5603 {
5604 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5605 work[0] = (SINGLE_BYTE_CHAR_P (c)
5606 ? c
5607 : multibyte_char_to_unibyte (c, Qnil));
5608 insert_1_both (work, 1, 1, 1, 0, 0);
5609 }
5610 }
5611 else if (! multibyte
5612 && ! NILP (current_buffer->enable_multibyte_characters))
5613 {
5614 int i, c, char_bytes;
5615 unsigned char *msg = (unsigned char *) m;
5616 unsigned char str[MAX_MULTIBYTE_LENGTH];
5617 /* Convert a single-byte string to multibyte
5618 for the *Message* buffer. */
5619 for (i = 0; i < nbytes; i++)
5620 {
5621 c = unibyte_char_to_multibyte (msg[i]);
5622 char_bytes = CHAR_STRING (c, str);
5623 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5624 }
5625 }
5626 else if (nbytes)
5627 insert_1 (m, nbytes, 1, 0, 0);
5628
5629 if (nlflag)
5630 {
5631 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5632 insert_1 ("\n", 1, 1, 0, 0);
5633
5634 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5635 this_bol = PT;
5636 this_bol_byte = PT_BYTE;
5637
5638 if (this_bol > BEG)
5639 {
5640 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5641 prev_bol = PT;
5642 prev_bol_byte = PT_BYTE;
5643
5644 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5645 this_bol, this_bol_byte);
5646 if (dup)
5647 {
5648 del_range_both (prev_bol, prev_bol_byte,
5649 this_bol, this_bol_byte, 0);
5650 if (dup > 1)
5651 {
5652 char dupstr[40];
5653 int duplen;
5654
5655 /* If you change this format, don't forget to also
5656 change message_log_check_duplicate. */
5657 sprintf (dupstr, " [%d times]", dup);
5658 duplen = strlen (dupstr);
5659 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5660 insert_1 (dupstr, duplen, 1, 0, 1);
5661 }
5662 }
5663 }
5664
5665 if (NATNUMP (Vmessage_log_max))
5666 {
5667 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5668 -XFASTINT (Vmessage_log_max) - 1, 0);
5669 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5670 }
5671 }
5672 BEGV = XMARKER (oldbegv)->charpos;
5673 BEGV_BYTE = marker_byte_position (oldbegv);
5674
5675 if (zv_at_end)
5676 {
5677 ZV = Z;
5678 ZV_BYTE = Z_BYTE;
5679 }
5680 else
5681 {
5682 ZV = XMARKER (oldzv)->charpos;
5683 ZV_BYTE = marker_byte_position (oldzv);
5684 }
5685
5686 if (point_at_end)
5687 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5688 else
5689 /* We can't do Fgoto_char (oldpoint) because it will run some
5690 Lisp code. */
5691 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5692 XMARKER (oldpoint)->bytepos);
5693
5694 UNGCPRO;
5695 free_marker (oldpoint);
5696 free_marker (oldbegv);
5697 free_marker (oldzv);
5698
5699 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5700 set_buffer_internal (oldbuf);
5701 if (NILP (tem))
5702 windows_or_buffers_changed = old_windows_or_buffers_changed;
5703 message_log_need_newline = !nlflag;
5704 Vdeactivate_mark = old_deactivate_mark;
5705 }
5706 }
5707
5708
5709 /* We are at the end of the buffer after just having inserted a newline.
5710 (Note: We depend on the fact we won't be crossing the gap.)
5711 Check to see if the most recent message looks a lot like the previous one.
5712 Return 0 if different, 1 if the new one should just replace it, or a
5713 value N > 1 if we should also append " [N times]". */
5714
5715 static int
5716 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5717 int prev_bol, this_bol;
5718 int prev_bol_byte, this_bol_byte;
5719 {
5720 int i;
5721 int len = Z_BYTE - 1 - this_bol_byte;
5722 int seen_dots = 0;
5723 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5724 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5725
5726 for (i = 0; i < len; i++)
5727 {
5728 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5729 seen_dots = 1;
5730 if (p1[i] != p2[i])
5731 return seen_dots;
5732 }
5733 p1 += len;
5734 if (*p1 == '\n')
5735 return 2;
5736 if (*p1++ == ' ' && *p1++ == '[')
5737 {
5738 int n = 0;
5739 while (*p1 >= '0' && *p1 <= '9')
5740 n = n * 10 + *p1++ - '0';
5741 if (strncmp (p1, " times]\n", 8) == 0)
5742 return n+1;
5743 }
5744 return 0;
5745 }
5746
5747
5748 /* Display an echo area message M with a specified length of NBYTES
5749 bytes. The string may include null characters. If M is 0, clear
5750 out any existing message, and let the mini-buffer text show
5751 through.
5752
5753 The buffer M must continue to exist until after the echo area gets
5754 cleared or some other message gets displayed there. This means do
5755 not pass text that is stored in a Lisp string; do not pass text in
5756 a buffer that was alloca'd. */
5757
5758 void
5759 message2 (m, nbytes, multibyte)
5760 char *m;
5761 int nbytes;
5762 int multibyte;
5763 {
5764 /* First flush out any partial line written with print. */
5765 message_log_maybe_newline ();
5766 if (m)
5767 message_dolog (m, nbytes, 1, multibyte);
5768 message2_nolog (m, nbytes, multibyte);
5769 }
5770
5771
5772 /* The non-logging counterpart of message2. */
5773
5774 void
5775 message2_nolog (m, nbytes, multibyte)
5776 char *m;
5777 int nbytes;
5778 {
5779 struct frame *sf = SELECTED_FRAME ();
5780 message_enable_multibyte = multibyte;
5781
5782 if (noninteractive)
5783 {
5784 if (noninteractive_need_newline)
5785 putc ('\n', stderr);
5786 noninteractive_need_newline = 0;
5787 if (m)
5788 fwrite (m, nbytes, 1, stderr);
5789 if (cursor_in_echo_area == 0)
5790 fprintf (stderr, "\n");
5791 fflush (stderr);
5792 }
5793 /* A null message buffer means that the frame hasn't really been
5794 initialized yet. Error messages get reported properly by
5795 cmd_error, so this must be just an informative message; toss it. */
5796 else if (INTERACTIVE
5797 && sf->glyphs_initialized_p
5798 && FRAME_MESSAGE_BUF (sf))
5799 {
5800 Lisp_Object mini_window;
5801 struct frame *f;
5802
5803 /* Get the frame containing the mini-buffer
5804 that the selected frame is using. */
5805 mini_window = FRAME_MINIBUF_WINDOW (sf);
5806 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5807
5808 FRAME_SAMPLE_VISIBILITY (f);
5809 if (FRAME_VISIBLE_P (sf)
5810 && ! FRAME_VISIBLE_P (f))
5811 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5812
5813 if (m)
5814 {
5815 set_message (m, Qnil, nbytes, multibyte);
5816 if (minibuffer_auto_raise)
5817 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5818 }
5819 else
5820 clear_message (1, 1);
5821
5822 do_pending_window_change (0);
5823 echo_area_display (1);
5824 do_pending_window_change (0);
5825 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5826 (*frame_up_to_date_hook) (f);
5827 }
5828 }
5829
5830
5831 /* Display an echo area message M with a specified length of NBYTES
5832 bytes. The string may include null characters. If M is not a
5833 string, clear out any existing message, and let the mini-buffer
5834 text show through. */
5835
5836 void
5837 message3 (m, nbytes, multibyte)
5838 Lisp_Object m;
5839 int nbytes;
5840 int multibyte;
5841 {
5842 struct gcpro gcpro1;
5843
5844 GCPRO1 (m);
5845
5846 /* First flush out any partial line written with print. */
5847 message_log_maybe_newline ();
5848 if (STRINGP (m))
5849 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5850 message3_nolog (m, nbytes, multibyte);
5851
5852 UNGCPRO;
5853 }
5854
5855
5856 /* The non-logging version of message3. */
5857
5858 void
5859 message3_nolog (m, nbytes, multibyte)
5860 Lisp_Object m;
5861 int nbytes, multibyte;
5862 {
5863 struct frame *sf = SELECTED_FRAME ();
5864 message_enable_multibyte = multibyte;
5865
5866 if (noninteractive)
5867 {
5868 if (noninteractive_need_newline)
5869 putc ('\n', stderr);
5870 noninteractive_need_newline = 0;
5871 if (STRINGP (m))
5872 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5873 if (cursor_in_echo_area == 0)
5874 fprintf (stderr, "\n");
5875 fflush (stderr);
5876 }
5877 /* A null message buffer means that the frame hasn't really been
5878 initialized yet. Error messages get reported properly by
5879 cmd_error, so this must be just an informative message; toss it. */
5880 else if (INTERACTIVE
5881 && sf->glyphs_initialized_p
5882 && FRAME_MESSAGE_BUF (sf))
5883 {
5884 Lisp_Object mini_window;
5885 Lisp_Object frame;
5886 struct frame *f;
5887
5888 /* Get the frame containing the mini-buffer
5889 that the selected frame is using. */
5890 mini_window = FRAME_MINIBUF_WINDOW (sf);
5891 frame = XWINDOW (mini_window)->frame;
5892 f = XFRAME (frame);
5893
5894 FRAME_SAMPLE_VISIBILITY (f);
5895 if (FRAME_VISIBLE_P (sf)
5896 && !FRAME_VISIBLE_P (f))
5897 Fmake_frame_visible (frame);
5898
5899 if (STRINGP (m) && XSTRING (m)->size)
5900 {
5901 set_message (NULL, m, nbytes, multibyte);
5902 if (minibuffer_auto_raise)
5903 Fraise_frame (frame);
5904 }
5905 else
5906 clear_message (1, 1);
5907
5908 do_pending_window_change (0);
5909 echo_area_display (1);
5910 do_pending_window_change (0);
5911 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5912 (*frame_up_to_date_hook) (f);
5913 }
5914 }
5915
5916
5917 /* Display a null-terminated echo area message M. If M is 0, clear
5918 out any existing message, and let the mini-buffer text show through.
5919
5920 The buffer M must continue to exist until after the echo area gets
5921 cleared or some other message gets displayed there. Do not pass
5922 text that is stored in a Lisp string. Do not pass text in a buffer
5923 that was alloca'd. */
5924
5925 void
5926 message1 (m)
5927 char *m;
5928 {
5929 message2 (m, (m ? strlen (m) : 0), 0);
5930 }
5931
5932
5933 /* The non-logging counterpart of message1. */
5934
5935 void
5936 message1_nolog (m)
5937 char *m;
5938 {
5939 message2_nolog (m, (m ? strlen (m) : 0), 0);
5940 }
5941
5942 /* Display a message M which contains a single %s
5943 which gets replaced with STRING. */
5944
5945 void
5946 message_with_string (m, string, log)
5947 char *m;
5948 Lisp_Object string;
5949 int log;
5950 {
5951 if (noninteractive)
5952 {
5953 if (m)
5954 {
5955 if (noninteractive_need_newline)
5956 putc ('\n', stderr);
5957 noninteractive_need_newline = 0;
5958 fprintf (stderr, m, XSTRING (string)->data);
5959 if (cursor_in_echo_area == 0)
5960 fprintf (stderr, "\n");
5961 fflush (stderr);
5962 }
5963 }
5964 else if (INTERACTIVE)
5965 {
5966 /* The frame whose minibuffer we're going to display the message on.
5967 It may be larger than the selected frame, so we need
5968 to use its buffer, not the selected frame's buffer. */
5969 Lisp_Object mini_window;
5970 struct frame *f, *sf = SELECTED_FRAME ();
5971
5972 /* Get the frame containing the minibuffer
5973 that the selected frame is using. */
5974 mini_window = FRAME_MINIBUF_WINDOW (sf);
5975 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5976
5977 /* A null message buffer means that the frame hasn't really been
5978 initialized yet. Error messages get reported properly by
5979 cmd_error, so this must be just an informative message; toss it. */
5980 if (FRAME_MESSAGE_BUF (f))
5981 {
5982 int len;
5983 char *a[1];
5984 a[0] = (char *) XSTRING (string)->data;
5985
5986 len = doprnt (FRAME_MESSAGE_BUF (f),
5987 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5988
5989 if (log)
5990 message2 (FRAME_MESSAGE_BUF (f), len,
5991 STRING_MULTIBYTE (string));
5992 else
5993 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5994 STRING_MULTIBYTE (string));
5995
5996 /* Print should start at the beginning of the message
5997 buffer next time. */
5998 message_buf_print = 0;
5999 }
6000 }
6001 }
6002
6003
6004 /* Dump an informative message to the minibuf. If M is 0, clear out
6005 any existing message, and let the mini-buffer text show through. */
6006
6007 /* VARARGS 1 */
6008 void
6009 message (m, a1, a2, a3)
6010 char *m;
6011 EMACS_INT a1, a2, a3;
6012 {
6013 if (noninteractive)
6014 {
6015 if (m)
6016 {
6017 if (noninteractive_need_newline)
6018 putc ('\n', stderr);
6019 noninteractive_need_newline = 0;
6020 fprintf (stderr, m, a1, a2, a3);
6021 if (cursor_in_echo_area == 0)
6022 fprintf (stderr, "\n");
6023 fflush (stderr);
6024 }
6025 }
6026 else if (INTERACTIVE)
6027 {
6028 /* The frame whose mini-buffer we're going to display the message
6029 on. It may be larger than the selected frame, so we need to
6030 use its buffer, not the selected frame's buffer. */
6031 Lisp_Object mini_window;
6032 struct frame *f, *sf = SELECTED_FRAME ();
6033
6034 /* Get the frame containing the mini-buffer
6035 that the selected frame is using. */
6036 mini_window = FRAME_MINIBUF_WINDOW (sf);
6037 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6038
6039 /* A null message buffer means that the frame hasn't really been
6040 initialized yet. Error messages get reported properly by
6041 cmd_error, so this must be just an informative message; toss
6042 it. */
6043 if (FRAME_MESSAGE_BUF (f))
6044 {
6045 if (m)
6046 {
6047 int len;
6048 #ifdef NO_ARG_ARRAY
6049 char *a[3];
6050 a[0] = (char *) a1;
6051 a[1] = (char *) a2;
6052 a[2] = (char *) a3;
6053
6054 len = doprnt (FRAME_MESSAGE_BUF (f),
6055 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6056 #else
6057 len = doprnt (FRAME_MESSAGE_BUF (f),
6058 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6059 (char **) &a1);
6060 #endif /* NO_ARG_ARRAY */
6061
6062 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6063 }
6064 else
6065 message1 (0);
6066
6067 /* Print should start at the beginning of the message
6068 buffer next time. */
6069 message_buf_print = 0;
6070 }
6071 }
6072 }
6073
6074
6075 /* The non-logging version of message. */
6076
6077 void
6078 message_nolog (m, a1, a2, a3)
6079 char *m;
6080 EMACS_INT a1, a2, a3;
6081 {
6082 Lisp_Object old_log_max;
6083 old_log_max = Vmessage_log_max;
6084 Vmessage_log_max = Qnil;
6085 message (m, a1, a2, a3);
6086 Vmessage_log_max = old_log_max;
6087 }
6088
6089
6090 /* Display the current message in the current mini-buffer. This is
6091 only called from error handlers in process.c, and is not time
6092 critical. */
6093
6094 void
6095 update_echo_area ()
6096 {
6097 if (!NILP (echo_area_buffer[0]))
6098 {
6099 Lisp_Object string;
6100 string = Fcurrent_message ();
6101 message3 (string, XSTRING (string)->size,
6102 !NILP (current_buffer->enable_multibyte_characters));
6103 }
6104 }
6105
6106
6107 /* Make sure echo area buffers in echo_buffers[] are life. If they
6108 aren't, make new ones. */
6109
6110 static void
6111 ensure_echo_area_buffers ()
6112 {
6113 int i;
6114
6115 for (i = 0; i < 2; ++i)
6116 if (!BUFFERP (echo_buffer[i])
6117 || NILP (XBUFFER (echo_buffer[i])->name))
6118 {
6119 char name[30];
6120 Lisp_Object old_buffer;
6121 int j;
6122
6123 old_buffer = echo_buffer[i];
6124 sprintf (name, " *Echo Area %d*", i);
6125 echo_buffer[i] = Fget_buffer_create (build_string (name));
6126 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6127
6128 for (j = 0; j < 2; ++j)
6129 if (EQ (old_buffer, echo_area_buffer[j]))
6130 echo_area_buffer[j] = echo_buffer[i];
6131 }
6132 }
6133
6134
6135 /* Call FN with args A1..A4 with either the current or last displayed
6136 echo_area_buffer as current buffer.
6137
6138 WHICH zero means use the current message buffer
6139 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6140 from echo_buffer[] and clear it.
6141
6142 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6143 suitable buffer from echo_buffer[] and clear it.
6144
6145 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6146 that the current message becomes the last displayed one, make
6147 choose a suitable buffer for echo_area_buffer[0], and clear it.
6148
6149 Value is what FN returns. */
6150
6151 static int
6152 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6153 struct window *w;
6154 int which;
6155 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6156 EMACS_INT a1;
6157 Lisp_Object a2;
6158 EMACS_INT a3, a4;
6159 {
6160 Lisp_Object buffer;
6161 int this_one, the_other, clear_buffer_p, rc;
6162 int count = BINDING_STACK_SIZE ();
6163
6164 /* If buffers aren't life, make new ones. */
6165 ensure_echo_area_buffers ();
6166
6167 clear_buffer_p = 0;
6168
6169 if (which == 0)
6170 this_one = 0, the_other = 1;
6171 else if (which > 0)
6172 this_one = 1, the_other = 0;
6173 else
6174 {
6175 this_one = 0, the_other = 1;
6176 clear_buffer_p = 1;
6177
6178 /* We need a fresh one in case the current echo buffer equals
6179 the one containing the last displayed echo area message. */
6180 if (!NILP (echo_area_buffer[this_one])
6181 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6182 echo_area_buffer[this_one] = Qnil;
6183 }
6184
6185 /* Choose a suitable buffer from echo_buffer[] is we don't
6186 have one. */
6187 if (NILP (echo_area_buffer[this_one]))
6188 {
6189 echo_area_buffer[this_one]
6190 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6191 ? echo_buffer[the_other]
6192 : echo_buffer[this_one]);
6193 clear_buffer_p = 1;
6194 }
6195
6196 buffer = echo_area_buffer[this_one];
6197
6198 /* Don't get confused by reusing the buffer used for echoing
6199 for a different purpose. */
6200 if (!echoing && EQ (buffer, echo_message_buffer))
6201 cancel_echoing ();
6202
6203 record_unwind_protect (unwind_with_echo_area_buffer,
6204 with_echo_area_buffer_unwind_data (w));
6205
6206 /* Make the echo area buffer current. Note that for display
6207 purposes, it is not necessary that the displayed window's buffer
6208 == current_buffer, except for text property lookup. So, let's
6209 only set that buffer temporarily here without doing a full
6210 Fset_window_buffer. We must also change w->pointm, though,
6211 because otherwise an assertions in unshow_buffer fails, and Emacs
6212 aborts. */
6213 set_buffer_internal_1 (XBUFFER (buffer));
6214 if (w)
6215 {
6216 w->buffer = buffer;
6217 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6218 }
6219
6220 current_buffer->undo_list = Qt;
6221 current_buffer->read_only = Qnil;
6222 specbind (Qinhibit_read_only, Qt);
6223
6224 if (clear_buffer_p && Z > BEG)
6225 del_range (BEG, Z);
6226
6227 xassert (BEGV >= BEG);
6228 xassert (ZV <= Z && ZV >= BEGV);
6229
6230 rc = fn (a1, a2, a3, a4);
6231
6232 xassert (BEGV >= BEG);
6233 xassert (ZV <= Z && ZV >= BEGV);
6234
6235 unbind_to (count, Qnil);
6236 return rc;
6237 }
6238
6239
6240 /* Save state that should be preserved around the call to the function
6241 FN called in with_echo_area_buffer. */
6242
6243 static Lisp_Object
6244 with_echo_area_buffer_unwind_data (w)
6245 struct window *w;
6246 {
6247 int i = 0;
6248 Lisp_Object vector;
6249
6250 /* Reduce consing by keeping one vector in
6251 Vwith_echo_area_save_vector. */
6252 vector = Vwith_echo_area_save_vector;
6253 Vwith_echo_area_save_vector = Qnil;
6254
6255 if (NILP (vector))
6256 vector = Fmake_vector (make_number (7), Qnil);
6257
6258 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6259 AREF (vector, i) = Vdeactivate_mark, ++i;
6260 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6261
6262 if (w)
6263 {
6264 XSETWINDOW (AREF (vector, i), w); ++i;
6265 AREF (vector, i) = w->buffer; ++i;
6266 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6267 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6268 }
6269 else
6270 {
6271 int end = i + 4;
6272 for (; i < end; ++i)
6273 AREF (vector, i) = Qnil;
6274 }
6275
6276 xassert (i == ASIZE (vector));
6277 return vector;
6278 }
6279
6280
6281 /* Restore global state from VECTOR which was created by
6282 with_echo_area_buffer_unwind_data. */
6283
6284 static Lisp_Object
6285 unwind_with_echo_area_buffer (vector)
6286 Lisp_Object vector;
6287 {
6288 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6289 Vdeactivate_mark = AREF (vector, 1);
6290 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6291
6292 if (WINDOWP (AREF (vector, 3)))
6293 {
6294 struct window *w;
6295 Lisp_Object buffer, charpos, bytepos;
6296
6297 w = XWINDOW (AREF (vector, 3));
6298 buffer = AREF (vector, 4);
6299 charpos = AREF (vector, 5);
6300 bytepos = AREF (vector, 6);
6301
6302 w->buffer = buffer;
6303 set_marker_both (w->pointm, buffer,
6304 XFASTINT (charpos), XFASTINT (bytepos));
6305 }
6306
6307 Vwith_echo_area_save_vector = vector;
6308 return Qnil;
6309 }
6310
6311
6312 /* Set up the echo area for use by print functions. MULTIBYTE_P
6313 non-zero means we will print multibyte. */
6314
6315 void
6316 setup_echo_area_for_printing (multibyte_p)
6317 int multibyte_p;
6318 {
6319 ensure_echo_area_buffers ();
6320
6321 if (!message_buf_print)
6322 {
6323 /* A message has been output since the last time we printed.
6324 Choose a fresh echo area buffer. */
6325 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6326 echo_area_buffer[0] = echo_buffer[1];
6327 else
6328 echo_area_buffer[0] = echo_buffer[0];
6329
6330 /* Switch to that buffer and clear it. */
6331 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6332 current_buffer->truncate_lines = Qnil;
6333
6334 if (Z > BEG)
6335 {
6336 int count = BINDING_STACK_SIZE ();
6337 specbind (Qinhibit_read_only, Qt);
6338 del_range (BEG, Z);
6339 unbind_to (count, Qnil);
6340 }
6341 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6342
6343 /* Set up the buffer for the multibyteness we need. */
6344 if (multibyte_p
6345 != !NILP (current_buffer->enable_multibyte_characters))
6346 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6347
6348 /* Raise the frame containing the echo area. */
6349 if (minibuffer_auto_raise)
6350 {
6351 struct frame *sf = SELECTED_FRAME ();
6352 Lisp_Object mini_window;
6353 mini_window = FRAME_MINIBUF_WINDOW (sf);
6354 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6355 }
6356
6357 message_log_maybe_newline ();
6358 message_buf_print = 1;
6359 }
6360 else
6361 {
6362 if (NILP (echo_area_buffer[0]))
6363 {
6364 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6365 echo_area_buffer[0] = echo_buffer[1];
6366 else
6367 echo_area_buffer[0] = echo_buffer[0];
6368 }
6369
6370 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6371 {
6372 /* Someone switched buffers between print requests. */
6373 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6374 current_buffer->truncate_lines = Qnil;
6375 }
6376 }
6377 }
6378
6379
6380 /* Display an echo area message in window W. Value is non-zero if W's
6381 height is changed. If display_last_displayed_message_p is
6382 non-zero, display the message that was last displayed, otherwise
6383 display the current message. */
6384
6385 static int
6386 display_echo_area (w)
6387 struct window *w;
6388 {
6389 int i, no_message_p, window_height_changed_p, count;
6390
6391 /* Temporarily disable garbage collections while displaying the echo
6392 area. This is done because a GC can print a message itself.
6393 That message would modify the echo area buffer's contents while a
6394 redisplay of the buffer is going on, and seriously confuse
6395 redisplay. */
6396 count = inhibit_garbage_collection ();
6397
6398 /* If there is no message, we must call display_echo_area_1
6399 nevertheless because it resizes the window. But we will have to
6400 reset the echo_area_buffer in question to nil at the end because
6401 with_echo_area_buffer will sets it to an empty buffer. */
6402 i = display_last_displayed_message_p ? 1 : 0;
6403 no_message_p = NILP (echo_area_buffer[i]);
6404
6405 window_height_changed_p
6406 = with_echo_area_buffer (w, display_last_displayed_message_p,
6407 display_echo_area_1,
6408 (EMACS_INT) w, Qnil, 0, 0);
6409
6410 if (no_message_p)
6411 echo_area_buffer[i] = Qnil;
6412
6413 unbind_to (count, Qnil);
6414 return window_height_changed_p;
6415 }
6416
6417
6418 /* Helper for display_echo_area. Display the current buffer which
6419 contains the current echo area message in window W, a mini-window,
6420 a pointer to which is passed in A1. A2..A4 are currently not used.
6421 Change the height of W so that all of the message is displayed.
6422 Value is non-zero if height of W was changed. */
6423
6424 static int
6425 display_echo_area_1 (a1, a2, a3, a4)
6426 EMACS_INT a1;
6427 Lisp_Object a2;
6428 EMACS_INT a3, a4;
6429 {
6430 struct window *w = (struct window *) a1;
6431 Lisp_Object window;
6432 struct text_pos start;
6433 int window_height_changed_p = 0;
6434
6435 /* Do this before displaying, so that we have a large enough glyph
6436 matrix for the display. */
6437 window_height_changed_p = resize_mini_window (w, 0);
6438
6439 /* Display. */
6440 clear_glyph_matrix (w->desired_matrix);
6441 XSETWINDOW (window, w);
6442 SET_TEXT_POS (start, BEG, BEG_BYTE);
6443 try_window (window, start);
6444
6445 return window_height_changed_p;
6446 }
6447
6448
6449 /* Resize the echo area window to exactly the size needed for the
6450 currently displayed message, if there is one. */
6451
6452 void
6453 resize_echo_area_axactly ()
6454 {
6455 if (BUFFERP (echo_area_buffer[0])
6456 && WINDOWP (echo_area_window))
6457 {
6458 struct window *w = XWINDOW (echo_area_window);
6459 int resized_p;
6460
6461 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6462 (EMACS_INT) w, Qnil, 0, 0);
6463 if (resized_p)
6464 {
6465 ++windows_or_buffers_changed;
6466 ++update_mode_lines;
6467 redisplay_internal (0);
6468 }
6469 }
6470 }
6471
6472
6473 /* Callback function for with_echo_area_buffer, when used from
6474 resize_echo_area_axactly. A1 contains a pointer to the window to
6475 resize, A2 to A4 are not used. Value is what resize_mini_window
6476 returns. */
6477
6478 static int
6479 resize_mini_window_1 (a1, a2, a3, a4)
6480 EMACS_INT a1;
6481 Lisp_Object a2;
6482 EMACS_INT a3, a4;
6483 {
6484 return resize_mini_window ((struct window *) a1, 1);
6485 }
6486
6487
6488 /* Resize mini-window W to fit the size of its contents. EXACT:P
6489 means size the window exactly to the size needed. Otherwise, it's
6490 only enlarged until W's buffer is empty. Value is non-zero if
6491 the window height has been changed. */
6492
6493 int
6494 resize_mini_window (w, exact_p)
6495 struct window *w;
6496 int exact_p;
6497 {
6498 struct frame *f = XFRAME (w->frame);
6499 int window_height_changed_p = 0;
6500
6501 xassert (MINI_WINDOW_P (w));
6502
6503 /* Nil means don't try to resize. */
6504 if (NILP (Vresize_mini_windows)
6505 || (FRAME_X_P (f) && f->output_data.x == NULL))
6506 return 0;
6507
6508 if (!FRAME_MINIBUF_ONLY_P (f))
6509 {
6510 struct it it;
6511 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6512 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6513 int height, max_height;
6514 int unit = CANON_Y_UNIT (f);
6515 struct text_pos start;
6516 struct buffer *old_current_buffer = NULL;
6517
6518 if (current_buffer != XBUFFER (w->buffer))
6519 {
6520 old_current_buffer = current_buffer;
6521 set_buffer_internal (XBUFFER (w->buffer));
6522 }
6523
6524 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6525
6526 /* Compute the max. number of lines specified by the user. */
6527 if (FLOATP (Vmax_mini_window_height))
6528 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
6529 else if (INTEGERP (Vmax_mini_window_height))
6530 max_height = XINT (Vmax_mini_window_height);
6531 else
6532 max_height = total_height / 4;
6533
6534 /* Correct that max. height if it's bogus. */
6535 max_height = max (1, max_height);
6536 max_height = min (total_height, max_height);
6537
6538 /* Find out the height of the text in the window. */
6539 if (it.truncate_lines_p)
6540 height = 1;
6541 else
6542 {
6543 last_height = 0;
6544 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6545 if (it.max_ascent == 0 && it.max_descent == 0)
6546 height = it.current_y + last_height;
6547 else
6548 height = it.current_y + it.max_ascent + it.max_descent;
6549 height -= it.extra_line_spacing;
6550 height = (height + unit - 1) / unit;
6551 }
6552
6553 /* Compute a suitable window start. */
6554 if (height > max_height)
6555 {
6556 height = max_height;
6557 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6558 move_it_vertically_backward (&it, (height - 1) * unit);
6559 start = it.current.pos;
6560 }
6561 else
6562 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6563 SET_MARKER_FROM_TEXT_POS (w->start, start);
6564
6565 if (EQ (Vresize_mini_windows, Qgrow_only))
6566 {
6567 /* Let it grow only, until we display an empty message, in which
6568 case the window shrinks again. */
6569 if (height > XFASTINT (w->height))
6570 {
6571 int old_height = XFASTINT (w->height);
6572 freeze_window_starts (f, 1);
6573 grow_mini_window (w, height - XFASTINT (w->height));
6574 window_height_changed_p = XFASTINT (w->height) != old_height;
6575 }
6576 else if (height < XFASTINT (w->height)
6577 && (exact_p || BEGV == ZV))
6578 {
6579 int old_height = XFASTINT (w->height);
6580 freeze_window_starts (f, 0);
6581 shrink_mini_window (w);
6582 window_height_changed_p = XFASTINT (w->height) != old_height;
6583 }
6584 }
6585 else
6586 {
6587 /* Always resize to exact size needed. */
6588 if (height > XFASTINT (w->height))
6589 {
6590 int old_height = XFASTINT (w->height);
6591 freeze_window_starts (f, 1);
6592 grow_mini_window (w, height - XFASTINT (w->height));
6593 window_height_changed_p = XFASTINT (w->height) != old_height;
6594 }
6595 else if (height < XFASTINT (w->height))
6596 {
6597 int old_height = XFASTINT (w->height);
6598 freeze_window_starts (f, 0);
6599 shrink_mini_window (w);
6600
6601 if (height)
6602 {
6603 freeze_window_starts (f, 1);
6604 grow_mini_window (w, height - XFASTINT (w->height));
6605 }
6606
6607 window_height_changed_p = XFASTINT (w->height) != old_height;
6608 }
6609 }
6610
6611 if (old_current_buffer)
6612 set_buffer_internal (old_current_buffer);
6613 }
6614
6615 return window_height_changed_p;
6616 }
6617
6618
6619 /* Value is the current message, a string, or nil if there is no
6620 current message. */
6621
6622 Lisp_Object
6623 current_message ()
6624 {
6625 Lisp_Object msg;
6626
6627 if (NILP (echo_area_buffer[0]))
6628 msg = Qnil;
6629 else
6630 {
6631 with_echo_area_buffer (0, 0, current_message_1,
6632 (EMACS_INT) &msg, Qnil, 0, 0);
6633 if (NILP (msg))
6634 echo_area_buffer[0] = Qnil;
6635 }
6636
6637 return msg;
6638 }
6639
6640
6641 static int
6642 current_message_1 (a1, a2, a3, a4)
6643 EMACS_INT a1;
6644 Lisp_Object a2;
6645 EMACS_INT a3, a4;
6646 {
6647 Lisp_Object *msg = (Lisp_Object *) a1;
6648
6649 if (Z > BEG)
6650 *msg = make_buffer_string (BEG, Z, 1);
6651 else
6652 *msg = Qnil;
6653 return 0;
6654 }
6655
6656
6657 /* Push the current message on Vmessage_stack for later restauration
6658 by restore_message. Value is non-zero if the current message isn't
6659 empty. This is a relatively infrequent operation, so it's not
6660 worth optimizing. */
6661
6662 int
6663 push_message ()
6664 {
6665 Lisp_Object msg;
6666 msg = current_message ();
6667 Vmessage_stack = Fcons (msg, Vmessage_stack);
6668 return STRINGP (msg);
6669 }
6670
6671
6672 /* Handler for record_unwind_protect calling pop_message. */
6673
6674 Lisp_Object
6675 push_message_unwind (dummy)
6676 Lisp_Object dummy;
6677 {
6678 pop_message ();
6679 return Qnil;
6680 }
6681
6682
6683 /* Restore message display from the top of Vmessage_stack. */
6684
6685 void
6686 restore_message ()
6687 {
6688 Lisp_Object msg;
6689
6690 xassert (CONSP (Vmessage_stack));
6691 msg = XCAR (Vmessage_stack);
6692 if (STRINGP (msg))
6693 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6694 else
6695 message3_nolog (msg, 0, 0);
6696 }
6697
6698
6699 /* Pop the top-most entry off Vmessage_stack. */
6700
6701 void
6702 pop_message ()
6703 {
6704 xassert (CONSP (Vmessage_stack));
6705 Vmessage_stack = XCDR (Vmessage_stack);
6706 }
6707
6708
6709 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6710 exits. If the stack is not empty, we have a missing pop_message
6711 somewhere. */
6712
6713 void
6714 check_message_stack ()
6715 {
6716 if (!NILP (Vmessage_stack))
6717 abort ();
6718 }
6719
6720
6721 /* Truncate to NCHARS what will be displayed in the echo area the next
6722 time we display it---but don't redisplay it now. */
6723
6724 void
6725 truncate_echo_area (nchars)
6726 int nchars;
6727 {
6728 if (nchars == 0)
6729 echo_area_buffer[0] = Qnil;
6730 /* A null message buffer means that the frame hasn't really been
6731 initialized yet. Error messages get reported properly by
6732 cmd_error, so this must be just an informative message; toss it. */
6733 else if (!noninteractive
6734 && INTERACTIVE
6735 && !NILP (echo_area_buffer[0]))
6736 {
6737 struct frame *sf = SELECTED_FRAME ();
6738 if (FRAME_MESSAGE_BUF (sf))
6739 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6740 }
6741 }
6742
6743
6744 /* Helper function for truncate_echo_area. Truncate the current
6745 message to at most NCHARS characters. */
6746
6747 static int
6748 truncate_message_1 (nchars, a2, a3, a4)
6749 EMACS_INT nchars;
6750 Lisp_Object a2;
6751 EMACS_INT a3, a4;
6752 {
6753 if (BEG + nchars < Z)
6754 del_range (BEG + nchars, Z);
6755 if (Z == BEG)
6756 echo_area_buffer[0] = Qnil;
6757 return 0;
6758 }
6759
6760
6761 /* Set the current message to a substring of S or STRING.
6762
6763 If STRING is a Lisp string, set the message to the first NBYTES
6764 bytes from STRING. NBYTES zero means use the whole string. If
6765 STRING is multibyte, the message will be displayed multibyte.
6766
6767 If S is not null, set the message to the first LEN bytes of S. LEN
6768 zero means use the whole string. MULTIBYTE_P non-zero means S is
6769 multibyte. Display the message multibyte in that case. */
6770
6771 void
6772 set_message (s, string, nbytes, multibyte_p)
6773 char *s;
6774 Lisp_Object string;
6775 int nbytes;
6776 {
6777 message_enable_multibyte
6778 = ((s && multibyte_p)
6779 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6780
6781 with_echo_area_buffer (0, -1, set_message_1,
6782 (EMACS_INT) s, string, nbytes, multibyte_p);
6783 message_buf_print = 0;
6784 help_echo_showing_p = 0;
6785 }
6786
6787
6788 /* Helper function for set_message. Arguments have the same meaning
6789 as there, with A1 corresponding to S and A2 corresponding to STRING
6790 This function is called with the echo area buffer being
6791 current. */
6792
6793 static int
6794 set_message_1 (a1, a2, nbytes, multibyte_p)
6795 EMACS_INT a1;
6796 Lisp_Object a2;
6797 EMACS_INT nbytes, multibyte_p;
6798 {
6799 char *s = (char *) a1;
6800 Lisp_Object string = a2;
6801
6802 xassert (BEG == Z);
6803
6804 /* Change multibyteness of the echo buffer appropriately. */
6805 if (message_enable_multibyte
6806 != !NILP (current_buffer->enable_multibyte_characters))
6807 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6808
6809 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6810
6811 /* Insert new message at BEG. */
6812 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6813
6814 if (STRINGP (string))
6815 {
6816 int nchars;
6817
6818 if (nbytes == 0)
6819 nbytes = XSTRING (string)->size_byte;
6820 nchars = string_byte_to_char (string, nbytes);
6821
6822 /* This function takes care of single/multibyte conversion. We
6823 just have to ensure that the echo area buffer has the right
6824 setting of enable_multibyte_characters. */
6825 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6826 }
6827 else if (s)
6828 {
6829 if (nbytes == 0)
6830 nbytes = strlen (s);
6831
6832 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6833 {
6834 /* Convert from multi-byte to single-byte. */
6835 int i, c, n;
6836 unsigned char work[1];
6837
6838 /* Convert a multibyte string to single-byte. */
6839 for (i = 0; i < nbytes; i += n)
6840 {
6841 c = string_char_and_length (s + i, nbytes - i, &n);
6842 work[0] = (SINGLE_BYTE_CHAR_P (c)
6843 ? c
6844 : multibyte_char_to_unibyte (c, Qnil));
6845 insert_1_both (work, 1, 1, 1, 0, 0);
6846 }
6847 }
6848 else if (!multibyte_p
6849 && !NILP (current_buffer->enable_multibyte_characters))
6850 {
6851 /* Convert from single-byte to multi-byte. */
6852 int i, c, n;
6853 unsigned char *msg = (unsigned char *) s;
6854 unsigned char str[MAX_MULTIBYTE_LENGTH];
6855
6856 /* Convert a single-byte string to multibyte. */
6857 for (i = 0; i < nbytes; i++)
6858 {
6859 c = unibyte_char_to_multibyte (msg[i]);
6860 n = CHAR_STRING (c, str);
6861 insert_1_both (str, 1, n, 1, 0, 0);
6862 }
6863 }
6864 else
6865 insert_1 (s, nbytes, 1, 0, 0);
6866 }
6867
6868 return 0;
6869 }
6870
6871
6872 /* Clear messages. CURRENT_P non-zero means clear the current
6873 message. LAST_DISPLAYED_P non-zero means clear the message
6874 last displayed. */
6875
6876 void
6877 clear_message (current_p, last_displayed_p)
6878 int current_p, last_displayed_p;
6879 {
6880 if (current_p)
6881 echo_area_buffer[0] = Qnil;
6882
6883 if (last_displayed_p)
6884 echo_area_buffer[1] = Qnil;
6885
6886 message_buf_print = 0;
6887 }
6888
6889 /* Clear garbaged frames.
6890
6891 This function is used where the old redisplay called
6892 redraw_garbaged_frames which in turn called redraw_frame which in
6893 turn called clear_frame. The call to clear_frame was a source of
6894 flickering. I believe a clear_frame is not necessary. It should
6895 suffice in the new redisplay to invalidate all current matrices,
6896 and ensure a complete redisplay of all windows. */
6897
6898 static void
6899 clear_garbaged_frames ()
6900 {
6901 if (frame_garbaged)
6902 {
6903 Lisp_Object tail, frame;
6904
6905 FOR_EACH_FRAME (tail, frame)
6906 {
6907 struct frame *f = XFRAME (frame);
6908
6909 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6910 {
6911 clear_current_matrices (f);
6912 f->garbaged = 0;
6913 }
6914 }
6915
6916 frame_garbaged = 0;
6917 ++windows_or_buffers_changed;
6918 }
6919 }
6920
6921
6922 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6923 is non-zero update selected_frame. Value is non-zero if the
6924 mini-windows height has been changed. */
6925
6926 static int
6927 echo_area_display (update_frame_p)
6928 int update_frame_p;
6929 {
6930 Lisp_Object mini_window;
6931 struct window *w;
6932 struct frame *f;
6933 int window_height_changed_p = 0;
6934 struct frame *sf = SELECTED_FRAME ();
6935
6936 mini_window = FRAME_MINIBUF_WINDOW (sf);
6937 w = XWINDOW (mini_window);
6938 f = XFRAME (WINDOW_FRAME (w));
6939
6940 /* Don't display if frame is invisible or not yet initialized. */
6941 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6942 return 0;
6943
6944 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
6945 #ifndef macintosh
6946 #ifdef HAVE_WINDOW_SYSTEM
6947 /* When Emacs starts, selected_frame may be a visible terminal
6948 frame, even if we run under a window system. If we let this
6949 through, a message would be displayed on the terminal. */
6950 if (EQ (selected_frame, Vterminal_frame)
6951 && !NILP (Vwindow_system))
6952 return 0;
6953 #endif /* HAVE_WINDOW_SYSTEM */
6954 #endif
6955
6956 /* Redraw garbaged frames. */
6957 if (frame_garbaged)
6958 clear_garbaged_frames ();
6959
6960 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6961 {
6962 echo_area_window = mini_window;
6963 window_height_changed_p = display_echo_area (w);
6964 w->must_be_updated_p = 1;
6965
6966 /* Update the display, unless called from redisplay_internal.
6967 Also don't update the screen during redisplay itself. The
6968 update will happen at the end of redisplay, and an update
6969 here could cause confusion. */
6970 if (update_frame_p && !redisplaying_p)
6971 {
6972 int n = 0;
6973
6974 /* If the display update has been interrupted by pending
6975 input, update mode lines in the frame. Due to the
6976 pending input, it might have been that redisplay hasn't
6977 been called, so that mode lines above the echo area are
6978 garbaged. This looks odd, so we prevent it here. */
6979 if (!display_completed)
6980 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6981
6982 if (window_height_changed_p
6983 /* Don't do this if Emacs is shutting down. Redisplay
6984 needs to run hooks. */
6985 && !NILP (Vrun_hooks))
6986 {
6987 /* Must update other windows. Likewise as in other
6988 cases, don't let this update be interrupted by
6989 pending input. */
6990 int count = BINDING_STACK_SIZE ();
6991 specbind (Qredisplay_dont_pause, Qt);
6992 windows_or_buffers_changed = 1;
6993 redisplay_internal (0);
6994 unbind_to (count, Qnil);
6995 }
6996 else if (FRAME_WINDOW_P (f) && n == 0)
6997 {
6998 /* Window configuration is the same as before.
6999 Can do with a display update of the echo area,
7000 unless we displayed some mode lines. */
7001 update_single_window (w, 1);
7002 rif->flush_display (f);
7003 }
7004 else
7005 update_frame (f, 1, 1);
7006
7007 /* If cursor is in the echo area, make sure that the next
7008 redisplay displays the minibuffer, so that the cursor will
7009 be replaced with what the minibuffer wants. */
7010 if (cursor_in_echo_area)
7011 ++windows_or_buffers_changed;
7012 }
7013 }
7014 else if (!EQ (mini_window, selected_window))
7015 windows_or_buffers_changed++;
7016
7017 /* Last displayed message is now the current message. */
7018 echo_area_buffer[1] = echo_area_buffer[0];
7019
7020 /* Prevent redisplay optimization in redisplay_internal by resetting
7021 this_line_start_pos. This is done because the mini-buffer now
7022 displays the message instead of its buffer text. */
7023 if (EQ (mini_window, selected_window))
7024 CHARPOS (this_line_start_pos) = 0;
7025
7026 return window_height_changed_p;
7027 }
7028
7029
7030 \f
7031 /***********************************************************************
7032 Frame Titles
7033 ***********************************************************************/
7034
7035
7036 #ifdef HAVE_WINDOW_SYSTEM
7037
7038 /* A buffer for constructing frame titles in it; allocated from the
7039 heap in init_xdisp and resized as needed in store_frame_title_char. */
7040
7041 static char *frame_title_buf;
7042
7043 /* The buffer's end, and a current output position in it. */
7044
7045 static char *frame_title_buf_end;
7046 static char *frame_title_ptr;
7047
7048
7049 /* Store a single character C for the frame title in frame_title_buf.
7050 Re-allocate frame_title_buf if necessary. */
7051
7052 static void
7053 store_frame_title_char (c)
7054 char c;
7055 {
7056 /* If output position has reached the end of the allocated buffer,
7057 double the buffer's size. */
7058 if (frame_title_ptr == frame_title_buf_end)
7059 {
7060 int len = frame_title_ptr - frame_title_buf;
7061 int new_size = 2 * len * sizeof *frame_title_buf;
7062 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7063 frame_title_buf_end = frame_title_buf + new_size;
7064 frame_title_ptr = frame_title_buf + len;
7065 }
7066
7067 *frame_title_ptr++ = c;
7068 }
7069
7070
7071 /* Store part of a frame title in frame_title_buf, beginning at
7072 frame_title_ptr. STR is the string to store. Do not copy
7073 characters that yield more columns than PRECISION; PRECISION <= 0
7074 means copy the whole string. Pad with spaces until FIELD_WIDTH
7075 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7076 pad. Called from display_mode_element when it is used to build a
7077 frame title. */
7078
7079 static int
7080 store_frame_title (str, field_width, precision)
7081 unsigned char *str;
7082 int field_width, precision;
7083 {
7084 int n = 0;
7085 int dummy, nbytes, width;
7086
7087 /* Copy at most PRECISION chars from STR. */
7088 nbytes = strlen (str);
7089 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7090 while (nbytes--)
7091 store_frame_title_char (*str++);
7092
7093 /* Fill up with spaces until FIELD_WIDTH reached. */
7094 while (field_width > 0
7095 && n < field_width)
7096 {
7097 store_frame_title_char (' ');
7098 ++n;
7099 }
7100
7101 return n;
7102 }
7103
7104
7105 /* Set the title of FRAME, if it has changed. The title format is
7106 Vicon_title_format if FRAME is iconified, otherwise it is
7107 frame_title_format. */
7108
7109 static void
7110 x_consider_frame_title (frame)
7111 Lisp_Object frame;
7112 {
7113 struct frame *f = XFRAME (frame);
7114
7115 if (FRAME_WINDOW_P (f)
7116 || FRAME_MINIBUF_ONLY_P (f)
7117 || f->explicit_name)
7118 {
7119 /* Do we have more than one visible frame on this X display? */
7120 Lisp_Object tail;
7121 Lisp_Object fmt;
7122 struct buffer *obuf;
7123 int len;
7124 struct it it;
7125
7126 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7127 {
7128 struct frame *tf = XFRAME (XCAR (tail));
7129
7130 if (tf != f
7131 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7132 && !FRAME_MINIBUF_ONLY_P (tf)
7133 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7134 break;
7135 }
7136
7137 /* Set global variable indicating that multiple frames exist. */
7138 multiple_frames = CONSP (tail);
7139
7140 /* Switch to the buffer of selected window of the frame. Set up
7141 frame_title_ptr so that display_mode_element will output into it;
7142 then display the title. */
7143 obuf = current_buffer;
7144 Fset_buffer (XWINDOW (f->selected_window)->buffer);
7145 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7146 frame_title_ptr = frame_title_buf;
7147 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7148 NULL, DEFAULT_FACE_ID);
7149 display_mode_element (&it, 0, -1, -1, fmt);
7150 len = frame_title_ptr - frame_title_buf;
7151 frame_title_ptr = NULL;
7152 set_buffer_internal (obuf);
7153
7154 /* Set the title only if it's changed. This avoids consing in
7155 the common case where it hasn't. (If it turns out that we've
7156 already wasted too much time by walking through the list with
7157 display_mode_element, then we might need to optimize at a
7158 higher level than this.) */
7159 if (! STRINGP (f->name)
7160 || STRING_BYTES (XSTRING (f->name)) != len
7161 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7162 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7163 }
7164 }
7165
7166 #else /* not HAVE_WINDOW_SYSTEM */
7167
7168 #define frame_title_ptr ((char *)0)
7169 #define store_frame_title(str, mincol, maxcol) 0
7170
7171 #endif /* not HAVE_WINDOW_SYSTEM */
7172
7173
7174
7175 \f
7176 /***********************************************************************
7177 Menu Bars
7178 ***********************************************************************/
7179
7180
7181 /* Prepare for redisplay by updating menu-bar item lists when
7182 appropriate. This can call eval. */
7183
7184 void
7185 prepare_menu_bars ()
7186 {
7187 int all_windows;
7188 struct gcpro gcpro1, gcpro2;
7189 struct frame *f;
7190 Lisp_Object tooltip_frame;
7191
7192 #ifdef HAVE_X_WINDOWS
7193 tooltip_frame = tip_frame;
7194 #else
7195 tooltip_frame = Qnil;
7196 #endif
7197
7198 /* Update all frame titles based on their buffer names, etc. We do
7199 this before the menu bars so that the buffer-menu will show the
7200 up-to-date frame titles. */
7201 #ifdef HAVE_WINDOW_SYSTEM
7202 if (windows_or_buffers_changed || update_mode_lines)
7203 {
7204 Lisp_Object tail, frame;
7205
7206 FOR_EACH_FRAME (tail, frame)
7207 {
7208 f = XFRAME (frame);
7209 if (!EQ (frame, tooltip_frame)
7210 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7211 x_consider_frame_title (frame);
7212 }
7213 }
7214 #endif /* HAVE_WINDOW_SYSTEM */
7215
7216 /* Update the menu bar item lists, if appropriate. This has to be
7217 done before any actual redisplay or generation of display lines. */
7218 all_windows = (update_mode_lines
7219 || buffer_shared > 1
7220 || windows_or_buffers_changed);
7221 if (all_windows)
7222 {
7223 Lisp_Object tail, frame;
7224 int count = BINDING_STACK_SIZE ();
7225
7226 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7227
7228 FOR_EACH_FRAME (tail, frame)
7229 {
7230 f = XFRAME (frame);
7231
7232 /* Ignore tooltip frame. */
7233 if (EQ (frame, tooltip_frame))
7234 continue;
7235
7236 /* If a window on this frame changed size, report that to
7237 the user and clear the size-change flag. */
7238 if (FRAME_WINDOW_SIZES_CHANGED (f))
7239 {
7240 Lisp_Object functions;
7241
7242 /* Clear flag first in case we get an error below. */
7243 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7244 functions = Vwindow_size_change_functions;
7245 GCPRO2 (tail, functions);
7246
7247 while (CONSP (functions))
7248 {
7249 call1 (XCAR (functions), frame);
7250 functions = XCDR (functions);
7251 }
7252 UNGCPRO;
7253 }
7254
7255 GCPRO1 (tail);
7256 update_menu_bar (f, 0);
7257 #ifdef HAVE_WINDOW_SYSTEM
7258 update_tool_bar (f, 0);
7259 #endif
7260 UNGCPRO;
7261 }
7262
7263 unbind_to (count, Qnil);
7264 }
7265 else
7266 {
7267 struct frame *sf = SELECTED_FRAME ();
7268 update_menu_bar (sf, 1);
7269 #ifdef HAVE_WINDOW_SYSTEM
7270 update_tool_bar (sf, 1);
7271 #endif
7272 }
7273
7274 /* Motif needs this. See comment in xmenu.c. Turn it off when
7275 pending_menu_activation is not defined. */
7276 #ifdef USE_X_TOOLKIT
7277 pending_menu_activation = 0;
7278 #endif
7279 }
7280
7281
7282 /* Update the menu bar item list for frame F. This has to be done
7283 before we start to fill in any display lines, because it can call
7284 eval.
7285
7286 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7287
7288 static void
7289 update_menu_bar (f, save_match_data)
7290 struct frame *f;
7291 int save_match_data;
7292 {
7293 Lisp_Object window;
7294 register struct window *w;
7295
7296 /* If called recursively during a menu update, do nothing. This can
7297 happen when, for instance, an activate-menubar-hook causes a
7298 redisplay. */
7299 if (inhibit_menubar_update)
7300 return;
7301
7302 window = FRAME_SELECTED_WINDOW (f);
7303 w = XWINDOW (window);
7304
7305 if (update_mode_lines)
7306 w->update_mode_line = Qt;
7307
7308 if (FRAME_WINDOW_P (f)
7309 ?
7310 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7311 FRAME_EXTERNAL_MENU_BAR (f)
7312 #else
7313 FRAME_MENU_BAR_LINES (f) > 0
7314 #endif
7315 : FRAME_MENU_BAR_LINES (f) > 0)
7316 {
7317 /* If the user has switched buffers or windows, we need to
7318 recompute to reflect the new bindings. But we'll
7319 recompute when update_mode_lines is set too; that means
7320 that people can use force-mode-line-update to request
7321 that the menu bar be recomputed. The adverse effect on
7322 the rest of the redisplay algorithm is about the same as
7323 windows_or_buffers_changed anyway. */
7324 if (windows_or_buffers_changed
7325 || !NILP (w->update_mode_line)
7326 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7327 < BUF_MODIFF (XBUFFER (w->buffer)))
7328 != !NILP (w->last_had_star))
7329 || ((!NILP (Vtransient_mark_mode)
7330 && !NILP (XBUFFER (w->buffer)->mark_active))
7331 != !NILP (w->region_showing)))
7332 {
7333 struct buffer *prev = current_buffer;
7334 int count = BINDING_STACK_SIZE ();
7335
7336 specbind (Qinhibit_menubar_update, Qt);
7337
7338 set_buffer_internal_1 (XBUFFER (w->buffer));
7339 if (save_match_data)
7340 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7341 if (NILP (Voverriding_local_map_menu_flag))
7342 {
7343 specbind (Qoverriding_terminal_local_map, Qnil);
7344 specbind (Qoverriding_local_map, Qnil);
7345 }
7346
7347 /* Run the Lucid hook. */
7348 safe_run_hooks (Qactivate_menubar_hook);
7349
7350 /* If it has changed current-menubar from previous value,
7351 really recompute the menu-bar from the value. */
7352 if (! NILP (Vlucid_menu_bar_dirty_flag))
7353 call0 (Qrecompute_lucid_menubar);
7354
7355 safe_run_hooks (Qmenu_bar_update_hook);
7356 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7357
7358 /* Redisplay the menu bar in case we changed it. */
7359 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7360 if (FRAME_WINDOW_P (f)
7361 #if defined (macintosh)
7362 /* All frames on Mac OS share the same menubar. So only the
7363 selected frame should be allowed to set it. */
7364 && f == SELECTED_FRAME ()
7365 #endif
7366 )
7367 set_frame_menubar (f, 0, 0);
7368 else
7369 /* On a terminal screen, the menu bar is an ordinary screen
7370 line, and this makes it get updated. */
7371 w->update_mode_line = Qt;
7372 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7373 /* In the non-toolkit version, the menu bar is an ordinary screen
7374 line, and this makes it get updated. */
7375 w->update_mode_line = Qt;
7376 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7377
7378 unbind_to (count, Qnil);
7379 set_buffer_internal_1 (prev);
7380 }
7381 }
7382 }
7383
7384
7385 \f
7386 /***********************************************************************
7387 Tool-bars
7388 ***********************************************************************/
7389
7390 #ifdef HAVE_WINDOW_SYSTEM
7391
7392 /* Update the tool-bar item list for frame F. This has to be done
7393 before we start to fill in any display lines. Called from
7394 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7395 and restore it here. */
7396
7397 static void
7398 update_tool_bar (f, save_match_data)
7399 struct frame *f;
7400 int save_match_data;
7401 {
7402 if (WINDOWP (f->tool_bar_window)
7403 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7404 {
7405 Lisp_Object window;
7406 struct window *w;
7407
7408 window = FRAME_SELECTED_WINDOW (f);
7409 w = XWINDOW (window);
7410
7411 /* If the user has switched buffers or windows, we need to
7412 recompute to reflect the new bindings. But we'll
7413 recompute when update_mode_lines is set too; that means
7414 that people can use force-mode-line-update to request
7415 that the menu bar be recomputed. The adverse effect on
7416 the rest of the redisplay algorithm is about the same as
7417 windows_or_buffers_changed anyway. */
7418 if (windows_or_buffers_changed
7419 || !NILP (w->update_mode_line)
7420 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7421 < BUF_MODIFF (XBUFFER (w->buffer)))
7422 != !NILP (w->last_had_star))
7423 || ((!NILP (Vtransient_mark_mode)
7424 && !NILP (XBUFFER (w->buffer)->mark_active))
7425 != !NILP (w->region_showing)))
7426 {
7427 struct buffer *prev = current_buffer;
7428 int count = BINDING_STACK_SIZE ();
7429
7430 /* Set current_buffer to the buffer of the selected
7431 window of the frame, so that we get the right local
7432 keymaps. */
7433 set_buffer_internal_1 (XBUFFER (w->buffer));
7434
7435 /* Save match data, if we must. */
7436 if (save_match_data)
7437 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7438
7439 /* Make sure that we don't accidentally use bogus keymaps. */
7440 if (NILP (Voverriding_local_map_menu_flag))
7441 {
7442 specbind (Qoverriding_terminal_local_map, Qnil);
7443 specbind (Qoverriding_local_map, Qnil);
7444 }
7445
7446 /* Build desired tool-bar items from keymaps. */
7447 f->tool_bar_items
7448 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7449
7450 /* Redisplay the tool-bar in case we changed it. */
7451 w->update_mode_line = Qt;
7452
7453 unbind_to (count, Qnil);
7454 set_buffer_internal_1 (prev);
7455 }
7456 }
7457 }
7458
7459
7460 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7461 F's desired tool-bar contents. F->tool_bar_items must have
7462 been set up previously by calling prepare_menu_bars. */
7463
7464 static void
7465 build_desired_tool_bar_string (f)
7466 struct frame *f;
7467 {
7468 int i, size, size_needed;
7469 struct gcpro gcpro1, gcpro2, gcpro3;
7470 Lisp_Object image, plist, props;
7471
7472 image = plist = props = Qnil;
7473 GCPRO3 (image, plist, props);
7474
7475 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7476 Otherwise, make a new string. */
7477
7478 /* The size of the string we might be able to reuse. */
7479 size = (STRINGP (f->desired_tool_bar_string)
7480 ? XSTRING (f->desired_tool_bar_string)->size
7481 : 0);
7482
7483 /* We need one space in the string for each image. */
7484 size_needed = f->n_tool_bar_items;
7485
7486 /* Reuse f->desired_tool_bar_string, if possible. */
7487 if (size < size_needed || NILP (f->desired_tool_bar_string))
7488 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7489 make_number (' '));
7490 else
7491 {
7492 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7493 Fremove_text_properties (make_number (0), make_number (size),
7494 props, f->desired_tool_bar_string);
7495 }
7496
7497 /* Put a `display' property on the string for the images to display,
7498 put a `menu_item' property on tool-bar items with a value that
7499 is the index of the item in F's tool-bar item vector. */
7500 for (i = 0; i < f->n_tool_bar_items; ++i)
7501 {
7502 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7503
7504 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7505 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7506 int hmargin, vmargin, relief, idx, end;
7507 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7508 extern Lisp_Object Qlaplace;
7509
7510 /* If image is a vector, choose the image according to the
7511 button state. */
7512 image = PROP (TOOL_BAR_ITEM_IMAGES);
7513 if (VECTORP (image))
7514 {
7515 if (enabled_p)
7516 idx = (selected_p
7517 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7518 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7519 else
7520 idx = (selected_p
7521 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7522 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7523
7524 xassert (ASIZE (image) >= idx);
7525 image = AREF (image, idx);
7526 }
7527 else
7528 idx = -1;
7529
7530 /* Ignore invalid image specifications. */
7531 if (!valid_image_p (image))
7532 continue;
7533
7534 /* Display the tool-bar button pressed, or depressed. */
7535 plist = Fcopy_sequence (XCDR (image));
7536
7537 /* Compute margin and relief to draw. */
7538 relief = (tool_bar_button_relief > 0
7539 ? tool_bar_button_relief
7540 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7541 hmargin = vmargin = relief;
7542
7543 if (INTEGERP (Vtool_bar_button_margin)
7544 && XINT (Vtool_bar_button_margin) > 0)
7545 {
7546 hmargin += XFASTINT (Vtool_bar_button_margin);
7547 vmargin += XFASTINT (Vtool_bar_button_margin);
7548 }
7549 else if (CONSP (Vtool_bar_button_margin))
7550 {
7551 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7552 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7553 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7554
7555 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7556 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7557 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7558 }
7559
7560 if (auto_raise_tool_bar_buttons_p)
7561 {
7562 /* Add a `:relief' property to the image spec if the item is
7563 selected. */
7564 if (selected_p)
7565 {
7566 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7567 hmargin -= relief;
7568 vmargin -= relief;
7569 }
7570 }
7571 else
7572 {
7573 /* If image is selected, display it pressed, i.e. with a
7574 negative relief. If it's not selected, display it with a
7575 raised relief. */
7576 plist = Fplist_put (plist, QCrelief,
7577 (selected_p
7578 ? make_number (-relief)
7579 : make_number (relief)));
7580 hmargin -= relief;
7581 vmargin -= relief;
7582 }
7583
7584 /* Put a margin around the image. */
7585 if (hmargin || vmargin)
7586 {
7587 if (hmargin == vmargin)
7588 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7589 else
7590 plist = Fplist_put (plist, QCmargin,
7591 Fcons (make_number (hmargin),
7592 make_number (vmargin)));
7593 }
7594
7595 /* If button is not enabled, and we don't have special images
7596 for the disabled state, make the image appear disabled by
7597 applying an appropriate algorithm to it. */
7598 if (!enabled_p && idx < 0)
7599 plist = Fplist_put (plist, QCconversion, Qdisabled);
7600
7601 /* Put a `display' text property on the string for the image to
7602 display. Put a `menu-item' property on the string that gives
7603 the start of this item's properties in the tool-bar items
7604 vector. */
7605 image = Fcons (Qimage, plist);
7606 props = list4 (Qdisplay, image,
7607 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7608
7609 /* Let the last image hide all remaining spaces in the tool bar
7610 string. The string can be longer than needed when we reuse a
7611 previous string. */
7612 if (i + 1 == f->n_tool_bar_items)
7613 end = XSTRING (f->desired_tool_bar_string)->size;
7614 else
7615 end = i + 1;
7616 Fadd_text_properties (make_number (i), make_number (end),
7617 props, f->desired_tool_bar_string);
7618 #undef PROP
7619 }
7620
7621 UNGCPRO;
7622 }
7623
7624
7625 /* Display one line of the tool-bar of frame IT->f. */
7626
7627 static void
7628 display_tool_bar_line (it)
7629 struct it *it;
7630 {
7631 struct glyph_row *row = it->glyph_row;
7632 int max_x = it->last_visible_x;
7633 struct glyph *last;
7634
7635 prepare_desired_row (row);
7636 row->y = it->current_y;
7637
7638 /* Note that this isn't made use of if the face hasn't a box,
7639 so there's no need to check the face here. */
7640 it->start_of_box_run_p = 1;
7641
7642 while (it->current_x < max_x)
7643 {
7644 int x_before, x, n_glyphs_before, i, nglyphs;
7645
7646 /* Get the next display element. */
7647 if (!get_next_display_element (it))
7648 break;
7649
7650 /* Produce glyphs. */
7651 x_before = it->current_x;
7652 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7653 PRODUCE_GLYPHS (it);
7654
7655 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7656 i = 0;
7657 x = x_before;
7658 while (i < nglyphs)
7659 {
7660 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7661
7662 if (x + glyph->pixel_width > max_x)
7663 {
7664 /* Glyph doesn't fit on line. */
7665 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7666 it->current_x = x;
7667 goto out;
7668 }
7669
7670 ++it->hpos;
7671 x += glyph->pixel_width;
7672 ++i;
7673 }
7674
7675 /* Stop at line ends. */
7676 if (ITERATOR_AT_END_OF_LINE_P (it))
7677 break;
7678
7679 set_iterator_to_next (it, 1);
7680 }
7681
7682 out:;
7683
7684 row->displays_text_p = row->used[TEXT_AREA] != 0;
7685 extend_face_to_end_of_line (it);
7686 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7687 last->right_box_line_p = 1;
7688 if (last == row->glyphs[TEXT_AREA])
7689 last->left_box_line_p = 1;
7690 compute_line_metrics (it);
7691
7692 /* If line is empty, make it occupy the rest of the tool-bar. */
7693 if (!row->displays_text_p)
7694 {
7695 row->height = row->phys_height = it->last_visible_y - row->y;
7696 row->ascent = row->phys_ascent = 0;
7697 }
7698
7699 row->full_width_p = 1;
7700 row->continued_p = 0;
7701 row->truncated_on_left_p = 0;
7702 row->truncated_on_right_p = 0;
7703
7704 it->current_x = it->hpos = 0;
7705 it->current_y += row->height;
7706 ++it->vpos;
7707 ++it->glyph_row;
7708 }
7709
7710
7711 /* Value is the number of screen lines needed to make all tool-bar
7712 items of frame F visible. */
7713
7714 static int
7715 tool_bar_lines_needed (f)
7716 struct frame *f;
7717 {
7718 struct window *w = XWINDOW (f->tool_bar_window);
7719 struct it it;
7720
7721 /* Initialize an iterator for iteration over
7722 F->desired_tool_bar_string in the tool-bar window of frame F. */
7723 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7724 it.first_visible_x = 0;
7725 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7726 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7727
7728 while (!ITERATOR_AT_END_P (&it))
7729 {
7730 it.glyph_row = w->desired_matrix->rows;
7731 clear_glyph_row (it.glyph_row);
7732 display_tool_bar_line (&it);
7733 }
7734
7735 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7736 }
7737
7738
7739 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7740 0, 1, 0,
7741 "Return the number of lines occupied by the tool bar of FRAME.")
7742 (frame)
7743 Lisp_Object frame;
7744 {
7745 struct frame *f;
7746 struct window *w;
7747 int nlines = 0;
7748
7749 if (NILP (frame))
7750 frame = selected_frame;
7751 else
7752 CHECK_FRAME (frame, 0);
7753 f = XFRAME (frame);
7754
7755 if (WINDOWP (f->tool_bar_window)
7756 || (w = XWINDOW (f->tool_bar_window),
7757 XFASTINT (w->height) > 0))
7758 {
7759 update_tool_bar (f, 1);
7760 if (f->n_tool_bar_items)
7761 {
7762 build_desired_tool_bar_string (f);
7763 nlines = tool_bar_lines_needed (f);
7764 }
7765 }
7766
7767 return make_number (nlines);
7768 }
7769
7770
7771 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7772 height should be changed. */
7773
7774 static int
7775 redisplay_tool_bar (f)
7776 struct frame *f;
7777 {
7778 struct window *w;
7779 struct it it;
7780 struct glyph_row *row;
7781 int change_height_p = 0;
7782
7783 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7784 do anything. This means you must start with tool-bar-lines
7785 non-zero to get the auto-sizing effect. Or in other words, you
7786 can turn off tool-bars by specifying tool-bar-lines zero. */
7787 if (!WINDOWP (f->tool_bar_window)
7788 || (w = XWINDOW (f->tool_bar_window),
7789 XFASTINT (w->height) == 0))
7790 return 0;
7791
7792 /* Set up an iterator for the tool-bar window. */
7793 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7794 it.first_visible_x = 0;
7795 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7796 row = it.glyph_row;
7797
7798 /* Build a string that represents the contents of the tool-bar. */
7799 build_desired_tool_bar_string (f);
7800 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7801
7802 /* Display as many lines as needed to display all tool-bar items. */
7803 while (it.current_y < it.last_visible_y)
7804 display_tool_bar_line (&it);
7805
7806 /* It doesn't make much sense to try scrolling in the tool-bar
7807 window, so don't do it. */
7808 w->desired_matrix->no_scrolling_p = 1;
7809 w->must_be_updated_p = 1;
7810
7811 if (auto_resize_tool_bars_p)
7812 {
7813 int nlines;
7814
7815 /* If we couldn't display everything, change the tool-bar's
7816 height. */
7817 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7818 change_height_p = 1;
7819
7820 /* If there are blank lines at the end, except for a partially
7821 visible blank line at the end that is smaller than
7822 CANON_Y_UNIT, change the tool-bar's height. */
7823 row = it.glyph_row - 1;
7824 if (!row->displays_text_p
7825 && row->height >= CANON_Y_UNIT (f))
7826 change_height_p = 1;
7827
7828 /* If row displays tool-bar items, but is partially visible,
7829 change the tool-bar's height. */
7830 if (row->displays_text_p
7831 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7832 change_height_p = 1;
7833
7834 /* Resize windows as needed by changing the `tool-bar-lines'
7835 frame parameter. */
7836 if (change_height_p
7837 && (nlines = tool_bar_lines_needed (f),
7838 nlines != XFASTINT (w->height)))
7839 {
7840 extern Lisp_Object Qtool_bar_lines;
7841 Lisp_Object frame;
7842 int old_height = XFASTINT (w->height);
7843
7844 XSETFRAME (frame, f);
7845 clear_glyph_matrix (w->desired_matrix);
7846 Fmodify_frame_parameters (frame,
7847 Fcons (Fcons (Qtool_bar_lines,
7848 make_number (nlines)),
7849 Qnil));
7850 if (XFASTINT (w->height) != old_height)
7851 fonts_changed_p = 1;
7852 }
7853 }
7854
7855 return change_height_p;
7856 }
7857
7858
7859 /* Get information about the tool-bar item which is displayed in GLYPH
7860 on frame F. Return in *PROP_IDX the index where tool-bar item
7861 properties start in F->tool_bar_items. Value is zero if
7862 GLYPH doesn't display a tool-bar item. */
7863
7864 int
7865 tool_bar_item_info (f, glyph, prop_idx)
7866 struct frame *f;
7867 struct glyph *glyph;
7868 int *prop_idx;
7869 {
7870 Lisp_Object prop;
7871 int success_p;
7872
7873 /* Get the text property `menu-item' at pos. The value of that
7874 property is the start index of this item's properties in
7875 F->tool_bar_items. */
7876 prop = Fget_text_property (make_number (glyph->charpos),
7877 Qmenu_item, f->current_tool_bar_string);
7878 if (INTEGERP (prop))
7879 {
7880 *prop_idx = XINT (prop);
7881 success_p = 1;
7882 }
7883 else
7884 success_p = 0;
7885
7886 return success_p;
7887 }
7888
7889 #endif /* HAVE_WINDOW_SYSTEM */
7890
7891
7892 \f
7893 /************************************************************************
7894 Horizontal scrolling
7895 ************************************************************************/
7896
7897 static int hscroll_window_tree P_ ((Lisp_Object));
7898 static int hscroll_windows P_ ((Lisp_Object));
7899
7900 /* For all leaf windows in the window tree rooted at WINDOW, set their
7901 hscroll value so that PT is (i) visible in the window, and (ii) so
7902 that it is not within a certain margin at the window's left and
7903 right border. Value is non-zero if any window's hscroll has been
7904 changed. */
7905
7906 static int
7907 hscroll_window_tree (window)
7908 Lisp_Object window;
7909 {
7910 int hscrolled_p = 0;
7911
7912 while (WINDOWP (window))
7913 {
7914 struct window *w = XWINDOW (window);
7915
7916 if (WINDOWP (w->hchild))
7917 hscrolled_p |= hscroll_window_tree (w->hchild);
7918 else if (WINDOWP (w->vchild))
7919 hscrolled_p |= hscroll_window_tree (w->vchild);
7920 else if (w->cursor.vpos >= 0)
7921 {
7922 int hscroll_margin, text_area_x, text_area_y;
7923 int text_area_width, text_area_height;
7924 struct glyph_row *current_cursor_row
7925 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7926 struct glyph_row *desired_cursor_row
7927 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7928 struct glyph_row *cursor_row
7929 = (desired_cursor_row->enabled_p
7930 ? desired_cursor_row
7931 : current_cursor_row);
7932
7933 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7934 &text_area_width, &text_area_height);
7935
7936 /* Scroll when cursor is inside this scroll margin. */
7937 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7938
7939 if ((XFASTINT (w->hscroll)
7940 && w->cursor.x < hscroll_margin)
7941 || (cursor_row->enabled_p
7942 && cursor_row->truncated_on_right_p
7943 && (w->cursor.x > text_area_width - hscroll_margin)))
7944 {
7945 struct it it;
7946 int hscroll;
7947 struct buffer *saved_current_buffer;
7948 int pt;
7949
7950 /* Find point in a display of infinite width. */
7951 saved_current_buffer = current_buffer;
7952 current_buffer = XBUFFER (w->buffer);
7953
7954 if (w == XWINDOW (selected_window))
7955 pt = BUF_PT (current_buffer);
7956 else
7957 {
7958 pt = marker_position (w->pointm);
7959 pt = max (BEGV, pt);
7960 pt = min (ZV, pt);
7961 }
7962
7963 /* Move iterator to pt starting at cursor_row->start in
7964 a line with infinite width. */
7965 init_to_row_start (&it, w, cursor_row);
7966 it.last_visible_x = INFINITY;
7967 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7968 current_buffer = saved_current_buffer;
7969
7970 /* Center cursor in window. */
7971 hscroll = (max (0, it.current_x - text_area_width / 2)
7972 / CANON_X_UNIT (it.f));
7973 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
7974
7975 /* Don't call Fset_window_hscroll if value hasn't
7976 changed because it will prevent redisplay
7977 optimizations. */
7978 if (XFASTINT (w->hscroll) != hscroll)
7979 {
7980 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
7981 w->hscroll = make_number (hscroll);
7982 hscrolled_p = 1;
7983 }
7984 }
7985 }
7986
7987 window = w->next;
7988 }
7989
7990 /* Value is non-zero if hscroll of any leaf window has been changed. */
7991 return hscrolled_p;
7992 }
7993
7994
7995 /* Set hscroll so that cursor is visible and not inside horizontal
7996 scroll margins for all windows in the tree rooted at WINDOW. See
7997 also hscroll_window_tree above. Value is non-zero if any window's
7998 hscroll has been changed. If it has, desired matrices on the frame
7999 of WINDOW are cleared. */
8000
8001 static int
8002 hscroll_windows (window)
8003 Lisp_Object window;
8004 {
8005 int hscrolled_p;
8006
8007 if (automatic_hscrolling_p)
8008 {
8009 hscrolled_p = hscroll_window_tree (window);
8010 if (hscrolled_p)
8011 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8012 }
8013 else
8014 hscrolled_p = 0;
8015 return hscrolled_p;
8016 }
8017
8018
8019 \f
8020 /************************************************************************
8021 Redisplay
8022 ************************************************************************/
8023
8024 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8025 to a non-zero value. This is sometimes handy to have in a debugger
8026 session. */
8027
8028 #if GLYPH_DEBUG
8029
8030 /* First and last unchanged row for try_window_id. */
8031
8032 int debug_first_unchanged_at_end_vpos;
8033 int debug_last_unchanged_at_beg_vpos;
8034
8035 /* Delta vpos and y. */
8036
8037 int debug_dvpos, debug_dy;
8038
8039 /* Delta in characters and bytes for try_window_id. */
8040
8041 int debug_delta, debug_delta_bytes;
8042
8043 /* Values of window_end_pos and window_end_vpos at the end of
8044 try_window_id. */
8045
8046 int debug_end_pos, debug_end_vpos;
8047
8048 /* Append a string to W->desired_matrix->method. FMT is a printf
8049 format string. A1...A9 are a supplement for a variable-length
8050 argument list. If trace_redisplay_p is non-zero also printf the
8051 resulting string to stderr. */
8052
8053 static void
8054 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8055 struct window *w;
8056 char *fmt;
8057 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8058 {
8059 char buffer[512];
8060 char *method = w->desired_matrix->method;
8061 int len = strlen (method);
8062 int size = sizeof w->desired_matrix->method;
8063 int remaining = size - len - 1;
8064
8065 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8066 if (len && remaining)
8067 {
8068 method[len] = '|';
8069 --remaining, ++len;
8070 }
8071
8072 strncpy (method + len, buffer, remaining);
8073
8074 if (trace_redisplay_p)
8075 fprintf (stderr, "%p (%s): %s\n",
8076 w,
8077 ((BUFFERP (w->buffer)
8078 && STRINGP (XBUFFER (w->buffer)->name))
8079 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
8080 : "no buffer"),
8081 buffer);
8082 }
8083
8084 #endif /* GLYPH_DEBUG */
8085
8086
8087 /* This counter is used to clear the face cache every once in a while
8088 in redisplay_internal. It is incremented for each redisplay.
8089 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8090 cleared. */
8091
8092 #define CLEAR_FACE_CACHE_COUNT 10000
8093 static int clear_face_cache_count;
8094
8095 /* Record the previous terminal frame we displayed. */
8096
8097 static struct frame *previous_terminal_frame;
8098
8099 /* Non-zero while redisplay_internal is in progress. */
8100
8101 int redisplaying_p;
8102
8103
8104 /* Value is non-zero if all changes in window W, which displays
8105 current_buffer, are in the text between START and END. START is a
8106 buffer position, END is given as a distance from Z. Used in
8107 redisplay_internal for display optimization. */
8108
8109 static INLINE int
8110 text_outside_line_unchanged_p (w, start, end)
8111 struct window *w;
8112 int start, end;
8113 {
8114 int unchanged_p = 1;
8115
8116 /* If text or overlays have changed, see where. */
8117 if (XFASTINT (w->last_modified) < MODIFF
8118 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8119 {
8120 /* Gap in the line? */
8121 if (GPT < start || Z - GPT < end)
8122 unchanged_p = 0;
8123
8124 /* Changes start in front of the line, or end after it? */
8125 if (unchanged_p
8126 && (BEG_UNCHANGED < start - 1
8127 || END_UNCHANGED < end))
8128 unchanged_p = 0;
8129
8130 /* If selective display, can't optimize if changes start at the
8131 beginning of the line. */
8132 if (unchanged_p
8133 && INTEGERP (current_buffer->selective_display)
8134 && XINT (current_buffer->selective_display) > 0
8135 && (BEG_UNCHANGED < start || GPT <= start))
8136 unchanged_p = 0;
8137 }
8138
8139 return unchanged_p;
8140 }
8141
8142
8143 /* Do a frame update, taking possible shortcuts into account. This is
8144 the main external entry point for redisplay.
8145
8146 If the last redisplay displayed an echo area message and that message
8147 is no longer requested, we clear the echo area or bring back the
8148 mini-buffer if that is in use. */
8149
8150 void
8151 redisplay ()
8152 {
8153 redisplay_internal (0);
8154 }
8155
8156 /* Return 1 if point moved out of or into a composition. Otherwise
8157 return 0. PREV_BUF and PREV_PT are the last point buffer and
8158 position. BUF and PT are the current point buffer and position. */
8159
8160 int
8161 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8162 struct buffer *prev_buf, *buf;
8163 int prev_pt, pt;
8164 {
8165 int start, end;
8166 Lisp_Object prop;
8167 Lisp_Object buffer;
8168
8169 XSETBUFFER (buffer, buf);
8170 /* Check a composition at the last point if point moved within the
8171 same buffer. */
8172 if (prev_buf == buf)
8173 {
8174 if (prev_pt == pt)
8175 /* Point didn't move. */
8176 return 0;
8177
8178 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8179 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8180 && COMPOSITION_VALID_P (start, end, prop)
8181 && start < prev_pt && end > prev_pt)
8182 /* The last point was within the composition. Return 1 iff
8183 point moved out of the composition. */
8184 return (pt <= start || pt >= end);
8185 }
8186
8187 /* Check a composition at the current point. */
8188 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8189 && find_composition (pt, -1, &start, &end, &prop, buffer)
8190 && COMPOSITION_VALID_P (start, end, prop)
8191 && start < pt && end > pt);
8192 }
8193
8194 /* Reconsider the setting of B->clip_changed which is displayed
8195 in window W. */
8196
8197 static INLINE void
8198 reconsider_clip_changes (w, b)
8199 struct window *w;
8200 struct buffer *b;
8201 {
8202 if (b->prevent_redisplay_optimizations_p)
8203 b->clip_changed = 1;
8204 else if (b->clip_changed
8205 && !NILP (w->window_end_valid)
8206 && w->current_matrix->buffer == b
8207 && w->current_matrix->zv == BUF_ZV (b)
8208 && w->current_matrix->begv == BUF_BEGV (b))
8209 b->clip_changed = 0;
8210
8211 /* If display wasn't paused, and W is not a tool bar window, see if
8212 point has been moved into or out of a composition. In that case,
8213 we set b->clip_changed to 1 to force updating the screen. If
8214 b->clip_changed has already been set to 1, we can skip this
8215 check. */
8216 if (!b->clip_changed
8217 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8218 {
8219 int pt;
8220
8221 if (w == XWINDOW (selected_window))
8222 pt = BUF_PT (current_buffer);
8223 else
8224 pt = marker_position (w->pointm);
8225
8226 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8227 || pt != XINT (w->last_point))
8228 && check_point_in_composition (w->current_matrix->buffer,
8229 XINT (w->last_point),
8230 XBUFFER (w->buffer), pt))
8231 b->clip_changed = 1;
8232 }
8233 }
8234
8235
8236 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8237 response to any user action; therefore, we should preserve the echo
8238 area. (Actually, our caller does that job.) Perhaps in the future
8239 avoid recentering windows if it is not necessary; currently that
8240 causes some problems. */
8241
8242 static void
8243 redisplay_internal (preserve_echo_area)
8244 int preserve_echo_area;
8245 {
8246 struct window *w = XWINDOW (selected_window);
8247 struct frame *f = XFRAME (w->frame);
8248 int pause;
8249 int must_finish = 0;
8250 struct text_pos tlbufpos, tlendpos;
8251 int number_of_visible_frames;
8252 int count;
8253 struct frame *sf = SELECTED_FRAME ();
8254
8255 /* Non-zero means redisplay has to consider all windows on all
8256 frames. Zero means, only selected_window is considered. */
8257 int consider_all_windows_p;
8258
8259 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8260
8261 /* No redisplay if running in batch mode or frame is not yet fully
8262 initialized, or redisplay is explicitly turned off by setting
8263 Vinhibit_redisplay. */
8264 if (noninteractive
8265 || !NILP (Vinhibit_redisplay)
8266 || !f->glyphs_initialized_p)
8267 return;
8268
8269 /* The flag redisplay_performed_directly_p is set by
8270 direct_output_for_insert when it already did the whole screen
8271 update necessary. */
8272 if (redisplay_performed_directly_p)
8273 {
8274 redisplay_performed_directly_p = 0;
8275 if (!hscroll_windows (selected_window))
8276 return;
8277 }
8278
8279 #ifdef USE_X_TOOLKIT
8280 if (popup_activated ())
8281 return;
8282 #endif
8283
8284 /* I don't think this happens but let's be paranoid. */
8285 if (redisplaying_p)
8286 return;
8287
8288 /* Record a function that resets redisplaying_p to its old value
8289 when we leave this function. */
8290 count = BINDING_STACK_SIZE ();
8291 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8292 ++redisplaying_p;
8293
8294 retry:
8295 pause = 0;
8296 reconsider_clip_changes (w, current_buffer);
8297
8298 /* If new fonts have been loaded that make a glyph matrix adjustment
8299 necessary, do it. */
8300 if (fonts_changed_p)
8301 {
8302 adjust_glyphs (NULL);
8303 ++windows_or_buffers_changed;
8304 fonts_changed_p = 0;
8305 }
8306
8307 /* If face_change_count is non-zero, init_iterator will free all
8308 realized faces, which includes the faces referenced from current
8309 matrices. So, we can't reuse current matrices in this case. */
8310 if (face_change_count)
8311 ++windows_or_buffers_changed;
8312
8313 if (! FRAME_WINDOW_P (sf)
8314 && previous_terminal_frame != sf)
8315 {
8316 /* Since frames on an ASCII terminal share the same display
8317 area, displaying a different frame means redisplay the whole
8318 thing. */
8319 windows_or_buffers_changed++;
8320 SET_FRAME_GARBAGED (sf);
8321 XSETFRAME (Vterminal_frame, sf);
8322 }
8323 previous_terminal_frame = sf;
8324
8325 /* Set the visible flags for all frames. Do this before checking
8326 for resized or garbaged frames; they want to know if their frames
8327 are visible. See the comment in frame.h for
8328 FRAME_SAMPLE_VISIBILITY. */
8329 {
8330 Lisp_Object tail, frame;
8331
8332 number_of_visible_frames = 0;
8333
8334 FOR_EACH_FRAME (tail, frame)
8335 {
8336 struct frame *f = XFRAME (frame);
8337
8338 FRAME_SAMPLE_VISIBILITY (f);
8339 if (FRAME_VISIBLE_P (f))
8340 ++number_of_visible_frames;
8341 clear_desired_matrices (f);
8342 }
8343 }
8344
8345 /* Notice any pending interrupt request to change frame size. */
8346 do_pending_window_change (1);
8347
8348 /* Clear frames marked as garbaged. */
8349 if (frame_garbaged)
8350 clear_garbaged_frames ();
8351
8352 /* Build menubar and tool-bar items. */
8353 prepare_menu_bars ();
8354
8355 if (windows_or_buffers_changed)
8356 update_mode_lines++;
8357
8358 /* Detect case that we need to write or remove a star in the mode line. */
8359 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8360 {
8361 w->update_mode_line = Qt;
8362 if (buffer_shared > 1)
8363 update_mode_lines++;
8364 }
8365
8366 /* If %c is in the mode line, update it if needed. */
8367 if (!NILP (w->column_number_displayed)
8368 /* This alternative quickly identifies a common case
8369 where no change is needed. */
8370 && !(PT == XFASTINT (w->last_point)
8371 && XFASTINT (w->last_modified) >= MODIFF
8372 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8373 && XFASTINT (w->column_number_displayed) != current_column ())
8374 w->update_mode_line = Qt;
8375
8376 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8377
8378 /* The variable buffer_shared is set in redisplay_window and
8379 indicates that we redisplay a buffer in different windows. See
8380 there. */
8381 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8382
8383 /* If specs for an arrow have changed, do thorough redisplay
8384 to ensure we remove any arrow that should no longer exist. */
8385 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8386 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8387 consider_all_windows_p = windows_or_buffers_changed = 1;
8388
8389 /* Normally the message* functions will have already displayed and
8390 updated the echo area, but the frame may have been trashed, or
8391 the update may have been preempted, so display the echo area
8392 again here. Checking both message buffers captures the case that
8393 the echo area should be cleared. */
8394 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
8395 {
8396 int window_height_changed_p = echo_area_display (0);
8397 must_finish = 1;
8398
8399 if (fonts_changed_p)
8400 goto retry;
8401 else if (window_height_changed_p)
8402 {
8403 consider_all_windows_p = 1;
8404 ++update_mode_lines;
8405 ++windows_or_buffers_changed;
8406
8407 /* If window configuration was changed, frames may have been
8408 marked garbaged. Clear them or we will experience
8409 surprises wrt scrolling. */
8410 if (frame_garbaged)
8411 clear_garbaged_frames ();
8412 }
8413 }
8414 else if (EQ (selected_window, minibuf_window)
8415 && (current_buffer->clip_changed
8416 || XFASTINT (w->last_modified) < MODIFF
8417 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8418 && resize_mini_window (w, 0))
8419 {
8420 /* Resized active mini-window to fit the size of what it is
8421 showing if its contents might have changed. */
8422 must_finish = 1;
8423 consider_all_windows_p = 1;
8424 ++windows_or_buffers_changed;
8425 ++update_mode_lines;
8426
8427 /* If window configuration was changed, frames may have been
8428 marked garbaged. Clear them or we will experience
8429 surprises wrt scrolling. */
8430 if (frame_garbaged)
8431 clear_garbaged_frames ();
8432 }
8433
8434
8435 /* If showing the region, and mark has changed, we must redisplay
8436 the whole window. The assignment to this_line_start_pos prevents
8437 the optimization directly below this if-statement. */
8438 if (((!NILP (Vtransient_mark_mode)
8439 && !NILP (XBUFFER (w->buffer)->mark_active))
8440 != !NILP (w->region_showing))
8441 || (!NILP (w->region_showing)
8442 && !EQ (w->region_showing,
8443 Fmarker_position (XBUFFER (w->buffer)->mark))))
8444 CHARPOS (this_line_start_pos) = 0;
8445
8446 /* Optimize the case that only the line containing the cursor in the
8447 selected window has changed. Variables starting with this_ are
8448 set in display_line and record information about the line
8449 containing the cursor. */
8450 tlbufpos = this_line_start_pos;
8451 tlendpos = this_line_end_pos;
8452 if (!consider_all_windows_p
8453 && CHARPOS (tlbufpos) > 0
8454 && NILP (w->update_mode_line)
8455 && !current_buffer->clip_changed
8456 && FRAME_VISIBLE_P (XFRAME (w->frame))
8457 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8458 /* Make sure recorded data applies to current buffer, etc. */
8459 && this_line_buffer == current_buffer
8460 && current_buffer == XBUFFER (w->buffer)
8461 && NILP (w->force_start)
8462 /* Point must be on the line that we have info recorded about. */
8463 && PT >= CHARPOS (tlbufpos)
8464 && PT <= Z - CHARPOS (tlendpos)
8465 /* All text outside that line, including its final newline,
8466 must be unchanged */
8467 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8468 CHARPOS (tlendpos)))
8469 {
8470 if (CHARPOS (tlbufpos) > BEGV
8471 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8472 && (CHARPOS (tlbufpos) == ZV
8473 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8474 /* Former continuation line has disappeared by becoming empty */
8475 goto cancel;
8476 else if (XFASTINT (w->last_modified) < MODIFF
8477 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8478 || MINI_WINDOW_P (w))
8479 {
8480 /* We have to handle the case of continuation around a
8481 wide-column character (See the comment in indent.c around
8482 line 885).
8483
8484 For instance, in the following case:
8485
8486 -------- Insert --------
8487 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8488 J_I_ ==> J_I_ `^^' are cursors.
8489 ^^ ^^
8490 -------- --------
8491
8492 As we have to redraw the line above, we should goto cancel. */
8493
8494 struct it it;
8495 int line_height_before = this_line_pixel_height;
8496
8497 /* Note that start_display will handle the case that the
8498 line starting at tlbufpos is a continuation lines. */
8499 start_display (&it, w, tlbufpos);
8500
8501 /* Implementation note: It this still necessary? */
8502 if (it.current_x != this_line_start_x)
8503 goto cancel;
8504
8505 TRACE ((stderr, "trying display optimization 1\n"));
8506 w->cursor.vpos = -1;
8507 overlay_arrow_seen = 0;
8508 it.vpos = this_line_vpos;
8509 it.current_y = this_line_y;
8510 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8511 display_line (&it);
8512
8513 /* If line contains point, is not continued,
8514 and ends at same distance from eob as before, we win */
8515 if (w->cursor.vpos >= 0
8516 /* Line is not continued, otherwise this_line_start_pos
8517 would have been set to 0 in display_line. */
8518 && CHARPOS (this_line_start_pos)
8519 /* Line ends as before. */
8520 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8521 /* Line has same height as before. Otherwise other lines
8522 would have to be shifted up or down. */
8523 && this_line_pixel_height == line_height_before)
8524 {
8525 /* If this is not the window's last line, we must adjust
8526 the charstarts of the lines below. */
8527 if (it.current_y < it.last_visible_y)
8528 {
8529 struct glyph_row *row
8530 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8531 int delta, delta_bytes;
8532
8533 if (Z - CHARPOS (tlendpos) == ZV)
8534 {
8535 /* This line ends at end of (accessible part of)
8536 buffer. There is no newline to count. */
8537 delta = (Z
8538 - CHARPOS (tlendpos)
8539 - MATRIX_ROW_START_CHARPOS (row));
8540 delta_bytes = (Z_BYTE
8541 - BYTEPOS (tlendpos)
8542 - MATRIX_ROW_START_BYTEPOS (row));
8543 }
8544 else
8545 {
8546 /* This line ends in a newline. Must take
8547 account of the newline and the rest of the
8548 text that follows. */
8549 delta = (Z
8550 - CHARPOS (tlendpos)
8551 - MATRIX_ROW_START_CHARPOS (row));
8552 delta_bytes = (Z_BYTE
8553 - BYTEPOS (tlendpos)
8554 - MATRIX_ROW_START_BYTEPOS (row));
8555 }
8556
8557 increment_matrix_positions (w->current_matrix,
8558 this_line_vpos + 1,
8559 w->current_matrix->nrows,
8560 delta, delta_bytes);
8561 }
8562
8563 /* If this row displays text now but previously didn't,
8564 or vice versa, w->window_end_vpos may have to be
8565 adjusted. */
8566 if ((it.glyph_row - 1)->displays_text_p)
8567 {
8568 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8569 XSETINT (w->window_end_vpos, this_line_vpos);
8570 }
8571 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8572 && this_line_vpos > 0)
8573 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8574 w->window_end_valid = Qnil;
8575
8576 /* Update hint: No need to try to scroll in update_window. */
8577 w->desired_matrix->no_scrolling_p = 1;
8578
8579 #if GLYPH_DEBUG
8580 *w->desired_matrix->method = 0;
8581 debug_method_add (w, "optimization 1");
8582 #endif
8583 goto update;
8584 }
8585 else
8586 goto cancel;
8587 }
8588 else if (/* Cursor position hasn't changed. */
8589 PT == XFASTINT (w->last_point)
8590 /* Make sure the cursor was last displayed
8591 in this window. Otherwise we have to reposition it. */
8592 && 0 <= w->cursor.vpos
8593 && XINT (w->height) > w->cursor.vpos)
8594 {
8595 if (!must_finish)
8596 {
8597 do_pending_window_change (1);
8598
8599 /* We used to always goto end_of_redisplay here, but this
8600 isn't enough if we have a blinking cursor. */
8601 if (w->cursor_off_p == w->last_cursor_off_p)
8602 goto end_of_redisplay;
8603 }
8604 goto update;
8605 }
8606 /* If highlighting the region, or if the cursor is in the echo area,
8607 then we can't just move the cursor. */
8608 else if (! (!NILP (Vtransient_mark_mode)
8609 && !NILP (current_buffer->mark_active))
8610 && (EQ (selected_window, current_buffer->last_selected_window)
8611 || highlight_nonselected_windows)
8612 && NILP (w->region_showing)
8613 && NILP (Vshow_trailing_whitespace)
8614 && !cursor_in_echo_area)
8615 {
8616 struct it it;
8617 struct glyph_row *row;
8618
8619 /* Skip from tlbufpos to PT and see where it is. Note that
8620 PT may be in invisible text. If so, we will end at the
8621 next visible position. */
8622 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8623 NULL, DEFAULT_FACE_ID);
8624 it.current_x = this_line_start_x;
8625 it.current_y = this_line_y;
8626 it.vpos = this_line_vpos;
8627
8628 /* The call to move_it_to stops in front of PT, but
8629 moves over before-strings. */
8630 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8631
8632 if (it.vpos == this_line_vpos
8633 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8634 row->enabled_p))
8635 {
8636 xassert (this_line_vpos == it.vpos);
8637 xassert (this_line_y == it.current_y);
8638 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8639 #if GLYPH_DEBUG
8640 *w->desired_matrix->method = 0;
8641 debug_method_add (w, "optimization 3");
8642 #endif
8643 goto update;
8644 }
8645 else
8646 goto cancel;
8647 }
8648
8649 cancel:
8650 /* Text changed drastically or point moved off of line. */
8651 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8652 }
8653
8654 CHARPOS (this_line_start_pos) = 0;
8655 consider_all_windows_p |= buffer_shared > 1;
8656 ++clear_face_cache_count;
8657
8658
8659 /* Build desired matrices, and update the display. If
8660 consider_all_windows_p is non-zero, do it for all windows on all
8661 frames. Otherwise do it for selected_window, only. */
8662
8663 if (consider_all_windows_p)
8664 {
8665 Lisp_Object tail, frame;
8666 int i, n = 0, size = 50;
8667 struct frame **updated
8668 = (struct frame **) alloca (size * sizeof *updated);
8669
8670 /* Clear the face cache eventually. */
8671 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8672 {
8673 clear_face_cache (0);
8674 clear_face_cache_count = 0;
8675 }
8676
8677 /* Recompute # windows showing selected buffer. This will be
8678 incremented each time such a window is displayed. */
8679 buffer_shared = 0;
8680
8681 FOR_EACH_FRAME (tail, frame)
8682 {
8683 struct frame *f = XFRAME (frame);
8684
8685 if (FRAME_WINDOW_P (f) || f == sf)
8686 {
8687 /* Mark all the scroll bars to be removed; we'll redeem
8688 the ones we want when we redisplay their windows. */
8689 if (condemn_scroll_bars_hook)
8690 condemn_scroll_bars_hook (f);
8691
8692 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8693 redisplay_windows (FRAME_ROOT_WINDOW (f));
8694
8695 /* Any scroll bars which redisplay_windows should have
8696 nuked should now go away. */
8697 if (judge_scroll_bars_hook)
8698 judge_scroll_bars_hook (f);
8699
8700 /* If fonts changed, display again. */
8701 if (fonts_changed_p)
8702 goto retry;
8703
8704 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8705 {
8706 /* See if we have to hscroll. */
8707 if (hscroll_windows (f->root_window))
8708 goto retry;
8709
8710 /* Prevent various kinds of signals during display
8711 update. stdio is not robust about handling
8712 signals, which can cause an apparent I/O
8713 error. */
8714 if (interrupt_input)
8715 unrequest_sigio ();
8716 stop_polling ();
8717
8718 /* Update the display. */
8719 set_window_update_flags (XWINDOW (f->root_window), 1);
8720 pause |= update_frame (f, 0, 0);
8721 if (pause)
8722 break;
8723
8724 if (n == size)
8725 {
8726 int nbytes = size * sizeof *updated;
8727 struct frame **p = (struct frame **) alloca (2 * nbytes);
8728 bcopy (updated, p, nbytes);
8729 size *= 2;
8730 }
8731
8732 updated[n++] = f;
8733 }
8734 }
8735 }
8736
8737 /* Do the mark_window_display_accurate after all windows have
8738 been redisplayed because this call resets flags in buffers
8739 which are needed for proper redisplay. */
8740 for (i = 0; i < n; ++i)
8741 {
8742 struct frame *f = updated[i];
8743 mark_window_display_accurate (f->root_window, 1);
8744 if (frame_up_to_date_hook)
8745 frame_up_to_date_hook (f);
8746 }
8747 }
8748 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8749 {
8750 Lisp_Object mini_window;
8751 struct frame *mini_frame;
8752
8753 redisplay_window (selected_window, 1);
8754
8755 /* Compare desired and current matrices, perform output. */
8756 update:
8757
8758 /* If fonts changed, display again. */
8759 if (fonts_changed_p)
8760 goto retry;
8761
8762 /* Prevent various kinds of signals during display update.
8763 stdio is not robust about handling signals,
8764 which can cause an apparent I/O error. */
8765 if (interrupt_input)
8766 unrequest_sigio ();
8767 stop_polling ();
8768
8769 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8770 {
8771 if (hscroll_windows (selected_window))
8772 goto retry;
8773
8774 XWINDOW (selected_window)->must_be_updated_p = 1;
8775 pause = update_frame (sf, 0, 0);
8776 }
8777
8778 /* We may have called echo_area_display at the top of this
8779 function. If the echo area is on another frame, that may
8780 have put text on a frame other than the selected one, so the
8781 above call to update_frame would not have caught it. Catch
8782 it here. */
8783 mini_window = FRAME_MINIBUF_WINDOW (sf);
8784 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8785
8786 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8787 {
8788 XWINDOW (mini_window)->must_be_updated_p = 1;
8789 pause |= update_frame (mini_frame, 0, 0);
8790 if (!pause && hscroll_windows (mini_window))
8791 goto retry;
8792 }
8793 }
8794
8795 /* If display was paused because of pending input, make sure we do a
8796 thorough update the next time. */
8797 if (pause)
8798 {
8799 /* Prevent the optimization at the beginning of
8800 redisplay_internal that tries a single-line update of the
8801 line containing the cursor in the selected window. */
8802 CHARPOS (this_line_start_pos) = 0;
8803
8804 /* Let the overlay arrow be updated the next time. */
8805 if (!NILP (last_arrow_position))
8806 {
8807 last_arrow_position = Qt;
8808 last_arrow_string = Qt;
8809 }
8810
8811 /* If we pause after scrolling, some rows in the current
8812 matrices of some windows are not valid. */
8813 if (!WINDOW_FULL_WIDTH_P (w)
8814 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8815 update_mode_lines = 1;
8816 }
8817 else
8818 {
8819 if (!consider_all_windows_p)
8820 {
8821 /* This has already been done above if
8822 consider_all_windows_p is set. */
8823 mark_window_display_accurate_1 (w, 1);
8824
8825 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8826 last_arrow_string = Voverlay_arrow_string;
8827
8828 if (frame_up_to_date_hook != 0)
8829 frame_up_to_date_hook (sf);
8830 }
8831
8832 update_mode_lines = 0;
8833 windows_or_buffers_changed = 0;
8834 }
8835
8836 /* Start SIGIO interrupts coming again. Having them off during the
8837 code above makes it less likely one will discard output, but not
8838 impossible, since there might be stuff in the system buffer here.
8839 But it is much hairier to try to do anything about that. */
8840 if (interrupt_input)
8841 request_sigio ();
8842 start_polling ();
8843
8844 /* If a frame has become visible which was not before, redisplay
8845 again, so that we display it. Expose events for such a frame
8846 (which it gets when becoming visible) don't call the parts of
8847 redisplay constructing glyphs, so simply exposing a frame won't
8848 display anything in this case. So, we have to display these
8849 frames here explicitly. */
8850 if (!pause)
8851 {
8852 Lisp_Object tail, frame;
8853 int new_count = 0;
8854
8855 FOR_EACH_FRAME (tail, frame)
8856 {
8857 int this_is_visible = 0;
8858
8859 if (XFRAME (frame)->visible)
8860 this_is_visible = 1;
8861 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8862 if (XFRAME (frame)->visible)
8863 this_is_visible = 1;
8864
8865 if (this_is_visible)
8866 new_count++;
8867 }
8868
8869 if (new_count != number_of_visible_frames)
8870 windows_or_buffers_changed++;
8871 }
8872
8873 /* Change frame size now if a change is pending. */
8874 do_pending_window_change (1);
8875
8876 /* If we just did a pending size change, or have additional
8877 visible frames, redisplay again. */
8878 if (windows_or_buffers_changed && !pause)
8879 goto retry;
8880
8881 end_of_redisplay:;
8882
8883 unbind_to (count, Qnil);
8884 }
8885
8886
8887 /* Redisplay, but leave alone any recent echo area message unless
8888 another message has been requested in its place.
8889
8890 This is useful in situations where you need to redisplay but no
8891 user action has occurred, making it inappropriate for the message
8892 area to be cleared. See tracking_off and
8893 wait_reading_process_input for examples of these situations.
8894
8895 FROM_WHERE is an integer saying from where this function was
8896 called. This is useful for debugging. */
8897
8898 void
8899 redisplay_preserve_echo_area (from_where)
8900 int from_where;
8901 {
8902 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
8903
8904 if (!NILP (echo_area_buffer[1]))
8905 {
8906 /* We have a previously displayed message, but no current
8907 message. Redisplay the previous message. */
8908 display_last_displayed_message_p = 1;
8909 redisplay_internal (1);
8910 display_last_displayed_message_p = 0;
8911 }
8912 else
8913 redisplay_internal (1);
8914 }
8915
8916
8917 /* Function registered with record_unwind_protect in
8918 redisplay_internal. Clears the flag indicating that a redisplay is
8919 in progress. */
8920
8921 static Lisp_Object
8922 unwind_redisplay (old_redisplaying_p)
8923 Lisp_Object old_redisplaying_p;
8924 {
8925 redisplaying_p = XFASTINT (old_redisplaying_p);
8926 return Qnil;
8927 }
8928
8929
8930 /* Mark the display of window W as accurate or inaccurate. If
8931 ACCURATE_P is non-zero mark display of W as accurate. If
8932 ACCURATE_P is zero, arrange for W to be redisplayed the next time
8933 redisplay_internal is called. */
8934
8935 static void
8936 mark_window_display_accurate_1 (w, accurate_p)
8937 struct window *w;
8938 int accurate_p;
8939 {
8940 if (BUFFERP (w->buffer))
8941 {
8942 struct buffer *b = XBUFFER (w->buffer);
8943
8944 w->last_modified
8945 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
8946 w->last_overlay_modified
8947 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8948 w->last_had_star
8949 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
8950
8951 if (accurate_p)
8952 {
8953 b->clip_changed = 0;
8954 b->prevent_redisplay_optimizations_p = 0;
8955
8956 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8957 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8958 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8959 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
8960
8961 w->current_matrix->buffer = b;
8962 w->current_matrix->begv = BUF_BEGV (b);
8963 w->current_matrix->zv = BUF_ZV (b);
8964
8965 w->last_cursor = w->cursor;
8966 w->last_cursor_off_p = w->cursor_off_p;
8967
8968 if (w == XWINDOW (selected_window))
8969 w->last_point = make_number (BUF_PT (b));
8970 else
8971 w->last_point = make_number (XMARKER (w->pointm)->charpos);
8972 }
8973 }
8974
8975 if (accurate_p)
8976 {
8977 w->window_end_valid = w->buffer;
8978 w->update_mode_line = Qnil;
8979 }
8980 }
8981
8982
8983 /* Mark the display of windows in the window tree rooted at WINDOW as
8984 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
8985 windows as accurate. If ACCURATE_P is zero, arrange for windows to
8986 be redisplayed the next time redisplay_internal is called. */
8987
8988 void
8989 mark_window_display_accurate (window, accurate_p)
8990 Lisp_Object window;
8991 int accurate_p;
8992 {
8993 struct window *w;
8994
8995 for (; !NILP (window); window = w->next)
8996 {
8997 w = XWINDOW (window);
8998 mark_window_display_accurate_1 (w, accurate_p);
8999
9000 if (!NILP (w->vchild))
9001 mark_window_display_accurate (w->vchild, accurate_p);
9002 if (!NILP (w->hchild))
9003 mark_window_display_accurate (w->hchild, accurate_p);
9004 }
9005
9006 if (accurate_p)
9007 {
9008 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9009 last_arrow_string = Voverlay_arrow_string;
9010 }
9011 else
9012 {
9013 /* Force a thorough redisplay the next time by setting
9014 last_arrow_position and last_arrow_string to t, which is
9015 unequal to any useful value of Voverlay_arrow_... */
9016 last_arrow_position = Qt;
9017 last_arrow_string = Qt;
9018 }
9019 }
9020
9021
9022 /* Return value in display table DP (Lisp_Char_Table *) for character
9023 C. Since a display table doesn't have any parent, we don't have to
9024 follow parent. Do not call this function directly but use the
9025 macro DISP_CHAR_VECTOR. */
9026
9027 Lisp_Object
9028 disp_char_vector (dp, c)
9029 struct Lisp_Char_Table *dp;
9030 int c;
9031 {
9032 int code[4], i;
9033 Lisp_Object val;
9034
9035 if (SINGLE_BYTE_CHAR_P (c))
9036 return (dp->contents[c]);
9037
9038 SPLIT_CHAR (c, code[0], code[1], code[2]);
9039 if (code[1] < 32)
9040 code[1] = -1;
9041 else if (code[2] < 32)
9042 code[2] = -1;
9043
9044 /* Here, the possible range of code[0] (== charset ID) is
9045 128..max_charset. Since the top level char table contains data
9046 for multibyte characters after 256th element, we must increment
9047 code[0] by 128 to get a correct index. */
9048 code[0] += 128;
9049 code[3] = -1; /* anchor */
9050
9051 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9052 {
9053 val = dp->contents[code[i]];
9054 if (!SUB_CHAR_TABLE_P (val))
9055 return (NILP (val) ? dp->defalt : val);
9056 }
9057
9058 /* Here, val is a sub char table. We return the default value of
9059 it. */
9060 return (dp->defalt);
9061 }
9062
9063
9064 \f
9065 /***********************************************************************
9066 Window Redisplay
9067 ***********************************************************************/
9068
9069 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9070
9071 static void
9072 redisplay_windows (window)
9073 Lisp_Object window;
9074 {
9075 while (!NILP (window))
9076 {
9077 struct window *w = XWINDOW (window);
9078
9079 if (!NILP (w->hchild))
9080 redisplay_windows (w->hchild);
9081 else if (!NILP (w->vchild))
9082 redisplay_windows (w->vchild);
9083 else
9084 redisplay_window (window, 0);
9085
9086 window = w->next;
9087 }
9088 }
9089
9090
9091 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9092 DELTA is the number of bytes by which positions recorded in ROW
9093 differ from current buffer positions. */
9094
9095 void
9096 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9097 struct window *w;
9098 struct glyph_row *row;
9099 struct glyph_matrix *matrix;
9100 int delta, delta_bytes, dy, dvpos;
9101 {
9102 struct glyph *glyph = row->glyphs[TEXT_AREA];
9103 struct glyph *end = glyph + row->used[TEXT_AREA];
9104 int x = row->x;
9105 int pt_old = PT - delta;
9106
9107 /* Skip over glyphs not having an object at the start of the row.
9108 These are special glyphs like truncation marks on terminal
9109 frames. */
9110 if (row->displays_text_p)
9111 while (glyph < end
9112 && INTEGERP (glyph->object)
9113 && glyph->charpos < 0)
9114 {
9115 x += glyph->pixel_width;
9116 ++glyph;
9117 }
9118
9119 while (glyph < end
9120 && !INTEGERP (glyph->object)
9121 && (!BUFFERP (glyph->object)
9122 || glyph->charpos < pt_old))
9123 {
9124 x += glyph->pixel_width;
9125 ++glyph;
9126 }
9127
9128 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9129 w->cursor.x = x;
9130 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9131 w->cursor.y = row->y + dy;
9132
9133 if (w == XWINDOW (selected_window))
9134 {
9135 if (!row->continued_p
9136 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9137 && row->x == 0)
9138 {
9139 this_line_buffer = XBUFFER (w->buffer);
9140
9141 CHARPOS (this_line_start_pos)
9142 = MATRIX_ROW_START_CHARPOS (row) + delta;
9143 BYTEPOS (this_line_start_pos)
9144 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9145
9146 CHARPOS (this_line_end_pos)
9147 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9148 BYTEPOS (this_line_end_pos)
9149 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9150
9151 this_line_y = w->cursor.y;
9152 this_line_pixel_height = row->height;
9153 this_line_vpos = w->cursor.vpos;
9154 this_line_start_x = row->x;
9155 }
9156 else
9157 CHARPOS (this_line_start_pos) = 0;
9158 }
9159 }
9160
9161
9162 /* Run window scroll functions, if any, for WINDOW with new window
9163 start STARTP. Sets the window start of WINDOW to that position.
9164
9165 We assume that the window's buffer is really current. */
9166
9167 static INLINE struct text_pos
9168 run_window_scroll_functions (window, startp)
9169 Lisp_Object window;
9170 struct text_pos startp;
9171 {
9172 struct window *w = XWINDOW (window);
9173 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9174
9175 if (current_buffer != XBUFFER (w->buffer))
9176 abort ();
9177
9178 if (!NILP (Vwindow_scroll_functions))
9179 {
9180 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9181 make_number (CHARPOS (startp)));
9182 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9183 /* In case the hook functions switch buffers. */
9184 if (current_buffer != XBUFFER (w->buffer))
9185 set_buffer_internal_1 (XBUFFER (w->buffer));
9186 }
9187
9188 return startp;
9189 }
9190
9191
9192 /* Modify the desired matrix of window W and W->vscroll so that the
9193 line containing the cursor is fully visible. */
9194
9195 static void
9196 make_cursor_line_fully_visible (w)
9197 struct window *w;
9198 {
9199 struct glyph_matrix *matrix;
9200 struct glyph_row *row;
9201 int window_height;
9202
9203 /* It's not always possible to find the cursor, e.g, when a window
9204 is full of overlay strings. Don't do anything in that case. */
9205 if (w->cursor.vpos < 0)
9206 return;
9207
9208 matrix = w->desired_matrix;
9209 row = MATRIX_ROW (matrix, w->cursor.vpos);
9210
9211 /* If the cursor row is not partially visible, there's nothing
9212 to do. */
9213 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9214 return;
9215
9216 /* If the row the cursor is in is taller than the window's height,
9217 it's not clear what to do, so do nothing. */
9218 window_height = window_box_height (w);
9219 if (row->height >= window_height)
9220 return;
9221
9222 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9223 {
9224 int dy = row->height - row->visible_height;
9225 w->vscroll = 0;
9226 w->cursor.y += dy;
9227 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9228 }
9229 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9230 {
9231 int dy = - (row->height - row->visible_height);
9232 w->vscroll = dy;
9233 w->cursor.y += dy;
9234 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9235 }
9236
9237 /* When we change the cursor y-position of the selected window,
9238 change this_line_y as well so that the display optimization for
9239 the cursor line of the selected window in redisplay_internal uses
9240 the correct y-position. */
9241 if (w == XWINDOW (selected_window))
9242 this_line_y = w->cursor.y;
9243 }
9244
9245
9246 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9247 non-zero means only WINDOW is redisplayed in redisplay_internal.
9248 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9249 in redisplay_window to bring a partially visible line into view in
9250 the case that only the cursor has moved.
9251
9252 Value is
9253
9254 1 if scrolling succeeded
9255
9256 0 if scrolling didn't find point.
9257
9258 -1 if new fonts have been loaded so that we must interrupt
9259 redisplay, adjust glyph matrices, and try again. */
9260
9261 static int
9262 try_scrolling (window, just_this_one_p, scroll_conservatively,
9263 scroll_step, temp_scroll_step)
9264 Lisp_Object window;
9265 int just_this_one_p;
9266 int scroll_conservatively, scroll_step;
9267 int temp_scroll_step;
9268 {
9269 struct window *w = XWINDOW (window);
9270 struct frame *f = XFRAME (w->frame);
9271 struct text_pos scroll_margin_pos;
9272 struct text_pos pos;
9273 struct text_pos startp;
9274 struct it it;
9275 Lisp_Object window_end;
9276 int this_scroll_margin;
9277 int dy = 0;
9278 int scroll_max;
9279 int rc;
9280 int amount_to_scroll = 0;
9281 Lisp_Object aggressive;
9282 int height;
9283
9284 #if GLYPH_DEBUG
9285 debug_method_add (w, "try_scrolling");
9286 #endif
9287
9288 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9289
9290 /* Compute scroll margin height in pixels. We scroll when point is
9291 within this distance from the top or bottom of the window. */
9292 if (scroll_margin > 0)
9293 {
9294 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9295 this_scroll_margin *= CANON_Y_UNIT (f);
9296 }
9297 else
9298 this_scroll_margin = 0;
9299
9300 /* Compute how much we should try to scroll maximally to bring point
9301 into view. */
9302 if (scroll_step || scroll_conservatively || temp_scroll_step)
9303 scroll_max = max (scroll_step,
9304 max (scroll_conservatively, temp_scroll_step));
9305 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9306 || NUMBERP (current_buffer->scroll_up_aggressively))
9307 /* We're trying to scroll because of aggressive scrolling
9308 but no scroll_step is set. Choose an arbitrary one. Maybe
9309 there should be a variable for this. */
9310 scroll_max = 10;
9311 else
9312 scroll_max = 0;
9313 scroll_max *= CANON_Y_UNIT (f);
9314
9315 /* Decide whether we have to scroll down. Start at the window end
9316 and move this_scroll_margin up to find the position of the scroll
9317 margin. */
9318 window_end = Fwindow_end (window, Qt);
9319 CHARPOS (scroll_margin_pos) = XINT (window_end);
9320 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9321 if (this_scroll_margin)
9322 {
9323 start_display (&it, w, scroll_margin_pos);
9324 move_it_vertically (&it, - this_scroll_margin);
9325 scroll_margin_pos = it.current.pos;
9326 }
9327
9328 if (PT >= CHARPOS (scroll_margin_pos))
9329 {
9330 int y0;
9331
9332 /* Point is in the scroll margin at the bottom of the window, or
9333 below. Compute a new window start that makes point visible. */
9334
9335 /* Compute the distance from the scroll margin to PT.
9336 Give up if the distance is greater than scroll_max. */
9337 start_display (&it, w, scroll_margin_pos);
9338 y0 = it.current_y;
9339 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9340 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9341
9342 /* With a scroll_margin of 0, scroll_margin_pos is at the window
9343 end, which is one line below the window. The iterator's
9344 current_y will be same as y0 in that case, but we have to
9345 scroll a line to make PT visible. That's the reason why 1 is
9346 added below. */
9347 dy = 1 + it.current_y - y0;
9348
9349 if (dy > scroll_max)
9350 return 0;
9351
9352 /* Move the window start down. If scrolling conservatively,
9353 move it just enough down to make point visible. If
9354 scroll_step is set, move it down by scroll_step. */
9355 start_display (&it, w, startp);
9356
9357 if (scroll_conservatively)
9358 amount_to_scroll
9359 = max (max (dy, CANON_Y_UNIT (f)),
9360 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9361 else if (scroll_step || temp_scroll_step)
9362 amount_to_scroll = scroll_max;
9363 else
9364 {
9365 aggressive = current_buffer->scroll_down_aggressively;
9366 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9367 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9368 if (NUMBERP (aggressive))
9369 amount_to_scroll = XFLOATINT (aggressive) * height;
9370 }
9371
9372 if (amount_to_scroll <= 0)
9373 return 0;
9374
9375 move_it_vertically (&it, amount_to_scroll);
9376 startp = it.current.pos;
9377 }
9378 else
9379 {
9380 /* See if point is inside the scroll margin at the top of the
9381 window. */
9382 scroll_margin_pos = startp;
9383 if (this_scroll_margin)
9384 {
9385 start_display (&it, w, startp);
9386 move_it_vertically (&it, this_scroll_margin);
9387 scroll_margin_pos = it.current.pos;
9388 }
9389
9390 if (PT < CHARPOS (scroll_margin_pos))
9391 {
9392 /* Point is in the scroll margin at the top of the window or
9393 above what is displayed in the window. */
9394 int y0;
9395
9396 /* Compute the vertical distance from PT to the scroll
9397 margin position. Give up if distance is greater than
9398 scroll_max. */
9399 SET_TEXT_POS (pos, PT, PT_BYTE);
9400 start_display (&it, w, pos);
9401 y0 = it.current_y;
9402 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9403 it.last_visible_y, -1,
9404 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9405 dy = it.current_y - y0;
9406 if (dy > scroll_max)
9407 return 0;
9408
9409 /* Compute new window start. */
9410 start_display (&it, w, startp);
9411
9412 if (scroll_conservatively)
9413 amount_to_scroll =
9414 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9415 else if (scroll_step || temp_scroll_step)
9416 amount_to_scroll = scroll_max;
9417 else
9418 {
9419 aggressive = current_buffer->scroll_up_aggressively;
9420 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9421 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9422 if (NUMBERP (aggressive))
9423 amount_to_scroll = XFLOATINT (aggressive) * height;
9424 }
9425
9426 if (amount_to_scroll <= 0)
9427 return 0;
9428
9429 move_it_vertically (&it, - amount_to_scroll);
9430 startp = it.current.pos;
9431 }
9432 }
9433
9434 /* Run window scroll functions. */
9435 startp = run_window_scroll_functions (window, startp);
9436
9437 /* Display the window. Give up if new fonts are loaded, or if point
9438 doesn't appear. */
9439 if (!try_window (window, startp))
9440 rc = -1;
9441 else if (w->cursor.vpos < 0)
9442 {
9443 clear_glyph_matrix (w->desired_matrix);
9444 rc = 0;
9445 }
9446 else
9447 {
9448 /* Maybe forget recorded base line for line number display. */
9449 if (!just_this_one_p
9450 || current_buffer->clip_changed
9451 || BEG_UNCHANGED < CHARPOS (startp))
9452 w->base_line_number = Qnil;
9453
9454 /* If cursor ends up on a partially visible line, shift display
9455 lines up or down. */
9456 make_cursor_line_fully_visible (w);
9457 rc = 1;
9458 }
9459
9460 return rc;
9461 }
9462
9463
9464 /* Compute a suitable window start for window W if display of W starts
9465 on a continuation line. Value is non-zero if a new window start
9466 was computed.
9467
9468 The new window start will be computed, based on W's width, starting
9469 from the start of the continued line. It is the start of the
9470 screen line with the minimum distance from the old start W->start. */
9471
9472 static int
9473 compute_window_start_on_continuation_line (w)
9474 struct window *w;
9475 {
9476 struct text_pos pos, start_pos;
9477 int window_start_changed_p = 0;
9478
9479 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9480
9481 /* If window start is on a continuation line... Window start may be
9482 < BEGV in case there's invisible text at the start of the
9483 buffer (M-x rmail, for example). */
9484 if (CHARPOS (start_pos) > BEGV
9485 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9486 {
9487 struct it it;
9488 struct glyph_row *row;
9489
9490 /* Handle the case that the window start is out of range. */
9491 if (CHARPOS (start_pos) < BEGV)
9492 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9493 else if (CHARPOS (start_pos) > ZV)
9494 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9495
9496 /* Find the start of the continued line. This should be fast
9497 because scan_buffer is fast (newline cache). */
9498 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9499 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9500 row, DEFAULT_FACE_ID);
9501 reseat_at_previous_visible_line_start (&it);
9502
9503 /* If the line start is "too far" away from the window start,
9504 say it takes too much time to compute a new window start. */
9505 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9506 < XFASTINT (w->height) * XFASTINT (w->width))
9507 {
9508 int min_distance, distance;
9509
9510 /* Move forward by display lines to find the new window
9511 start. If window width was enlarged, the new start can
9512 be expected to be > the old start. If window width was
9513 decreased, the new window start will be < the old start.
9514 So, we're looking for the display line start with the
9515 minimum distance from the old window start. */
9516 pos = it.current.pos;
9517 min_distance = INFINITY;
9518 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9519 distance < min_distance)
9520 {
9521 min_distance = distance;
9522 pos = it.current.pos;
9523 move_it_by_lines (&it, 1, 0);
9524 }
9525
9526 /* Set the window start there. */
9527 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9528 window_start_changed_p = 1;
9529 }
9530 }
9531
9532 return window_start_changed_p;
9533 }
9534
9535
9536 /* Try cursor movement in case text has not changes in window WINDOW,
9537 with window start STARTP. Value is
9538
9539 1 if successful
9540
9541 0 if this method cannot be used
9542
9543 -1 if we know we have to scroll the display. *SCROLL_STEP is
9544 set to 1, under certain circumstances, if we want to scroll as
9545 if scroll-step were set to 1. See the code. */
9546
9547 static int
9548 try_cursor_movement (window, startp, scroll_step)
9549 Lisp_Object window;
9550 struct text_pos startp;
9551 int *scroll_step;
9552 {
9553 struct window *w = XWINDOW (window);
9554 struct frame *f = XFRAME (w->frame);
9555 int rc = 0;
9556
9557 /* Handle case where text has not changed, only point, and it has
9558 not moved off the frame. */
9559 if (/* Point may be in this window. */
9560 PT >= CHARPOS (startp)
9561 /* Selective display hasn't changed. */
9562 && !current_buffer->clip_changed
9563 /* Function force-mode-line-update is used to force a thorough
9564 redisplay. It sets either windows_or_buffers_changed or
9565 update_mode_lines. So don't take a shortcut here for these
9566 cases. */
9567 && !update_mode_lines
9568 && !windows_or_buffers_changed
9569 /* Can't use this case if highlighting a region. When a
9570 region exists, cursor movement has to do more than just
9571 set the cursor. */
9572 && !(!NILP (Vtransient_mark_mode)
9573 && !NILP (current_buffer->mark_active))
9574 && NILP (w->region_showing)
9575 && NILP (Vshow_trailing_whitespace)
9576 /* Right after splitting windows, last_point may be nil. */
9577 && INTEGERP (w->last_point)
9578 /* This code is not used for mini-buffer for the sake of the case
9579 of redisplaying to replace an echo area message; since in
9580 that case the mini-buffer contents per se are usually
9581 unchanged. This code is of no real use in the mini-buffer
9582 since the handling of this_line_start_pos, etc., in redisplay
9583 handles the same cases. */
9584 && !EQ (window, minibuf_window)
9585 /* When splitting windows or for new windows, it happens that
9586 redisplay is called with a nil window_end_vpos or one being
9587 larger than the window. This should really be fixed in
9588 window.c. I don't have this on my list, now, so we do
9589 approximately the same as the old redisplay code. --gerd. */
9590 && INTEGERP (w->window_end_vpos)
9591 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9592 && (FRAME_WINDOW_P (f)
9593 || !MARKERP (Voverlay_arrow_position)
9594 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9595 {
9596 int this_scroll_margin;
9597 struct glyph_row *row = NULL;
9598
9599 #if GLYPH_DEBUG
9600 debug_method_add (w, "cursor movement");
9601 #endif
9602
9603 /* Scroll if point within this distance from the top or bottom
9604 of the window. This is a pixel value. */
9605 this_scroll_margin = max (0, scroll_margin);
9606 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9607 this_scroll_margin *= CANON_Y_UNIT (f);
9608
9609 /* Start with the row the cursor was displayed during the last
9610 not paused redisplay. Give up if that row is not valid. */
9611 if (w->last_cursor.vpos < 0
9612 || w->last_cursor.vpos >= w->current_matrix->nrows)
9613 rc = -1;
9614 else
9615 {
9616 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9617 if (row->mode_line_p)
9618 ++row;
9619 if (!row->enabled_p)
9620 rc = -1;
9621 }
9622
9623 if (rc == 0)
9624 {
9625 int scroll_p = 0;
9626 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9627
9628 if (PT > XFASTINT (w->last_point))
9629 {
9630 /* Point has moved forward. */
9631 while (MATRIX_ROW_END_CHARPOS (row) < PT
9632 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9633 {
9634 xassert (row->enabled_p);
9635 ++row;
9636 }
9637
9638 /* The end position of a row equals the start position
9639 of the next row. If PT is there, we would rather
9640 display it in the next line. */
9641 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9642 && MATRIX_ROW_END_CHARPOS (row) == PT
9643 && !cursor_row_p (w, row))
9644 ++row;
9645
9646 /* If within the scroll margin, scroll. Note that
9647 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9648 the next line would be drawn, and that
9649 this_scroll_margin can be zero. */
9650 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9651 || PT > MATRIX_ROW_END_CHARPOS (row)
9652 /* Line is completely visible last line in window
9653 and PT is to be set in the next line. */
9654 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9655 && PT == MATRIX_ROW_END_CHARPOS (row)
9656 && !row->ends_at_zv_p
9657 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9658 scroll_p = 1;
9659 }
9660 else if (PT < XFASTINT (w->last_point))
9661 {
9662 /* Cursor has to be moved backward. Note that PT >=
9663 CHARPOS (startp) because of the outer
9664 if-statement. */
9665 while (!row->mode_line_p
9666 && (MATRIX_ROW_START_CHARPOS (row) > PT
9667 || (MATRIX_ROW_START_CHARPOS (row) == PT
9668 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9669 && (row->y > this_scroll_margin
9670 || CHARPOS (startp) == BEGV))
9671 {
9672 xassert (row->enabled_p);
9673 --row;
9674 }
9675
9676 /* Consider the following case: Window starts at BEGV,
9677 there is invisible, intangible text at BEGV, so that
9678 display starts at some point START > BEGV. It can
9679 happen that we are called with PT somewhere between
9680 BEGV and START. Try to handle that case. */
9681 if (row < w->current_matrix->rows
9682 || row->mode_line_p)
9683 {
9684 row = w->current_matrix->rows;
9685 if (row->mode_line_p)
9686 ++row;
9687 }
9688
9689 /* Due to newlines in overlay strings, we may have to
9690 skip forward over overlay strings. */
9691 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9692 && MATRIX_ROW_END_CHARPOS (row) == PT
9693 && !cursor_row_p (w, row))
9694 ++row;
9695
9696 /* If within the scroll margin, scroll. */
9697 if (row->y < this_scroll_margin
9698 && CHARPOS (startp) != BEGV)
9699 scroll_p = 1;
9700 }
9701
9702 if (PT < MATRIX_ROW_START_CHARPOS (row)
9703 || PT > MATRIX_ROW_END_CHARPOS (row))
9704 {
9705 /* if PT is not in the glyph row, give up. */
9706 rc = -1;
9707 }
9708 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9709 {
9710 if (PT == MATRIX_ROW_END_CHARPOS (row)
9711 && !row->ends_at_zv_p
9712 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
9713 rc = -1;
9714 else if (row->height > window_box_height (w))
9715 {
9716 /* If we end up in a partially visible line, let's
9717 make it fully visible, except when it's taller
9718 than the window, in which case we can't do much
9719 about it. */
9720 *scroll_step = 1;
9721 rc = -1;
9722 }
9723 else
9724 {
9725 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9726 try_window (window, startp);
9727 make_cursor_line_fully_visible (w);
9728 rc = 1;
9729 }
9730 }
9731 else if (scroll_p)
9732 rc = -1;
9733 else
9734 {
9735 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9736 rc = 1;
9737 }
9738 }
9739 }
9740
9741 return rc;
9742 }
9743
9744
9745 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9746 selected_window is redisplayed. */
9747
9748 static void
9749 redisplay_window (window, just_this_one_p)
9750 Lisp_Object window;
9751 int just_this_one_p;
9752 {
9753 struct window *w = XWINDOW (window);
9754 struct frame *f = XFRAME (w->frame);
9755 struct buffer *buffer = XBUFFER (w->buffer);
9756 struct buffer *old = current_buffer;
9757 struct text_pos lpoint, opoint, startp;
9758 int update_mode_line;
9759 int tem;
9760 struct it it;
9761 /* Record it now because it's overwritten. */
9762 int current_matrix_up_to_date_p = 0;
9763 int temp_scroll_step = 0;
9764 int count = BINDING_STACK_SIZE ();
9765 int rc;
9766
9767 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9768 opoint = lpoint;
9769
9770 /* W must be a leaf window here. */
9771 xassert (!NILP (w->buffer));
9772 #if GLYPH_DEBUG
9773 *w->desired_matrix->method = 0;
9774 #endif
9775
9776 specbind (Qinhibit_point_motion_hooks, Qt);
9777
9778 reconsider_clip_changes (w, buffer);
9779
9780 /* Has the mode line to be updated? */
9781 update_mode_line = (!NILP (w->update_mode_line)
9782 || update_mode_lines
9783 || buffer->clip_changed);
9784
9785 if (MINI_WINDOW_P (w))
9786 {
9787 if (w == XWINDOW (echo_area_window)
9788 && !NILP (echo_area_buffer[0]))
9789 {
9790 if (update_mode_line)
9791 /* We may have to update a tty frame's menu bar or a
9792 tool-bar. Example `M-x C-h C-h C-g'. */
9793 goto finish_menu_bars;
9794 else
9795 /* We've already displayed the echo area glyphs in this window. */
9796 goto finish_scroll_bars;
9797 }
9798 else if (w != XWINDOW (minibuf_window))
9799 {
9800 /* W is a mini-buffer window, but it's not the currently
9801 active one, so clear it. */
9802 int yb = window_text_bottom_y (w);
9803 struct glyph_row *row;
9804 int y;
9805
9806 for (y = 0, row = w->desired_matrix->rows;
9807 y < yb;
9808 y += row->height, ++row)
9809 blank_row (w, row, y);
9810 goto finish_scroll_bars;
9811 }
9812
9813 clear_glyph_matrix (w->desired_matrix);
9814 }
9815
9816 /* Otherwise set up data on this window; select its buffer and point
9817 value. */
9818 /* Really select the buffer, for the sake of buffer-local
9819 variables. */
9820 set_buffer_internal_1 (XBUFFER (w->buffer));
9821 SET_TEXT_POS (opoint, PT, PT_BYTE);
9822
9823 current_matrix_up_to_date_p
9824 = (!NILP (w->window_end_valid)
9825 && !current_buffer->clip_changed
9826 && XFASTINT (w->last_modified) >= MODIFF
9827 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9828
9829 /* When windows_or_buffers_changed is non-zero, we can't rely on
9830 the window end being valid, so set it to nil there. */
9831 if (windows_or_buffers_changed)
9832 {
9833 /* If window starts on a continuation line, maybe adjust the
9834 window start in case the window's width changed. */
9835 if (XMARKER (w->start)->buffer == current_buffer)
9836 compute_window_start_on_continuation_line (w);
9837
9838 w->window_end_valid = Qnil;
9839 }
9840
9841 /* Some sanity checks. */
9842 CHECK_WINDOW_END (w);
9843 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9844 abort ();
9845 if (BYTEPOS (opoint) < CHARPOS (opoint))
9846 abort ();
9847
9848 /* If %c is in mode line, update it if needed. */
9849 if (!NILP (w->column_number_displayed)
9850 /* This alternative quickly identifies a common case
9851 where no change is needed. */
9852 && !(PT == XFASTINT (w->last_point)
9853 && XFASTINT (w->last_modified) >= MODIFF
9854 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9855 && XFASTINT (w->column_number_displayed) != current_column ())
9856 update_mode_line = 1;
9857
9858 /* Count number of windows showing the selected buffer. An indirect
9859 buffer counts as its base buffer. */
9860 if (!just_this_one_p)
9861 {
9862 struct buffer *current_base, *window_base;
9863 current_base = current_buffer;
9864 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9865 if (current_base->base_buffer)
9866 current_base = current_base->base_buffer;
9867 if (window_base->base_buffer)
9868 window_base = window_base->base_buffer;
9869 if (current_base == window_base)
9870 buffer_shared++;
9871 }
9872
9873 /* Point refers normally to the selected window. For any other
9874 window, set up appropriate value. */
9875 if (!EQ (window, selected_window))
9876 {
9877 int new_pt = XMARKER (w->pointm)->charpos;
9878 int new_pt_byte = marker_byte_position (w->pointm);
9879 if (new_pt < BEGV)
9880 {
9881 new_pt = BEGV;
9882 new_pt_byte = BEGV_BYTE;
9883 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9884 }
9885 else if (new_pt > (ZV - 1))
9886 {
9887 new_pt = ZV;
9888 new_pt_byte = ZV_BYTE;
9889 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9890 }
9891
9892 /* We don't use SET_PT so that the point-motion hooks don't run. */
9893 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9894 }
9895
9896 /* If any of the character widths specified in the display table
9897 have changed, invalidate the width run cache. It's true that
9898 this may be a bit late to catch such changes, but the rest of
9899 redisplay goes (non-fatally) haywire when the display table is
9900 changed, so why should we worry about doing any better? */
9901 if (current_buffer->width_run_cache)
9902 {
9903 struct Lisp_Char_Table *disptab = buffer_display_table ();
9904
9905 if (! disptab_matches_widthtab (disptab,
9906 XVECTOR (current_buffer->width_table)))
9907 {
9908 invalidate_region_cache (current_buffer,
9909 current_buffer->width_run_cache,
9910 BEG, Z);
9911 recompute_width_table (current_buffer, disptab);
9912 }
9913 }
9914
9915 /* If window-start is screwed up, choose a new one. */
9916 if (XMARKER (w->start)->buffer != current_buffer)
9917 goto recenter;
9918
9919 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9920
9921 /* If someone specified a new starting point but did not insist,
9922 check whether it can be used. */
9923 if (!NILP (w->optional_new_start)
9924 && CHARPOS (startp) >= BEGV
9925 && CHARPOS (startp) <= ZV)
9926 {
9927 w->optional_new_start = Qnil;
9928 start_display (&it, w, startp);
9929 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9930 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9931 if (IT_CHARPOS (it) == PT)
9932 w->force_start = Qt;
9933 }
9934
9935 /* Handle case where place to start displaying has been specified,
9936 unless the specified location is outside the accessible range. */
9937 if (!NILP (w->force_start)
9938 || w->frozen_window_start_p)
9939 {
9940 w->force_start = Qnil;
9941 w->vscroll = 0;
9942 w->window_end_valid = Qnil;
9943
9944 /* Forget any recorded base line for line number display. */
9945 if (!current_matrix_up_to_date_p
9946 || current_buffer->clip_changed)
9947 w->base_line_number = Qnil;
9948
9949 /* Redisplay the mode line. Select the buffer properly for that.
9950 Also, run the hook window-scroll-functions
9951 because we have scrolled. */
9952 /* Note, we do this after clearing force_start because
9953 if there's an error, it is better to forget about force_start
9954 than to get into an infinite loop calling the hook functions
9955 and having them get more errors. */
9956 if (!update_mode_line
9957 || ! NILP (Vwindow_scroll_functions))
9958 {
9959 update_mode_line = 1;
9960 w->update_mode_line = Qt;
9961 startp = run_window_scroll_functions (window, startp);
9962 }
9963
9964 w->last_modified = make_number (0);
9965 w->last_overlay_modified = make_number (0);
9966 if (CHARPOS (startp) < BEGV)
9967 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9968 else if (CHARPOS (startp) > ZV)
9969 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9970
9971 /* Redisplay, then check if cursor has been set during the
9972 redisplay. Give up if new fonts were loaded. */
9973 if (!try_window (window, startp))
9974 {
9975 w->force_start = Qt;
9976 clear_glyph_matrix (w->desired_matrix);
9977 goto finish_scroll_bars;
9978 }
9979
9980 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
9981 {
9982 /* If point does not appear, try to move point so it does
9983 appear. The desired matrix has been built above, so we
9984 can use it here. */
9985 int window_height;
9986 struct glyph_row *row;
9987
9988 window_height = window_box_height (w) / 2;
9989 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9990 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
9991 ++row;
9992
9993 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9994 MATRIX_ROW_START_BYTEPOS (row));
9995
9996 if (w != XWINDOW (selected_window))
9997 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
9998 else if (current_buffer == old)
9999 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10000
10001 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10002
10003 /* If we are highlighting the region, then we just changed
10004 the region, so redisplay to show it. */
10005 if (!NILP (Vtransient_mark_mode)
10006 && !NILP (current_buffer->mark_active))
10007 {
10008 clear_glyph_matrix (w->desired_matrix);
10009 if (!try_window (window, startp))
10010 goto finish_scroll_bars;
10011 }
10012 }
10013
10014 make_cursor_line_fully_visible (w);
10015 #if GLYPH_DEBUG
10016 debug_method_add (w, "forced window start");
10017 #endif
10018 goto done;
10019 }
10020
10021 /* Handle case where text has not changed, only point, and it has
10022 not moved off the frame. */
10023 if (current_matrix_up_to_date_p
10024 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10025 rc != 0))
10026 {
10027 if (rc == -1)
10028 goto try_to_scroll;
10029 else
10030 goto done;
10031 }
10032 /* If current starting point was originally the beginning of a line
10033 but no longer is, find a new starting point. */
10034 else if (!NILP (w->start_at_line_beg)
10035 && !(CHARPOS (startp) <= BEGV
10036 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10037 {
10038 #if GLYPH_DEBUG
10039 debug_method_add (w, "recenter 1");
10040 #endif
10041 goto recenter;
10042 }
10043
10044 /* Try scrolling with try_window_id. Value is > 0 if update has
10045 been done, it is -1 if we know that the same window start will
10046 not work. It is 0 if unsuccessful for some other reason. */
10047 else if ((tem = try_window_id (w)) != 0)
10048 {
10049 #if GLYPH_DEBUG
10050 debug_method_add (w, "try_window_id %d", tem);
10051 #endif
10052
10053 if (fonts_changed_p)
10054 goto finish_scroll_bars;
10055 if (tem > 0)
10056 goto done;
10057
10058 /* Otherwise try_window_id has returned -1 which means that we
10059 don't want the alternative below this comment to execute. */
10060 }
10061 else if (CHARPOS (startp) >= BEGV
10062 && CHARPOS (startp) <= ZV
10063 && PT >= CHARPOS (startp)
10064 && (CHARPOS (startp) < ZV
10065 /* Avoid starting at end of buffer. */
10066 || CHARPOS (startp) == BEGV
10067 || (XFASTINT (w->last_modified) >= MODIFF
10068 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10069 {
10070 #if GLYPH_DEBUG
10071 debug_method_add (w, "same window start");
10072 #endif
10073
10074 /* Try to redisplay starting at same place as before.
10075 If point has not moved off frame, accept the results. */
10076 if (!current_matrix_up_to_date_p
10077 /* Don't use try_window_reusing_current_matrix in this case
10078 because a window scroll function can have changed the
10079 buffer. */
10080 || !NILP (Vwindow_scroll_functions)
10081 || MINI_WINDOW_P (w)
10082 || !try_window_reusing_current_matrix (w))
10083 {
10084 IF_DEBUG (debug_method_add (w, "1"));
10085 try_window (window, startp);
10086 }
10087
10088 if (fonts_changed_p)
10089 goto finish_scroll_bars;
10090
10091 if (w->cursor.vpos >= 0)
10092 {
10093 if (!just_this_one_p
10094 || current_buffer->clip_changed
10095 || BEG_UNCHANGED < CHARPOS (startp))
10096 /* Forget any recorded base line for line number display. */
10097 w->base_line_number = Qnil;
10098
10099 make_cursor_line_fully_visible (w);
10100 goto done;
10101 }
10102 else
10103 clear_glyph_matrix (w->desired_matrix);
10104 }
10105
10106 try_to_scroll:
10107
10108 w->last_modified = make_number (0);
10109 w->last_overlay_modified = make_number (0);
10110
10111 /* Redisplay the mode line. Select the buffer properly for that. */
10112 if (!update_mode_line)
10113 {
10114 update_mode_line = 1;
10115 w->update_mode_line = Qt;
10116 }
10117
10118 /* Try to scroll by specified few lines. */
10119 if ((scroll_conservatively
10120 || scroll_step
10121 || temp_scroll_step
10122 || NUMBERP (current_buffer->scroll_up_aggressively)
10123 || NUMBERP (current_buffer->scroll_down_aggressively))
10124 && !current_buffer->clip_changed
10125 && CHARPOS (startp) >= BEGV
10126 && CHARPOS (startp) <= ZV)
10127 {
10128 /* The function returns -1 if new fonts were loaded, 1 if
10129 successful, 0 if not successful. */
10130 int rc = try_scrolling (window, just_this_one_p,
10131 scroll_conservatively,
10132 scroll_step,
10133 temp_scroll_step);
10134 if (rc > 0)
10135 goto done;
10136 else if (rc < 0)
10137 goto finish_scroll_bars;
10138 }
10139
10140 /* Finally, just choose place to start which centers point */
10141
10142 recenter:
10143
10144 #if GLYPH_DEBUG
10145 debug_method_add (w, "recenter");
10146 #endif
10147
10148 /* w->vscroll = 0; */
10149
10150 /* Forget any previously recorded base line for line number display. */
10151 if (!current_matrix_up_to_date_p
10152 || current_buffer->clip_changed)
10153 w->base_line_number = Qnil;
10154
10155 /* Move backward half the height of the window. */
10156 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10157 it.current_y = it.last_visible_y;
10158 move_it_vertically_backward (&it, window_box_height (w) / 2);
10159 xassert (IT_CHARPOS (it) >= BEGV);
10160
10161 /* The function move_it_vertically_backward may move over more
10162 than the specified y-distance. If it->w is small, e.g. a
10163 mini-buffer window, we may end up in front of the window's
10164 display area. Start displaying at the start of the line
10165 containing PT in this case. */
10166 if (it.current_y <= 0)
10167 {
10168 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10169 move_it_vertically (&it, 0);
10170 xassert (IT_CHARPOS (it) <= PT);
10171 it.current_y = 0;
10172 }
10173
10174 it.current_x = it.hpos = 0;
10175
10176 /* Set startp here explicitly in case that helps avoid an infinite loop
10177 in case the window-scroll-functions functions get errors. */
10178 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10179
10180 /* Run scroll hooks. */
10181 startp = run_window_scroll_functions (window, it.current.pos);
10182
10183 /* Redisplay the window. */
10184 if (!current_matrix_up_to_date_p
10185 || windows_or_buffers_changed
10186 /* Don't use try_window_reusing_current_matrix in this case
10187 because it can have changed the buffer. */
10188 || !NILP (Vwindow_scroll_functions)
10189 || !just_this_one_p
10190 || MINI_WINDOW_P (w)
10191 || !try_window_reusing_current_matrix (w))
10192 try_window (window, startp);
10193
10194 /* If new fonts have been loaded (due to fontsets), give up. We
10195 have to start a new redisplay since we need to re-adjust glyph
10196 matrices. */
10197 if (fonts_changed_p)
10198 goto finish_scroll_bars;
10199
10200 /* If cursor did not appear assume that the middle of the window is
10201 in the first line of the window. Do it again with the next line.
10202 (Imagine a window of height 100, displaying two lines of height
10203 60. Moving back 50 from it->last_visible_y will end in the first
10204 line.) */
10205 if (w->cursor.vpos < 0)
10206 {
10207 if (!NILP (w->window_end_valid)
10208 && PT >= Z - XFASTINT (w->window_end_pos))
10209 {
10210 clear_glyph_matrix (w->desired_matrix);
10211 move_it_by_lines (&it, 1, 0);
10212 try_window (window, it.current.pos);
10213 }
10214 else if (PT < IT_CHARPOS (it))
10215 {
10216 clear_glyph_matrix (w->desired_matrix);
10217 move_it_by_lines (&it, -1, 0);
10218 try_window (window, it.current.pos);
10219 }
10220 else
10221 {
10222 /* Not much we can do about it. */
10223 }
10224 }
10225
10226 /* Consider the following case: Window starts at BEGV, there is
10227 invisible, intangible text at BEGV, so that display starts at
10228 some point START > BEGV. It can happen that we are called with
10229 PT somewhere between BEGV and START. Try to handle that case. */
10230 if (w->cursor.vpos < 0)
10231 {
10232 struct glyph_row *row = w->current_matrix->rows;
10233 if (row->mode_line_p)
10234 ++row;
10235 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10236 }
10237
10238 make_cursor_line_fully_visible (w);
10239
10240 done:
10241
10242 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10243 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10244 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10245 ? Qt : Qnil);
10246
10247 /* Display the mode line, if we must. */
10248 if ((update_mode_line
10249 /* If window not full width, must redo its mode line
10250 if (a) the window to its side is being redone and
10251 (b) we do a frame-based redisplay. This is a consequence
10252 of how inverted lines are drawn in frame-based redisplay. */
10253 || (!just_this_one_p
10254 && !FRAME_WINDOW_P (f)
10255 && !WINDOW_FULL_WIDTH_P (w))
10256 /* Line number to display. */
10257 || INTEGERP (w->base_line_pos)
10258 /* Column number is displayed and different from the one displayed. */
10259 || (!NILP (w->column_number_displayed)
10260 && XFASTINT (w->column_number_displayed) != current_column ()))
10261 /* This means that the window has a mode line. */
10262 && (WINDOW_WANTS_MODELINE_P (w)
10263 || WINDOW_WANTS_HEADER_LINE_P (w)))
10264 {
10265 Lisp_Object old_selected_frame;
10266
10267 old_selected_frame = selected_frame;
10268
10269 XSETFRAME (selected_frame, f);
10270 display_mode_lines (w);
10271 selected_frame = old_selected_frame;
10272
10273 /* If mode line height has changed, arrange for a thorough
10274 immediate redisplay using the correct mode line height. */
10275 if (WINDOW_WANTS_MODELINE_P (w)
10276 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10277 {
10278 fonts_changed_p = 1;
10279 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10280 = DESIRED_MODE_LINE_HEIGHT (w);
10281 }
10282
10283 /* If top line height has changed, arrange for a thorough
10284 immediate redisplay using the correct mode line height. */
10285 if (WINDOW_WANTS_HEADER_LINE_P (w)
10286 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10287 {
10288 fonts_changed_p = 1;
10289 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10290 = DESIRED_HEADER_LINE_HEIGHT (w);
10291 }
10292
10293 if (fonts_changed_p)
10294 goto finish_scroll_bars;
10295 }
10296
10297 if (!line_number_displayed
10298 && !BUFFERP (w->base_line_pos))
10299 {
10300 w->base_line_pos = Qnil;
10301 w->base_line_number = Qnil;
10302 }
10303
10304 finish_menu_bars:
10305
10306 /* When we reach a frame's selected window, redo the frame's menu bar. */
10307 if (update_mode_line
10308 && EQ (FRAME_SELECTED_WINDOW (f), window))
10309 {
10310 int redisplay_menu_p = 0;
10311
10312 if (FRAME_WINDOW_P (f))
10313 {
10314 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10315 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10316 #else
10317 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10318 #endif
10319 }
10320 else
10321 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10322
10323 if (redisplay_menu_p)
10324 display_menu_bar (w);
10325
10326 #ifdef HAVE_WINDOW_SYSTEM
10327 if (WINDOWP (f->tool_bar_window)
10328 && (FRAME_TOOL_BAR_LINES (f) > 0
10329 || auto_resize_tool_bars_p))
10330 redisplay_tool_bar (f);
10331 #endif
10332 }
10333
10334 finish_scroll_bars:
10335
10336 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10337 {
10338 int start, end, whole;
10339
10340 /* Calculate the start and end positions for the current window.
10341 At some point, it would be nice to choose between scrollbars
10342 which reflect the whole buffer size, with special markers
10343 indicating narrowing, and scrollbars which reflect only the
10344 visible region.
10345
10346 Note that mini-buffers sometimes aren't displaying any text. */
10347 if (!MINI_WINDOW_P (w)
10348 || (w == XWINDOW (minibuf_window)
10349 && NILP (echo_area_buffer[0])))
10350 {
10351 whole = ZV - BEGV;
10352 start = marker_position (w->start) - BEGV;
10353 /* I don't think this is guaranteed to be right. For the
10354 moment, we'll pretend it is. */
10355 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10356
10357 if (end < start)
10358 end = start;
10359 if (whole < (end - start))
10360 whole = end - start;
10361 }
10362 else
10363 start = end = whole = 0;
10364
10365 /* Indicate what this scroll bar ought to be displaying now. */
10366 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10367
10368 /* Note that we actually used the scroll bar attached to this
10369 window, so it shouldn't be deleted at the end of redisplay. */
10370 redeem_scroll_bar_hook (w);
10371 }
10372
10373 /* Restore current_buffer and value of point in it. */
10374 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10375 set_buffer_internal_1 (old);
10376 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10377
10378 unbind_to (count, Qnil);
10379 }
10380
10381
10382 /* Build the complete desired matrix of WINDOW with a window start
10383 buffer position POS. Value is non-zero if successful. It is zero
10384 if fonts were loaded during redisplay which makes re-adjusting
10385 glyph matrices necessary. */
10386
10387 int
10388 try_window (window, pos)
10389 Lisp_Object window;
10390 struct text_pos pos;
10391 {
10392 struct window *w = XWINDOW (window);
10393 struct it it;
10394 struct glyph_row *last_text_row = NULL;
10395
10396 /* Make POS the new window start. */
10397 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10398
10399 /* Mark cursor position as unknown. No overlay arrow seen. */
10400 w->cursor.vpos = -1;
10401 overlay_arrow_seen = 0;
10402
10403 /* Initialize iterator and info to start at POS. */
10404 start_display (&it, w, pos);
10405
10406 /* Display all lines of W. */
10407 while (it.current_y < it.last_visible_y)
10408 {
10409 if (display_line (&it))
10410 last_text_row = it.glyph_row - 1;
10411 if (fonts_changed_p)
10412 return 0;
10413 }
10414
10415 /* If bottom moved off end of frame, change mode line percentage. */
10416 if (XFASTINT (w->window_end_pos) <= 0
10417 && Z != IT_CHARPOS (it))
10418 w->update_mode_line = Qt;
10419
10420 /* Set window_end_pos to the offset of the last character displayed
10421 on the window from the end of current_buffer. Set
10422 window_end_vpos to its row number. */
10423 if (last_text_row)
10424 {
10425 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10426 w->window_end_bytepos
10427 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10428 w->window_end_pos
10429 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10430 w->window_end_vpos
10431 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10432 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10433 ->displays_text_p);
10434 }
10435 else
10436 {
10437 w->window_end_bytepos = 0;
10438 w->window_end_pos = w->window_end_vpos = make_number (0);
10439 }
10440
10441 /* But that is not valid info until redisplay finishes. */
10442 w->window_end_valid = Qnil;
10443 return 1;
10444 }
10445
10446
10447 \f
10448 /************************************************************************
10449 Window redisplay reusing current matrix when buffer has not changed
10450 ************************************************************************/
10451
10452 /* Try redisplay of window W showing an unchanged buffer with a
10453 different window start than the last time it was displayed by
10454 reusing its current matrix. Value is non-zero if successful.
10455 W->start is the new window start. */
10456
10457 static int
10458 try_window_reusing_current_matrix (w)
10459 struct window *w;
10460 {
10461 struct frame *f = XFRAME (w->frame);
10462 struct glyph_row *row, *bottom_row;
10463 struct it it;
10464 struct run run;
10465 struct text_pos start, new_start;
10466 int nrows_scrolled, i;
10467 struct glyph_row *last_text_row;
10468 struct glyph_row *last_reused_text_row;
10469 struct glyph_row *start_row;
10470 int start_vpos, min_y, max_y;
10471
10472 if (/* This function doesn't handle terminal frames. */
10473 !FRAME_WINDOW_P (f)
10474 /* Don't try to reuse the display if windows have been split
10475 or such. */
10476 || windows_or_buffers_changed)
10477 return 0;
10478
10479 /* Can't do this if region may have changed. */
10480 if ((!NILP (Vtransient_mark_mode)
10481 && !NILP (current_buffer->mark_active))
10482 || !NILP (w->region_showing)
10483 || !NILP (Vshow_trailing_whitespace))
10484 return 0;
10485
10486 /* If top-line visibility has changed, give up. */
10487 if (WINDOW_WANTS_HEADER_LINE_P (w)
10488 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10489 return 0;
10490
10491 /* Give up if old or new display is scrolled vertically. We could
10492 make this function handle this, but right now it doesn't. */
10493 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10494 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10495 return 0;
10496
10497 /* The variable new_start now holds the new window start. The old
10498 start `start' can be determined from the current matrix. */
10499 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10500 start = start_row->start.pos;
10501 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10502
10503 /* Clear the desired matrix for the display below. */
10504 clear_glyph_matrix (w->desired_matrix);
10505
10506 if (CHARPOS (new_start) <= CHARPOS (start))
10507 {
10508 int first_row_y;
10509
10510 /* Don't use this method if the display starts with an ellipsis
10511 displayed for invisible text. It's not easy to handle that case
10512 below, and it's certainly not worth the effort since this is
10513 not a frequent case. */
10514 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10515 return 0;
10516
10517 IF_DEBUG (debug_method_add (w, "twu1"));
10518
10519 /* Display up to a row that can be reused. The variable
10520 last_text_row is set to the last row displayed that displays
10521 text. Note that it.vpos == 0 if or if not there is a
10522 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10523 start_display (&it, w, new_start);
10524 first_row_y = it.current_y;
10525 w->cursor.vpos = -1;
10526 last_text_row = last_reused_text_row = NULL;
10527
10528 while (it.current_y < it.last_visible_y
10529 && IT_CHARPOS (it) < CHARPOS (start)
10530 && !fonts_changed_p)
10531 if (display_line (&it))
10532 last_text_row = it.glyph_row - 1;
10533
10534 /* A value of current_y < last_visible_y means that we stopped
10535 at the previous window start, which in turn means that we
10536 have at least one reusable row. */
10537 if (it.current_y < it.last_visible_y)
10538 {
10539 /* IT.vpos always starts from 0; it counts text lines. */
10540 nrows_scrolled = it.vpos;
10541
10542 /* Find PT if not already found in the lines displayed. */
10543 if (w->cursor.vpos < 0)
10544 {
10545 int dy = it.current_y - first_row_y;
10546
10547 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10548 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10549 {
10550 if (PT >= MATRIX_ROW_START_CHARPOS (row)
10551 && PT < MATRIX_ROW_END_CHARPOS (row))
10552 {
10553 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10554 dy, nrows_scrolled);
10555 break;
10556 }
10557
10558 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10559 break;
10560
10561 ++row;
10562 }
10563
10564 /* Give up if point was not found. This shouldn't
10565 happen often; not more often than with try_window
10566 itself. */
10567 if (w->cursor.vpos < 0)
10568 {
10569 clear_glyph_matrix (w->desired_matrix);
10570 return 0;
10571 }
10572 }
10573
10574 /* Scroll the display. Do it before the current matrix is
10575 changed. The problem here is that update has not yet
10576 run, i.e. part of the current matrix is not up to date.
10577 scroll_run_hook will clear the cursor, and use the
10578 current matrix to get the height of the row the cursor is
10579 in. */
10580 run.current_y = first_row_y;
10581 run.desired_y = it.current_y;
10582 run.height = it.last_visible_y - it.current_y;
10583
10584 if (run.height > 0 && run.current_y != run.desired_y)
10585 {
10586 update_begin (f);
10587 rif->update_window_begin_hook (w);
10588 rif->clear_mouse_face (w);
10589 rif->scroll_run_hook (w, &run);
10590 rif->update_window_end_hook (w, 0, 0);
10591 update_end (f);
10592 }
10593
10594 /* Shift current matrix down by nrows_scrolled lines. */
10595 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10596 rotate_matrix (w->current_matrix,
10597 start_vpos,
10598 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10599 nrows_scrolled);
10600
10601 /* Disable lines that must be updated. */
10602 for (i = 0; i < it.vpos; ++i)
10603 (start_row + i)->enabled_p = 0;
10604
10605 /* Re-compute Y positions. */
10606 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10607 max_y = it.last_visible_y;
10608 for (row = start_row + nrows_scrolled;
10609 row < bottom_row;
10610 ++row)
10611 {
10612 row->y = it.current_y;
10613
10614 if (row->y < min_y)
10615 row->visible_height = row->height - (min_y - row->y);
10616 else if (row->y + row->height > max_y)
10617 row->visible_height
10618 = row->height - (row->y + row->height - max_y);
10619 else
10620 row->visible_height = row->height;
10621
10622 it.current_y += row->height;
10623
10624 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10625 last_reused_text_row = row;
10626 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10627 break;
10628 }
10629
10630 /* Disable lines in the current matrix which are now
10631 below the window. */
10632 for (++row; row < bottom_row; ++row)
10633 row->enabled_p = 0;
10634 }
10635
10636 /* Update window_end_pos etc.; last_reused_text_row is the last
10637 reused row from the current matrix containing text, if any.
10638 The value of last_text_row is the last displayed line
10639 containing text. */
10640 if (last_reused_text_row)
10641 {
10642 w->window_end_bytepos
10643 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10644 w->window_end_pos
10645 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10646 w->window_end_vpos
10647 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10648 w->current_matrix));
10649 }
10650 else if (last_text_row)
10651 {
10652 w->window_end_bytepos
10653 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10654 w->window_end_pos
10655 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10656 w->window_end_vpos
10657 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10658 }
10659 else
10660 {
10661 /* This window must be completely empty. */
10662 w->window_end_bytepos = 0;
10663 w->window_end_pos = w->window_end_vpos = make_number (0);
10664 }
10665 w->window_end_valid = Qnil;
10666
10667 /* Update hint: don't try scrolling again in update_window. */
10668 w->desired_matrix->no_scrolling_p = 1;
10669
10670 #if GLYPH_DEBUG
10671 debug_method_add (w, "try_window_reusing_current_matrix 1");
10672 #endif
10673 return 1;
10674 }
10675 else if (CHARPOS (new_start) > CHARPOS (start))
10676 {
10677 struct glyph_row *pt_row, *row;
10678 struct glyph_row *first_reusable_row;
10679 struct glyph_row *first_row_to_display;
10680 int dy;
10681 int yb = window_text_bottom_y (w);
10682
10683 /* Find the row starting at new_start, if there is one. Don't
10684 reuse a partially visible line at the end. */
10685 first_reusable_row = start_row;
10686 while (first_reusable_row->enabled_p
10687 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10688 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10689 < CHARPOS (new_start)))
10690 ++first_reusable_row;
10691
10692 /* Give up if there is no row to reuse. */
10693 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10694 || !first_reusable_row->enabled_p
10695 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10696 != CHARPOS (new_start)))
10697 return 0;
10698
10699 /* We can reuse fully visible rows beginning with
10700 first_reusable_row to the end of the window. Set
10701 first_row_to_display to the first row that cannot be reused.
10702 Set pt_row to the row containing point, if there is any. */
10703 pt_row = NULL;
10704 for (first_row_to_display = first_reusable_row;
10705 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
10706 ++first_row_to_display)
10707 {
10708 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10709 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10710 pt_row = first_row_to_display;
10711 }
10712
10713 /* Start displaying at the start of first_row_to_display. */
10714 xassert (first_row_to_display->y < yb);
10715 init_to_row_start (&it, w, first_row_to_display);
10716
10717 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10718 - start_vpos);
10719 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10720 - nrows_scrolled);
10721 it.current_y = (first_row_to_display->y - first_reusable_row->y
10722 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10723
10724 /* Display lines beginning with first_row_to_display in the
10725 desired matrix. Set last_text_row to the last row displayed
10726 that displays text. */
10727 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10728 if (pt_row == NULL)
10729 w->cursor.vpos = -1;
10730 last_text_row = NULL;
10731 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10732 if (display_line (&it))
10733 last_text_row = it.glyph_row - 1;
10734
10735 /* Give up If point isn't in a row displayed or reused. */
10736 if (w->cursor.vpos < 0)
10737 {
10738 clear_glyph_matrix (w->desired_matrix);
10739 return 0;
10740 }
10741
10742 /* If point is in a reused row, adjust y and vpos of the cursor
10743 position. */
10744 if (pt_row)
10745 {
10746 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10747 w->current_matrix);
10748 w->cursor.y -= first_reusable_row->y;
10749 }
10750
10751 /* Scroll the display. */
10752 run.current_y = first_reusable_row->y;
10753 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10754 run.height = it.last_visible_y - run.current_y;
10755 dy = run.current_y - run.desired_y;
10756
10757 if (run.height)
10758 {
10759 struct frame *f = XFRAME (WINDOW_FRAME (w));
10760 update_begin (f);
10761 rif->update_window_begin_hook (w);
10762 rif->clear_mouse_face (w);
10763 rif->scroll_run_hook (w, &run);
10764 rif->update_window_end_hook (w, 0, 0);
10765 update_end (f);
10766 }
10767
10768 /* Adjust Y positions of reused rows. */
10769 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10770 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10771 max_y = it.last_visible_y;
10772 for (row = first_reusable_row; row < first_row_to_display; ++row)
10773 {
10774 row->y -= dy;
10775 if (row->y < min_y)
10776 row->visible_height = row->height - (min_y - row->y);
10777 else if (row->y + row->height > max_y)
10778 row->visible_height
10779 = row->height - (row->y + row->height - max_y);
10780 else
10781 row->visible_height = row->height;
10782 }
10783
10784 /* Scroll the current matrix. */
10785 xassert (nrows_scrolled > 0);
10786 rotate_matrix (w->current_matrix,
10787 start_vpos,
10788 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10789 -nrows_scrolled);
10790
10791 /* Disable rows not reused. */
10792 for (row -= nrows_scrolled; row < bottom_row; ++row)
10793 row->enabled_p = 0;
10794
10795 /* Adjust window end. A null value of last_text_row means that
10796 the window end is in reused rows which in turn means that
10797 only its vpos can have changed. */
10798 if (last_text_row)
10799 {
10800 w->window_end_bytepos
10801 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10802 w->window_end_pos
10803 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10804 w->window_end_vpos
10805 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10806 }
10807 else
10808 {
10809 w->window_end_vpos
10810 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
10811 }
10812
10813 w->window_end_valid = Qnil;
10814 w->desired_matrix->no_scrolling_p = 1;
10815
10816 #if GLYPH_DEBUG
10817 debug_method_add (w, "try_window_reusing_current_matrix 2");
10818 #endif
10819 return 1;
10820 }
10821
10822 return 0;
10823 }
10824
10825
10826 \f
10827 /************************************************************************
10828 Window redisplay reusing current matrix when buffer has changed
10829 ************************************************************************/
10830
10831 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10832 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10833 int *, int *));
10834 static struct glyph_row *
10835 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10836 struct glyph_row *));
10837
10838
10839 /* Return the last row in MATRIX displaying text. If row START is
10840 non-null, start searching with that row. IT gives the dimensions
10841 of the display. Value is null if matrix is empty; otherwise it is
10842 a pointer to the row found. */
10843
10844 static struct glyph_row *
10845 find_last_row_displaying_text (matrix, it, start)
10846 struct glyph_matrix *matrix;
10847 struct it *it;
10848 struct glyph_row *start;
10849 {
10850 struct glyph_row *row, *row_found;
10851
10852 /* Set row_found to the last row in IT->w's current matrix
10853 displaying text. The loop looks funny but think of partially
10854 visible lines. */
10855 row_found = NULL;
10856 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10857 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10858 {
10859 xassert (row->enabled_p);
10860 row_found = row;
10861 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10862 break;
10863 ++row;
10864 }
10865
10866 return row_found;
10867 }
10868
10869
10870 /* Return the last row in the current matrix of W that is not affected
10871 by changes at the start of current_buffer that occurred since W's
10872 current matrix was built. Value is null if no such row exists.
10873
10874 BEG_UNCHANGED us the number of characters unchanged at the start of
10875 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
10876 first changed character in current_buffer. Characters at positions <
10877 BEG + BEG_UNCHANGED are at the same buffer positions as they were
10878 when the current matrix was built. */
10879
10880 static struct glyph_row *
10881 find_last_unchanged_at_beg_row (w)
10882 struct window *w;
10883 {
10884 int first_changed_pos = BEG + BEG_UNCHANGED;
10885 struct glyph_row *row;
10886 struct glyph_row *row_found = NULL;
10887 int yb = window_text_bottom_y (w);
10888
10889 /* Find the last row displaying unchanged text. */
10890 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10891 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10892 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10893 {
10894 if (/* If row ends before first_changed_pos, it is unchanged,
10895 except in some case. */
10896 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10897 /* When row ends in ZV and we write at ZV it is not
10898 unchanged. */
10899 && !row->ends_at_zv_p
10900 /* When first_changed_pos is the end of a continued line,
10901 row is not unchanged because it may be no longer
10902 continued. */
10903 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10904 && row->continued_p))
10905 row_found = row;
10906
10907 /* Stop if last visible row. */
10908 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10909 break;
10910
10911 ++row;
10912 }
10913
10914 return row_found;
10915 }
10916
10917
10918 /* Find the first glyph row in the current matrix of W that is not
10919 affected by changes at the end of current_buffer since the
10920 time W's current matrix was built.
10921
10922 Return in *DELTA the number of chars by which buffer positions in
10923 unchanged text at the end of current_buffer must be adjusted.
10924
10925 Return in *DELTA_BYTES the corresponding number of bytes.
10926
10927 Value is null if no such row exists, i.e. all rows are affected by
10928 changes. */
10929
10930 static struct glyph_row *
10931 find_first_unchanged_at_end_row (w, delta, delta_bytes)
10932 struct window *w;
10933 int *delta, *delta_bytes;
10934 {
10935 struct glyph_row *row;
10936 struct glyph_row *row_found = NULL;
10937
10938 *delta = *delta_bytes = 0;
10939
10940 /* Display must not have been paused, otherwise the current matrix
10941 is not up to date. */
10942 if (NILP (w->window_end_valid))
10943 abort ();
10944
10945 /* A value of window_end_pos >= END_UNCHANGED means that the window
10946 end is in the range of changed text. If so, there is no
10947 unchanged row at the end of W's current matrix. */
10948 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
10949 return NULL;
10950
10951 /* Set row to the last row in W's current matrix displaying text. */
10952 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10953
10954 /* If matrix is entirely empty, no unchanged row exists. */
10955 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10956 {
10957 /* The value of row is the last glyph row in the matrix having a
10958 meaningful buffer position in it. The end position of row
10959 corresponds to window_end_pos. This allows us to translate
10960 buffer positions in the current matrix to current buffer
10961 positions for characters not in changed text. */
10962 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10963 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10964 int last_unchanged_pos, last_unchanged_pos_old;
10965 struct glyph_row *first_text_row
10966 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10967
10968 *delta = Z - Z_old;
10969 *delta_bytes = Z_BYTE - Z_BYTE_old;
10970
10971 /* Set last_unchanged_pos to the buffer position of the last
10972 character in the buffer that has not been changed. Z is the
10973 index + 1 of the last character in current_buffer, i.e. by
10974 subtracting END_UNCHANGED we get the index of the last
10975 unchanged character, and we have to add BEG to get its buffer
10976 position. */
10977 last_unchanged_pos = Z - END_UNCHANGED + BEG;
10978 last_unchanged_pos_old = last_unchanged_pos - *delta;
10979
10980 /* Search backward from ROW for a row displaying a line that
10981 starts at a minimum position >= last_unchanged_pos_old. */
10982 for (; row > first_text_row; --row)
10983 {
10984 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
10985 abort ();
10986
10987 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10988 row_found = row;
10989 }
10990 }
10991
10992 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
10993 abort ();
10994
10995 return row_found;
10996 }
10997
10998
10999 /* Make sure that glyph rows in the current matrix of window W
11000 reference the same glyph memory as corresponding rows in the
11001 frame's frame matrix. This function is called after scrolling W's
11002 current matrix on a terminal frame in try_window_id and
11003 try_window_reusing_current_matrix. */
11004
11005 static void
11006 sync_frame_with_window_matrix_rows (w)
11007 struct window *w;
11008 {
11009 struct frame *f = XFRAME (w->frame);
11010 struct glyph_row *window_row, *window_row_end, *frame_row;
11011
11012 /* Preconditions: W must be a leaf window and full-width. Its frame
11013 must have a frame matrix. */
11014 xassert (NILP (w->hchild) && NILP (w->vchild));
11015 xassert (WINDOW_FULL_WIDTH_P (w));
11016 xassert (!FRAME_WINDOW_P (f));
11017
11018 /* If W is a full-width window, glyph pointers in W's current matrix
11019 have, by definition, to be the same as glyph pointers in the
11020 corresponding frame matrix. */
11021 window_row = w->current_matrix->rows;
11022 window_row_end = window_row + w->current_matrix->nrows;
11023 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11024 while (window_row < window_row_end)
11025 {
11026 int area;
11027
11028 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
11029 frame_row->glyphs[area] = window_row->glyphs[area];
11030
11031 /* Disable frame rows whose corresponding window rows have
11032 been disabled in try_window_id. */
11033 if (!window_row->enabled_p)
11034 frame_row->enabled_p = 0;
11035
11036 ++window_row, ++frame_row;
11037 }
11038 }
11039
11040
11041 /* Find the glyph row in window W containing CHARPOS. Consider all
11042 rows between START and END (not inclusive). END null means search
11043 all rows to the end of the display area of W. Value is the row
11044 containing CHARPOS or null. */
11045
11046 static struct glyph_row *
11047 row_containing_pos (w, charpos, start, end)
11048 struct window *w;
11049 int charpos;
11050 struct glyph_row *start, *end;
11051 {
11052 struct glyph_row *row = start;
11053 int last_y;
11054
11055 /* If we happen to start on a header-line, skip that. */
11056 if (row->mode_line_p)
11057 ++row;
11058
11059 if ((end && row >= end) || !row->enabled_p)
11060 return NULL;
11061
11062 last_y = window_text_bottom_y (w);
11063
11064 while ((end == NULL || row < end)
11065 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11066 /* The end position of a row equals the start
11067 position of the next row. If CHARPOS is there, we
11068 would rather display it in the next line, except
11069 when this line ends in ZV. */
11070 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11071 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11072 || !row->ends_at_zv_p)))
11073 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11074 ++row;
11075
11076 /* Give up if CHARPOS not found. */
11077 if ((end && row >= end)
11078 || charpos < MATRIX_ROW_START_CHARPOS (row)
11079 || charpos > MATRIX_ROW_END_CHARPOS (row))
11080 row = NULL;
11081
11082 return row;
11083 }
11084
11085
11086 /* Try to redisplay window W by reusing its existing display. W's
11087 current matrix must be up to date when this function is called,
11088 i.e. window_end_valid must not be nil.
11089
11090 Value is
11091
11092 1 if display has been updated
11093 0 if otherwise unsuccessful
11094 -1 if redisplay with same window start is known not to succeed
11095
11096 The following steps are performed:
11097
11098 1. Find the last row in the current matrix of W that is not
11099 affected by changes at the start of current_buffer. If no such row
11100 is found, give up.
11101
11102 2. Find the first row in W's current matrix that is not affected by
11103 changes at the end of current_buffer. Maybe there is no such row.
11104
11105 3. Display lines beginning with the row + 1 found in step 1 to the
11106 row found in step 2 or, if step 2 didn't find a row, to the end of
11107 the window.
11108
11109 4. If cursor is not known to appear on the window, give up.
11110
11111 5. If display stopped at the row found in step 2, scroll the
11112 display and current matrix as needed.
11113
11114 6. Maybe display some lines at the end of W, if we must. This can
11115 happen under various circumstances, like a partially visible line
11116 becoming fully visible, or because newly displayed lines are displayed
11117 in smaller font sizes.
11118
11119 7. Update W's window end information. */
11120
11121 static int
11122 try_window_id (w)
11123 struct window *w;
11124 {
11125 struct frame *f = XFRAME (w->frame);
11126 struct glyph_matrix *current_matrix = w->current_matrix;
11127 struct glyph_matrix *desired_matrix = w->desired_matrix;
11128 struct glyph_row *last_unchanged_at_beg_row;
11129 struct glyph_row *first_unchanged_at_end_row;
11130 struct glyph_row *row;
11131 struct glyph_row *bottom_row;
11132 int bottom_vpos;
11133 struct it it;
11134 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11135 struct text_pos start_pos;
11136 struct run run;
11137 int first_unchanged_at_end_vpos = 0;
11138 struct glyph_row *last_text_row, *last_text_row_at_end;
11139 struct text_pos start;
11140 int first_changed_charpos, last_changed_charpos;
11141
11142 /* This is handy for debugging. */
11143 #if 0
11144 #define GIVE_UP(X) \
11145 do { \
11146 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11147 return 0; \
11148 } while (0)
11149 #else
11150 #define GIVE_UP(X) return 0
11151 #endif
11152
11153 SET_TEXT_POS_FROM_MARKER (start, w->start);
11154
11155 /* Don't use this for mini-windows because these can show
11156 messages and mini-buffers, and we don't handle that here. */
11157 if (MINI_WINDOW_P (w))
11158 GIVE_UP (1);
11159
11160 /* This flag is used to prevent redisplay optimizations. */
11161 if (windows_or_buffers_changed)
11162 GIVE_UP (2);
11163
11164 /* Verify that narrowing has not changed. This flag is also set to prevent
11165 redisplay optimizations. It would be nice to further
11166 reduce the number of cases where this prevents try_window_id. */
11167 if (current_buffer->clip_changed)
11168 GIVE_UP (3);
11169
11170 /* Window must either use window-based redisplay or be full width. */
11171 if (!FRAME_WINDOW_P (f)
11172 && (!line_ins_del_ok
11173 || !WINDOW_FULL_WIDTH_P (w)))
11174 GIVE_UP (4);
11175
11176 /* Give up if point is not known NOT to appear in W. */
11177 if (PT < CHARPOS (start))
11178 GIVE_UP (5);
11179
11180 /* Another way to prevent redisplay optimizations. */
11181 if (XFASTINT (w->last_modified) == 0)
11182 GIVE_UP (6);
11183
11184 /* Verify that window is not hscrolled. */
11185 if (XFASTINT (w->hscroll) != 0)
11186 GIVE_UP (7);
11187
11188 /* Verify that display wasn't paused. */
11189 if (NILP (w->window_end_valid))
11190 GIVE_UP (8);
11191
11192 /* Can't use this if highlighting a region because a cursor movement
11193 will do more than just set the cursor. */
11194 if (!NILP (Vtransient_mark_mode)
11195 && !NILP (current_buffer->mark_active))
11196 GIVE_UP (9);
11197
11198 /* Likewise if highlighting trailing whitespace. */
11199 if (!NILP (Vshow_trailing_whitespace))
11200 GIVE_UP (11);
11201
11202 /* Likewise if showing a region. */
11203 if (!NILP (w->region_showing))
11204 GIVE_UP (10);
11205
11206 /* Can use this if overlay arrow position and or string have changed. */
11207 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11208 || !EQ (last_arrow_string, Voverlay_arrow_string))
11209 GIVE_UP (12);
11210
11211
11212 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11213 only if buffer has really changed. The reason is that the gap is
11214 initially at Z for freshly visited files. The code below would
11215 set end_unchanged to 0 in that case. */
11216 if (MODIFF > SAVE_MODIFF
11217 /* This seems to happen sometimes after saving a buffer. */
11218 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11219 {
11220 if (GPT - BEG < BEG_UNCHANGED)
11221 BEG_UNCHANGED = GPT - BEG;
11222 if (Z - GPT < END_UNCHANGED)
11223 END_UNCHANGED = Z - GPT;
11224 }
11225
11226 /* The position of the first and last character that has been changed. */
11227 first_changed_charpos = BEG + BEG_UNCHANGED;
11228 last_changed_charpos = Z - END_UNCHANGED;
11229
11230 /* If window starts after a line end, and the last change is in
11231 front of that newline, then changes don't affect the display.
11232 This case happens with stealth-fontification. Note that although
11233 the display is unchanged, glyph positions in the matrix have to
11234 be adjusted, of course. */
11235 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11236 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11237 && ((last_changed_charpos < CHARPOS (start)
11238 && CHARPOS (start) == BEGV)
11239 || (last_changed_charpos < CHARPOS (start) - 1
11240 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11241 {
11242 int Z_old, delta, Z_BYTE_old, delta_bytes;
11243 struct glyph_row *r0;
11244
11245 /* Compute how many chars/bytes have been added to or removed
11246 from the buffer. */
11247 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11248 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11249 delta = Z - Z_old;
11250 delta_bytes = Z_BYTE - Z_BYTE_old;
11251
11252 /* Give up if PT is not in the window. Note that it already has
11253 been checked at the start of try_window_id that PT is not in
11254 front of the window start. */
11255 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11256 GIVE_UP (13);
11257
11258 /* If window start is unchanged, we can reuse the whole matrix
11259 as is, after adjusting glyph positions. No need to compute
11260 the window end again, since its offset from Z hasn't changed. */
11261 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11262 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11263 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11264 {
11265 /* Adjust positions in the glyph matrix. */
11266 if (delta || delta_bytes)
11267 {
11268 struct glyph_row *r1
11269 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11270 increment_matrix_positions (w->current_matrix,
11271 MATRIX_ROW_VPOS (r0, current_matrix),
11272 MATRIX_ROW_VPOS (r1, current_matrix),
11273 delta, delta_bytes);
11274 }
11275
11276 /* Set the cursor. */
11277 row = row_containing_pos (w, PT, r0, NULL);
11278 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11279 return 1;
11280 }
11281 }
11282
11283 /* Handle the case that changes are all below what is displayed in
11284 the window, and that PT is in the window. This shortcut cannot
11285 be taken if ZV is visible in the window, and text has been added
11286 there that is visible in the window. */
11287 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11288 /* ZV is not visible in the window, or there are no
11289 changes at ZV, actually. */
11290 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11291 || first_changed_charpos == last_changed_charpos))
11292 {
11293 struct glyph_row *r0;
11294
11295 /* Give up if PT is not in the window. Note that it already has
11296 been checked at the start of try_window_id that PT is not in
11297 front of the window start. */
11298 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11299 GIVE_UP (14);
11300
11301 /* If window start is unchanged, we can reuse the whole matrix
11302 as is, without changing glyph positions since no text has
11303 been added/removed in front of the window end. */
11304 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11305 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11306 {
11307 /* We have to compute the window end anew since text
11308 can have been added/removed after it. */
11309 w->window_end_pos
11310 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11311 w->window_end_bytepos
11312 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11313
11314 /* Set the cursor. */
11315 row = row_containing_pos (w, PT, r0, NULL);
11316 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11317 return 2;
11318 }
11319 }
11320
11321 /* Give up if window start is in the changed area.
11322
11323 The condition used to read
11324
11325 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11326
11327 but why that was tested escapes me at the moment. */
11328 if (CHARPOS (start) >= first_changed_charpos
11329 && CHARPOS (start) <= last_changed_charpos)
11330 GIVE_UP (15);
11331
11332 /* Check that window start agrees with the start of the first glyph
11333 row in its current matrix. Check this after we know the window
11334 start is not in changed text, otherwise positions would not be
11335 comparable. */
11336 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11337 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11338 GIVE_UP (16);
11339
11340 /* Compute the position at which we have to start displaying new
11341 lines. Some of the lines at the top of the window might be
11342 reusable because they are not displaying changed text. Find the
11343 last row in W's current matrix not affected by changes at the
11344 start of current_buffer. Value is null if changes start in the
11345 first line of window. */
11346 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11347 if (last_unchanged_at_beg_row)
11348 {
11349 /* Avoid starting to display in the moddle of a character, a TAB
11350 for instance. This is easier than to set up the iterator
11351 exactly, and it's not a frequent case, so the additional
11352 effort wouldn't really pay off. */
11353 while (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11354 && last_unchanged_at_beg_row > w->current_matrix->rows)
11355 --last_unchanged_at_beg_row;
11356
11357 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11358 GIVE_UP (17);
11359
11360 init_to_row_end (&it, w, last_unchanged_at_beg_row);
11361 start_pos = it.current.pos;
11362
11363 /* Start displaying new lines in the desired matrix at the same
11364 vpos we would use in the current matrix, i.e. below
11365 last_unchanged_at_beg_row. */
11366 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11367 current_matrix);
11368 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11369 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11370
11371 xassert (it.hpos == 0 && it.current_x == 0);
11372 }
11373 else
11374 {
11375 /* There are no reusable lines at the start of the window.
11376 Start displaying in the first line. */
11377 start_display (&it, w, start);
11378 start_pos = it.current.pos;
11379 }
11380
11381 /* Find the first row that is not affected by changes at the end of
11382 the buffer. Value will be null if there is no unchanged row, in
11383 which case we must redisplay to the end of the window. delta
11384 will be set to the value by which buffer positions beginning with
11385 first_unchanged_at_end_row have to be adjusted due to text
11386 changes. */
11387 first_unchanged_at_end_row
11388 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11389 IF_DEBUG (debug_delta = delta);
11390 IF_DEBUG (debug_delta_bytes = delta_bytes);
11391
11392 /* Set stop_pos to the buffer position up to which we will have to
11393 display new lines. If first_unchanged_at_end_row != NULL, this
11394 is the buffer position of the start of the line displayed in that
11395 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11396 that we don't stop at a buffer position. */
11397 stop_pos = 0;
11398 if (first_unchanged_at_end_row)
11399 {
11400 xassert (last_unchanged_at_beg_row == NULL
11401 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11402
11403 /* If this is a continuation line, move forward to the next one
11404 that isn't. Changes in lines above affect this line.
11405 Caution: this may move first_unchanged_at_end_row to a row
11406 not displaying text. */
11407 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11408 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11409 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11410 < it.last_visible_y))
11411 ++first_unchanged_at_end_row;
11412
11413 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11414 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11415 >= it.last_visible_y))
11416 first_unchanged_at_end_row = NULL;
11417 else
11418 {
11419 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11420 + delta);
11421 first_unchanged_at_end_vpos
11422 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11423 xassert (stop_pos >= Z - END_UNCHANGED);
11424 }
11425 }
11426 else if (last_unchanged_at_beg_row == NULL)
11427 GIVE_UP (18);
11428
11429
11430 #if GLYPH_DEBUG
11431
11432 /* Either there is no unchanged row at the end, or the one we have
11433 now displays text. This is a necessary condition for the window
11434 end pos calculation at the end of this function. */
11435 xassert (first_unchanged_at_end_row == NULL
11436 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11437
11438 debug_last_unchanged_at_beg_vpos
11439 = (last_unchanged_at_beg_row
11440 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11441 : -1);
11442 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11443
11444 #endif /* GLYPH_DEBUG != 0 */
11445
11446
11447 /* Display new lines. Set last_text_row to the last new line
11448 displayed which has text on it, i.e. might end up as being the
11449 line where the window_end_vpos is. */
11450 w->cursor.vpos = -1;
11451 last_text_row = NULL;
11452 overlay_arrow_seen = 0;
11453 while (it.current_y < it.last_visible_y
11454 && !fonts_changed_p
11455 && (first_unchanged_at_end_row == NULL
11456 || IT_CHARPOS (it) < stop_pos))
11457 {
11458 if (display_line (&it))
11459 last_text_row = it.glyph_row - 1;
11460 }
11461
11462 if (fonts_changed_p)
11463 return -1;
11464
11465
11466 /* Compute differences in buffer positions, y-positions etc. for
11467 lines reused at the bottom of the window. Compute what we can
11468 scroll. */
11469 if (first_unchanged_at_end_row
11470 /* No lines reused because we displayed everything up to the
11471 bottom of the window. */
11472 && it.current_y < it.last_visible_y)
11473 {
11474 dvpos = (it.vpos
11475 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11476 current_matrix));
11477 dy = it.current_y - first_unchanged_at_end_row->y;
11478 run.current_y = first_unchanged_at_end_row->y;
11479 run.desired_y = run.current_y + dy;
11480 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11481 }
11482 else
11483 {
11484 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11485 first_unchanged_at_end_row = NULL;
11486 }
11487 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11488
11489
11490 /* Find the cursor if not already found. We have to decide whether
11491 PT will appear on this window (it sometimes doesn't, but this is
11492 not a very frequent case.) This decision has to be made before
11493 the current matrix is altered. A value of cursor.vpos < 0 means
11494 that PT is either in one of the lines beginning at
11495 first_unchanged_at_end_row or below the window. Don't care for
11496 lines that might be displayed later at the window end; as
11497 mentioned, this is not a frequent case. */
11498 if (w->cursor.vpos < 0)
11499 {
11500 /* Cursor in unchanged rows at the top? */
11501 if (PT < CHARPOS (start_pos)
11502 && last_unchanged_at_beg_row)
11503 {
11504 row = row_containing_pos (w, PT,
11505 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11506 last_unchanged_at_beg_row + 1);
11507 if (row)
11508 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11509 }
11510
11511 /* Start from first_unchanged_at_end_row looking for PT. */
11512 else if (first_unchanged_at_end_row)
11513 {
11514 row = row_containing_pos (w, PT - delta,
11515 first_unchanged_at_end_row, NULL);
11516 if (row)
11517 set_cursor_from_row (w, row, w->current_matrix, delta,
11518 delta_bytes, dy, dvpos);
11519 }
11520
11521 /* Give up if cursor was not found. */
11522 if (w->cursor.vpos < 0)
11523 {
11524 clear_glyph_matrix (w->desired_matrix);
11525 return -1;
11526 }
11527 }
11528
11529 /* Don't let the cursor end in the scroll margins. */
11530 {
11531 int this_scroll_margin, cursor_height;
11532
11533 this_scroll_margin = max (0, scroll_margin);
11534 this_scroll_margin = min (this_scroll_margin,
11535 XFASTINT (w->height) / 4);
11536 this_scroll_margin *= CANON_Y_UNIT (it.f);
11537 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11538
11539 if ((w->cursor.y < this_scroll_margin
11540 && CHARPOS (start) > BEGV)
11541 /* Don't take scroll margin into account at the bottom because
11542 old redisplay didn't do it either. */
11543 || w->cursor.y + cursor_height > it.last_visible_y)
11544 {
11545 w->cursor.vpos = -1;
11546 clear_glyph_matrix (w->desired_matrix);
11547 return -1;
11548 }
11549 }
11550
11551 /* Scroll the display. Do it before changing the current matrix so
11552 that xterm.c doesn't get confused about where the cursor glyph is
11553 found. */
11554 if (dy && run.height)
11555 {
11556 update_begin (f);
11557
11558 if (FRAME_WINDOW_P (f))
11559 {
11560 rif->update_window_begin_hook (w);
11561 rif->clear_mouse_face (w);
11562 rif->scroll_run_hook (w, &run);
11563 rif->update_window_end_hook (w, 0, 0);
11564 }
11565 else
11566 {
11567 /* Terminal frame. In this case, dvpos gives the number of
11568 lines to scroll by; dvpos < 0 means scroll up. */
11569 int first_unchanged_at_end_vpos
11570 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11571 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11572 int end = (XFASTINT (w->top)
11573 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11574 + window_internal_height (w));
11575
11576 /* Perform the operation on the screen. */
11577 if (dvpos > 0)
11578 {
11579 /* Scroll last_unchanged_at_beg_row to the end of the
11580 window down dvpos lines. */
11581 set_terminal_window (end);
11582
11583 /* On dumb terminals delete dvpos lines at the end
11584 before inserting dvpos empty lines. */
11585 if (!scroll_region_ok)
11586 ins_del_lines (end - dvpos, -dvpos);
11587
11588 /* Insert dvpos empty lines in front of
11589 last_unchanged_at_beg_row. */
11590 ins_del_lines (from, dvpos);
11591 }
11592 else if (dvpos < 0)
11593 {
11594 /* Scroll up last_unchanged_at_beg_vpos to the end of
11595 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11596 set_terminal_window (end);
11597
11598 /* Delete dvpos lines in front of
11599 last_unchanged_at_beg_vpos. ins_del_lines will set
11600 the cursor to the given vpos and emit |dvpos| delete
11601 line sequences. */
11602 ins_del_lines (from + dvpos, dvpos);
11603
11604 /* On a dumb terminal insert dvpos empty lines at the
11605 end. */
11606 if (!scroll_region_ok)
11607 ins_del_lines (end + dvpos, -dvpos);
11608 }
11609
11610 set_terminal_window (0);
11611 }
11612
11613 update_end (f);
11614 }
11615
11616 /* Shift reused rows of the current matrix to the right position.
11617 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11618 text. */
11619 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11620 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11621 if (dvpos < 0)
11622 {
11623 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11624 bottom_vpos, dvpos);
11625 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11626 bottom_vpos, 0);
11627 }
11628 else if (dvpos > 0)
11629 {
11630 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11631 bottom_vpos, dvpos);
11632 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11633 first_unchanged_at_end_vpos + dvpos, 0);
11634 }
11635
11636 /* For frame-based redisplay, make sure that current frame and window
11637 matrix are in sync with respect to glyph memory. */
11638 if (!FRAME_WINDOW_P (f))
11639 sync_frame_with_window_matrix_rows (w);
11640
11641 /* Adjust buffer positions in reused rows. */
11642 if (delta)
11643 increment_matrix_positions (current_matrix,
11644 first_unchanged_at_end_vpos + dvpos,
11645 bottom_vpos, delta, delta_bytes);
11646
11647 /* Adjust Y positions. */
11648 if (dy)
11649 shift_glyph_matrix (w, current_matrix,
11650 first_unchanged_at_end_vpos + dvpos,
11651 bottom_vpos, dy);
11652
11653 if (first_unchanged_at_end_row)
11654 first_unchanged_at_end_row += dvpos;
11655
11656 /* If scrolling up, there may be some lines to display at the end of
11657 the window. */
11658 last_text_row_at_end = NULL;
11659 if (dy < 0)
11660 {
11661 /* Set last_row to the glyph row in the current matrix where the
11662 window end line is found. It has been moved up or down in
11663 the matrix by dvpos. */
11664 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11665 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11666
11667 /* If last_row is the window end line, it should display text. */
11668 xassert (last_row->displays_text_p);
11669
11670 /* If window end line was partially visible before, begin
11671 displaying at that line. Otherwise begin displaying with the
11672 line following it. */
11673 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11674 {
11675 init_to_row_start (&it, w, last_row);
11676 it.vpos = last_vpos;
11677 it.current_y = last_row->y;
11678 }
11679 else
11680 {
11681 init_to_row_end (&it, w, last_row);
11682 it.vpos = 1 + last_vpos;
11683 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11684 ++last_row;
11685 }
11686
11687 /* We may start in a continuation line. If so, we have to get
11688 the right continuation_lines_width and current_x. */
11689 it.continuation_lines_width = last_row->continuation_lines_width;
11690 it.hpos = it.current_x = 0;
11691
11692 /* Display the rest of the lines at the window end. */
11693 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11694 while (it.current_y < it.last_visible_y
11695 && !fonts_changed_p)
11696 {
11697 /* Is it always sure that the display agrees with lines in
11698 the current matrix? I don't think so, so we mark rows
11699 displayed invalid in the current matrix by setting their
11700 enabled_p flag to zero. */
11701 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11702 if (display_line (&it))
11703 last_text_row_at_end = it.glyph_row - 1;
11704 }
11705 }
11706
11707 /* Update window_end_pos and window_end_vpos. */
11708 if (first_unchanged_at_end_row
11709 && first_unchanged_at_end_row->y < it.last_visible_y
11710 && !last_text_row_at_end)
11711 {
11712 /* Window end line if one of the preserved rows from the current
11713 matrix. Set row to the last row displaying text in current
11714 matrix starting at first_unchanged_at_end_row, after
11715 scrolling. */
11716 xassert (first_unchanged_at_end_row->displays_text_p);
11717 row = find_last_row_displaying_text (w->current_matrix, &it,
11718 first_unchanged_at_end_row);
11719 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11720
11721 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11722 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11723 w->window_end_vpos
11724 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
11725 xassert (w->window_end_bytepos >= 0);
11726 IF_DEBUG (debug_method_add (w, "A"));
11727 }
11728 else if (last_text_row_at_end)
11729 {
11730 w->window_end_pos
11731 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11732 w->window_end_bytepos
11733 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11734 w->window_end_vpos
11735 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11736 xassert (w->window_end_bytepos >= 0);
11737 IF_DEBUG (debug_method_add (w, "B"));
11738 }
11739 else if (last_text_row)
11740 {
11741 /* We have displayed either to the end of the window or at the
11742 end of the window, i.e. the last row with text is to be found
11743 in the desired matrix. */
11744 w->window_end_pos
11745 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11746 w->window_end_bytepos
11747 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11748 w->window_end_vpos
11749 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11750 xassert (w->window_end_bytepos >= 0);
11751 }
11752 else if (first_unchanged_at_end_row == NULL
11753 && last_text_row == NULL
11754 && last_text_row_at_end == NULL)
11755 {
11756 /* Displayed to end of window, but no line containing text was
11757 displayed. Lines were deleted at the end of the window. */
11758 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11759 int vpos = XFASTINT (w->window_end_vpos);
11760 struct glyph_row *current_row = current_matrix->rows + vpos;
11761 struct glyph_row *desired_row = desired_matrix->rows + vpos;
11762
11763 for (row = NULL;
11764 row == NULL && vpos >= first_vpos;
11765 --vpos, --current_row, --desired_row)
11766 {
11767 if (desired_row->enabled_p)
11768 {
11769 if (desired_row->displays_text_p)
11770 row = desired_row;
11771 }
11772 else if (current_row->displays_text_p)
11773 row = current_row;
11774 }
11775
11776 xassert (row != NULL);
11777 w->window_end_vpos = make_number (vpos + 1);
11778 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11779 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11780 xassert (w->window_end_bytepos >= 0);
11781 IF_DEBUG (debug_method_add (w, "C"));
11782 }
11783 else
11784 abort ();
11785
11786 #if 0 /* This leads to problems, for instance when the cursor is
11787 at ZV, and the cursor line displays no text. */
11788 /* Disable rows below what's displayed in the window. This makes
11789 debugging easier. */
11790 enable_glyph_matrix_rows (current_matrix,
11791 XFASTINT (w->window_end_vpos) + 1,
11792 bottom_vpos, 0);
11793 #endif
11794
11795 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11796 debug_end_vpos = XFASTINT (w->window_end_vpos));
11797
11798 /* Record that display has not been completed. */
11799 w->window_end_valid = Qnil;
11800 w->desired_matrix->no_scrolling_p = 1;
11801 return 3;
11802
11803 #undef GIVE_UP
11804 }
11805
11806
11807 \f
11808 /***********************************************************************
11809 More debugging support
11810 ***********************************************************************/
11811
11812 #if GLYPH_DEBUG
11813
11814 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
11815 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11816 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
11817
11818
11819 /* Dump the contents of glyph matrix MATRIX on stderr.
11820
11821 GLYPHS 0 means don't show glyph contents.
11822 GLYPHS 1 means show glyphs in short form
11823 GLYPHS > 1 means show glyphs in long form. */
11824
11825 void
11826 dump_glyph_matrix (matrix, glyphs)
11827 struct glyph_matrix *matrix;
11828 int glyphs;
11829 {
11830 int i;
11831 for (i = 0; i < matrix->nrows; ++i)
11832 dump_glyph_row (matrix, i, glyphs);
11833 }
11834
11835
11836 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
11837 the glyph row and area where the glyph comes from. */
11838
11839 void
11840 dump_glyph (row, glyph, area)
11841 struct glyph_row *row;
11842 struct glyph *glyph;
11843 int area;
11844 {
11845 if (glyph->type == CHAR_GLYPH)
11846 {
11847 fprintf (stderr,
11848 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11849 glyph - row->glyphs[TEXT_AREA],
11850 'C',
11851 glyph->charpos,
11852 (BUFFERP (glyph->object)
11853 ? 'B'
11854 : (STRINGP (glyph->object)
11855 ? 'S'
11856 : '-')),
11857 glyph->pixel_width,
11858 glyph->u.ch,
11859 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11860 ? glyph->u.ch
11861 : '.'),
11862 glyph->face_id,
11863 glyph->left_box_line_p,
11864 glyph->right_box_line_p);
11865 }
11866 else if (glyph->type == STRETCH_GLYPH)
11867 {
11868 fprintf (stderr,
11869 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11870 glyph - row->glyphs[TEXT_AREA],
11871 'S',
11872 glyph->charpos,
11873 (BUFFERP (glyph->object)
11874 ? 'B'
11875 : (STRINGP (glyph->object)
11876 ? 'S'
11877 : '-')),
11878 glyph->pixel_width,
11879 0,
11880 '.',
11881 glyph->face_id,
11882 glyph->left_box_line_p,
11883 glyph->right_box_line_p);
11884 }
11885 else if (glyph->type == IMAGE_GLYPH)
11886 {
11887 fprintf (stderr,
11888 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11889 glyph - row->glyphs[TEXT_AREA],
11890 'I',
11891 glyph->charpos,
11892 (BUFFERP (glyph->object)
11893 ? 'B'
11894 : (STRINGP (glyph->object)
11895 ? 'S'
11896 : '-')),
11897 glyph->pixel_width,
11898 glyph->u.img_id,
11899 '.',
11900 glyph->face_id,
11901 glyph->left_box_line_p,
11902 glyph->right_box_line_p);
11903 }
11904 }
11905
11906
11907 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
11908 GLYPHS 0 means don't show glyph contents.
11909 GLYPHS 1 means show glyphs in short form
11910 GLYPHS > 1 means show glyphs in long form. */
11911
11912 void
11913 dump_glyph_row (matrix, vpos, glyphs)
11914 struct glyph_matrix *matrix;
11915 int vpos, glyphs;
11916 {
11917 struct glyph_row *row;
11918
11919 if (vpos < 0 || vpos >= matrix->nrows)
11920 return;
11921
11922 row = MATRIX_ROW (matrix, vpos);
11923
11924 if (glyphs != 1)
11925 {
11926 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
11927 fprintf (stderr, "=======================================================================\n");
11928
11929 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
11930 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
11931 row - matrix->rows,
11932 MATRIX_ROW_START_CHARPOS (row),
11933 MATRIX_ROW_END_CHARPOS (row),
11934 row->used[TEXT_AREA],
11935 row->contains_overlapping_glyphs_p,
11936 row->enabled_p,
11937 row->inverse_p,
11938 row->truncated_on_left_p,
11939 row->truncated_on_right_p,
11940 row->overlay_arrow_p,
11941 row->continued_p,
11942 MATRIX_ROW_CONTINUATION_LINE_P (row),
11943 row->displays_text_p,
11944 row->ends_at_zv_p,
11945 row->fill_line_p,
11946 row->ends_in_middle_of_char_p,
11947 row->starts_in_middle_of_char_p,
11948 row->mouse_face_p,
11949 row->x,
11950 row->y,
11951 row->pixel_width,
11952 row->height,
11953 row->visible_height,
11954 row->ascent,
11955 row->phys_ascent);
11956 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
11957 row->end.overlay_string_index,
11958 row->continuation_lines_width);
11959 fprintf (stderr, "%9d %5d\n",
11960 CHARPOS (row->start.string_pos),
11961 CHARPOS (row->end.string_pos));
11962 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11963 row->end.dpvec_index);
11964 }
11965
11966 if (glyphs > 1)
11967 {
11968 int area;
11969
11970 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11971 {
11972 struct glyph *glyph = row->glyphs[area];
11973 struct glyph *glyph_end = glyph + row->used[area];
11974
11975 /* Glyph for a line end in text. */
11976 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
11977 ++glyph_end;
11978
11979 if (glyph < glyph_end)
11980 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
11981
11982 for (; glyph < glyph_end; ++glyph)
11983 dump_glyph (row, glyph, area);
11984 }
11985 }
11986 else if (glyphs == 1)
11987 {
11988 int area;
11989
11990 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11991 {
11992 char *s = (char *) alloca (row->used[area] + 1);
11993 int i;
11994
11995 for (i = 0; i < row->used[area]; ++i)
11996 {
11997 struct glyph *glyph = row->glyphs[area] + i;
11998 if (glyph->type == CHAR_GLYPH
11999 && glyph->u.ch < 0x80
12000 && glyph->u.ch >= ' ')
12001 s[i] = glyph->u.ch;
12002 else
12003 s[i] = '.';
12004 }
12005
12006 s[i] = '\0';
12007 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12008 }
12009 }
12010 }
12011
12012
12013 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12014 Sdump_glyph_matrix, 0, 1, "p",
12015 "Dump the current matrix of the selected window to stderr.\n\
12016 Shows contents of glyph row structures. With non-nil\n\
12017 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show\n\
12018 glyphs in short form, otherwise show glyphs in long form.")
12019 (glyphs)
12020 Lisp_Object glyphs;
12021 {
12022 struct window *w = XWINDOW (selected_window);
12023 struct buffer *buffer = XBUFFER (w->buffer);
12024
12025 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12026 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12027 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12028 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12029 fprintf (stderr, "=============================================\n");
12030 dump_glyph_matrix (w->current_matrix,
12031 NILP (glyphs) ? 0 : XINT (glyphs));
12032 return Qnil;
12033 }
12034
12035
12036 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12037 "Dump glyph row ROW to stderr.\n\
12038 GLYPH 0 means don't dump glyphs.\n\
12039 GLYPH 1 means dump glyphs in short form.\n\
12040 GLYPH > 1 or omitted means dump glyphs in long form.")
12041 (row, glyphs)
12042 Lisp_Object row, glyphs;
12043 {
12044 CHECK_NUMBER (row, 0);
12045 dump_glyph_row (XWINDOW (selected_window)->current_matrix,
12046 XINT (row),
12047 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12048 return Qnil;
12049 }
12050
12051
12052 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12053 "Dump glyph row ROW of the tool-bar of the current frame to stderr.\n\
12054 GLYPH 0 means don't dump glyphs.\n\
12055 GLYPH 1 means dump glyphs in short form.\n\
12056 GLYPH > 1 or omitted means dump glyphs in long form.")
12057 (row, glyphs)
12058 Lisp_Object row, glyphs;
12059 {
12060 struct frame *sf = SELECTED_FRAME ();
12061 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)->current_matrix);
12062 dump_glyph_row (m, XINT (row), INTEGERP (glyphs) ? XINT (glyphs) : 2);
12063 return Qnil;
12064 }
12065
12066
12067 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12068 "Toggle tracing of redisplay.\n\
12069 With ARG, turn tracing on if and only if ARG is positive.")
12070 (arg)
12071 Lisp_Object arg;
12072 {
12073 if (NILP (arg))
12074 trace_redisplay_p = !trace_redisplay_p;
12075 else
12076 {
12077 arg = Fprefix_numeric_value (arg);
12078 trace_redisplay_p = XINT (arg) > 0;
12079 }
12080
12081 return Qnil;
12082 }
12083
12084
12085 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
12086 "Print STRING to stderr.")
12087 (string)
12088 Lisp_Object string;
12089 {
12090 CHECK_STRING (string, 0);
12091 fprintf (stderr, "%s", XSTRING (string)->data);
12092 return Qnil;
12093 }
12094
12095 #endif /* GLYPH_DEBUG */
12096
12097
12098 \f
12099 /***********************************************************************
12100 Building Desired Matrix Rows
12101 ***********************************************************************/
12102
12103 /* Return a temporary glyph row holding the glyphs of an overlay
12104 arrow. Only used for non-window-redisplay windows. */
12105
12106 static struct glyph_row *
12107 get_overlay_arrow_glyph_row (w)
12108 struct window *w;
12109 {
12110 struct frame *f = XFRAME (WINDOW_FRAME (w));
12111 struct buffer *buffer = XBUFFER (w->buffer);
12112 struct buffer *old = current_buffer;
12113 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
12114 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
12115 unsigned char *arrow_end = arrow_string + arrow_len;
12116 unsigned char *p;
12117 struct it it;
12118 int multibyte_p;
12119 int n_glyphs_before;
12120
12121 set_buffer_temp (buffer);
12122 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12123 it.glyph_row->used[TEXT_AREA] = 0;
12124 SET_TEXT_POS (it.position, 0, 0);
12125
12126 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12127 p = arrow_string;
12128 while (p < arrow_end)
12129 {
12130 Lisp_Object face, ilisp;
12131
12132 /* Get the next character. */
12133 if (multibyte_p)
12134 it.c = string_char_and_length (p, arrow_len, &it.len);
12135 else
12136 it.c = *p, it.len = 1;
12137 p += it.len;
12138
12139 /* Get its face. */
12140 ilisp = make_number (p - arrow_string);
12141 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12142 it.face_id = compute_char_face (f, it.c, face);
12143
12144 /* Compute its width, get its glyphs. */
12145 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12146 SET_TEXT_POS (it.position, -1, -1);
12147 PRODUCE_GLYPHS (&it);
12148
12149 /* If this character doesn't fit any more in the line, we have
12150 to remove some glyphs. */
12151 if (it.current_x > it.last_visible_x)
12152 {
12153 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12154 break;
12155 }
12156 }
12157
12158 set_buffer_temp (old);
12159 return it.glyph_row;
12160 }
12161
12162
12163 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12164 glyphs are only inserted for terminal frames since we can't really
12165 win with truncation glyphs when partially visible glyphs are
12166 involved. Which glyphs to insert is determined by
12167 produce_special_glyphs. */
12168
12169 static void
12170 insert_left_trunc_glyphs (it)
12171 struct it *it;
12172 {
12173 struct it truncate_it;
12174 struct glyph *from, *end, *to, *toend;
12175
12176 xassert (!FRAME_WINDOW_P (it->f));
12177
12178 /* Get the truncation glyphs. */
12179 truncate_it = *it;
12180 truncate_it.current_x = 0;
12181 truncate_it.face_id = DEFAULT_FACE_ID;
12182 truncate_it.glyph_row = &scratch_glyph_row;
12183 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12184 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12185 truncate_it.object = make_number (0);
12186 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12187
12188 /* Overwrite glyphs from IT with truncation glyphs. */
12189 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12190 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12191 to = it->glyph_row->glyphs[TEXT_AREA];
12192 toend = to + it->glyph_row->used[TEXT_AREA];
12193
12194 while (from < end)
12195 *to++ = *from++;
12196
12197 /* There may be padding glyphs left over. Overwrite them too. */
12198 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12199 {
12200 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12201 while (from < end)
12202 *to++ = *from++;
12203 }
12204
12205 if (to > toend)
12206 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12207 }
12208
12209
12210 /* Compute the pixel height and width of IT->glyph_row.
12211
12212 Most of the time, ascent and height of a display line will be equal
12213 to the max_ascent and max_height values of the display iterator
12214 structure. This is not the case if
12215
12216 1. We hit ZV without displaying anything. In this case, max_ascent
12217 and max_height will be zero.
12218
12219 2. We have some glyphs that don't contribute to the line height.
12220 (The glyph row flag contributes_to_line_height_p is for future
12221 pixmap extensions).
12222
12223 The first case is easily covered by using default values because in
12224 these cases, the line height does not really matter, except that it
12225 must not be zero. */
12226
12227 static void
12228 compute_line_metrics (it)
12229 struct it *it;
12230 {
12231 struct glyph_row *row = it->glyph_row;
12232 int area, i;
12233
12234 if (FRAME_WINDOW_P (it->f))
12235 {
12236 int i, header_line_height;
12237
12238 /* The line may consist of one space only, that was added to
12239 place the cursor on it. If so, the row's height hasn't been
12240 computed yet. */
12241 if (row->height == 0)
12242 {
12243 if (it->max_ascent + it->max_descent == 0)
12244 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12245 row->ascent = it->max_ascent;
12246 row->height = it->max_ascent + it->max_descent;
12247 row->phys_ascent = it->max_phys_ascent;
12248 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12249 }
12250
12251 /* Compute the width of this line. */
12252 row->pixel_width = row->x;
12253 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12254 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12255
12256 xassert (row->pixel_width >= 0);
12257 xassert (row->ascent >= 0 && row->height > 0);
12258
12259 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12260 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12261
12262 /* If first line's physical ascent is larger than its logical
12263 ascent, use the physical ascent, and make the row taller.
12264 This makes accented characters fully visible. */
12265 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12266 && row->phys_ascent > row->ascent)
12267 {
12268 row->height += row->phys_ascent - row->ascent;
12269 row->ascent = row->phys_ascent;
12270 }
12271
12272 /* Compute how much of the line is visible. */
12273 row->visible_height = row->height;
12274
12275 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12276 if (row->y < header_line_height)
12277 row->visible_height -= header_line_height - row->y;
12278 else
12279 {
12280 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12281 if (row->y + row->height > max_y)
12282 row->visible_height -= row->y + row->height - max_y;
12283 }
12284 }
12285 else
12286 {
12287 row->pixel_width = row->used[TEXT_AREA];
12288 if (row->continued_p)
12289 row->pixel_width -= it->continuation_pixel_width;
12290 else if (row->truncated_on_right_p)
12291 row->pixel_width -= it->truncation_pixel_width;
12292 row->ascent = row->phys_ascent = 0;
12293 row->height = row->phys_height = row->visible_height = 1;
12294 }
12295
12296 /* Compute a hash code for this row. */
12297 row->hash = 0;
12298 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12299 for (i = 0; i < row->used[area]; ++i)
12300 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12301 + row->glyphs[area][i].u.val
12302 + row->glyphs[area][i].face_id
12303 + row->glyphs[area][i].padding_p
12304 + (row->glyphs[area][i].type << 2));
12305
12306 it->max_ascent = it->max_descent = 0;
12307 it->max_phys_ascent = it->max_phys_descent = 0;
12308 }
12309
12310
12311 /* Append one space to the glyph row of iterator IT if doing a
12312 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12313 space have the default face, otherwise let it have the same face as
12314 IT->face_id. Value is non-zero if a space was added.
12315
12316 This function is called to make sure that there is always one glyph
12317 at the end of a glyph row that the cursor can be set on under
12318 window-systems. (If there weren't such a glyph we would not know
12319 how wide and tall a box cursor should be displayed).
12320
12321 At the same time this space let's a nicely handle clearing to the
12322 end of the line if the row ends in italic text. */
12323
12324 static int
12325 append_space (it, default_face_p)
12326 struct it *it;
12327 int default_face_p;
12328 {
12329 if (FRAME_WINDOW_P (it->f))
12330 {
12331 int n = it->glyph_row->used[TEXT_AREA];
12332
12333 if (it->glyph_row->glyphs[TEXT_AREA] + n
12334 < it->glyph_row->glyphs[1 + TEXT_AREA])
12335 {
12336 /* Save some values that must not be changed.
12337 Must save IT->c and IT->len because otherwise
12338 ITERATOR_AT_END_P wouldn't work anymore after
12339 append_space has been called. */
12340 enum display_element_type saved_what = it->what;
12341 int saved_c = it->c, saved_len = it->len;
12342 int saved_x = it->current_x;
12343 int saved_face_id = it->face_id;
12344 struct text_pos saved_pos;
12345 Lisp_Object saved_object;
12346 struct face *face;
12347
12348 saved_object = it->object;
12349 saved_pos = it->position;
12350
12351 it->what = IT_CHARACTER;
12352 bzero (&it->position, sizeof it->position);
12353 it->object = make_number (0);
12354 it->c = ' ';
12355 it->len = 1;
12356
12357 if (default_face_p)
12358 it->face_id = DEFAULT_FACE_ID;
12359 else if (it->face_before_selective_p)
12360 it->face_id = it->saved_face_id;
12361 face = FACE_FROM_ID (it->f, it->face_id);
12362 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12363
12364 PRODUCE_GLYPHS (it);
12365
12366 it->current_x = saved_x;
12367 it->object = saved_object;
12368 it->position = saved_pos;
12369 it->what = saved_what;
12370 it->face_id = saved_face_id;
12371 it->len = saved_len;
12372 it->c = saved_c;
12373 return 1;
12374 }
12375 }
12376
12377 return 0;
12378 }
12379
12380
12381 /* Extend the face of the last glyph in the text area of IT->glyph_row
12382 to the end of the display line. Called from display_line.
12383 If the glyph row is empty, add a space glyph to it so that we
12384 know the face to draw. Set the glyph row flag fill_line_p. */
12385
12386 static void
12387 extend_face_to_end_of_line (it)
12388 struct it *it;
12389 {
12390 struct face *face;
12391 struct frame *f = it->f;
12392
12393 /* If line is already filled, do nothing. */
12394 if (it->current_x >= it->last_visible_x)
12395 return;
12396
12397 /* Face extension extends the background and box of IT->face_id
12398 to the end of the line. If the background equals the background
12399 of the frame, we don't have to do anything. */
12400 if (it->face_before_selective_p)
12401 face = FACE_FROM_ID (it->f, it->saved_face_id);
12402 else
12403 face = FACE_FROM_ID (f, it->face_id);
12404
12405 if (FRAME_WINDOW_P (f)
12406 && face->box == FACE_NO_BOX
12407 && face->background == FRAME_BACKGROUND_PIXEL (f)
12408 && !face->stipple)
12409 return;
12410
12411 /* Set the glyph row flag indicating that the face of the last glyph
12412 in the text area has to be drawn to the end of the text area. */
12413 it->glyph_row->fill_line_p = 1;
12414
12415 /* If current character of IT is not ASCII, make sure we have the
12416 ASCII face. This will be automatically undone the next time
12417 get_next_display_element returns a multibyte character. Note
12418 that the character will always be single byte in unibyte text. */
12419 if (!SINGLE_BYTE_CHAR_P (it->c))
12420 {
12421 it->face_id = FACE_FOR_CHAR (f, face, 0);
12422 }
12423
12424 if (FRAME_WINDOW_P (f))
12425 {
12426 /* If the row is empty, add a space with the current face of IT,
12427 so that we know which face to draw. */
12428 if (it->glyph_row->used[TEXT_AREA] == 0)
12429 {
12430 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12431 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12432 it->glyph_row->used[TEXT_AREA] = 1;
12433 }
12434 }
12435 else
12436 {
12437 /* Save some values that must not be changed. */
12438 int saved_x = it->current_x;
12439 struct text_pos saved_pos;
12440 Lisp_Object saved_object;
12441 enum display_element_type saved_what = it->what;
12442 int saved_face_id = it->face_id;
12443
12444 saved_object = it->object;
12445 saved_pos = it->position;
12446
12447 it->what = IT_CHARACTER;
12448 bzero (&it->position, sizeof it->position);
12449 it->object = make_number (0);
12450 it->c = ' ';
12451 it->len = 1;
12452 it->face_id = face->id;
12453
12454 PRODUCE_GLYPHS (it);
12455
12456 while (it->current_x <= it->last_visible_x)
12457 PRODUCE_GLYPHS (it);
12458
12459 /* Don't count these blanks really. It would let us insert a left
12460 truncation glyph below and make us set the cursor on them, maybe. */
12461 it->current_x = saved_x;
12462 it->object = saved_object;
12463 it->position = saved_pos;
12464 it->what = saved_what;
12465 it->face_id = saved_face_id;
12466 }
12467 }
12468
12469
12470 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12471 trailing whitespace. */
12472
12473 static int
12474 trailing_whitespace_p (charpos)
12475 int charpos;
12476 {
12477 int bytepos = CHAR_TO_BYTE (charpos);
12478 int c = 0;
12479
12480 while (bytepos < ZV_BYTE
12481 && (c = FETCH_CHAR (bytepos),
12482 c == ' ' || c == '\t'))
12483 ++bytepos;
12484
12485 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12486 {
12487 if (bytepos != PT_BYTE)
12488 return 1;
12489 }
12490 return 0;
12491 }
12492
12493
12494 /* Highlight trailing whitespace, if any, in ROW. */
12495
12496 void
12497 highlight_trailing_whitespace (f, row)
12498 struct frame *f;
12499 struct glyph_row *row;
12500 {
12501 int used = row->used[TEXT_AREA];
12502
12503 if (used)
12504 {
12505 struct glyph *start = row->glyphs[TEXT_AREA];
12506 struct glyph *glyph = start + used - 1;
12507
12508 /* Skip over glyphs inserted to display the cursor at the
12509 end of a line, for extending the face of the last glyph
12510 to the end of the line on terminals, and for truncation
12511 and continuation glyphs. */
12512 while (glyph >= start
12513 && glyph->type == CHAR_GLYPH
12514 && INTEGERP (glyph->object))
12515 --glyph;
12516
12517 /* If last glyph is a space or stretch, and it's trailing
12518 whitespace, set the face of all trailing whitespace glyphs in
12519 IT->glyph_row to `trailing-whitespace'. */
12520 if (glyph >= start
12521 && BUFFERP (glyph->object)
12522 && (glyph->type == STRETCH_GLYPH
12523 || (glyph->type == CHAR_GLYPH
12524 && glyph->u.ch == ' '))
12525 && trailing_whitespace_p (glyph->charpos))
12526 {
12527 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12528
12529 while (glyph >= start
12530 && BUFFERP (glyph->object)
12531 && (glyph->type == STRETCH_GLYPH
12532 || (glyph->type == CHAR_GLYPH
12533 && glyph->u.ch == ' ')))
12534 (glyph--)->face_id = face_id;
12535 }
12536 }
12537 }
12538
12539
12540 /* Value is non-zero if glyph row ROW in window W should be
12541 used to hold the cursor. */
12542
12543 static int
12544 cursor_row_p (w, row)
12545 struct window *w;
12546 struct glyph_row *row;
12547 {
12548 int cursor_row_p = 1;
12549
12550 if (PT == MATRIX_ROW_END_CHARPOS (row))
12551 {
12552 /* If the row ends with a newline from a string, we don't want
12553 the cursor there (if the row is continued it doesn't end in a
12554 newline). */
12555 if (CHARPOS (row->end.string_pos) >= 0
12556 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12557 cursor_row_p = row->continued_p;
12558
12559 /* If the row ends at ZV, display the cursor at the end of that
12560 row instead of at the start of the row below. */
12561 else if (row->ends_at_zv_p)
12562 cursor_row_p = 1;
12563 else
12564 cursor_row_p = 0;
12565 }
12566
12567 return cursor_row_p;
12568 }
12569
12570
12571 /* Construct the glyph row IT->glyph_row in the desired matrix of
12572 IT->w from text at the current position of IT. See dispextern.h
12573 for an overview of struct it. Value is non-zero if
12574 IT->glyph_row displays text, as opposed to a line displaying ZV
12575 only. */
12576
12577 static int
12578 display_line (it)
12579 struct it *it;
12580 {
12581 struct glyph_row *row = it->glyph_row;
12582
12583 /* We always start displaying at hpos zero even if hscrolled. */
12584 xassert (it->hpos == 0 && it->current_x == 0);
12585
12586 /* We must not display in a row that's not a text row. */
12587 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12588 < it->w->desired_matrix->nrows);
12589
12590 /* Is IT->w showing the region? */
12591 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12592
12593 /* Clear the result glyph row and enable it. */
12594 prepare_desired_row (row);
12595
12596 row->y = it->current_y;
12597 row->start = it->current;
12598 row->continuation_lines_width = it->continuation_lines_width;
12599 row->displays_text_p = 1;
12600 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12601 it->starts_in_middle_of_char_p = 0;
12602
12603 /* Arrange the overlays nicely for our purposes. Usually, we call
12604 display_line on only one line at a time, in which case this
12605 can't really hurt too much, or we call it on lines which appear
12606 one after another in the buffer, in which case all calls to
12607 recenter_overlay_lists but the first will be pretty cheap. */
12608 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12609
12610 /* Move over display elements that are not visible because we are
12611 hscrolled. This may stop at an x-position < IT->first_visible_x
12612 if the first glyph is partially visible or if we hit a line end. */
12613 if (it->current_x < it->first_visible_x)
12614 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12615 MOVE_TO_POS | MOVE_TO_X);
12616
12617 /* Get the initial row height. This is either the height of the
12618 text hscrolled, if there is any, or zero. */
12619 row->ascent = it->max_ascent;
12620 row->height = it->max_ascent + it->max_descent;
12621 row->phys_ascent = it->max_phys_ascent;
12622 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12623
12624 /* Loop generating characters. The loop is left with IT on the next
12625 character to display. */
12626 while (1)
12627 {
12628 int n_glyphs_before, hpos_before, x_before;
12629 int x, i, nglyphs;
12630 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12631
12632 /* Retrieve the next thing to display. Value is zero if end of
12633 buffer reached. */
12634 if (!get_next_display_element (it))
12635 {
12636 /* Maybe add a space at the end of this line that is used to
12637 display the cursor there under X. Set the charpos of the
12638 first glyph of blank lines not corresponding to any text
12639 to -1. */
12640 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12641 || row->used[TEXT_AREA] == 0)
12642 {
12643 row->glyphs[TEXT_AREA]->charpos = -1;
12644 row->displays_text_p = 0;
12645
12646 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
12647 row->indicate_empty_line_p = 1;
12648 }
12649
12650 it->continuation_lines_width = 0;
12651 row->ends_at_zv_p = 1;
12652 break;
12653 }
12654
12655 /* Now, get the metrics of what we want to display. This also
12656 generates glyphs in `row' (which is IT->glyph_row). */
12657 n_glyphs_before = row->used[TEXT_AREA];
12658 x = it->current_x;
12659
12660 /* Remember the line height so far in case the next element doesn't
12661 fit on the line. */
12662 if (!it->truncate_lines_p)
12663 {
12664 ascent = it->max_ascent;
12665 descent = it->max_descent;
12666 phys_ascent = it->max_phys_ascent;
12667 phys_descent = it->max_phys_descent;
12668 }
12669
12670 PRODUCE_GLYPHS (it);
12671
12672 /* If this display element was in marginal areas, continue with
12673 the next one. */
12674 if (it->area != TEXT_AREA)
12675 {
12676 row->ascent = max (row->ascent, it->max_ascent);
12677 row->height = max (row->height, it->max_ascent + it->max_descent);
12678 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12679 row->phys_height = max (row->phys_height,
12680 it->max_phys_ascent + it->max_phys_descent);
12681 set_iterator_to_next (it, 1);
12682 continue;
12683 }
12684
12685 /* Does the display element fit on the line? If we truncate
12686 lines, we should draw past the right edge of the window. If
12687 we don't truncate, we want to stop so that we can display the
12688 continuation glyph before the right margin. If lines are
12689 continued, there are two possible strategies for characters
12690 resulting in more than 1 glyph (e.g. tabs): Display as many
12691 glyphs as possible in this line and leave the rest for the
12692 continuation line, or display the whole element in the next
12693 line. Original redisplay did the former, so we do it also. */
12694 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12695 hpos_before = it->hpos;
12696 x_before = x;
12697
12698 if (/* Not a newline. */
12699 nglyphs > 0
12700 /* Glyphs produced fit entirely in the line. */
12701 && it->current_x < it->last_visible_x)
12702 {
12703 it->hpos += nglyphs;
12704 row->ascent = max (row->ascent, it->max_ascent);
12705 row->height = max (row->height, it->max_ascent + it->max_descent);
12706 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12707 row->phys_height = max (row->phys_height,
12708 it->max_phys_ascent + it->max_phys_descent);
12709 if (it->current_x - it->pixel_width < it->first_visible_x)
12710 row->x = x - it->first_visible_x;
12711 }
12712 else
12713 {
12714 int new_x;
12715 struct glyph *glyph;
12716
12717 for (i = 0; i < nglyphs; ++i, x = new_x)
12718 {
12719 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12720 new_x = x + glyph->pixel_width;
12721
12722 if (/* Lines are continued. */
12723 !it->truncate_lines_p
12724 && (/* Glyph doesn't fit on the line. */
12725 new_x > it->last_visible_x
12726 /* Or it fits exactly on a window system frame. */
12727 || (new_x == it->last_visible_x
12728 && FRAME_WINDOW_P (it->f))))
12729 {
12730 /* End of a continued line. */
12731
12732 if (it->hpos == 0
12733 || (new_x == it->last_visible_x
12734 && FRAME_WINDOW_P (it->f)))
12735 {
12736 /* Current glyph is the only one on the line or
12737 fits exactly on the line. We must continue
12738 the line because we can't draw the cursor
12739 after the glyph. */
12740 row->continued_p = 1;
12741 it->current_x = new_x;
12742 it->continuation_lines_width += new_x;
12743 ++it->hpos;
12744 if (i == nglyphs - 1)
12745 set_iterator_to_next (it, 1);
12746 }
12747 else if (CHAR_GLYPH_PADDING_P (*glyph)
12748 && !FRAME_WINDOW_P (it->f))
12749 {
12750 /* A padding glyph that doesn't fit on this line.
12751 This means the whole character doesn't fit
12752 on the line. */
12753 row->used[TEXT_AREA] = n_glyphs_before;
12754
12755 /* Fill the rest of the row with continuation
12756 glyphs like in 20.x. */
12757 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12758 < row->glyphs[1 + TEXT_AREA])
12759 produce_special_glyphs (it, IT_CONTINUATION);
12760
12761 row->continued_p = 1;
12762 it->current_x = x_before;
12763 it->continuation_lines_width += x_before;
12764
12765 /* Restore the height to what it was before the
12766 element not fitting on the line. */
12767 it->max_ascent = ascent;
12768 it->max_descent = descent;
12769 it->max_phys_ascent = phys_ascent;
12770 it->max_phys_descent = phys_descent;
12771 }
12772 else
12773 {
12774 /* Display element draws past the right edge of
12775 the window. Restore positions to values
12776 before the element. The next line starts
12777 with current_x before the glyph that could
12778 not be displayed, so that TAB works right. */
12779 row->used[TEXT_AREA] = n_glyphs_before + i;
12780
12781 /* Display continuation glyphs. */
12782 if (!FRAME_WINDOW_P (it->f))
12783 produce_special_glyphs (it, IT_CONTINUATION);
12784 row->continued_p = 1;
12785
12786 it->current_x = x;
12787 it->continuation_lines_width += x;
12788 if (nglyphs > 1 && i > 0)
12789 {
12790 row->ends_in_middle_of_char_p = 1;
12791 it->starts_in_middle_of_char_p = 1;
12792 }
12793
12794 /* Restore the height to what it was before the
12795 element not fitting on the line. */
12796 it->max_ascent = ascent;
12797 it->max_descent = descent;
12798 it->max_phys_ascent = phys_ascent;
12799 it->max_phys_descent = phys_descent;
12800 }
12801
12802 break;
12803 }
12804 else if (new_x > it->first_visible_x)
12805 {
12806 /* Increment number of glyphs actually displayed. */
12807 ++it->hpos;
12808
12809 if (x < it->first_visible_x)
12810 /* Glyph is partially visible, i.e. row starts at
12811 negative X position. */
12812 row->x = x - it->first_visible_x;
12813 }
12814 else
12815 {
12816 /* Glyph is completely off the left margin of the
12817 window. This should not happen because of the
12818 move_it_in_display_line at the start of
12819 this function. */
12820 abort ();
12821 }
12822 }
12823
12824 row->ascent = max (row->ascent, it->max_ascent);
12825 row->height = max (row->height, it->max_ascent + it->max_descent);
12826 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12827 row->phys_height = max (row->phys_height,
12828 it->max_phys_ascent + it->max_phys_descent);
12829
12830 /* End of this display line if row is continued. */
12831 if (row->continued_p)
12832 break;
12833 }
12834
12835 /* Is this a line end? If yes, we're also done, after making
12836 sure that a non-default face is extended up to the right
12837 margin of the window. */
12838 if (ITERATOR_AT_END_OF_LINE_P (it))
12839 {
12840 int used_before = row->used[TEXT_AREA];
12841
12842 /* Add a space at the end of the line that is used to
12843 display the cursor there. */
12844 append_space (it, 0);
12845
12846 /* Extend the face to the end of the line. */
12847 extend_face_to_end_of_line (it);
12848
12849 /* Make sure we have the position. */
12850 if (used_before == 0)
12851 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
12852
12853 /* Consume the line end. This skips over invisible lines. */
12854 set_iterator_to_next (it, 1);
12855 it->continuation_lines_width = 0;
12856 break;
12857 }
12858
12859 /* Proceed with next display element. Note that this skips
12860 over lines invisible because of selective display. */
12861 set_iterator_to_next (it, 1);
12862
12863 /* If we truncate lines, we are done when the last displayed
12864 glyphs reach past the right margin of the window. */
12865 if (it->truncate_lines_p
12866 && (FRAME_WINDOW_P (it->f)
12867 ? (it->current_x >= it->last_visible_x)
12868 : (it->current_x > it->last_visible_x)))
12869 {
12870 /* Maybe add truncation glyphs. */
12871 if (!FRAME_WINDOW_P (it->f))
12872 {
12873 int i, n;
12874
12875 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
12876 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
12877 break;
12878
12879 for (n = row->used[TEXT_AREA]; i < n; ++i)
12880 {
12881 row->used[TEXT_AREA] = i;
12882 produce_special_glyphs (it, IT_TRUNCATION);
12883 }
12884 }
12885
12886 row->truncated_on_right_p = 1;
12887 it->continuation_lines_width = 0;
12888 reseat_at_next_visible_line_start (it, 0);
12889 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12890 it->hpos = hpos_before;
12891 it->current_x = x_before;
12892 break;
12893 }
12894 }
12895
12896 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12897 at the left window margin. */
12898 if (it->first_visible_x
12899 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12900 {
12901 if (!FRAME_WINDOW_P (it->f))
12902 insert_left_trunc_glyphs (it);
12903 row->truncated_on_left_p = 1;
12904 }
12905
12906 /* If the start of this line is the overlay arrow-position, then
12907 mark this glyph row as the one containing the overlay arrow.
12908 This is clearly a mess with variable size fonts. It would be
12909 better to let it be displayed like cursors under X. */
12910 if (MARKERP (Voverlay_arrow_position)
12911 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
12912 && (MATRIX_ROW_START_CHARPOS (row)
12913 == marker_position (Voverlay_arrow_position))
12914 && STRINGP (Voverlay_arrow_string)
12915 && ! overlay_arrow_seen)
12916 {
12917 /* Overlay arrow in window redisplay is a bitmap. */
12918 if (!FRAME_WINDOW_P (it->f))
12919 {
12920 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
12921 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
12922 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
12923 struct glyph *p = row->glyphs[TEXT_AREA];
12924 struct glyph *p2, *end;
12925
12926 /* Copy the arrow glyphs. */
12927 while (glyph < arrow_end)
12928 *p++ = *glyph++;
12929
12930 /* Throw away padding glyphs. */
12931 p2 = p;
12932 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
12933 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
12934 ++p2;
12935 if (p2 > p)
12936 {
12937 while (p2 < end)
12938 *p++ = *p2++;
12939 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
12940 }
12941 }
12942
12943 overlay_arrow_seen = 1;
12944 row->overlay_arrow_p = 1;
12945 }
12946
12947 /* Compute pixel dimensions of this line. */
12948 compute_line_metrics (it);
12949
12950 /* Remember the position at which this line ends. */
12951 row->end = it->current;
12952
12953 /* Maybe set the cursor. */
12954 if (it->w->cursor.vpos < 0
12955 && PT >= MATRIX_ROW_START_CHARPOS (row)
12956 && PT <= MATRIX_ROW_END_CHARPOS (row)
12957 && cursor_row_p (it->w, row))
12958 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
12959
12960 /* Highlight trailing whitespace. */
12961 if (!NILP (Vshow_trailing_whitespace))
12962 highlight_trailing_whitespace (it->f, it->glyph_row);
12963
12964 /* Prepare for the next line. This line starts horizontally at (X
12965 HPOS) = (0 0). Vertical positions are incremented. As a
12966 convenience for the caller, IT->glyph_row is set to the next
12967 row to be used. */
12968 it->current_x = it->hpos = 0;
12969 it->current_y += row->height;
12970 ++it->vpos;
12971 ++it->glyph_row;
12972 return row->displays_text_p;
12973 }
12974
12975
12976 \f
12977 /***********************************************************************
12978 Menu Bar
12979 ***********************************************************************/
12980
12981 /* Redisplay the menu bar in the frame for window W.
12982
12983 The menu bar of X frames that don't have X toolkit support is
12984 displayed in a special window W->frame->menu_bar_window.
12985
12986 The menu bar of terminal frames is treated specially as far as
12987 glyph matrices are concerned. Menu bar lines are not part of
12988 windows, so the update is done directly on the frame matrix rows
12989 for the menu bar. */
12990
12991 static void
12992 display_menu_bar (w)
12993 struct window *w;
12994 {
12995 struct frame *f = XFRAME (WINDOW_FRAME (w));
12996 struct it it;
12997 Lisp_Object items;
12998 int i;
12999
13000 /* Don't do all this for graphical frames. */
13001 #ifdef HAVE_NTGUI
13002 if (!NILP (Vwindow_system))
13003 return;
13004 #endif
13005 #ifdef USE_X_TOOLKIT
13006 if (FRAME_X_P (f))
13007 return;
13008 #endif
13009 #ifdef macintosh
13010 if (FRAME_MAC_P (f))
13011 return;
13012 #endif
13013
13014 #ifdef USE_X_TOOLKIT
13015 xassert (!FRAME_WINDOW_P (f));
13016 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13017 it.first_visible_x = 0;
13018 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13019 #else /* not USE_X_TOOLKIT */
13020 if (FRAME_WINDOW_P (f))
13021 {
13022 /* Menu bar lines are displayed in the desired matrix of the
13023 dummy window menu_bar_window. */
13024 struct window *menu_w;
13025 xassert (WINDOWP (f->menu_bar_window));
13026 menu_w = XWINDOW (f->menu_bar_window);
13027 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13028 MENU_FACE_ID);
13029 it.first_visible_x = 0;
13030 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13031 }
13032 else
13033 {
13034 /* This is a TTY frame, i.e. character hpos/vpos are used as
13035 pixel x/y. */
13036 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13037 MENU_FACE_ID);
13038 it.first_visible_x = 0;
13039 it.last_visible_x = FRAME_WIDTH (f);
13040 }
13041 #endif /* not USE_X_TOOLKIT */
13042
13043 if (! mode_line_inverse_video)
13044 /* Force the menu-bar to be displayed in the default face. */
13045 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13046
13047 /* Clear all rows of the menu bar. */
13048 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13049 {
13050 struct glyph_row *row = it.glyph_row + i;
13051 clear_glyph_row (row);
13052 row->enabled_p = 1;
13053 row->full_width_p = 1;
13054 }
13055
13056 /* Display all items of the menu bar. */
13057 items = FRAME_MENU_BAR_ITEMS (it.f);
13058 for (i = 0; i < XVECTOR (items)->size; i += 4)
13059 {
13060 Lisp_Object string;
13061
13062 /* Stop at nil string. */
13063 string = AREF (items, i + 1);
13064 if (NILP (string))
13065 break;
13066
13067 /* Remember where item was displayed. */
13068 AREF (items, i + 3) = make_number (it.hpos);
13069
13070 /* Display the item, pad with one space. */
13071 if (it.current_x < it.last_visible_x)
13072 display_string (NULL, string, Qnil, 0, 0, &it,
13073 XSTRING (string)->size + 1, 0, 0, -1);
13074 }
13075
13076 /* Fill out the line with spaces. */
13077 if (it.current_x < it.last_visible_x)
13078 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13079
13080 /* Compute the total height of the lines. */
13081 compute_line_metrics (&it);
13082 }
13083
13084
13085 \f
13086 /***********************************************************************
13087 Mode Line
13088 ***********************************************************************/
13089
13090 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13091 FORCE is non-zero, redisplay mode lines unconditionally.
13092 Otherwise, redisplay only mode lines that are garbaged. Value is
13093 the number of windows whose mode lines were redisplayed. */
13094
13095 static int
13096 redisplay_mode_lines (window, force)
13097 Lisp_Object window;
13098 int force;
13099 {
13100 int nwindows = 0;
13101
13102 while (!NILP (window))
13103 {
13104 struct window *w = XWINDOW (window);
13105
13106 if (WINDOWP (w->hchild))
13107 nwindows += redisplay_mode_lines (w->hchild, force);
13108 else if (WINDOWP (w->vchild))
13109 nwindows += redisplay_mode_lines (w->vchild, force);
13110 else if (force
13111 || FRAME_GARBAGED_P (XFRAME (w->frame))
13112 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13113 {
13114 Lisp_Object old_selected_frame;
13115 struct text_pos lpoint;
13116 struct buffer *old = current_buffer;
13117
13118 /* Set the window's buffer for the mode line display. */
13119 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13120 set_buffer_internal_1 (XBUFFER (w->buffer));
13121
13122 /* Point refers normally to the selected window. For any
13123 other window, set up appropriate value. */
13124 if (!EQ (window, selected_window))
13125 {
13126 struct text_pos pt;
13127
13128 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13129 if (CHARPOS (pt) < BEGV)
13130 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13131 else if (CHARPOS (pt) > (ZV - 1))
13132 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13133 else
13134 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13135 }
13136
13137 /* Temporarily set up the selected frame. */
13138 old_selected_frame = selected_frame;
13139 selected_frame = w->frame;
13140
13141 /* Display mode lines. */
13142 clear_glyph_matrix (w->desired_matrix);
13143 if (display_mode_lines (w))
13144 {
13145 ++nwindows;
13146 w->must_be_updated_p = 1;
13147 }
13148
13149 /* Restore old settings. */
13150 selected_frame = old_selected_frame;
13151 set_buffer_internal_1 (old);
13152 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13153 }
13154
13155 window = w->next;
13156 }
13157
13158 return nwindows;
13159 }
13160
13161
13162 /* Display the mode and/or top line of window W. Value is the number
13163 of mode lines displayed. */
13164
13165 static int
13166 display_mode_lines (w)
13167 struct window *w;
13168 {
13169 int n = 0;
13170
13171 /* These will be set while the mode line specs are processed. */
13172 line_number_displayed = 0;
13173 w->column_number_displayed = Qnil;
13174
13175 if (WINDOW_WANTS_MODELINE_P (w))
13176 {
13177 display_mode_line (w, MODE_LINE_FACE_ID,
13178 current_buffer->mode_line_format);
13179 ++n;
13180 }
13181
13182 if (WINDOW_WANTS_HEADER_LINE_P (w))
13183 {
13184 display_mode_line (w, HEADER_LINE_FACE_ID,
13185 current_buffer->header_line_format);
13186 ++n;
13187 }
13188
13189 return n;
13190 }
13191
13192
13193 /* Display mode or top line of window W. FACE_ID specifies which line
13194 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13195 FORMAT is the mode line format to display. Value is the pixel
13196 height of the mode line displayed. */
13197
13198 static int
13199 display_mode_line (w, face_id, format)
13200 struct window *w;
13201 enum face_id face_id;
13202 Lisp_Object format;
13203 {
13204 struct it it;
13205 struct face *face;
13206
13207 init_iterator (&it, w, -1, -1, NULL, face_id);
13208 prepare_desired_row (it.glyph_row);
13209
13210 if (! mode_line_inverse_video)
13211 /* Force the mode-line to be displayed in the default face. */
13212 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13213
13214 /* Temporarily make frame's keyboard the current kboard so that
13215 kboard-local variables in the mode_line_format will get the right
13216 values. */
13217 push_frame_kboard (it.f);
13218 display_mode_element (&it, 0, 0, 0, format);
13219 pop_frame_kboard ();
13220
13221 /* Fill up with spaces. */
13222 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13223
13224 compute_line_metrics (&it);
13225 it.glyph_row->full_width_p = 1;
13226 it.glyph_row->mode_line_p = 1;
13227 it.glyph_row->inverse_p = 0;
13228 it.glyph_row->continued_p = 0;
13229 it.glyph_row->truncated_on_left_p = 0;
13230 it.glyph_row->truncated_on_right_p = 0;
13231
13232 /* Make a 3D mode-line have a shadow at its right end. */
13233 face = FACE_FROM_ID (it.f, face_id);
13234 extend_face_to_end_of_line (&it);
13235 if (face->box != FACE_NO_BOX)
13236 {
13237 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13238 + it.glyph_row->used[TEXT_AREA] - 1);
13239 last->right_box_line_p = 1;
13240 }
13241
13242 return it.glyph_row->height;
13243 }
13244
13245
13246 /* Contribute ELT to the mode line for window IT->w. How it
13247 translates into text depends on its data type.
13248
13249 IT describes the display environment in which we display, as usual.
13250
13251 DEPTH is the depth in recursion. It is used to prevent
13252 infinite recursion here.
13253
13254 FIELD_WIDTH is the number of characters the display of ELT should
13255 occupy in the mode line, and PRECISION is the maximum number of
13256 characters to display from ELT's representation. See
13257 display_string for details. *
13258
13259 Returns the hpos of the end of the text generated by ELT. */
13260
13261 static int
13262 display_mode_element (it, depth, field_width, precision, elt)
13263 struct it *it;
13264 int depth;
13265 int field_width, precision;
13266 Lisp_Object elt;
13267 {
13268 int n = 0, field, prec;
13269
13270 tail_recurse:
13271 if (depth > 10)
13272 goto invalid;
13273
13274 depth++;
13275
13276 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13277 {
13278 case Lisp_String:
13279 {
13280 /* A string: output it and check for %-constructs within it. */
13281 unsigned char c;
13282 unsigned char *this = XSTRING (elt)->data;
13283 unsigned char *lisp_string = this;
13284
13285 while ((precision <= 0 || n < precision)
13286 && *this
13287 && (frame_title_ptr
13288 || it->current_x < it->last_visible_x))
13289 {
13290 unsigned char *last = this;
13291
13292 /* Advance to end of string or next format specifier. */
13293 while ((c = *this++) != '\0' && c != '%')
13294 ;
13295
13296 if (this - 1 != last)
13297 {
13298 /* Output to end of string or up to '%'. Field width
13299 is length of string. Don't output more than
13300 PRECISION allows us. */
13301 --this;
13302 prec = strwidth (last, this - last);
13303 if (precision > 0 && prec > precision - n)
13304 prec = precision - n;
13305
13306 if (frame_title_ptr)
13307 n += store_frame_title (last, 0, prec);
13308 else
13309 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
13310 it, 0, prec, 0, -1);
13311 }
13312 else /* c == '%' */
13313 {
13314 unsigned char *percent_position = this;
13315
13316 /* Get the specified minimum width. Zero means
13317 don't pad. */
13318 field = 0;
13319 while ((c = *this++) >= '0' && c <= '9')
13320 field = field * 10 + c - '0';
13321
13322 /* Don't pad beyond the total padding allowed. */
13323 if (field_width - n > 0 && field > field_width - n)
13324 field = field_width - n;
13325
13326 /* Note that either PRECISION <= 0 or N < PRECISION. */
13327 prec = precision - n;
13328
13329 if (c == 'M')
13330 n += display_mode_element (it, depth, field, prec,
13331 Vglobal_mode_string);
13332 else if (c != 0)
13333 {
13334 unsigned char *spec
13335 = decode_mode_spec (it->w, c, field, prec);
13336
13337 if (frame_title_ptr)
13338 n += store_frame_title (spec, field, prec);
13339 else
13340 {
13341 int nglyphs_before
13342 = it->glyph_row->used[TEXT_AREA];
13343 int charpos
13344 = percent_position - XSTRING (elt)->data;
13345 int nwritten
13346 = display_string (spec, Qnil, elt, charpos, 0, it,
13347 field, prec, 0, -1);
13348
13349 /* Assign to the glyphs written above the
13350 string where the `%x' came from, position
13351 of the `%'. */
13352 if (nwritten > 0)
13353 {
13354 struct glyph *glyph
13355 = (it->glyph_row->glyphs[TEXT_AREA]
13356 + nglyphs_before);
13357 int i;
13358
13359 for (i = 0; i < nwritten; ++i)
13360 {
13361 glyph[i].object = elt;
13362 glyph[i].charpos = charpos;
13363 }
13364
13365 n += nwritten;
13366 }
13367 }
13368 }
13369 }
13370 }
13371 }
13372 break;
13373
13374 case Lisp_Symbol:
13375 /* A symbol: process the value of the symbol recursively
13376 as if it appeared here directly. Avoid error if symbol void.
13377 Special case: if value of symbol is a string, output the string
13378 literally. */
13379 {
13380 register Lisp_Object tem;
13381 tem = Fboundp (elt);
13382 if (!NILP (tem))
13383 {
13384 tem = Fsymbol_value (elt);
13385 /* If value is a string, output that string literally:
13386 don't check for % within it. */
13387 if (STRINGP (tem))
13388 {
13389 prec = precision - n;
13390 if (frame_title_ptr)
13391 n += store_frame_title (XSTRING (tem)->data, -1, prec);
13392 else
13393 n += display_string (NULL, tem, Qnil, 0, 0, it,
13394 0, prec, 0, -1);
13395 }
13396 else if (!EQ (tem, elt))
13397 {
13398 /* Give up right away for nil or t. */
13399 elt = tem;
13400 goto tail_recurse;
13401 }
13402 }
13403 }
13404 break;
13405
13406 case Lisp_Cons:
13407 {
13408 register Lisp_Object car, tem;
13409
13410 /* A cons cell: three distinct cases.
13411 If first element is a string or a cons, process all the elements
13412 and effectively concatenate them.
13413 If first element is a negative number, truncate displaying cdr to
13414 at most that many characters. If positive, pad (with spaces)
13415 to at least that many characters.
13416 If first element is a symbol, process the cadr or caddr recursively
13417 according to whether the symbol's value is non-nil or nil. */
13418 car = XCAR (elt);
13419 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
13420 {
13421 /* An element of the form (:eval FORM) means evaluate FORM
13422 and use the result as mode line elements. */
13423 struct gcpro gcpro1;
13424 Lisp_Object spec;
13425
13426 spec = safe_eval (XCAR (XCDR (elt)));
13427 GCPRO1 (spec);
13428 n += display_mode_element (it, depth, field_width - n,
13429 precision - n, spec);
13430 UNGCPRO;
13431 }
13432 else if (SYMBOLP (car))
13433 {
13434 tem = Fboundp (car);
13435 elt = XCDR (elt);
13436 if (!CONSP (elt))
13437 goto invalid;
13438 /* elt is now the cdr, and we know it is a cons cell.
13439 Use its car if CAR has a non-nil value. */
13440 if (!NILP (tem))
13441 {
13442 tem = Fsymbol_value (car);
13443 if (!NILP (tem))
13444 {
13445 elt = XCAR (elt);
13446 goto tail_recurse;
13447 }
13448 }
13449 /* Symbol's value is nil (or symbol is unbound)
13450 Get the cddr of the original list
13451 and if possible find the caddr and use that. */
13452 elt = XCDR (elt);
13453 if (NILP (elt))
13454 break;
13455 else if (!CONSP (elt))
13456 goto invalid;
13457 elt = XCAR (elt);
13458 goto tail_recurse;
13459 }
13460 else if (INTEGERP (car))
13461 {
13462 register int lim = XINT (car);
13463 elt = XCDR (elt);
13464 if (lim < 0)
13465 {
13466 /* Negative int means reduce maximum width. */
13467 if (precision <= 0)
13468 precision = -lim;
13469 else
13470 precision = min (precision, -lim);
13471 }
13472 else if (lim > 0)
13473 {
13474 /* Padding specified. Don't let it be more than
13475 current maximum. */
13476 if (precision > 0)
13477 lim = min (precision, lim);
13478
13479 /* If that's more padding than already wanted, queue it.
13480 But don't reduce padding already specified even if
13481 that is beyond the current truncation point. */
13482 field_width = max (lim, field_width);
13483 }
13484 goto tail_recurse;
13485 }
13486 else if (STRINGP (car) || CONSP (car))
13487 {
13488 register int limit = 50;
13489 /* Limit is to protect against circular lists. */
13490 while (CONSP (elt)
13491 && --limit > 0
13492 && (precision <= 0 || n < precision))
13493 {
13494 n += display_mode_element (it, depth, field_width - n,
13495 precision - n, XCAR (elt));
13496 elt = XCDR (elt);
13497 }
13498 }
13499 }
13500 break;
13501
13502 default:
13503 invalid:
13504 if (frame_title_ptr)
13505 n += store_frame_title ("*invalid*", 0, precision - n);
13506 else
13507 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13508 precision - n, 0, 0);
13509 return n;
13510 }
13511
13512 /* Pad to FIELD_WIDTH. */
13513 if (field_width > 0 && n < field_width)
13514 {
13515 if (frame_title_ptr)
13516 n += store_frame_title ("", field_width - n, 0);
13517 else
13518 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13519 0, 0, 0);
13520 }
13521
13522 return n;
13523 }
13524
13525
13526 /* Write a null-terminated, right justified decimal representation of
13527 the positive integer D to BUF using a minimal field width WIDTH. */
13528
13529 static void
13530 pint2str (buf, width, d)
13531 register char *buf;
13532 register int width;
13533 register int d;
13534 {
13535 register char *p = buf;
13536
13537 if (d <= 0)
13538 *p++ = '0';
13539 else
13540 {
13541 while (d > 0)
13542 {
13543 *p++ = d % 10 + '0';
13544 d /= 10;
13545 }
13546 }
13547
13548 for (width -= (int) (p - buf); width > 0; --width)
13549 *p++ = ' ';
13550 *p-- = '\0';
13551 while (p > buf)
13552 {
13553 d = *buf;
13554 *buf++ = *p;
13555 *p-- = d;
13556 }
13557 }
13558
13559 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
13560 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13561 type of CODING_SYSTEM. Return updated pointer into BUF. */
13562
13563 static unsigned char invalid_eol_type[] = "(*invalid*)";
13564
13565 static char *
13566 decode_mode_spec_coding (coding_system, buf, eol_flag)
13567 Lisp_Object coding_system;
13568 register char *buf;
13569 int eol_flag;
13570 {
13571 Lisp_Object val;
13572 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
13573 unsigned char *eol_str;
13574 int eol_str_len;
13575 /* The EOL conversion we are using. */
13576 Lisp_Object eoltype;
13577
13578 val = Fget (coding_system, Qcoding_system);
13579 eoltype = Qnil;
13580
13581 if (!VECTORP (val)) /* Not yet decided. */
13582 {
13583 if (multibyte)
13584 *buf++ = '-';
13585 if (eol_flag)
13586 eoltype = eol_mnemonic_undecided;
13587 /* Don't mention EOL conversion if it isn't decided. */
13588 }
13589 else
13590 {
13591 Lisp_Object eolvalue;
13592
13593 eolvalue = Fget (coding_system, Qeol_type);
13594
13595 if (multibyte)
13596 *buf++ = XFASTINT (AREF (val, 1));
13597
13598 if (eol_flag)
13599 {
13600 /* The EOL conversion that is normal on this system. */
13601
13602 if (NILP (eolvalue)) /* Not yet decided. */
13603 eoltype = eol_mnemonic_undecided;
13604 else if (VECTORP (eolvalue)) /* Not yet decided. */
13605 eoltype = eol_mnemonic_undecided;
13606 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
13607 eoltype = (XFASTINT (eolvalue) == 0
13608 ? eol_mnemonic_unix
13609 : (XFASTINT (eolvalue) == 1
13610 ? eol_mnemonic_dos : eol_mnemonic_mac));
13611 }
13612 }
13613
13614 if (eol_flag)
13615 {
13616 /* Mention the EOL conversion if it is not the usual one. */
13617 if (STRINGP (eoltype))
13618 {
13619 eol_str = XSTRING (eoltype)->data;
13620 eol_str_len = XSTRING (eoltype)->size;
13621 }
13622 else if (INTEGERP (eoltype)
13623 && CHAR_VALID_P (XINT (eoltype), 0))
13624 {
13625 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
13626 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
13627 }
13628 else
13629 {
13630 eol_str = invalid_eol_type;
13631 eol_str_len = sizeof (invalid_eol_type) - 1;
13632 }
13633 bcopy (eol_str, buf, eol_str_len);
13634 buf += eol_str_len;
13635 }
13636
13637 return buf;
13638 }
13639
13640 /* Return a string for the output of a mode line %-spec for window W,
13641 generated by character C. PRECISION >= 0 means don't return a
13642 string longer than that value. FIELD_WIDTH > 0 means pad the
13643 string returned with spaces to that value. */
13644
13645 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
13646
13647 static char *
13648 decode_mode_spec (w, c, field_width, precision)
13649 struct window *w;
13650 register int c;
13651 int field_width, precision;
13652 {
13653 Lisp_Object obj;
13654 struct frame *f = XFRAME (WINDOW_FRAME (w));
13655 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
13656 struct buffer *b = XBUFFER (w->buffer);
13657
13658 obj = Qnil;
13659
13660 switch (c)
13661 {
13662 case '*':
13663 if (!NILP (b->read_only))
13664 return "%";
13665 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13666 return "*";
13667 return "-";
13668
13669 case '+':
13670 /* This differs from %* only for a modified read-only buffer. */
13671 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13672 return "*";
13673 if (!NILP (b->read_only))
13674 return "%";
13675 return "-";
13676
13677 case '&':
13678 /* This differs from %* in ignoring read-only-ness. */
13679 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13680 return "*";
13681 return "-";
13682
13683 case '%':
13684 return "%";
13685
13686 case '[':
13687 {
13688 int i;
13689 char *p;
13690
13691 if (command_loop_level > 5)
13692 return "[[[... ";
13693 p = decode_mode_spec_buf;
13694 for (i = 0; i < command_loop_level; i++)
13695 *p++ = '[';
13696 *p = 0;
13697 return decode_mode_spec_buf;
13698 }
13699
13700 case ']':
13701 {
13702 int i;
13703 char *p;
13704
13705 if (command_loop_level > 5)
13706 return " ...]]]";
13707 p = decode_mode_spec_buf;
13708 for (i = 0; i < command_loop_level; i++)
13709 *p++ = ']';
13710 *p = 0;
13711 return decode_mode_spec_buf;
13712 }
13713
13714 case '-':
13715 {
13716 register int i;
13717
13718 /* Let lots_of_dashes be a string of infinite length. */
13719 if (field_width <= 0
13720 || field_width > sizeof (lots_of_dashes))
13721 {
13722 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
13723 decode_mode_spec_buf[i] = '-';
13724 decode_mode_spec_buf[i] = '\0';
13725 return decode_mode_spec_buf;
13726 }
13727 else
13728 return lots_of_dashes;
13729 }
13730
13731 case 'b':
13732 obj = b->name;
13733 break;
13734
13735 case 'c':
13736 {
13737 int col = current_column ();
13738 w->column_number_displayed = make_number (col);
13739 pint2str (decode_mode_spec_buf, field_width, col);
13740 return decode_mode_spec_buf;
13741 }
13742
13743 case 'F':
13744 /* %F displays the frame name. */
13745 if (!NILP (f->title))
13746 return (char *) XSTRING (f->title)->data;
13747 if (f->explicit_name || ! FRAME_WINDOW_P (f))
13748 return (char *) XSTRING (f->name)->data;
13749 return "Emacs";
13750
13751 case 'f':
13752 obj = b->filename;
13753 break;
13754
13755 case 'l':
13756 {
13757 int startpos = XMARKER (w->start)->charpos;
13758 int startpos_byte = marker_byte_position (w->start);
13759 int line, linepos, linepos_byte, topline;
13760 int nlines, junk;
13761 int height = XFASTINT (w->height);
13762
13763 /* If we decided that this buffer isn't suitable for line numbers,
13764 don't forget that too fast. */
13765 if (EQ (w->base_line_pos, w->buffer))
13766 goto no_value;
13767 /* But do forget it, if the window shows a different buffer now. */
13768 else if (BUFFERP (w->base_line_pos))
13769 w->base_line_pos = Qnil;
13770
13771 /* If the buffer is very big, don't waste time. */
13772 if (INTEGERP (Vline_number_display_limit)
13773 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
13774 {
13775 w->base_line_pos = Qnil;
13776 w->base_line_number = Qnil;
13777 goto no_value;
13778 }
13779
13780 if (!NILP (w->base_line_number)
13781 && !NILP (w->base_line_pos)
13782 && XFASTINT (w->base_line_pos) <= startpos)
13783 {
13784 line = XFASTINT (w->base_line_number);
13785 linepos = XFASTINT (w->base_line_pos);
13786 linepos_byte = buf_charpos_to_bytepos (b, linepos);
13787 }
13788 else
13789 {
13790 line = 1;
13791 linepos = BUF_BEGV (b);
13792 linepos_byte = BUF_BEGV_BYTE (b);
13793 }
13794
13795 /* Count lines from base line to window start position. */
13796 nlines = display_count_lines (linepos, linepos_byte,
13797 startpos_byte,
13798 startpos, &junk);
13799
13800 topline = nlines + line;
13801
13802 /* Determine a new base line, if the old one is too close
13803 or too far away, or if we did not have one.
13804 "Too close" means it's plausible a scroll-down would
13805 go back past it. */
13806 if (startpos == BUF_BEGV (b))
13807 {
13808 w->base_line_number = make_number (topline);
13809 w->base_line_pos = make_number (BUF_BEGV (b));
13810 }
13811 else if (nlines < height + 25 || nlines > height * 3 + 50
13812 || linepos == BUF_BEGV (b))
13813 {
13814 int limit = BUF_BEGV (b);
13815 int limit_byte = BUF_BEGV_BYTE (b);
13816 int position;
13817 int distance = (height * 2 + 30) * line_number_display_limit_width;
13818
13819 if (startpos - distance > limit)
13820 {
13821 limit = startpos - distance;
13822 limit_byte = CHAR_TO_BYTE (limit);
13823 }
13824
13825 nlines = display_count_lines (startpos, startpos_byte,
13826 limit_byte,
13827 - (height * 2 + 30),
13828 &position);
13829 /* If we couldn't find the lines we wanted within
13830 line_number_display_limit_width chars per line,
13831 give up on line numbers for this window. */
13832 if (position == limit_byte && limit == startpos - distance)
13833 {
13834 w->base_line_pos = w->buffer;
13835 w->base_line_number = Qnil;
13836 goto no_value;
13837 }
13838
13839 w->base_line_number = make_number (topline - nlines);
13840 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
13841 }
13842
13843 /* Now count lines from the start pos to point. */
13844 nlines = display_count_lines (startpos, startpos_byte,
13845 PT_BYTE, PT, &junk);
13846
13847 /* Record that we did display the line number. */
13848 line_number_displayed = 1;
13849
13850 /* Make the string to show. */
13851 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
13852 return decode_mode_spec_buf;
13853 no_value:
13854 {
13855 char* p = decode_mode_spec_buf;
13856 int pad = field_width - 2;
13857 while (pad-- > 0)
13858 *p++ = ' ';
13859 *p++ = '?';
13860 *p++ = '?';
13861 *p = '\0';
13862 return decode_mode_spec_buf;
13863 }
13864 }
13865 break;
13866
13867 case 'm':
13868 obj = b->mode_name;
13869 break;
13870
13871 case 'n':
13872 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
13873 return " Narrow";
13874 break;
13875
13876 case 'p':
13877 {
13878 int pos = marker_position (w->start);
13879 int total = BUF_ZV (b) - BUF_BEGV (b);
13880
13881 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
13882 {
13883 if (pos <= BUF_BEGV (b))
13884 return "All";
13885 else
13886 return "Bottom";
13887 }
13888 else if (pos <= BUF_BEGV (b))
13889 return "Top";
13890 else
13891 {
13892 if (total > 1000000)
13893 /* Do it differently for a large value, to avoid overflow. */
13894 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13895 else
13896 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
13897 /* We can't normally display a 3-digit number,
13898 so get us a 2-digit number that is close. */
13899 if (total == 100)
13900 total = 99;
13901 sprintf (decode_mode_spec_buf, "%2d%%", total);
13902 return decode_mode_spec_buf;
13903 }
13904 }
13905
13906 /* Display percentage of size above the bottom of the screen. */
13907 case 'P':
13908 {
13909 int toppos = marker_position (w->start);
13910 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
13911 int total = BUF_ZV (b) - BUF_BEGV (b);
13912
13913 if (botpos >= BUF_ZV (b))
13914 {
13915 if (toppos <= BUF_BEGV (b))
13916 return "All";
13917 else
13918 return "Bottom";
13919 }
13920 else
13921 {
13922 if (total > 1000000)
13923 /* Do it differently for a large value, to avoid overflow. */
13924 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13925 else
13926 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
13927 /* We can't normally display a 3-digit number,
13928 so get us a 2-digit number that is close. */
13929 if (total == 100)
13930 total = 99;
13931 if (toppos <= BUF_BEGV (b))
13932 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
13933 else
13934 sprintf (decode_mode_spec_buf, "%2d%%", total);
13935 return decode_mode_spec_buf;
13936 }
13937 }
13938
13939 case 's':
13940 /* status of process */
13941 obj = Fget_buffer_process (w->buffer);
13942 if (NILP (obj))
13943 return "no process";
13944 #ifdef subprocesses
13945 obj = Fsymbol_name (Fprocess_status (obj));
13946 #endif
13947 break;
13948
13949 case 't': /* indicate TEXT or BINARY */
13950 #ifdef MODE_LINE_BINARY_TEXT
13951 return MODE_LINE_BINARY_TEXT (b);
13952 #else
13953 return "T";
13954 #endif
13955
13956 case 'z':
13957 /* coding-system (not including end-of-line format) */
13958 case 'Z':
13959 /* coding-system (including end-of-line type) */
13960 {
13961 int eol_flag = (c == 'Z');
13962 char *p = decode_mode_spec_buf;
13963
13964 if (! FRAME_WINDOW_P (f))
13965 {
13966 /* No need to mention EOL here--the terminal never needs
13967 to do EOL conversion. */
13968 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
13969 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
13970 }
13971 p = decode_mode_spec_coding (b->buffer_file_coding_system,
13972 p, eol_flag);
13973
13974 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
13975 #ifdef subprocesses
13976 obj = Fget_buffer_process (Fcurrent_buffer ());
13977 if (PROCESSP (obj))
13978 {
13979 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
13980 p, eol_flag);
13981 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
13982 p, eol_flag);
13983 }
13984 #endif /* subprocesses */
13985 #endif /* 0 */
13986 *p = 0;
13987 return decode_mode_spec_buf;
13988 }
13989 }
13990
13991 if (STRINGP (obj))
13992 return (char *) XSTRING (obj)->data;
13993 else
13994 return "";
13995 }
13996
13997
13998 /* Count up to COUNT lines starting from START / START_BYTE.
13999 But don't go beyond LIMIT_BYTE.
14000 Return the number of lines thus found (always nonnegative).
14001
14002 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14003
14004 static int
14005 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14006 int start, start_byte, limit_byte, count;
14007 int *byte_pos_ptr;
14008 {
14009 register unsigned char *cursor;
14010 unsigned char *base;
14011
14012 register int ceiling;
14013 register unsigned char *ceiling_addr;
14014 int orig_count = count;
14015
14016 /* If we are not in selective display mode,
14017 check only for newlines. */
14018 int selective_display = (!NILP (current_buffer->selective_display)
14019 && !INTEGERP (current_buffer->selective_display));
14020
14021 if (count > 0)
14022 {
14023 while (start_byte < limit_byte)
14024 {
14025 ceiling = BUFFER_CEILING_OF (start_byte);
14026 ceiling = min (limit_byte - 1, ceiling);
14027 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14028 base = (cursor = BYTE_POS_ADDR (start_byte));
14029 while (1)
14030 {
14031 if (selective_display)
14032 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14033 ;
14034 else
14035 while (*cursor != '\n' && ++cursor != ceiling_addr)
14036 ;
14037
14038 if (cursor != ceiling_addr)
14039 {
14040 if (--count == 0)
14041 {
14042 start_byte += cursor - base + 1;
14043 *byte_pos_ptr = start_byte;
14044 return orig_count;
14045 }
14046 else
14047 if (++cursor == ceiling_addr)
14048 break;
14049 }
14050 else
14051 break;
14052 }
14053 start_byte += cursor - base;
14054 }
14055 }
14056 else
14057 {
14058 while (start_byte > limit_byte)
14059 {
14060 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14061 ceiling = max (limit_byte, ceiling);
14062 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14063 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14064 while (1)
14065 {
14066 if (selective_display)
14067 while (--cursor != ceiling_addr
14068 && *cursor != '\n' && *cursor != 015)
14069 ;
14070 else
14071 while (--cursor != ceiling_addr && *cursor != '\n')
14072 ;
14073
14074 if (cursor != ceiling_addr)
14075 {
14076 if (++count == 0)
14077 {
14078 start_byte += cursor - base + 1;
14079 *byte_pos_ptr = start_byte;
14080 /* When scanning backwards, we should
14081 not count the newline posterior to which we stop. */
14082 return - orig_count - 1;
14083 }
14084 }
14085 else
14086 break;
14087 }
14088 /* Here we add 1 to compensate for the last decrement
14089 of CURSOR, which took it past the valid range. */
14090 start_byte += cursor - base + 1;
14091 }
14092 }
14093
14094 *byte_pos_ptr = limit_byte;
14095
14096 if (count < 0)
14097 return - orig_count + count;
14098 return orig_count - count;
14099
14100 }
14101
14102
14103 \f
14104 /***********************************************************************
14105 Displaying strings
14106 ***********************************************************************/
14107
14108 /* Display a NUL-terminated string, starting with index START.
14109
14110 If STRING is non-null, display that C string. Otherwise, the Lisp
14111 string LISP_STRING is displayed.
14112
14113 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14114 FACE_STRING. Display STRING or LISP_STRING with the face at
14115 FACE_STRING_POS in FACE_STRING:
14116
14117 Display the string in the environment given by IT, but use the
14118 standard display table, temporarily.
14119
14120 FIELD_WIDTH is the minimum number of output glyphs to produce.
14121 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14122 with spaces. If STRING has more characters, more than FIELD_WIDTH
14123 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14124
14125 PRECISION is the maximum number of characters to output from
14126 STRING. PRECISION < 0 means don't truncate the string.
14127
14128 This is roughly equivalent to printf format specifiers:
14129
14130 FIELD_WIDTH PRECISION PRINTF
14131 ----------------------------------------
14132 -1 -1 %s
14133 -1 10 %.10s
14134 10 -1 %10s
14135 20 10 %20.10s
14136
14137 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14138 display them, and < 0 means obey the current buffer's value of
14139 enable_multibyte_characters.
14140
14141 Value is the number of glyphs produced. */
14142
14143 static int
14144 display_string (string, lisp_string, face_string, face_string_pos,
14145 start, it, field_width, precision, max_x, multibyte)
14146 unsigned char *string;
14147 Lisp_Object lisp_string;
14148 Lisp_Object face_string;
14149 int face_string_pos;
14150 int start;
14151 struct it *it;
14152 int field_width, precision, max_x;
14153 int multibyte;
14154 {
14155 int hpos_at_start = it->hpos;
14156 int saved_face_id = it->face_id;
14157 struct glyph_row *row = it->glyph_row;
14158
14159 /* Initialize the iterator IT for iteration over STRING beginning
14160 with index START. */
14161 reseat_to_string (it, string, lisp_string, start,
14162 precision, field_width, multibyte);
14163
14164 /* If displaying STRING, set up the face of the iterator
14165 from LISP_STRING, if that's given. */
14166 if (STRINGP (face_string))
14167 {
14168 int endptr;
14169 struct face *face;
14170
14171 it->face_id
14172 = face_at_string_position (it->w, face_string, face_string_pos,
14173 0, it->region_beg_charpos,
14174 it->region_end_charpos,
14175 &endptr, it->base_face_id, 0);
14176 face = FACE_FROM_ID (it->f, it->face_id);
14177 it->face_box_p = face->box != FACE_NO_BOX;
14178 }
14179
14180 /* Set max_x to the maximum allowed X position. Don't let it go
14181 beyond the right edge of the window. */
14182 if (max_x <= 0)
14183 max_x = it->last_visible_x;
14184 else
14185 max_x = min (max_x, it->last_visible_x);
14186
14187 /* Skip over display elements that are not visible. because IT->w is
14188 hscrolled. */
14189 if (it->current_x < it->first_visible_x)
14190 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14191 MOVE_TO_POS | MOVE_TO_X);
14192
14193 row->ascent = it->max_ascent;
14194 row->height = it->max_ascent + it->max_descent;
14195 row->phys_ascent = it->max_phys_ascent;
14196 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14197
14198 /* This condition is for the case that we are called with current_x
14199 past last_visible_x. */
14200 while (it->current_x < max_x)
14201 {
14202 int x_before, x, n_glyphs_before, i, nglyphs;
14203
14204 /* Get the next display element. */
14205 if (!get_next_display_element (it))
14206 break;
14207
14208 /* Produce glyphs. */
14209 x_before = it->current_x;
14210 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14211 PRODUCE_GLYPHS (it);
14212
14213 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14214 i = 0;
14215 x = x_before;
14216 while (i < nglyphs)
14217 {
14218 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14219
14220 if (!it->truncate_lines_p
14221 && x + glyph->pixel_width > max_x)
14222 {
14223 /* End of continued line or max_x reached. */
14224 if (CHAR_GLYPH_PADDING_P (*glyph))
14225 {
14226 /* A wide character is unbreakable. */
14227 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14228 it->current_x = x_before;
14229 }
14230 else
14231 {
14232 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14233 it->current_x = x;
14234 }
14235 break;
14236 }
14237 else if (x + glyph->pixel_width > it->first_visible_x)
14238 {
14239 /* Glyph is at least partially visible. */
14240 ++it->hpos;
14241 if (x < it->first_visible_x)
14242 it->glyph_row->x = x - it->first_visible_x;
14243 }
14244 else
14245 {
14246 /* Glyph is off the left margin of the display area.
14247 Should not happen. */
14248 abort ();
14249 }
14250
14251 row->ascent = max (row->ascent, it->max_ascent);
14252 row->height = max (row->height, it->max_ascent + it->max_descent);
14253 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14254 row->phys_height = max (row->phys_height,
14255 it->max_phys_ascent + it->max_phys_descent);
14256 x += glyph->pixel_width;
14257 ++i;
14258 }
14259
14260 /* Stop if max_x reached. */
14261 if (i < nglyphs)
14262 break;
14263
14264 /* Stop at line ends. */
14265 if (ITERATOR_AT_END_OF_LINE_P (it))
14266 {
14267 it->continuation_lines_width = 0;
14268 break;
14269 }
14270
14271 set_iterator_to_next (it, 1);
14272
14273 /* Stop if truncating at the right edge. */
14274 if (it->truncate_lines_p
14275 && it->current_x >= it->last_visible_x)
14276 {
14277 /* Add truncation mark, but don't do it if the line is
14278 truncated at a padding space. */
14279 if (IT_CHARPOS (*it) < it->string_nchars)
14280 {
14281 if (!FRAME_WINDOW_P (it->f))
14282 {
14283 int i, n;
14284
14285 if (it->current_x > it->last_visible_x)
14286 {
14287 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14288 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14289 break;
14290 for (n = row->used[TEXT_AREA]; i < n; ++i)
14291 {
14292 row->used[TEXT_AREA] = i;
14293 produce_special_glyphs (it, IT_TRUNCATION);
14294 }
14295 }
14296 produce_special_glyphs (it, IT_TRUNCATION);
14297 }
14298 it->glyph_row->truncated_on_right_p = 1;
14299 }
14300 break;
14301 }
14302 }
14303
14304 /* Maybe insert a truncation at the left. */
14305 if (it->first_visible_x
14306 && IT_CHARPOS (*it) > 0)
14307 {
14308 if (!FRAME_WINDOW_P (it->f))
14309 insert_left_trunc_glyphs (it);
14310 it->glyph_row->truncated_on_left_p = 1;
14311 }
14312
14313 it->face_id = saved_face_id;
14314
14315 /* Value is number of columns displayed. */
14316 return it->hpos - hpos_at_start;
14317 }
14318
14319
14320 \f
14321 /* This is like a combination of memq and assq. Return 1 if PROPVAL
14322 appears as an element of LIST or as the car of an element of LIST.
14323 If PROPVAL is a list, compare each element against LIST in that
14324 way, and return 1 if any element of PROPVAL is found in LIST.
14325 Otherwise return 0. This function cannot quit. */
14326
14327 int
14328 invisible_p (propval, list)
14329 register Lisp_Object propval;
14330 Lisp_Object list;
14331 {
14332 register Lisp_Object tail, proptail;
14333
14334 for (tail = list; CONSP (tail); tail = XCDR (tail))
14335 {
14336 register Lisp_Object tem;
14337 tem = XCAR (tail);
14338 if (EQ (propval, tem))
14339 return 1;
14340 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14341 return 1;
14342 }
14343
14344 if (CONSP (propval))
14345 {
14346 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14347 {
14348 Lisp_Object propelt;
14349 propelt = XCAR (proptail);
14350 for (tail = list; CONSP (tail); tail = XCDR (tail))
14351 {
14352 register Lisp_Object tem;
14353 tem = XCAR (tail);
14354 if (EQ (propelt, tem))
14355 return 1;
14356 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14357 return 1;
14358 }
14359 }
14360 }
14361
14362 return 0;
14363 }
14364
14365
14366 /* Return 1 if PROPVAL appears as the car of an element of LIST and
14367 the cdr of that element is non-nil. If PROPVAL is a list, check
14368 each element of PROPVAL in that way, and the first time some
14369 element is found, return 1 if the cdr of that element is non-nil.
14370 Otherwise return 0. This function cannot quit. */
14371
14372 int
14373 invisible_ellipsis_p (propval, list)
14374 register Lisp_Object propval;
14375 Lisp_Object list;
14376 {
14377 register Lisp_Object tail, proptail;
14378
14379 for (tail = list; CONSP (tail); tail = XCDR (tail))
14380 {
14381 register Lisp_Object tem;
14382 tem = XCAR (tail);
14383 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14384 return ! NILP (XCDR (tem));
14385 }
14386
14387 if (CONSP (propval))
14388 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14389 {
14390 Lisp_Object propelt;
14391 propelt = XCAR (proptail);
14392 for (tail = list; CONSP (tail); tail = XCDR (tail))
14393 {
14394 register Lisp_Object tem;
14395 tem = XCAR (tail);
14396 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14397 return ! NILP (XCDR (tem));
14398 }
14399 }
14400
14401 return 0;
14402 }
14403
14404
14405 \f
14406 /***********************************************************************
14407 Initialization
14408 ***********************************************************************/
14409
14410 void
14411 syms_of_xdisp ()
14412 {
14413 Vwith_echo_area_save_vector = Qnil;
14414 staticpro (&Vwith_echo_area_save_vector);
14415
14416 Vmessage_stack = Qnil;
14417 staticpro (&Vmessage_stack);
14418
14419 Qinhibit_redisplay = intern ("inhibit-redisplay");
14420 staticpro (&Qinhibit_redisplay);
14421
14422 #if GLYPH_DEBUG
14423 defsubr (&Sdump_glyph_matrix);
14424 defsubr (&Sdump_glyph_row);
14425 defsubr (&Sdump_tool_bar_row);
14426 defsubr (&Strace_redisplay);
14427 defsubr (&Strace_to_stderr);
14428 #endif
14429 #ifdef HAVE_WINDOW_SYSTEM
14430 defsubr (&Stool_bar_lines_needed);
14431 #endif
14432
14433 staticpro (&Qmenu_bar_update_hook);
14434 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14435
14436 staticpro (&Qoverriding_terminal_local_map);
14437 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14438
14439 staticpro (&Qoverriding_local_map);
14440 Qoverriding_local_map = intern ("overriding-local-map");
14441
14442 staticpro (&Qwindow_scroll_functions);
14443 Qwindow_scroll_functions = intern ("window-scroll-functions");
14444
14445 staticpro (&Qredisplay_end_trigger_functions);
14446 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14447
14448 staticpro (&Qinhibit_point_motion_hooks);
14449 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14450
14451 QCdata = intern (":data");
14452 staticpro (&QCdata);
14453 Qdisplay = intern ("display");
14454 staticpro (&Qdisplay);
14455 Qspace_width = intern ("space-width");
14456 staticpro (&Qspace_width);
14457 Qraise = intern ("raise");
14458 staticpro (&Qraise);
14459 Qspace = intern ("space");
14460 staticpro (&Qspace);
14461 Qmargin = intern ("margin");
14462 staticpro (&Qmargin);
14463 Qleft_margin = intern ("left-margin");
14464 staticpro (&Qleft_margin);
14465 Qright_margin = intern ("right-margin");
14466 staticpro (&Qright_margin);
14467 Qalign_to = intern ("align-to");
14468 staticpro (&Qalign_to);
14469 QCalign_to = intern (":align-to");
14470 staticpro (&QCalign_to);
14471 Qrelative_width = intern ("relative-width");
14472 staticpro (&Qrelative_width);
14473 QCrelative_width = intern (":relative-width");
14474 staticpro (&QCrelative_width);
14475 QCrelative_height = intern (":relative-height");
14476 staticpro (&QCrelative_height);
14477 QCeval = intern (":eval");
14478 staticpro (&QCeval);
14479 Qwhen = intern ("when");
14480 staticpro (&Qwhen);
14481 QCfile = intern (":file");
14482 staticpro (&QCfile);
14483 Qfontified = intern ("fontified");
14484 staticpro (&Qfontified);
14485 Qfontification_functions = intern ("fontification-functions");
14486 staticpro (&Qfontification_functions);
14487 Qtrailing_whitespace = intern ("trailing-whitespace");
14488 staticpro (&Qtrailing_whitespace);
14489 Qimage = intern ("image");
14490 staticpro (&Qimage);
14491 Qmessage_truncate_lines = intern ("message-truncate-lines");
14492 staticpro (&Qmessage_truncate_lines);
14493 Qgrow_only = intern ("grow-only");
14494 staticpro (&Qgrow_only);
14495 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14496 staticpro (&Qinhibit_menubar_update);
14497 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14498 staticpro (&Qinhibit_eval_during_redisplay);
14499
14500 last_arrow_position = Qnil;
14501 last_arrow_string = Qnil;
14502 staticpro (&last_arrow_position);
14503 staticpro (&last_arrow_string);
14504
14505 echo_buffer[0] = echo_buffer[1] = Qnil;
14506 staticpro (&echo_buffer[0]);
14507 staticpro (&echo_buffer[1]);
14508
14509 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14510 staticpro (&echo_area_buffer[0]);
14511 staticpro (&echo_area_buffer[1]);
14512
14513 Vmessages_buffer_name = build_string ("*Messages*");
14514 staticpro (&Vmessages_buffer_name);
14515
14516 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14517 "Non-nil means highlight trailing whitespace.\n\
14518 The face used for trailing whitespace is `trailing-whitespace'.");
14519 Vshow_trailing_whitespace = Qnil;
14520
14521 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14522 "Non-nil means don't actually do any redisplay.\n\
14523 This is used for internal purposes.");
14524 Vinhibit_redisplay = Qnil;
14525
14526 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14527 "String (or mode line construct) included (normally) in `mode-line-format'.");
14528 Vglobal_mode_string = Qnil;
14529
14530 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14531 "Marker for where to display an arrow on top of the buffer text.\n\
14532 This must be the beginning of a line in order to work.\n\
14533 See also `overlay-arrow-string'.");
14534 Voverlay_arrow_position = Qnil;
14535
14536 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14537 "String to display as an arrow. See also `overlay-arrow-position'.");
14538 Voverlay_arrow_string = Qnil;
14539
14540 DEFVAR_INT ("scroll-step", &scroll_step,
14541 "*The number of lines to try scrolling a window by when point moves out.\n\
14542 If that fails to bring point back on frame, point is centered instead.\n\
14543 If this is zero, point is always centered after it moves off frame.\n\
14544 If you want scrolling to always be a line at a time, you should set\n\
14545 `scroll-conservatively' to a large value rather than set this to 1.");
14546
14547 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14548 "*Scroll up to this many lines, to bring point back on screen.\n\
14549 A value of zero means to scroll the text to center point vertically\n\
14550 in the window.");
14551 scroll_conservatively = 0;
14552
14553 DEFVAR_INT ("scroll-margin", &scroll_margin,
14554 "*Number of lines of margin at the top and bottom of a window.\n\
14555 Recenter the window whenever point gets within this many lines\n\
14556 of the top or bottom of the window.");
14557 scroll_margin = 0;
14558
14559 #if GLYPH_DEBUG
14560 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
14561 #endif
14562
14563 DEFVAR_BOOL ("truncate-partial-width-windows",
14564 &truncate_partial_width_windows,
14565 "*Non-nil means truncate lines in all windows less than full frame wide.");
14566 truncate_partial_width_windows = 1;
14567
14568 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14569 "nil means display the mode-line/header-line/menu-bar in the default face.\n\
14570 Any other value means to use the appropriate face, `mode-line',\n\
14571 `header-line', or `menu' respectively.\n\
14572 \n\
14573 This variable is deprecated; please change the above faces instead.");
14574 mode_line_inverse_video = 1;
14575
14576 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14577 "*Maximum buffer size for which line number should be displayed.\n\
14578 If the buffer is bigger than this, the line number does not appear\n\
14579 in the mode line. A value of nil means no limit.");
14580 Vline_number_display_limit = Qnil;
14581
14582 DEFVAR_INT ("line-number-display-limit-width",
14583 &line_number_display_limit_width,
14584 "*Maximum line width (in characters) for line number display.\n\
14585 If the average length of the lines near point is bigger than this, then the\n\
14586 line number may be omitted from the mode line.");
14587 line_number_display_limit_width = 200;
14588
14589 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14590 "*Non-nil means highlight region even in nonselected windows.");
14591 highlight_nonselected_windows = 0;
14592
14593 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14594 "Non-nil if more than one frame is visible on this display.\n\
14595 Minibuffer-only frames don't count, but iconified frames do.\n\
14596 This variable is not guaranteed to be accurate except while processing\n\
14597 `frame-title-format' and `icon-title-format'.");
14598
14599 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14600 "Template for displaying the title bar of visible frames.\n\
14601 \(Assuming the window manager supports this feature.)\n\
14602 This variable has the same structure as `mode-line-format' (which see),\n\
14603 and is used only on frames for which no explicit name has been set\n\
14604 \(see `modify-frame-parameters').");
14605 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
14606 "Template for displaying the title bar of an iconified frame.\n\
14607 \(Assuming the window manager supports this feature.)\n\
14608 This variable has the same structure as `mode-line-format' (which see),\n\
14609 and is used only on frames for which no explicit name has been set\n\
14610 \(see `modify-frame-parameters').");
14611 Vicon_title_format
14612 = Vframe_title_format
14613 = Fcons (intern ("multiple-frames"),
14614 Fcons (build_string ("%b"),
14615 Fcons (Fcons (build_string (""),
14616 Fcons (intern ("invocation-name"),
14617 Fcons (build_string ("@"),
14618 Fcons (intern ("system-name"),
14619 Qnil)))),
14620 Qnil)));
14621
14622 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
14623 "Maximum number of lines to keep in the message log buffer.\n\
14624 If nil, disable message logging. If t, log messages but don't truncate\n\
14625 the buffer when it becomes large.");
14626 Vmessage_log_max = make_number (50);
14627
14628 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
14629 "Functions called before redisplay, if window sizes have changed.\n\
14630 The value should be a list of functions that take one argument.\n\
14631 Just before redisplay, for each frame, if any of its windows have changed\n\
14632 size since the last redisplay, or have been split or deleted,\n\
14633 all the functions in the list are called, with the frame as argument.");
14634 Vwindow_size_change_functions = Qnil;
14635
14636 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
14637 "List of Functions to call before redisplaying a window with scrolling.\n\
14638 Each function is called with two arguments, the window\n\
14639 and its new display-start position. Note that the value of `window-end'\n\
14640 is not valid when these functions are called.");
14641 Vwindow_scroll_functions = Qnil;
14642
14643 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
14644 "*Non-nil means automatically resize tool-bars.\n\
14645 This increases a tool-bar's height if not all tool-bar items are visible.\n\
14646 It decreases a tool-bar's height when it would display blank lines\n\
14647 otherwise.");
14648 auto_resize_tool_bars_p = 1;
14649
14650 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
14651 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
14652 auto_raise_tool_bar_buttons_p = 1;
14653
14654 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
14655 "*Margin around tool-bar buttons in pixels.\n\
14656 If an integer, use that for both horizontal and vertical margins.\n\
14657 Otherwise, value should be a pair of integers `(HORZ : VERT)' with\n\
14658 HORZ specifying the horizontal margin, and VERT specifying the\n\
14659 vertical margin.");
14660 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
14661
14662 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
14663 "Relief thickness of tool-bar buttons.");
14664 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
14665
14666 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
14667 "List of functions to call to fontify regions of text.\n\
14668 Each function is called with one argument POS. Functions must\n\
14669 fontify a region starting at POS in the current buffer, and give\n\
14670 fontified regions the property `fontified'.\n\
14671 This variable automatically becomes buffer-local when set.");
14672 Vfontification_functions = Qnil;
14673 Fmake_variable_buffer_local (Qfontification_functions);
14674
14675 DEFVAR_BOOL ("unibyte-display-via-language-environment",
14676 &unibyte_display_via_language_environment,
14677 "*Non-nil means display unibyte text according to language environment.\n\
14678 Specifically this means that unibyte non-ASCII characters\n\
14679 are displayed by converting them to the equivalent multibyte characters\n\
14680 according to the current language environment. As a result, they are\n\
14681 displayed according to the current fontset.");
14682 unibyte_display_via_language_environment = 0;
14683
14684 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
14685 "*Maximum height for resizing mini-windows.\n\
14686 If a float, it specifies a fraction of the mini-window frame's height.\n\
14687 If an integer, it specifies a number of lines.");
14688 Vmax_mini_window_height = make_float (0.25);
14689
14690 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
14691 "*How to resize mini-windows.\n\
14692 A value of nil means don't automatically resize mini-windows.\n\
14693 A value of t means resize them to fit the text displayed in them.\n\
14694 A value of `grow-only', the default, means let mini-windows grow\n\
14695 only, until their display becomes empty, at which point the windows\n\
14696 go back to their normal size.");
14697 Vresize_mini_windows = Qgrow_only;
14698
14699 DEFVAR_BOOL ("cursor-in-non-selected-windows",
14700 &cursor_in_non_selected_windows,
14701 "*Non-nil means display a hollow cursor in non-selected windows.\n\
14702 Nil means don't display a cursor there.");
14703 cursor_in_non_selected_windows = 1;
14704
14705 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
14706 "*Non-nil means scroll the display automatically to make point visible.");
14707 automatic_hscrolling_p = 1;
14708
14709 DEFVAR_LISP ("image-types", &Vimage_types,
14710 "List of supported image types.\n\
14711 Each element of the list is a symbol for a supported image type.");
14712 Vimage_types = Qnil;
14713
14714 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
14715 "If non-nil, messages are truncated instead of resizing the echo area.\n\
14716 Bind this around calls to `message' to let it take effect.");
14717 message_truncate_lines = 0;
14718
14719 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
14720 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
14721 Can be used to update submenus whose contents should vary.");
14722 Vmenu_bar_update_hook = Qnil;
14723
14724 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
14725 "Non-nil means don't update menu bars. Internal use only.");
14726 inhibit_menubar_update = 0;
14727
14728 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
14729 "Non-nil means don't eval Lisp during redisplay.");
14730 inhibit_eval_during_redisplay = 0;
14731 }
14732
14733
14734 /* Initialize this module when Emacs starts. */
14735
14736 void
14737 init_xdisp ()
14738 {
14739 Lisp_Object root_window;
14740 struct window *mini_w;
14741
14742 current_header_line_height = current_mode_line_height = -1;
14743
14744 CHARPOS (this_line_start_pos) = 0;
14745
14746 mini_w = XWINDOW (minibuf_window);
14747 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
14748
14749 if (!noninteractive)
14750 {
14751 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
14752 int i;
14753
14754 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
14755 set_window_height (root_window,
14756 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
14757 0);
14758 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
14759 set_window_height (minibuf_window, 1, 0);
14760
14761 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
14762 mini_w->width = make_number (FRAME_WIDTH (f));
14763
14764 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
14765 scratch_glyph_row.glyphs[TEXT_AREA + 1]
14766 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
14767
14768 /* The default ellipsis glyphs `...'. */
14769 for (i = 0; i < 3; ++i)
14770 default_invis_vector[i] = make_number ('.');
14771 }
14772
14773 #ifdef HAVE_WINDOW_SYSTEM
14774 {
14775 /* Allocate the buffer for frame titles. */
14776 int size = 100;
14777 frame_title_buf = (char *) xmalloc (size);
14778 frame_title_buf_end = frame_title_buf + size;
14779 frame_title_ptr = NULL;
14780 }
14781 #endif /* HAVE_WINDOW_SYSTEM */
14782
14783 help_echo_showing_p = 0;
14784 }
14785
14786