(browse-url-default-windows-browser): Support
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 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 len = CHAR_STRING (it->c, str);
4231
4232 for (i = 0; i < len; i++)
4233 {
4234 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4235 /* Insert three more glyphs into IT->ctl_chars for
4236 the octal display of the character. */
4237 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4238 XSETINT (it->ctl_chars[i * 4 + 1], g);
4239 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4240 XSETINT (it->ctl_chars[i * 4 + 2], g);
4241 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4242 XSETINT (it->ctl_chars[i * 4 + 3], g);
4243 }
4244
4245 /* Set up IT->dpvec and return the first character
4246 from it. */
4247 it->dpvec_char_len = it->len;
4248 it->dpvec = it->ctl_chars;
4249 it->dpend = it->dpvec + len * 4;
4250 it->current.dpvec_index = 0;
4251 it->method = next_element_from_display_vector;
4252 get_next_display_element (it);
4253 }
4254 }
4255 }
4256
4257 /* Adjust face id for a multibyte character. There are no
4258 multibyte character in unibyte text. */
4259 if (it->multibyte_p
4260 && success_p
4261 && FRAME_WINDOW_P (it->f))
4262 {
4263 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4264 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4265 }
4266 }
4267
4268 /* Is this character the last one of a run of characters with
4269 box? If yes, set IT->end_of_box_run_p to 1. */
4270 if (it->face_box_p
4271 && it->s == NULL)
4272 {
4273 int face_id;
4274 struct face *face;
4275
4276 it->end_of_box_run_p
4277 = ((face_id = face_after_it_pos (it),
4278 face_id != it->face_id)
4279 && (face = FACE_FROM_ID (it->f, face_id),
4280 face->box == FACE_NO_BOX));
4281 }
4282
4283 /* Value is 0 if end of buffer or string reached. */
4284 return success_p;
4285 }
4286
4287
4288 /* Move IT to the next display element.
4289
4290 RESEAT_P non-zero means if called on a newline in buffer text,
4291 skip to the next visible line start.
4292
4293 Functions get_next_display_element and set_iterator_to_next are
4294 separate because I find this arrangement easier to handle than a
4295 get_next_display_element function that also increments IT's
4296 position. The way it is we can first look at an iterator's current
4297 display element, decide whether it fits on a line, and if it does,
4298 increment the iterator position. The other way around we probably
4299 would either need a flag indicating whether the iterator has to be
4300 incremented the next time, or we would have to implement a
4301 decrement position function which would not be easy to write. */
4302
4303 void
4304 set_iterator_to_next (it, reseat_p)
4305 struct it *it;
4306 int reseat_p;
4307 {
4308 /* Reset flags indicating start and end of a sequence of characters
4309 with box. Reset them at the start of this function because
4310 moving the iterator to a new position might set them. */
4311 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4312
4313 if (it->method == next_element_from_buffer)
4314 {
4315 /* The current display element of IT is a character from
4316 current_buffer. Advance in the buffer, and maybe skip over
4317 invisible lines that are so because of selective display. */
4318 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4319 reseat_at_next_visible_line_start (it, 0);
4320 else
4321 {
4322 xassert (it->len != 0);
4323 IT_BYTEPOS (*it) += it->len;
4324 IT_CHARPOS (*it) += 1;
4325 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4326 }
4327 }
4328 else if (it->method == next_element_from_composition)
4329 {
4330 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4331 if (STRINGP (it->string))
4332 {
4333 IT_STRING_BYTEPOS (*it) += it->len;
4334 IT_STRING_CHARPOS (*it) += it->cmp_len;
4335 it->method = next_element_from_string;
4336 goto consider_string_end;
4337 }
4338 else
4339 {
4340 IT_BYTEPOS (*it) += it->len;
4341 IT_CHARPOS (*it) += it->cmp_len;
4342 it->method = next_element_from_buffer;
4343 }
4344 }
4345 else if (it->method == next_element_from_c_string)
4346 {
4347 /* Current display element of IT is from a C string. */
4348 IT_BYTEPOS (*it) += it->len;
4349 IT_CHARPOS (*it) += 1;
4350 }
4351 else if (it->method == next_element_from_display_vector)
4352 {
4353 /* Current display element of IT is from a display table entry.
4354 Advance in the display table definition. Reset it to null if
4355 end reached, and continue with characters from buffers/
4356 strings. */
4357 ++it->current.dpvec_index;
4358
4359 /* Restore face of the iterator to what they were before the
4360 display vector entry (these entries may contain faces). */
4361 it->face_id = it->saved_face_id;
4362
4363 if (it->dpvec + it->current.dpvec_index == it->dpend)
4364 {
4365 if (it->s)
4366 it->method = next_element_from_c_string;
4367 else if (STRINGP (it->string))
4368 it->method = next_element_from_string;
4369 else
4370 it->method = next_element_from_buffer;
4371
4372 it->dpvec = NULL;
4373 it->current.dpvec_index = -1;
4374
4375 /* Skip over characters which were displayed via IT->dpvec. */
4376 if (it->dpvec_char_len < 0)
4377 reseat_at_next_visible_line_start (it, 1);
4378 else if (it->dpvec_char_len > 0)
4379 {
4380 it->len = it->dpvec_char_len;
4381 set_iterator_to_next (it, reseat_p);
4382 }
4383 }
4384 }
4385 else if (it->method == next_element_from_string)
4386 {
4387 /* Current display element is a character from a Lisp string. */
4388 xassert (it->s == NULL && STRINGP (it->string));
4389 IT_STRING_BYTEPOS (*it) += it->len;
4390 IT_STRING_CHARPOS (*it) += 1;
4391
4392 consider_string_end:
4393
4394 if (it->current.overlay_string_index >= 0)
4395 {
4396 /* IT->string is an overlay string. Advance to the
4397 next, if there is one. */
4398 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4399 next_overlay_string (it);
4400 }
4401 else
4402 {
4403 /* IT->string is not an overlay string. If we reached
4404 its end, and there is something on IT->stack, proceed
4405 with what is on the stack. This can be either another
4406 string, this time an overlay string, or a buffer. */
4407 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4408 && it->sp > 0)
4409 {
4410 pop_it (it);
4411 if (!STRINGP (it->string))
4412 it->method = next_element_from_buffer;
4413 else
4414 goto consider_string_end;
4415 }
4416 }
4417 }
4418 else if (it->method == next_element_from_image
4419 || it->method == next_element_from_stretch)
4420 {
4421 /* The position etc with which we have to proceed are on
4422 the stack. The position may be at the end of a string,
4423 if the `display' property takes up the whole string. */
4424 pop_it (it);
4425 it->image_id = 0;
4426 if (STRINGP (it->string))
4427 {
4428 it->method = next_element_from_string;
4429 goto consider_string_end;
4430 }
4431 else
4432 it->method = next_element_from_buffer;
4433 }
4434 else
4435 /* There are no other methods defined, so this should be a bug. */
4436 abort ();
4437
4438 xassert (it->method != next_element_from_string
4439 || (STRINGP (it->string)
4440 && IT_STRING_CHARPOS (*it) >= 0));
4441 }
4442
4443
4444 /* Load IT's display element fields with information about the next
4445 display element which comes from a display table entry or from the
4446 result of translating a control character to one of the forms `^C'
4447 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4448
4449 static int
4450 next_element_from_display_vector (it)
4451 struct it *it;
4452 {
4453 /* Precondition. */
4454 xassert (it->dpvec && it->current.dpvec_index >= 0);
4455
4456 /* Remember the current face id in case glyphs specify faces.
4457 IT's face is restored in set_iterator_to_next. */
4458 it->saved_face_id = it->face_id;
4459
4460 if (INTEGERP (*it->dpvec)
4461 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4462 {
4463 int lface_id;
4464 GLYPH g;
4465
4466 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4467 it->c = FAST_GLYPH_CHAR (g);
4468 it->len = CHAR_BYTES (it->c);
4469
4470 /* The entry may contain a face id to use. Such a face id is
4471 the id of a Lisp face, not a realized face. A face id of
4472 zero means no face is specified. */
4473 lface_id = FAST_GLYPH_FACE (g);
4474 if (lface_id)
4475 {
4476 /* The function returns -1 if lface_id is invalid. */
4477 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4478 if (face_id >= 0)
4479 it->face_id = face_id;
4480 }
4481 }
4482 else
4483 /* Display table entry is invalid. Return a space. */
4484 it->c = ' ', it->len = 1;
4485
4486 /* Don't change position and object of the iterator here. They are
4487 still the values of the character that had this display table
4488 entry or was translated, and that's what we want. */
4489 it->what = IT_CHARACTER;
4490 return 1;
4491 }
4492
4493
4494 /* Load IT with the next display element from Lisp string IT->string.
4495 IT->current.string_pos is the current position within the string.
4496 If IT->current.overlay_string_index >= 0, the Lisp string is an
4497 overlay string. */
4498
4499 static int
4500 next_element_from_string (it)
4501 struct it *it;
4502 {
4503 struct text_pos position;
4504
4505 xassert (STRINGP (it->string));
4506 xassert (IT_STRING_CHARPOS (*it) >= 0);
4507 position = it->current.string_pos;
4508
4509 /* Time to check for invisible text? */
4510 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4511 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4512 {
4513 handle_stop (it);
4514
4515 /* Since a handler may have changed IT->method, we must
4516 recurse here. */
4517 return get_next_display_element (it);
4518 }
4519
4520 if (it->current.overlay_string_index >= 0)
4521 {
4522 /* Get the next character from an overlay string. In overlay
4523 strings, There is no field width or padding with spaces to
4524 do. */
4525 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4526 {
4527 it->what = IT_EOB;
4528 return 0;
4529 }
4530 else if (STRING_MULTIBYTE (it->string))
4531 {
4532 int remaining = (STRING_BYTES (XSTRING (it->string))
4533 - IT_STRING_BYTEPOS (*it));
4534 unsigned char *s = (XSTRING (it->string)->data
4535 + IT_STRING_BYTEPOS (*it));
4536 it->c = string_char_and_length (s, remaining, &it->len);
4537 }
4538 else
4539 {
4540 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4541 it->len = 1;
4542 }
4543 }
4544 else
4545 {
4546 /* Get the next character from a Lisp string that is not an
4547 overlay string. Such strings come from the mode line, for
4548 example. We may have to pad with spaces, or truncate the
4549 string. See also next_element_from_c_string. */
4550 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4551 {
4552 it->what = IT_EOB;
4553 return 0;
4554 }
4555 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4556 {
4557 /* Pad with spaces. */
4558 it->c = ' ', it->len = 1;
4559 CHARPOS (position) = BYTEPOS (position) = -1;
4560 }
4561 else if (STRING_MULTIBYTE (it->string))
4562 {
4563 int maxlen = (STRING_BYTES (XSTRING (it->string))
4564 - IT_STRING_BYTEPOS (*it));
4565 unsigned char *s = (XSTRING (it->string)->data
4566 + IT_STRING_BYTEPOS (*it));
4567 it->c = string_char_and_length (s, maxlen, &it->len);
4568 }
4569 else
4570 {
4571 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4572 it->len = 1;
4573 }
4574 }
4575
4576 /* Record what we have and where it came from. Note that we store a
4577 buffer position in IT->position although it could arguably be a
4578 string position. */
4579 it->what = IT_CHARACTER;
4580 it->object = it->string;
4581 it->position = position;
4582 return 1;
4583 }
4584
4585
4586 /* Load IT with next display element from C string IT->s.
4587 IT->string_nchars is the maximum number of characters to return
4588 from the string. IT->end_charpos may be greater than
4589 IT->string_nchars when this function is called, in which case we
4590 may have to return padding spaces. Value is zero if end of string
4591 reached, including padding spaces. */
4592
4593 static int
4594 next_element_from_c_string (it)
4595 struct it *it;
4596 {
4597 int success_p = 1;
4598
4599 xassert (it->s);
4600 it->what = IT_CHARACTER;
4601 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4602 it->object = Qnil;
4603
4604 /* IT's position can be greater IT->string_nchars in case a field
4605 width or precision has been specified when the iterator was
4606 initialized. */
4607 if (IT_CHARPOS (*it) >= it->end_charpos)
4608 {
4609 /* End of the game. */
4610 it->what = IT_EOB;
4611 success_p = 0;
4612 }
4613 else if (IT_CHARPOS (*it) >= it->string_nchars)
4614 {
4615 /* Pad with spaces. */
4616 it->c = ' ', it->len = 1;
4617 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4618 }
4619 else if (it->multibyte_p)
4620 {
4621 /* Implementation note: The calls to strlen apparently aren't a
4622 performance problem because there is no noticeable performance
4623 difference between Emacs running in unibyte or multibyte mode. */
4624 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4625 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4626 maxlen, &it->len);
4627 }
4628 else
4629 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4630
4631 return success_p;
4632 }
4633
4634
4635 /* Set up IT to return characters from an ellipsis, if appropriate.
4636 The definition of the ellipsis glyphs may come from a display table
4637 entry. This function Fills IT with the first glyph from the
4638 ellipsis if an ellipsis is to be displayed. */
4639
4640 static int
4641 next_element_from_ellipsis (it)
4642 struct it *it;
4643 {
4644 if (it->selective_display_ellipsis_p)
4645 {
4646 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4647 {
4648 /* Use the display table definition for `...'. Invalid glyphs
4649 will be handled by the method returning elements from dpvec. */
4650 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4651 it->dpvec_char_len = it->len;
4652 it->dpvec = v->contents;
4653 it->dpend = v->contents + v->size;
4654 it->current.dpvec_index = 0;
4655 it->method = next_element_from_display_vector;
4656 }
4657 else
4658 {
4659 /* Use default `...' which is stored in default_invis_vector. */
4660 it->dpvec_char_len = it->len;
4661 it->dpvec = default_invis_vector;
4662 it->dpend = default_invis_vector + 3;
4663 it->current.dpvec_index = 0;
4664 it->method = next_element_from_display_vector;
4665 }
4666 }
4667 else
4668 {
4669 /* The face at the current position may be different from the
4670 face we find after the invisible text. Remember what it
4671 was in IT->saved_face_id, and signal that it's there by
4672 setting face_before_selective_p. */
4673 it->saved_face_id = it->face_id;
4674 it->method = next_element_from_buffer;
4675 reseat_at_next_visible_line_start (it, 1);
4676 it->face_before_selective_p = 1;
4677 }
4678
4679 return get_next_display_element (it);
4680 }
4681
4682
4683 /* Deliver an image display element. The iterator IT is already
4684 filled with image information (done in handle_display_prop). Value
4685 is always 1. */
4686
4687
4688 static int
4689 next_element_from_image (it)
4690 struct it *it;
4691 {
4692 it->what = IT_IMAGE;
4693 return 1;
4694 }
4695
4696
4697 /* Fill iterator IT with next display element from a stretch glyph
4698 property. IT->object is the value of the text property. Value is
4699 always 1. */
4700
4701 static int
4702 next_element_from_stretch (it)
4703 struct it *it;
4704 {
4705 it->what = IT_STRETCH;
4706 return 1;
4707 }
4708
4709
4710 /* Load IT with the next display element from current_buffer. Value
4711 is zero if end of buffer reached. IT->stop_charpos is the next
4712 position at which to stop and check for text properties or buffer
4713 end. */
4714
4715 static int
4716 next_element_from_buffer (it)
4717 struct it *it;
4718 {
4719 int success_p = 1;
4720
4721 /* Check this assumption, otherwise, we would never enter the
4722 if-statement, below. */
4723 xassert (IT_CHARPOS (*it) >= BEGV
4724 && IT_CHARPOS (*it) <= it->stop_charpos);
4725
4726 if (IT_CHARPOS (*it) >= it->stop_charpos)
4727 {
4728 if (IT_CHARPOS (*it) >= it->end_charpos)
4729 {
4730 int overlay_strings_follow_p;
4731
4732 /* End of the game, except when overlay strings follow that
4733 haven't been returned yet. */
4734 if (it->overlay_strings_at_end_processed_p)
4735 overlay_strings_follow_p = 0;
4736 else
4737 {
4738 it->overlay_strings_at_end_processed_p = 1;
4739 overlay_strings_follow_p = get_overlay_strings (it, 0);
4740 }
4741
4742 if (overlay_strings_follow_p)
4743 success_p = get_next_display_element (it);
4744 else
4745 {
4746 it->what = IT_EOB;
4747 it->position = it->current.pos;
4748 success_p = 0;
4749 }
4750 }
4751 else
4752 {
4753 handle_stop (it);
4754 return get_next_display_element (it);
4755 }
4756 }
4757 else
4758 {
4759 /* No face changes, overlays etc. in sight, so just return a
4760 character from current_buffer. */
4761 unsigned char *p;
4762
4763 /* Maybe run the redisplay end trigger hook. Performance note:
4764 This doesn't seem to cost measurable time. */
4765 if (it->redisplay_end_trigger_charpos
4766 && it->glyph_row
4767 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4768 run_redisplay_end_trigger_hook (it);
4769
4770 /* Get the next character, maybe multibyte. */
4771 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4772 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4773 {
4774 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4775 - IT_BYTEPOS (*it));
4776 it->c = string_char_and_length (p, maxlen, &it->len);
4777 }
4778 else
4779 it->c = *p, it->len = 1;
4780
4781 /* Record what we have and where it came from. */
4782 it->what = IT_CHARACTER;;
4783 it->object = it->w->buffer;
4784 it->position = it->current.pos;
4785
4786 /* Normally we return the character found above, except when we
4787 really want to return an ellipsis for selective display. */
4788 if (it->selective)
4789 {
4790 if (it->c == '\n')
4791 {
4792 /* A value of selective > 0 means hide lines indented more
4793 than that number of columns. */
4794 if (it->selective > 0
4795 && IT_CHARPOS (*it) + 1 < ZV
4796 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4797 IT_BYTEPOS (*it) + 1,
4798 it->selective))
4799 {
4800 success_p = next_element_from_ellipsis (it);
4801 it->dpvec_char_len = -1;
4802 }
4803 }
4804 else if (it->c == '\r' && it->selective == -1)
4805 {
4806 /* A value of selective == -1 means that everything from the
4807 CR to the end of the line is invisible, with maybe an
4808 ellipsis displayed for it. */
4809 success_p = next_element_from_ellipsis (it);
4810 it->dpvec_char_len = -1;
4811 }
4812 }
4813 }
4814
4815 /* Value is zero if end of buffer reached. */
4816 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4817 return success_p;
4818 }
4819
4820
4821 /* Run the redisplay end trigger hook for IT. */
4822
4823 static void
4824 run_redisplay_end_trigger_hook (it)
4825 struct it *it;
4826 {
4827 Lisp_Object args[3];
4828
4829 /* IT->glyph_row should be non-null, i.e. we should be actually
4830 displaying something, or otherwise we should not run the hook. */
4831 xassert (it->glyph_row);
4832
4833 /* Set up hook arguments. */
4834 args[0] = Qredisplay_end_trigger_functions;
4835 args[1] = it->window;
4836 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4837 it->redisplay_end_trigger_charpos = 0;
4838
4839 /* Since we are *trying* to run these functions, don't try to run
4840 them again, even if they get an error. */
4841 it->w->redisplay_end_trigger = Qnil;
4842 Frun_hook_with_args (3, args);
4843
4844 /* Notice if it changed the face of the character we are on. */
4845 handle_face_prop (it);
4846 }
4847
4848
4849 /* Deliver a composition display element. The iterator IT is already
4850 filled with composition information (done in
4851 handle_composition_prop). Value is always 1. */
4852
4853 static int
4854 next_element_from_composition (it)
4855 struct it *it;
4856 {
4857 it->what = IT_COMPOSITION;
4858 it->position = (STRINGP (it->string)
4859 ? it->current.string_pos
4860 : it->current.pos);
4861 return 1;
4862 }
4863
4864
4865 \f
4866 /***********************************************************************
4867 Moving an iterator without producing glyphs
4868 ***********************************************************************/
4869
4870 /* Move iterator IT to a specified buffer or X position within one
4871 line on the display without producing glyphs.
4872
4873 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4874 whichever is reached first.
4875
4876 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4877
4878 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4879 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4880 TO_X includes the amount by which a window is horizontally
4881 scrolled.
4882
4883 Value is
4884
4885 MOVE_POS_MATCH_OR_ZV
4886 - when TO_POS or ZV was reached.
4887
4888 MOVE_X_REACHED
4889 -when TO_X was reached before TO_POS or ZV were reached.
4890
4891 MOVE_LINE_CONTINUED
4892 - when we reached the end of the display area and the line must
4893 be continued.
4894
4895 MOVE_LINE_TRUNCATED
4896 - when we reached the end of the display area and the line is
4897 truncated.
4898
4899 MOVE_NEWLINE_OR_CR
4900 - when we stopped at a line end, i.e. a newline or a CR and selective
4901 display is on. */
4902
4903 static enum move_it_result
4904 move_it_in_display_line_to (it, to_charpos, to_x, op)
4905 struct it *it;
4906 int to_charpos, to_x, op;
4907 {
4908 enum move_it_result result = MOVE_UNDEFINED;
4909 struct glyph_row *saved_glyph_row;
4910
4911 /* Don't produce glyphs in produce_glyphs. */
4912 saved_glyph_row = it->glyph_row;
4913 it->glyph_row = NULL;
4914
4915 while (1)
4916 {
4917 int x, i, ascent = 0, descent = 0;
4918
4919 /* Stop when ZV or TO_CHARPOS reached. */
4920 if (!get_next_display_element (it)
4921 || ((op & MOVE_TO_POS) != 0
4922 && BUFFERP (it->object)
4923 && IT_CHARPOS (*it) >= to_charpos))
4924 {
4925 result = MOVE_POS_MATCH_OR_ZV;
4926 break;
4927 }
4928
4929 /* The call to produce_glyphs will get the metrics of the
4930 display element IT is loaded with. We record in x the
4931 x-position before this display element in case it does not
4932 fit on the line. */
4933 x = it->current_x;
4934
4935 /* Remember the line height so far in case the next element doesn't
4936 fit on the line. */
4937 if (!it->truncate_lines_p)
4938 {
4939 ascent = it->max_ascent;
4940 descent = it->max_descent;
4941 }
4942
4943 PRODUCE_GLYPHS (it);
4944
4945 if (it->area != TEXT_AREA)
4946 {
4947 set_iterator_to_next (it, 1);
4948 continue;
4949 }
4950
4951 /* The number of glyphs we get back in IT->nglyphs will normally
4952 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4953 character on a terminal frame, or (iii) a line end. For the
4954 second case, IT->nglyphs - 1 padding glyphs will be present
4955 (on X frames, there is only one glyph produced for a
4956 composite character.
4957
4958 The behavior implemented below means, for continuation lines,
4959 that as many spaces of a TAB as fit on the current line are
4960 displayed there. For terminal frames, as many glyphs of a
4961 multi-glyph character are displayed in the current line, too.
4962 This is what the old redisplay code did, and we keep it that
4963 way. Under X, the whole shape of a complex character must
4964 fit on the line or it will be completely displayed in the
4965 next line.
4966
4967 Note that both for tabs and padding glyphs, all glyphs have
4968 the same width. */
4969 if (it->nglyphs)
4970 {
4971 /* More than one glyph or glyph doesn't fit on line. All
4972 glyphs have the same width. */
4973 int single_glyph_width = it->pixel_width / it->nglyphs;
4974 int new_x;
4975
4976 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4977 {
4978 new_x = x + single_glyph_width;
4979
4980 /* We want to leave anything reaching TO_X to the caller. */
4981 if ((op & MOVE_TO_X) && new_x > to_x)
4982 {
4983 it->current_x = x;
4984 result = MOVE_X_REACHED;
4985 break;
4986 }
4987 else if (/* Lines are continued. */
4988 !it->truncate_lines_p
4989 && (/* And glyph doesn't fit on the line. */
4990 new_x > it->last_visible_x
4991 /* Or it fits exactly and we're on a window
4992 system frame. */
4993 || (new_x == it->last_visible_x
4994 && FRAME_WINDOW_P (it->f))))
4995 {
4996 if (/* IT->hpos == 0 means the very first glyph
4997 doesn't fit on the line, e.g. a wide image. */
4998 it->hpos == 0
4999 || (new_x == it->last_visible_x
5000 && FRAME_WINDOW_P (it->f)))
5001 {
5002 ++it->hpos;
5003 it->current_x = new_x;
5004 if (i == it->nglyphs - 1)
5005 set_iterator_to_next (it, 1);
5006 }
5007 else
5008 {
5009 it->current_x = x;
5010 it->max_ascent = ascent;
5011 it->max_descent = descent;
5012 }
5013
5014 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5015 IT_CHARPOS (*it)));
5016 result = MOVE_LINE_CONTINUED;
5017 break;
5018 }
5019 else if (new_x > it->first_visible_x)
5020 {
5021 /* Glyph is visible. Increment number of glyphs that
5022 would be displayed. */
5023 ++it->hpos;
5024 }
5025 else
5026 {
5027 /* Glyph is completely off the left margin of the display
5028 area. Nothing to do. */
5029 }
5030 }
5031
5032 if (result != MOVE_UNDEFINED)
5033 break;
5034 }
5035 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5036 {
5037 /* Stop when TO_X specified and reached. This check is
5038 necessary here because of lines consisting of a line end,
5039 only. The line end will not produce any glyphs and we
5040 would never get MOVE_X_REACHED. */
5041 xassert (it->nglyphs == 0);
5042 result = MOVE_X_REACHED;
5043 break;
5044 }
5045
5046 /* Is this a line end? If yes, we're done. */
5047 if (ITERATOR_AT_END_OF_LINE_P (it))
5048 {
5049 result = MOVE_NEWLINE_OR_CR;
5050 break;
5051 }
5052
5053 /* The current display element has been consumed. Advance
5054 to the next. */
5055 set_iterator_to_next (it, 1);
5056
5057 /* Stop if lines are truncated and IT's current x-position is
5058 past the right edge of the window now. */
5059 if (it->truncate_lines_p
5060 && it->current_x >= it->last_visible_x)
5061 {
5062 result = MOVE_LINE_TRUNCATED;
5063 break;
5064 }
5065 }
5066
5067 /* Restore the iterator settings altered at the beginning of this
5068 function. */
5069 it->glyph_row = saved_glyph_row;
5070 return result;
5071 }
5072
5073
5074 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
5075 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
5076 the description of enum move_operation_enum.
5077
5078 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5079 screen line, this function will set IT to the next position >
5080 TO_CHARPOS. */
5081
5082 void
5083 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5084 struct it *it;
5085 int to_charpos, to_x, to_y, to_vpos;
5086 int op;
5087 {
5088 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5089 int line_height;
5090 int reached = 0;
5091
5092 for (;;)
5093 {
5094 if (op & MOVE_TO_VPOS)
5095 {
5096 /* If no TO_CHARPOS and no TO_X specified, stop at the
5097 start of the line TO_VPOS. */
5098 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5099 {
5100 if (it->vpos == to_vpos)
5101 {
5102 reached = 1;
5103 break;
5104 }
5105 else
5106 skip = move_it_in_display_line_to (it, -1, -1, 0);
5107 }
5108 else
5109 {
5110 /* TO_VPOS >= 0 means stop at TO_X in the line at
5111 TO_VPOS, or at TO_POS, whichever comes first. */
5112 if (it->vpos == to_vpos)
5113 {
5114 reached = 2;
5115 break;
5116 }
5117
5118 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5119
5120 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5121 {
5122 reached = 3;
5123 break;
5124 }
5125 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5126 {
5127 /* We have reached TO_X but not in the line we want. */
5128 skip = move_it_in_display_line_to (it, to_charpos,
5129 -1, MOVE_TO_POS);
5130 if (skip == MOVE_POS_MATCH_OR_ZV)
5131 {
5132 reached = 4;
5133 break;
5134 }
5135 }
5136 }
5137 }
5138 else if (op & MOVE_TO_Y)
5139 {
5140 struct it it_backup;
5141
5142 /* TO_Y specified means stop at TO_X in the line containing
5143 TO_Y---or at TO_CHARPOS if this is reached first. The
5144 problem is that we can't really tell whether the line
5145 contains TO_Y before we have completely scanned it, and
5146 this may skip past TO_X. What we do is to first scan to
5147 TO_X.
5148
5149 If TO_X is not specified, use a TO_X of zero. The reason
5150 is to make the outcome of this function more predictable.
5151 If we didn't use TO_X == 0, we would stop at the end of
5152 the line which is probably not what a caller would expect
5153 to happen. */
5154 skip = move_it_in_display_line_to (it, to_charpos,
5155 ((op & MOVE_TO_X)
5156 ? to_x : 0),
5157 (MOVE_TO_X
5158 | (op & MOVE_TO_POS)));
5159
5160 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5161 if (skip == MOVE_POS_MATCH_OR_ZV)
5162 {
5163 reached = 5;
5164 break;
5165 }
5166
5167 /* If TO_X was reached, we would like to know whether TO_Y
5168 is in the line. This can only be said if we know the
5169 total line height which requires us to scan the rest of
5170 the line. */
5171 if (skip == MOVE_X_REACHED)
5172 {
5173 it_backup = *it;
5174 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5175 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5176 op & MOVE_TO_POS);
5177 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5178 }
5179
5180 /* Now, decide whether TO_Y is in this line. */
5181 line_height = it->max_ascent + it->max_descent;
5182 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5183
5184 if (to_y >= it->current_y
5185 && to_y < it->current_y + line_height)
5186 {
5187 if (skip == MOVE_X_REACHED)
5188 /* If TO_Y is in this line and TO_X was reached above,
5189 we scanned too far. We have to restore IT's settings
5190 to the ones before skipping. */
5191 *it = it_backup;
5192 reached = 6;
5193 }
5194 else if (skip == MOVE_X_REACHED)
5195 {
5196 skip = skip2;
5197 if (skip == MOVE_POS_MATCH_OR_ZV)
5198 reached = 7;
5199 }
5200
5201 if (reached)
5202 break;
5203 }
5204 else
5205 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5206
5207 switch (skip)
5208 {
5209 case MOVE_POS_MATCH_OR_ZV:
5210 reached = 8;
5211 goto out;
5212
5213 case MOVE_NEWLINE_OR_CR:
5214 set_iterator_to_next (it, 1);
5215 it->continuation_lines_width = 0;
5216 break;
5217
5218 case MOVE_LINE_TRUNCATED:
5219 it->continuation_lines_width = 0;
5220 reseat_at_next_visible_line_start (it, 0);
5221 if ((op & MOVE_TO_POS) != 0
5222 && IT_CHARPOS (*it) > to_charpos)
5223 {
5224 reached = 9;
5225 goto out;
5226 }
5227 break;
5228
5229 case MOVE_LINE_CONTINUED:
5230 it->continuation_lines_width += it->current_x;
5231 break;
5232
5233 default:
5234 abort ();
5235 }
5236
5237 /* Reset/increment for the next run. */
5238 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5239 it->current_x = it->hpos = 0;
5240 it->current_y += it->max_ascent + it->max_descent;
5241 ++it->vpos;
5242 last_height = it->max_ascent + it->max_descent;
5243 last_max_ascent = it->max_ascent;
5244 it->max_ascent = it->max_descent = 0;
5245 }
5246
5247 out:
5248
5249 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5250 }
5251
5252
5253 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5254
5255 If DY > 0, move IT backward at least that many pixels. DY = 0
5256 means move IT backward to the preceding line start or BEGV. This
5257 function may move over more than DY pixels if IT->current_y - DY
5258 ends up in the middle of a line; in this case IT->current_y will be
5259 set to the top of the line moved to. */
5260
5261 void
5262 move_it_vertically_backward (it, dy)
5263 struct it *it;
5264 int dy;
5265 {
5266 int nlines, h, line_height;
5267 struct it it2;
5268 int start_pos = IT_CHARPOS (*it);
5269
5270 xassert (dy >= 0);
5271
5272 /* Estimate how many newlines we must move back. */
5273 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5274
5275 /* Set the iterator's position that many lines back. */
5276 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5277 back_to_previous_visible_line_start (it);
5278
5279 /* Reseat the iterator here. When moving backward, we don't want
5280 reseat to skip forward over invisible text, set up the iterator
5281 to deliver from overlay strings at the new position etc. So,
5282 use reseat_1 here. */
5283 reseat_1 (it, it->current.pos, 1);
5284
5285 /* We are now surely at a line start. */
5286 it->current_x = it->hpos = 0;
5287
5288 /* Move forward and see what y-distance we moved. First move to the
5289 start of the next line so that we get its height. We need this
5290 height to be able to tell whether we reached the specified
5291 y-distance. */
5292 it2 = *it;
5293 it2.max_ascent = it2.max_descent = 0;
5294 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5295 MOVE_TO_POS | MOVE_TO_VPOS);
5296 xassert (IT_CHARPOS (*it) >= BEGV);
5297 line_height = it2.max_ascent + it2.max_descent;
5298
5299 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5300 xassert (IT_CHARPOS (*it) >= BEGV);
5301 h = it2.current_y - it->current_y;
5302 nlines = it2.vpos - it->vpos;
5303
5304 /* Correct IT's y and vpos position. */
5305 it->vpos -= nlines;
5306 it->current_y -= h;
5307
5308 if (dy == 0)
5309 {
5310 /* DY == 0 means move to the start of the screen line. The
5311 value of nlines is > 0 if continuation lines were involved. */
5312 if (nlines > 0)
5313 move_it_by_lines (it, nlines, 1);
5314 xassert (IT_CHARPOS (*it) <= start_pos);
5315 }
5316 else if (nlines)
5317 {
5318 /* The y-position we try to reach. Note that h has been
5319 subtracted in front of the if-statement. */
5320 int target_y = it->current_y + h - dy;
5321
5322 /* If we did not reach target_y, try to move further backward if
5323 we can. If we moved too far backward, try to move forward. */
5324 if (target_y < it->current_y
5325 && IT_CHARPOS (*it) > BEGV)
5326 {
5327 move_it_vertically (it, target_y - it->current_y);
5328 xassert (IT_CHARPOS (*it) >= BEGV);
5329 }
5330 else if (target_y >= it->current_y + line_height
5331 && IT_CHARPOS (*it) < ZV)
5332 {
5333 move_it_vertically (it, target_y - (it->current_y + line_height));
5334 xassert (IT_CHARPOS (*it) >= BEGV);
5335 }
5336 }
5337 }
5338
5339
5340 /* Move IT by a specified amount of pixel lines DY. DY negative means
5341 move backwards. DY = 0 means move to start of screen line. At the
5342 end, IT will be on the start of a screen line. */
5343
5344 void
5345 move_it_vertically (it, dy)
5346 struct it *it;
5347 int dy;
5348 {
5349 if (dy <= 0)
5350 move_it_vertically_backward (it, -dy);
5351 else if (dy > 0)
5352 {
5353 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5354 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5355 MOVE_TO_POS | MOVE_TO_Y);
5356 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5357
5358 /* If buffer ends in ZV without a newline, move to the start of
5359 the line to satisfy the post-condition. */
5360 if (IT_CHARPOS (*it) == ZV
5361 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5362 move_it_by_lines (it, 0, 0);
5363 }
5364 }
5365
5366
5367 /* Move iterator IT past the end of the text line it is in. */
5368
5369 void
5370 move_it_past_eol (it)
5371 struct it *it;
5372 {
5373 enum move_it_result rc;
5374
5375 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5376 if (rc == MOVE_NEWLINE_OR_CR)
5377 set_iterator_to_next (it, 0);
5378 }
5379
5380
5381 #if 0 /* Currently not used. */
5382
5383 /* Return non-zero if some text between buffer positions START_CHARPOS
5384 and END_CHARPOS is invisible. IT->window is the window for text
5385 property lookup. */
5386
5387 static int
5388 invisible_text_between_p (it, start_charpos, end_charpos)
5389 struct it *it;
5390 int start_charpos, end_charpos;
5391 {
5392 Lisp_Object prop, limit;
5393 int invisible_found_p;
5394
5395 xassert (it != NULL && start_charpos <= end_charpos);
5396
5397 /* Is text at START invisible? */
5398 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5399 it->window);
5400 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5401 invisible_found_p = 1;
5402 else
5403 {
5404 limit = Fnext_single_char_property_change (make_number (start_charpos),
5405 Qinvisible, Qnil,
5406 make_number (end_charpos));
5407 invisible_found_p = XFASTINT (limit) < end_charpos;
5408 }
5409
5410 return invisible_found_p;
5411 }
5412
5413 #endif /* 0 */
5414
5415
5416 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5417 negative means move up. DVPOS == 0 means move to the start of the
5418 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5419 NEED_Y_P is zero, IT->current_y will be left unchanged.
5420
5421 Further optimization ideas: If we would know that IT->f doesn't use
5422 a face with proportional font, we could be faster for
5423 truncate-lines nil. */
5424
5425 void
5426 move_it_by_lines (it, dvpos, need_y_p)
5427 struct it *it;
5428 int dvpos, need_y_p;
5429 {
5430 struct position pos;
5431
5432 if (!FRAME_WINDOW_P (it->f))
5433 {
5434 struct text_pos textpos;
5435
5436 /* We can use vmotion on frames without proportional fonts. */
5437 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5438 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5439 reseat (it, textpos, 1);
5440 it->vpos += pos.vpos;
5441 it->current_y += pos.vpos;
5442 }
5443 else if (dvpos == 0)
5444 {
5445 /* DVPOS == 0 means move to the start of the screen line. */
5446 move_it_vertically_backward (it, 0);
5447 xassert (it->current_x == 0 && it->hpos == 0);
5448 }
5449 else if (dvpos > 0)
5450 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5451 else
5452 {
5453 struct it it2;
5454 int start_charpos, i;
5455
5456 /* Go back -DVPOS visible lines and reseat the iterator there. */
5457 start_charpos = IT_CHARPOS (*it);
5458 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5459 back_to_previous_visible_line_start (it);
5460 reseat (it, it->current.pos, 1);
5461 it->current_x = it->hpos = 0;
5462
5463 /* Above call may have moved too far if continuation lines
5464 are involved. Scan forward and see if it did. */
5465 it2 = *it;
5466 it2.vpos = it2.current_y = 0;
5467 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5468 it->vpos -= it2.vpos;
5469 it->current_y -= it2.current_y;
5470 it->current_x = it->hpos = 0;
5471
5472 /* If we moved too far, move IT some lines forward. */
5473 if (it2.vpos > -dvpos)
5474 {
5475 int delta = it2.vpos + dvpos;
5476 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5477 }
5478 }
5479 }
5480
5481
5482 \f
5483 /***********************************************************************
5484 Messages
5485 ***********************************************************************/
5486
5487
5488 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5489 to *Messages*. */
5490
5491 void
5492 add_to_log (format, arg1, arg2)
5493 char *format;
5494 Lisp_Object arg1, arg2;
5495 {
5496 Lisp_Object args[3];
5497 Lisp_Object msg, fmt;
5498 char *buffer;
5499 int len;
5500 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5501
5502 fmt = msg = Qnil;
5503 GCPRO4 (fmt, msg, arg1, arg2);
5504
5505 args[0] = fmt = build_string (format);
5506 args[1] = arg1;
5507 args[2] = arg2;
5508 msg = Fformat (3, args);
5509
5510 len = STRING_BYTES (XSTRING (msg)) + 1;
5511 buffer = (char *) alloca (len);
5512 strcpy (buffer, XSTRING (msg)->data);
5513
5514 message_dolog (buffer, len - 1, 1, 0);
5515 UNGCPRO;
5516 }
5517
5518
5519 /* Output a newline in the *Messages* buffer if "needs" one. */
5520
5521 void
5522 message_log_maybe_newline ()
5523 {
5524 if (message_log_need_newline)
5525 message_dolog ("", 0, 1, 0);
5526 }
5527
5528
5529 /* Add a string M of length NBYTES to the message log, optionally
5530 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5531 nonzero, means interpret the contents of M as multibyte. This
5532 function calls low-level routines in order to bypass text property
5533 hooks, etc. which might not be safe to run. */
5534
5535 void
5536 message_dolog (m, nbytes, nlflag, multibyte)
5537 char *m;
5538 int nbytes, nlflag, multibyte;
5539 {
5540 if (!NILP (Vmessage_log_max))
5541 {
5542 struct buffer *oldbuf;
5543 Lisp_Object oldpoint, oldbegv, oldzv;
5544 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5545 int point_at_end = 0;
5546 int zv_at_end = 0;
5547 Lisp_Object old_deactivate_mark, tem;
5548 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5549
5550 old_deactivate_mark = Vdeactivate_mark;
5551 oldbuf = current_buffer;
5552 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5553 current_buffer->undo_list = Qt;
5554
5555 oldpoint = Fpoint_marker ();
5556 oldbegv = Fpoint_min_marker ();
5557 oldzv = Fpoint_max_marker ();
5558 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5559
5560 if (PT == Z)
5561 point_at_end = 1;
5562 if (ZV == Z)
5563 zv_at_end = 1;
5564
5565 BEGV = BEG;
5566 BEGV_BYTE = BEG_BYTE;
5567 ZV = Z;
5568 ZV_BYTE = Z_BYTE;
5569 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5570
5571 /* Insert the string--maybe converting multibyte to single byte
5572 or vice versa, so that all the text fits the buffer. */
5573 if (multibyte
5574 && NILP (current_buffer->enable_multibyte_characters))
5575 {
5576 int i, c, char_bytes;
5577 unsigned char work[1];
5578
5579 /* Convert a multibyte string to single-byte
5580 for the *Message* buffer. */
5581 for (i = 0; i < nbytes; i += nbytes)
5582 {
5583 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5584 work[0] = (SINGLE_BYTE_CHAR_P (c)
5585 ? c
5586 : multibyte_char_to_unibyte (c, Qnil));
5587 insert_1_both (work, 1, 1, 1, 0, 0);
5588 }
5589 }
5590 else if (! multibyte
5591 && ! NILP (current_buffer->enable_multibyte_characters))
5592 {
5593 int i, c, char_bytes;
5594 unsigned char *msg = (unsigned char *) m;
5595 unsigned char str[MAX_MULTIBYTE_LENGTH];
5596 /* Convert a single-byte string to multibyte
5597 for the *Message* buffer. */
5598 for (i = 0; i < nbytes; i++)
5599 {
5600 c = unibyte_char_to_multibyte (msg[i]);
5601 char_bytes = CHAR_STRING (c, str);
5602 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5603 }
5604 }
5605 else if (nbytes)
5606 insert_1 (m, nbytes, 1, 0, 0);
5607
5608 if (nlflag)
5609 {
5610 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5611 insert_1 ("\n", 1, 1, 0, 0);
5612
5613 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5614 this_bol = PT;
5615 this_bol_byte = PT_BYTE;
5616
5617 if (this_bol > BEG)
5618 {
5619 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5620 prev_bol = PT;
5621 prev_bol_byte = PT_BYTE;
5622
5623 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5624 this_bol, this_bol_byte);
5625 if (dup)
5626 {
5627 del_range_both (prev_bol, prev_bol_byte,
5628 this_bol, this_bol_byte, 0);
5629 if (dup > 1)
5630 {
5631 char dupstr[40];
5632 int duplen;
5633
5634 /* If you change this format, don't forget to also
5635 change message_log_check_duplicate. */
5636 sprintf (dupstr, " [%d times]", dup);
5637 duplen = strlen (dupstr);
5638 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5639 insert_1 (dupstr, duplen, 1, 0, 1);
5640 }
5641 }
5642 }
5643
5644 if (NATNUMP (Vmessage_log_max))
5645 {
5646 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5647 -XFASTINT (Vmessage_log_max) - 1, 0);
5648 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5649 }
5650 }
5651 BEGV = XMARKER (oldbegv)->charpos;
5652 BEGV_BYTE = marker_byte_position (oldbegv);
5653
5654 if (zv_at_end)
5655 {
5656 ZV = Z;
5657 ZV_BYTE = Z_BYTE;
5658 }
5659 else
5660 {
5661 ZV = XMARKER (oldzv)->charpos;
5662 ZV_BYTE = marker_byte_position (oldzv);
5663 }
5664
5665 if (point_at_end)
5666 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5667 else
5668 /* We can't do Fgoto_char (oldpoint) because it will run some
5669 Lisp code. */
5670 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5671 XMARKER (oldpoint)->bytepos);
5672
5673 UNGCPRO;
5674 free_marker (oldpoint);
5675 free_marker (oldbegv);
5676 free_marker (oldzv);
5677
5678 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5679 set_buffer_internal (oldbuf);
5680 if (NILP (tem))
5681 windows_or_buffers_changed = old_windows_or_buffers_changed;
5682 message_log_need_newline = !nlflag;
5683 Vdeactivate_mark = old_deactivate_mark;
5684 }
5685 }
5686
5687
5688 /* We are at the end of the buffer after just having inserted a newline.
5689 (Note: We depend on the fact we won't be crossing the gap.)
5690 Check to see if the most recent message looks a lot like the previous one.
5691 Return 0 if different, 1 if the new one should just replace it, or a
5692 value N > 1 if we should also append " [N times]". */
5693
5694 static int
5695 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5696 int prev_bol, this_bol;
5697 int prev_bol_byte, this_bol_byte;
5698 {
5699 int i;
5700 int len = Z_BYTE - 1 - this_bol_byte;
5701 int seen_dots = 0;
5702 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5703 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5704
5705 for (i = 0; i < len; i++)
5706 {
5707 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5708 seen_dots = 1;
5709 if (p1[i] != p2[i])
5710 return seen_dots;
5711 }
5712 p1 += len;
5713 if (*p1 == '\n')
5714 return 2;
5715 if (*p1++ == ' ' && *p1++ == '[')
5716 {
5717 int n = 0;
5718 while (*p1 >= '0' && *p1 <= '9')
5719 n = n * 10 + *p1++ - '0';
5720 if (strncmp (p1, " times]\n", 8) == 0)
5721 return n+1;
5722 }
5723 return 0;
5724 }
5725
5726
5727 /* Display an echo area message M with a specified length of NBYTES
5728 bytes. The string may include null characters. If M is 0, clear
5729 out any existing message, and let the mini-buffer text show
5730 through.
5731
5732 The buffer M must continue to exist until after the echo area gets
5733 cleared or some other message gets displayed there. This means do
5734 not pass text that is stored in a Lisp string; do not pass text in
5735 a buffer that was alloca'd. */
5736
5737 void
5738 message2 (m, nbytes, multibyte)
5739 char *m;
5740 int nbytes;
5741 int multibyte;
5742 {
5743 /* First flush out any partial line written with print. */
5744 message_log_maybe_newline ();
5745 if (m)
5746 message_dolog (m, nbytes, 1, multibyte);
5747 message2_nolog (m, nbytes, multibyte);
5748 }
5749
5750
5751 /* The non-logging counterpart of message2. */
5752
5753 void
5754 message2_nolog (m, nbytes, multibyte)
5755 char *m;
5756 int nbytes;
5757 {
5758 struct frame *sf = SELECTED_FRAME ();
5759 message_enable_multibyte = multibyte;
5760
5761 if (noninteractive)
5762 {
5763 if (noninteractive_need_newline)
5764 putc ('\n', stderr);
5765 noninteractive_need_newline = 0;
5766 if (m)
5767 fwrite (m, nbytes, 1, stderr);
5768 if (cursor_in_echo_area == 0)
5769 fprintf (stderr, "\n");
5770 fflush (stderr);
5771 }
5772 /* A null message buffer means that the frame hasn't really been
5773 initialized yet. Error messages get reported properly by
5774 cmd_error, so this must be just an informative message; toss it. */
5775 else if (INTERACTIVE
5776 && sf->glyphs_initialized_p
5777 && FRAME_MESSAGE_BUF (sf))
5778 {
5779 Lisp_Object mini_window;
5780 struct frame *f;
5781
5782 /* Get the frame containing the mini-buffer
5783 that the selected frame is using. */
5784 mini_window = FRAME_MINIBUF_WINDOW (sf);
5785 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5786
5787 FRAME_SAMPLE_VISIBILITY (f);
5788 if (FRAME_VISIBLE_P (sf)
5789 && ! FRAME_VISIBLE_P (f))
5790 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5791
5792 if (m)
5793 {
5794 set_message (m, Qnil, nbytes, multibyte);
5795 if (minibuffer_auto_raise)
5796 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5797 }
5798 else
5799 clear_message (1, 1);
5800
5801 do_pending_window_change (0);
5802 echo_area_display (1);
5803 do_pending_window_change (0);
5804 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5805 (*frame_up_to_date_hook) (f);
5806 }
5807 }
5808
5809
5810 /* Display an echo area message M with a specified length of NBYTES
5811 bytes. The string may include null characters. If M is not a
5812 string, clear out any existing message, and let the mini-buffer
5813 text show through. */
5814
5815 void
5816 message3 (m, nbytes, multibyte)
5817 Lisp_Object m;
5818 int nbytes;
5819 int multibyte;
5820 {
5821 struct gcpro gcpro1;
5822
5823 GCPRO1 (m);
5824
5825 /* First flush out any partial line written with print. */
5826 message_log_maybe_newline ();
5827 if (STRINGP (m))
5828 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5829 message3_nolog (m, nbytes, multibyte);
5830
5831 UNGCPRO;
5832 }
5833
5834
5835 /* The non-logging version of message3. */
5836
5837 void
5838 message3_nolog (m, nbytes, multibyte)
5839 Lisp_Object m;
5840 int nbytes, multibyte;
5841 {
5842 struct frame *sf = SELECTED_FRAME ();
5843 message_enable_multibyte = multibyte;
5844
5845 if (noninteractive)
5846 {
5847 if (noninteractive_need_newline)
5848 putc ('\n', stderr);
5849 noninteractive_need_newline = 0;
5850 if (STRINGP (m))
5851 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5852 if (cursor_in_echo_area == 0)
5853 fprintf (stderr, "\n");
5854 fflush (stderr);
5855 }
5856 /* A null message buffer means that the frame hasn't really been
5857 initialized yet. Error messages get reported properly by
5858 cmd_error, so this must be just an informative message; toss it. */
5859 else if (INTERACTIVE
5860 && sf->glyphs_initialized_p
5861 && FRAME_MESSAGE_BUF (sf))
5862 {
5863 Lisp_Object mini_window;
5864 Lisp_Object frame;
5865 struct frame *f;
5866
5867 /* Get the frame containing the mini-buffer
5868 that the selected frame is using. */
5869 mini_window = FRAME_MINIBUF_WINDOW (sf);
5870 frame = XWINDOW (mini_window)->frame;
5871 f = XFRAME (frame);
5872
5873 FRAME_SAMPLE_VISIBILITY (f);
5874 if (FRAME_VISIBLE_P (sf)
5875 && !FRAME_VISIBLE_P (f))
5876 Fmake_frame_visible (frame);
5877
5878 if (STRINGP (m) && XSTRING (m)->size)
5879 {
5880 set_message (NULL, m, nbytes, multibyte);
5881 if (minibuffer_auto_raise)
5882 Fraise_frame (frame);
5883 }
5884 else
5885 clear_message (1, 1);
5886
5887 do_pending_window_change (0);
5888 echo_area_display (1);
5889 do_pending_window_change (0);
5890 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5891 (*frame_up_to_date_hook) (f);
5892 }
5893 }
5894
5895
5896 /* Display a null-terminated echo area message M. If M is 0, clear
5897 out any existing message, and let the mini-buffer text show through.
5898
5899 The buffer M must continue to exist until after the echo area gets
5900 cleared or some other message gets displayed there. Do not pass
5901 text that is stored in a Lisp string. Do not pass text in a buffer
5902 that was alloca'd. */
5903
5904 void
5905 message1 (m)
5906 char *m;
5907 {
5908 message2 (m, (m ? strlen (m) : 0), 0);
5909 }
5910
5911
5912 /* The non-logging counterpart of message1. */
5913
5914 void
5915 message1_nolog (m)
5916 char *m;
5917 {
5918 message2_nolog (m, (m ? strlen (m) : 0), 0);
5919 }
5920
5921 /* Display a message M which contains a single %s
5922 which gets replaced with STRING. */
5923
5924 void
5925 message_with_string (m, string, log)
5926 char *m;
5927 Lisp_Object string;
5928 int log;
5929 {
5930 if (noninteractive)
5931 {
5932 if (m)
5933 {
5934 if (noninteractive_need_newline)
5935 putc ('\n', stderr);
5936 noninteractive_need_newline = 0;
5937 fprintf (stderr, m, XSTRING (string)->data);
5938 if (cursor_in_echo_area == 0)
5939 fprintf (stderr, "\n");
5940 fflush (stderr);
5941 }
5942 }
5943 else if (INTERACTIVE)
5944 {
5945 /* The frame whose minibuffer we're going to display the message on.
5946 It may be larger than the selected frame, so we need
5947 to use its buffer, not the selected frame's buffer. */
5948 Lisp_Object mini_window;
5949 struct frame *f, *sf = SELECTED_FRAME ();
5950
5951 /* Get the frame containing the minibuffer
5952 that the selected frame is using. */
5953 mini_window = FRAME_MINIBUF_WINDOW (sf);
5954 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5955
5956 /* A null message buffer means that the frame hasn't really been
5957 initialized yet. Error messages get reported properly by
5958 cmd_error, so this must be just an informative message; toss it. */
5959 if (FRAME_MESSAGE_BUF (f))
5960 {
5961 int len;
5962 char *a[1];
5963 a[0] = (char *) XSTRING (string)->data;
5964
5965 len = doprnt (FRAME_MESSAGE_BUF (f),
5966 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5967
5968 if (log)
5969 message2 (FRAME_MESSAGE_BUF (f), len,
5970 STRING_MULTIBYTE (string));
5971 else
5972 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5973 STRING_MULTIBYTE (string));
5974
5975 /* Print should start at the beginning of the message
5976 buffer next time. */
5977 message_buf_print = 0;
5978 }
5979 }
5980 }
5981
5982
5983 /* Dump an informative message to the minibuf. If M is 0, clear out
5984 any existing message, and let the mini-buffer text show through. */
5985
5986 /* VARARGS 1 */
5987 void
5988 message (m, a1, a2, a3)
5989 char *m;
5990 EMACS_INT a1, a2, a3;
5991 {
5992 if (noninteractive)
5993 {
5994 if (m)
5995 {
5996 if (noninteractive_need_newline)
5997 putc ('\n', stderr);
5998 noninteractive_need_newline = 0;
5999 fprintf (stderr, m, a1, a2, a3);
6000 if (cursor_in_echo_area == 0)
6001 fprintf (stderr, "\n");
6002 fflush (stderr);
6003 }
6004 }
6005 else if (INTERACTIVE)
6006 {
6007 /* The frame whose mini-buffer we're going to display the message
6008 on. It may be larger than the selected frame, so we need to
6009 use its buffer, not the selected frame's buffer. */
6010 Lisp_Object mini_window;
6011 struct frame *f, *sf = SELECTED_FRAME ();
6012
6013 /* Get the frame containing the mini-buffer
6014 that the selected frame is using. */
6015 mini_window = FRAME_MINIBUF_WINDOW (sf);
6016 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6017
6018 /* A null message buffer means that the frame hasn't really been
6019 initialized yet. Error messages get reported properly by
6020 cmd_error, so this must be just an informative message; toss
6021 it. */
6022 if (FRAME_MESSAGE_BUF (f))
6023 {
6024 if (m)
6025 {
6026 int len;
6027 #ifdef NO_ARG_ARRAY
6028 char *a[3];
6029 a[0] = (char *) a1;
6030 a[1] = (char *) a2;
6031 a[2] = (char *) a3;
6032
6033 len = doprnt (FRAME_MESSAGE_BUF (f),
6034 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6035 #else
6036 len = doprnt (FRAME_MESSAGE_BUF (f),
6037 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6038 (char **) &a1);
6039 #endif /* NO_ARG_ARRAY */
6040
6041 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6042 }
6043 else
6044 message1 (0);
6045
6046 /* Print should start at the beginning of the message
6047 buffer next time. */
6048 message_buf_print = 0;
6049 }
6050 }
6051 }
6052
6053
6054 /* The non-logging version of message. */
6055
6056 void
6057 message_nolog (m, a1, a2, a3)
6058 char *m;
6059 EMACS_INT a1, a2, a3;
6060 {
6061 Lisp_Object old_log_max;
6062 old_log_max = Vmessage_log_max;
6063 Vmessage_log_max = Qnil;
6064 message (m, a1, a2, a3);
6065 Vmessage_log_max = old_log_max;
6066 }
6067
6068
6069 /* Display the current message in the current mini-buffer. This is
6070 only called from error handlers in process.c, and is not time
6071 critical. */
6072
6073 void
6074 update_echo_area ()
6075 {
6076 if (!NILP (echo_area_buffer[0]))
6077 {
6078 Lisp_Object string;
6079 string = Fcurrent_message ();
6080 message3 (string, XSTRING (string)->size,
6081 !NILP (current_buffer->enable_multibyte_characters));
6082 }
6083 }
6084
6085
6086 /* Make sure echo area buffers in echo_buffers[] are life. If they
6087 aren't, make new ones. */
6088
6089 static void
6090 ensure_echo_area_buffers ()
6091 {
6092 int i;
6093
6094 for (i = 0; i < 2; ++i)
6095 if (!BUFFERP (echo_buffer[i])
6096 || NILP (XBUFFER (echo_buffer[i])->name))
6097 {
6098 char name[30];
6099 Lisp_Object old_buffer;
6100 int j;
6101
6102 old_buffer = echo_buffer[i];
6103 sprintf (name, " *Echo Area %d*", i);
6104 echo_buffer[i] = Fget_buffer_create (build_string (name));
6105 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6106
6107 for (j = 0; j < 2; ++j)
6108 if (EQ (old_buffer, echo_area_buffer[j]))
6109 echo_area_buffer[j] = echo_buffer[i];
6110 }
6111 }
6112
6113
6114 /* Call FN with args A1..A4 with either the current or last displayed
6115 echo_area_buffer as current buffer.
6116
6117 WHICH zero means use the current message buffer
6118 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6119 from echo_buffer[] and clear it.
6120
6121 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6122 suitable buffer from echo_buffer[] and clear it.
6123
6124 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6125 that the current message becomes the last displayed one, make
6126 choose a suitable buffer for echo_area_buffer[0], and clear it.
6127
6128 Value is what FN returns. */
6129
6130 static int
6131 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6132 struct window *w;
6133 int which;
6134 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6135 EMACS_INT a1;
6136 Lisp_Object a2;
6137 EMACS_INT a3, a4;
6138 {
6139 Lisp_Object buffer;
6140 int this_one, the_other, clear_buffer_p, rc;
6141 int count = BINDING_STACK_SIZE ();
6142
6143 /* If buffers aren't life, make new ones. */
6144 ensure_echo_area_buffers ();
6145
6146 clear_buffer_p = 0;
6147
6148 if (which == 0)
6149 this_one = 0, the_other = 1;
6150 else if (which > 0)
6151 this_one = 1, the_other = 0;
6152 else
6153 {
6154 this_one = 0, the_other = 1;
6155 clear_buffer_p = 1;
6156
6157 /* We need a fresh one in case the current echo buffer equals
6158 the one containing the last displayed echo area message. */
6159 if (!NILP (echo_area_buffer[this_one])
6160 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6161 echo_area_buffer[this_one] = Qnil;
6162 }
6163
6164 /* Choose a suitable buffer from echo_buffer[] is we don't
6165 have one. */
6166 if (NILP (echo_area_buffer[this_one]))
6167 {
6168 echo_area_buffer[this_one]
6169 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6170 ? echo_buffer[the_other]
6171 : echo_buffer[this_one]);
6172 clear_buffer_p = 1;
6173 }
6174
6175 buffer = echo_area_buffer[this_one];
6176
6177 /* Don't get confused by reusing the buffer used for echoing
6178 for a different purpose. */
6179 if (!echoing && EQ (buffer, echo_message_buffer))
6180 cancel_echoing ();
6181
6182 record_unwind_protect (unwind_with_echo_area_buffer,
6183 with_echo_area_buffer_unwind_data (w));
6184
6185 /* Make the echo area buffer current. Note that for display
6186 purposes, it is not necessary that the displayed window's buffer
6187 == current_buffer, except for text property lookup. So, let's
6188 only set that buffer temporarily here without doing a full
6189 Fset_window_buffer. We must also change w->pointm, though,
6190 because otherwise an assertions in unshow_buffer fails, and Emacs
6191 aborts. */
6192 set_buffer_internal_1 (XBUFFER (buffer));
6193 if (w)
6194 {
6195 w->buffer = buffer;
6196 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6197 }
6198
6199 current_buffer->undo_list = Qt;
6200 current_buffer->read_only = Qnil;
6201 specbind (Qinhibit_read_only, Qt);
6202
6203 if (clear_buffer_p && Z > BEG)
6204 del_range (BEG, Z);
6205
6206 xassert (BEGV >= BEG);
6207 xassert (ZV <= Z && ZV >= BEGV);
6208
6209 rc = fn (a1, a2, a3, a4);
6210
6211 xassert (BEGV >= BEG);
6212 xassert (ZV <= Z && ZV >= BEGV);
6213
6214 unbind_to (count, Qnil);
6215 return rc;
6216 }
6217
6218
6219 /* Save state that should be preserved around the call to the function
6220 FN called in with_echo_area_buffer. */
6221
6222 static Lisp_Object
6223 with_echo_area_buffer_unwind_data (w)
6224 struct window *w;
6225 {
6226 int i = 0;
6227 Lisp_Object vector;
6228
6229 /* Reduce consing by keeping one vector in
6230 Vwith_echo_area_save_vector. */
6231 vector = Vwith_echo_area_save_vector;
6232 Vwith_echo_area_save_vector = Qnil;
6233
6234 if (NILP (vector))
6235 vector = Fmake_vector (make_number (7), Qnil);
6236
6237 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6238 AREF (vector, i) = Vdeactivate_mark, ++i;
6239 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6240
6241 if (w)
6242 {
6243 XSETWINDOW (AREF (vector, i), w); ++i;
6244 AREF (vector, i) = w->buffer; ++i;
6245 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6246 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6247 }
6248 else
6249 {
6250 int end = i + 4;
6251 for (; i < end; ++i)
6252 AREF (vector, i) = Qnil;
6253 }
6254
6255 xassert (i == ASIZE (vector));
6256 return vector;
6257 }
6258
6259
6260 /* Restore global state from VECTOR which was created by
6261 with_echo_area_buffer_unwind_data. */
6262
6263 static Lisp_Object
6264 unwind_with_echo_area_buffer (vector)
6265 Lisp_Object vector;
6266 {
6267 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6268 Vdeactivate_mark = AREF (vector, 1);
6269 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6270
6271 if (WINDOWP (AREF (vector, 3)))
6272 {
6273 struct window *w;
6274 Lisp_Object buffer, charpos, bytepos;
6275
6276 w = XWINDOW (AREF (vector, 3));
6277 buffer = AREF (vector, 4);
6278 charpos = AREF (vector, 5);
6279 bytepos = AREF (vector, 6);
6280
6281 w->buffer = buffer;
6282 set_marker_both (w->pointm, buffer,
6283 XFASTINT (charpos), XFASTINT (bytepos));
6284 }
6285
6286 Vwith_echo_area_save_vector = vector;
6287 return Qnil;
6288 }
6289
6290
6291 /* Set up the echo area for use by print functions. MULTIBYTE_P
6292 non-zero means we will print multibyte. */
6293
6294 void
6295 setup_echo_area_for_printing (multibyte_p)
6296 int multibyte_p;
6297 {
6298 ensure_echo_area_buffers ();
6299
6300 if (!message_buf_print)
6301 {
6302 /* A message has been output since the last time we printed.
6303 Choose a fresh echo area buffer. */
6304 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6305 echo_area_buffer[0] = echo_buffer[1];
6306 else
6307 echo_area_buffer[0] = echo_buffer[0];
6308
6309 /* Switch to that buffer and clear it. */
6310 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6311 current_buffer->truncate_lines = Qnil;
6312
6313 if (Z > BEG)
6314 {
6315 int count = BINDING_STACK_SIZE ();
6316 specbind (Qinhibit_read_only, Qt);
6317 del_range (BEG, Z);
6318 unbind_to (count, Qnil);
6319 }
6320 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6321
6322 /* Set up the buffer for the multibyteness we need. */
6323 if (multibyte_p
6324 != !NILP (current_buffer->enable_multibyte_characters))
6325 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6326
6327 /* Raise the frame containing the echo area. */
6328 if (minibuffer_auto_raise)
6329 {
6330 struct frame *sf = SELECTED_FRAME ();
6331 Lisp_Object mini_window;
6332 mini_window = FRAME_MINIBUF_WINDOW (sf);
6333 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6334 }
6335
6336 message_log_maybe_newline ();
6337 message_buf_print = 1;
6338 }
6339 else
6340 {
6341 if (NILP (echo_area_buffer[0]))
6342 {
6343 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6344 echo_area_buffer[0] = echo_buffer[1];
6345 else
6346 echo_area_buffer[0] = echo_buffer[0];
6347 }
6348
6349 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6350 {
6351 /* Someone switched buffers between print requests. */
6352 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6353 current_buffer->truncate_lines = Qnil;
6354 }
6355 }
6356 }
6357
6358
6359 /* Display an echo area message in window W. Value is non-zero if W's
6360 height is changed. If display_last_displayed_message_p is
6361 non-zero, display the message that was last displayed, otherwise
6362 display the current message. */
6363
6364 static int
6365 display_echo_area (w)
6366 struct window *w;
6367 {
6368 int i, no_message_p, window_height_changed_p, count;
6369
6370 /* Temporarily disable garbage collections while displaying the echo
6371 area. This is done because a GC can print a message itself.
6372 That message would modify the echo area buffer's contents while a
6373 redisplay of the buffer is going on, and seriously confuse
6374 redisplay. */
6375 count = inhibit_garbage_collection ();
6376
6377 /* If there is no message, we must call display_echo_area_1
6378 nevertheless because it resizes the window. But we will have to
6379 reset the echo_area_buffer in question to nil at the end because
6380 with_echo_area_buffer will sets it to an empty buffer. */
6381 i = display_last_displayed_message_p ? 1 : 0;
6382 no_message_p = NILP (echo_area_buffer[i]);
6383
6384 window_height_changed_p
6385 = with_echo_area_buffer (w, display_last_displayed_message_p,
6386 display_echo_area_1,
6387 (EMACS_INT) w, Qnil, 0, 0);
6388
6389 if (no_message_p)
6390 echo_area_buffer[i] = Qnil;
6391
6392 unbind_to (count, Qnil);
6393 return window_height_changed_p;
6394 }
6395
6396
6397 /* Helper for display_echo_area. Display the current buffer which
6398 contains the current echo area message in window W, a mini-window,
6399 a pointer to which is passed in A1. A2..A4 are currently not used.
6400 Change the height of W so that all of the message is displayed.
6401 Value is non-zero if height of W was changed. */
6402
6403 static int
6404 display_echo_area_1 (a1, a2, a3, a4)
6405 EMACS_INT a1;
6406 Lisp_Object a2;
6407 EMACS_INT a3, a4;
6408 {
6409 struct window *w = (struct window *) a1;
6410 Lisp_Object window;
6411 struct text_pos start;
6412 int window_height_changed_p = 0;
6413
6414 /* Do this before displaying, so that we have a large enough glyph
6415 matrix for the display. */
6416 window_height_changed_p = resize_mini_window (w, 0);
6417
6418 /* Display. */
6419 clear_glyph_matrix (w->desired_matrix);
6420 XSETWINDOW (window, w);
6421 SET_TEXT_POS (start, BEG, BEG_BYTE);
6422 try_window (window, start);
6423
6424 return window_height_changed_p;
6425 }
6426
6427
6428 /* Resize the echo area window to exactly the size needed for the
6429 currently displayed message, if there is one. */
6430
6431 void
6432 resize_echo_area_axactly ()
6433 {
6434 if (BUFFERP (echo_area_buffer[0])
6435 && WINDOWP (echo_area_window))
6436 {
6437 struct window *w = XWINDOW (echo_area_window);
6438 int resized_p;
6439
6440 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6441 (EMACS_INT) w, Qnil, 0, 0);
6442 if (resized_p)
6443 {
6444 ++windows_or_buffers_changed;
6445 ++update_mode_lines;
6446 redisplay_internal (0);
6447 }
6448 }
6449 }
6450
6451
6452 /* Callback function for with_echo_area_buffer, when used from
6453 resize_echo_area_axactly. A1 contains a pointer to the window to
6454 resize, A2 to A4 are not used. Value is what resize_mini_window
6455 returns. */
6456
6457 static int
6458 resize_mini_window_1 (a1, a2, a3, a4)
6459 EMACS_INT a1;
6460 Lisp_Object a2;
6461 EMACS_INT a3, a4;
6462 {
6463 return resize_mini_window ((struct window *) a1, 1);
6464 }
6465
6466
6467 /* Resize mini-window W to fit the size of its contents. EXACT:P
6468 means size the window exactly to the size needed. Otherwise, it's
6469 only enlarged until W's buffer is empty. Value is non-zero if
6470 the window height has been changed. */
6471
6472 int
6473 resize_mini_window (w, exact_p)
6474 struct window *w;
6475 int exact_p;
6476 {
6477 struct frame *f = XFRAME (w->frame);
6478 int window_height_changed_p = 0;
6479
6480 xassert (MINI_WINDOW_P (w));
6481
6482 /* Nil means don't try to resize. */
6483 if (NILP (Vresize_mini_windows)
6484 || (FRAME_X_P (f) && f->output_data.x == NULL))
6485 return 0;
6486
6487 if (!FRAME_MINIBUF_ONLY_P (f))
6488 {
6489 struct it it;
6490 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6491 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6492 int height, max_height;
6493 int unit = CANON_Y_UNIT (f);
6494 struct text_pos start;
6495 struct buffer *old_current_buffer = NULL;
6496
6497 if (current_buffer != XBUFFER (w->buffer))
6498 {
6499 old_current_buffer = current_buffer;
6500 set_buffer_internal (XBUFFER (w->buffer));
6501 }
6502
6503 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6504
6505 /* Compute the max. number of lines specified by the user. */
6506 if (FLOATP (Vmax_mini_window_height))
6507 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
6508 else if (INTEGERP (Vmax_mini_window_height))
6509 max_height = XINT (Vmax_mini_window_height);
6510 else
6511 max_height = total_height / 4;
6512
6513 /* Correct that max. height if it's bogus. */
6514 max_height = max (1, max_height);
6515 max_height = min (total_height, max_height);
6516
6517 /* Find out the height of the text in the window. */
6518 if (it.truncate_lines_p)
6519 height = 1;
6520 else
6521 {
6522 last_height = 0;
6523 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6524 if (it.max_ascent == 0 && it.max_descent == 0)
6525 height = it.current_y + last_height;
6526 else
6527 height = it.current_y + it.max_ascent + it.max_descent;
6528 height -= it.extra_line_spacing;
6529 height = (height + unit - 1) / unit;
6530 }
6531
6532 /* Compute a suitable window start. */
6533 if (height > max_height)
6534 {
6535 height = max_height;
6536 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6537 move_it_vertically_backward (&it, (height - 1) * unit);
6538 start = it.current.pos;
6539 }
6540 else
6541 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6542 SET_MARKER_FROM_TEXT_POS (w->start, start);
6543
6544 if (EQ (Vresize_mini_windows, Qgrow_only))
6545 {
6546 /* Let it grow only, until we display an empty message, in which
6547 case the window shrinks again. */
6548 if (height > XFASTINT (w->height))
6549 {
6550 int old_height = XFASTINT (w->height);
6551 freeze_window_starts (f, 1);
6552 grow_mini_window (w, height - XFASTINT (w->height));
6553 window_height_changed_p = XFASTINT (w->height) != old_height;
6554 }
6555 else if (height < XFASTINT (w->height)
6556 && (exact_p || BEGV == ZV))
6557 {
6558 int old_height = XFASTINT (w->height);
6559 freeze_window_starts (f, 0);
6560 shrink_mini_window (w);
6561 window_height_changed_p = XFASTINT (w->height) != old_height;
6562 }
6563 }
6564 else
6565 {
6566 /* Always resize to exact size needed. */
6567 if (height > XFASTINT (w->height))
6568 {
6569 int old_height = XFASTINT (w->height);
6570 freeze_window_starts (f, 1);
6571 grow_mini_window (w, height - XFASTINT (w->height));
6572 window_height_changed_p = XFASTINT (w->height) != old_height;
6573 }
6574 else if (height < XFASTINT (w->height))
6575 {
6576 int old_height = XFASTINT (w->height);
6577 freeze_window_starts (f, 0);
6578 shrink_mini_window (w);
6579
6580 if (height)
6581 {
6582 freeze_window_starts (f, 1);
6583 grow_mini_window (w, height - XFASTINT (w->height));
6584 }
6585
6586 window_height_changed_p = XFASTINT (w->height) != old_height;
6587 }
6588 }
6589
6590 if (old_current_buffer)
6591 set_buffer_internal (old_current_buffer);
6592 }
6593
6594 return window_height_changed_p;
6595 }
6596
6597
6598 /* Value is the current message, a string, or nil if there is no
6599 current message. */
6600
6601 Lisp_Object
6602 current_message ()
6603 {
6604 Lisp_Object msg;
6605
6606 if (NILP (echo_area_buffer[0]))
6607 msg = Qnil;
6608 else
6609 {
6610 with_echo_area_buffer (0, 0, current_message_1,
6611 (EMACS_INT) &msg, Qnil, 0, 0);
6612 if (NILP (msg))
6613 echo_area_buffer[0] = Qnil;
6614 }
6615
6616 return msg;
6617 }
6618
6619
6620 static int
6621 current_message_1 (a1, a2, a3, a4)
6622 EMACS_INT a1;
6623 Lisp_Object a2;
6624 EMACS_INT a3, a4;
6625 {
6626 Lisp_Object *msg = (Lisp_Object *) a1;
6627
6628 if (Z > BEG)
6629 *msg = make_buffer_string (BEG, Z, 1);
6630 else
6631 *msg = Qnil;
6632 return 0;
6633 }
6634
6635
6636 /* Push the current message on Vmessage_stack for later restauration
6637 by restore_message. Value is non-zero if the current message isn't
6638 empty. This is a relatively infrequent operation, so it's not
6639 worth optimizing. */
6640
6641 int
6642 push_message ()
6643 {
6644 Lisp_Object msg;
6645 msg = current_message ();
6646 Vmessage_stack = Fcons (msg, Vmessage_stack);
6647 return STRINGP (msg);
6648 }
6649
6650
6651 /* Handler for record_unwind_protect calling pop_message. */
6652
6653 Lisp_Object
6654 push_message_unwind (dummy)
6655 Lisp_Object dummy;
6656 {
6657 pop_message ();
6658 return Qnil;
6659 }
6660
6661
6662 /* Restore message display from the top of Vmessage_stack. */
6663
6664 void
6665 restore_message ()
6666 {
6667 Lisp_Object msg;
6668
6669 xassert (CONSP (Vmessage_stack));
6670 msg = XCAR (Vmessage_stack);
6671 if (STRINGP (msg))
6672 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6673 else
6674 message3_nolog (msg, 0, 0);
6675 }
6676
6677
6678 /* Pop the top-most entry off Vmessage_stack. */
6679
6680 void
6681 pop_message ()
6682 {
6683 xassert (CONSP (Vmessage_stack));
6684 Vmessage_stack = XCDR (Vmessage_stack);
6685 }
6686
6687
6688 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6689 exits. If the stack is not empty, we have a missing pop_message
6690 somewhere. */
6691
6692 void
6693 check_message_stack ()
6694 {
6695 if (!NILP (Vmessage_stack))
6696 abort ();
6697 }
6698
6699
6700 /* Truncate to NCHARS what will be displayed in the echo area the next
6701 time we display it---but don't redisplay it now. */
6702
6703 void
6704 truncate_echo_area (nchars)
6705 int nchars;
6706 {
6707 if (nchars == 0)
6708 echo_area_buffer[0] = Qnil;
6709 /* A null message buffer means that the frame hasn't really been
6710 initialized yet. Error messages get reported properly by
6711 cmd_error, so this must be just an informative message; toss it. */
6712 else if (!noninteractive
6713 && INTERACTIVE
6714 && !NILP (echo_area_buffer[0]))
6715 {
6716 struct frame *sf = SELECTED_FRAME ();
6717 if (FRAME_MESSAGE_BUF (sf))
6718 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6719 }
6720 }
6721
6722
6723 /* Helper function for truncate_echo_area. Truncate the current
6724 message to at most NCHARS characters. */
6725
6726 static int
6727 truncate_message_1 (nchars, a2, a3, a4)
6728 EMACS_INT nchars;
6729 Lisp_Object a2;
6730 EMACS_INT a3, a4;
6731 {
6732 if (BEG + nchars < Z)
6733 del_range (BEG + nchars, Z);
6734 if (Z == BEG)
6735 echo_area_buffer[0] = Qnil;
6736 return 0;
6737 }
6738
6739
6740 /* Set the current message to a substring of S or STRING.
6741
6742 If STRING is a Lisp string, set the message to the first NBYTES
6743 bytes from STRING. NBYTES zero means use the whole string. If
6744 STRING is multibyte, the message will be displayed multibyte.
6745
6746 If S is not null, set the message to the first LEN bytes of S. LEN
6747 zero means use the whole string. MULTIBYTE_P non-zero means S is
6748 multibyte. Display the message multibyte in that case. */
6749
6750 void
6751 set_message (s, string, nbytes, multibyte_p)
6752 char *s;
6753 Lisp_Object string;
6754 int nbytes;
6755 {
6756 message_enable_multibyte
6757 = ((s && multibyte_p)
6758 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6759
6760 with_echo_area_buffer (0, -1, set_message_1,
6761 (EMACS_INT) s, string, nbytes, multibyte_p);
6762 message_buf_print = 0;
6763 help_echo_showing_p = 0;
6764 }
6765
6766
6767 /* Helper function for set_message. Arguments have the same meaning
6768 as there, with A1 corresponding to S and A2 corresponding to STRING
6769 This function is called with the echo area buffer being
6770 current. */
6771
6772 static int
6773 set_message_1 (a1, a2, nbytes, multibyte_p)
6774 EMACS_INT a1;
6775 Lisp_Object a2;
6776 EMACS_INT nbytes, multibyte_p;
6777 {
6778 char *s = (char *) a1;
6779 Lisp_Object string = a2;
6780
6781 xassert (BEG == Z);
6782
6783 /* Change multibyteness of the echo buffer appropriately. */
6784 if (message_enable_multibyte
6785 != !NILP (current_buffer->enable_multibyte_characters))
6786 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6787
6788 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6789
6790 /* Insert new message at BEG. */
6791 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6792
6793 if (STRINGP (string))
6794 {
6795 int nchars;
6796
6797 if (nbytes == 0)
6798 nbytes = XSTRING (string)->size_byte;
6799 nchars = string_byte_to_char (string, nbytes);
6800
6801 /* This function takes care of single/multibyte conversion. We
6802 just have to ensure that the echo area buffer has the right
6803 setting of enable_multibyte_characters. */
6804 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6805 }
6806 else if (s)
6807 {
6808 if (nbytes == 0)
6809 nbytes = strlen (s);
6810
6811 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6812 {
6813 /* Convert from multi-byte to single-byte. */
6814 int i, c, n;
6815 unsigned char work[1];
6816
6817 /* Convert a multibyte string to single-byte. */
6818 for (i = 0; i < nbytes; i += n)
6819 {
6820 c = string_char_and_length (s + i, nbytes - i, &n);
6821 work[0] = (SINGLE_BYTE_CHAR_P (c)
6822 ? c
6823 : multibyte_char_to_unibyte (c, Qnil));
6824 insert_1_both (work, 1, 1, 1, 0, 0);
6825 }
6826 }
6827 else if (!multibyte_p
6828 && !NILP (current_buffer->enable_multibyte_characters))
6829 {
6830 /* Convert from single-byte to multi-byte. */
6831 int i, c, n;
6832 unsigned char *msg = (unsigned char *) s;
6833 unsigned char str[MAX_MULTIBYTE_LENGTH];
6834
6835 /* Convert a single-byte string to multibyte. */
6836 for (i = 0; i < nbytes; i++)
6837 {
6838 c = unibyte_char_to_multibyte (msg[i]);
6839 n = CHAR_STRING (c, str);
6840 insert_1_both (str, 1, n, 1, 0, 0);
6841 }
6842 }
6843 else
6844 insert_1 (s, nbytes, 1, 0, 0);
6845 }
6846
6847 return 0;
6848 }
6849
6850
6851 /* Clear messages. CURRENT_P non-zero means clear the current
6852 message. LAST_DISPLAYED_P non-zero means clear the message
6853 last displayed. */
6854
6855 void
6856 clear_message (current_p, last_displayed_p)
6857 int current_p, last_displayed_p;
6858 {
6859 if (current_p)
6860 echo_area_buffer[0] = Qnil;
6861
6862 if (last_displayed_p)
6863 echo_area_buffer[1] = Qnil;
6864
6865 message_buf_print = 0;
6866 }
6867
6868 /* Clear garbaged frames.
6869
6870 This function is used where the old redisplay called
6871 redraw_garbaged_frames which in turn called redraw_frame which in
6872 turn called clear_frame. The call to clear_frame was a source of
6873 flickering. I believe a clear_frame is not necessary. It should
6874 suffice in the new redisplay to invalidate all current matrices,
6875 and ensure a complete redisplay of all windows. */
6876
6877 static void
6878 clear_garbaged_frames ()
6879 {
6880 if (frame_garbaged)
6881 {
6882 Lisp_Object tail, frame;
6883
6884 FOR_EACH_FRAME (tail, frame)
6885 {
6886 struct frame *f = XFRAME (frame);
6887
6888 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6889 {
6890 clear_current_matrices (f);
6891 f->garbaged = 0;
6892 }
6893 }
6894
6895 frame_garbaged = 0;
6896 ++windows_or_buffers_changed;
6897 }
6898 }
6899
6900
6901 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6902 is non-zero update selected_frame. Value is non-zero if the
6903 mini-windows height has been changed. */
6904
6905 static int
6906 echo_area_display (update_frame_p)
6907 int update_frame_p;
6908 {
6909 Lisp_Object mini_window;
6910 struct window *w;
6911 struct frame *f;
6912 int window_height_changed_p = 0;
6913 struct frame *sf = SELECTED_FRAME ();
6914
6915 mini_window = FRAME_MINIBUF_WINDOW (sf);
6916 w = XWINDOW (mini_window);
6917 f = XFRAME (WINDOW_FRAME (w));
6918
6919 /* Don't display if frame is invisible or not yet initialized. */
6920 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6921 return 0;
6922
6923 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
6924 #ifndef macintosh
6925 #ifdef HAVE_WINDOW_SYSTEM
6926 /* When Emacs starts, selected_frame may be a visible terminal
6927 frame, even if we run under a window system. If we let this
6928 through, a message would be displayed on the terminal. */
6929 if (EQ (selected_frame, Vterminal_frame)
6930 && !NILP (Vwindow_system))
6931 return 0;
6932 #endif /* HAVE_WINDOW_SYSTEM */
6933 #endif
6934
6935 /* Redraw garbaged frames. */
6936 if (frame_garbaged)
6937 clear_garbaged_frames ();
6938
6939 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6940 {
6941 echo_area_window = mini_window;
6942 window_height_changed_p = display_echo_area (w);
6943 w->must_be_updated_p = 1;
6944
6945 /* Update the display, unless called from redisplay_internal.
6946 Also don't update the screen during redisplay itself. The
6947 update will happen at the end of redisplay, and an update
6948 here could cause confusion. */
6949 if (update_frame_p && !redisplaying_p)
6950 {
6951 int n = 0;
6952
6953 /* If the display update has been interrupted by pending
6954 input, update mode lines in the frame. Due to the
6955 pending input, it might have been that redisplay hasn't
6956 been called, so that mode lines above the echo area are
6957 garbaged. This looks odd, so we prevent it here. */
6958 if (!display_completed)
6959 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6960
6961 if (window_height_changed_p
6962 /* Don't do this if Emacs is shutting down. Redisplay
6963 needs to run hooks. */
6964 && !NILP (Vrun_hooks))
6965 {
6966 /* Must update other windows. Likewise as in other
6967 cases, don't let this update be interrupted by
6968 pending input. */
6969 int count = BINDING_STACK_SIZE ();
6970 specbind (Qredisplay_dont_pause, Qt);
6971 windows_or_buffers_changed = 1;
6972 redisplay_internal (0);
6973 unbind_to (count, Qnil);
6974 }
6975 else if (FRAME_WINDOW_P (f) && n == 0)
6976 {
6977 /* Window configuration is the same as before.
6978 Can do with a display update of the echo area,
6979 unless we displayed some mode lines. */
6980 update_single_window (w, 1);
6981 rif->flush_display (f);
6982 }
6983 else
6984 update_frame (f, 1, 1);
6985
6986 /* If cursor is in the echo area, make sure that the next
6987 redisplay displays the minibuffer, so that the cursor will
6988 be replaced with what the minibuffer wants. */
6989 if (cursor_in_echo_area)
6990 ++windows_or_buffers_changed;
6991 }
6992 }
6993 else if (!EQ (mini_window, selected_window))
6994 windows_or_buffers_changed++;
6995
6996 /* Last displayed message is now the current message. */
6997 echo_area_buffer[1] = echo_area_buffer[0];
6998
6999 /* Prevent redisplay optimization in redisplay_internal by resetting
7000 this_line_start_pos. This is done because the mini-buffer now
7001 displays the message instead of its buffer text. */
7002 if (EQ (mini_window, selected_window))
7003 CHARPOS (this_line_start_pos) = 0;
7004
7005 return window_height_changed_p;
7006 }
7007
7008
7009 \f
7010 /***********************************************************************
7011 Frame Titles
7012 ***********************************************************************/
7013
7014
7015 #ifdef HAVE_WINDOW_SYSTEM
7016
7017 /* A buffer for constructing frame titles in it; allocated from the
7018 heap in init_xdisp and resized as needed in store_frame_title_char. */
7019
7020 static char *frame_title_buf;
7021
7022 /* The buffer's end, and a current output position in it. */
7023
7024 static char *frame_title_buf_end;
7025 static char *frame_title_ptr;
7026
7027
7028 /* Store a single character C for the frame title in frame_title_buf.
7029 Re-allocate frame_title_buf if necessary. */
7030
7031 static void
7032 store_frame_title_char (c)
7033 char c;
7034 {
7035 /* If output position has reached the end of the allocated buffer,
7036 double the buffer's size. */
7037 if (frame_title_ptr == frame_title_buf_end)
7038 {
7039 int len = frame_title_ptr - frame_title_buf;
7040 int new_size = 2 * len * sizeof *frame_title_buf;
7041 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7042 frame_title_buf_end = frame_title_buf + new_size;
7043 frame_title_ptr = frame_title_buf + len;
7044 }
7045
7046 *frame_title_ptr++ = c;
7047 }
7048
7049
7050 /* Store part of a frame title in frame_title_buf, beginning at
7051 frame_title_ptr. STR is the string to store. Do not copy
7052 characters that yield more columns than PRECISION; PRECISION <= 0
7053 means copy the whole string. Pad with spaces until FIELD_WIDTH
7054 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7055 pad. Called from display_mode_element when it is used to build a
7056 frame title. */
7057
7058 static int
7059 store_frame_title (str, field_width, precision)
7060 unsigned char *str;
7061 int field_width, precision;
7062 {
7063 int n = 0;
7064 int dummy, nbytes, width;
7065
7066 /* Copy at most PRECISION chars from STR. */
7067 nbytes = strlen (str);
7068 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7069 while (nbytes--)
7070 store_frame_title_char (*str++);
7071
7072 /* Fill up with spaces until FIELD_WIDTH reached. */
7073 while (field_width > 0
7074 && n < field_width)
7075 {
7076 store_frame_title_char (' ');
7077 ++n;
7078 }
7079
7080 return n;
7081 }
7082
7083
7084 /* Set the title of FRAME, if it has changed. The title format is
7085 Vicon_title_format if FRAME is iconified, otherwise it is
7086 frame_title_format. */
7087
7088 static void
7089 x_consider_frame_title (frame)
7090 Lisp_Object frame;
7091 {
7092 struct frame *f = XFRAME (frame);
7093
7094 if (FRAME_WINDOW_P (f)
7095 || FRAME_MINIBUF_ONLY_P (f)
7096 || f->explicit_name)
7097 {
7098 /* Do we have more than one visible frame on this X display? */
7099 Lisp_Object tail;
7100 Lisp_Object fmt;
7101 struct buffer *obuf;
7102 int len;
7103 struct it it;
7104
7105 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7106 {
7107 struct frame *tf = XFRAME (XCAR (tail));
7108
7109 if (tf != f
7110 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7111 && !FRAME_MINIBUF_ONLY_P (tf)
7112 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7113 break;
7114 }
7115
7116 /* Set global variable indicating that multiple frames exist. */
7117 multiple_frames = CONSP (tail);
7118
7119 /* Switch to the buffer of selected window of the frame. Set up
7120 frame_title_ptr so that display_mode_element will output into it;
7121 then display the title. */
7122 obuf = current_buffer;
7123 Fset_buffer (XWINDOW (f->selected_window)->buffer);
7124 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7125 frame_title_ptr = frame_title_buf;
7126 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7127 NULL, DEFAULT_FACE_ID);
7128 display_mode_element (&it, 0, -1, -1, fmt);
7129 len = frame_title_ptr - frame_title_buf;
7130 frame_title_ptr = NULL;
7131 set_buffer_internal (obuf);
7132
7133 /* Set the title only if it's changed. This avoids consing in
7134 the common case where it hasn't. (If it turns out that we've
7135 already wasted too much time by walking through the list with
7136 display_mode_element, then we might need to optimize at a
7137 higher level than this.) */
7138 if (! STRINGP (f->name)
7139 || STRING_BYTES (XSTRING (f->name)) != len
7140 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7141 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7142 }
7143 }
7144
7145 #else /* not HAVE_WINDOW_SYSTEM */
7146
7147 #define frame_title_ptr ((char *)0)
7148 #define store_frame_title(str, mincol, maxcol) 0
7149
7150 #endif /* not HAVE_WINDOW_SYSTEM */
7151
7152
7153
7154 \f
7155 /***********************************************************************
7156 Menu Bars
7157 ***********************************************************************/
7158
7159
7160 /* Prepare for redisplay by updating menu-bar item lists when
7161 appropriate. This can call eval. */
7162
7163 void
7164 prepare_menu_bars ()
7165 {
7166 int all_windows;
7167 struct gcpro gcpro1, gcpro2;
7168 struct frame *f;
7169 Lisp_Object tooltip_frame;
7170
7171 #ifdef HAVE_X_WINDOWS
7172 tooltip_frame = tip_frame;
7173 #else
7174 tooltip_frame = Qnil;
7175 #endif
7176
7177 /* Update all frame titles based on their buffer names, etc. We do
7178 this before the menu bars so that the buffer-menu will show the
7179 up-to-date frame titles. */
7180 #ifdef HAVE_WINDOW_SYSTEM
7181 if (windows_or_buffers_changed || update_mode_lines)
7182 {
7183 Lisp_Object tail, frame;
7184
7185 FOR_EACH_FRAME (tail, frame)
7186 {
7187 f = XFRAME (frame);
7188 if (!EQ (frame, tooltip_frame)
7189 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7190 x_consider_frame_title (frame);
7191 }
7192 }
7193 #endif /* HAVE_WINDOW_SYSTEM */
7194
7195 /* Update the menu bar item lists, if appropriate. This has to be
7196 done before any actual redisplay or generation of display lines. */
7197 all_windows = (update_mode_lines
7198 || buffer_shared > 1
7199 || windows_or_buffers_changed);
7200 if (all_windows)
7201 {
7202 Lisp_Object tail, frame;
7203 int count = BINDING_STACK_SIZE ();
7204
7205 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7206
7207 FOR_EACH_FRAME (tail, frame)
7208 {
7209 f = XFRAME (frame);
7210
7211 /* Ignore tooltip frame. */
7212 if (EQ (frame, tooltip_frame))
7213 continue;
7214
7215 /* If a window on this frame changed size, report that to
7216 the user and clear the size-change flag. */
7217 if (FRAME_WINDOW_SIZES_CHANGED (f))
7218 {
7219 Lisp_Object functions;
7220
7221 /* Clear flag first in case we get an error below. */
7222 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7223 functions = Vwindow_size_change_functions;
7224 GCPRO2 (tail, functions);
7225
7226 while (CONSP (functions))
7227 {
7228 call1 (XCAR (functions), frame);
7229 functions = XCDR (functions);
7230 }
7231 UNGCPRO;
7232 }
7233
7234 GCPRO1 (tail);
7235 update_menu_bar (f, 0);
7236 #ifdef HAVE_WINDOW_SYSTEM
7237 update_tool_bar (f, 0);
7238 #endif
7239 UNGCPRO;
7240 }
7241
7242 unbind_to (count, Qnil);
7243 }
7244 else
7245 {
7246 struct frame *sf = SELECTED_FRAME ();
7247 update_menu_bar (sf, 1);
7248 #ifdef HAVE_WINDOW_SYSTEM
7249 update_tool_bar (sf, 1);
7250 #endif
7251 }
7252
7253 /* Motif needs this. See comment in xmenu.c. Turn it off when
7254 pending_menu_activation is not defined. */
7255 #ifdef USE_X_TOOLKIT
7256 pending_menu_activation = 0;
7257 #endif
7258 }
7259
7260
7261 /* Update the menu bar item list for frame F. This has to be done
7262 before we start to fill in any display lines, because it can call
7263 eval.
7264
7265 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7266
7267 static void
7268 update_menu_bar (f, save_match_data)
7269 struct frame *f;
7270 int save_match_data;
7271 {
7272 Lisp_Object window;
7273 register struct window *w;
7274
7275 /* If called recursively during a menu update, do nothing. This can
7276 happen when, for instance, an activate-menubar-hook causes a
7277 redisplay. */
7278 if (inhibit_menubar_update)
7279 return;
7280
7281 window = FRAME_SELECTED_WINDOW (f);
7282 w = XWINDOW (window);
7283
7284 if (update_mode_lines)
7285 w->update_mode_line = Qt;
7286
7287 if (FRAME_WINDOW_P (f)
7288 ?
7289 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7290 FRAME_EXTERNAL_MENU_BAR (f)
7291 #else
7292 FRAME_MENU_BAR_LINES (f) > 0
7293 #endif
7294 : FRAME_MENU_BAR_LINES (f) > 0)
7295 {
7296 /* If the user has switched buffers or windows, we need to
7297 recompute to reflect the new bindings. But we'll
7298 recompute when update_mode_lines is set too; that means
7299 that people can use force-mode-line-update to request
7300 that the menu bar be recomputed. The adverse effect on
7301 the rest of the redisplay algorithm is about the same as
7302 windows_or_buffers_changed anyway. */
7303 if (windows_or_buffers_changed
7304 || !NILP (w->update_mode_line)
7305 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7306 < BUF_MODIFF (XBUFFER (w->buffer)))
7307 != !NILP (w->last_had_star))
7308 || ((!NILP (Vtransient_mark_mode)
7309 && !NILP (XBUFFER (w->buffer)->mark_active))
7310 != !NILP (w->region_showing)))
7311 {
7312 struct buffer *prev = current_buffer;
7313 int count = BINDING_STACK_SIZE ();
7314
7315 specbind (Qinhibit_menubar_update, Qt);
7316
7317 set_buffer_internal_1 (XBUFFER (w->buffer));
7318 if (save_match_data)
7319 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7320 if (NILP (Voverriding_local_map_menu_flag))
7321 {
7322 specbind (Qoverriding_terminal_local_map, Qnil);
7323 specbind (Qoverriding_local_map, Qnil);
7324 }
7325
7326 /* Run the Lucid hook. */
7327 safe_run_hooks (Qactivate_menubar_hook);
7328
7329 /* If it has changed current-menubar from previous value,
7330 really recompute the menu-bar from the value. */
7331 if (! NILP (Vlucid_menu_bar_dirty_flag))
7332 call0 (Qrecompute_lucid_menubar);
7333
7334 safe_run_hooks (Qmenu_bar_update_hook);
7335 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7336
7337 /* Redisplay the menu bar in case we changed it. */
7338 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7339 if (FRAME_WINDOW_P (f)
7340 #if defined (macintosh)
7341 /* All frames on Mac OS share the same menubar. So only the
7342 selected frame should be allowed to set it. */
7343 && f == SELECTED_FRAME ()
7344 #endif
7345 )
7346 set_frame_menubar (f, 0, 0);
7347 else
7348 /* On a terminal screen, the menu bar is an ordinary screen
7349 line, and this makes it get updated. */
7350 w->update_mode_line = Qt;
7351 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7352 /* In the non-toolkit version, the menu bar is an ordinary screen
7353 line, and this makes it get updated. */
7354 w->update_mode_line = Qt;
7355 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7356
7357 unbind_to (count, Qnil);
7358 set_buffer_internal_1 (prev);
7359 }
7360 }
7361 }
7362
7363
7364 \f
7365 /***********************************************************************
7366 Tool-bars
7367 ***********************************************************************/
7368
7369 #ifdef HAVE_WINDOW_SYSTEM
7370
7371 /* Update the tool-bar item list for frame F. This has to be done
7372 before we start to fill in any display lines. Called from
7373 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7374 and restore it here. */
7375
7376 static void
7377 update_tool_bar (f, save_match_data)
7378 struct frame *f;
7379 int save_match_data;
7380 {
7381 if (WINDOWP (f->tool_bar_window)
7382 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7383 {
7384 Lisp_Object window;
7385 struct window *w;
7386
7387 window = FRAME_SELECTED_WINDOW (f);
7388 w = XWINDOW (window);
7389
7390 /* If the user has switched buffers or windows, we need to
7391 recompute to reflect the new bindings. But we'll
7392 recompute when update_mode_lines is set too; that means
7393 that people can use force-mode-line-update to request
7394 that the menu bar be recomputed. The adverse effect on
7395 the rest of the redisplay algorithm is about the same as
7396 windows_or_buffers_changed anyway. */
7397 if (windows_or_buffers_changed
7398 || !NILP (w->update_mode_line)
7399 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7400 < BUF_MODIFF (XBUFFER (w->buffer)))
7401 != !NILP (w->last_had_star))
7402 || ((!NILP (Vtransient_mark_mode)
7403 && !NILP (XBUFFER (w->buffer)->mark_active))
7404 != !NILP (w->region_showing)))
7405 {
7406 struct buffer *prev = current_buffer;
7407 int count = BINDING_STACK_SIZE ();
7408
7409 /* Set current_buffer to the buffer of the selected
7410 window of the frame, so that we get the right local
7411 keymaps. */
7412 set_buffer_internal_1 (XBUFFER (w->buffer));
7413
7414 /* Save match data, if we must. */
7415 if (save_match_data)
7416 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7417
7418 /* Make sure that we don't accidentally use bogus keymaps. */
7419 if (NILP (Voverriding_local_map_menu_flag))
7420 {
7421 specbind (Qoverriding_terminal_local_map, Qnil);
7422 specbind (Qoverriding_local_map, Qnil);
7423 }
7424
7425 /* Build desired tool-bar items from keymaps. */
7426 f->tool_bar_items
7427 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7428
7429 /* Redisplay the tool-bar in case we changed it. */
7430 w->update_mode_line = Qt;
7431
7432 unbind_to (count, Qnil);
7433 set_buffer_internal_1 (prev);
7434 }
7435 }
7436 }
7437
7438
7439 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7440 F's desired tool-bar contents. F->tool_bar_items must have
7441 been set up previously by calling prepare_menu_bars. */
7442
7443 static void
7444 build_desired_tool_bar_string (f)
7445 struct frame *f;
7446 {
7447 int i, size, size_needed;
7448 struct gcpro gcpro1, gcpro2, gcpro3;
7449 Lisp_Object image, plist, props;
7450
7451 image = plist = props = Qnil;
7452 GCPRO3 (image, plist, props);
7453
7454 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7455 Otherwise, make a new string. */
7456
7457 /* The size of the string we might be able to reuse. */
7458 size = (STRINGP (f->desired_tool_bar_string)
7459 ? XSTRING (f->desired_tool_bar_string)->size
7460 : 0);
7461
7462 /* We need one space in the string for each image. */
7463 size_needed = f->n_tool_bar_items;
7464
7465 /* Reuse f->desired_tool_bar_string, if possible. */
7466 if (size < size_needed || NILP (f->desired_tool_bar_string))
7467 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7468 make_number (' '));
7469 else
7470 {
7471 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7472 Fremove_text_properties (make_number (0), make_number (size),
7473 props, f->desired_tool_bar_string);
7474 }
7475
7476 /* Put a `display' property on the string for the images to display,
7477 put a `menu_item' property on tool-bar items with a value that
7478 is the index of the item in F's tool-bar item vector. */
7479 for (i = 0; i < f->n_tool_bar_items; ++i)
7480 {
7481 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7482
7483 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7484 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7485 int hmargin, vmargin, relief, idx, end;
7486 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7487 extern Lisp_Object Qlaplace;
7488
7489 /* If image is a vector, choose the image according to the
7490 button state. */
7491 image = PROP (TOOL_BAR_ITEM_IMAGES);
7492 if (VECTORP (image))
7493 {
7494 if (enabled_p)
7495 idx = (selected_p
7496 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7497 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7498 else
7499 idx = (selected_p
7500 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7501 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7502
7503 xassert (ASIZE (image) >= idx);
7504 image = AREF (image, idx);
7505 }
7506 else
7507 idx = -1;
7508
7509 /* Ignore invalid image specifications. */
7510 if (!valid_image_p (image))
7511 continue;
7512
7513 /* Display the tool-bar button pressed, or depressed. */
7514 plist = Fcopy_sequence (XCDR (image));
7515
7516 /* Compute margin and relief to draw. */
7517 relief = (tool_bar_button_relief > 0
7518 ? tool_bar_button_relief
7519 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7520 hmargin = vmargin = relief;
7521
7522 if (INTEGERP (Vtool_bar_button_margin)
7523 && XINT (Vtool_bar_button_margin) > 0)
7524 {
7525 hmargin += XFASTINT (Vtool_bar_button_margin);
7526 vmargin += XFASTINT (Vtool_bar_button_margin);
7527 }
7528 else if (CONSP (Vtool_bar_button_margin))
7529 {
7530 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7531 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7532 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7533
7534 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7535 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7536 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7537 }
7538
7539 if (auto_raise_tool_bar_buttons_p)
7540 {
7541 /* Add a `:relief' property to the image spec if the item is
7542 selected. */
7543 if (selected_p)
7544 {
7545 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7546 hmargin -= relief;
7547 vmargin -= relief;
7548 }
7549 }
7550 else
7551 {
7552 /* If image is selected, display it pressed, i.e. with a
7553 negative relief. If it's not selected, display it with a
7554 raised relief. */
7555 plist = Fplist_put (plist, QCrelief,
7556 (selected_p
7557 ? make_number (-relief)
7558 : make_number (relief)));
7559 hmargin -= relief;
7560 vmargin -= relief;
7561 }
7562
7563 /* Put a margin around the image. */
7564 if (hmargin || vmargin)
7565 {
7566 if (hmargin == vmargin)
7567 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7568 else
7569 plist = Fplist_put (plist, QCmargin,
7570 Fcons (make_number (hmargin),
7571 make_number (vmargin)));
7572 }
7573
7574 /* If button is not enabled, and we don't have special images
7575 for the disabled state, make the image appear disabled by
7576 applying an appropriate algorithm to it. */
7577 if (!enabled_p && idx < 0)
7578 plist = Fplist_put (plist, QCconversion, Qdisabled);
7579
7580 /* Put a `display' text property on the string for the image to
7581 display. Put a `menu-item' property on the string that gives
7582 the start of this item's properties in the tool-bar items
7583 vector. */
7584 image = Fcons (Qimage, plist);
7585 props = list4 (Qdisplay, image,
7586 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7587
7588 /* Let the last image hide all remaining spaces in the tool bar
7589 string. The string can be longer than needed when we reuse a
7590 previous string. */
7591 if (i + 1 == f->n_tool_bar_items)
7592 end = XSTRING (f->desired_tool_bar_string)->size;
7593 else
7594 end = i + 1;
7595 Fadd_text_properties (make_number (i), make_number (end),
7596 props, f->desired_tool_bar_string);
7597 #undef PROP
7598 }
7599
7600 UNGCPRO;
7601 }
7602
7603
7604 /* Display one line of the tool-bar of frame IT->f. */
7605
7606 static void
7607 display_tool_bar_line (it)
7608 struct it *it;
7609 {
7610 struct glyph_row *row = it->glyph_row;
7611 int max_x = it->last_visible_x;
7612 struct glyph *last;
7613
7614 prepare_desired_row (row);
7615 row->y = it->current_y;
7616
7617 /* Note that this isn't made use of if the face hasn't a box,
7618 so there's no need to check the face here. */
7619 it->start_of_box_run_p = 1;
7620
7621 while (it->current_x < max_x)
7622 {
7623 int x_before, x, n_glyphs_before, i, nglyphs;
7624
7625 /* Get the next display element. */
7626 if (!get_next_display_element (it))
7627 break;
7628
7629 /* Produce glyphs. */
7630 x_before = it->current_x;
7631 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7632 PRODUCE_GLYPHS (it);
7633
7634 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7635 i = 0;
7636 x = x_before;
7637 while (i < nglyphs)
7638 {
7639 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7640
7641 if (x + glyph->pixel_width > max_x)
7642 {
7643 /* Glyph doesn't fit on line. */
7644 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7645 it->current_x = x;
7646 goto out;
7647 }
7648
7649 ++it->hpos;
7650 x += glyph->pixel_width;
7651 ++i;
7652 }
7653
7654 /* Stop at line ends. */
7655 if (ITERATOR_AT_END_OF_LINE_P (it))
7656 break;
7657
7658 set_iterator_to_next (it, 1);
7659 }
7660
7661 out:;
7662
7663 row->displays_text_p = row->used[TEXT_AREA] != 0;
7664 extend_face_to_end_of_line (it);
7665 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7666 last->right_box_line_p = 1;
7667 if (last == row->glyphs[TEXT_AREA])
7668 last->left_box_line_p = 1;
7669 compute_line_metrics (it);
7670
7671 /* If line is empty, make it occupy the rest of the tool-bar. */
7672 if (!row->displays_text_p)
7673 {
7674 row->height = row->phys_height = it->last_visible_y - row->y;
7675 row->ascent = row->phys_ascent = 0;
7676 }
7677
7678 row->full_width_p = 1;
7679 row->continued_p = 0;
7680 row->truncated_on_left_p = 0;
7681 row->truncated_on_right_p = 0;
7682
7683 it->current_x = it->hpos = 0;
7684 it->current_y += row->height;
7685 ++it->vpos;
7686 ++it->glyph_row;
7687 }
7688
7689
7690 /* Value is the number of screen lines needed to make all tool-bar
7691 items of frame F visible. */
7692
7693 static int
7694 tool_bar_lines_needed (f)
7695 struct frame *f;
7696 {
7697 struct window *w = XWINDOW (f->tool_bar_window);
7698 struct it it;
7699
7700 /* Initialize an iterator for iteration over
7701 F->desired_tool_bar_string in the tool-bar window of frame F. */
7702 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7703 it.first_visible_x = 0;
7704 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7705 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7706
7707 while (!ITERATOR_AT_END_P (&it))
7708 {
7709 it.glyph_row = w->desired_matrix->rows;
7710 clear_glyph_row (it.glyph_row);
7711 display_tool_bar_line (&it);
7712 }
7713
7714 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7715 }
7716
7717
7718 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7719 0, 1, 0,
7720 "Return the number of lines occupied by the tool bar of FRAME.")
7721 (frame)
7722 Lisp_Object frame;
7723 {
7724 struct frame *f;
7725 struct window *w;
7726 int nlines = 0;
7727
7728 if (NILP (frame))
7729 frame = selected_frame;
7730 else
7731 CHECK_FRAME (frame, 0);
7732 f = XFRAME (frame);
7733
7734 if (WINDOWP (f->tool_bar_window)
7735 || (w = XWINDOW (f->tool_bar_window),
7736 XFASTINT (w->height) > 0))
7737 {
7738 update_tool_bar (f, 1);
7739 if (f->n_tool_bar_items)
7740 {
7741 build_desired_tool_bar_string (f);
7742 nlines = tool_bar_lines_needed (f);
7743 }
7744 }
7745
7746 return make_number (nlines);
7747 }
7748
7749
7750 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7751 height should be changed. */
7752
7753 static int
7754 redisplay_tool_bar (f)
7755 struct frame *f;
7756 {
7757 struct window *w;
7758 struct it it;
7759 struct glyph_row *row;
7760 int change_height_p = 0;
7761
7762 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7763 do anything. This means you must start with tool-bar-lines
7764 non-zero to get the auto-sizing effect. Or in other words, you
7765 can turn off tool-bars by specifying tool-bar-lines zero. */
7766 if (!WINDOWP (f->tool_bar_window)
7767 || (w = XWINDOW (f->tool_bar_window),
7768 XFASTINT (w->height) == 0))
7769 return 0;
7770
7771 /* Set up an iterator for the tool-bar window. */
7772 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7773 it.first_visible_x = 0;
7774 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7775 row = it.glyph_row;
7776
7777 /* Build a string that represents the contents of the tool-bar. */
7778 build_desired_tool_bar_string (f);
7779 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7780
7781 /* Display as many lines as needed to display all tool-bar items. */
7782 while (it.current_y < it.last_visible_y)
7783 display_tool_bar_line (&it);
7784
7785 /* It doesn't make much sense to try scrolling in the tool-bar
7786 window, so don't do it. */
7787 w->desired_matrix->no_scrolling_p = 1;
7788 w->must_be_updated_p = 1;
7789
7790 if (auto_resize_tool_bars_p)
7791 {
7792 int nlines;
7793
7794 /* If we couldn't display everything, change the tool-bar's
7795 height. */
7796 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7797 change_height_p = 1;
7798
7799 /* If there are blank lines at the end, except for a partially
7800 visible blank line at the end that is smaller than
7801 CANON_Y_UNIT, change the tool-bar's height. */
7802 row = it.glyph_row - 1;
7803 if (!row->displays_text_p
7804 && row->height >= CANON_Y_UNIT (f))
7805 change_height_p = 1;
7806
7807 /* If row displays tool-bar items, but is partially visible,
7808 change the tool-bar's height. */
7809 if (row->displays_text_p
7810 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7811 change_height_p = 1;
7812
7813 /* Resize windows as needed by changing the `tool-bar-lines'
7814 frame parameter. */
7815 if (change_height_p
7816 && (nlines = tool_bar_lines_needed (f),
7817 nlines != XFASTINT (w->height)))
7818 {
7819 extern Lisp_Object Qtool_bar_lines;
7820 Lisp_Object frame;
7821 int old_height = XFASTINT (w->height);
7822
7823 XSETFRAME (frame, f);
7824 clear_glyph_matrix (w->desired_matrix);
7825 Fmodify_frame_parameters (frame,
7826 Fcons (Fcons (Qtool_bar_lines,
7827 make_number (nlines)),
7828 Qnil));
7829 if (XFASTINT (w->height) != old_height)
7830 fonts_changed_p = 1;
7831 }
7832 }
7833
7834 return change_height_p;
7835 }
7836
7837
7838 /* Get information about the tool-bar item which is displayed in GLYPH
7839 on frame F. Return in *PROP_IDX the index where tool-bar item
7840 properties start in F->tool_bar_items. Value is zero if
7841 GLYPH doesn't display a tool-bar item. */
7842
7843 int
7844 tool_bar_item_info (f, glyph, prop_idx)
7845 struct frame *f;
7846 struct glyph *glyph;
7847 int *prop_idx;
7848 {
7849 Lisp_Object prop;
7850 int success_p;
7851
7852 /* Get the text property `menu-item' at pos. The value of that
7853 property is the start index of this item's properties in
7854 F->tool_bar_items. */
7855 prop = Fget_text_property (make_number (glyph->charpos),
7856 Qmenu_item, f->current_tool_bar_string);
7857 if (INTEGERP (prop))
7858 {
7859 *prop_idx = XINT (prop);
7860 success_p = 1;
7861 }
7862 else
7863 success_p = 0;
7864
7865 return success_p;
7866 }
7867
7868 #endif /* HAVE_WINDOW_SYSTEM */
7869
7870
7871 \f
7872 /************************************************************************
7873 Horizontal scrolling
7874 ************************************************************************/
7875
7876 static int hscroll_window_tree P_ ((Lisp_Object));
7877 static int hscroll_windows P_ ((Lisp_Object));
7878
7879 /* For all leaf windows in the window tree rooted at WINDOW, set their
7880 hscroll value so that PT is (i) visible in the window, and (ii) so
7881 that it is not within a certain margin at the window's left and
7882 right border. Value is non-zero if any window's hscroll has been
7883 changed. */
7884
7885 static int
7886 hscroll_window_tree (window)
7887 Lisp_Object window;
7888 {
7889 int hscrolled_p = 0;
7890
7891 while (WINDOWP (window))
7892 {
7893 struct window *w = XWINDOW (window);
7894
7895 if (WINDOWP (w->hchild))
7896 hscrolled_p |= hscroll_window_tree (w->hchild);
7897 else if (WINDOWP (w->vchild))
7898 hscrolled_p |= hscroll_window_tree (w->vchild);
7899 else if (w->cursor.vpos >= 0)
7900 {
7901 int hscroll_margin, text_area_x, text_area_y;
7902 int text_area_width, text_area_height;
7903 struct glyph_row *current_cursor_row
7904 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7905 struct glyph_row *desired_cursor_row
7906 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7907 struct glyph_row *cursor_row
7908 = (desired_cursor_row->enabled_p
7909 ? desired_cursor_row
7910 : current_cursor_row);
7911
7912 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7913 &text_area_width, &text_area_height);
7914
7915 /* Scroll when cursor is inside this scroll margin. */
7916 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7917
7918 if ((XFASTINT (w->hscroll)
7919 && w->cursor.x < hscroll_margin)
7920 || (cursor_row->enabled_p
7921 && cursor_row->truncated_on_right_p
7922 && (w->cursor.x > text_area_width - hscroll_margin)))
7923 {
7924 struct it it;
7925 int hscroll;
7926 struct buffer *saved_current_buffer;
7927 int pt;
7928
7929 /* Find point in a display of infinite width. */
7930 saved_current_buffer = current_buffer;
7931 current_buffer = XBUFFER (w->buffer);
7932
7933 if (w == XWINDOW (selected_window))
7934 pt = BUF_PT (current_buffer);
7935 else
7936 {
7937 pt = marker_position (w->pointm);
7938 pt = max (BEGV, pt);
7939 pt = min (ZV, pt);
7940 }
7941
7942 /* Move iterator to pt starting at cursor_row->start in
7943 a line with infinite width. */
7944 init_to_row_start (&it, w, cursor_row);
7945 it.last_visible_x = INFINITY;
7946 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7947 current_buffer = saved_current_buffer;
7948
7949 /* Center cursor in window. */
7950 hscroll = (max (0, it.current_x - text_area_width / 2)
7951 / CANON_X_UNIT (it.f));
7952 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
7953
7954 /* Don't call Fset_window_hscroll if value hasn't
7955 changed because it will prevent redisplay
7956 optimizations. */
7957 if (XFASTINT (w->hscroll) != hscroll)
7958 {
7959 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
7960 w->hscroll = make_number (hscroll);
7961 hscrolled_p = 1;
7962 }
7963 }
7964 }
7965
7966 window = w->next;
7967 }
7968
7969 /* Value is non-zero if hscroll of any leaf window has been changed. */
7970 return hscrolled_p;
7971 }
7972
7973
7974 /* Set hscroll so that cursor is visible and not inside horizontal
7975 scroll margins for all windows in the tree rooted at WINDOW. See
7976 also hscroll_window_tree above. Value is non-zero if any window's
7977 hscroll has been changed. If it has, desired matrices on the frame
7978 of WINDOW are cleared. */
7979
7980 static int
7981 hscroll_windows (window)
7982 Lisp_Object window;
7983 {
7984 int hscrolled_p;
7985
7986 if (automatic_hscrolling_p)
7987 {
7988 hscrolled_p = hscroll_window_tree (window);
7989 if (hscrolled_p)
7990 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7991 }
7992 else
7993 hscrolled_p = 0;
7994 return hscrolled_p;
7995 }
7996
7997
7998 \f
7999 /************************************************************************
8000 Redisplay
8001 ************************************************************************/
8002
8003 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8004 to a non-zero value. This is sometimes handy to have in a debugger
8005 session. */
8006
8007 #if GLYPH_DEBUG
8008
8009 /* First and last unchanged row for try_window_id. */
8010
8011 int debug_first_unchanged_at_end_vpos;
8012 int debug_last_unchanged_at_beg_vpos;
8013
8014 /* Delta vpos and y. */
8015
8016 int debug_dvpos, debug_dy;
8017
8018 /* Delta in characters and bytes for try_window_id. */
8019
8020 int debug_delta, debug_delta_bytes;
8021
8022 /* Values of window_end_pos and window_end_vpos at the end of
8023 try_window_id. */
8024
8025 int debug_end_pos, debug_end_vpos;
8026
8027 /* Append a string to W->desired_matrix->method. FMT is a printf
8028 format string. A1...A9 are a supplement for a variable-length
8029 argument list. If trace_redisplay_p is non-zero also printf the
8030 resulting string to stderr. */
8031
8032 static void
8033 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8034 struct window *w;
8035 char *fmt;
8036 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8037 {
8038 char buffer[512];
8039 char *method = w->desired_matrix->method;
8040 int len = strlen (method);
8041 int size = sizeof w->desired_matrix->method;
8042 int remaining = size - len - 1;
8043
8044 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8045 if (len && remaining)
8046 {
8047 method[len] = '|';
8048 --remaining, ++len;
8049 }
8050
8051 strncpy (method + len, buffer, remaining);
8052
8053 if (trace_redisplay_p)
8054 fprintf (stderr, "%p (%s): %s\n",
8055 w,
8056 ((BUFFERP (w->buffer)
8057 && STRINGP (XBUFFER (w->buffer)->name))
8058 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
8059 : "no buffer"),
8060 buffer);
8061 }
8062
8063 #endif /* GLYPH_DEBUG */
8064
8065
8066 /* This counter is used to clear the face cache every once in a while
8067 in redisplay_internal. It is incremented for each redisplay.
8068 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8069 cleared. */
8070
8071 #define CLEAR_FACE_CACHE_COUNT 10000
8072 static int clear_face_cache_count;
8073
8074 /* Record the previous terminal frame we displayed. */
8075
8076 static struct frame *previous_terminal_frame;
8077
8078 /* Non-zero while redisplay_internal is in progress. */
8079
8080 int redisplaying_p;
8081
8082
8083 /* Value is non-zero if all changes in window W, which displays
8084 current_buffer, are in the text between START and END. START is a
8085 buffer position, END is given as a distance from Z. Used in
8086 redisplay_internal for display optimization. */
8087
8088 static INLINE int
8089 text_outside_line_unchanged_p (w, start, end)
8090 struct window *w;
8091 int start, end;
8092 {
8093 int unchanged_p = 1;
8094
8095 /* If text or overlays have changed, see where. */
8096 if (XFASTINT (w->last_modified) < MODIFF
8097 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8098 {
8099 /* Gap in the line? */
8100 if (GPT < start || Z - GPT < end)
8101 unchanged_p = 0;
8102
8103 /* Changes start in front of the line, or end after it? */
8104 if (unchanged_p
8105 && (BEG_UNCHANGED < start - 1
8106 || END_UNCHANGED < end))
8107 unchanged_p = 0;
8108
8109 /* If selective display, can't optimize if changes start at the
8110 beginning of the line. */
8111 if (unchanged_p
8112 && INTEGERP (current_buffer->selective_display)
8113 && XINT (current_buffer->selective_display) > 0
8114 && (BEG_UNCHANGED < start || GPT <= start))
8115 unchanged_p = 0;
8116 }
8117
8118 return unchanged_p;
8119 }
8120
8121
8122 /* Do a frame update, taking possible shortcuts into account. This is
8123 the main external entry point for redisplay.
8124
8125 If the last redisplay displayed an echo area message and that message
8126 is no longer requested, we clear the echo area or bring back the
8127 mini-buffer if that is in use. */
8128
8129 void
8130 redisplay ()
8131 {
8132 redisplay_internal (0);
8133 }
8134
8135 /* Return 1 if point moved out of or into a composition. Otherwise
8136 return 0. PREV_BUF and PREV_PT are the last point buffer and
8137 position. BUF and PT are the current point buffer and position. */
8138
8139 int
8140 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8141 struct buffer *prev_buf, *buf;
8142 int prev_pt, pt;
8143 {
8144 int start, end;
8145 Lisp_Object prop;
8146 Lisp_Object buffer;
8147
8148 XSETBUFFER (buffer, buf);
8149 /* Check a composition at the last point if point moved within the
8150 same buffer. */
8151 if (prev_buf == buf)
8152 {
8153 if (prev_pt == pt)
8154 /* Point didn't move. */
8155 return 0;
8156
8157 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8158 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8159 && COMPOSITION_VALID_P (start, end, prop)
8160 && start < prev_pt && end > prev_pt)
8161 /* The last point was within the composition. Return 1 iff
8162 point moved out of the composition. */
8163 return (pt <= start || pt >= end);
8164 }
8165
8166 /* Check a composition at the current point. */
8167 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8168 && find_composition (pt, -1, &start, &end, &prop, buffer)
8169 && COMPOSITION_VALID_P (start, end, prop)
8170 && start < pt && end > pt);
8171 }
8172
8173 /* Reconsider the setting of B->clip_changed which is displayed
8174 in window W. */
8175
8176 static INLINE void
8177 reconsider_clip_changes (w, b)
8178 struct window *w;
8179 struct buffer *b;
8180 {
8181 if (b->prevent_redisplay_optimizations_p)
8182 b->clip_changed = 1;
8183 else if (b->clip_changed
8184 && !NILP (w->window_end_valid)
8185 && w->current_matrix->buffer == b
8186 && w->current_matrix->zv == BUF_ZV (b)
8187 && w->current_matrix->begv == BUF_BEGV (b))
8188 b->clip_changed = 0;
8189
8190 /* If display wasn't paused, and W is not a tool bar window, see if
8191 point has been moved into or out of a composition. In that case,
8192 we set b->clip_changed to 1 to force updating the screen. If
8193 b->clip_changed has already been set to 1, we can skip this
8194 check. */
8195 if (!b->clip_changed
8196 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8197 {
8198 int pt;
8199
8200 if (w == XWINDOW (selected_window))
8201 pt = BUF_PT (current_buffer);
8202 else
8203 pt = marker_position (w->pointm);
8204
8205 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8206 || pt != XINT (w->last_point))
8207 && check_point_in_composition (w->current_matrix->buffer,
8208 XINT (w->last_point),
8209 XBUFFER (w->buffer), pt))
8210 b->clip_changed = 1;
8211 }
8212 }
8213
8214
8215 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8216 response to any user action; therefore, we should preserve the echo
8217 area. (Actually, our caller does that job.) Perhaps in the future
8218 avoid recentering windows if it is not necessary; currently that
8219 causes some problems. */
8220
8221 static void
8222 redisplay_internal (preserve_echo_area)
8223 int preserve_echo_area;
8224 {
8225 struct window *w = XWINDOW (selected_window);
8226 struct frame *f = XFRAME (w->frame);
8227 int pause;
8228 int must_finish = 0;
8229 struct text_pos tlbufpos, tlendpos;
8230 int number_of_visible_frames;
8231 int count;
8232 struct frame *sf = SELECTED_FRAME ();
8233
8234 /* Non-zero means redisplay has to consider all windows on all
8235 frames. Zero means, only selected_window is considered. */
8236 int consider_all_windows_p;
8237
8238 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8239
8240 /* No redisplay if running in batch mode or frame is not yet fully
8241 initialized, or redisplay is explicitly turned off by setting
8242 Vinhibit_redisplay. */
8243 if (noninteractive
8244 || !NILP (Vinhibit_redisplay)
8245 || !f->glyphs_initialized_p)
8246 return;
8247
8248 /* The flag redisplay_performed_directly_p is set by
8249 direct_output_for_insert when it already did the whole screen
8250 update necessary. */
8251 if (redisplay_performed_directly_p)
8252 {
8253 redisplay_performed_directly_p = 0;
8254 if (!hscroll_windows (selected_window))
8255 return;
8256 }
8257
8258 #ifdef USE_X_TOOLKIT
8259 if (popup_activated ())
8260 return;
8261 #endif
8262
8263 /* I don't think this happens but let's be paranoid. */
8264 if (redisplaying_p)
8265 return;
8266
8267 /* Record a function that resets redisplaying_p to its old value
8268 when we leave this function. */
8269 count = BINDING_STACK_SIZE ();
8270 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8271 ++redisplaying_p;
8272
8273 retry:
8274 pause = 0;
8275 reconsider_clip_changes (w, current_buffer);
8276
8277 /* If new fonts have been loaded that make a glyph matrix adjustment
8278 necessary, do it. */
8279 if (fonts_changed_p)
8280 {
8281 adjust_glyphs (NULL);
8282 ++windows_or_buffers_changed;
8283 fonts_changed_p = 0;
8284 }
8285
8286 /* If face_change_count is non-zero, init_iterator will free all
8287 realized faces, which includes the faces referenced from current
8288 matrices. So, we can't reuse current matrices in this case. */
8289 if (face_change_count)
8290 ++windows_or_buffers_changed;
8291
8292 if (! FRAME_WINDOW_P (sf)
8293 && previous_terminal_frame != sf)
8294 {
8295 /* Since frames on an ASCII terminal share the same display
8296 area, displaying a different frame means redisplay the whole
8297 thing. */
8298 windows_or_buffers_changed++;
8299 SET_FRAME_GARBAGED (sf);
8300 XSETFRAME (Vterminal_frame, sf);
8301 }
8302 previous_terminal_frame = sf;
8303
8304 /* Set the visible flags for all frames. Do this before checking
8305 for resized or garbaged frames; they want to know if their frames
8306 are visible. See the comment in frame.h for
8307 FRAME_SAMPLE_VISIBILITY. */
8308 {
8309 Lisp_Object tail, frame;
8310
8311 number_of_visible_frames = 0;
8312
8313 FOR_EACH_FRAME (tail, frame)
8314 {
8315 struct frame *f = XFRAME (frame);
8316
8317 FRAME_SAMPLE_VISIBILITY (f);
8318 if (FRAME_VISIBLE_P (f))
8319 ++number_of_visible_frames;
8320 clear_desired_matrices (f);
8321 }
8322 }
8323
8324 /* Notice any pending interrupt request to change frame size. */
8325 do_pending_window_change (1);
8326
8327 /* Clear frames marked as garbaged. */
8328 if (frame_garbaged)
8329 clear_garbaged_frames ();
8330
8331 /* Build menubar and tool-bar items. */
8332 prepare_menu_bars ();
8333
8334 if (windows_or_buffers_changed)
8335 update_mode_lines++;
8336
8337 /* Detect case that we need to write or remove a star in the mode line. */
8338 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8339 {
8340 w->update_mode_line = Qt;
8341 if (buffer_shared > 1)
8342 update_mode_lines++;
8343 }
8344
8345 /* If %c is in the mode line, update it if needed. */
8346 if (!NILP (w->column_number_displayed)
8347 /* This alternative quickly identifies a common case
8348 where no change is needed. */
8349 && !(PT == XFASTINT (w->last_point)
8350 && XFASTINT (w->last_modified) >= MODIFF
8351 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8352 && XFASTINT (w->column_number_displayed) != current_column ())
8353 w->update_mode_line = Qt;
8354
8355 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8356
8357 /* The variable buffer_shared is set in redisplay_window and
8358 indicates that we redisplay a buffer in different windows. See
8359 there. */
8360 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8361
8362 /* If specs for an arrow have changed, do thorough redisplay
8363 to ensure we remove any arrow that should no longer exist. */
8364 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8365 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8366 consider_all_windows_p = windows_or_buffers_changed = 1;
8367
8368 /* Normally the message* functions will have already displayed and
8369 updated the echo area, but the frame may have been trashed, or
8370 the update may have been preempted, so display the echo area
8371 again here. Checking both message buffers captures the case that
8372 the echo area should be cleared. */
8373 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
8374 {
8375 int window_height_changed_p = echo_area_display (0);
8376 must_finish = 1;
8377
8378 if (fonts_changed_p)
8379 goto retry;
8380 else if (window_height_changed_p)
8381 {
8382 consider_all_windows_p = 1;
8383 ++update_mode_lines;
8384 ++windows_or_buffers_changed;
8385
8386 /* If window configuration was changed, frames may have been
8387 marked garbaged. Clear them or we will experience
8388 surprises wrt scrolling. */
8389 if (frame_garbaged)
8390 clear_garbaged_frames ();
8391 }
8392 }
8393 else if (EQ (selected_window, minibuf_window)
8394 && (current_buffer->clip_changed
8395 || XFASTINT (w->last_modified) < MODIFF
8396 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8397 && resize_mini_window (w, 0))
8398 {
8399 /* Resized active mini-window to fit the size of what it is
8400 showing if its contents might have changed. */
8401 must_finish = 1;
8402 consider_all_windows_p = 1;
8403 ++windows_or_buffers_changed;
8404 ++update_mode_lines;
8405
8406 /* If window configuration was changed, frames may have been
8407 marked garbaged. Clear them or we will experience
8408 surprises wrt scrolling. */
8409 if (frame_garbaged)
8410 clear_garbaged_frames ();
8411 }
8412
8413
8414 /* If showing the region, and mark has changed, we must redisplay
8415 the whole window. The assignment to this_line_start_pos prevents
8416 the optimization directly below this if-statement. */
8417 if (((!NILP (Vtransient_mark_mode)
8418 && !NILP (XBUFFER (w->buffer)->mark_active))
8419 != !NILP (w->region_showing))
8420 || (!NILP (w->region_showing)
8421 && !EQ (w->region_showing,
8422 Fmarker_position (XBUFFER (w->buffer)->mark))))
8423 CHARPOS (this_line_start_pos) = 0;
8424
8425 /* Optimize the case that only the line containing the cursor in the
8426 selected window has changed. Variables starting with this_ are
8427 set in display_line and record information about the line
8428 containing the cursor. */
8429 tlbufpos = this_line_start_pos;
8430 tlendpos = this_line_end_pos;
8431 if (!consider_all_windows_p
8432 && CHARPOS (tlbufpos) > 0
8433 && NILP (w->update_mode_line)
8434 && !current_buffer->clip_changed
8435 && FRAME_VISIBLE_P (XFRAME (w->frame))
8436 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8437 /* Make sure recorded data applies to current buffer, etc. */
8438 && this_line_buffer == current_buffer
8439 && current_buffer == XBUFFER (w->buffer)
8440 && NILP (w->force_start)
8441 /* Point must be on the line that we have info recorded about. */
8442 && PT >= CHARPOS (tlbufpos)
8443 && PT <= Z - CHARPOS (tlendpos)
8444 /* All text outside that line, including its final newline,
8445 must be unchanged */
8446 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8447 CHARPOS (tlendpos)))
8448 {
8449 if (CHARPOS (tlbufpos) > BEGV
8450 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8451 && (CHARPOS (tlbufpos) == ZV
8452 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8453 /* Former continuation line has disappeared by becoming empty */
8454 goto cancel;
8455 else if (XFASTINT (w->last_modified) < MODIFF
8456 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8457 || MINI_WINDOW_P (w))
8458 {
8459 /* We have to handle the case of continuation around a
8460 wide-column character (See the comment in indent.c around
8461 line 885).
8462
8463 For instance, in the following case:
8464
8465 -------- Insert --------
8466 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8467 J_I_ ==> J_I_ `^^' are cursors.
8468 ^^ ^^
8469 -------- --------
8470
8471 As we have to redraw the line above, we should goto cancel. */
8472
8473 struct it it;
8474 int line_height_before = this_line_pixel_height;
8475
8476 /* Note that start_display will handle the case that the
8477 line starting at tlbufpos is a continuation lines. */
8478 start_display (&it, w, tlbufpos);
8479
8480 /* Implementation note: It this still necessary? */
8481 if (it.current_x != this_line_start_x)
8482 goto cancel;
8483
8484 TRACE ((stderr, "trying display optimization 1\n"));
8485 w->cursor.vpos = -1;
8486 overlay_arrow_seen = 0;
8487 it.vpos = this_line_vpos;
8488 it.current_y = this_line_y;
8489 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8490 display_line (&it);
8491
8492 /* If line contains point, is not continued,
8493 and ends at same distance from eob as before, we win */
8494 if (w->cursor.vpos >= 0
8495 /* Line is not continued, otherwise this_line_start_pos
8496 would have been set to 0 in display_line. */
8497 && CHARPOS (this_line_start_pos)
8498 /* Line ends as before. */
8499 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8500 /* Line has same height as before. Otherwise other lines
8501 would have to be shifted up or down. */
8502 && this_line_pixel_height == line_height_before)
8503 {
8504 /* If this is not the window's last line, we must adjust
8505 the charstarts of the lines below. */
8506 if (it.current_y < it.last_visible_y)
8507 {
8508 struct glyph_row *row
8509 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8510 int delta, delta_bytes;
8511
8512 if (Z - CHARPOS (tlendpos) == ZV)
8513 {
8514 /* This line ends at end of (accessible part of)
8515 buffer. There is no newline to count. */
8516 delta = (Z
8517 - CHARPOS (tlendpos)
8518 - MATRIX_ROW_START_CHARPOS (row));
8519 delta_bytes = (Z_BYTE
8520 - BYTEPOS (tlendpos)
8521 - MATRIX_ROW_START_BYTEPOS (row));
8522 }
8523 else
8524 {
8525 /* This line ends in a newline. Must take
8526 account of the newline and the rest of the
8527 text that follows. */
8528 delta = (Z
8529 - CHARPOS (tlendpos)
8530 - MATRIX_ROW_START_CHARPOS (row));
8531 delta_bytes = (Z_BYTE
8532 - BYTEPOS (tlendpos)
8533 - MATRIX_ROW_START_BYTEPOS (row));
8534 }
8535
8536 increment_matrix_positions (w->current_matrix,
8537 this_line_vpos + 1,
8538 w->current_matrix->nrows,
8539 delta, delta_bytes);
8540 }
8541
8542 /* If this row displays text now but previously didn't,
8543 or vice versa, w->window_end_vpos may have to be
8544 adjusted. */
8545 if ((it.glyph_row - 1)->displays_text_p)
8546 {
8547 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8548 XSETINT (w->window_end_vpos, this_line_vpos);
8549 }
8550 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8551 && this_line_vpos > 0)
8552 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8553 w->window_end_valid = Qnil;
8554
8555 /* Update hint: No need to try to scroll in update_window. */
8556 w->desired_matrix->no_scrolling_p = 1;
8557
8558 #if GLYPH_DEBUG
8559 *w->desired_matrix->method = 0;
8560 debug_method_add (w, "optimization 1");
8561 #endif
8562 goto update;
8563 }
8564 else
8565 goto cancel;
8566 }
8567 else if (/* Cursor position hasn't changed. */
8568 PT == XFASTINT (w->last_point)
8569 /* Make sure the cursor was last displayed
8570 in this window. Otherwise we have to reposition it. */
8571 && 0 <= w->cursor.vpos
8572 && XINT (w->height) > w->cursor.vpos)
8573 {
8574 if (!must_finish)
8575 {
8576 do_pending_window_change (1);
8577
8578 /* We used to always goto end_of_redisplay here, but this
8579 isn't enough if we have a blinking cursor. */
8580 if (w->cursor_off_p == w->last_cursor_off_p)
8581 goto end_of_redisplay;
8582 }
8583 goto update;
8584 }
8585 /* If highlighting the region, or if the cursor is in the echo area,
8586 then we can't just move the cursor. */
8587 else if (! (!NILP (Vtransient_mark_mode)
8588 && !NILP (current_buffer->mark_active))
8589 && (EQ (selected_window, current_buffer->last_selected_window)
8590 || highlight_nonselected_windows)
8591 && NILP (w->region_showing)
8592 && NILP (Vshow_trailing_whitespace)
8593 && !cursor_in_echo_area)
8594 {
8595 struct it it;
8596 struct glyph_row *row;
8597
8598 /* Skip from tlbufpos to PT and see where it is. Note that
8599 PT may be in invisible text. If so, we will end at the
8600 next visible position. */
8601 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8602 NULL, DEFAULT_FACE_ID);
8603 it.current_x = this_line_start_x;
8604 it.current_y = this_line_y;
8605 it.vpos = this_line_vpos;
8606
8607 /* The call to move_it_to stops in front of PT, but
8608 moves over before-strings. */
8609 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8610
8611 if (it.vpos == this_line_vpos
8612 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8613 row->enabled_p))
8614 {
8615 xassert (this_line_vpos == it.vpos);
8616 xassert (this_line_y == it.current_y);
8617 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8618 #if GLYPH_DEBUG
8619 *w->desired_matrix->method = 0;
8620 debug_method_add (w, "optimization 3");
8621 #endif
8622 goto update;
8623 }
8624 else
8625 goto cancel;
8626 }
8627
8628 cancel:
8629 /* Text changed drastically or point moved off of line. */
8630 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8631 }
8632
8633 CHARPOS (this_line_start_pos) = 0;
8634 consider_all_windows_p |= buffer_shared > 1;
8635 ++clear_face_cache_count;
8636
8637
8638 /* Build desired matrices, and update the display. If
8639 consider_all_windows_p is non-zero, do it for all windows on all
8640 frames. Otherwise do it for selected_window, only. */
8641
8642 if (consider_all_windows_p)
8643 {
8644 Lisp_Object tail, frame;
8645 int i, n = 0, size = 50;
8646 struct frame **updated
8647 = (struct frame **) alloca (size * sizeof *updated);
8648
8649 /* Clear the face cache eventually. */
8650 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8651 {
8652 clear_face_cache (0);
8653 clear_face_cache_count = 0;
8654 }
8655
8656 /* Recompute # windows showing selected buffer. This will be
8657 incremented each time such a window is displayed. */
8658 buffer_shared = 0;
8659
8660 FOR_EACH_FRAME (tail, frame)
8661 {
8662 struct frame *f = XFRAME (frame);
8663
8664 if (FRAME_WINDOW_P (f) || f == sf)
8665 {
8666 /* Mark all the scroll bars to be removed; we'll redeem
8667 the ones we want when we redisplay their windows. */
8668 if (condemn_scroll_bars_hook)
8669 condemn_scroll_bars_hook (f);
8670
8671 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8672 redisplay_windows (FRAME_ROOT_WINDOW (f));
8673
8674 /* Any scroll bars which redisplay_windows should have
8675 nuked should now go away. */
8676 if (judge_scroll_bars_hook)
8677 judge_scroll_bars_hook (f);
8678
8679 /* If fonts changed, display again. */
8680 if (fonts_changed_p)
8681 goto retry;
8682
8683 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8684 {
8685 /* See if we have to hscroll. */
8686 if (hscroll_windows (f->root_window))
8687 goto retry;
8688
8689 /* Prevent various kinds of signals during display
8690 update. stdio is not robust about handling
8691 signals, which can cause an apparent I/O
8692 error. */
8693 if (interrupt_input)
8694 unrequest_sigio ();
8695 stop_polling ();
8696
8697 /* Update the display. */
8698 set_window_update_flags (XWINDOW (f->root_window), 1);
8699 pause |= update_frame (f, 0, 0);
8700 if (pause)
8701 break;
8702
8703 if (n == size)
8704 {
8705 int nbytes = size * sizeof *updated;
8706 struct frame **p = (struct frame **) alloca (2 * nbytes);
8707 bcopy (updated, p, nbytes);
8708 size *= 2;
8709 }
8710
8711 updated[n++] = f;
8712 }
8713 }
8714 }
8715
8716 /* Do the mark_window_display_accurate after all windows have
8717 been redisplayed because this call resets flags in buffers
8718 which are needed for proper redisplay. */
8719 for (i = 0; i < n; ++i)
8720 {
8721 struct frame *f = updated[i];
8722 mark_window_display_accurate (f->root_window, 1);
8723 if (frame_up_to_date_hook)
8724 frame_up_to_date_hook (f);
8725 }
8726 }
8727 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8728 {
8729 Lisp_Object mini_window;
8730 struct frame *mini_frame;
8731
8732 redisplay_window (selected_window, 1);
8733
8734 /* Compare desired and current matrices, perform output. */
8735 update:
8736
8737 /* If fonts changed, display again. */
8738 if (fonts_changed_p)
8739 goto retry;
8740
8741 /* Prevent various kinds of signals during display update.
8742 stdio is not robust about handling signals,
8743 which can cause an apparent I/O error. */
8744 if (interrupt_input)
8745 unrequest_sigio ();
8746 stop_polling ();
8747
8748 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8749 {
8750 if (hscroll_windows (selected_window))
8751 goto retry;
8752
8753 XWINDOW (selected_window)->must_be_updated_p = 1;
8754 pause = update_frame (sf, 0, 0);
8755 }
8756
8757 /* We may have called echo_area_display at the top of this
8758 function. If the echo area is on another frame, that may
8759 have put text on a frame other than the selected one, so the
8760 above call to update_frame would not have caught it. Catch
8761 it here. */
8762 mini_window = FRAME_MINIBUF_WINDOW (sf);
8763 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8764
8765 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8766 {
8767 XWINDOW (mini_window)->must_be_updated_p = 1;
8768 pause |= update_frame (mini_frame, 0, 0);
8769 if (!pause && hscroll_windows (mini_window))
8770 goto retry;
8771 }
8772 }
8773
8774 /* If display was paused because of pending input, make sure we do a
8775 thorough update the next time. */
8776 if (pause)
8777 {
8778 /* Prevent the optimization at the beginning of
8779 redisplay_internal that tries a single-line update of the
8780 line containing the cursor in the selected window. */
8781 CHARPOS (this_line_start_pos) = 0;
8782
8783 /* Let the overlay arrow be updated the next time. */
8784 if (!NILP (last_arrow_position))
8785 {
8786 last_arrow_position = Qt;
8787 last_arrow_string = Qt;
8788 }
8789
8790 /* If we pause after scrolling, some rows in the current
8791 matrices of some windows are not valid. */
8792 if (!WINDOW_FULL_WIDTH_P (w)
8793 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8794 update_mode_lines = 1;
8795 }
8796 else
8797 {
8798 if (!consider_all_windows_p)
8799 {
8800 /* This has already been done above if
8801 consider_all_windows_p is set. */
8802 mark_window_display_accurate_1 (w, 1);
8803
8804 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8805 last_arrow_string = Voverlay_arrow_string;
8806
8807 if (frame_up_to_date_hook != 0)
8808 frame_up_to_date_hook (sf);
8809 }
8810
8811 update_mode_lines = 0;
8812 windows_or_buffers_changed = 0;
8813 }
8814
8815 /* Start SIGIO interrupts coming again. Having them off during the
8816 code above makes it less likely one will discard output, but not
8817 impossible, since there might be stuff in the system buffer here.
8818 But it is much hairier to try to do anything about that. */
8819 if (interrupt_input)
8820 request_sigio ();
8821 start_polling ();
8822
8823 /* If a frame has become visible which was not before, redisplay
8824 again, so that we display it. Expose events for such a frame
8825 (which it gets when becoming visible) don't call the parts of
8826 redisplay constructing glyphs, so simply exposing a frame won't
8827 display anything in this case. So, we have to display these
8828 frames here explicitly. */
8829 if (!pause)
8830 {
8831 Lisp_Object tail, frame;
8832 int new_count = 0;
8833
8834 FOR_EACH_FRAME (tail, frame)
8835 {
8836 int this_is_visible = 0;
8837
8838 if (XFRAME (frame)->visible)
8839 this_is_visible = 1;
8840 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8841 if (XFRAME (frame)->visible)
8842 this_is_visible = 1;
8843
8844 if (this_is_visible)
8845 new_count++;
8846 }
8847
8848 if (new_count != number_of_visible_frames)
8849 windows_or_buffers_changed++;
8850 }
8851
8852 /* Change frame size now if a change is pending. */
8853 do_pending_window_change (1);
8854
8855 /* If we just did a pending size change, or have additional
8856 visible frames, redisplay again. */
8857 if (windows_or_buffers_changed && !pause)
8858 goto retry;
8859
8860 end_of_redisplay:;
8861
8862 unbind_to (count, Qnil);
8863 }
8864
8865
8866 /* Redisplay, but leave alone any recent echo area message unless
8867 another message has been requested in its place.
8868
8869 This is useful in situations where you need to redisplay but no
8870 user action has occurred, making it inappropriate for the message
8871 area to be cleared. See tracking_off and
8872 wait_reading_process_input for examples of these situations.
8873
8874 FROM_WHERE is an integer saying from where this function was
8875 called. This is useful for debugging. */
8876
8877 void
8878 redisplay_preserve_echo_area (from_where)
8879 int from_where;
8880 {
8881 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
8882
8883 if (!NILP (echo_area_buffer[1]))
8884 {
8885 /* We have a previously displayed message, but no current
8886 message. Redisplay the previous message. */
8887 display_last_displayed_message_p = 1;
8888 redisplay_internal (1);
8889 display_last_displayed_message_p = 0;
8890 }
8891 else
8892 redisplay_internal (1);
8893 }
8894
8895
8896 /* Function registered with record_unwind_protect in
8897 redisplay_internal. Clears the flag indicating that a redisplay is
8898 in progress. */
8899
8900 static Lisp_Object
8901 unwind_redisplay (old_redisplaying_p)
8902 Lisp_Object old_redisplaying_p;
8903 {
8904 redisplaying_p = XFASTINT (old_redisplaying_p);
8905 return Qnil;
8906 }
8907
8908
8909 /* Mark the display of window W as accurate or inaccurate. If
8910 ACCURATE_P is non-zero mark display of W as accurate. If
8911 ACCURATE_P is zero, arrange for W to be redisplayed the next time
8912 redisplay_internal is called. */
8913
8914 static void
8915 mark_window_display_accurate_1 (w, accurate_p)
8916 struct window *w;
8917 int accurate_p;
8918 {
8919 if (BUFFERP (w->buffer))
8920 {
8921 struct buffer *b = XBUFFER (w->buffer);
8922
8923 w->last_modified
8924 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
8925 w->last_overlay_modified
8926 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8927 w->last_had_star
8928 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
8929
8930 if (accurate_p)
8931 {
8932 b->clip_changed = 0;
8933 b->prevent_redisplay_optimizations_p = 0;
8934
8935 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8936 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8937 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8938 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
8939
8940 w->current_matrix->buffer = b;
8941 w->current_matrix->begv = BUF_BEGV (b);
8942 w->current_matrix->zv = BUF_ZV (b);
8943
8944 w->last_cursor = w->cursor;
8945 w->last_cursor_off_p = w->cursor_off_p;
8946
8947 if (w == XWINDOW (selected_window))
8948 w->last_point = make_number (BUF_PT (b));
8949 else
8950 w->last_point = make_number (XMARKER (w->pointm)->charpos);
8951 }
8952 }
8953
8954 if (accurate_p)
8955 {
8956 w->window_end_valid = w->buffer;
8957 w->update_mode_line = Qnil;
8958 }
8959 }
8960
8961
8962 /* Mark the display of windows in the window tree rooted at WINDOW as
8963 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
8964 windows as accurate. If ACCURATE_P is zero, arrange for windows to
8965 be redisplayed the next time redisplay_internal is called. */
8966
8967 void
8968 mark_window_display_accurate (window, accurate_p)
8969 Lisp_Object window;
8970 int accurate_p;
8971 {
8972 struct window *w;
8973
8974 for (; !NILP (window); window = w->next)
8975 {
8976 w = XWINDOW (window);
8977 mark_window_display_accurate_1 (w, accurate_p);
8978
8979 if (!NILP (w->vchild))
8980 mark_window_display_accurate (w->vchild, accurate_p);
8981 if (!NILP (w->hchild))
8982 mark_window_display_accurate (w->hchild, accurate_p);
8983 }
8984
8985 if (accurate_p)
8986 {
8987 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8988 last_arrow_string = Voverlay_arrow_string;
8989 }
8990 else
8991 {
8992 /* Force a thorough redisplay the next time by setting
8993 last_arrow_position and last_arrow_string to t, which is
8994 unequal to any useful value of Voverlay_arrow_... */
8995 last_arrow_position = Qt;
8996 last_arrow_string = Qt;
8997 }
8998 }
8999
9000
9001 /* Return value in display table DP (Lisp_Char_Table *) for character
9002 C. Since a display table doesn't have any parent, we don't have to
9003 follow parent. Do not call this function directly but use the
9004 macro DISP_CHAR_VECTOR. */
9005
9006 Lisp_Object
9007 disp_char_vector (dp, c)
9008 struct Lisp_Char_Table *dp;
9009 int c;
9010 {
9011 int code[4], i;
9012 Lisp_Object val;
9013
9014 if (SINGLE_BYTE_CHAR_P (c))
9015 return (dp->contents[c]);
9016
9017 SPLIT_CHAR (c, code[0], code[1], code[2]);
9018 if (code[1] < 32)
9019 code[1] = -1;
9020 else if (code[2] < 32)
9021 code[2] = -1;
9022
9023 /* Here, the possible range of code[0] (== charset ID) is
9024 128..max_charset. Since the top level char table contains data
9025 for multibyte characters after 256th element, we must increment
9026 code[0] by 128 to get a correct index. */
9027 code[0] += 128;
9028 code[3] = -1; /* anchor */
9029
9030 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9031 {
9032 val = dp->contents[code[i]];
9033 if (!SUB_CHAR_TABLE_P (val))
9034 return (NILP (val) ? dp->defalt : val);
9035 }
9036
9037 /* Here, val is a sub char table. We return the default value of
9038 it. */
9039 return (dp->defalt);
9040 }
9041
9042
9043 \f
9044 /***********************************************************************
9045 Window Redisplay
9046 ***********************************************************************/
9047
9048 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9049
9050 static void
9051 redisplay_windows (window)
9052 Lisp_Object window;
9053 {
9054 while (!NILP (window))
9055 {
9056 struct window *w = XWINDOW (window);
9057
9058 if (!NILP (w->hchild))
9059 redisplay_windows (w->hchild);
9060 else if (!NILP (w->vchild))
9061 redisplay_windows (w->vchild);
9062 else
9063 redisplay_window (window, 0);
9064
9065 window = w->next;
9066 }
9067 }
9068
9069
9070 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9071 DELTA is the number of bytes by which positions recorded in ROW
9072 differ from current buffer positions. */
9073
9074 void
9075 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9076 struct window *w;
9077 struct glyph_row *row;
9078 struct glyph_matrix *matrix;
9079 int delta, delta_bytes, dy, dvpos;
9080 {
9081 struct glyph *glyph = row->glyphs[TEXT_AREA];
9082 struct glyph *end = glyph + row->used[TEXT_AREA];
9083 int x = row->x;
9084 int pt_old = PT - delta;
9085
9086 /* Skip over glyphs not having an object at the start of the row.
9087 These are special glyphs like truncation marks on terminal
9088 frames. */
9089 if (row->displays_text_p)
9090 while (glyph < end
9091 && INTEGERP (glyph->object)
9092 && glyph->charpos < 0)
9093 {
9094 x += glyph->pixel_width;
9095 ++glyph;
9096 }
9097
9098 while (glyph < end
9099 && !INTEGERP (glyph->object)
9100 && (!BUFFERP (glyph->object)
9101 || glyph->charpos < pt_old))
9102 {
9103 x += glyph->pixel_width;
9104 ++glyph;
9105 }
9106
9107 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9108 w->cursor.x = x;
9109 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9110 w->cursor.y = row->y + dy;
9111
9112 if (w == XWINDOW (selected_window))
9113 {
9114 if (!row->continued_p
9115 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9116 && row->x == 0)
9117 {
9118 this_line_buffer = XBUFFER (w->buffer);
9119
9120 CHARPOS (this_line_start_pos)
9121 = MATRIX_ROW_START_CHARPOS (row) + delta;
9122 BYTEPOS (this_line_start_pos)
9123 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9124
9125 CHARPOS (this_line_end_pos)
9126 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9127 BYTEPOS (this_line_end_pos)
9128 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9129
9130 this_line_y = w->cursor.y;
9131 this_line_pixel_height = row->height;
9132 this_line_vpos = w->cursor.vpos;
9133 this_line_start_x = row->x;
9134 }
9135 else
9136 CHARPOS (this_line_start_pos) = 0;
9137 }
9138 }
9139
9140
9141 /* Run window scroll functions, if any, for WINDOW with new window
9142 start STARTP. Sets the window start of WINDOW to that position.
9143
9144 We assume that the window's buffer is really current. */
9145
9146 static INLINE struct text_pos
9147 run_window_scroll_functions (window, startp)
9148 Lisp_Object window;
9149 struct text_pos startp;
9150 {
9151 struct window *w = XWINDOW (window);
9152 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9153
9154 if (current_buffer != XBUFFER (w->buffer))
9155 abort ();
9156
9157 if (!NILP (Vwindow_scroll_functions))
9158 {
9159 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9160 make_number (CHARPOS (startp)));
9161 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9162 /* In case the hook functions switch buffers. */
9163 if (current_buffer != XBUFFER (w->buffer))
9164 set_buffer_internal_1 (XBUFFER (w->buffer));
9165 }
9166
9167 return startp;
9168 }
9169
9170
9171 /* Modify the desired matrix of window W and W->vscroll so that the
9172 line containing the cursor is fully visible. */
9173
9174 static void
9175 make_cursor_line_fully_visible (w)
9176 struct window *w;
9177 {
9178 struct glyph_matrix *matrix;
9179 struct glyph_row *row;
9180 int window_height;
9181
9182 /* It's not always possible to find the cursor, e.g, when a window
9183 is full of overlay strings. Don't do anything in that case. */
9184 if (w->cursor.vpos < 0)
9185 return;
9186
9187 matrix = w->desired_matrix;
9188 row = MATRIX_ROW (matrix, w->cursor.vpos);
9189
9190 /* If the cursor row is not partially visible, there's nothing
9191 to do. */
9192 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9193 return;
9194
9195 /* If the row the cursor is in is taller than the window's height,
9196 it's not clear what to do, so do nothing. */
9197 window_height = window_box_height (w);
9198 if (row->height >= window_height)
9199 return;
9200
9201 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9202 {
9203 int dy = row->height - row->visible_height;
9204 w->vscroll = 0;
9205 w->cursor.y += dy;
9206 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9207 }
9208 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9209 {
9210 int dy = - (row->height - row->visible_height);
9211 w->vscroll = dy;
9212 w->cursor.y += dy;
9213 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9214 }
9215
9216 /* When we change the cursor y-position of the selected window,
9217 change this_line_y as well so that the display optimization for
9218 the cursor line of the selected window in redisplay_internal uses
9219 the correct y-position. */
9220 if (w == XWINDOW (selected_window))
9221 this_line_y = w->cursor.y;
9222 }
9223
9224
9225 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9226 non-zero means only WINDOW is redisplayed in redisplay_internal.
9227 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9228 in redisplay_window to bring a partially visible line into view in
9229 the case that only the cursor has moved.
9230
9231 Value is
9232
9233 1 if scrolling succeeded
9234
9235 0 if scrolling didn't find point.
9236
9237 -1 if new fonts have been loaded so that we must interrupt
9238 redisplay, adjust glyph matrices, and try again. */
9239
9240 static int
9241 try_scrolling (window, just_this_one_p, scroll_conservatively,
9242 scroll_step, temp_scroll_step)
9243 Lisp_Object window;
9244 int just_this_one_p;
9245 int scroll_conservatively, scroll_step;
9246 int temp_scroll_step;
9247 {
9248 struct window *w = XWINDOW (window);
9249 struct frame *f = XFRAME (w->frame);
9250 struct text_pos scroll_margin_pos;
9251 struct text_pos pos;
9252 struct text_pos startp;
9253 struct it it;
9254 Lisp_Object window_end;
9255 int this_scroll_margin;
9256 int dy = 0;
9257 int scroll_max;
9258 int rc;
9259 int amount_to_scroll = 0;
9260 Lisp_Object aggressive;
9261 int height;
9262
9263 #if GLYPH_DEBUG
9264 debug_method_add (w, "try_scrolling");
9265 #endif
9266
9267 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9268
9269 /* Compute scroll margin height in pixels. We scroll when point is
9270 within this distance from the top or bottom of the window. */
9271 if (scroll_margin > 0)
9272 {
9273 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9274 this_scroll_margin *= CANON_Y_UNIT (f);
9275 }
9276 else
9277 this_scroll_margin = 0;
9278
9279 /* Compute how much we should try to scroll maximally to bring point
9280 into view. */
9281 if (scroll_step || scroll_conservatively || temp_scroll_step)
9282 scroll_max = max (scroll_step,
9283 max (scroll_conservatively, temp_scroll_step));
9284 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9285 || NUMBERP (current_buffer->scroll_up_aggressively))
9286 /* We're trying to scroll because of aggressive scrolling
9287 but no scroll_step is set. Choose an arbitrary one. Maybe
9288 there should be a variable for this. */
9289 scroll_max = 10;
9290 else
9291 scroll_max = 0;
9292 scroll_max *= CANON_Y_UNIT (f);
9293
9294 /* Decide whether we have to scroll down. Start at the window end
9295 and move this_scroll_margin up to find the position of the scroll
9296 margin. */
9297 window_end = Fwindow_end (window, Qt);
9298 CHARPOS (scroll_margin_pos) = XINT (window_end);
9299 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9300 if (this_scroll_margin)
9301 {
9302 start_display (&it, w, scroll_margin_pos);
9303 move_it_vertically (&it, - this_scroll_margin);
9304 scroll_margin_pos = it.current.pos;
9305 }
9306
9307 if (PT >= CHARPOS (scroll_margin_pos))
9308 {
9309 int y0;
9310
9311 /* Point is in the scroll margin at the bottom of the window, or
9312 below. Compute a new window start that makes point visible. */
9313
9314 /* Compute the distance from the scroll margin to PT.
9315 Give up if the distance is greater than scroll_max. */
9316 start_display (&it, w, scroll_margin_pos);
9317 y0 = it.current_y;
9318 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9319 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9320
9321 /* With a scroll_margin of 0, scroll_margin_pos is at the window
9322 end, which is one line below the window. The iterator's
9323 current_y will be same as y0 in that case, but we have to
9324 scroll a line to make PT visible. That's the reason why 1 is
9325 added below. */
9326 dy = 1 + it.current_y - y0;
9327
9328 if (dy > scroll_max)
9329 return 0;
9330
9331 /* Move the window start down. If scrolling conservatively,
9332 move it just enough down to make point visible. If
9333 scroll_step is set, move it down by scroll_step. */
9334 start_display (&it, w, startp);
9335
9336 if (scroll_conservatively)
9337 amount_to_scroll
9338 = max (max (dy, CANON_Y_UNIT (f)),
9339 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9340 else if (scroll_step || temp_scroll_step)
9341 amount_to_scroll = scroll_max;
9342 else
9343 {
9344 aggressive = current_buffer->scroll_down_aggressively;
9345 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9346 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9347 if (NUMBERP (aggressive))
9348 amount_to_scroll = XFLOATINT (aggressive) * height;
9349 }
9350
9351 if (amount_to_scroll <= 0)
9352 return 0;
9353
9354 move_it_vertically (&it, amount_to_scroll);
9355 startp = it.current.pos;
9356 }
9357 else
9358 {
9359 /* See if point is inside the scroll margin at the top of the
9360 window. */
9361 scroll_margin_pos = startp;
9362 if (this_scroll_margin)
9363 {
9364 start_display (&it, w, startp);
9365 move_it_vertically (&it, this_scroll_margin);
9366 scroll_margin_pos = it.current.pos;
9367 }
9368
9369 if (PT < CHARPOS (scroll_margin_pos))
9370 {
9371 /* Point is in the scroll margin at the top of the window or
9372 above what is displayed in the window. */
9373 int y0;
9374
9375 /* Compute the vertical distance from PT to the scroll
9376 margin position. Give up if distance is greater than
9377 scroll_max. */
9378 SET_TEXT_POS (pos, PT, PT_BYTE);
9379 start_display (&it, w, pos);
9380 y0 = it.current_y;
9381 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9382 it.last_visible_y, -1,
9383 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9384 dy = it.current_y - y0;
9385 if (dy > scroll_max)
9386 return 0;
9387
9388 /* Compute new window start. */
9389 start_display (&it, w, startp);
9390
9391 if (scroll_conservatively)
9392 amount_to_scroll =
9393 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9394 else if (scroll_step || temp_scroll_step)
9395 amount_to_scroll = scroll_max;
9396 else
9397 {
9398 aggressive = current_buffer->scroll_up_aggressively;
9399 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9400 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9401 if (NUMBERP (aggressive))
9402 amount_to_scroll = XFLOATINT (aggressive) * height;
9403 }
9404
9405 if (amount_to_scroll <= 0)
9406 return 0;
9407
9408 move_it_vertically (&it, - amount_to_scroll);
9409 startp = it.current.pos;
9410 }
9411 }
9412
9413 /* Run window scroll functions. */
9414 startp = run_window_scroll_functions (window, startp);
9415
9416 /* Display the window. Give up if new fonts are loaded, or if point
9417 doesn't appear. */
9418 if (!try_window (window, startp))
9419 rc = -1;
9420 else if (w->cursor.vpos < 0)
9421 {
9422 clear_glyph_matrix (w->desired_matrix);
9423 rc = 0;
9424 }
9425 else
9426 {
9427 /* Maybe forget recorded base line for line number display. */
9428 if (!just_this_one_p
9429 || current_buffer->clip_changed
9430 || BEG_UNCHANGED < CHARPOS (startp))
9431 w->base_line_number = Qnil;
9432
9433 /* If cursor ends up on a partially visible line, shift display
9434 lines up or down. */
9435 make_cursor_line_fully_visible (w);
9436 rc = 1;
9437 }
9438
9439 return rc;
9440 }
9441
9442
9443 /* Compute a suitable window start for window W if display of W starts
9444 on a continuation line. Value is non-zero if a new window start
9445 was computed.
9446
9447 The new window start will be computed, based on W's width, starting
9448 from the start of the continued line. It is the start of the
9449 screen line with the minimum distance from the old start W->start. */
9450
9451 static int
9452 compute_window_start_on_continuation_line (w)
9453 struct window *w;
9454 {
9455 struct text_pos pos, start_pos;
9456 int window_start_changed_p = 0;
9457
9458 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9459
9460 /* If window start is on a continuation line... Window start may be
9461 < BEGV in case there's invisible text at the start of the
9462 buffer (M-x rmail, for example). */
9463 if (CHARPOS (start_pos) > BEGV
9464 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9465 {
9466 struct it it;
9467 struct glyph_row *row;
9468
9469 /* Handle the case that the window start is out of range. */
9470 if (CHARPOS (start_pos) < BEGV)
9471 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9472 else if (CHARPOS (start_pos) > ZV)
9473 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9474
9475 /* Find the start of the continued line. This should be fast
9476 because scan_buffer is fast (newline cache). */
9477 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9478 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9479 row, DEFAULT_FACE_ID);
9480 reseat_at_previous_visible_line_start (&it);
9481
9482 /* If the line start is "too far" away from the window start,
9483 say it takes too much time to compute a new window start. */
9484 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9485 < XFASTINT (w->height) * XFASTINT (w->width))
9486 {
9487 int min_distance, distance;
9488
9489 /* Move forward by display lines to find the new window
9490 start. If window width was enlarged, the new start can
9491 be expected to be > the old start. If window width was
9492 decreased, the new window start will be < the old start.
9493 So, we're looking for the display line start with the
9494 minimum distance from the old window start. */
9495 pos = it.current.pos;
9496 min_distance = INFINITY;
9497 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9498 distance < min_distance)
9499 {
9500 min_distance = distance;
9501 pos = it.current.pos;
9502 move_it_by_lines (&it, 1, 0);
9503 }
9504
9505 /* Set the window start there. */
9506 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9507 window_start_changed_p = 1;
9508 }
9509 }
9510
9511 return window_start_changed_p;
9512 }
9513
9514
9515 /* Try cursor movement in case text has not changes in window WINDOW,
9516 with window start STARTP. Value is
9517
9518 1 if successful
9519
9520 0 if this method cannot be used
9521
9522 -1 if we know we have to scroll the display. *SCROLL_STEP is
9523 set to 1, under certain circumstances, if we want to scroll as
9524 if scroll-step were set to 1. See the code. */
9525
9526 static int
9527 try_cursor_movement (window, startp, scroll_step)
9528 Lisp_Object window;
9529 struct text_pos startp;
9530 int *scroll_step;
9531 {
9532 struct window *w = XWINDOW (window);
9533 struct frame *f = XFRAME (w->frame);
9534 int rc = 0;
9535
9536 /* Handle case where text has not changed, only point, and it has
9537 not moved off the frame. */
9538 if (/* Point may be in this window. */
9539 PT >= CHARPOS (startp)
9540 /* Selective display hasn't changed. */
9541 && !current_buffer->clip_changed
9542 /* Function force-mode-line-update is used to force a thorough
9543 redisplay. It sets either windows_or_buffers_changed or
9544 update_mode_lines. So don't take a shortcut here for these
9545 cases. */
9546 && !update_mode_lines
9547 && !windows_or_buffers_changed
9548 /* Can't use this case if highlighting a region. When a
9549 region exists, cursor movement has to do more than just
9550 set the cursor. */
9551 && !(!NILP (Vtransient_mark_mode)
9552 && !NILP (current_buffer->mark_active))
9553 && NILP (w->region_showing)
9554 && NILP (Vshow_trailing_whitespace)
9555 /* Right after splitting windows, last_point may be nil. */
9556 && INTEGERP (w->last_point)
9557 /* This code is not used for mini-buffer for the sake of the case
9558 of redisplaying to replace an echo area message; since in
9559 that case the mini-buffer contents per se are usually
9560 unchanged. This code is of no real use in the mini-buffer
9561 since the handling of this_line_start_pos, etc., in redisplay
9562 handles the same cases. */
9563 && !EQ (window, minibuf_window)
9564 /* When splitting windows or for new windows, it happens that
9565 redisplay is called with a nil window_end_vpos or one being
9566 larger than the window. This should really be fixed in
9567 window.c. I don't have this on my list, now, so we do
9568 approximately the same as the old redisplay code. --gerd. */
9569 && INTEGERP (w->window_end_vpos)
9570 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9571 && (FRAME_WINDOW_P (f)
9572 || !MARKERP (Voverlay_arrow_position)
9573 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9574 {
9575 int this_scroll_margin;
9576 struct glyph_row *row;
9577
9578 #if GLYPH_DEBUG
9579 debug_method_add (w, "cursor movement");
9580 #endif
9581
9582 /* Scroll if point within this distance from the top or bottom
9583 of the window. This is a pixel value. */
9584 this_scroll_margin = max (0, scroll_margin);
9585 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9586 this_scroll_margin *= CANON_Y_UNIT (f);
9587
9588 /* Start with the row the cursor was displayed during the last
9589 not paused redisplay. Give up if that row is not valid. */
9590 if (w->last_cursor.vpos < 0
9591 || w->last_cursor.vpos >= w->current_matrix->nrows)
9592 rc = -1;
9593 else
9594 {
9595 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9596 if (row->mode_line_p)
9597 ++row;
9598 if (!row->enabled_p)
9599 rc = -1;
9600 }
9601
9602 if (rc == 0)
9603 {
9604 int scroll_p = 0;
9605 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9606
9607 if (PT > XFASTINT (w->last_point))
9608 {
9609 /* Point has moved forward. */
9610 while (MATRIX_ROW_END_CHARPOS (row) < PT
9611 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9612 {
9613 xassert (row->enabled_p);
9614 ++row;
9615 }
9616
9617 /* The end position of a row equals the start position
9618 of the next row. If PT is there, we would rather
9619 display it in the next line. */
9620 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9621 && MATRIX_ROW_END_CHARPOS (row) == PT
9622 && !cursor_row_p (w, row))
9623 ++row;
9624
9625 /* If within the scroll margin, scroll. Note that
9626 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9627 the next line would be drawn, and that
9628 this_scroll_margin can be zero. */
9629 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9630 || PT > MATRIX_ROW_END_CHARPOS (row)
9631 /* Line is completely visible last line in window
9632 and PT is to be set in the next line. */
9633 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9634 && PT == MATRIX_ROW_END_CHARPOS (row)
9635 && !row->ends_at_zv_p
9636 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9637 scroll_p = 1;
9638 }
9639 else if (PT < XFASTINT (w->last_point))
9640 {
9641 /* Cursor has to be moved backward. Note that PT >=
9642 CHARPOS (startp) because of the outer
9643 if-statement. */
9644 while (!row->mode_line_p
9645 && (MATRIX_ROW_START_CHARPOS (row) > PT
9646 || (MATRIX_ROW_START_CHARPOS (row) == PT
9647 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9648 && (row->y > this_scroll_margin
9649 || CHARPOS (startp) == BEGV))
9650 {
9651 xassert (row->enabled_p);
9652 --row;
9653 }
9654
9655 /* Consider the following case: Window starts at BEGV,
9656 there is invisible, intangible text at BEGV, so that
9657 display starts at some point START > BEGV. It can
9658 happen that we are called with PT somewhere between
9659 BEGV and START. Try to handle that case. */
9660 if (row < w->current_matrix->rows
9661 || row->mode_line_p)
9662 {
9663 row = w->current_matrix->rows;
9664 if (row->mode_line_p)
9665 ++row;
9666 }
9667
9668 /* Due to newlines in overlay strings, we may have to
9669 skip forward over overlay strings. */
9670 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9671 && MATRIX_ROW_END_CHARPOS (row) == PT
9672 && !cursor_row_p (w, row))
9673 ++row;
9674
9675 /* If within the scroll margin, scroll. */
9676 if (row->y < this_scroll_margin
9677 && CHARPOS (startp) != BEGV)
9678 scroll_p = 1;
9679 }
9680
9681 if (PT < MATRIX_ROW_START_CHARPOS (row)
9682 || PT > MATRIX_ROW_END_CHARPOS (row))
9683 {
9684 /* if PT is not in the glyph row, give up. */
9685 rc = -1;
9686 }
9687 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9688 {
9689 if (PT == MATRIX_ROW_END_CHARPOS (row)
9690 && !row->ends_at_zv_p
9691 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
9692 rc = -1;
9693 else if (row->height > window_box_height (w))
9694 {
9695 /* If we end up in a partially visible line, let's
9696 make it fully visible, except when it's taller
9697 than the window, in which case we can't do much
9698 about it. */
9699 *scroll_step = 1;
9700 rc = -1;
9701 }
9702 else
9703 {
9704 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9705 try_window (window, startp);
9706 make_cursor_line_fully_visible (w);
9707 rc = 1;
9708 }
9709 }
9710 else if (scroll_p)
9711 rc = -1;
9712 else
9713 {
9714 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9715 rc = 1;
9716 }
9717 }
9718 }
9719
9720 return rc;
9721 }
9722
9723
9724 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9725 selected_window is redisplayed. */
9726
9727 static void
9728 redisplay_window (window, just_this_one_p)
9729 Lisp_Object window;
9730 int just_this_one_p;
9731 {
9732 struct window *w = XWINDOW (window);
9733 struct frame *f = XFRAME (w->frame);
9734 struct buffer *buffer = XBUFFER (w->buffer);
9735 struct buffer *old = current_buffer;
9736 struct text_pos lpoint, opoint, startp;
9737 int update_mode_line;
9738 int tem;
9739 struct it it;
9740 /* Record it now because it's overwritten. */
9741 int current_matrix_up_to_date_p = 0;
9742 int temp_scroll_step = 0;
9743 int count = BINDING_STACK_SIZE ();
9744 int rc;
9745
9746 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9747 opoint = lpoint;
9748
9749 /* W must be a leaf window here. */
9750 xassert (!NILP (w->buffer));
9751 #if GLYPH_DEBUG
9752 *w->desired_matrix->method = 0;
9753 #endif
9754
9755 specbind (Qinhibit_point_motion_hooks, Qt);
9756
9757 reconsider_clip_changes (w, buffer);
9758
9759 /* Has the mode line to be updated? */
9760 update_mode_line = (!NILP (w->update_mode_line)
9761 || update_mode_lines
9762 || buffer->clip_changed);
9763
9764 if (MINI_WINDOW_P (w))
9765 {
9766 if (w == XWINDOW (echo_area_window)
9767 && !NILP (echo_area_buffer[0]))
9768 {
9769 if (update_mode_line)
9770 /* We may have to update a tty frame's menu bar or a
9771 tool-bar. Example `M-x C-h C-h C-g'. */
9772 goto finish_menu_bars;
9773 else
9774 /* We've already displayed the echo area glyphs in this window. */
9775 goto finish_scroll_bars;
9776 }
9777 else if (w != XWINDOW (minibuf_window))
9778 {
9779 /* W is a mini-buffer window, but it's not the currently
9780 active one, so clear it. */
9781 int yb = window_text_bottom_y (w);
9782 struct glyph_row *row;
9783 int y;
9784
9785 for (y = 0, row = w->desired_matrix->rows;
9786 y < yb;
9787 y += row->height, ++row)
9788 blank_row (w, row, y);
9789 goto finish_scroll_bars;
9790 }
9791
9792 clear_glyph_matrix (w->desired_matrix);
9793 }
9794
9795 /* Otherwise set up data on this window; select its buffer and point
9796 value. */
9797 /* Really select the buffer, for the sake of buffer-local
9798 variables. */
9799 set_buffer_internal_1 (XBUFFER (w->buffer));
9800 SET_TEXT_POS (opoint, PT, PT_BYTE);
9801
9802 current_matrix_up_to_date_p
9803 = (!NILP (w->window_end_valid)
9804 && !current_buffer->clip_changed
9805 && XFASTINT (w->last_modified) >= MODIFF
9806 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9807
9808 /* When windows_or_buffers_changed is non-zero, we can't rely on
9809 the window end being valid, so set it to nil there. */
9810 if (windows_or_buffers_changed)
9811 {
9812 /* If window starts on a continuation line, maybe adjust the
9813 window start in case the window's width changed. */
9814 if (XMARKER (w->start)->buffer == current_buffer)
9815 compute_window_start_on_continuation_line (w);
9816
9817 w->window_end_valid = Qnil;
9818 }
9819
9820 /* Some sanity checks. */
9821 CHECK_WINDOW_END (w);
9822 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9823 abort ();
9824 if (BYTEPOS (opoint) < CHARPOS (opoint))
9825 abort ();
9826
9827 /* If %c is in mode line, update it if needed. */
9828 if (!NILP (w->column_number_displayed)
9829 /* This alternative quickly identifies a common case
9830 where no change is needed. */
9831 && !(PT == XFASTINT (w->last_point)
9832 && XFASTINT (w->last_modified) >= MODIFF
9833 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9834 && XFASTINT (w->column_number_displayed) != current_column ())
9835 update_mode_line = 1;
9836
9837 /* Count number of windows showing the selected buffer. An indirect
9838 buffer counts as its base buffer. */
9839 if (!just_this_one_p)
9840 {
9841 struct buffer *current_base, *window_base;
9842 current_base = current_buffer;
9843 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9844 if (current_base->base_buffer)
9845 current_base = current_base->base_buffer;
9846 if (window_base->base_buffer)
9847 window_base = window_base->base_buffer;
9848 if (current_base == window_base)
9849 buffer_shared++;
9850 }
9851
9852 /* Point refers normally to the selected window. For any other
9853 window, set up appropriate value. */
9854 if (!EQ (window, selected_window))
9855 {
9856 int new_pt = XMARKER (w->pointm)->charpos;
9857 int new_pt_byte = marker_byte_position (w->pointm);
9858 if (new_pt < BEGV)
9859 {
9860 new_pt = BEGV;
9861 new_pt_byte = BEGV_BYTE;
9862 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9863 }
9864 else if (new_pt > (ZV - 1))
9865 {
9866 new_pt = ZV;
9867 new_pt_byte = ZV_BYTE;
9868 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9869 }
9870
9871 /* We don't use SET_PT so that the point-motion hooks don't run. */
9872 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9873 }
9874
9875 /* If any of the character widths specified in the display table
9876 have changed, invalidate the width run cache. It's true that
9877 this may be a bit late to catch such changes, but the rest of
9878 redisplay goes (non-fatally) haywire when the display table is
9879 changed, so why should we worry about doing any better? */
9880 if (current_buffer->width_run_cache)
9881 {
9882 struct Lisp_Char_Table *disptab = buffer_display_table ();
9883
9884 if (! disptab_matches_widthtab (disptab,
9885 XVECTOR (current_buffer->width_table)))
9886 {
9887 invalidate_region_cache (current_buffer,
9888 current_buffer->width_run_cache,
9889 BEG, Z);
9890 recompute_width_table (current_buffer, disptab);
9891 }
9892 }
9893
9894 /* If window-start is screwed up, choose a new one. */
9895 if (XMARKER (w->start)->buffer != current_buffer)
9896 goto recenter;
9897
9898 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9899
9900 /* If someone specified a new starting point but did not insist,
9901 check whether it can be used. */
9902 if (!NILP (w->optional_new_start)
9903 && CHARPOS (startp) >= BEGV
9904 && CHARPOS (startp) <= ZV)
9905 {
9906 w->optional_new_start = Qnil;
9907 start_display (&it, w, startp);
9908 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9909 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9910 if (IT_CHARPOS (it) == PT)
9911 w->force_start = Qt;
9912 }
9913
9914 /* Handle case where place to start displaying has been specified,
9915 unless the specified location is outside the accessible range. */
9916 if (!NILP (w->force_start)
9917 || w->frozen_window_start_p)
9918 {
9919 w->force_start = Qnil;
9920 w->vscroll = 0;
9921 w->window_end_valid = Qnil;
9922
9923 /* Forget any recorded base line for line number display. */
9924 if (!current_matrix_up_to_date_p
9925 || current_buffer->clip_changed)
9926 w->base_line_number = Qnil;
9927
9928 /* Redisplay the mode line. Select the buffer properly for that.
9929 Also, run the hook window-scroll-functions
9930 because we have scrolled. */
9931 /* Note, we do this after clearing force_start because
9932 if there's an error, it is better to forget about force_start
9933 than to get into an infinite loop calling the hook functions
9934 and having them get more errors. */
9935 if (!update_mode_line
9936 || ! NILP (Vwindow_scroll_functions))
9937 {
9938 update_mode_line = 1;
9939 w->update_mode_line = Qt;
9940 startp = run_window_scroll_functions (window, startp);
9941 }
9942
9943 w->last_modified = make_number (0);
9944 w->last_overlay_modified = make_number (0);
9945 if (CHARPOS (startp) < BEGV)
9946 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9947 else if (CHARPOS (startp) > ZV)
9948 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9949
9950 /* Redisplay, then check if cursor has been set during the
9951 redisplay. Give up if new fonts were loaded. */
9952 if (!try_window (window, startp))
9953 {
9954 w->force_start = Qt;
9955 clear_glyph_matrix (w->desired_matrix);
9956 goto finish_scroll_bars;
9957 }
9958
9959 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
9960 {
9961 /* If point does not appear, try to move point so it does
9962 appear. The desired matrix has been built above, so we
9963 can use it here. */
9964 int window_height;
9965 struct glyph_row *row;
9966
9967 window_height = window_box_height (w) / 2;
9968 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9969 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
9970 ++row;
9971
9972 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9973 MATRIX_ROW_START_BYTEPOS (row));
9974
9975 if (w != XWINDOW (selected_window))
9976 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
9977 else if (current_buffer == old)
9978 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9979
9980 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9981
9982 /* If we are highlighting the region, then we just changed
9983 the region, so redisplay to show it. */
9984 if (!NILP (Vtransient_mark_mode)
9985 && !NILP (current_buffer->mark_active))
9986 {
9987 clear_glyph_matrix (w->desired_matrix);
9988 if (!try_window (window, startp))
9989 goto finish_scroll_bars;
9990 }
9991 }
9992
9993 make_cursor_line_fully_visible (w);
9994 #if GLYPH_DEBUG
9995 debug_method_add (w, "forced window start");
9996 #endif
9997 goto done;
9998 }
9999
10000 /* Handle case where text has not changed, only point, and it has
10001 not moved off the frame. */
10002 if (current_matrix_up_to_date_p
10003 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10004 rc != 0))
10005 {
10006 if (rc == -1)
10007 goto try_to_scroll;
10008 else
10009 goto done;
10010 }
10011 /* If current starting point was originally the beginning of a line
10012 but no longer is, find a new starting point. */
10013 else if (!NILP (w->start_at_line_beg)
10014 && !(CHARPOS (startp) <= BEGV
10015 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10016 {
10017 #if GLYPH_DEBUG
10018 debug_method_add (w, "recenter 1");
10019 #endif
10020 goto recenter;
10021 }
10022
10023 /* Try scrolling with try_window_id. Value is > 0 if update has
10024 been done, it is -1 if we know that the same window start will
10025 not work. It is 0 if unsuccessful for some other reason. */
10026 else if ((tem = try_window_id (w)) != 0)
10027 {
10028 #if GLYPH_DEBUG
10029 debug_method_add (w, "try_window_id %d", tem);
10030 #endif
10031
10032 if (fonts_changed_p)
10033 goto finish_scroll_bars;
10034 if (tem > 0)
10035 goto done;
10036
10037 /* Otherwise try_window_id has returned -1 which means that we
10038 don't want the alternative below this comment to execute. */
10039 }
10040 else if (CHARPOS (startp) >= BEGV
10041 && CHARPOS (startp) <= ZV
10042 && PT >= CHARPOS (startp)
10043 && (CHARPOS (startp) < ZV
10044 /* Avoid starting at end of buffer. */
10045 || CHARPOS (startp) == BEGV
10046 || (XFASTINT (w->last_modified) >= MODIFF
10047 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10048 {
10049 #if GLYPH_DEBUG
10050 debug_method_add (w, "same window start");
10051 #endif
10052
10053 /* Try to redisplay starting at same place as before.
10054 If point has not moved off frame, accept the results. */
10055 if (!current_matrix_up_to_date_p
10056 /* Don't use try_window_reusing_current_matrix in this case
10057 because a window scroll function can have changed the
10058 buffer. */
10059 || !NILP (Vwindow_scroll_functions)
10060 || MINI_WINDOW_P (w)
10061 || !try_window_reusing_current_matrix (w))
10062 {
10063 IF_DEBUG (debug_method_add (w, "1"));
10064 try_window (window, startp);
10065 }
10066
10067 if (fonts_changed_p)
10068 goto finish_scroll_bars;
10069
10070 if (w->cursor.vpos >= 0)
10071 {
10072 if (!just_this_one_p
10073 || current_buffer->clip_changed
10074 || BEG_UNCHANGED < CHARPOS (startp))
10075 /* Forget any recorded base line for line number display. */
10076 w->base_line_number = Qnil;
10077
10078 make_cursor_line_fully_visible (w);
10079 goto done;
10080 }
10081 else
10082 clear_glyph_matrix (w->desired_matrix);
10083 }
10084
10085 try_to_scroll:
10086
10087 w->last_modified = make_number (0);
10088 w->last_overlay_modified = make_number (0);
10089
10090 /* Redisplay the mode line. Select the buffer properly for that. */
10091 if (!update_mode_line)
10092 {
10093 update_mode_line = 1;
10094 w->update_mode_line = Qt;
10095 }
10096
10097 /* Try to scroll by specified few lines. */
10098 if ((scroll_conservatively
10099 || scroll_step
10100 || temp_scroll_step
10101 || NUMBERP (current_buffer->scroll_up_aggressively)
10102 || NUMBERP (current_buffer->scroll_down_aggressively))
10103 && !current_buffer->clip_changed
10104 && CHARPOS (startp) >= BEGV
10105 && CHARPOS (startp) <= ZV)
10106 {
10107 /* The function returns -1 if new fonts were loaded, 1 if
10108 successful, 0 if not successful. */
10109 int rc = try_scrolling (window, just_this_one_p,
10110 scroll_conservatively,
10111 scroll_step,
10112 temp_scroll_step);
10113 if (rc > 0)
10114 goto done;
10115 else if (rc < 0)
10116 goto finish_scroll_bars;
10117 }
10118
10119 /* Finally, just choose place to start which centers point */
10120
10121 recenter:
10122
10123 #if GLYPH_DEBUG
10124 debug_method_add (w, "recenter");
10125 #endif
10126
10127 /* w->vscroll = 0; */
10128
10129 /* Forget any previously recorded base line for line number display. */
10130 if (!current_matrix_up_to_date_p
10131 || current_buffer->clip_changed)
10132 w->base_line_number = Qnil;
10133
10134 /* Move backward half the height of the window. */
10135 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10136 it.current_y = it.last_visible_y;
10137 move_it_vertically_backward (&it, window_box_height (w) / 2);
10138 xassert (IT_CHARPOS (it) >= BEGV);
10139
10140 /* The function move_it_vertically_backward may move over more
10141 than the specified y-distance. If it->w is small, e.g. a
10142 mini-buffer window, we may end up in front of the window's
10143 display area. Start displaying at the start of the line
10144 containing PT in this case. */
10145 if (it.current_y <= 0)
10146 {
10147 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10148 move_it_vertically (&it, 0);
10149 xassert (IT_CHARPOS (it) <= PT);
10150 it.current_y = 0;
10151 }
10152
10153 it.current_x = it.hpos = 0;
10154
10155 /* Set startp here explicitly in case that helps avoid an infinite loop
10156 in case the window-scroll-functions functions get errors. */
10157 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10158
10159 /* Run scroll hooks. */
10160 startp = run_window_scroll_functions (window, it.current.pos);
10161
10162 /* Redisplay the window. */
10163 if (!current_matrix_up_to_date_p
10164 || windows_or_buffers_changed
10165 /* Don't use try_window_reusing_current_matrix in this case
10166 because it can have changed the buffer. */
10167 || !NILP (Vwindow_scroll_functions)
10168 || !just_this_one_p
10169 || MINI_WINDOW_P (w)
10170 || !try_window_reusing_current_matrix (w))
10171 try_window (window, startp);
10172
10173 /* If new fonts have been loaded (due to fontsets), give up. We
10174 have to start a new redisplay since we need to re-adjust glyph
10175 matrices. */
10176 if (fonts_changed_p)
10177 goto finish_scroll_bars;
10178
10179 /* If cursor did not appear assume that the middle of the window is
10180 in the first line of the window. Do it again with the next line.
10181 (Imagine a window of height 100, displaying two lines of height
10182 60. Moving back 50 from it->last_visible_y will end in the first
10183 line.) */
10184 if (w->cursor.vpos < 0)
10185 {
10186 if (!NILP (w->window_end_valid)
10187 && PT >= Z - XFASTINT (w->window_end_pos))
10188 {
10189 clear_glyph_matrix (w->desired_matrix);
10190 move_it_by_lines (&it, 1, 0);
10191 try_window (window, it.current.pos);
10192 }
10193 else if (PT < IT_CHARPOS (it))
10194 {
10195 clear_glyph_matrix (w->desired_matrix);
10196 move_it_by_lines (&it, -1, 0);
10197 try_window (window, it.current.pos);
10198 }
10199 else
10200 {
10201 /* Not much we can do about it. */
10202 }
10203 }
10204
10205 /* Consider the following case: Window starts at BEGV, there is
10206 invisible, intangible text at BEGV, so that display starts at
10207 some point START > BEGV. It can happen that we are called with
10208 PT somewhere between BEGV and START. Try to handle that case. */
10209 if (w->cursor.vpos < 0)
10210 {
10211 struct glyph_row *row = w->current_matrix->rows;
10212 if (row->mode_line_p)
10213 ++row;
10214 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10215 }
10216
10217 make_cursor_line_fully_visible (w);
10218
10219 done:
10220
10221 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10222 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10223 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10224 ? Qt : Qnil);
10225
10226 /* Display the mode line, if we must. */
10227 if ((update_mode_line
10228 /* If window not full width, must redo its mode line
10229 if (a) the window to its side is being redone and
10230 (b) we do a frame-based redisplay. This is a consequence
10231 of how inverted lines are drawn in frame-based redisplay. */
10232 || (!just_this_one_p
10233 && !FRAME_WINDOW_P (f)
10234 && !WINDOW_FULL_WIDTH_P (w))
10235 /* Line number to display. */
10236 || INTEGERP (w->base_line_pos)
10237 /* Column number is displayed and different from the one displayed. */
10238 || (!NILP (w->column_number_displayed)
10239 && XFASTINT (w->column_number_displayed) != current_column ()))
10240 /* This means that the window has a mode line. */
10241 && (WINDOW_WANTS_MODELINE_P (w)
10242 || WINDOW_WANTS_HEADER_LINE_P (w)))
10243 {
10244 Lisp_Object old_selected_frame;
10245
10246 old_selected_frame = selected_frame;
10247
10248 XSETFRAME (selected_frame, f);
10249 display_mode_lines (w);
10250 selected_frame = old_selected_frame;
10251
10252 /* If mode line height has changed, arrange for a thorough
10253 immediate redisplay using the correct mode line height. */
10254 if (WINDOW_WANTS_MODELINE_P (w)
10255 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10256 {
10257 fonts_changed_p = 1;
10258 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10259 = DESIRED_MODE_LINE_HEIGHT (w);
10260 }
10261
10262 /* If top line height has changed, arrange for a thorough
10263 immediate redisplay using the correct mode line height. */
10264 if (WINDOW_WANTS_HEADER_LINE_P (w)
10265 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10266 {
10267 fonts_changed_p = 1;
10268 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10269 = DESIRED_HEADER_LINE_HEIGHT (w);
10270 }
10271
10272 if (fonts_changed_p)
10273 goto finish_scroll_bars;
10274 }
10275
10276 if (!line_number_displayed
10277 && !BUFFERP (w->base_line_pos))
10278 {
10279 w->base_line_pos = Qnil;
10280 w->base_line_number = Qnil;
10281 }
10282
10283 finish_menu_bars:
10284
10285 /* When we reach a frame's selected window, redo the frame's menu bar. */
10286 if (update_mode_line
10287 && EQ (FRAME_SELECTED_WINDOW (f), window))
10288 {
10289 int redisplay_menu_p = 0;
10290
10291 if (FRAME_WINDOW_P (f))
10292 {
10293 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10294 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10295 #else
10296 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10297 #endif
10298 }
10299 else
10300 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10301
10302 if (redisplay_menu_p)
10303 display_menu_bar (w);
10304
10305 #ifdef HAVE_WINDOW_SYSTEM
10306 if (WINDOWP (f->tool_bar_window)
10307 && (FRAME_TOOL_BAR_LINES (f) > 0
10308 || auto_resize_tool_bars_p))
10309 redisplay_tool_bar (f);
10310 #endif
10311 }
10312
10313 finish_scroll_bars:
10314
10315 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10316 {
10317 int start, end, whole;
10318
10319 /* Calculate the start and end positions for the current window.
10320 At some point, it would be nice to choose between scrollbars
10321 which reflect the whole buffer size, with special markers
10322 indicating narrowing, and scrollbars which reflect only the
10323 visible region.
10324
10325 Note that mini-buffers sometimes aren't displaying any text. */
10326 if (!MINI_WINDOW_P (w)
10327 || (w == XWINDOW (minibuf_window)
10328 && NILP (echo_area_buffer[0])))
10329 {
10330 whole = ZV - BEGV;
10331 start = marker_position (w->start) - BEGV;
10332 /* I don't think this is guaranteed to be right. For the
10333 moment, we'll pretend it is. */
10334 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10335
10336 if (end < start)
10337 end = start;
10338 if (whole < (end - start))
10339 whole = end - start;
10340 }
10341 else
10342 start = end = whole = 0;
10343
10344 /* Indicate what this scroll bar ought to be displaying now. */
10345 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10346
10347 /* Note that we actually used the scroll bar attached to this
10348 window, so it shouldn't be deleted at the end of redisplay. */
10349 redeem_scroll_bar_hook (w);
10350 }
10351
10352 /* Restore current_buffer and value of point in it. */
10353 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10354 set_buffer_internal_1 (old);
10355 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10356
10357 unbind_to (count, Qnil);
10358 }
10359
10360
10361 /* Build the complete desired matrix of WINDOW with a window start
10362 buffer position POS. Value is non-zero if successful. It is zero
10363 if fonts were loaded during redisplay which makes re-adjusting
10364 glyph matrices necessary. */
10365
10366 int
10367 try_window (window, pos)
10368 Lisp_Object window;
10369 struct text_pos pos;
10370 {
10371 struct window *w = XWINDOW (window);
10372 struct it it;
10373 struct glyph_row *last_text_row = NULL;
10374
10375 /* Make POS the new window start. */
10376 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10377
10378 /* Mark cursor position as unknown. No overlay arrow seen. */
10379 w->cursor.vpos = -1;
10380 overlay_arrow_seen = 0;
10381
10382 /* Initialize iterator and info to start at POS. */
10383 start_display (&it, w, pos);
10384
10385 /* Display all lines of W. */
10386 while (it.current_y < it.last_visible_y)
10387 {
10388 if (display_line (&it))
10389 last_text_row = it.glyph_row - 1;
10390 if (fonts_changed_p)
10391 return 0;
10392 }
10393
10394 /* If bottom moved off end of frame, change mode line percentage. */
10395 if (XFASTINT (w->window_end_pos) <= 0
10396 && Z != IT_CHARPOS (it))
10397 w->update_mode_line = Qt;
10398
10399 /* Set window_end_pos to the offset of the last character displayed
10400 on the window from the end of current_buffer. Set
10401 window_end_vpos to its row number. */
10402 if (last_text_row)
10403 {
10404 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10405 w->window_end_bytepos
10406 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10407 w->window_end_pos
10408 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10409 w->window_end_vpos
10410 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10411 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10412 ->displays_text_p);
10413 }
10414 else
10415 {
10416 w->window_end_bytepos = 0;
10417 w->window_end_pos = w->window_end_vpos = make_number (0);
10418 }
10419
10420 /* But that is not valid info until redisplay finishes. */
10421 w->window_end_valid = Qnil;
10422 return 1;
10423 }
10424
10425
10426 \f
10427 /************************************************************************
10428 Window redisplay reusing current matrix when buffer has not changed
10429 ************************************************************************/
10430
10431 /* Try redisplay of window W showing an unchanged buffer with a
10432 different window start than the last time it was displayed by
10433 reusing its current matrix. Value is non-zero if successful.
10434 W->start is the new window start. */
10435
10436 static int
10437 try_window_reusing_current_matrix (w)
10438 struct window *w;
10439 {
10440 struct frame *f = XFRAME (w->frame);
10441 struct glyph_row *row, *bottom_row;
10442 struct it it;
10443 struct run run;
10444 struct text_pos start, new_start;
10445 int nrows_scrolled, i;
10446 struct glyph_row *last_text_row;
10447 struct glyph_row *last_reused_text_row;
10448 struct glyph_row *start_row;
10449 int start_vpos, min_y, max_y;
10450
10451 if (/* This function doesn't handle terminal frames. */
10452 !FRAME_WINDOW_P (f)
10453 /* Don't try to reuse the display if windows have been split
10454 or such. */
10455 || windows_or_buffers_changed)
10456 return 0;
10457
10458 /* Can't do this if region may have changed. */
10459 if ((!NILP (Vtransient_mark_mode)
10460 && !NILP (current_buffer->mark_active))
10461 || !NILP (w->region_showing)
10462 || !NILP (Vshow_trailing_whitespace))
10463 return 0;
10464
10465 /* If top-line visibility has changed, give up. */
10466 if (WINDOW_WANTS_HEADER_LINE_P (w)
10467 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10468 return 0;
10469
10470 /* Give up if old or new display is scrolled vertically. We could
10471 make this function handle this, but right now it doesn't. */
10472 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10473 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10474 return 0;
10475
10476 /* The variable new_start now holds the new window start. The old
10477 start `start' can be determined from the current matrix. */
10478 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10479 start = start_row->start.pos;
10480 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10481
10482 /* Clear the desired matrix for the display below. */
10483 clear_glyph_matrix (w->desired_matrix);
10484
10485 if (CHARPOS (new_start) <= CHARPOS (start))
10486 {
10487 int first_row_y;
10488
10489 /* Don't use this method if the display starts with an ellipsis
10490 displayed for invisible text. It's not easy to handle that case
10491 below, and it's certainly not worth the effort since this is
10492 not a frequent case. */
10493 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10494 return 0;
10495
10496 IF_DEBUG (debug_method_add (w, "twu1"));
10497
10498 /* Display up to a row that can be reused. The variable
10499 last_text_row is set to the last row displayed that displays
10500 text. Note that it.vpos == 0 if or if not there is a
10501 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10502 start_display (&it, w, new_start);
10503 first_row_y = it.current_y;
10504 w->cursor.vpos = -1;
10505 last_text_row = last_reused_text_row = NULL;
10506
10507 while (it.current_y < it.last_visible_y
10508 && IT_CHARPOS (it) < CHARPOS (start)
10509 && !fonts_changed_p)
10510 if (display_line (&it))
10511 last_text_row = it.glyph_row - 1;
10512
10513 /* A value of current_y < last_visible_y means that we stopped
10514 at the previous window start, which in turn means that we
10515 have at least one reusable row. */
10516 if (it.current_y < it.last_visible_y)
10517 {
10518 /* IT.vpos always starts from 0; it counts text lines. */
10519 nrows_scrolled = it.vpos;
10520
10521 /* Find PT if not already found in the lines displayed. */
10522 if (w->cursor.vpos < 0)
10523 {
10524 int dy = it.current_y - first_row_y;
10525
10526 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10527 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10528 {
10529 if (PT >= MATRIX_ROW_START_CHARPOS (row)
10530 && PT < MATRIX_ROW_END_CHARPOS (row))
10531 {
10532 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10533 dy, nrows_scrolled);
10534 break;
10535 }
10536
10537 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10538 break;
10539
10540 ++row;
10541 }
10542
10543 /* Give up if point was not found. This shouldn't
10544 happen often; not more often than with try_window
10545 itself. */
10546 if (w->cursor.vpos < 0)
10547 {
10548 clear_glyph_matrix (w->desired_matrix);
10549 return 0;
10550 }
10551 }
10552
10553 /* Scroll the display. Do it before the current matrix is
10554 changed. The problem here is that update has not yet
10555 run, i.e. part of the current matrix is not up to date.
10556 scroll_run_hook will clear the cursor, and use the
10557 current matrix to get the height of the row the cursor is
10558 in. */
10559 run.current_y = first_row_y;
10560 run.desired_y = it.current_y;
10561 run.height = it.last_visible_y - it.current_y;
10562
10563 if (run.height > 0 && run.current_y != run.desired_y)
10564 {
10565 update_begin (f);
10566 rif->update_window_begin_hook (w);
10567 rif->clear_mouse_face (w);
10568 rif->scroll_run_hook (w, &run);
10569 rif->update_window_end_hook (w, 0, 0);
10570 update_end (f);
10571 }
10572
10573 /* Shift current matrix down by nrows_scrolled lines. */
10574 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10575 rotate_matrix (w->current_matrix,
10576 start_vpos,
10577 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10578 nrows_scrolled);
10579
10580 /* Disable lines that must be updated. */
10581 for (i = 0; i < it.vpos; ++i)
10582 (start_row + i)->enabled_p = 0;
10583
10584 /* Re-compute Y positions. */
10585 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10586 max_y = it.last_visible_y;
10587 for (row = start_row + nrows_scrolled;
10588 row < bottom_row;
10589 ++row)
10590 {
10591 row->y = it.current_y;
10592
10593 if (row->y < min_y)
10594 row->visible_height = row->height - (min_y - row->y);
10595 else if (row->y + row->height > max_y)
10596 row->visible_height
10597 = row->height - (row->y + row->height - max_y);
10598 else
10599 row->visible_height = row->height;
10600
10601 it.current_y += row->height;
10602
10603 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10604 last_reused_text_row = row;
10605 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10606 break;
10607 }
10608
10609 /* Disable lines in the current matrix which are now
10610 below the window. */
10611 for (++row; row < bottom_row; ++row)
10612 row->enabled_p = 0;
10613 }
10614
10615 /* Update window_end_pos etc.; last_reused_text_row is the last
10616 reused row from the current matrix containing text, if any.
10617 The value of last_text_row is the last displayed line
10618 containing text. */
10619 if (last_reused_text_row)
10620 {
10621 w->window_end_bytepos
10622 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10623 w->window_end_pos
10624 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10625 w->window_end_vpos
10626 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10627 w->current_matrix));
10628 }
10629 else if (last_text_row)
10630 {
10631 w->window_end_bytepos
10632 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10633 w->window_end_pos
10634 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10635 w->window_end_vpos
10636 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10637 }
10638 else
10639 {
10640 /* This window must be completely empty. */
10641 w->window_end_bytepos = 0;
10642 w->window_end_pos = w->window_end_vpos = make_number (0);
10643 }
10644 w->window_end_valid = Qnil;
10645
10646 /* Update hint: don't try scrolling again in update_window. */
10647 w->desired_matrix->no_scrolling_p = 1;
10648
10649 #if GLYPH_DEBUG
10650 debug_method_add (w, "try_window_reusing_current_matrix 1");
10651 #endif
10652 return 1;
10653 }
10654 else if (CHARPOS (new_start) > CHARPOS (start))
10655 {
10656 struct glyph_row *pt_row, *row;
10657 struct glyph_row *first_reusable_row;
10658 struct glyph_row *first_row_to_display;
10659 int dy;
10660 int yb = window_text_bottom_y (w);
10661
10662 /* Find the row starting at new_start, if there is one. Don't
10663 reuse a partially visible line at the end. */
10664 first_reusable_row = start_row;
10665 while (first_reusable_row->enabled_p
10666 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10667 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10668 < CHARPOS (new_start)))
10669 ++first_reusable_row;
10670
10671 /* Give up if there is no row to reuse. */
10672 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10673 || !first_reusable_row->enabled_p
10674 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10675 != CHARPOS (new_start)))
10676 return 0;
10677
10678 /* We can reuse fully visible rows beginning with
10679 first_reusable_row to the end of the window. Set
10680 first_row_to_display to the first row that cannot be reused.
10681 Set pt_row to the row containing point, if there is any. */
10682 pt_row = NULL;
10683 for (first_row_to_display = first_reusable_row;
10684 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
10685 ++first_row_to_display)
10686 {
10687 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10688 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10689 pt_row = first_row_to_display;
10690 }
10691
10692 /* Start displaying at the start of first_row_to_display. */
10693 xassert (first_row_to_display->y < yb);
10694 init_to_row_start (&it, w, first_row_to_display);
10695
10696 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10697 - start_vpos);
10698 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10699 - nrows_scrolled);
10700 it.current_y = (first_row_to_display->y - first_reusable_row->y
10701 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10702
10703 /* Display lines beginning with first_row_to_display in the
10704 desired matrix. Set last_text_row to the last row displayed
10705 that displays text. */
10706 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10707 if (pt_row == NULL)
10708 w->cursor.vpos = -1;
10709 last_text_row = NULL;
10710 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10711 if (display_line (&it))
10712 last_text_row = it.glyph_row - 1;
10713
10714 /* Give up If point isn't in a row displayed or reused. */
10715 if (w->cursor.vpos < 0)
10716 {
10717 clear_glyph_matrix (w->desired_matrix);
10718 return 0;
10719 }
10720
10721 /* If point is in a reused row, adjust y and vpos of the cursor
10722 position. */
10723 if (pt_row)
10724 {
10725 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10726 w->current_matrix);
10727 w->cursor.y -= first_reusable_row->y;
10728 }
10729
10730 /* Scroll the display. */
10731 run.current_y = first_reusable_row->y;
10732 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10733 run.height = it.last_visible_y - run.current_y;
10734 dy = run.current_y - run.desired_y;
10735
10736 if (run.height)
10737 {
10738 struct frame *f = XFRAME (WINDOW_FRAME (w));
10739 update_begin (f);
10740 rif->update_window_begin_hook (w);
10741 rif->clear_mouse_face (w);
10742 rif->scroll_run_hook (w, &run);
10743 rif->update_window_end_hook (w, 0, 0);
10744 update_end (f);
10745 }
10746
10747 /* Adjust Y positions of reused rows. */
10748 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10749 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10750 max_y = it.last_visible_y;
10751 for (row = first_reusable_row; row < first_row_to_display; ++row)
10752 {
10753 row->y -= dy;
10754 if (row->y < min_y)
10755 row->visible_height = row->height - (min_y - row->y);
10756 else if (row->y + row->height > max_y)
10757 row->visible_height
10758 = row->height - (row->y + row->height - max_y);
10759 else
10760 row->visible_height = row->height;
10761 }
10762
10763 /* Scroll the current matrix. */
10764 xassert (nrows_scrolled > 0);
10765 rotate_matrix (w->current_matrix,
10766 start_vpos,
10767 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10768 -nrows_scrolled);
10769
10770 /* Disable rows not reused. */
10771 for (row -= nrows_scrolled; row < bottom_row; ++row)
10772 row->enabled_p = 0;
10773
10774 /* Adjust window end. A null value of last_text_row means that
10775 the window end is in reused rows which in turn means that
10776 only its vpos can have changed. */
10777 if (last_text_row)
10778 {
10779 w->window_end_bytepos
10780 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10781 w->window_end_pos
10782 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10783 w->window_end_vpos
10784 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10785 }
10786 else
10787 {
10788 w->window_end_vpos
10789 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
10790 }
10791
10792 w->window_end_valid = Qnil;
10793 w->desired_matrix->no_scrolling_p = 1;
10794
10795 #if GLYPH_DEBUG
10796 debug_method_add (w, "try_window_reusing_current_matrix 2");
10797 #endif
10798 return 1;
10799 }
10800
10801 return 0;
10802 }
10803
10804
10805 \f
10806 /************************************************************************
10807 Window redisplay reusing current matrix when buffer has changed
10808 ************************************************************************/
10809
10810 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10811 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10812 int *, int *));
10813 static struct glyph_row *
10814 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10815 struct glyph_row *));
10816
10817
10818 /* Return the last row in MATRIX displaying text. If row START is
10819 non-null, start searching with that row. IT gives the dimensions
10820 of the display. Value is null if matrix is empty; otherwise it is
10821 a pointer to the row found. */
10822
10823 static struct glyph_row *
10824 find_last_row_displaying_text (matrix, it, start)
10825 struct glyph_matrix *matrix;
10826 struct it *it;
10827 struct glyph_row *start;
10828 {
10829 struct glyph_row *row, *row_found;
10830
10831 /* Set row_found to the last row in IT->w's current matrix
10832 displaying text. The loop looks funny but think of partially
10833 visible lines. */
10834 row_found = NULL;
10835 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10836 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10837 {
10838 xassert (row->enabled_p);
10839 row_found = row;
10840 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10841 break;
10842 ++row;
10843 }
10844
10845 return row_found;
10846 }
10847
10848
10849 /* Return the last row in the current matrix of W that is not affected
10850 by changes at the start of current_buffer that occurred since W's
10851 current matrix was built. Value is null if no such row exists.
10852
10853 BEG_UNCHANGED us the number of characters unchanged at the start of
10854 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
10855 first changed character in current_buffer. Characters at positions <
10856 BEG + BEG_UNCHANGED are at the same buffer positions as they were
10857 when the current matrix was built. */
10858
10859 static struct glyph_row *
10860 find_last_unchanged_at_beg_row (w)
10861 struct window *w;
10862 {
10863 int first_changed_pos = BEG + BEG_UNCHANGED;
10864 struct glyph_row *row;
10865 struct glyph_row *row_found = NULL;
10866 int yb = window_text_bottom_y (w);
10867
10868 /* Find the last row displaying unchanged text. */
10869 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10870 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10871 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10872 {
10873 if (/* If row ends before first_changed_pos, it is unchanged,
10874 except in some case. */
10875 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10876 /* When row ends in ZV and we write at ZV it is not
10877 unchanged. */
10878 && !row->ends_at_zv_p
10879 /* When first_changed_pos is the end of a continued line,
10880 row is not unchanged because it may be no longer
10881 continued. */
10882 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10883 && row->continued_p))
10884 row_found = row;
10885
10886 /* Stop if last visible row. */
10887 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10888 break;
10889
10890 ++row;
10891 }
10892
10893 return row_found;
10894 }
10895
10896
10897 /* Find the first glyph row in the current matrix of W that is not
10898 affected by changes at the end of current_buffer since the
10899 time W's current matrix was built.
10900
10901 Return in *DELTA the number of chars by which buffer positions in
10902 unchanged text at the end of current_buffer must be adjusted.
10903
10904 Return in *DELTA_BYTES the corresponding number of bytes.
10905
10906 Value is null if no such row exists, i.e. all rows are affected by
10907 changes. */
10908
10909 static struct glyph_row *
10910 find_first_unchanged_at_end_row (w, delta, delta_bytes)
10911 struct window *w;
10912 int *delta, *delta_bytes;
10913 {
10914 struct glyph_row *row;
10915 struct glyph_row *row_found = NULL;
10916
10917 *delta = *delta_bytes = 0;
10918
10919 /* Display must not have been paused, otherwise the current matrix
10920 is not up to date. */
10921 if (NILP (w->window_end_valid))
10922 abort ();
10923
10924 /* A value of window_end_pos >= END_UNCHANGED means that the window
10925 end is in the range of changed text. If so, there is no
10926 unchanged row at the end of W's current matrix. */
10927 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
10928 return NULL;
10929
10930 /* Set row to the last row in W's current matrix displaying text. */
10931 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10932
10933 /* If matrix is entirely empty, no unchanged row exists. */
10934 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10935 {
10936 /* The value of row is the last glyph row in the matrix having a
10937 meaningful buffer position in it. The end position of row
10938 corresponds to window_end_pos. This allows us to translate
10939 buffer positions in the current matrix to current buffer
10940 positions for characters not in changed text. */
10941 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10942 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10943 int last_unchanged_pos, last_unchanged_pos_old;
10944 struct glyph_row *first_text_row
10945 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10946
10947 *delta = Z - Z_old;
10948 *delta_bytes = Z_BYTE - Z_BYTE_old;
10949
10950 /* Set last_unchanged_pos to the buffer position of the last
10951 character in the buffer that has not been changed. Z is the
10952 index + 1 of the last character in current_buffer, i.e. by
10953 subtracting END_UNCHANGED we get the index of the last
10954 unchanged character, and we have to add BEG to get its buffer
10955 position. */
10956 last_unchanged_pos = Z - END_UNCHANGED + BEG;
10957 last_unchanged_pos_old = last_unchanged_pos - *delta;
10958
10959 /* Search backward from ROW for a row displaying a line that
10960 starts at a minimum position >= last_unchanged_pos_old. */
10961 for (; row > first_text_row; --row)
10962 {
10963 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
10964 abort ();
10965
10966 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10967 row_found = row;
10968 }
10969 }
10970
10971 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
10972 abort ();
10973
10974 return row_found;
10975 }
10976
10977
10978 /* Make sure that glyph rows in the current matrix of window W
10979 reference the same glyph memory as corresponding rows in the
10980 frame's frame matrix. This function is called after scrolling W's
10981 current matrix on a terminal frame in try_window_id and
10982 try_window_reusing_current_matrix. */
10983
10984 static void
10985 sync_frame_with_window_matrix_rows (w)
10986 struct window *w;
10987 {
10988 struct frame *f = XFRAME (w->frame);
10989 struct glyph_row *window_row, *window_row_end, *frame_row;
10990
10991 /* Preconditions: W must be a leaf window and full-width. Its frame
10992 must have a frame matrix. */
10993 xassert (NILP (w->hchild) && NILP (w->vchild));
10994 xassert (WINDOW_FULL_WIDTH_P (w));
10995 xassert (!FRAME_WINDOW_P (f));
10996
10997 /* If W is a full-width window, glyph pointers in W's current matrix
10998 have, by definition, to be the same as glyph pointers in the
10999 corresponding frame matrix. */
11000 window_row = w->current_matrix->rows;
11001 window_row_end = window_row + w->current_matrix->nrows;
11002 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11003 while (window_row < window_row_end)
11004 {
11005 int area;
11006
11007 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
11008 frame_row->glyphs[area] = window_row->glyphs[area];
11009
11010 /* Disable frame rows whose corresponding window rows have
11011 been disabled in try_window_id. */
11012 if (!window_row->enabled_p)
11013 frame_row->enabled_p = 0;
11014
11015 ++window_row, ++frame_row;
11016 }
11017 }
11018
11019
11020 /* Find the glyph row in window W containing CHARPOS. Consider all
11021 rows between START and END (not inclusive). END null means search
11022 all rows to the end of the display area of W. Value is the row
11023 containing CHARPOS or null. */
11024
11025 static struct glyph_row *
11026 row_containing_pos (w, charpos, start, end)
11027 struct window *w;
11028 int charpos;
11029 struct glyph_row *start, *end;
11030 {
11031 struct glyph_row *row = start;
11032 int last_y;
11033
11034 /* If we happen to start on a header-line, skip that. */
11035 if (row->mode_line_p)
11036 ++row;
11037
11038 if ((end && row >= end) || !row->enabled_p)
11039 return NULL;
11040
11041 last_y = window_text_bottom_y (w);
11042
11043 while ((end == NULL || row < end)
11044 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11045 /* The end position of a row equals the start
11046 position of the next row. If CHARPOS is there, we
11047 would rather display it in the next line, except
11048 when this line ends in ZV. */
11049 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11050 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11051 || !row->ends_at_zv_p)))
11052 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11053 ++row;
11054
11055 /* Give up if CHARPOS not found. */
11056 if ((end && row >= end)
11057 || charpos < MATRIX_ROW_START_CHARPOS (row)
11058 || charpos > MATRIX_ROW_END_CHARPOS (row))
11059 row = NULL;
11060
11061 return row;
11062 }
11063
11064
11065 /* Try to redisplay window W by reusing its existing display. W's
11066 current matrix must be up to date when this function is called,
11067 i.e. window_end_valid must not be nil.
11068
11069 Value is
11070
11071 1 if display has been updated
11072 0 if otherwise unsuccessful
11073 -1 if redisplay with same window start is known not to succeed
11074
11075 The following steps are performed:
11076
11077 1. Find the last row in the current matrix of W that is not
11078 affected by changes at the start of current_buffer. If no such row
11079 is found, give up.
11080
11081 2. Find the first row in W's current matrix that is not affected by
11082 changes at the end of current_buffer. Maybe there is no such row.
11083
11084 3. Display lines beginning with the row + 1 found in step 1 to the
11085 row found in step 2 or, if step 2 didn't find a row, to the end of
11086 the window.
11087
11088 4. If cursor is not known to appear on the window, give up.
11089
11090 5. If display stopped at the row found in step 2, scroll the
11091 display and current matrix as needed.
11092
11093 6. Maybe display some lines at the end of W, if we must. This can
11094 happen under various circumstances, like a partially visible line
11095 becoming fully visible, or because newly displayed lines are displayed
11096 in smaller font sizes.
11097
11098 7. Update W's window end information. */
11099
11100 static int
11101 try_window_id (w)
11102 struct window *w;
11103 {
11104 struct frame *f = XFRAME (w->frame);
11105 struct glyph_matrix *current_matrix = w->current_matrix;
11106 struct glyph_matrix *desired_matrix = w->desired_matrix;
11107 struct glyph_row *last_unchanged_at_beg_row;
11108 struct glyph_row *first_unchanged_at_end_row;
11109 struct glyph_row *row;
11110 struct glyph_row *bottom_row;
11111 int bottom_vpos;
11112 struct it it;
11113 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11114 struct text_pos start_pos;
11115 struct run run;
11116 int first_unchanged_at_end_vpos = 0;
11117 struct glyph_row *last_text_row, *last_text_row_at_end;
11118 struct text_pos start;
11119 int first_changed_charpos, last_changed_charpos;
11120
11121 /* This is handy for debugging. */
11122 #if 0
11123 #define GIVE_UP(X) \
11124 do { \
11125 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11126 return 0; \
11127 } while (0)
11128 #else
11129 #define GIVE_UP(X) return 0
11130 #endif
11131
11132 SET_TEXT_POS_FROM_MARKER (start, w->start);
11133
11134 /* Don't use this for mini-windows because these can show
11135 messages and mini-buffers, and we don't handle that here. */
11136 if (MINI_WINDOW_P (w))
11137 GIVE_UP (1);
11138
11139 /* This flag is used to prevent redisplay optimizations. */
11140 if (windows_or_buffers_changed)
11141 GIVE_UP (2);
11142
11143 /* Verify that narrowing has not changed. This flag is also set to prevent
11144 redisplay optimizations. It would be nice to further
11145 reduce the number of cases where this prevents try_window_id. */
11146 if (current_buffer->clip_changed)
11147 GIVE_UP (3);
11148
11149 /* Window must either use window-based redisplay or be full width. */
11150 if (!FRAME_WINDOW_P (f)
11151 && (!line_ins_del_ok
11152 || !WINDOW_FULL_WIDTH_P (w)))
11153 GIVE_UP (4);
11154
11155 /* Give up if point is not known NOT to appear in W. */
11156 if (PT < CHARPOS (start))
11157 GIVE_UP (5);
11158
11159 /* Another way to prevent redisplay optimizations. */
11160 if (XFASTINT (w->last_modified) == 0)
11161 GIVE_UP (6);
11162
11163 /* Verify that window is not hscrolled. */
11164 if (XFASTINT (w->hscroll) != 0)
11165 GIVE_UP (7);
11166
11167 /* Verify that display wasn't paused. */
11168 if (NILP (w->window_end_valid))
11169 GIVE_UP (8);
11170
11171 /* Can't use this if highlighting a region because a cursor movement
11172 will do more than just set the cursor. */
11173 if (!NILP (Vtransient_mark_mode)
11174 && !NILP (current_buffer->mark_active))
11175 GIVE_UP (9);
11176
11177 /* Likewise if highlighting trailing whitespace. */
11178 if (!NILP (Vshow_trailing_whitespace))
11179 GIVE_UP (11);
11180
11181 /* Likewise if showing a region. */
11182 if (!NILP (w->region_showing))
11183 GIVE_UP (10);
11184
11185 /* Can use this if overlay arrow position and or string have changed. */
11186 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11187 || !EQ (last_arrow_string, Voverlay_arrow_string))
11188 GIVE_UP (12);
11189
11190
11191 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11192 only if buffer has really changed. The reason is that the gap is
11193 initially at Z for freshly visited files. The code below would
11194 set end_unchanged to 0 in that case. */
11195 if (MODIFF > SAVE_MODIFF
11196 /* This seems to happen sometimes after saving a buffer. */
11197 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11198 {
11199 if (GPT - BEG < BEG_UNCHANGED)
11200 BEG_UNCHANGED = GPT - BEG;
11201 if (Z - GPT < END_UNCHANGED)
11202 END_UNCHANGED = Z - GPT;
11203 }
11204
11205 /* The position of the first and last character that has been changed. */
11206 first_changed_charpos = BEG + BEG_UNCHANGED;
11207 last_changed_charpos = Z - END_UNCHANGED;
11208
11209 /* If window starts after a line end, and the last change is in
11210 front of that newline, then changes don't affect the display.
11211 This case happens with stealth-fontification. Note that although
11212 the display is unchanged, glyph positions in the matrix have to
11213 be adjusted, of course. */
11214 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11215 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11216 && ((last_changed_charpos < CHARPOS (start)
11217 && CHARPOS (start) == BEGV)
11218 || (last_changed_charpos < CHARPOS (start) - 1
11219 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11220 {
11221 int Z_old, delta, Z_BYTE_old, delta_bytes;
11222 struct glyph_row *r0;
11223
11224 /* Compute how many chars/bytes have been added to or removed
11225 from the buffer. */
11226 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11227 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11228 delta = Z - Z_old;
11229 delta_bytes = Z_BYTE - Z_BYTE_old;
11230
11231 /* Give up if PT is not in the window. Note that it already has
11232 been checked at the start of try_window_id that PT is not in
11233 front of the window start. */
11234 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11235 GIVE_UP (13);
11236
11237 /* If window start is unchanged, we can reuse the whole matrix
11238 as is, after adjusting glyph positions. No need to compute
11239 the window end again, since its offset from Z hasn't changed. */
11240 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11241 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11242 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11243 {
11244 /* Adjust positions in the glyph matrix. */
11245 if (delta || delta_bytes)
11246 {
11247 struct glyph_row *r1
11248 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11249 increment_matrix_positions (w->current_matrix,
11250 MATRIX_ROW_VPOS (r0, current_matrix),
11251 MATRIX_ROW_VPOS (r1, current_matrix),
11252 delta, delta_bytes);
11253 }
11254
11255 /* Set the cursor. */
11256 row = row_containing_pos (w, PT, r0, NULL);
11257 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11258 return 1;
11259 }
11260 }
11261
11262 /* Handle the case that changes are all below what is displayed in
11263 the window, and that PT is in the window. This shortcut cannot
11264 be taken if ZV is visible in the window, and text has been added
11265 there that is visible in the window. */
11266 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11267 /* ZV is not visible in the window, or there are no
11268 changes at ZV, actually. */
11269 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11270 || first_changed_charpos == last_changed_charpos))
11271 {
11272 struct glyph_row *r0;
11273
11274 /* Give up if PT is not in the window. Note that it already has
11275 been checked at the start of try_window_id that PT is not in
11276 front of the window start. */
11277 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11278 GIVE_UP (14);
11279
11280 /* If window start is unchanged, we can reuse the whole matrix
11281 as is, without changing glyph positions since no text has
11282 been added/removed in front of the window end. */
11283 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11284 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11285 {
11286 /* We have to compute the window end anew since text
11287 can have been added/removed after it. */
11288 w->window_end_pos
11289 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11290 w->window_end_bytepos
11291 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11292
11293 /* Set the cursor. */
11294 row = row_containing_pos (w, PT, r0, NULL);
11295 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11296 return 2;
11297 }
11298 }
11299
11300 /* Give up if window start is in the changed area.
11301
11302 The condition used to read
11303
11304 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11305
11306 but why that was tested escapes me at the moment. */
11307 if (CHARPOS (start) >= first_changed_charpos
11308 && CHARPOS (start) <= last_changed_charpos)
11309 GIVE_UP (15);
11310
11311 /* Check that window start agrees with the start of the first glyph
11312 row in its current matrix. Check this after we know the window
11313 start is not in changed text, otherwise positions would not be
11314 comparable. */
11315 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11316 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11317 GIVE_UP (16);
11318
11319 /* Compute the position at which we have to start displaying new
11320 lines. Some of the lines at the top of the window might be
11321 reusable because they are not displaying changed text. Find the
11322 last row in W's current matrix not affected by changes at the
11323 start of current_buffer. Value is null if changes start in the
11324 first line of window. */
11325 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11326 if (last_unchanged_at_beg_row)
11327 {
11328 /* Avoid starting to display in the moddle of a character, a TAB
11329 for instance. This is easier than to set up the iterator
11330 exactly, and it's not a frequent case, so the additional
11331 effort wouldn't really pay off. */
11332 while (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11333 && last_unchanged_at_beg_row > w->current_matrix->rows)
11334 --last_unchanged_at_beg_row;
11335
11336 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11337 GIVE_UP (17);
11338
11339 init_to_row_end (&it, w, last_unchanged_at_beg_row);
11340 start_pos = it.current.pos;
11341
11342 /* Start displaying new lines in the desired matrix at the same
11343 vpos we would use in the current matrix, i.e. below
11344 last_unchanged_at_beg_row. */
11345 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11346 current_matrix);
11347 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11348 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11349
11350 xassert (it.hpos == 0 && it.current_x == 0);
11351 }
11352 else
11353 {
11354 /* There are no reusable lines at the start of the window.
11355 Start displaying in the first line. */
11356 start_display (&it, w, start);
11357 start_pos = it.current.pos;
11358 }
11359
11360 /* Find the first row that is not affected by changes at the end of
11361 the buffer. Value will be null if there is no unchanged row, in
11362 which case we must redisplay to the end of the window. delta
11363 will be set to the value by which buffer positions beginning with
11364 first_unchanged_at_end_row have to be adjusted due to text
11365 changes. */
11366 first_unchanged_at_end_row
11367 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11368 IF_DEBUG (debug_delta = delta);
11369 IF_DEBUG (debug_delta_bytes = delta_bytes);
11370
11371 /* Set stop_pos to the buffer position up to which we will have to
11372 display new lines. If first_unchanged_at_end_row != NULL, this
11373 is the buffer position of the start of the line displayed in that
11374 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11375 that we don't stop at a buffer position. */
11376 stop_pos = 0;
11377 if (first_unchanged_at_end_row)
11378 {
11379 xassert (last_unchanged_at_beg_row == NULL
11380 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11381
11382 /* If this is a continuation line, move forward to the next one
11383 that isn't. Changes in lines above affect this line.
11384 Caution: this may move first_unchanged_at_end_row to a row
11385 not displaying text. */
11386 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11387 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11388 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11389 < it.last_visible_y))
11390 ++first_unchanged_at_end_row;
11391
11392 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11393 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11394 >= it.last_visible_y))
11395 first_unchanged_at_end_row = NULL;
11396 else
11397 {
11398 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11399 + delta);
11400 first_unchanged_at_end_vpos
11401 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11402 xassert (stop_pos >= Z - END_UNCHANGED);
11403 }
11404 }
11405 else if (last_unchanged_at_beg_row == NULL)
11406 GIVE_UP (18);
11407
11408
11409 #if GLYPH_DEBUG
11410
11411 /* Either there is no unchanged row at the end, or the one we have
11412 now displays text. This is a necessary condition for the window
11413 end pos calculation at the end of this function. */
11414 xassert (first_unchanged_at_end_row == NULL
11415 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11416
11417 debug_last_unchanged_at_beg_vpos
11418 = (last_unchanged_at_beg_row
11419 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11420 : -1);
11421 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11422
11423 #endif /* GLYPH_DEBUG != 0 */
11424
11425
11426 /* Display new lines. Set last_text_row to the last new line
11427 displayed which has text on it, i.e. might end up as being the
11428 line where the window_end_vpos is. */
11429 w->cursor.vpos = -1;
11430 last_text_row = NULL;
11431 overlay_arrow_seen = 0;
11432 while (it.current_y < it.last_visible_y
11433 && !fonts_changed_p
11434 && (first_unchanged_at_end_row == NULL
11435 || IT_CHARPOS (it) < stop_pos))
11436 {
11437 if (display_line (&it))
11438 last_text_row = it.glyph_row - 1;
11439 }
11440
11441 if (fonts_changed_p)
11442 return -1;
11443
11444
11445 /* Compute differences in buffer positions, y-positions etc. for
11446 lines reused at the bottom of the window. Compute what we can
11447 scroll. */
11448 if (first_unchanged_at_end_row
11449 /* No lines reused because we displayed everything up to the
11450 bottom of the window. */
11451 && it.current_y < it.last_visible_y)
11452 {
11453 dvpos = (it.vpos
11454 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11455 current_matrix));
11456 dy = it.current_y - first_unchanged_at_end_row->y;
11457 run.current_y = first_unchanged_at_end_row->y;
11458 run.desired_y = run.current_y + dy;
11459 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11460 }
11461 else
11462 {
11463 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11464 first_unchanged_at_end_row = NULL;
11465 }
11466 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11467
11468
11469 /* Find the cursor if not already found. We have to decide whether
11470 PT will appear on this window (it sometimes doesn't, but this is
11471 not a very frequent case.) This decision has to be made before
11472 the current matrix is altered. A value of cursor.vpos < 0 means
11473 that PT is either in one of the lines beginning at
11474 first_unchanged_at_end_row or below the window. Don't care for
11475 lines that might be displayed later at the window end; as
11476 mentioned, this is not a frequent case. */
11477 if (w->cursor.vpos < 0)
11478 {
11479 /* Cursor in unchanged rows at the top? */
11480 if (PT < CHARPOS (start_pos)
11481 && last_unchanged_at_beg_row)
11482 {
11483 row = row_containing_pos (w, PT,
11484 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11485 last_unchanged_at_beg_row + 1);
11486 if (row)
11487 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11488 }
11489
11490 /* Start from first_unchanged_at_end_row looking for PT. */
11491 else if (first_unchanged_at_end_row)
11492 {
11493 row = row_containing_pos (w, PT - delta,
11494 first_unchanged_at_end_row, NULL);
11495 if (row)
11496 set_cursor_from_row (w, row, w->current_matrix, delta,
11497 delta_bytes, dy, dvpos);
11498 }
11499
11500 /* Give up if cursor was not found. */
11501 if (w->cursor.vpos < 0)
11502 {
11503 clear_glyph_matrix (w->desired_matrix);
11504 return -1;
11505 }
11506 }
11507
11508 /* Don't let the cursor end in the scroll margins. */
11509 {
11510 int this_scroll_margin, cursor_height;
11511
11512 this_scroll_margin = max (0, scroll_margin);
11513 this_scroll_margin = min (this_scroll_margin,
11514 XFASTINT (w->height) / 4);
11515 this_scroll_margin *= CANON_Y_UNIT (it.f);
11516 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11517
11518 if ((w->cursor.y < this_scroll_margin
11519 && CHARPOS (start) > BEGV)
11520 /* Don't take scroll margin into account at the bottom because
11521 old redisplay didn't do it either. */
11522 || w->cursor.y + cursor_height > it.last_visible_y)
11523 {
11524 w->cursor.vpos = -1;
11525 clear_glyph_matrix (w->desired_matrix);
11526 return -1;
11527 }
11528 }
11529
11530 /* Scroll the display. Do it before changing the current matrix so
11531 that xterm.c doesn't get confused about where the cursor glyph is
11532 found. */
11533 if (dy && run.height)
11534 {
11535 update_begin (f);
11536
11537 if (FRAME_WINDOW_P (f))
11538 {
11539 rif->update_window_begin_hook (w);
11540 rif->clear_mouse_face (w);
11541 rif->scroll_run_hook (w, &run);
11542 rif->update_window_end_hook (w, 0, 0);
11543 }
11544 else
11545 {
11546 /* Terminal frame. In this case, dvpos gives the number of
11547 lines to scroll by; dvpos < 0 means scroll up. */
11548 int first_unchanged_at_end_vpos
11549 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11550 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11551 int end = (XFASTINT (w->top)
11552 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11553 + window_internal_height (w));
11554
11555 /* Perform the operation on the screen. */
11556 if (dvpos > 0)
11557 {
11558 /* Scroll last_unchanged_at_beg_row to the end of the
11559 window down dvpos lines. */
11560 set_terminal_window (end);
11561
11562 /* On dumb terminals delete dvpos lines at the end
11563 before inserting dvpos empty lines. */
11564 if (!scroll_region_ok)
11565 ins_del_lines (end - dvpos, -dvpos);
11566
11567 /* Insert dvpos empty lines in front of
11568 last_unchanged_at_beg_row. */
11569 ins_del_lines (from, dvpos);
11570 }
11571 else if (dvpos < 0)
11572 {
11573 /* Scroll up last_unchanged_at_beg_vpos to the end of
11574 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11575 set_terminal_window (end);
11576
11577 /* Delete dvpos lines in front of
11578 last_unchanged_at_beg_vpos. ins_del_lines will set
11579 the cursor to the given vpos and emit |dvpos| delete
11580 line sequences. */
11581 ins_del_lines (from + dvpos, dvpos);
11582
11583 /* On a dumb terminal insert dvpos empty lines at the
11584 end. */
11585 if (!scroll_region_ok)
11586 ins_del_lines (end + dvpos, -dvpos);
11587 }
11588
11589 set_terminal_window (0);
11590 }
11591
11592 update_end (f);
11593 }
11594
11595 /* Shift reused rows of the current matrix to the right position.
11596 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11597 text. */
11598 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11599 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11600 if (dvpos < 0)
11601 {
11602 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11603 bottom_vpos, dvpos);
11604 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11605 bottom_vpos, 0);
11606 }
11607 else if (dvpos > 0)
11608 {
11609 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11610 bottom_vpos, dvpos);
11611 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11612 first_unchanged_at_end_vpos + dvpos, 0);
11613 }
11614
11615 /* For frame-based redisplay, make sure that current frame and window
11616 matrix are in sync with respect to glyph memory. */
11617 if (!FRAME_WINDOW_P (f))
11618 sync_frame_with_window_matrix_rows (w);
11619
11620 /* Adjust buffer positions in reused rows. */
11621 if (delta)
11622 increment_matrix_positions (current_matrix,
11623 first_unchanged_at_end_vpos + dvpos,
11624 bottom_vpos, delta, delta_bytes);
11625
11626 /* Adjust Y positions. */
11627 if (dy)
11628 shift_glyph_matrix (w, current_matrix,
11629 first_unchanged_at_end_vpos + dvpos,
11630 bottom_vpos, dy);
11631
11632 if (first_unchanged_at_end_row)
11633 first_unchanged_at_end_row += dvpos;
11634
11635 /* If scrolling up, there may be some lines to display at the end of
11636 the window. */
11637 last_text_row_at_end = NULL;
11638 if (dy < 0)
11639 {
11640 /* Set last_row to the glyph row in the current matrix where the
11641 window end line is found. It has been moved up or down in
11642 the matrix by dvpos. */
11643 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11644 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11645
11646 /* If last_row is the window end line, it should display text. */
11647 xassert (last_row->displays_text_p);
11648
11649 /* If window end line was partially visible before, begin
11650 displaying at that line. Otherwise begin displaying with the
11651 line following it. */
11652 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11653 {
11654 init_to_row_start (&it, w, last_row);
11655 it.vpos = last_vpos;
11656 it.current_y = last_row->y;
11657 }
11658 else
11659 {
11660 init_to_row_end (&it, w, last_row);
11661 it.vpos = 1 + last_vpos;
11662 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11663 ++last_row;
11664 }
11665
11666 /* We may start in a continuation line. If so, we have to get
11667 the right continuation_lines_width and current_x. */
11668 it.continuation_lines_width = last_row->continuation_lines_width;
11669 it.hpos = it.current_x = 0;
11670
11671 /* Display the rest of the lines at the window end. */
11672 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11673 while (it.current_y < it.last_visible_y
11674 && !fonts_changed_p)
11675 {
11676 /* Is it always sure that the display agrees with lines in
11677 the current matrix? I don't think so, so we mark rows
11678 displayed invalid in the current matrix by setting their
11679 enabled_p flag to zero. */
11680 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11681 if (display_line (&it))
11682 last_text_row_at_end = it.glyph_row - 1;
11683 }
11684 }
11685
11686 /* Update window_end_pos and window_end_vpos. */
11687 if (first_unchanged_at_end_row
11688 && first_unchanged_at_end_row->y < it.last_visible_y
11689 && !last_text_row_at_end)
11690 {
11691 /* Window end line if one of the preserved rows from the current
11692 matrix. Set row to the last row displaying text in current
11693 matrix starting at first_unchanged_at_end_row, after
11694 scrolling. */
11695 xassert (first_unchanged_at_end_row->displays_text_p);
11696 row = find_last_row_displaying_text (w->current_matrix, &it,
11697 first_unchanged_at_end_row);
11698 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11699
11700 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11701 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11702 w->window_end_vpos
11703 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
11704 xassert (w->window_end_bytepos >= 0);
11705 IF_DEBUG (debug_method_add (w, "A"));
11706 }
11707 else if (last_text_row_at_end)
11708 {
11709 w->window_end_pos
11710 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11711 w->window_end_bytepos
11712 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11713 w->window_end_vpos
11714 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11715 xassert (w->window_end_bytepos >= 0);
11716 IF_DEBUG (debug_method_add (w, "B"));
11717 }
11718 else if (last_text_row)
11719 {
11720 /* We have displayed either to the end of the window or at the
11721 end of the window, i.e. the last row with text is to be found
11722 in the desired matrix. */
11723 w->window_end_pos
11724 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11725 w->window_end_bytepos
11726 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11727 w->window_end_vpos
11728 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11729 xassert (w->window_end_bytepos >= 0);
11730 }
11731 else if (first_unchanged_at_end_row == NULL
11732 && last_text_row == NULL
11733 && last_text_row_at_end == NULL)
11734 {
11735 /* Displayed to end of window, but no line containing text was
11736 displayed. Lines were deleted at the end of the window. */
11737 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11738 int vpos = XFASTINT (w->window_end_vpos);
11739 struct glyph_row *current_row = current_matrix->rows + vpos;
11740 struct glyph_row *desired_row = desired_matrix->rows + vpos;
11741
11742 for (row = NULL;
11743 row == NULL && vpos >= first_vpos;
11744 --vpos, --current_row, --desired_row)
11745 {
11746 if (desired_row->enabled_p)
11747 {
11748 if (desired_row->displays_text_p)
11749 row = desired_row;
11750 }
11751 else if (current_row->displays_text_p)
11752 row = current_row;
11753 }
11754
11755 xassert (row != NULL);
11756 w->window_end_vpos = make_number (vpos + 1);
11757 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11758 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11759 xassert (w->window_end_bytepos >= 0);
11760 IF_DEBUG (debug_method_add (w, "C"));
11761 }
11762 else
11763 abort ();
11764
11765 #if 0 /* This leads to problems, for instance when the cursor is
11766 at ZV, and the cursor line displays no text. */
11767 /* Disable rows below what's displayed in the window. This makes
11768 debugging easier. */
11769 enable_glyph_matrix_rows (current_matrix,
11770 XFASTINT (w->window_end_vpos) + 1,
11771 bottom_vpos, 0);
11772 #endif
11773
11774 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11775 debug_end_vpos = XFASTINT (w->window_end_vpos));
11776
11777 /* Record that display has not been completed. */
11778 w->window_end_valid = Qnil;
11779 w->desired_matrix->no_scrolling_p = 1;
11780 return 3;
11781
11782 #undef GIVE_UP
11783 }
11784
11785
11786 \f
11787 /***********************************************************************
11788 More debugging support
11789 ***********************************************************************/
11790
11791 #if GLYPH_DEBUG
11792
11793 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
11794 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11795 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
11796
11797
11798 /* Dump the contents of glyph matrix MATRIX on stderr.
11799
11800 GLYPHS 0 means don't show glyph contents.
11801 GLYPHS 1 means show glyphs in short form
11802 GLYPHS > 1 means show glyphs in long form. */
11803
11804 void
11805 dump_glyph_matrix (matrix, glyphs)
11806 struct glyph_matrix *matrix;
11807 int glyphs;
11808 {
11809 int i;
11810 for (i = 0; i < matrix->nrows; ++i)
11811 dump_glyph_row (matrix, i, glyphs);
11812 }
11813
11814
11815 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
11816 the glyph row and area where the glyph comes from. */
11817
11818 void
11819 dump_glyph (row, glyph, area)
11820 struct glyph_row *row;
11821 struct glyph *glyph;
11822 int area;
11823 {
11824 if (glyph->type == CHAR_GLYPH)
11825 {
11826 fprintf (stderr,
11827 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11828 glyph - row->glyphs[TEXT_AREA],
11829 'C',
11830 glyph->charpos,
11831 (BUFFERP (glyph->object)
11832 ? 'B'
11833 : (STRINGP (glyph->object)
11834 ? 'S'
11835 : '-')),
11836 glyph->pixel_width,
11837 glyph->u.ch,
11838 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11839 ? glyph->u.ch
11840 : '.'),
11841 glyph->face_id,
11842 glyph->left_box_line_p,
11843 glyph->right_box_line_p);
11844 }
11845 else if (glyph->type == STRETCH_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 'S',
11851 glyph->charpos,
11852 (BUFFERP (glyph->object)
11853 ? 'B'
11854 : (STRINGP (glyph->object)
11855 ? 'S'
11856 : '-')),
11857 glyph->pixel_width,
11858 0,
11859 '.',
11860 glyph->face_id,
11861 glyph->left_box_line_p,
11862 glyph->right_box_line_p);
11863 }
11864 else if (glyph->type == IMAGE_GLYPH)
11865 {
11866 fprintf (stderr,
11867 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11868 glyph - row->glyphs[TEXT_AREA],
11869 'I',
11870 glyph->charpos,
11871 (BUFFERP (glyph->object)
11872 ? 'B'
11873 : (STRINGP (glyph->object)
11874 ? 'S'
11875 : '-')),
11876 glyph->pixel_width,
11877 glyph->u.img_id,
11878 '.',
11879 glyph->face_id,
11880 glyph->left_box_line_p,
11881 glyph->right_box_line_p);
11882 }
11883 }
11884
11885
11886 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
11887 GLYPHS 0 means don't show glyph contents.
11888 GLYPHS 1 means show glyphs in short form
11889 GLYPHS > 1 means show glyphs in long form. */
11890
11891 void
11892 dump_glyph_row (matrix, vpos, glyphs)
11893 struct glyph_matrix *matrix;
11894 int vpos, glyphs;
11895 {
11896 struct glyph_row *row;
11897
11898 if (vpos < 0 || vpos >= matrix->nrows)
11899 return;
11900
11901 row = MATRIX_ROW (matrix, vpos);
11902
11903 if (glyphs != 1)
11904 {
11905 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
11906 fprintf (stderr, "=======================================================================\n");
11907
11908 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
11909 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
11910 row - matrix->rows,
11911 MATRIX_ROW_START_CHARPOS (row),
11912 MATRIX_ROW_END_CHARPOS (row),
11913 row->used[TEXT_AREA],
11914 row->contains_overlapping_glyphs_p,
11915 row->enabled_p,
11916 row->inverse_p,
11917 row->truncated_on_left_p,
11918 row->truncated_on_right_p,
11919 row->overlay_arrow_p,
11920 row->continued_p,
11921 MATRIX_ROW_CONTINUATION_LINE_P (row),
11922 row->displays_text_p,
11923 row->ends_at_zv_p,
11924 row->fill_line_p,
11925 row->ends_in_middle_of_char_p,
11926 row->starts_in_middle_of_char_p,
11927 row->mouse_face_p,
11928 row->x,
11929 row->y,
11930 row->pixel_width,
11931 row->height,
11932 row->visible_height,
11933 row->ascent,
11934 row->phys_ascent);
11935 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
11936 row->end.overlay_string_index,
11937 row->continuation_lines_width);
11938 fprintf (stderr, "%9d %5d\n",
11939 CHARPOS (row->start.string_pos),
11940 CHARPOS (row->end.string_pos));
11941 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11942 row->end.dpvec_index);
11943 }
11944
11945 if (glyphs > 1)
11946 {
11947 int area;
11948
11949 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11950 {
11951 struct glyph *glyph = row->glyphs[area];
11952 struct glyph *glyph_end = glyph + row->used[area];
11953
11954 /* Glyph for a line end in text. */
11955 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
11956 ++glyph_end;
11957
11958 if (glyph < glyph_end)
11959 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
11960
11961 for (; glyph < glyph_end; ++glyph)
11962 dump_glyph (row, glyph, area);
11963 }
11964 }
11965 else if (glyphs == 1)
11966 {
11967 int area;
11968
11969 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11970 {
11971 char *s = (char *) alloca (row->used[area] + 1);
11972 int i;
11973
11974 for (i = 0; i < row->used[area]; ++i)
11975 {
11976 struct glyph *glyph = row->glyphs[area] + i;
11977 if (glyph->type == CHAR_GLYPH
11978 && glyph->u.ch < 0x80
11979 && glyph->u.ch >= ' ')
11980 s[i] = glyph->u.ch;
11981 else
11982 s[i] = '.';
11983 }
11984
11985 s[i] = '\0';
11986 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
11987 }
11988 }
11989 }
11990
11991
11992 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11993 Sdump_glyph_matrix, 0, 1, "p",
11994 "Dump the current matrix of the selected window to stderr.\n\
11995 Shows contents of glyph row structures. With non-nil\n\
11996 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show\n\
11997 glyphs in short form, otherwise show glyphs in long form.")
11998 (glyphs)
11999 Lisp_Object glyphs;
12000 {
12001 struct window *w = XWINDOW (selected_window);
12002 struct buffer *buffer = XBUFFER (w->buffer);
12003
12004 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12005 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12006 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12007 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12008 fprintf (stderr, "=============================================\n");
12009 dump_glyph_matrix (w->current_matrix,
12010 NILP (glyphs) ? 0 : XINT (glyphs));
12011 return Qnil;
12012 }
12013
12014
12015 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12016 "Dump glyph row ROW to stderr.\n\
12017 GLYPH 0 means don't dump glyphs.\n\
12018 GLYPH 1 means dump glyphs in short form.\n\
12019 GLYPH > 1 or omitted means dump glyphs in long form.")
12020 (row, glyphs)
12021 Lisp_Object row, glyphs;
12022 {
12023 CHECK_NUMBER (row, 0);
12024 dump_glyph_row (XWINDOW (selected_window)->current_matrix,
12025 XINT (row),
12026 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12027 return Qnil;
12028 }
12029
12030
12031 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12032 "Dump glyph row ROW of the tool-bar of the current frame to stderr.\n\
12033 GLYPH 0 means don't dump glyphs.\n\
12034 GLYPH 1 means dump glyphs in short form.\n\
12035 GLYPH > 1 or omitted means dump glyphs in long form.")
12036 (row, glyphs)
12037 Lisp_Object row, glyphs;
12038 {
12039 struct frame *sf = SELECTED_FRAME ();
12040 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)->current_matrix);
12041 dump_glyph_row (m, XINT (row), INTEGERP (glyphs) ? XINT (glyphs) : 2);
12042 return Qnil;
12043 }
12044
12045
12046 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12047 "Toggle tracing of redisplay.\n\
12048 With ARG, turn tracing on if and only if ARG is positive.")
12049 (arg)
12050 Lisp_Object arg;
12051 {
12052 if (NILP (arg))
12053 trace_redisplay_p = !trace_redisplay_p;
12054 else
12055 {
12056 arg = Fprefix_numeric_value (arg);
12057 trace_redisplay_p = XINT (arg) > 0;
12058 }
12059
12060 return Qnil;
12061 }
12062
12063
12064 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
12065 "Print STRING to stderr.")
12066 (string)
12067 Lisp_Object string;
12068 {
12069 CHECK_STRING (string, 0);
12070 fprintf (stderr, "%s", XSTRING (string)->data);
12071 return Qnil;
12072 }
12073
12074 #endif /* GLYPH_DEBUG */
12075
12076
12077 \f
12078 /***********************************************************************
12079 Building Desired Matrix Rows
12080 ***********************************************************************/
12081
12082 /* Return a temporary glyph row holding the glyphs of an overlay
12083 arrow. Only used for non-window-redisplay windows. */
12084
12085 static struct glyph_row *
12086 get_overlay_arrow_glyph_row (w)
12087 struct window *w;
12088 {
12089 struct frame *f = XFRAME (WINDOW_FRAME (w));
12090 struct buffer *buffer = XBUFFER (w->buffer);
12091 struct buffer *old = current_buffer;
12092 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
12093 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
12094 unsigned char *arrow_end = arrow_string + arrow_len;
12095 unsigned char *p;
12096 struct it it;
12097 int multibyte_p;
12098 int n_glyphs_before;
12099
12100 set_buffer_temp (buffer);
12101 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12102 it.glyph_row->used[TEXT_AREA] = 0;
12103 SET_TEXT_POS (it.position, 0, 0);
12104
12105 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12106 p = arrow_string;
12107 while (p < arrow_end)
12108 {
12109 Lisp_Object face, ilisp;
12110
12111 /* Get the next character. */
12112 if (multibyte_p)
12113 it.c = string_char_and_length (p, arrow_len, &it.len);
12114 else
12115 it.c = *p, it.len = 1;
12116 p += it.len;
12117
12118 /* Get its face. */
12119 ilisp = make_number (p - arrow_string);
12120 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12121 it.face_id = compute_char_face (f, it.c, face);
12122
12123 /* Compute its width, get its glyphs. */
12124 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12125 SET_TEXT_POS (it.position, -1, -1);
12126 PRODUCE_GLYPHS (&it);
12127
12128 /* If this character doesn't fit any more in the line, we have
12129 to remove some glyphs. */
12130 if (it.current_x > it.last_visible_x)
12131 {
12132 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12133 break;
12134 }
12135 }
12136
12137 set_buffer_temp (old);
12138 return it.glyph_row;
12139 }
12140
12141
12142 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12143 glyphs are only inserted for terminal frames since we can't really
12144 win with truncation glyphs when partially visible glyphs are
12145 involved. Which glyphs to insert is determined by
12146 produce_special_glyphs. */
12147
12148 static void
12149 insert_left_trunc_glyphs (it)
12150 struct it *it;
12151 {
12152 struct it truncate_it;
12153 struct glyph *from, *end, *to, *toend;
12154
12155 xassert (!FRAME_WINDOW_P (it->f));
12156
12157 /* Get the truncation glyphs. */
12158 truncate_it = *it;
12159 truncate_it.current_x = 0;
12160 truncate_it.face_id = DEFAULT_FACE_ID;
12161 truncate_it.glyph_row = &scratch_glyph_row;
12162 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12163 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12164 truncate_it.object = make_number (0);
12165 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12166
12167 /* Overwrite glyphs from IT with truncation glyphs. */
12168 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12169 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12170 to = it->glyph_row->glyphs[TEXT_AREA];
12171 toend = to + it->glyph_row->used[TEXT_AREA];
12172
12173 while (from < end)
12174 *to++ = *from++;
12175
12176 /* There may be padding glyphs left over. Overwrite them too. */
12177 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12178 {
12179 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12180 while (from < end)
12181 *to++ = *from++;
12182 }
12183
12184 if (to > toend)
12185 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12186 }
12187
12188
12189 /* Compute the pixel height and width of IT->glyph_row.
12190
12191 Most of the time, ascent and height of a display line will be equal
12192 to the max_ascent and max_height values of the display iterator
12193 structure. This is not the case if
12194
12195 1. We hit ZV without displaying anything. In this case, max_ascent
12196 and max_height will be zero.
12197
12198 2. We have some glyphs that don't contribute to the line height.
12199 (The glyph row flag contributes_to_line_height_p is for future
12200 pixmap extensions).
12201
12202 The first case is easily covered by using default values because in
12203 these cases, the line height does not really matter, except that it
12204 must not be zero. */
12205
12206 static void
12207 compute_line_metrics (it)
12208 struct it *it;
12209 {
12210 struct glyph_row *row = it->glyph_row;
12211 int area, i;
12212
12213 if (FRAME_WINDOW_P (it->f))
12214 {
12215 int i, header_line_height;
12216
12217 /* The line may consist of one space only, that was added to
12218 place the cursor on it. If so, the row's height hasn't been
12219 computed yet. */
12220 if (row->height == 0)
12221 {
12222 if (it->max_ascent + it->max_descent == 0)
12223 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12224 row->ascent = it->max_ascent;
12225 row->height = it->max_ascent + it->max_descent;
12226 row->phys_ascent = it->max_phys_ascent;
12227 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12228 }
12229
12230 /* Compute the width of this line. */
12231 row->pixel_width = row->x;
12232 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12233 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12234
12235 xassert (row->pixel_width >= 0);
12236 xassert (row->ascent >= 0 && row->height > 0);
12237
12238 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12239 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12240
12241 /* If first line's physical ascent is larger than its logical
12242 ascent, use the physical ascent, and make the row taller.
12243 This makes accented characters fully visible. */
12244 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12245 && row->phys_ascent > row->ascent)
12246 {
12247 row->height += row->phys_ascent - row->ascent;
12248 row->ascent = row->phys_ascent;
12249 }
12250
12251 /* Compute how much of the line is visible. */
12252 row->visible_height = row->height;
12253
12254 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12255 if (row->y < header_line_height)
12256 row->visible_height -= header_line_height - row->y;
12257 else
12258 {
12259 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12260 if (row->y + row->height > max_y)
12261 row->visible_height -= row->y + row->height - max_y;
12262 }
12263 }
12264 else
12265 {
12266 row->pixel_width = row->used[TEXT_AREA];
12267 if (row->continued_p)
12268 row->pixel_width -= it->continuation_pixel_width;
12269 else if (row->truncated_on_right_p)
12270 row->pixel_width -= it->truncation_pixel_width;
12271 row->ascent = row->phys_ascent = 0;
12272 row->height = row->phys_height = row->visible_height = 1;
12273 }
12274
12275 /* Compute a hash code for this row. */
12276 row->hash = 0;
12277 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12278 for (i = 0; i < row->used[area]; ++i)
12279 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12280 + row->glyphs[area][i].u.val
12281 + row->glyphs[area][i].face_id
12282 + row->glyphs[area][i].padding_p
12283 + (row->glyphs[area][i].type << 2));
12284
12285 it->max_ascent = it->max_descent = 0;
12286 it->max_phys_ascent = it->max_phys_descent = 0;
12287 }
12288
12289
12290 /* Append one space to the glyph row of iterator IT if doing a
12291 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12292 space have the default face, otherwise let it have the same face as
12293 IT->face_id. Value is non-zero if a space was added.
12294
12295 This function is called to make sure that there is always one glyph
12296 at the end of a glyph row that the cursor can be set on under
12297 window-systems. (If there weren't such a glyph we would not know
12298 how wide and tall a box cursor should be displayed).
12299
12300 At the same time this space let's a nicely handle clearing to the
12301 end of the line if the row ends in italic text. */
12302
12303 static int
12304 append_space (it, default_face_p)
12305 struct it *it;
12306 int default_face_p;
12307 {
12308 if (FRAME_WINDOW_P (it->f))
12309 {
12310 int n = it->glyph_row->used[TEXT_AREA];
12311
12312 if (it->glyph_row->glyphs[TEXT_AREA] + n
12313 < it->glyph_row->glyphs[1 + TEXT_AREA])
12314 {
12315 /* Save some values that must not be changed.
12316 Must save IT->c and IT->len because otherwise
12317 ITERATOR_AT_END_P wouldn't work anymore after
12318 append_space has been called. */
12319 enum display_element_type saved_what = it->what;
12320 int saved_c = it->c, saved_len = it->len;
12321 int saved_x = it->current_x;
12322 int saved_face_id = it->face_id;
12323 struct text_pos saved_pos;
12324 Lisp_Object saved_object;
12325 struct face *face;
12326
12327 saved_object = it->object;
12328 saved_pos = it->position;
12329
12330 it->what = IT_CHARACTER;
12331 bzero (&it->position, sizeof it->position);
12332 it->object = make_number (0);
12333 it->c = ' ';
12334 it->len = 1;
12335
12336 if (default_face_p)
12337 it->face_id = DEFAULT_FACE_ID;
12338 else if (it->face_before_selective_p)
12339 it->face_id = it->saved_face_id;
12340 face = FACE_FROM_ID (it->f, it->face_id);
12341 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12342
12343 PRODUCE_GLYPHS (it);
12344
12345 it->current_x = saved_x;
12346 it->object = saved_object;
12347 it->position = saved_pos;
12348 it->what = saved_what;
12349 it->face_id = saved_face_id;
12350 it->len = saved_len;
12351 it->c = saved_c;
12352 return 1;
12353 }
12354 }
12355
12356 return 0;
12357 }
12358
12359
12360 /* Extend the face of the last glyph in the text area of IT->glyph_row
12361 to the end of the display line. Called from display_line.
12362 If the glyph row is empty, add a space glyph to it so that we
12363 know the face to draw. Set the glyph row flag fill_line_p. */
12364
12365 static void
12366 extend_face_to_end_of_line (it)
12367 struct it *it;
12368 {
12369 struct face *face;
12370 struct frame *f = it->f;
12371
12372 /* If line is already filled, do nothing. */
12373 if (it->current_x >= it->last_visible_x)
12374 return;
12375
12376 /* Face extension extends the background and box of IT->face_id
12377 to the end of the line. If the background equals the background
12378 of the frame, we don't have to do anything. */
12379 if (it->face_before_selective_p)
12380 face = FACE_FROM_ID (it->f, it->saved_face_id);
12381 else
12382 face = FACE_FROM_ID (f, it->face_id);
12383
12384 if (FRAME_WINDOW_P (f)
12385 && face->box == FACE_NO_BOX
12386 && face->background == FRAME_BACKGROUND_PIXEL (f)
12387 && !face->stipple)
12388 return;
12389
12390 /* Set the glyph row flag indicating that the face of the last glyph
12391 in the text area has to be drawn to the end of the text area. */
12392 it->glyph_row->fill_line_p = 1;
12393
12394 /* If current character of IT is not ASCII, make sure we have the
12395 ASCII face. This will be automatically undone the next time
12396 get_next_display_element returns a multibyte character. Note
12397 that the character will always be single byte in unibyte text. */
12398 if (!SINGLE_BYTE_CHAR_P (it->c))
12399 {
12400 it->face_id = FACE_FOR_CHAR (f, face, 0);
12401 }
12402
12403 if (FRAME_WINDOW_P (f))
12404 {
12405 /* If the row is empty, add a space with the current face of IT,
12406 so that we know which face to draw. */
12407 if (it->glyph_row->used[TEXT_AREA] == 0)
12408 {
12409 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12410 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12411 it->glyph_row->used[TEXT_AREA] = 1;
12412 }
12413 }
12414 else
12415 {
12416 /* Save some values that must not be changed. */
12417 int saved_x = it->current_x;
12418 struct text_pos saved_pos;
12419 Lisp_Object saved_object;
12420 enum display_element_type saved_what = it->what;
12421 int saved_face_id = it->face_id;
12422
12423 saved_object = it->object;
12424 saved_pos = it->position;
12425
12426 it->what = IT_CHARACTER;
12427 bzero (&it->position, sizeof it->position);
12428 it->object = make_number (0);
12429 it->c = ' ';
12430 it->len = 1;
12431 it->face_id = face->id;
12432
12433 PRODUCE_GLYPHS (it);
12434
12435 while (it->current_x <= it->last_visible_x)
12436 PRODUCE_GLYPHS (it);
12437
12438 /* Don't count these blanks really. It would let us insert a left
12439 truncation glyph below and make us set the cursor on them, maybe. */
12440 it->current_x = saved_x;
12441 it->object = saved_object;
12442 it->position = saved_pos;
12443 it->what = saved_what;
12444 it->face_id = saved_face_id;
12445 }
12446 }
12447
12448
12449 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12450 trailing whitespace. */
12451
12452 static int
12453 trailing_whitespace_p (charpos)
12454 int charpos;
12455 {
12456 int bytepos = CHAR_TO_BYTE (charpos);
12457 int c = 0;
12458
12459 while (bytepos < ZV_BYTE
12460 && (c = FETCH_CHAR (bytepos),
12461 c == ' ' || c == '\t'))
12462 ++bytepos;
12463
12464 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12465 {
12466 if (bytepos != PT_BYTE)
12467 return 1;
12468 }
12469 return 0;
12470 }
12471
12472
12473 /* Highlight trailing whitespace, if any, in ROW. */
12474
12475 void
12476 highlight_trailing_whitespace (f, row)
12477 struct frame *f;
12478 struct glyph_row *row;
12479 {
12480 int used = row->used[TEXT_AREA];
12481
12482 if (used)
12483 {
12484 struct glyph *start = row->glyphs[TEXT_AREA];
12485 struct glyph *glyph = start + used - 1;
12486
12487 /* Skip over glyphs inserted to display the cursor at the
12488 end of a line, for extending the face of the last glyph
12489 to the end of the line on terminals, and for truncation
12490 and continuation glyphs. */
12491 while (glyph >= start
12492 && glyph->type == CHAR_GLYPH
12493 && INTEGERP (glyph->object))
12494 --glyph;
12495
12496 /* If last glyph is a space or stretch, and it's trailing
12497 whitespace, set the face of all trailing whitespace glyphs in
12498 IT->glyph_row to `trailing-whitespace'. */
12499 if (glyph >= start
12500 && BUFFERP (glyph->object)
12501 && (glyph->type == STRETCH_GLYPH
12502 || (glyph->type == CHAR_GLYPH
12503 && glyph->u.ch == ' '))
12504 && trailing_whitespace_p (glyph->charpos))
12505 {
12506 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12507
12508 while (glyph >= start
12509 && BUFFERP (glyph->object)
12510 && (glyph->type == STRETCH_GLYPH
12511 || (glyph->type == CHAR_GLYPH
12512 && glyph->u.ch == ' ')))
12513 (glyph--)->face_id = face_id;
12514 }
12515 }
12516 }
12517
12518
12519 /* Value is non-zero if glyph row ROW in window W should be
12520 used to hold the cursor. */
12521
12522 static int
12523 cursor_row_p (w, row)
12524 struct window *w;
12525 struct glyph_row *row;
12526 {
12527 int cursor_row_p = 1;
12528
12529 if (PT == MATRIX_ROW_END_CHARPOS (row))
12530 {
12531 /* If the row ends with a newline from a string, we don't want
12532 the cursor there (if the row is continued it doesn't end in a
12533 newline). */
12534 if (CHARPOS (row->end.string_pos) >= 0
12535 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12536 cursor_row_p = row->continued_p;
12537
12538 /* If the row ends at ZV, display the cursor at the end of that
12539 row instead of at the start of the row below. */
12540 else if (row->ends_at_zv_p)
12541 cursor_row_p = 1;
12542 else
12543 cursor_row_p = 0;
12544 }
12545
12546 return cursor_row_p;
12547 }
12548
12549
12550 /* Construct the glyph row IT->glyph_row in the desired matrix of
12551 IT->w from text at the current position of IT. See dispextern.h
12552 for an overview of struct it. Value is non-zero if
12553 IT->glyph_row displays text, as opposed to a line displaying ZV
12554 only. */
12555
12556 static int
12557 display_line (it)
12558 struct it *it;
12559 {
12560 struct glyph_row *row = it->glyph_row;
12561
12562 /* We always start displaying at hpos zero even if hscrolled. */
12563 xassert (it->hpos == 0 && it->current_x == 0);
12564
12565 /* We must not display in a row that's not a text row. */
12566 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12567 < it->w->desired_matrix->nrows);
12568
12569 /* Is IT->w showing the region? */
12570 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12571
12572 /* Clear the result glyph row and enable it. */
12573 prepare_desired_row (row);
12574
12575 row->y = it->current_y;
12576 row->start = it->current;
12577 row->continuation_lines_width = it->continuation_lines_width;
12578 row->displays_text_p = 1;
12579 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12580 it->starts_in_middle_of_char_p = 0;
12581
12582 /* Arrange the overlays nicely for our purposes. Usually, we call
12583 display_line on only one line at a time, in which case this
12584 can't really hurt too much, or we call it on lines which appear
12585 one after another in the buffer, in which case all calls to
12586 recenter_overlay_lists but the first will be pretty cheap. */
12587 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12588
12589 /* Move over display elements that are not visible because we are
12590 hscrolled. This may stop at an x-position < IT->first_visible_x
12591 if the first glyph is partially visible or if we hit a line end. */
12592 if (it->current_x < it->first_visible_x)
12593 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12594 MOVE_TO_POS | MOVE_TO_X);
12595
12596 /* Get the initial row height. This is either the height of the
12597 text hscrolled, if there is any, or zero. */
12598 row->ascent = it->max_ascent;
12599 row->height = it->max_ascent + it->max_descent;
12600 row->phys_ascent = it->max_phys_ascent;
12601 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12602
12603 /* Loop generating characters. The loop is left with IT on the next
12604 character to display. */
12605 while (1)
12606 {
12607 int n_glyphs_before, hpos_before, x_before;
12608 int x, i, nglyphs;
12609 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12610
12611 /* Retrieve the next thing to display. Value is zero if end of
12612 buffer reached. */
12613 if (!get_next_display_element (it))
12614 {
12615 /* Maybe add a space at the end of this line that is used to
12616 display the cursor there under X. Set the charpos of the
12617 first glyph of blank lines not corresponding to any text
12618 to -1. */
12619 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12620 || row->used[TEXT_AREA] == 0)
12621 {
12622 row->glyphs[TEXT_AREA]->charpos = -1;
12623 row->displays_text_p = 0;
12624
12625 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
12626 row->indicate_empty_line_p = 1;
12627 }
12628
12629 it->continuation_lines_width = 0;
12630 row->ends_at_zv_p = 1;
12631 break;
12632 }
12633
12634 /* Now, get the metrics of what we want to display. This also
12635 generates glyphs in `row' (which is IT->glyph_row). */
12636 n_glyphs_before = row->used[TEXT_AREA];
12637 x = it->current_x;
12638
12639 /* Remember the line height so far in case the next element doesn't
12640 fit on the line. */
12641 if (!it->truncate_lines_p)
12642 {
12643 ascent = it->max_ascent;
12644 descent = it->max_descent;
12645 phys_ascent = it->max_phys_ascent;
12646 phys_descent = it->max_phys_descent;
12647 }
12648
12649 PRODUCE_GLYPHS (it);
12650
12651 /* If this display element was in marginal areas, continue with
12652 the next one. */
12653 if (it->area != TEXT_AREA)
12654 {
12655 row->ascent = max (row->ascent, it->max_ascent);
12656 row->height = max (row->height, it->max_ascent + it->max_descent);
12657 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12658 row->phys_height = max (row->phys_height,
12659 it->max_phys_ascent + it->max_phys_descent);
12660 set_iterator_to_next (it, 1);
12661 continue;
12662 }
12663
12664 /* Does the display element fit on the line? If we truncate
12665 lines, we should draw past the right edge of the window. If
12666 we don't truncate, we want to stop so that we can display the
12667 continuation glyph before the right margin. If lines are
12668 continued, there are two possible strategies for characters
12669 resulting in more than 1 glyph (e.g. tabs): Display as many
12670 glyphs as possible in this line and leave the rest for the
12671 continuation line, or display the whole element in the next
12672 line. Original redisplay did the former, so we do it also. */
12673 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12674 hpos_before = it->hpos;
12675 x_before = x;
12676
12677 if (/* Not a newline. */
12678 nglyphs > 0
12679 /* Glyphs produced fit entirely in the line. */
12680 && it->current_x < it->last_visible_x)
12681 {
12682 it->hpos += nglyphs;
12683 row->ascent = max (row->ascent, it->max_ascent);
12684 row->height = max (row->height, it->max_ascent + it->max_descent);
12685 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12686 row->phys_height = max (row->phys_height,
12687 it->max_phys_ascent + it->max_phys_descent);
12688 if (it->current_x - it->pixel_width < it->first_visible_x)
12689 row->x = x - it->first_visible_x;
12690 }
12691 else
12692 {
12693 int new_x;
12694 struct glyph *glyph;
12695
12696 for (i = 0; i < nglyphs; ++i, x = new_x)
12697 {
12698 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12699 new_x = x + glyph->pixel_width;
12700
12701 if (/* Lines are continued. */
12702 !it->truncate_lines_p
12703 && (/* Glyph doesn't fit on the line. */
12704 new_x > it->last_visible_x
12705 /* Or it fits exactly on a window system frame. */
12706 || (new_x == it->last_visible_x
12707 && FRAME_WINDOW_P (it->f))))
12708 {
12709 /* End of a continued line. */
12710
12711 if (it->hpos == 0
12712 || (new_x == it->last_visible_x
12713 && FRAME_WINDOW_P (it->f)))
12714 {
12715 /* Current glyph is the only one on the line or
12716 fits exactly on the line. We must continue
12717 the line because we can't draw the cursor
12718 after the glyph. */
12719 row->continued_p = 1;
12720 it->current_x = new_x;
12721 it->continuation_lines_width += new_x;
12722 ++it->hpos;
12723 if (i == nglyphs - 1)
12724 set_iterator_to_next (it, 1);
12725 }
12726 else if (CHAR_GLYPH_PADDING_P (*glyph)
12727 && !FRAME_WINDOW_P (it->f))
12728 {
12729 /* A padding glyph that doesn't fit on this line.
12730 This means the whole character doesn't fit
12731 on the line. */
12732 row->used[TEXT_AREA] = n_glyphs_before;
12733
12734 /* Fill the rest of the row with continuation
12735 glyphs like in 20.x. */
12736 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12737 < row->glyphs[1 + TEXT_AREA])
12738 produce_special_glyphs (it, IT_CONTINUATION);
12739
12740 row->continued_p = 1;
12741 it->current_x = x_before;
12742 it->continuation_lines_width += x_before;
12743
12744 /* Restore the height to what it was before the
12745 element not fitting on the line. */
12746 it->max_ascent = ascent;
12747 it->max_descent = descent;
12748 it->max_phys_ascent = phys_ascent;
12749 it->max_phys_descent = phys_descent;
12750 }
12751 else
12752 {
12753 /* Display element draws past the right edge of
12754 the window. Restore positions to values
12755 before the element. The next line starts
12756 with current_x before the glyph that could
12757 not be displayed, so that TAB works right. */
12758 row->used[TEXT_AREA] = n_glyphs_before + i;
12759
12760 /* Display continuation glyphs. */
12761 if (!FRAME_WINDOW_P (it->f))
12762 produce_special_glyphs (it, IT_CONTINUATION);
12763 row->continued_p = 1;
12764
12765 it->current_x = x;
12766 it->continuation_lines_width += x;
12767 if (nglyphs > 1 && i > 0)
12768 {
12769 row->ends_in_middle_of_char_p = 1;
12770 it->starts_in_middle_of_char_p = 1;
12771 }
12772
12773 /* Restore the height to what it was before the
12774 element not fitting on the line. */
12775 it->max_ascent = ascent;
12776 it->max_descent = descent;
12777 it->max_phys_ascent = phys_ascent;
12778 it->max_phys_descent = phys_descent;
12779 }
12780
12781 break;
12782 }
12783 else if (new_x > it->first_visible_x)
12784 {
12785 /* Increment number of glyphs actually displayed. */
12786 ++it->hpos;
12787
12788 if (x < it->first_visible_x)
12789 /* Glyph is partially visible, i.e. row starts at
12790 negative X position. */
12791 row->x = x - it->first_visible_x;
12792 }
12793 else
12794 {
12795 /* Glyph is completely off the left margin of the
12796 window. This should not happen because of the
12797 move_it_in_display_line at the start of
12798 this function. */
12799 abort ();
12800 }
12801 }
12802
12803 row->ascent = max (row->ascent, it->max_ascent);
12804 row->height = max (row->height, it->max_ascent + it->max_descent);
12805 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12806 row->phys_height = max (row->phys_height,
12807 it->max_phys_ascent + it->max_phys_descent);
12808
12809 /* End of this display line if row is continued. */
12810 if (row->continued_p)
12811 break;
12812 }
12813
12814 /* Is this a line end? If yes, we're also done, after making
12815 sure that a non-default face is extended up to the right
12816 margin of the window. */
12817 if (ITERATOR_AT_END_OF_LINE_P (it))
12818 {
12819 int used_before = row->used[TEXT_AREA];
12820
12821 /* Add a space at the end of the line that is used to
12822 display the cursor there. */
12823 append_space (it, 0);
12824
12825 /* Extend the face to the end of the line. */
12826 extend_face_to_end_of_line (it);
12827
12828 /* Make sure we have the position. */
12829 if (used_before == 0)
12830 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
12831
12832 /* Consume the line end. This skips over invisible lines. */
12833 set_iterator_to_next (it, 1);
12834 it->continuation_lines_width = 0;
12835 break;
12836 }
12837
12838 /* Proceed with next display element. Note that this skips
12839 over lines invisible because of selective display. */
12840 set_iterator_to_next (it, 1);
12841
12842 /* If we truncate lines, we are done when the last displayed
12843 glyphs reach past the right margin of the window. */
12844 if (it->truncate_lines_p
12845 && (FRAME_WINDOW_P (it->f)
12846 ? (it->current_x >= it->last_visible_x)
12847 : (it->current_x > it->last_visible_x)))
12848 {
12849 /* Maybe add truncation glyphs. */
12850 if (!FRAME_WINDOW_P (it->f))
12851 {
12852 int i, n;
12853
12854 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
12855 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
12856 break;
12857
12858 for (n = row->used[TEXT_AREA]; i < n; ++i)
12859 {
12860 row->used[TEXT_AREA] = i;
12861 produce_special_glyphs (it, IT_TRUNCATION);
12862 }
12863 }
12864
12865 row->truncated_on_right_p = 1;
12866 it->continuation_lines_width = 0;
12867 reseat_at_next_visible_line_start (it, 0);
12868 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12869 it->hpos = hpos_before;
12870 it->current_x = x_before;
12871 break;
12872 }
12873 }
12874
12875 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12876 at the left window margin. */
12877 if (it->first_visible_x
12878 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12879 {
12880 if (!FRAME_WINDOW_P (it->f))
12881 insert_left_trunc_glyphs (it);
12882 row->truncated_on_left_p = 1;
12883 }
12884
12885 /* If the start of this line is the overlay arrow-position, then
12886 mark this glyph row as the one containing the overlay arrow.
12887 This is clearly a mess with variable size fonts. It would be
12888 better to let it be displayed like cursors under X. */
12889 if (MARKERP (Voverlay_arrow_position)
12890 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
12891 && (MATRIX_ROW_START_CHARPOS (row)
12892 == marker_position (Voverlay_arrow_position))
12893 && STRINGP (Voverlay_arrow_string)
12894 && ! overlay_arrow_seen)
12895 {
12896 /* Overlay arrow in window redisplay is a bitmap. */
12897 if (!FRAME_WINDOW_P (it->f))
12898 {
12899 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
12900 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
12901 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
12902 struct glyph *p = row->glyphs[TEXT_AREA];
12903 struct glyph *p2, *end;
12904
12905 /* Copy the arrow glyphs. */
12906 while (glyph < arrow_end)
12907 *p++ = *glyph++;
12908
12909 /* Throw away padding glyphs. */
12910 p2 = p;
12911 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
12912 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
12913 ++p2;
12914 if (p2 > p)
12915 {
12916 while (p2 < end)
12917 *p++ = *p2++;
12918 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
12919 }
12920 }
12921
12922 overlay_arrow_seen = 1;
12923 row->overlay_arrow_p = 1;
12924 }
12925
12926 /* Compute pixel dimensions of this line. */
12927 compute_line_metrics (it);
12928
12929 /* Remember the position at which this line ends. */
12930 row->end = it->current;
12931
12932 /* Maybe set the cursor. */
12933 if (it->w->cursor.vpos < 0
12934 && PT >= MATRIX_ROW_START_CHARPOS (row)
12935 && PT <= MATRIX_ROW_END_CHARPOS (row)
12936 && cursor_row_p (it->w, row))
12937 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
12938
12939 /* Highlight trailing whitespace. */
12940 if (!NILP (Vshow_trailing_whitespace))
12941 highlight_trailing_whitespace (it->f, it->glyph_row);
12942
12943 /* Prepare for the next line. This line starts horizontally at (X
12944 HPOS) = (0 0). Vertical positions are incremented. As a
12945 convenience for the caller, IT->glyph_row is set to the next
12946 row to be used. */
12947 it->current_x = it->hpos = 0;
12948 it->current_y += row->height;
12949 ++it->vpos;
12950 ++it->glyph_row;
12951 return row->displays_text_p;
12952 }
12953
12954
12955 \f
12956 /***********************************************************************
12957 Menu Bar
12958 ***********************************************************************/
12959
12960 /* Redisplay the menu bar in the frame for window W.
12961
12962 The menu bar of X frames that don't have X toolkit support is
12963 displayed in a special window W->frame->menu_bar_window.
12964
12965 The menu bar of terminal frames is treated specially as far as
12966 glyph matrices are concerned. Menu bar lines are not part of
12967 windows, so the update is done directly on the frame matrix rows
12968 for the menu bar. */
12969
12970 static void
12971 display_menu_bar (w)
12972 struct window *w;
12973 {
12974 struct frame *f = XFRAME (WINDOW_FRAME (w));
12975 struct it it;
12976 Lisp_Object items;
12977 int i;
12978
12979 /* Don't do all this for graphical frames. */
12980 #ifdef HAVE_NTGUI
12981 if (!NILP (Vwindow_system))
12982 return;
12983 #endif
12984 #ifdef USE_X_TOOLKIT
12985 if (FRAME_X_P (f))
12986 return;
12987 #endif
12988 #ifdef macintosh
12989 if (FRAME_MAC_P (f))
12990 return;
12991 #endif
12992
12993 #ifdef USE_X_TOOLKIT
12994 xassert (!FRAME_WINDOW_P (f));
12995 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
12996 it.first_visible_x = 0;
12997 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12998 #else /* not USE_X_TOOLKIT */
12999 if (FRAME_WINDOW_P (f))
13000 {
13001 /* Menu bar lines are displayed in the desired matrix of the
13002 dummy window menu_bar_window. */
13003 struct window *menu_w;
13004 xassert (WINDOWP (f->menu_bar_window));
13005 menu_w = XWINDOW (f->menu_bar_window);
13006 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13007 MENU_FACE_ID);
13008 it.first_visible_x = 0;
13009 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13010 }
13011 else
13012 {
13013 /* This is a TTY frame, i.e. character hpos/vpos are used as
13014 pixel x/y. */
13015 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13016 MENU_FACE_ID);
13017 it.first_visible_x = 0;
13018 it.last_visible_x = FRAME_WIDTH (f);
13019 }
13020 #endif /* not USE_X_TOOLKIT */
13021
13022 if (! mode_line_inverse_video)
13023 /* Force the menu-bar to be displayed in the default face. */
13024 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13025
13026 /* Clear all rows of the menu bar. */
13027 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13028 {
13029 struct glyph_row *row = it.glyph_row + i;
13030 clear_glyph_row (row);
13031 row->enabled_p = 1;
13032 row->full_width_p = 1;
13033 }
13034
13035 /* Display all items of the menu bar. */
13036 items = FRAME_MENU_BAR_ITEMS (it.f);
13037 for (i = 0; i < XVECTOR (items)->size; i += 4)
13038 {
13039 Lisp_Object string;
13040
13041 /* Stop at nil string. */
13042 string = AREF (items, i + 1);
13043 if (NILP (string))
13044 break;
13045
13046 /* Remember where item was displayed. */
13047 AREF (items, i + 3) = make_number (it.hpos);
13048
13049 /* Display the item, pad with one space. */
13050 if (it.current_x < it.last_visible_x)
13051 display_string (NULL, string, Qnil, 0, 0, &it,
13052 XSTRING (string)->size + 1, 0, 0, -1);
13053 }
13054
13055 /* Fill out the line with spaces. */
13056 if (it.current_x < it.last_visible_x)
13057 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13058
13059 /* Compute the total height of the lines. */
13060 compute_line_metrics (&it);
13061 }
13062
13063
13064 \f
13065 /***********************************************************************
13066 Mode Line
13067 ***********************************************************************/
13068
13069 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13070 FORCE is non-zero, redisplay mode lines unconditionally.
13071 Otherwise, redisplay only mode lines that are garbaged. Value is
13072 the number of windows whose mode lines were redisplayed. */
13073
13074 static int
13075 redisplay_mode_lines (window, force)
13076 Lisp_Object window;
13077 int force;
13078 {
13079 int nwindows = 0;
13080
13081 while (!NILP (window))
13082 {
13083 struct window *w = XWINDOW (window);
13084
13085 if (WINDOWP (w->hchild))
13086 nwindows += redisplay_mode_lines (w->hchild, force);
13087 else if (WINDOWP (w->vchild))
13088 nwindows += redisplay_mode_lines (w->vchild, force);
13089 else if (force
13090 || FRAME_GARBAGED_P (XFRAME (w->frame))
13091 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13092 {
13093 Lisp_Object old_selected_frame;
13094 struct text_pos lpoint;
13095 struct buffer *old = current_buffer;
13096
13097 /* Set the window's buffer for the mode line display. */
13098 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13099 set_buffer_internal_1 (XBUFFER (w->buffer));
13100
13101 /* Point refers normally to the selected window. For any
13102 other window, set up appropriate value. */
13103 if (!EQ (window, selected_window))
13104 {
13105 struct text_pos pt;
13106
13107 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13108 if (CHARPOS (pt) < BEGV)
13109 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13110 else if (CHARPOS (pt) > (ZV - 1))
13111 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13112 else
13113 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13114 }
13115
13116 /* Temporarily set up the selected frame. */
13117 old_selected_frame = selected_frame;
13118 selected_frame = w->frame;
13119
13120 /* Display mode lines. */
13121 clear_glyph_matrix (w->desired_matrix);
13122 if (display_mode_lines (w))
13123 {
13124 ++nwindows;
13125 w->must_be_updated_p = 1;
13126 }
13127
13128 /* Restore old settings. */
13129 selected_frame = old_selected_frame;
13130 set_buffer_internal_1 (old);
13131 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13132 }
13133
13134 window = w->next;
13135 }
13136
13137 return nwindows;
13138 }
13139
13140
13141 /* Display the mode and/or top line of window W. Value is the number
13142 of mode lines displayed. */
13143
13144 static int
13145 display_mode_lines (w)
13146 struct window *w;
13147 {
13148 int n = 0;
13149
13150 /* These will be set while the mode line specs are processed. */
13151 line_number_displayed = 0;
13152 w->column_number_displayed = Qnil;
13153
13154 if (WINDOW_WANTS_MODELINE_P (w))
13155 {
13156 display_mode_line (w, MODE_LINE_FACE_ID,
13157 current_buffer->mode_line_format);
13158 ++n;
13159 }
13160
13161 if (WINDOW_WANTS_HEADER_LINE_P (w))
13162 {
13163 display_mode_line (w, HEADER_LINE_FACE_ID,
13164 current_buffer->header_line_format);
13165 ++n;
13166 }
13167
13168 return n;
13169 }
13170
13171
13172 /* Display mode or top line of window W. FACE_ID specifies which line
13173 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13174 FORMAT is the mode line format to display. Value is the pixel
13175 height of the mode line displayed. */
13176
13177 static int
13178 display_mode_line (w, face_id, format)
13179 struct window *w;
13180 enum face_id face_id;
13181 Lisp_Object format;
13182 {
13183 struct it it;
13184 struct face *face;
13185
13186 init_iterator (&it, w, -1, -1, NULL, face_id);
13187 prepare_desired_row (it.glyph_row);
13188
13189 if (! mode_line_inverse_video)
13190 /* Force the mode-line to be displayed in the default face. */
13191 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13192
13193 /* Temporarily make frame's keyboard the current kboard so that
13194 kboard-local variables in the mode_line_format will get the right
13195 values. */
13196 push_frame_kboard (it.f);
13197 display_mode_element (&it, 0, 0, 0, format);
13198 pop_frame_kboard ();
13199
13200 /* Fill up with spaces. */
13201 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13202
13203 compute_line_metrics (&it);
13204 it.glyph_row->full_width_p = 1;
13205 it.glyph_row->mode_line_p = 1;
13206 it.glyph_row->inverse_p = 0;
13207 it.glyph_row->continued_p = 0;
13208 it.glyph_row->truncated_on_left_p = 0;
13209 it.glyph_row->truncated_on_right_p = 0;
13210
13211 /* Make a 3D mode-line have a shadow at its right end. */
13212 face = FACE_FROM_ID (it.f, face_id);
13213 extend_face_to_end_of_line (&it);
13214 if (face->box != FACE_NO_BOX)
13215 {
13216 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13217 + it.glyph_row->used[TEXT_AREA] - 1);
13218 last->right_box_line_p = 1;
13219 }
13220
13221 return it.glyph_row->height;
13222 }
13223
13224
13225 /* Contribute ELT to the mode line for window IT->w. How it
13226 translates into text depends on its data type.
13227
13228 IT describes the display environment in which we display, as usual.
13229
13230 DEPTH is the depth in recursion. It is used to prevent
13231 infinite recursion here.
13232
13233 FIELD_WIDTH is the number of characters the display of ELT should
13234 occupy in the mode line, and PRECISION is the maximum number of
13235 characters to display from ELT's representation. See
13236 display_string for details. *
13237
13238 Returns the hpos of the end of the text generated by ELT. */
13239
13240 static int
13241 display_mode_element (it, depth, field_width, precision, elt)
13242 struct it *it;
13243 int depth;
13244 int field_width, precision;
13245 Lisp_Object elt;
13246 {
13247 int n = 0, field, prec;
13248
13249 tail_recurse:
13250 if (depth > 10)
13251 goto invalid;
13252
13253 depth++;
13254
13255 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13256 {
13257 case Lisp_String:
13258 {
13259 /* A string: output it and check for %-constructs within it. */
13260 unsigned char c;
13261 unsigned char *this = XSTRING (elt)->data;
13262 unsigned char *lisp_string = this;
13263
13264 while ((precision <= 0 || n < precision)
13265 && *this
13266 && (frame_title_ptr
13267 || it->current_x < it->last_visible_x))
13268 {
13269 unsigned char *last = this;
13270
13271 /* Advance to end of string or next format specifier. */
13272 while ((c = *this++) != '\0' && c != '%')
13273 ;
13274
13275 if (this - 1 != last)
13276 {
13277 /* Output to end of string or up to '%'. Field width
13278 is length of string. Don't output more than
13279 PRECISION allows us. */
13280 --this;
13281 prec = strwidth (last, this - last);
13282 if (precision > 0 && prec > precision - n)
13283 prec = precision - n;
13284
13285 if (frame_title_ptr)
13286 n += store_frame_title (last, 0, prec);
13287 else
13288 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
13289 it, 0, prec, 0, -1);
13290 }
13291 else /* c == '%' */
13292 {
13293 unsigned char *percent_position = this;
13294
13295 /* Get the specified minimum width. Zero means
13296 don't pad. */
13297 field = 0;
13298 while ((c = *this++) >= '0' && c <= '9')
13299 field = field * 10 + c - '0';
13300
13301 /* Don't pad beyond the total padding allowed. */
13302 if (field_width - n > 0 && field > field_width - n)
13303 field = field_width - n;
13304
13305 /* Note that either PRECISION <= 0 or N < PRECISION. */
13306 prec = precision - n;
13307
13308 if (c == 'M')
13309 n += display_mode_element (it, depth, field, prec,
13310 Vglobal_mode_string);
13311 else if (c != 0)
13312 {
13313 unsigned char *spec
13314 = decode_mode_spec (it->w, c, field, prec);
13315
13316 if (frame_title_ptr)
13317 n += store_frame_title (spec, field, prec);
13318 else
13319 {
13320 int nglyphs_before
13321 = it->glyph_row->used[TEXT_AREA];
13322 int charpos
13323 = percent_position - XSTRING (elt)->data;
13324 int nwritten
13325 = display_string (spec, Qnil, elt, charpos, 0, it,
13326 field, prec, 0, -1);
13327
13328 /* Assign to the glyphs written above the
13329 string where the `%x' came from, position
13330 of the `%'. */
13331 if (nwritten > 0)
13332 {
13333 struct glyph *glyph
13334 = (it->glyph_row->glyphs[TEXT_AREA]
13335 + nglyphs_before);
13336 int i;
13337
13338 for (i = 0; i < nwritten; ++i)
13339 {
13340 glyph[i].object = elt;
13341 glyph[i].charpos = charpos;
13342 }
13343
13344 n += nwritten;
13345 }
13346 }
13347 }
13348 }
13349 }
13350 }
13351 break;
13352
13353 case Lisp_Symbol:
13354 /* A symbol: process the value of the symbol recursively
13355 as if it appeared here directly. Avoid error if symbol void.
13356 Special case: if value of symbol is a string, output the string
13357 literally. */
13358 {
13359 register Lisp_Object tem;
13360 tem = Fboundp (elt);
13361 if (!NILP (tem))
13362 {
13363 tem = Fsymbol_value (elt);
13364 /* If value is a string, output that string literally:
13365 don't check for % within it. */
13366 if (STRINGP (tem))
13367 {
13368 prec = precision - n;
13369 if (frame_title_ptr)
13370 n += store_frame_title (XSTRING (tem)->data, -1, prec);
13371 else
13372 n += display_string (NULL, tem, Qnil, 0, 0, it,
13373 0, prec, 0, -1);
13374 }
13375 else if (!EQ (tem, elt))
13376 {
13377 /* Give up right away for nil or t. */
13378 elt = tem;
13379 goto tail_recurse;
13380 }
13381 }
13382 }
13383 break;
13384
13385 case Lisp_Cons:
13386 {
13387 register Lisp_Object car, tem;
13388
13389 /* A cons cell: three distinct cases.
13390 If first element is a string or a cons, process all the elements
13391 and effectively concatenate them.
13392 If first element is a negative number, truncate displaying cdr to
13393 at most that many characters. If positive, pad (with spaces)
13394 to at least that many characters.
13395 If first element is a symbol, process the cadr or caddr recursively
13396 according to whether the symbol's value is non-nil or nil. */
13397 car = XCAR (elt);
13398 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
13399 {
13400 /* An element of the form (:eval FORM) means evaluate FORM
13401 and use the result as mode line elements. */
13402 struct gcpro gcpro1;
13403 Lisp_Object spec;
13404
13405 spec = safe_eval (XCAR (XCDR (elt)));
13406 GCPRO1 (spec);
13407 n += display_mode_element (it, depth, field_width - n,
13408 precision - n, spec);
13409 UNGCPRO;
13410 }
13411 else if (SYMBOLP (car))
13412 {
13413 tem = Fboundp (car);
13414 elt = XCDR (elt);
13415 if (!CONSP (elt))
13416 goto invalid;
13417 /* elt is now the cdr, and we know it is a cons cell.
13418 Use its car if CAR has a non-nil value. */
13419 if (!NILP (tem))
13420 {
13421 tem = Fsymbol_value (car);
13422 if (!NILP (tem))
13423 {
13424 elt = XCAR (elt);
13425 goto tail_recurse;
13426 }
13427 }
13428 /* Symbol's value is nil (or symbol is unbound)
13429 Get the cddr of the original list
13430 and if possible find the caddr and use that. */
13431 elt = XCDR (elt);
13432 if (NILP (elt))
13433 break;
13434 else if (!CONSP (elt))
13435 goto invalid;
13436 elt = XCAR (elt);
13437 goto tail_recurse;
13438 }
13439 else if (INTEGERP (car))
13440 {
13441 register int lim = XINT (car);
13442 elt = XCDR (elt);
13443 if (lim < 0)
13444 {
13445 /* Negative int means reduce maximum width. */
13446 if (precision <= 0)
13447 precision = -lim;
13448 else
13449 precision = min (precision, -lim);
13450 }
13451 else if (lim > 0)
13452 {
13453 /* Padding specified. Don't let it be more than
13454 current maximum. */
13455 if (precision > 0)
13456 lim = min (precision, lim);
13457
13458 /* If that's more padding than already wanted, queue it.
13459 But don't reduce padding already specified even if
13460 that is beyond the current truncation point. */
13461 field_width = max (lim, field_width);
13462 }
13463 goto tail_recurse;
13464 }
13465 else if (STRINGP (car) || CONSP (car))
13466 {
13467 register int limit = 50;
13468 /* Limit is to protect against circular lists. */
13469 while (CONSP (elt)
13470 && --limit > 0
13471 && (precision <= 0 || n < precision))
13472 {
13473 n += display_mode_element (it, depth, field_width - n,
13474 precision - n, XCAR (elt));
13475 elt = XCDR (elt);
13476 }
13477 }
13478 }
13479 break;
13480
13481 default:
13482 invalid:
13483 if (frame_title_ptr)
13484 n += store_frame_title ("*invalid*", 0, precision - n);
13485 else
13486 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13487 precision - n, 0, 0);
13488 return n;
13489 }
13490
13491 /* Pad to FIELD_WIDTH. */
13492 if (field_width > 0 && n < field_width)
13493 {
13494 if (frame_title_ptr)
13495 n += store_frame_title ("", field_width - n, 0);
13496 else
13497 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13498 0, 0, 0);
13499 }
13500
13501 return n;
13502 }
13503
13504
13505 /* Write a null-terminated, right justified decimal representation of
13506 the positive integer D to BUF using a minimal field width WIDTH. */
13507
13508 static void
13509 pint2str (buf, width, d)
13510 register char *buf;
13511 register int width;
13512 register int d;
13513 {
13514 register char *p = buf;
13515
13516 if (d <= 0)
13517 *p++ = '0';
13518 else
13519 {
13520 while (d > 0)
13521 {
13522 *p++ = d % 10 + '0';
13523 d /= 10;
13524 }
13525 }
13526
13527 for (width -= (int) (p - buf); width > 0; --width)
13528 *p++ = ' ';
13529 *p-- = '\0';
13530 while (p > buf)
13531 {
13532 d = *buf;
13533 *buf++ = *p;
13534 *p-- = d;
13535 }
13536 }
13537
13538 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
13539 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13540 type of CODING_SYSTEM. Return updated pointer into BUF. */
13541
13542 static unsigned char invalid_eol_type[] = "(*invalid*)";
13543
13544 static char *
13545 decode_mode_spec_coding (coding_system, buf, eol_flag)
13546 Lisp_Object coding_system;
13547 register char *buf;
13548 int eol_flag;
13549 {
13550 Lisp_Object val;
13551 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
13552 unsigned char *eol_str;
13553 int eol_str_len;
13554 /* The EOL conversion we are using. */
13555 Lisp_Object eoltype;
13556
13557 val = Fget (coding_system, Qcoding_system);
13558 eoltype = Qnil;
13559
13560 if (!VECTORP (val)) /* Not yet decided. */
13561 {
13562 if (multibyte)
13563 *buf++ = '-';
13564 if (eol_flag)
13565 eoltype = eol_mnemonic_undecided;
13566 /* Don't mention EOL conversion if it isn't decided. */
13567 }
13568 else
13569 {
13570 Lisp_Object eolvalue;
13571
13572 eolvalue = Fget (coding_system, Qeol_type);
13573
13574 if (multibyte)
13575 *buf++ = XFASTINT (AREF (val, 1));
13576
13577 if (eol_flag)
13578 {
13579 /* The EOL conversion that is normal on this system. */
13580
13581 if (NILP (eolvalue)) /* Not yet decided. */
13582 eoltype = eol_mnemonic_undecided;
13583 else if (VECTORP (eolvalue)) /* Not yet decided. */
13584 eoltype = eol_mnemonic_undecided;
13585 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
13586 eoltype = (XFASTINT (eolvalue) == 0
13587 ? eol_mnemonic_unix
13588 : (XFASTINT (eolvalue) == 1
13589 ? eol_mnemonic_dos : eol_mnemonic_mac));
13590 }
13591 }
13592
13593 if (eol_flag)
13594 {
13595 /* Mention the EOL conversion if it is not the usual one. */
13596 if (STRINGP (eoltype))
13597 {
13598 eol_str = XSTRING (eoltype)->data;
13599 eol_str_len = XSTRING (eoltype)->size;
13600 }
13601 else if (INTEGERP (eoltype)
13602 && CHAR_VALID_P (XINT (eoltype), 0))
13603 {
13604 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
13605 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
13606 }
13607 else
13608 {
13609 eol_str = invalid_eol_type;
13610 eol_str_len = sizeof (invalid_eol_type) - 1;
13611 }
13612 bcopy (eol_str, buf, eol_str_len);
13613 buf += eol_str_len;
13614 }
13615
13616 return buf;
13617 }
13618
13619 /* Return a string for the output of a mode line %-spec for window W,
13620 generated by character C. PRECISION >= 0 means don't return a
13621 string longer than that value. FIELD_WIDTH > 0 means pad the
13622 string returned with spaces to that value. */
13623
13624 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
13625
13626 static char *
13627 decode_mode_spec (w, c, field_width, precision)
13628 struct window *w;
13629 register int c;
13630 int field_width, precision;
13631 {
13632 Lisp_Object obj;
13633 struct frame *f = XFRAME (WINDOW_FRAME (w));
13634 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
13635 struct buffer *b = XBUFFER (w->buffer);
13636
13637 obj = Qnil;
13638
13639 switch (c)
13640 {
13641 case '*':
13642 if (!NILP (b->read_only))
13643 return "%";
13644 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13645 return "*";
13646 return "-";
13647
13648 case '+':
13649 /* This differs from %* only for a modified read-only buffer. */
13650 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13651 return "*";
13652 if (!NILP (b->read_only))
13653 return "%";
13654 return "-";
13655
13656 case '&':
13657 /* This differs from %* in ignoring read-only-ness. */
13658 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13659 return "*";
13660 return "-";
13661
13662 case '%':
13663 return "%";
13664
13665 case '[':
13666 {
13667 int i;
13668 char *p;
13669
13670 if (command_loop_level > 5)
13671 return "[[[... ";
13672 p = decode_mode_spec_buf;
13673 for (i = 0; i < command_loop_level; i++)
13674 *p++ = '[';
13675 *p = 0;
13676 return decode_mode_spec_buf;
13677 }
13678
13679 case ']':
13680 {
13681 int i;
13682 char *p;
13683
13684 if (command_loop_level > 5)
13685 return " ...]]]";
13686 p = decode_mode_spec_buf;
13687 for (i = 0; i < command_loop_level; i++)
13688 *p++ = ']';
13689 *p = 0;
13690 return decode_mode_spec_buf;
13691 }
13692
13693 case '-':
13694 {
13695 register int i;
13696
13697 /* Let lots_of_dashes be a string of infinite length. */
13698 if (field_width <= 0
13699 || field_width > sizeof (lots_of_dashes))
13700 {
13701 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
13702 decode_mode_spec_buf[i] = '-';
13703 decode_mode_spec_buf[i] = '\0';
13704 return decode_mode_spec_buf;
13705 }
13706 else
13707 return lots_of_dashes;
13708 }
13709
13710 case 'b':
13711 obj = b->name;
13712 break;
13713
13714 case 'c':
13715 {
13716 int col = current_column ();
13717 w->column_number_displayed = make_number (col);
13718 pint2str (decode_mode_spec_buf, field_width, col);
13719 return decode_mode_spec_buf;
13720 }
13721
13722 case 'F':
13723 /* %F displays the frame name. */
13724 if (!NILP (f->title))
13725 return (char *) XSTRING (f->title)->data;
13726 if (f->explicit_name || ! FRAME_WINDOW_P (f))
13727 return (char *) XSTRING (f->name)->data;
13728 return "Emacs";
13729
13730 case 'f':
13731 obj = b->filename;
13732 break;
13733
13734 case 'l':
13735 {
13736 int startpos = XMARKER (w->start)->charpos;
13737 int startpos_byte = marker_byte_position (w->start);
13738 int line, linepos, linepos_byte, topline;
13739 int nlines, junk;
13740 int height = XFASTINT (w->height);
13741
13742 /* If we decided that this buffer isn't suitable for line numbers,
13743 don't forget that too fast. */
13744 if (EQ (w->base_line_pos, w->buffer))
13745 goto no_value;
13746 /* But do forget it, if the window shows a different buffer now. */
13747 else if (BUFFERP (w->base_line_pos))
13748 w->base_line_pos = Qnil;
13749
13750 /* If the buffer is very big, don't waste time. */
13751 if (INTEGERP (Vline_number_display_limit)
13752 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
13753 {
13754 w->base_line_pos = Qnil;
13755 w->base_line_number = Qnil;
13756 goto no_value;
13757 }
13758
13759 if (!NILP (w->base_line_number)
13760 && !NILP (w->base_line_pos)
13761 && XFASTINT (w->base_line_pos) <= startpos)
13762 {
13763 line = XFASTINT (w->base_line_number);
13764 linepos = XFASTINT (w->base_line_pos);
13765 linepos_byte = buf_charpos_to_bytepos (b, linepos);
13766 }
13767 else
13768 {
13769 line = 1;
13770 linepos = BUF_BEGV (b);
13771 linepos_byte = BUF_BEGV_BYTE (b);
13772 }
13773
13774 /* Count lines from base line to window start position. */
13775 nlines = display_count_lines (linepos, linepos_byte,
13776 startpos_byte,
13777 startpos, &junk);
13778
13779 topline = nlines + line;
13780
13781 /* Determine a new base line, if the old one is too close
13782 or too far away, or if we did not have one.
13783 "Too close" means it's plausible a scroll-down would
13784 go back past it. */
13785 if (startpos == BUF_BEGV (b))
13786 {
13787 w->base_line_number = make_number (topline);
13788 w->base_line_pos = make_number (BUF_BEGV (b));
13789 }
13790 else if (nlines < height + 25 || nlines > height * 3 + 50
13791 || linepos == BUF_BEGV (b))
13792 {
13793 int limit = BUF_BEGV (b);
13794 int limit_byte = BUF_BEGV_BYTE (b);
13795 int position;
13796 int distance = (height * 2 + 30) * line_number_display_limit_width;
13797
13798 if (startpos - distance > limit)
13799 {
13800 limit = startpos - distance;
13801 limit_byte = CHAR_TO_BYTE (limit);
13802 }
13803
13804 nlines = display_count_lines (startpos, startpos_byte,
13805 limit_byte,
13806 - (height * 2 + 30),
13807 &position);
13808 /* If we couldn't find the lines we wanted within
13809 line_number_display_limit_width chars per line,
13810 give up on line numbers for this window. */
13811 if (position == limit_byte && limit == startpos - distance)
13812 {
13813 w->base_line_pos = w->buffer;
13814 w->base_line_number = Qnil;
13815 goto no_value;
13816 }
13817
13818 w->base_line_number = make_number (topline - nlines);
13819 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
13820 }
13821
13822 /* Now count lines from the start pos to point. */
13823 nlines = display_count_lines (startpos, startpos_byte,
13824 PT_BYTE, PT, &junk);
13825
13826 /* Record that we did display the line number. */
13827 line_number_displayed = 1;
13828
13829 /* Make the string to show. */
13830 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
13831 return decode_mode_spec_buf;
13832 no_value:
13833 {
13834 char* p = decode_mode_spec_buf;
13835 int pad = field_width - 2;
13836 while (pad-- > 0)
13837 *p++ = ' ';
13838 *p++ = '?';
13839 *p++ = '?';
13840 *p = '\0';
13841 return decode_mode_spec_buf;
13842 }
13843 }
13844 break;
13845
13846 case 'm':
13847 obj = b->mode_name;
13848 break;
13849
13850 case 'n':
13851 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
13852 return " Narrow";
13853 break;
13854
13855 case 'p':
13856 {
13857 int pos = marker_position (w->start);
13858 int total = BUF_ZV (b) - BUF_BEGV (b);
13859
13860 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
13861 {
13862 if (pos <= BUF_BEGV (b))
13863 return "All";
13864 else
13865 return "Bottom";
13866 }
13867 else if (pos <= BUF_BEGV (b))
13868 return "Top";
13869 else
13870 {
13871 if (total > 1000000)
13872 /* Do it differently for a large value, to avoid overflow. */
13873 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13874 else
13875 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
13876 /* We can't normally display a 3-digit number,
13877 so get us a 2-digit number that is close. */
13878 if (total == 100)
13879 total = 99;
13880 sprintf (decode_mode_spec_buf, "%2d%%", total);
13881 return decode_mode_spec_buf;
13882 }
13883 }
13884
13885 /* Display percentage of size above the bottom of the screen. */
13886 case 'P':
13887 {
13888 int toppos = marker_position (w->start);
13889 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
13890 int total = BUF_ZV (b) - BUF_BEGV (b);
13891
13892 if (botpos >= BUF_ZV (b))
13893 {
13894 if (toppos <= BUF_BEGV (b))
13895 return "All";
13896 else
13897 return "Bottom";
13898 }
13899 else
13900 {
13901 if (total > 1000000)
13902 /* Do it differently for a large value, to avoid overflow. */
13903 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13904 else
13905 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
13906 /* We can't normally display a 3-digit number,
13907 so get us a 2-digit number that is close. */
13908 if (total == 100)
13909 total = 99;
13910 if (toppos <= BUF_BEGV (b))
13911 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
13912 else
13913 sprintf (decode_mode_spec_buf, "%2d%%", total);
13914 return decode_mode_spec_buf;
13915 }
13916 }
13917
13918 case 's':
13919 /* status of process */
13920 obj = Fget_buffer_process (w->buffer);
13921 if (NILP (obj))
13922 return "no process";
13923 #ifdef subprocesses
13924 obj = Fsymbol_name (Fprocess_status (obj));
13925 #endif
13926 break;
13927
13928 case 't': /* indicate TEXT or BINARY */
13929 #ifdef MODE_LINE_BINARY_TEXT
13930 return MODE_LINE_BINARY_TEXT (b);
13931 #else
13932 return "T";
13933 #endif
13934
13935 case 'z':
13936 /* coding-system (not including end-of-line format) */
13937 case 'Z':
13938 /* coding-system (including end-of-line type) */
13939 {
13940 int eol_flag = (c == 'Z');
13941 char *p = decode_mode_spec_buf;
13942
13943 if (! FRAME_WINDOW_P (f))
13944 {
13945 /* No need to mention EOL here--the terminal never needs
13946 to do EOL conversion. */
13947 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
13948 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
13949 }
13950 p = decode_mode_spec_coding (b->buffer_file_coding_system,
13951 p, eol_flag);
13952
13953 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
13954 #ifdef subprocesses
13955 obj = Fget_buffer_process (Fcurrent_buffer ());
13956 if (PROCESSP (obj))
13957 {
13958 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
13959 p, eol_flag);
13960 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
13961 p, eol_flag);
13962 }
13963 #endif /* subprocesses */
13964 #endif /* 0 */
13965 *p = 0;
13966 return decode_mode_spec_buf;
13967 }
13968 }
13969
13970 if (STRINGP (obj))
13971 return (char *) XSTRING (obj)->data;
13972 else
13973 return "";
13974 }
13975
13976
13977 /* Count up to COUNT lines starting from START / START_BYTE.
13978 But don't go beyond LIMIT_BYTE.
13979 Return the number of lines thus found (always nonnegative).
13980
13981 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
13982
13983 static int
13984 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
13985 int start, start_byte, limit_byte, count;
13986 int *byte_pos_ptr;
13987 {
13988 register unsigned char *cursor;
13989 unsigned char *base;
13990
13991 register int ceiling;
13992 register unsigned char *ceiling_addr;
13993 int orig_count = count;
13994
13995 /* If we are not in selective display mode,
13996 check only for newlines. */
13997 int selective_display = (!NILP (current_buffer->selective_display)
13998 && !INTEGERP (current_buffer->selective_display));
13999
14000 if (count > 0)
14001 {
14002 while (start_byte < limit_byte)
14003 {
14004 ceiling = BUFFER_CEILING_OF (start_byte);
14005 ceiling = min (limit_byte - 1, ceiling);
14006 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14007 base = (cursor = BYTE_POS_ADDR (start_byte));
14008 while (1)
14009 {
14010 if (selective_display)
14011 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14012 ;
14013 else
14014 while (*cursor != '\n' && ++cursor != ceiling_addr)
14015 ;
14016
14017 if (cursor != ceiling_addr)
14018 {
14019 if (--count == 0)
14020 {
14021 start_byte += cursor - base + 1;
14022 *byte_pos_ptr = start_byte;
14023 return orig_count;
14024 }
14025 else
14026 if (++cursor == ceiling_addr)
14027 break;
14028 }
14029 else
14030 break;
14031 }
14032 start_byte += cursor - base;
14033 }
14034 }
14035 else
14036 {
14037 while (start_byte > limit_byte)
14038 {
14039 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14040 ceiling = max (limit_byte, ceiling);
14041 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14042 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14043 while (1)
14044 {
14045 if (selective_display)
14046 while (--cursor != ceiling_addr
14047 && *cursor != '\n' && *cursor != 015)
14048 ;
14049 else
14050 while (--cursor != ceiling_addr && *cursor != '\n')
14051 ;
14052
14053 if (cursor != ceiling_addr)
14054 {
14055 if (++count == 0)
14056 {
14057 start_byte += cursor - base + 1;
14058 *byte_pos_ptr = start_byte;
14059 /* When scanning backwards, we should
14060 not count the newline posterior to which we stop. */
14061 return - orig_count - 1;
14062 }
14063 }
14064 else
14065 break;
14066 }
14067 /* Here we add 1 to compensate for the last decrement
14068 of CURSOR, which took it past the valid range. */
14069 start_byte += cursor - base + 1;
14070 }
14071 }
14072
14073 *byte_pos_ptr = limit_byte;
14074
14075 if (count < 0)
14076 return - orig_count + count;
14077 return orig_count - count;
14078
14079 }
14080
14081
14082 \f
14083 /***********************************************************************
14084 Displaying strings
14085 ***********************************************************************/
14086
14087 /* Display a NUL-terminated string, starting with index START.
14088
14089 If STRING is non-null, display that C string. Otherwise, the Lisp
14090 string LISP_STRING is displayed.
14091
14092 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14093 FACE_STRING. Display STRING or LISP_STRING with the face at
14094 FACE_STRING_POS in FACE_STRING:
14095
14096 Display the string in the environment given by IT, but use the
14097 standard display table, temporarily.
14098
14099 FIELD_WIDTH is the minimum number of output glyphs to produce.
14100 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14101 with spaces. If STRING has more characters, more than FIELD_WIDTH
14102 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14103
14104 PRECISION is the maximum number of characters to output from
14105 STRING. PRECISION < 0 means don't truncate the string.
14106
14107 This is roughly equivalent to printf format specifiers:
14108
14109 FIELD_WIDTH PRECISION PRINTF
14110 ----------------------------------------
14111 -1 -1 %s
14112 -1 10 %.10s
14113 10 -1 %10s
14114 20 10 %20.10s
14115
14116 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14117 display them, and < 0 means obey the current buffer's value of
14118 enable_multibyte_characters.
14119
14120 Value is the number of glyphs produced. */
14121
14122 static int
14123 display_string (string, lisp_string, face_string, face_string_pos,
14124 start, it, field_width, precision, max_x, multibyte)
14125 unsigned char *string;
14126 Lisp_Object lisp_string;
14127 Lisp_Object face_string;
14128 int face_string_pos;
14129 int start;
14130 struct it *it;
14131 int field_width, precision, max_x;
14132 int multibyte;
14133 {
14134 int hpos_at_start = it->hpos;
14135 int saved_face_id = it->face_id;
14136 struct glyph_row *row = it->glyph_row;
14137
14138 /* Initialize the iterator IT for iteration over STRING beginning
14139 with index START. */
14140 reseat_to_string (it, string, lisp_string, start,
14141 precision, field_width, multibyte);
14142
14143 /* If displaying STRING, set up the face of the iterator
14144 from LISP_STRING, if that's given. */
14145 if (STRINGP (face_string))
14146 {
14147 int endptr;
14148 struct face *face;
14149
14150 it->face_id
14151 = face_at_string_position (it->w, face_string, face_string_pos,
14152 0, it->region_beg_charpos,
14153 it->region_end_charpos,
14154 &endptr, it->base_face_id, 0);
14155 face = FACE_FROM_ID (it->f, it->face_id);
14156 it->face_box_p = face->box != FACE_NO_BOX;
14157 }
14158
14159 /* Set max_x to the maximum allowed X position. Don't let it go
14160 beyond the right edge of the window. */
14161 if (max_x <= 0)
14162 max_x = it->last_visible_x;
14163 else
14164 max_x = min (max_x, it->last_visible_x);
14165
14166 /* Skip over display elements that are not visible. because IT->w is
14167 hscrolled. */
14168 if (it->current_x < it->first_visible_x)
14169 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14170 MOVE_TO_POS | MOVE_TO_X);
14171
14172 row->ascent = it->max_ascent;
14173 row->height = it->max_ascent + it->max_descent;
14174 row->phys_ascent = it->max_phys_ascent;
14175 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14176
14177 /* This condition is for the case that we are called with current_x
14178 past last_visible_x. */
14179 while (it->current_x < max_x)
14180 {
14181 int x_before, x, n_glyphs_before, i, nglyphs;
14182
14183 /* Get the next display element. */
14184 if (!get_next_display_element (it))
14185 break;
14186
14187 /* Produce glyphs. */
14188 x_before = it->current_x;
14189 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14190 PRODUCE_GLYPHS (it);
14191
14192 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14193 i = 0;
14194 x = x_before;
14195 while (i < nglyphs)
14196 {
14197 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14198
14199 if (!it->truncate_lines_p
14200 && x + glyph->pixel_width > max_x)
14201 {
14202 /* End of continued line or max_x reached. */
14203 if (CHAR_GLYPH_PADDING_P (*glyph))
14204 {
14205 /* A wide character is unbreakable. */
14206 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14207 it->current_x = x_before;
14208 }
14209 else
14210 {
14211 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14212 it->current_x = x;
14213 }
14214 break;
14215 }
14216 else if (x + glyph->pixel_width > it->first_visible_x)
14217 {
14218 /* Glyph is at least partially visible. */
14219 ++it->hpos;
14220 if (x < it->first_visible_x)
14221 it->glyph_row->x = x - it->first_visible_x;
14222 }
14223 else
14224 {
14225 /* Glyph is off the left margin of the display area.
14226 Should not happen. */
14227 abort ();
14228 }
14229
14230 row->ascent = max (row->ascent, it->max_ascent);
14231 row->height = max (row->height, it->max_ascent + it->max_descent);
14232 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14233 row->phys_height = max (row->phys_height,
14234 it->max_phys_ascent + it->max_phys_descent);
14235 x += glyph->pixel_width;
14236 ++i;
14237 }
14238
14239 /* Stop if max_x reached. */
14240 if (i < nglyphs)
14241 break;
14242
14243 /* Stop at line ends. */
14244 if (ITERATOR_AT_END_OF_LINE_P (it))
14245 {
14246 it->continuation_lines_width = 0;
14247 break;
14248 }
14249
14250 set_iterator_to_next (it, 1);
14251
14252 /* Stop if truncating at the right edge. */
14253 if (it->truncate_lines_p
14254 && it->current_x >= it->last_visible_x)
14255 {
14256 /* Add truncation mark, but don't do it if the line is
14257 truncated at a padding space. */
14258 if (IT_CHARPOS (*it) < it->string_nchars)
14259 {
14260 if (!FRAME_WINDOW_P (it->f))
14261 {
14262 int i, n;
14263
14264 if (it->current_x > it->last_visible_x)
14265 {
14266 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14267 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14268 break;
14269 for (n = row->used[TEXT_AREA]; i < n; ++i)
14270 {
14271 row->used[TEXT_AREA] = i;
14272 produce_special_glyphs (it, IT_TRUNCATION);
14273 }
14274 }
14275 produce_special_glyphs (it, IT_TRUNCATION);
14276 }
14277 it->glyph_row->truncated_on_right_p = 1;
14278 }
14279 break;
14280 }
14281 }
14282
14283 /* Maybe insert a truncation at the left. */
14284 if (it->first_visible_x
14285 && IT_CHARPOS (*it) > 0)
14286 {
14287 if (!FRAME_WINDOW_P (it->f))
14288 insert_left_trunc_glyphs (it);
14289 it->glyph_row->truncated_on_left_p = 1;
14290 }
14291
14292 it->face_id = saved_face_id;
14293
14294 /* Value is number of columns displayed. */
14295 return it->hpos - hpos_at_start;
14296 }
14297
14298
14299 \f
14300 /* This is like a combination of memq and assq. Return 1 if PROPVAL
14301 appears as an element of LIST or as the car of an element of LIST.
14302 If PROPVAL is a list, compare each element against LIST in that
14303 way, and return 1 if any element of PROPVAL is found in LIST.
14304 Otherwise return 0. This function cannot quit. */
14305
14306 int
14307 invisible_p (propval, list)
14308 register Lisp_Object propval;
14309 Lisp_Object list;
14310 {
14311 register Lisp_Object tail, proptail;
14312
14313 for (tail = list; CONSP (tail); tail = XCDR (tail))
14314 {
14315 register Lisp_Object tem;
14316 tem = XCAR (tail);
14317 if (EQ (propval, tem))
14318 return 1;
14319 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14320 return 1;
14321 }
14322
14323 if (CONSP (propval))
14324 {
14325 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14326 {
14327 Lisp_Object propelt;
14328 propelt = XCAR (proptail);
14329 for (tail = list; CONSP (tail); tail = XCDR (tail))
14330 {
14331 register Lisp_Object tem;
14332 tem = XCAR (tail);
14333 if (EQ (propelt, tem))
14334 return 1;
14335 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14336 return 1;
14337 }
14338 }
14339 }
14340
14341 return 0;
14342 }
14343
14344
14345 /* Return 1 if PROPVAL appears as the car of an element of LIST and
14346 the cdr of that element is non-nil. If PROPVAL is a list, check
14347 each element of PROPVAL in that way, and the first time some
14348 element is found, return 1 if the cdr of that element is non-nil.
14349 Otherwise return 0. This function cannot quit. */
14350
14351 int
14352 invisible_ellipsis_p (propval, list)
14353 register Lisp_Object propval;
14354 Lisp_Object list;
14355 {
14356 register Lisp_Object tail, proptail;
14357
14358 for (tail = list; CONSP (tail); tail = XCDR (tail))
14359 {
14360 register Lisp_Object tem;
14361 tem = XCAR (tail);
14362 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14363 return ! NILP (XCDR (tem));
14364 }
14365
14366 if (CONSP (propval))
14367 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14368 {
14369 Lisp_Object propelt;
14370 propelt = XCAR (proptail);
14371 for (tail = list; CONSP (tail); tail = XCDR (tail))
14372 {
14373 register Lisp_Object tem;
14374 tem = XCAR (tail);
14375 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14376 return ! NILP (XCDR (tem));
14377 }
14378 }
14379
14380 return 0;
14381 }
14382
14383
14384 \f
14385 /***********************************************************************
14386 Initialization
14387 ***********************************************************************/
14388
14389 void
14390 syms_of_xdisp ()
14391 {
14392 Vwith_echo_area_save_vector = Qnil;
14393 staticpro (&Vwith_echo_area_save_vector);
14394
14395 Vmessage_stack = Qnil;
14396 staticpro (&Vmessage_stack);
14397
14398 Qinhibit_redisplay = intern ("inhibit-redisplay");
14399 staticpro (&Qinhibit_redisplay);
14400
14401 #if GLYPH_DEBUG
14402 defsubr (&Sdump_glyph_matrix);
14403 defsubr (&Sdump_glyph_row);
14404 defsubr (&Sdump_tool_bar_row);
14405 defsubr (&Strace_redisplay);
14406 defsubr (&Strace_to_stderr);
14407 #endif
14408 #ifdef HAVE_WINDOW_SYSTEM
14409 defsubr (&Stool_bar_lines_needed);
14410 #endif
14411
14412 staticpro (&Qmenu_bar_update_hook);
14413 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14414
14415 staticpro (&Qoverriding_terminal_local_map);
14416 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14417
14418 staticpro (&Qoverriding_local_map);
14419 Qoverriding_local_map = intern ("overriding-local-map");
14420
14421 staticpro (&Qwindow_scroll_functions);
14422 Qwindow_scroll_functions = intern ("window-scroll-functions");
14423
14424 staticpro (&Qredisplay_end_trigger_functions);
14425 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14426
14427 staticpro (&Qinhibit_point_motion_hooks);
14428 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14429
14430 QCdata = intern (":data");
14431 staticpro (&QCdata);
14432 Qdisplay = intern ("display");
14433 staticpro (&Qdisplay);
14434 Qspace_width = intern ("space-width");
14435 staticpro (&Qspace_width);
14436 Qraise = intern ("raise");
14437 staticpro (&Qraise);
14438 Qspace = intern ("space");
14439 staticpro (&Qspace);
14440 Qmargin = intern ("margin");
14441 staticpro (&Qmargin);
14442 Qleft_margin = intern ("left-margin");
14443 staticpro (&Qleft_margin);
14444 Qright_margin = intern ("right-margin");
14445 staticpro (&Qright_margin);
14446 Qalign_to = intern ("align-to");
14447 staticpro (&Qalign_to);
14448 QCalign_to = intern (":align-to");
14449 staticpro (&QCalign_to);
14450 Qrelative_width = intern ("relative-width");
14451 staticpro (&Qrelative_width);
14452 QCrelative_width = intern (":relative-width");
14453 staticpro (&QCrelative_width);
14454 QCrelative_height = intern (":relative-height");
14455 staticpro (&QCrelative_height);
14456 QCeval = intern (":eval");
14457 staticpro (&QCeval);
14458 Qwhen = intern ("when");
14459 staticpro (&Qwhen);
14460 QCfile = intern (":file");
14461 staticpro (&QCfile);
14462 Qfontified = intern ("fontified");
14463 staticpro (&Qfontified);
14464 Qfontification_functions = intern ("fontification-functions");
14465 staticpro (&Qfontification_functions);
14466 Qtrailing_whitespace = intern ("trailing-whitespace");
14467 staticpro (&Qtrailing_whitespace);
14468 Qimage = intern ("image");
14469 staticpro (&Qimage);
14470 Qmessage_truncate_lines = intern ("message-truncate-lines");
14471 staticpro (&Qmessage_truncate_lines);
14472 Qgrow_only = intern ("grow-only");
14473 staticpro (&Qgrow_only);
14474 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14475 staticpro (&Qinhibit_menubar_update);
14476 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14477 staticpro (&Qinhibit_eval_during_redisplay);
14478
14479 last_arrow_position = Qnil;
14480 last_arrow_string = Qnil;
14481 staticpro (&last_arrow_position);
14482 staticpro (&last_arrow_string);
14483
14484 echo_buffer[0] = echo_buffer[1] = Qnil;
14485 staticpro (&echo_buffer[0]);
14486 staticpro (&echo_buffer[1]);
14487
14488 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14489 staticpro (&echo_area_buffer[0]);
14490 staticpro (&echo_area_buffer[1]);
14491
14492 Vmessages_buffer_name = build_string ("*Messages*");
14493 staticpro (&Vmessages_buffer_name);
14494
14495 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14496 "Non-nil means highlight trailing whitespace.\n\
14497 The face used for trailing whitespace is `trailing-whitespace'.");
14498 Vshow_trailing_whitespace = Qnil;
14499
14500 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14501 "Non-nil means don't actually do any redisplay.\n\
14502 This is used for internal purposes.");
14503 Vinhibit_redisplay = Qnil;
14504
14505 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14506 "String (or mode line construct) included (normally) in `mode-line-format'.");
14507 Vglobal_mode_string = Qnil;
14508
14509 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14510 "Marker for where to display an arrow on top of the buffer text.\n\
14511 This must be the beginning of a line in order to work.\n\
14512 See also `overlay-arrow-string'.");
14513 Voverlay_arrow_position = Qnil;
14514
14515 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14516 "String to display as an arrow. See also `overlay-arrow-position'.");
14517 Voverlay_arrow_string = Qnil;
14518
14519 DEFVAR_INT ("scroll-step", &scroll_step,
14520 "*The number of lines to try scrolling a window by when point moves out.\n\
14521 If that fails to bring point back on frame, point is centered instead.\n\
14522 If this is zero, point is always centered after it moves off frame.\n\
14523 If you want scrolling to always be a line at a time, you should set\n\
14524 `scroll-conservatively' to a large value rather than set this to 1.");
14525
14526 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14527 "*Scroll up to this many lines, to bring point back on screen.\n\
14528 A value of zero means to scroll the text to center point vertically\n\
14529 in the window.");
14530 scroll_conservatively = 0;
14531
14532 DEFVAR_INT ("scroll-margin", &scroll_margin,
14533 "*Number of lines of margin at the top and bottom of a window.\n\
14534 Recenter the window whenever point gets within this many lines\n\
14535 of the top or bottom of the window.");
14536 scroll_margin = 0;
14537
14538 #if GLYPH_DEBUG
14539 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
14540 #endif
14541
14542 DEFVAR_BOOL ("truncate-partial-width-windows",
14543 &truncate_partial_width_windows,
14544 "*Non-nil means truncate lines in all windows less than full frame wide.");
14545 truncate_partial_width_windows = 1;
14546
14547 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14548 "nil means display the mode-line/header-line/menu-bar in the default face.\n\
14549 Any other value means to use the appropriate face, `mode-line',\n\
14550 `header-line', or `menu' respectively.\n\
14551 \n\
14552 This variable is deprecated; please change the above faces instead.");
14553 mode_line_inverse_video = 1;
14554
14555 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14556 "*Maximum buffer size for which line number should be displayed.\n\
14557 If the buffer is bigger than this, the line number does not appear\n\
14558 in the mode line. A value of nil means no limit.");
14559 Vline_number_display_limit = Qnil;
14560
14561 DEFVAR_INT ("line-number-display-limit-width",
14562 &line_number_display_limit_width,
14563 "*Maximum line width (in characters) for line number display.\n\
14564 If the average length of the lines near point is bigger than this, then the\n\
14565 line number may be omitted from the mode line.");
14566 line_number_display_limit_width = 200;
14567
14568 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14569 "*Non-nil means highlight region even in nonselected windows.");
14570 highlight_nonselected_windows = 0;
14571
14572 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14573 "Non-nil if more than one frame is visible on this display.\n\
14574 Minibuffer-only frames don't count, but iconified frames do.\n\
14575 This variable is not guaranteed to be accurate except while processing\n\
14576 `frame-title-format' and `icon-title-format'.");
14577
14578 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14579 "Template for displaying the title bar of visible frames.\n\
14580 \(Assuming the window manager supports this feature.)\n\
14581 This variable has the same structure as `mode-line-format' (which see),\n\
14582 and is used only on frames for which no explicit name has been set\n\
14583 \(see `modify-frame-parameters').");
14584 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
14585 "Template for displaying the title bar of an iconified frame.\n\
14586 \(Assuming the window manager supports this feature.)\n\
14587 This variable has the same structure as `mode-line-format' (which see),\n\
14588 and is used only on frames for which no explicit name has been set\n\
14589 \(see `modify-frame-parameters').");
14590 Vicon_title_format
14591 = Vframe_title_format
14592 = Fcons (intern ("multiple-frames"),
14593 Fcons (build_string ("%b"),
14594 Fcons (Fcons (build_string (""),
14595 Fcons (intern ("invocation-name"),
14596 Fcons (build_string ("@"),
14597 Fcons (intern ("system-name"),
14598 Qnil)))),
14599 Qnil)));
14600
14601 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
14602 "Maximum number of lines to keep in the message log buffer.\n\
14603 If nil, disable message logging. If t, log messages but don't truncate\n\
14604 the buffer when it becomes large.");
14605 Vmessage_log_max = make_number (50);
14606
14607 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
14608 "Functions called before redisplay, if window sizes have changed.\n\
14609 The value should be a list of functions that take one argument.\n\
14610 Just before redisplay, for each frame, if any of its windows have changed\n\
14611 size since the last redisplay, or have been split or deleted,\n\
14612 all the functions in the list are called, with the frame as argument.");
14613 Vwindow_size_change_functions = Qnil;
14614
14615 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
14616 "List of Functions to call before redisplaying a window with scrolling.\n\
14617 Each function is called with two arguments, the window\n\
14618 and its new display-start position. Note that the value of `window-end'\n\
14619 is not valid when these functions are called.");
14620 Vwindow_scroll_functions = Qnil;
14621
14622 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
14623 "*Non-nil means automatically resize tool-bars.\n\
14624 This increases a tool-bar's height if not all tool-bar items are visible.\n\
14625 It decreases a tool-bar's height when it would display blank lines\n\
14626 otherwise.");
14627 auto_resize_tool_bars_p = 1;
14628
14629 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
14630 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
14631 auto_raise_tool_bar_buttons_p = 1;
14632
14633 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
14634 "*Margin around tool-bar buttons in pixels.\n\
14635 If an integer, use that for both horizontal and vertical margins.\n\
14636 Otherwise, value should be a pair of integers `(HORZ : VERT)' with\n\
14637 HORZ specifying the horizontal margin, and VERT specifying the\n\
14638 vertical margin.");
14639 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
14640
14641 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
14642 "Relief thickness of tool-bar buttons.");
14643 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
14644
14645 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
14646 "List of functions to call to fontify regions of text.\n\
14647 Each function is called with one argument POS. Functions must\n\
14648 fontify a region starting at POS in the current buffer, and give\n\
14649 fontified regions the property `fontified'.\n\
14650 This variable automatically becomes buffer-local when set.");
14651 Vfontification_functions = Qnil;
14652 Fmake_variable_buffer_local (Qfontification_functions);
14653
14654 DEFVAR_BOOL ("unibyte-display-via-language-environment",
14655 &unibyte_display_via_language_environment,
14656 "*Non-nil means display unibyte text according to language environment.\n\
14657 Specifically this means that unibyte non-ASCII characters\n\
14658 are displayed by converting them to the equivalent multibyte characters\n\
14659 according to the current language environment. As a result, they are\n\
14660 displayed according to the current fontset.");
14661 unibyte_display_via_language_environment = 0;
14662
14663 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
14664 "*Maximum height for resizing mini-windows.\n\
14665 If a float, it specifies a fraction of the mini-window frame's height.\n\
14666 If an integer, it specifies a number of lines.");
14667 Vmax_mini_window_height = make_float (0.25);
14668
14669 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
14670 "*How to resize mini-windows.\n\
14671 A value of nil means don't automatically resize mini-windows.\n\
14672 A value of t means resize them to fit the text displayed in them.\n\
14673 A value of `grow-only', the default, means let mini-windows grow\n\
14674 only, until their display becomes empty, at which point the windows\n\
14675 go back to their normal size.");
14676 Vresize_mini_windows = Qgrow_only;
14677
14678 DEFVAR_BOOL ("cursor-in-non-selected-windows",
14679 &cursor_in_non_selected_windows,
14680 "*Non-nil means display a hollow cursor in non-selected windows.\n\
14681 Nil means don't display a cursor there.");
14682 cursor_in_non_selected_windows = 1;
14683
14684 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
14685 "*Non-nil means scroll the display automatically to make point visible.");
14686 automatic_hscrolling_p = 1;
14687
14688 DEFVAR_LISP ("image-types", &Vimage_types,
14689 "List of supported image types.\n\
14690 Each element of the list is a symbol for a supported image type.");
14691 Vimage_types = Qnil;
14692
14693 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
14694 "If non-nil, messages are truncated instead of resizing the echo area.\n\
14695 Bind this around calls to `message' to let it take effect.");
14696 message_truncate_lines = 0;
14697
14698 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
14699 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
14700 Can be used to update submenus whose contents should vary.");
14701 Vmenu_bar_update_hook = Qnil;
14702
14703 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
14704 "Non-nil means don't update menu bars. Internal use only.");
14705 inhibit_menubar_update = 0;
14706
14707 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
14708 "Non-nil means don't eval Lisp during redisplay.");
14709 inhibit_eval_during_redisplay = 0;
14710 }
14711
14712
14713 /* Initialize this module when Emacs starts. */
14714
14715 void
14716 init_xdisp ()
14717 {
14718 Lisp_Object root_window;
14719 struct window *mini_w;
14720
14721 current_header_line_height = current_mode_line_height = -1;
14722
14723 CHARPOS (this_line_start_pos) = 0;
14724
14725 mini_w = XWINDOW (minibuf_window);
14726 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
14727
14728 if (!noninteractive)
14729 {
14730 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
14731 int i;
14732
14733 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
14734 set_window_height (root_window,
14735 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
14736 0);
14737 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
14738 set_window_height (minibuf_window, 1, 0);
14739
14740 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
14741 mini_w->width = make_number (FRAME_WIDTH (f));
14742
14743 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
14744 scratch_glyph_row.glyphs[TEXT_AREA + 1]
14745 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
14746
14747 /* The default ellipsis glyphs `...'. */
14748 for (i = 0; i < 3; ++i)
14749 default_invis_vector[i] = make_number ('.');
14750 }
14751
14752 #ifdef HAVE_WINDOW_SYSTEM
14753 {
14754 /* Allocate the buffer for frame titles. */
14755 int size = 100;
14756 frame_title_buf = (char *) xmalloc (size);
14757 frame_title_buf_end = frame_title_buf + size;
14758 frame_title_ptr = NULL;
14759 }
14760 #endif /* HAVE_WINDOW_SYSTEM */
14761
14762 help_echo_showing_p = 0;
14763 }
14764
14765