Use new string macros.
[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, 2002
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 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 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 displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
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 INFINITY 10000000
202
203 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
204 extern void set_frame_menubar P_ ((struct frame *f, int, int));
205 extern int pending_menu_activation;
206 #endif
207
208 extern int interrupt_input;
209 extern int command_loop_level;
210
211 extern int minibuffer_auto_raise;
212
213 extern Lisp_Object Qface;
214
215 extern Lisp_Object Voverriding_local_map;
216 extern Lisp_Object Voverriding_local_map_menu_flag;
217 extern Lisp_Object Qmenu_item;
218
219 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
220 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
221 Lisp_Object Qredisplay_end_trigger_functions;
222 Lisp_Object Qinhibit_point_motion_hooks;
223 Lisp_Object QCeval, Qwhen, QCfile, QCdata, QCpropertize;
224 Lisp_Object Qfontified;
225 Lisp_Object Qgrow_only;
226 Lisp_Object Qinhibit_eval_during_redisplay;
227 Lisp_Object Qbuffer_position, Qposition, Qobject;
228
229 /* Functions called to fontify regions of text. */
230
231 Lisp_Object Vfontification_functions;
232 Lisp_Object Qfontification_functions;
233
234 /* Non-zero means draw tool bar buttons raised when the mouse moves
235 over them. */
236
237 int auto_raise_tool_bar_buttons_p;
238
239 /* Margin around tool bar buttons in pixels. */
240
241 Lisp_Object Vtool_bar_button_margin;
242
243 /* Thickness of shadow to draw around tool bar buttons. */
244
245 EMACS_INT tool_bar_button_relief;
246
247 /* Non-zero means automatically resize tool-bars so that all tool-bar
248 items are visible, and no blank lines remain. */
249
250 int auto_resize_tool_bars_p;
251
252 /* Non-nil means don't actually do any redisplay. */
253
254 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
255
256 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
257
258 int inhibit_eval_during_redisplay;
259
260 /* Names of text properties relevant for redisplay. */
261
262 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
263 extern Lisp_Object Qface, Qinvisible, Qwidth;
264
265 /* Symbols used in text property values. */
266
267 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
268 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
269 Lisp_Object Qmargin;
270 extern Lisp_Object Qheight;
271
272 /* Non-nil means highlight trailing whitespace. */
273
274 Lisp_Object Vshow_trailing_whitespace;
275
276 /* Name of the face used to highlight trailing whitespace. */
277
278 Lisp_Object Qtrailing_whitespace;
279
280 /* The symbol `image' which is the car of the lists used to represent
281 images in Lisp. */
282
283 Lisp_Object Qimage;
284
285 /* Non-zero means print newline to stdout before next mini-buffer
286 message. */
287
288 int noninteractive_need_newline;
289
290 /* Non-zero means print newline to message log before next message. */
291
292 static int message_log_need_newline;
293
294 /* Three markers that message_dolog uses.
295 It could allocate them itself, but that causes trouble
296 in handling memory-full errors. */
297 static Lisp_Object message_dolog_marker1;
298 static Lisp_Object message_dolog_marker2;
299 static Lisp_Object message_dolog_marker3;
300 \f
301 /* The buffer position of the first character appearing entirely or
302 partially on the line of the selected window which contains the
303 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
304 redisplay optimization in redisplay_internal. */
305
306 static struct text_pos this_line_start_pos;
307
308 /* Number of characters past the end of the line above, including the
309 terminating newline. */
310
311 static struct text_pos this_line_end_pos;
312
313 /* The vertical positions and the height of this line. */
314
315 static int this_line_vpos;
316 static int this_line_y;
317 static int this_line_pixel_height;
318
319 /* X position at which this display line starts. Usually zero;
320 negative if first character is partially visible. */
321
322 static int this_line_start_x;
323
324 /* Buffer that this_line_.* variables are referring to. */
325
326 static struct buffer *this_line_buffer;
327
328 /* Nonzero means truncate lines in all windows less wide than the
329 frame. */
330
331 int truncate_partial_width_windows;
332
333 /* A flag to control how to display unibyte 8-bit character. */
334
335 int unibyte_display_via_language_environment;
336
337 /* Nonzero means we have more than one non-mini-buffer-only frame.
338 Not guaranteed to be accurate except while parsing
339 frame-title-format. */
340
341 int multiple_frames;
342
343 Lisp_Object Vglobal_mode_string;
344
345 /* Marker for where to display an arrow on top of the buffer text. */
346
347 Lisp_Object Voverlay_arrow_position;
348
349 /* String to display for the arrow. Only used on terminal frames. */
350
351 Lisp_Object Voverlay_arrow_string;
352
353 /* Values of those variables at last redisplay. However, if
354 Voverlay_arrow_position is a marker, last_arrow_position is its
355 numerical position. */
356
357 static Lisp_Object last_arrow_position, last_arrow_string;
358
359 /* Like mode-line-format, but for the title bar on a visible frame. */
360
361 Lisp_Object Vframe_title_format;
362
363 /* Like mode-line-format, but for the title bar on an iconified frame. */
364
365 Lisp_Object Vicon_title_format;
366
367 /* List of functions to call when a window's size changes. These
368 functions get one arg, a frame on which one or more windows' sizes
369 have changed. */
370
371 static Lisp_Object Vwindow_size_change_functions;
372
373 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
374
375 /* Nonzero if overlay arrow has been displayed once in this window. */
376
377 static int overlay_arrow_seen;
378
379 /* Nonzero means highlight the region even in nonselected windows. */
380
381 int highlight_nonselected_windows;
382
383 /* If cursor motion alone moves point off frame, try scrolling this
384 many lines up or down if that will bring it back. */
385
386 static EMACS_INT scroll_step;
387
388 /* Nonzero means scroll just far enough to bring point back on the
389 screen, when appropriate. */
390
391 static EMACS_INT scroll_conservatively;
392
393 /* Recenter the window whenever point gets within this many lines of
394 the top or bottom of the window. This value is translated into a
395 pixel value by multiplying it with CANON_Y_UNIT, which means that
396 there is really a fixed pixel height scroll margin. */
397
398 EMACS_INT scroll_margin;
399
400 /* Number of windows showing the buffer of the selected window (or
401 another buffer with the same base buffer). keyboard.c refers to
402 this. */
403
404 int buffer_shared;
405
406 /* Vector containing glyphs for an ellipsis `...'. */
407
408 static Lisp_Object default_invis_vector[3];
409
410 /* Zero means display the mode-line/header-line/menu-bar in the default face
411 (this slightly odd definition is for compatibility with previous versions
412 of emacs), non-zero means display them using their respective faces.
413
414 This variable is deprecated. */
415
416 int mode_line_inverse_video;
417
418 /* Prompt to display in front of the mini-buffer contents. */
419
420 Lisp_Object minibuf_prompt;
421
422 /* Width of current mini-buffer prompt. Only set after display_line
423 of the line that contains the prompt. */
424
425 int minibuf_prompt_width;
426 int minibuf_prompt_pixel_width;
427
428 /* This is the window where the echo area message was displayed. It
429 is always a mini-buffer window, but it may not be the same window
430 currently active as a mini-buffer. */
431
432 Lisp_Object echo_area_window;
433
434 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
435 pushes the current message and the value of
436 message_enable_multibyte on the stack, the function restore_message
437 pops the stack and displays MESSAGE again. */
438
439 Lisp_Object Vmessage_stack;
440
441 /* Nonzero means multibyte characters were enabled when the echo area
442 message was specified. */
443
444 int message_enable_multibyte;
445
446 /* Nonzero if we should redraw the mode lines on the next redisplay. */
447
448 int update_mode_lines;
449
450 /* Nonzero if window sizes or contents have changed since last
451 redisplay that finished. */
452
453 int windows_or_buffers_changed;
454
455 /* Nonzero after display_mode_line if %l was used and it displayed a
456 line number. */
457
458 int line_number_displayed;
459
460 /* Maximum buffer size for which to display line numbers. */
461
462 Lisp_Object Vline_number_display_limit;
463
464 /* Line width to consider when repositioning for line number display. */
465
466 static EMACS_INT line_number_display_limit_width;
467
468 /* Number of lines to keep in the message log buffer. t means
469 infinite. nil means don't log at all. */
470
471 Lisp_Object Vmessage_log_max;
472
473 /* The name of the *Messages* buffer, a string. */
474
475 static Lisp_Object Vmessages_buffer_name;
476
477 /* Current, index 0, and last displayed echo area message. Either
478 buffers from echo_buffers, or nil to indicate no message. */
479
480 Lisp_Object echo_area_buffer[2];
481
482 /* The buffers referenced from echo_area_buffer. */
483
484 static Lisp_Object echo_buffer[2];
485
486 /* A vector saved used in with_area_buffer to reduce consing. */
487
488 static Lisp_Object Vwith_echo_area_save_vector;
489
490 /* Non-zero means display_echo_area should display the last echo area
491 message again. Set by redisplay_preserve_echo_area. */
492
493 static int display_last_displayed_message_p;
494
495 /* Nonzero if echo area is being used by print; zero if being used by
496 message. */
497
498 int message_buf_print;
499
500 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
501
502 Lisp_Object Qinhibit_menubar_update;
503 int inhibit_menubar_update;
504
505 /* Maximum height for resizing mini-windows. Either a float
506 specifying a fraction of the available height, or an integer
507 specifying a number of lines. */
508
509 Lisp_Object Vmax_mini_window_height;
510
511 /* Non-zero means messages should be displayed with truncated
512 lines instead of being continued. */
513
514 int message_truncate_lines;
515 Lisp_Object Qmessage_truncate_lines;
516
517 /* Set to 1 in clear_message to make redisplay_internal aware
518 of an emptied echo area. */
519
520 static int message_cleared_p;
521
522 /* Non-zero means we want a hollow cursor in windows that are not
523 selected. Zero means there's no cursor in such windows. */
524
525 int cursor_in_non_selected_windows;
526 Lisp_Object Qcursor_in_non_selected_windows;
527
528 /* A scratch glyph row with contents used for generating truncation
529 glyphs. Also used in direct_output_for_insert. */
530
531 #define MAX_SCRATCH_GLYPHS 100
532 struct glyph_row scratch_glyph_row;
533 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
534
535 /* Ascent and height of the last line processed by move_it_to. */
536
537 static int last_max_ascent, last_height;
538
539 /* Non-zero if there's a help-echo in the echo area. */
540
541 int help_echo_showing_p;
542
543 /* If >= 0, computed, exact values of mode-line and header-line height
544 to use in the macros CURRENT_MODE_LINE_HEIGHT and
545 CURRENT_HEADER_LINE_HEIGHT. */
546
547 int current_mode_line_height, current_header_line_height;
548
549 /* The maximum distance to look ahead for text properties. Values
550 that are too small let us call compute_char_face and similar
551 functions too often which is expensive. Values that are too large
552 let us call compute_char_face and alike too often because we
553 might not be interested in text properties that far away. */
554
555 #define TEXT_PROP_DISTANCE_LIMIT 100
556
557 #if GLYPH_DEBUG
558
559 /* Variables to turn off display optimizations from Lisp. */
560
561 int inhibit_try_window_id, inhibit_try_window_reusing;
562 int inhibit_try_cursor_movement;
563
564 /* Non-zero means print traces of redisplay if compiled with
565 GLYPH_DEBUG != 0. */
566
567 int trace_redisplay_p;
568
569 #endif /* GLYPH_DEBUG */
570
571 #ifdef DEBUG_TRACE_MOVE
572 /* Non-zero means trace with TRACE_MOVE to stderr. */
573 int trace_move;
574
575 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
576 #else
577 #define TRACE_MOVE(x) (void) 0
578 #endif
579
580 /* Non-zero means automatically scroll windows horizontally to make
581 point visible. */
582
583 int automatic_hscrolling_p;
584
585 /* How close to the margin can point get before the window is scrolled
586 horizontally. */
587 EMACS_INT hscroll_margin;
588
589 /* How much to scroll horizontally when point is inside the above margin. */
590 Lisp_Object Vhscroll_step;
591
592 /* A list of symbols, one for each supported image type. */
593
594 Lisp_Object Vimage_types;
595
596 /* The variable `resize-mini-windows'. If nil, don't resize
597 mini-windows. If t, always resize them to fit the text they
598 display. If `grow-only', let mini-windows grow only until they
599 become empty. */
600
601 Lisp_Object Vresize_mini_windows;
602
603 /* Buffer being redisplayed -- for redisplay_window_error. */
604
605 struct buffer *displayed_buffer;
606
607 /* Value returned from text property handlers (see below). */
608
609 enum prop_handled
610 {
611 HANDLED_NORMALLY,
612 HANDLED_RECOMPUTE_PROPS,
613 HANDLED_OVERLAY_STRING_CONSUMED,
614 HANDLED_RETURN
615 };
616
617 /* A description of text properties that redisplay is interested
618 in. */
619
620 struct props
621 {
622 /* The name of the property. */
623 Lisp_Object *name;
624
625 /* A unique index for the property. */
626 enum prop_idx idx;
627
628 /* A handler function called to set up iterator IT from the property
629 at IT's current position. Value is used to steer handle_stop. */
630 enum prop_handled (*handler) P_ ((struct it *it));
631 };
632
633 static enum prop_handled handle_face_prop P_ ((struct it *));
634 static enum prop_handled handle_invisible_prop P_ ((struct it *));
635 static enum prop_handled handle_display_prop P_ ((struct it *));
636 static enum prop_handled handle_composition_prop P_ ((struct it *));
637 static enum prop_handled handle_overlay_change P_ ((struct it *));
638 static enum prop_handled handle_fontified_prop P_ ((struct it *));
639
640 /* Properties handled by iterators. */
641
642 static struct props it_props[] =
643 {
644 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
645 /* Handle `face' before `display' because some sub-properties of
646 `display' need to know the face. */
647 {&Qface, FACE_PROP_IDX, handle_face_prop},
648 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
649 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
650 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
651 {NULL, 0, NULL}
652 };
653
654 /* Value is the position described by X. If X is a marker, value is
655 the marker_position of X. Otherwise, value is X. */
656
657 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
658
659 /* Enumeration returned by some move_it_.* functions internally. */
660
661 enum move_it_result
662 {
663 /* Not used. Undefined value. */
664 MOVE_UNDEFINED,
665
666 /* Move ended at the requested buffer position or ZV. */
667 MOVE_POS_MATCH_OR_ZV,
668
669 /* Move ended at the requested X pixel position. */
670 MOVE_X_REACHED,
671
672 /* Move within a line ended at the end of a line that must be
673 continued. */
674 MOVE_LINE_CONTINUED,
675
676 /* Move within a line ended at the end of a line that would
677 be displayed truncated. */
678 MOVE_LINE_TRUNCATED,
679
680 /* Move within a line ended at a line end. */
681 MOVE_NEWLINE_OR_CR
682 };
683
684
685 \f
686 /* Function prototypes. */
687
688 static void setup_for_ellipsis P_ ((struct it *));
689 static void mark_window_display_accurate_1 P_ ((struct window *, int));
690 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
691 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
692 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
693 static int redisplay_mode_lines P_ ((Lisp_Object, int));
694 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
695
696 #if 0
697 static int invisible_text_between_p P_ ((struct it *, int, int));
698 #endif
699
700 static int next_element_from_ellipsis P_ ((struct it *));
701 static void pint2str P_ ((char *, int, int));
702 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
703 struct text_pos));
704 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
705 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
706 static void store_frame_title_char P_ ((char));
707 static int store_frame_title P_ ((unsigned char *, int, int));
708 static void x_consider_frame_title P_ ((Lisp_Object));
709 static void handle_stop P_ ((struct it *));
710 static int tool_bar_lines_needed P_ ((struct frame *));
711 static int single_display_prop_intangible_p P_ ((Lisp_Object));
712 static void ensure_echo_area_buffers P_ ((void));
713 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
714 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
715 static int with_echo_area_buffer P_ ((struct window *, int,
716 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
717 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
718 static void clear_garbaged_frames P_ ((void));
719 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
720 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
721 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
722 static int display_echo_area P_ ((struct window *));
723 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
724 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
725 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
726 static int string_char_and_length P_ ((unsigned char *, int, int *));
727 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
728 struct text_pos));
729 static int compute_window_start_on_continuation_line P_ ((struct window *));
730 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
731 static void insert_left_trunc_glyphs P_ ((struct it *));
732 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
733 static void extend_face_to_end_of_line P_ ((struct it *));
734 static int append_space P_ ((struct it *, int));
735 static int make_cursor_line_fully_visible P_ ((struct window *));
736 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int));
737 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
738 static int trailing_whitespace_p P_ ((int));
739 static int message_log_check_duplicate P_ ((int, int, int, int));
740 static void push_it P_ ((struct it *));
741 static void pop_it P_ ((struct it *));
742 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
743 static void redisplay_internal P_ ((int));
744 static int echo_area_display P_ ((int));
745 static void redisplay_windows P_ ((Lisp_Object));
746 static void redisplay_window P_ ((Lisp_Object, int));
747 static Lisp_Object redisplay_window_error ();
748 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
749 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
750 static void update_menu_bar P_ ((struct frame *, int));
751 static int try_window_reusing_current_matrix P_ ((struct window *));
752 static int try_window_id P_ ((struct window *));
753 static int display_line P_ ((struct it *));
754 static int display_mode_lines P_ ((struct window *));
755 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
756 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object));
757 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
758 static void display_menu_bar P_ ((struct window *));
759 static int display_count_lines P_ ((int, int, int, int, int *));
760 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
761 int, int, struct it *, int, int, int, int));
762 static void compute_line_metrics P_ ((struct it *));
763 static void run_redisplay_end_trigger_hook P_ ((struct it *));
764 static int get_overlay_strings P_ ((struct it *, int));
765 static void next_overlay_string P_ ((struct it *));
766 static void reseat P_ ((struct it *, struct text_pos, int));
767 static void reseat_1 P_ ((struct it *, struct text_pos, int));
768 static void back_to_previous_visible_line_start P_ ((struct it *));
769 static void reseat_at_previous_visible_line_start P_ ((struct it *));
770 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
771 static int next_element_from_display_vector P_ ((struct it *));
772 static int next_element_from_string P_ ((struct it *));
773 static int next_element_from_c_string P_ ((struct it *));
774 static int next_element_from_buffer P_ ((struct it *));
775 static int next_element_from_composition P_ ((struct it *));
776 static int next_element_from_image P_ ((struct it *));
777 static int next_element_from_stretch P_ ((struct it *));
778 static void load_overlay_strings P_ ((struct it *, int));
779 static int init_from_display_pos P_ ((struct it *, struct window *,
780 struct display_pos *));
781 static void reseat_to_string P_ ((struct it *, unsigned char *,
782 Lisp_Object, int, int, int, int));
783 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
784 int, int, int));
785 void move_it_vertically_backward P_ ((struct it *, int));
786 static void init_to_row_start P_ ((struct it *, struct window *,
787 struct glyph_row *));
788 static int init_to_row_end P_ ((struct it *, struct window *,
789 struct glyph_row *));
790 static void back_to_previous_line_start P_ ((struct it *));
791 static int forward_to_next_line_start P_ ((struct it *, int *));
792 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
793 Lisp_Object, int));
794 static struct text_pos string_pos P_ ((int, Lisp_Object));
795 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
796 static int number_of_chars P_ ((unsigned char *, int));
797 static void compute_stop_pos P_ ((struct it *));
798 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
799 Lisp_Object));
800 static int face_before_or_after_it_pos P_ ((struct it *, int));
801 static int next_overlay_change P_ ((int));
802 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
803 Lisp_Object, struct text_pos *,
804 int));
805 static int underlying_face_id P_ ((struct it *));
806 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
807 struct window *));
808
809 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
810 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
811
812 #ifdef HAVE_WINDOW_SYSTEM
813
814 static void update_tool_bar P_ ((struct frame *, int));
815 static void build_desired_tool_bar_string P_ ((struct frame *f));
816 static int redisplay_tool_bar P_ ((struct frame *));
817 static void display_tool_bar_line P_ ((struct it *));
818
819 #endif /* HAVE_WINDOW_SYSTEM */
820
821 \f
822 /***********************************************************************
823 Window display dimensions
824 ***********************************************************************/
825
826 /* Return the window-relative maximum y + 1 for glyph rows displaying
827 text in window W. This is the height of W minus the height of a
828 mode line, if any. */
829
830 INLINE int
831 window_text_bottom_y (w)
832 struct window *w;
833 {
834 struct frame *f = XFRAME (w->frame);
835 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
836
837 if (WINDOW_WANTS_MODELINE_P (w))
838 height -= CURRENT_MODE_LINE_HEIGHT (w);
839 return height;
840 }
841
842
843 /* Return the pixel width of display area AREA of window W. AREA < 0
844 means return the total width of W, not including fringes to
845 the left and right of the window. */
846
847 INLINE int
848 window_box_width (w, area)
849 struct window *w;
850 int area;
851 {
852 struct frame *f = XFRAME (w->frame);
853 int width = XFASTINT (w->width);
854
855 if (!w->pseudo_window_p)
856 {
857 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
858
859 if (area == TEXT_AREA)
860 {
861 if (INTEGERP (w->left_margin_width))
862 width -= XFASTINT (w->left_margin_width);
863 if (INTEGERP (w->right_margin_width))
864 width -= XFASTINT (w->right_margin_width);
865 }
866 else if (area == LEFT_MARGIN_AREA)
867 width = (INTEGERP (w->left_margin_width)
868 ? XFASTINT (w->left_margin_width) : 0);
869 else if (area == RIGHT_MARGIN_AREA)
870 width = (INTEGERP (w->right_margin_width)
871 ? XFASTINT (w->right_margin_width) : 0);
872 }
873
874 return width * CANON_X_UNIT (f);
875 }
876
877
878 /* Return the pixel height of the display area of window W, not
879 including mode lines of W, if any. */
880
881 INLINE int
882 window_box_height (w)
883 struct window *w;
884 {
885 struct frame *f = XFRAME (w->frame);
886 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
887
888 xassert (height >= 0);
889
890 /* Note: the code below that determines the mode-line/header-line
891 height is essentially the same as that contained in the macro
892 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
893 the appropriate glyph row has its `mode_line_p' flag set,
894 and if it doesn't, uses estimate_mode_line_height instead. */
895
896 if (WINDOW_WANTS_MODELINE_P (w))
897 {
898 struct glyph_row *ml_row
899 = (w->current_matrix && w->current_matrix->rows
900 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
901 : 0);
902 if (ml_row && ml_row->mode_line_p)
903 height -= ml_row->height;
904 else
905 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
906 }
907
908 if (WINDOW_WANTS_HEADER_LINE_P (w))
909 {
910 struct glyph_row *hl_row
911 = (w->current_matrix && w->current_matrix->rows
912 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
913 : 0);
914 if (hl_row && hl_row->mode_line_p)
915 height -= hl_row->height;
916 else
917 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
918 }
919
920 /* With a very small font and a mode-line that's taller than
921 default, we might end up with a negative height. */
922 return max (0, height);
923 }
924
925
926 /* Return the frame-relative coordinate of the left edge of display
927 area AREA of window W. AREA < 0 means return the left edge of the
928 whole window, to the right of the left fringe of W. */
929
930 INLINE int
931 window_box_left (w, area)
932 struct window *w;
933 int area;
934 {
935 struct frame *f = XFRAME (w->frame);
936 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
937
938 if (!w->pseudo_window_p)
939 {
940 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
941 + FRAME_LEFT_FRINGE_WIDTH (f));
942
943 if (area == TEXT_AREA)
944 x += window_box_width (w, LEFT_MARGIN_AREA);
945 else if (area == RIGHT_MARGIN_AREA)
946 x += (window_box_width (w, LEFT_MARGIN_AREA)
947 + window_box_width (w, TEXT_AREA));
948 }
949
950 return x;
951 }
952
953
954 /* Return the frame-relative coordinate of the right edge of display
955 area AREA of window W. AREA < 0 means return the left edge of the
956 whole window, to the left of the right fringe of W. */
957
958 INLINE int
959 window_box_right (w, area)
960 struct window *w;
961 int area;
962 {
963 return window_box_left (w, area) + window_box_width (w, area);
964 }
965
966
967 /* Get the bounding box of the display area AREA of window W, without
968 mode lines, in frame-relative coordinates. AREA < 0 means the
969 whole window, not including the left and right fringes of
970 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
971 coordinates of the upper-left corner of the box. Return in
972 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
973
974 INLINE void
975 window_box (w, area, box_x, box_y, box_width, box_height)
976 struct window *w;
977 int area;
978 int *box_x, *box_y, *box_width, *box_height;
979 {
980 struct frame *f = XFRAME (w->frame);
981
982 *box_width = window_box_width (w, area);
983 *box_height = window_box_height (w);
984 *box_x = window_box_left (w, area);
985 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
986 + XFASTINT (w->top) * CANON_Y_UNIT (f));
987 if (WINDOW_WANTS_HEADER_LINE_P (w))
988 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
989 }
990
991
992 /* Get the bounding box of the display area AREA of window W, without
993 mode lines. AREA < 0 means the whole window, not including the
994 left and right fringe of the window. Return in *TOP_LEFT_X
995 and TOP_LEFT_Y the frame-relative pixel coordinates of the
996 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
997 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
998 box. */
999
1000 INLINE void
1001 window_box_edges (w, area, top_left_x, top_left_y,
1002 bottom_right_x, bottom_right_y)
1003 struct window *w;
1004 int area;
1005 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1006 {
1007 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1008 bottom_right_y);
1009 *bottom_right_x += *top_left_x;
1010 *bottom_right_y += *top_left_y;
1011 }
1012
1013
1014 \f
1015 /***********************************************************************
1016 Utilities
1017 ***********************************************************************/
1018
1019 /* Return the bottom y-position of the line the iterator IT is in.
1020 This can modify IT's settings. */
1021
1022 int
1023 line_bottom_y (it)
1024 struct it *it;
1025 {
1026 int line_height = it->max_ascent + it->max_descent;
1027 int line_top_y = it->current_y;
1028
1029 if (line_height == 0)
1030 {
1031 if (last_height)
1032 line_height = last_height;
1033 else if (IT_CHARPOS (*it) < ZV)
1034 {
1035 move_it_by_lines (it, 1, 1);
1036 line_height = (it->max_ascent || it->max_descent
1037 ? it->max_ascent + it->max_descent
1038 : last_height);
1039 }
1040 else
1041 {
1042 struct glyph_row *row = it->glyph_row;
1043
1044 /* Use the default character height. */
1045 it->glyph_row = NULL;
1046 it->what = IT_CHARACTER;
1047 it->c = ' ';
1048 it->len = 1;
1049 PRODUCE_GLYPHS (it);
1050 line_height = it->ascent + it->descent;
1051 it->glyph_row = row;
1052 }
1053 }
1054
1055 return line_top_y + line_height;
1056 }
1057
1058
1059 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1060 1 if POS is visible and the line containing POS is fully visible.
1061 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1062 and header-lines heights. */
1063
1064 int
1065 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1066 struct window *w;
1067 int charpos, *fully, exact_mode_line_heights_p;
1068 {
1069 struct it it;
1070 struct text_pos top;
1071 int visible_p;
1072 struct buffer *old_buffer = NULL;
1073
1074 if (XBUFFER (w->buffer) != current_buffer)
1075 {
1076 old_buffer = current_buffer;
1077 set_buffer_internal_1 (XBUFFER (w->buffer));
1078 }
1079
1080 *fully = visible_p = 0;
1081 SET_TEXT_POS_FROM_MARKER (top, w->start);
1082
1083 /* Compute exact mode line heights, if requested. */
1084 if (exact_mode_line_heights_p)
1085 {
1086 if (WINDOW_WANTS_MODELINE_P (w))
1087 current_mode_line_height
1088 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1089 current_buffer->mode_line_format);
1090
1091 if (WINDOW_WANTS_HEADER_LINE_P (w))
1092 current_header_line_height
1093 = display_mode_line (w, HEADER_LINE_FACE_ID,
1094 current_buffer->header_line_format);
1095 }
1096
1097 start_display (&it, w, top);
1098 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1099 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1100
1101 /* Note that we may overshoot because of invisible text. */
1102 if (IT_CHARPOS (it) >= charpos)
1103 {
1104 int top_y = it.current_y;
1105 int bottom_y = line_bottom_y (&it);
1106 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1107
1108 if (top_y < window_top_y)
1109 visible_p = bottom_y > window_top_y;
1110 else if (top_y < it.last_visible_y)
1111 {
1112 visible_p = 1;
1113 *fully = bottom_y <= it.last_visible_y;
1114 }
1115 }
1116 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1117 {
1118 move_it_by_lines (&it, 1, 0);
1119 if (charpos < IT_CHARPOS (it))
1120 {
1121 visible_p = 1;
1122 *fully = 0;
1123 }
1124 }
1125
1126 if (old_buffer)
1127 set_buffer_internal_1 (old_buffer);
1128
1129 current_header_line_height = current_mode_line_height = -1;
1130 return visible_p;
1131 }
1132
1133
1134 /* Return the next character from STR which is MAXLEN bytes long.
1135 Return in *LEN the length of the character. This is like
1136 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1137 we find one, we return a `?', but with the length of the invalid
1138 character. */
1139
1140 static INLINE int
1141 string_char_and_length (str, maxlen, len)
1142 unsigned char *str;
1143 int maxlen, *len;
1144 {
1145 int c;
1146
1147 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1148 if (!CHAR_VALID_P (c, 1))
1149 /* We may not change the length here because other places in Emacs
1150 don't use this function, i.e. they silently accept invalid
1151 characters. */
1152 c = '?';
1153
1154 return c;
1155 }
1156
1157
1158
1159 /* Given a position POS containing a valid character and byte position
1160 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1161
1162 static struct text_pos
1163 string_pos_nchars_ahead (pos, string, nchars)
1164 struct text_pos pos;
1165 Lisp_Object string;
1166 int nchars;
1167 {
1168 xassert (STRINGP (string) && nchars >= 0);
1169
1170 if (STRING_MULTIBYTE (string))
1171 {
1172 int rest = SBYTES (string) - BYTEPOS (pos);
1173 unsigned char *p = SDATA (string) + BYTEPOS (pos);
1174 int len;
1175
1176 while (nchars--)
1177 {
1178 string_char_and_length (p, rest, &len);
1179 p += len, rest -= len;
1180 xassert (rest >= 0);
1181 CHARPOS (pos) += 1;
1182 BYTEPOS (pos) += len;
1183 }
1184 }
1185 else
1186 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1187
1188 return pos;
1189 }
1190
1191
1192 /* Value is the text position, i.e. character and byte position,
1193 for character position CHARPOS in STRING. */
1194
1195 static INLINE struct text_pos
1196 string_pos (charpos, string)
1197 int charpos;
1198 Lisp_Object string;
1199 {
1200 struct text_pos pos;
1201 xassert (STRINGP (string));
1202 xassert (charpos >= 0);
1203 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1204 return pos;
1205 }
1206
1207
1208 /* Value is a text position, i.e. character and byte position, for
1209 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1210 means recognize multibyte characters. */
1211
1212 static struct text_pos
1213 c_string_pos (charpos, s, multibyte_p)
1214 int charpos;
1215 unsigned char *s;
1216 int multibyte_p;
1217 {
1218 struct text_pos pos;
1219
1220 xassert (s != NULL);
1221 xassert (charpos >= 0);
1222
1223 if (multibyte_p)
1224 {
1225 int rest = strlen (s), len;
1226
1227 SET_TEXT_POS (pos, 0, 0);
1228 while (charpos--)
1229 {
1230 string_char_and_length (s, rest, &len);
1231 s += len, rest -= len;
1232 xassert (rest >= 0);
1233 CHARPOS (pos) += 1;
1234 BYTEPOS (pos) += len;
1235 }
1236 }
1237 else
1238 SET_TEXT_POS (pos, charpos, charpos);
1239
1240 return pos;
1241 }
1242
1243
1244 /* Value is the number of characters in C string S. MULTIBYTE_P
1245 non-zero means recognize multibyte characters. */
1246
1247 static int
1248 number_of_chars (s, multibyte_p)
1249 unsigned char *s;
1250 int multibyte_p;
1251 {
1252 int nchars;
1253
1254 if (multibyte_p)
1255 {
1256 int rest = strlen (s), len;
1257 unsigned char *p = (unsigned char *) s;
1258
1259 for (nchars = 0; rest > 0; ++nchars)
1260 {
1261 string_char_and_length (p, rest, &len);
1262 rest -= len, p += len;
1263 }
1264 }
1265 else
1266 nchars = strlen (s);
1267
1268 return nchars;
1269 }
1270
1271
1272 /* Compute byte position NEWPOS->bytepos corresponding to
1273 NEWPOS->charpos. POS is a known position in string STRING.
1274 NEWPOS->charpos must be >= POS.charpos. */
1275
1276 static void
1277 compute_string_pos (newpos, pos, string)
1278 struct text_pos *newpos, pos;
1279 Lisp_Object string;
1280 {
1281 xassert (STRINGP (string));
1282 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1283
1284 if (STRING_MULTIBYTE (string))
1285 *newpos = string_pos_nchars_ahead (pos, string,
1286 CHARPOS (*newpos) - CHARPOS (pos));
1287 else
1288 BYTEPOS (*newpos) = CHARPOS (*newpos);
1289 }
1290
1291
1292 \f
1293 /***********************************************************************
1294 Lisp form evaluation
1295 ***********************************************************************/
1296
1297 /* Error handler for safe_eval and safe_call. */
1298
1299 static Lisp_Object
1300 safe_eval_handler (arg)
1301 Lisp_Object arg;
1302 {
1303 add_to_log ("Error during redisplay: %s", arg, Qnil);
1304 return Qnil;
1305 }
1306
1307
1308 /* Evaluate SEXPR and return the result, or nil if something went
1309 wrong. Prevent redisplay during the evaluation. */
1310
1311 Lisp_Object
1312 safe_eval (sexpr)
1313 Lisp_Object sexpr;
1314 {
1315 Lisp_Object val;
1316
1317 if (inhibit_eval_during_redisplay)
1318 val = Qnil;
1319 else
1320 {
1321 int count = BINDING_STACK_SIZE ();
1322 struct gcpro gcpro1;
1323
1324 GCPRO1 (sexpr);
1325 specbind (Qinhibit_redisplay, Qt);
1326 val = internal_condition_case_1 (Feval, sexpr, Qerror,
1327 safe_eval_handler);
1328 UNGCPRO;
1329 val = unbind_to (count, val);
1330 }
1331
1332 return val;
1333 }
1334
1335
1336 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1337 Return the result, or nil if something went wrong. Prevent
1338 redisplay during the evaluation. */
1339
1340 Lisp_Object
1341 safe_call (nargs, args)
1342 int nargs;
1343 Lisp_Object *args;
1344 {
1345 Lisp_Object val;
1346
1347 if (inhibit_eval_during_redisplay)
1348 val = Qnil;
1349 else
1350 {
1351 int count = BINDING_STACK_SIZE ();
1352 struct gcpro gcpro1;
1353
1354 GCPRO1 (args[0]);
1355 gcpro1.nvars = nargs;
1356 specbind (Qinhibit_redisplay, Qt);
1357 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1358 safe_eval_handler);
1359 UNGCPRO;
1360 val = unbind_to (count, val);
1361 }
1362
1363 return val;
1364 }
1365
1366
1367 /* Call function FN with one argument ARG.
1368 Return the result, or nil if something went wrong. */
1369
1370 Lisp_Object
1371 safe_call1 (fn, arg)
1372 Lisp_Object fn, arg;
1373 {
1374 Lisp_Object args[2];
1375 args[0] = fn;
1376 args[1] = arg;
1377 return safe_call (2, args);
1378 }
1379
1380
1381 \f
1382 /***********************************************************************
1383 Debugging
1384 ***********************************************************************/
1385
1386 #if 0
1387
1388 /* Define CHECK_IT to perform sanity checks on iterators.
1389 This is for debugging. It is too slow to do unconditionally. */
1390
1391 static void
1392 check_it (it)
1393 struct it *it;
1394 {
1395 if (it->method == next_element_from_string)
1396 {
1397 xassert (STRINGP (it->string));
1398 xassert (IT_STRING_CHARPOS (*it) >= 0);
1399 }
1400 else if (it->method == next_element_from_buffer)
1401 {
1402 /* Check that character and byte positions agree. */
1403 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1404 }
1405
1406 if (it->dpvec)
1407 xassert (it->current.dpvec_index >= 0);
1408 else
1409 xassert (it->current.dpvec_index < 0);
1410 }
1411
1412 #define CHECK_IT(IT) check_it ((IT))
1413
1414 #else /* not 0 */
1415
1416 #define CHECK_IT(IT) (void) 0
1417
1418 #endif /* not 0 */
1419
1420
1421 #if GLYPH_DEBUG
1422
1423 /* Check that the window end of window W is what we expect it
1424 to be---the last row in the current matrix displaying text. */
1425
1426 static void
1427 check_window_end (w)
1428 struct window *w;
1429 {
1430 if (!MINI_WINDOW_P (w)
1431 && !NILP (w->window_end_valid))
1432 {
1433 struct glyph_row *row;
1434 xassert ((row = MATRIX_ROW (w->current_matrix,
1435 XFASTINT (w->window_end_vpos)),
1436 !row->enabled_p
1437 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1438 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1439 }
1440 }
1441
1442 #define CHECK_WINDOW_END(W) check_window_end ((W))
1443
1444 #else /* not GLYPH_DEBUG */
1445
1446 #define CHECK_WINDOW_END(W) (void) 0
1447
1448 #endif /* not GLYPH_DEBUG */
1449
1450
1451 \f
1452 /***********************************************************************
1453 Iterator initialization
1454 ***********************************************************************/
1455
1456 /* Initialize IT for displaying current_buffer in window W, starting
1457 at character position CHARPOS. CHARPOS < 0 means that no buffer
1458 position is specified which is useful when the iterator is assigned
1459 a position later. BYTEPOS is the byte position corresponding to
1460 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1461
1462 If ROW is not null, calls to produce_glyphs with IT as parameter
1463 will produce glyphs in that row.
1464
1465 BASE_FACE_ID is the id of a base face to use. It must be one of
1466 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1467 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1468 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1469
1470 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1471 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1472 will be initialized to use the corresponding mode line glyph row of
1473 the desired matrix of W. */
1474
1475 void
1476 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1477 struct it *it;
1478 struct window *w;
1479 int charpos, bytepos;
1480 struct glyph_row *row;
1481 enum face_id base_face_id;
1482 {
1483 int highlight_region_p;
1484
1485 /* Some precondition checks. */
1486 xassert (w != NULL && it != NULL);
1487 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1488 && charpos <= ZV));
1489
1490 /* If face attributes have been changed since the last redisplay,
1491 free realized faces now because they depend on face definitions
1492 that might have changed. */
1493 if (face_change_count)
1494 {
1495 face_change_count = 0;
1496 free_all_realized_faces (Qnil);
1497 }
1498
1499 /* Use one of the mode line rows of W's desired matrix if
1500 appropriate. */
1501 if (row == NULL)
1502 {
1503 if (base_face_id == MODE_LINE_FACE_ID
1504 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1505 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1506 else if (base_face_id == HEADER_LINE_FACE_ID)
1507 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1508 }
1509
1510 /* Clear IT. */
1511 bzero (it, sizeof *it);
1512 it->current.overlay_string_index = -1;
1513 it->current.dpvec_index = -1;
1514 it->base_face_id = base_face_id;
1515
1516 /* The window in which we iterate over current_buffer: */
1517 XSETWINDOW (it->window, w);
1518 it->w = w;
1519 it->f = XFRAME (w->frame);
1520
1521 /* Extra space between lines (on window systems only). */
1522 if (base_face_id == DEFAULT_FACE_ID
1523 && FRAME_WINDOW_P (it->f))
1524 {
1525 if (NATNUMP (current_buffer->extra_line_spacing))
1526 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1527 else if (it->f->extra_line_spacing > 0)
1528 it->extra_line_spacing = it->f->extra_line_spacing;
1529 }
1530
1531 /* If realized faces have been removed, e.g. because of face
1532 attribute changes of named faces, recompute them. When running
1533 in batch mode, the face cache of Vterminal_frame is null. If
1534 we happen to get called, make a dummy face cache. */
1535 if (
1536 #ifndef WINDOWSNT
1537 noninteractive &&
1538 #endif
1539 FRAME_FACE_CACHE (it->f) == NULL)
1540 init_frame_faces (it->f);
1541 if (FRAME_FACE_CACHE (it->f)->used == 0)
1542 recompute_basic_faces (it->f);
1543
1544 /* Current value of the `space-width', and 'height' properties. */
1545 it->space_width = Qnil;
1546 it->font_height = Qnil;
1547
1548 /* Are control characters displayed as `^C'? */
1549 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1550
1551 /* -1 means everything between a CR and the following line end
1552 is invisible. >0 means lines indented more than this value are
1553 invisible. */
1554 it->selective = (INTEGERP (current_buffer->selective_display)
1555 ? XFASTINT (current_buffer->selective_display)
1556 : (!NILP (current_buffer->selective_display)
1557 ? -1 : 0));
1558 it->selective_display_ellipsis_p
1559 = !NILP (current_buffer->selective_display_ellipses);
1560
1561 /* Display table to use. */
1562 it->dp = window_display_table (w);
1563
1564 /* Are multibyte characters enabled in current_buffer? */
1565 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1566
1567 /* Non-zero if we should highlight the region. */
1568 highlight_region_p
1569 = (!NILP (Vtransient_mark_mode)
1570 && !NILP (current_buffer->mark_active)
1571 && XMARKER (current_buffer->mark)->buffer != 0);
1572
1573 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1574 start and end of a visible region in window IT->w. Set both to
1575 -1 to indicate no region. */
1576 if (highlight_region_p
1577 /* Maybe highlight only in selected window. */
1578 && (/* Either show region everywhere. */
1579 highlight_nonselected_windows
1580 /* Or show region in the selected window. */
1581 || w == XWINDOW (selected_window)
1582 /* Or show the region if we are in the mini-buffer and W is
1583 the window the mini-buffer refers to. */
1584 || (MINI_WINDOW_P (XWINDOW (selected_window))
1585 && WINDOWP (minibuf_selected_window)
1586 && w == XWINDOW (minibuf_selected_window))))
1587 {
1588 int charpos = marker_position (current_buffer->mark);
1589 it->region_beg_charpos = min (PT, charpos);
1590 it->region_end_charpos = max (PT, charpos);
1591 }
1592 else
1593 it->region_beg_charpos = it->region_end_charpos = -1;
1594
1595 /* Get the position at which the redisplay_end_trigger hook should
1596 be run, if it is to be run at all. */
1597 if (MARKERP (w->redisplay_end_trigger)
1598 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1599 it->redisplay_end_trigger_charpos
1600 = marker_position (w->redisplay_end_trigger);
1601 else if (INTEGERP (w->redisplay_end_trigger))
1602 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1603
1604 /* Correct bogus values of tab_width. */
1605 it->tab_width = XINT (current_buffer->tab_width);
1606 if (it->tab_width <= 0 || it->tab_width > 1000)
1607 it->tab_width = 8;
1608
1609 /* Are lines in the display truncated? */
1610 it->truncate_lines_p
1611 = (base_face_id != DEFAULT_FACE_ID
1612 || XINT (it->w->hscroll)
1613 || (truncate_partial_width_windows
1614 && !WINDOW_FULL_WIDTH_P (it->w))
1615 || !NILP (current_buffer->truncate_lines));
1616
1617 /* Get dimensions of truncation and continuation glyphs. These are
1618 displayed as fringe bitmaps under X, so we don't need them for such
1619 frames. */
1620 if (!FRAME_WINDOW_P (it->f))
1621 {
1622 if (it->truncate_lines_p)
1623 {
1624 /* We will need the truncation glyph. */
1625 xassert (it->glyph_row == NULL);
1626 produce_special_glyphs (it, IT_TRUNCATION);
1627 it->truncation_pixel_width = it->pixel_width;
1628 }
1629 else
1630 {
1631 /* We will need the continuation glyph. */
1632 xassert (it->glyph_row == NULL);
1633 produce_special_glyphs (it, IT_CONTINUATION);
1634 it->continuation_pixel_width = it->pixel_width;
1635 }
1636
1637 /* Reset these values to zero because the produce_special_glyphs
1638 above has changed them. */
1639 it->pixel_width = it->ascent = it->descent = 0;
1640 it->phys_ascent = it->phys_descent = 0;
1641 }
1642
1643 /* Set this after getting the dimensions of truncation and
1644 continuation glyphs, so that we don't produce glyphs when calling
1645 produce_special_glyphs, above. */
1646 it->glyph_row = row;
1647 it->area = TEXT_AREA;
1648
1649 /* Get the dimensions of the display area. The display area
1650 consists of the visible window area plus a horizontally scrolled
1651 part to the left of the window. All x-values are relative to the
1652 start of this total display area. */
1653 if (base_face_id != DEFAULT_FACE_ID)
1654 {
1655 /* Mode lines, menu bar in terminal frames. */
1656 it->first_visible_x = 0;
1657 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1658 }
1659 else
1660 {
1661 it->first_visible_x
1662 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1663 it->last_visible_x = (it->first_visible_x
1664 + window_box_width (w, TEXT_AREA));
1665
1666 /* If we truncate lines, leave room for the truncator glyph(s) at
1667 the right margin. Otherwise, leave room for the continuation
1668 glyph(s). Truncation and continuation glyphs are not inserted
1669 for window-based redisplay. */
1670 if (!FRAME_WINDOW_P (it->f))
1671 {
1672 if (it->truncate_lines_p)
1673 it->last_visible_x -= it->truncation_pixel_width;
1674 else
1675 it->last_visible_x -= it->continuation_pixel_width;
1676 }
1677
1678 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1679 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1680 }
1681
1682 /* Leave room for a border glyph. */
1683 if (!FRAME_WINDOW_P (it->f)
1684 && !WINDOW_RIGHTMOST_P (it->w))
1685 it->last_visible_x -= 1;
1686
1687 it->last_visible_y = window_text_bottom_y (w);
1688
1689 /* For mode lines and alike, arrange for the first glyph having a
1690 left box line if the face specifies a box. */
1691 if (base_face_id != DEFAULT_FACE_ID)
1692 {
1693 struct face *face;
1694
1695 it->face_id = base_face_id;
1696
1697 /* If we have a boxed mode line, make the first character appear
1698 with a left box line. */
1699 face = FACE_FROM_ID (it->f, base_face_id);
1700 if (face->box != FACE_NO_BOX)
1701 it->start_of_box_run_p = 1;
1702 }
1703
1704 /* If a buffer position was specified, set the iterator there,
1705 getting overlays and face properties from that position. */
1706 if (charpos >= BUF_BEG (current_buffer))
1707 {
1708 it->end_charpos = ZV;
1709 it->face_id = -1;
1710 IT_CHARPOS (*it) = charpos;
1711
1712 /* Compute byte position if not specified. */
1713 if (bytepos < charpos)
1714 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1715 else
1716 IT_BYTEPOS (*it) = bytepos;
1717
1718 /* Compute faces etc. */
1719 reseat (it, it->current.pos, 1);
1720 }
1721
1722 CHECK_IT (it);
1723 }
1724
1725
1726 /* Initialize IT for the display of window W with window start POS. */
1727
1728 void
1729 start_display (it, w, pos)
1730 struct it *it;
1731 struct window *w;
1732 struct text_pos pos;
1733 {
1734 struct glyph_row *row;
1735 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1736
1737 row = w->desired_matrix->rows + first_vpos;
1738 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1739
1740 if (!it->truncate_lines_p)
1741 {
1742 int start_at_line_beg_p;
1743 int first_y = it->current_y;
1744
1745 /* If window start is not at a line start, skip forward to POS to
1746 get the correct continuation lines width. */
1747 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1748 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1749 if (!start_at_line_beg_p)
1750 {
1751 reseat_at_previous_visible_line_start (it);
1752 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1753
1754 /* If lines are continued, this line may end in the middle
1755 of a multi-glyph character (e.g. a control character
1756 displayed as \003, or in the middle of an overlay
1757 string). In this case move_it_to above will not have
1758 taken us to the start of the continuation line but to the
1759 end of the continued line. */
1760 if (it->current_x > 0)
1761 {
1762 if (it->current.dpvec_index >= 0
1763 || it->current.overlay_string_index >= 0)
1764 {
1765 set_iterator_to_next (it, 1);
1766 move_it_in_display_line_to (it, -1, -1, 0);
1767 }
1768
1769 it->continuation_lines_width += it->current_x;
1770 }
1771
1772 /* We're starting a new display line, not affected by the
1773 height of the continued line, so clear the appropriate
1774 fields in the iterator structure. */
1775 it->max_ascent = it->max_descent = 0;
1776 it->max_phys_ascent = it->max_phys_descent = 0;
1777
1778 it->current_y = first_y;
1779 it->vpos = 0;
1780 it->current_x = it->hpos = 0;
1781 }
1782 }
1783
1784 #if 0 /* Don't assert the following because start_display is sometimes
1785 called intentionally with a window start that is not at a
1786 line start. Please leave this code in as a comment. */
1787
1788 /* Window start should be on a line start, now. */
1789 xassert (it->continuation_lines_width
1790 || IT_CHARPOS (it) == BEGV
1791 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1792 #endif /* 0 */
1793 }
1794
1795
1796 /* Return 1 if POS is a position in ellipses displayed for invisible
1797 text. W is the window we display, for text property lookup. */
1798
1799 static int
1800 in_ellipses_for_invisible_text_p (pos, w)
1801 struct display_pos *pos;
1802 struct window *w;
1803 {
1804 Lisp_Object prop, window;
1805 int ellipses_p = 0;
1806 int charpos = CHARPOS (pos->pos);
1807
1808 /* If POS specifies a position in a display vector, this might
1809 be for an ellipsis displayed for invisible text. We won't
1810 get the iterator set up for delivering that ellipsis unless
1811 we make sure that it gets aware of the invisible text. */
1812 if (pos->dpvec_index >= 0
1813 && pos->overlay_string_index < 0
1814 && CHARPOS (pos->string_pos) < 0
1815 && charpos > BEGV
1816 && (XSETWINDOW (window, w),
1817 prop = Fget_char_property (make_number (charpos),
1818 Qinvisible, window),
1819 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1820 {
1821 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1822 window);
1823 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
1824 }
1825
1826 return ellipses_p;
1827 }
1828
1829
1830 /* Initialize IT for stepping through current_buffer in window W,
1831 starting at position POS that includes overlay string and display
1832 vector/ control character translation position information. Value
1833 is zero if there are overlay strings with newlines at POS. */
1834
1835 static int
1836 init_from_display_pos (it, w, pos)
1837 struct it *it;
1838 struct window *w;
1839 struct display_pos *pos;
1840 {
1841 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1842 int i, overlay_strings_with_newlines = 0;
1843
1844 /* If POS specifies a position in a display vector, this might
1845 be for an ellipsis displayed for invisible text. We won't
1846 get the iterator set up for delivering that ellipsis unless
1847 we make sure that it gets aware of the invisible text. */
1848 if (in_ellipses_for_invisible_text_p (pos, w))
1849 {
1850 --charpos;
1851 bytepos = 0;
1852 }
1853
1854 /* Keep in mind: the call to reseat in init_iterator skips invisible
1855 text, so we might end up at a position different from POS. This
1856 is only a problem when POS is a row start after a newline and an
1857 overlay starts there with an after-string, and the overlay has an
1858 invisible property. Since we don't skip invisible text in
1859 display_line and elsewhere immediately after consuming the
1860 newline before the row start, such a POS will not be in a string,
1861 but the call to init_iterator below will move us to the
1862 after-string. */
1863 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1864
1865 for (i = 0; i < it->n_overlay_strings; ++i)
1866 {
1867 char *s = SDATA (it->overlay_strings[i]);
1868 char *e = s + SBYTES (it->overlay_strings[i]);
1869
1870 while (s < e && *s != '\n')
1871 ++s;
1872
1873 if (s < e)
1874 {
1875 overlay_strings_with_newlines = 1;
1876 break;
1877 }
1878 }
1879
1880 /* If position is within an overlay string, set up IT to the right
1881 overlay string. */
1882 if (pos->overlay_string_index >= 0)
1883 {
1884 int relative_index;
1885
1886 /* If the first overlay string happens to have a `display'
1887 property for an image, the iterator will be set up for that
1888 image, and we have to undo that setup first before we can
1889 correct the overlay string index. */
1890 if (it->method == next_element_from_image)
1891 pop_it (it);
1892
1893 /* We already have the first chunk of overlay strings in
1894 IT->overlay_strings. Load more until the one for
1895 pos->overlay_string_index is in IT->overlay_strings. */
1896 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1897 {
1898 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1899 it->current.overlay_string_index = 0;
1900 while (n--)
1901 {
1902 load_overlay_strings (it, 0);
1903 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1904 }
1905 }
1906
1907 it->current.overlay_string_index = pos->overlay_string_index;
1908 relative_index = (it->current.overlay_string_index
1909 % OVERLAY_STRING_CHUNK_SIZE);
1910 it->string = it->overlay_strings[relative_index];
1911 xassert (STRINGP (it->string));
1912 it->current.string_pos = pos->string_pos;
1913 it->method = next_element_from_string;
1914 }
1915
1916 #if 0 /* This is bogus because POS not having an overlay string
1917 position does not mean it's after the string. Example: A
1918 line starting with a before-string and initialization of IT
1919 to the previous row's end position. */
1920 else if (it->current.overlay_string_index >= 0)
1921 {
1922 /* If POS says we're already after an overlay string ending at
1923 POS, make sure to pop the iterator because it will be in
1924 front of that overlay string. When POS is ZV, we've thereby
1925 also ``processed'' overlay strings at ZV. */
1926 while (it->sp)
1927 pop_it (it);
1928 it->current.overlay_string_index = -1;
1929 it->method = next_element_from_buffer;
1930 if (CHARPOS (pos->pos) == ZV)
1931 it->overlay_strings_at_end_processed_p = 1;
1932 }
1933 #endif /* 0 */
1934
1935 if (CHARPOS (pos->string_pos) >= 0)
1936 {
1937 /* Recorded position is not in an overlay string, but in another
1938 string. This can only be a string from a `display' property.
1939 IT should already be filled with that string. */
1940 it->current.string_pos = pos->string_pos;
1941 xassert (STRINGP (it->string));
1942 }
1943
1944 /* Restore position in display vector translations, control
1945 character translations or ellipses. */
1946 if (pos->dpvec_index >= 0)
1947 {
1948 if (it->dpvec == NULL)
1949 get_next_display_element (it);
1950 xassert (it->dpvec && it->current.dpvec_index == 0);
1951 it->current.dpvec_index = pos->dpvec_index;
1952 }
1953
1954 CHECK_IT (it);
1955 return !overlay_strings_with_newlines;
1956 }
1957
1958
1959 /* Initialize IT for stepping through current_buffer in window W
1960 starting at ROW->start. */
1961
1962 static void
1963 init_to_row_start (it, w, row)
1964 struct it *it;
1965 struct window *w;
1966 struct glyph_row *row;
1967 {
1968 init_from_display_pos (it, w, &row->start);
1969 it->continuation_lines_width = row->continuation_lines_width;
1970 CHECK_IT (it);
1971 }
1972
1973
1974 /* Initialize IT for stepping through current_buffer in window W
1975 starting in the line following ROW, i.e. starting at ROW->end.
1976 Value is zero if there are overlay strings with newlines at ROW's
1977 end position. */
1978
1979 static int
1980 init_to_row_end (it, w, row)
1981 struct it *it;
1982 struct window *w;
1983 struct glyph_row *row;
1984 {
1985 int success = 0;
1986
1987 if (init_from_display_pos (it, w, &row->end))
1988 {
1989 if (row->continued_p)
1990 it->continuation_lines_width
1991 = row->continuation_lines_width + row->pixel_width;
1992 CHECK_IT (it);
1993 success = 1;
1994 }
1995
1996 return success;
1997 }
1998
1999
2000
2001 \f
2002 /***********************************************************************
2003 Text properties
2004 ***********************************************************************/
2005
2006 /* Called when IT reaches IT->stop_charpos. Handle text property and
2007 overlay changes. Set IT->stop_charpos to the next position where
2008 to stop. */
2009
2010 static void
2011 handle_stop (it)
2012 struct it *it;
2013 {
2014 enum prop_handled handled;
2015 int handle_overlay_change_p = 1;
2016 struct props *p;
2017
2018 it->dpvec = NULL;
2019 it->current.dpvec_index = -1;
2020
2021 do
2022 {
2023 handled = HANDLED_NORMALLY;
2024
2025 /* Call text property handlers. */
2026 for (p = it_props; p->handler; ++p)
2027 {
2028 handled = p->handler (it);
2029
2030 if (handled == HANDLED_RECOMPUTE_PROPS)
2031 break;
2032 else if (handled == HANDLED_RETURN)
2033 return;
2034 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2035 handle_overlay_change_p = 0;
2036 }
2037
2038 if (handled != HANDLED_RECOMPUTE_PROPS)
2039 {
2040 /* Don't check for overlay strings below when set to deliver
2041 characters from a display vector. */
2042 if (it->method == next_element_from_display_vector)
2043 handle_overlay_change_p = 0;
2044
2045 /* Handle overlay changes. */
2046 if (handle_overlay_change_p)
2047 handled = handle_overlay_change (it);
2048
2049 /* Determine where to stop next. */
2050 if (handled == HANDLED_NORMALLY)
2051 compute_stop_pos (it);
2052 }
2053 }
2054 while (handled == HANDLED_RECOMPUTE_PROPS);
2055 }
2056
2057
2058 /* Compute IT->stop_charpos from text property and overlay change
2059 information for IT's current position. */
2060
2061 static void
2062 compute_stop_pos (it)
2063 struct it *it;
2064 {
2065 register INTERVAL iv, next_iv;
2066 Lisp_Object object, limit, position;
2067
2068 /* If nowhere else, stop at the end. */
2069 it->stop_charpos = it->end_charpos;
2070
2071 if (STRINGP (it->string))
2072 {
2073 /* Strings are usually short, so don't limit the search for
2074 properties. */
2075 object = it->string;
2076 limit = Qnil;
2077 position = make_number (IT_STRING_CHARPOS (*it));
2078 }
2079 else
2080 {
2081 int charpos;
2082
2083 /* If next overlay change is in front of the current stop pos
2084 (which is IT->end_charpos), stop there. Note: value of
2085 next_overlay_change is point-max if no overlay change
2086 follows. */
2087 charpos = next_overlay_change (IT_CHARPOS (*it));
2088 if (charpos < it->stop_charpos)
2089 it->stop_charpos = charpos;
2090
2091 /* If showing the region, we have to stop at the region
2092 start or end because the face might change there. */
2093 if (it->region_beg_charpos > 0)
2094 {
2095 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2096 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2097 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2098 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2099 }
2100
2101 /* Set up variables for computing the stop position from text
2102 property changes. */
2103 XSETBUFFER (object, current_buffer);
2104 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2105 position = make_number (IT_CHARPOS (*it));
2106
2107 }
2108
2109 /* Get the interval containing IT's position. Value is a null
2110 interval if there isn't such an interval. */
2111 iv = validate_interval_range (object, &position, &position, 0);
2112 if (!NULL_INTERVAL_P (iv))
2113 {
2114 Lisp_Object values_here[LAST_PROP_IDX];
2115 struct props *p;
2116
2117 /* Get properties here. */
2118 for (p = it_props; p->handler; ++p)
2119 values_here[p->idx] = textget (iv->plist, *p->name);
2120
2121 /* Look for an interval following iv that has different
2122 properties. */
2123 for (next_iv = next_interval (iv);
2124 (!NULL_INTERVAL_P (next_iv)
2125 && (NILP (limit)
2126 || XFASTINT (limit) > next_iv->position));
2127 next_iv = next_interval (next_iv))
2128 {
2129 for (p = it_props; p->handler; ++p)
2130 {
2131 Lisp_Object new_value;
2132
2133 new_value = textget (next_iv->plist, *p->name);
2134 if (!EQ (values_here[p->idx], new_value))
2135 break;
2136 }
2137
2138 if (p->handler)
2139 break;
2140 }
2141
2142 if (!NULL_INTERVAL_P (next_iv))
2143 {
2144 if (INTEGERP (limit)
2145 && next_iv->position >= XFASTINT (limit))
2146 /* No text property change up to limit. */
2147 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2148 else
2149 /* Text properties change in next_iv. */
2150 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2151 }
2152 }
2153
2154 xassert (STRINGP (it->string)
2155 || (it->stop_charpos >= BEGV
2156 && it->stop_charpos >= IT_CHARPOS (*it)));
2157 }
2158
2159
2160 /* Return the position of the next overlay change after POS in
2161 current_buffer. Value is point-max if no overlay change
2162 follows. This is like `next-overlay-change' but doesn't use
2163 xmalloc. */
2164
2165 static int
2166 next_overlay_change (pos)
2167 int pos;
2168 {
2169 int noverlays;
2170 int endpos;
2171 Lisp_Object *overlays;
2172 int len;
2173 int i;
2174
2175 /* Get all overlays at the given position. */
2176 len = 10;
2177 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2178 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2179 if (noverlays > len)
2180 {
2181 len = noverlays;
2182 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2183 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2184 }
2185
2186 /* If any of these overlays ends before endpos,
2187 use its ending point instead. */
2188 for (i = 0; i < noverlays; ++i)
2189 {
2190 Lisp_Object oend;
2191 int oendpos;
2192
2193 oend = OVERLAY_END (overlays[i]);
2194 oendpos = OVERLAY_POSITION (oend);
2195 endpos = min (endpos, oendpos);
2196 }
2197
2198 return endpos;
2199 }
2200
2201
2202 \f
2203 /***********************************************************************
2204 Fontification
2205 ***********************************************************************/
2206
2207 /* Handle changes in the `fontified' property of the current buffer by
2208 calling hook functions from Qfontification_functions to fontify
2209 regions of text. */
2210
2211 static enum prop_handled
2212 handle_fontified_prop (it)
2213 struct it *it;
2214 {
2215 Lisp_Object prop, pos;
2216 enum prop_handled handled = HANDLED_NORMALLY;
2217
2218 /* Get the value of the `fontified' property at IT's current buffer
2219 position. (The `fontified' property doesn't have a special
2220 meaning in strings.) If the value is nil, call functions from
2221 Qfontification_functions. */
2222 if (!STRINGP (it->string)
2223 && it->s == NULL
2224 && !NILP (Vfontification_functions)
2225 && !NILP (Vrun_hooks)
2226 && (pos = make_number (IT_CHARPOS (*it)),
2227 prop = Fget_char_property (pos, Qfontified, Qnil),
2228 NILP (prop)))
2229 {
2230 int count = BINDING_STACK_SIZE ();
2231 Lisp_Object val;
2232
2233 val = Vfontification_functions;
2234 specbind (Qfontification_functions, Qnil);
2235
2236 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2237 safe_call1 (val, pos);
2238 else
2239 {
2240 Lisp_Object globals, fn;
2241 struct gcpro gcpro1, gcpro2;
2242
2243 globals = Qnil;
2244 GCPRO2 (val, globals);
2245
2246 for (; CONSP (val); val = XCDR (val))
2247 {
2248 fn = XCAR (val);
2249
2250 if (EQ (fn, Qt))
2251 {
2252 /* A value of t indicates this hook has a local
2253 binding; it means to run the global binding too.
2254 In a global value, t should not occur. If it
2255 does, we must ignore it to avoid an endless
2256 loop. */
2257 for (globals = Fdefault_value (Qfontification_functions);
2258 CONSP (globals);
2259 globals = XCDR (globals))
2260 {
2261 fn = XCAR (globals);
2262 if (!EQ (fn, Qt))
2263 safe_call1 (fn, pos);
2264 }
2265 }
2266 else
2267 safe_call1 (fn, pos);
2268 }
2269
2270 UNGCPRO;
2271 }
2272
2273 unbind_to (count, Qnil);
2274
2275 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2276 something. This avoids an endless loop if they failed to
2277 fontify the text for which reason ever. */
2278 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2279 handled = HANDLED_RECOMPUTE_PROPS;
2280 }
2281
2282 return handled;
2283 }
2284
2285
2286 \f
2287 /***********************************************************************
2288 Faces
2289 ***********************************************************************/
2290
2291 /* Set up iterator IT from face properties at its current position.
2292 Called from handle_stop. */
2293
2294 static enum prop_handled
2295 handle_face_prop (it)
2296 struct it *it;
2297 {
2298 int new_face_id, next_stop;
2299
2300 if (!STRINGP (it->string))
2301 {
2302 new_face_id
2303 = face_at_buffer_position (it->w,
2304 IT_CHARPOS (*it),
2305 it->region_beg_charpos,
2306 it->region_end_charpos,
2307 &next_stop,
2308 (IT_CHARPOS (*it)
2309 + TEXT_PROP_DISTANCE_LIMIT),
2310 0);
2311
2312 /* Is this a start of a run of characters with box face?
2313 Caveat: this can be called for a freshly initialized
2314 iterator; face_id is -1 in this case. We know that the new
2315 face will not change until limit, i.e. if the new face has a
2316 box, all characters up to limit will have one. But, as
2317 usual, we don't know whether limit is really the end. */
2318 if (new_face_id != it->face_id)
2319 {
2320 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2321
2322 /* If new face has a box but old face has not, this is
2323 the start of a run of characters with box, i.e. it has
2324 a shadow on the left side. The value of face_id of the
2325 iterator will be -1 if this is the initial call that gets
2326 the face. In this case, we have to look in front of IT's
2327 position and see whether there is a face != new_face_id. */
2328 it->start_of_box_run_p
2329 = (new_face->box != FACE_NO_BOX
2330 && (it->face_id >= 0
2331 || IT_CHARPOS (*it) == BEG
2332 || new_face_id != face_before_it_pos (it)));
2333 it->face_box_p = new_face->box != FACE_NO_BOX;
2334 }
2335 }
2336 else
2337 {
2338 int base_face_id, bufpos;
2339
2340 if (it->current.overlay_string_index >= 0)
2341 bufpos = IT_CHARPOS (*it);
2342 else
2343 bufpos = 0;
2344
2345 /* For strings from a buffer, i.e. overlay strings or strings
2346 from a `display' property, use the face at IT's current
2347 buffer position as the base face to merge with, so that
2348 overlay strings appear in the same face as surrounding
2349 text, unless they specify their own faces. */
2350 base_face_id = underlying_face_id (it);
2351
2352 new_face_id = face_at_string_position (it->w,
2353 it->string,
2354 IT_STRING_CHARPOS (*it),
2355 bufpos,
2356 it->region_beg_charpos,
2357 it->region_end_charpos,
2358 &next_stop,
2359 base_face_id, 0);
2360
2361 #if 0 /* This shouldn't be neccessary. Let's check it. */
2362 /* If IT is used to display a mode line we would really like to
2363 use the mode line face instead of the frame's default face. */
2364 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2365 && new_face_id == DEFAULT_FACE_ID)
2366 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2367 #endif
2368
2369 /* Is this a start of a run of characters with box? Caveat:
2370 this can be called for a freshly allocated iterator; face_id
2371 is -1 is this case. We know that the new face will not
2372 change until the next check pos, i.e. if the new face has a
2373 box, all characters up to that position will have a
2374 box. But, as usual, we don't know whether that position
2375 is really the end. */
2376 if (new_face_id != it->face_id)
2377 {
2378 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2379 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2380
2381 /* If new face has a box but old face hasn't, this is the
2382 start of a run of characters with box, i.e. it has a
2383 shadow on the left side. */
2384 it->start_of_box_run_p
2385 = new_face->box && (old_face == NULL || !old_face->box);
2386 it->face_box_p = new_face->box != FACE_NO_BOX;
2387 }
2388 }
2389
2390 it->face_id = new_face_id;
2391 return HANDLED_NORMALLY;
2392 }
2393
2394
2395 /* Return the ID of the face ``underlying'' IT's current position,
2396 which is in a string. If the iterator is associated with a
2397 buffer, return the face at IT's current buffer position.
2398 Otherwise, use the iterator's base_face_id. */
2399
2400 static int
2401 underlying_face_id (it)
2402 struct it *it;
2403 {
2404 int face_id = it->base_face_id, i;
2405
2406 xassert (STRINGP (it->string));
2407
2408 for (i = it->sp - 1; i >= 0; --i)
2409 if (NILP (it->stack[i].string))
2410 face_id = it->stack[i].face_id;
2411
2412 return face_id;
2413 }
2414
2415
2416 /* Compute the face one character before or after the current position
2417 of IT. BEFORE_P non-zero means get the face in front of IT's
2418 position. Value is the id of the face. */
2419
2420 static int
2421 face_before_or_after_it_pos (it, before_p)
2422 struct it *it;
2423 int before_p;
2424 {
2425 int face_id, limit;
2426 int next_check_charpos;
2427 struct text_pos pos;
2428
2429 xassert (it->s == NULL);
2430
2431 if (STRINGP (it->string))
2432 {
2433 int bufpos, base_face_id;
2434
2435 /* No face change past the end of the string (for the case
2436 we are padding with spaces). No face change before the
2437 string start. */
2438 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2439 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2440 return it->face_id;
2441
2442 /* Set pos to the position before or after IT's current position. */
2443 if (before_p)
2444 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2445 else
2446 /* For composition, we must check the character after the
2447 composition. */
2448 pos = (it->what == IT_COMPOSITION
2449 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2450 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2451
2452 if (it->current.overlay_string_index >= 0)
2453 bufpos = IT_CHARPOS (*it);
2454 else
2455 bufpos = 0;
2456
2457 base_face_id = underlying_face_id (it);
2458
2459 /* Get the face for ASCII, or unibyte. */
2460 face_id = face_at_string_position (it->w,
2461 it->string,
2462 CHARPOS (pos),
2463 bufpos,
2464 it->region_beg_charpos,
2465 it->region_end_charpos,
2466 &next_check_charpos,
2467 base_face_id, 0);
2468
2469 /* Correct the face for charsets different from ASCII. Do it
2470 for the multibyte case only. The face returned above is
2471 suitable for unibyte text if IT->string is unibyte. */
2472 if (STRING_MULTIBYTE (it->string))
2473 {
2474 unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2475 int rest = SBYTES (it->string) - BYTEPOS (pos);
2476 int c, len;
2477 struct face *face = FACE_FROM_ID (it->f, face_id);
2478
2479 c = string_char_and_length (p, rest, &len);
2480 face_id = FACE_FOR_CHAR (it->f, face, c);
2481 }
2482 }
2483 else
2484 {
2485 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2486 || (IT_CHARPOS (*it) <= BEGV && before_p))
2487 return it->face_id;
2488
2489 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2490 pos = it->current.pos;
2491
2492 if (before_p)
2493 DEC_TEXT_POS (pos, it->multibyte_p);
2494 else
2495 {
2496 if (it->what == IT_COMPOSITION)
2497 /* For composition, we must check the position after the
2498 composition. */
2499 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2500 else
2501 INC_TEXT_POS (pos, it->multibyte_p);
2502 }
2503
2504 /* Determine face for CHARSET_ASCII, or unibyte. */
2505 face_id = face_at_buffer_position (it->w,
2506 CHARPOS (pos),
2507 it->region_beg_charpos,
2508 it->region_end_charpos,
2509 &next_check_charpos,
2510 limit, 0);
2511
2512 /* Correct the face for charsets different from ASCII. Do it
2513 for the multibyte case only. The face returned above is
2514 suitable for unibyte text if current_buffer is unibyte. */
2515 if (it->multibyte_p)
2516 {
2517 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2518 struct face *face = FACE_FROM_ID (it->f, face_id);
2519 face_id = FACE_FOR_CHAR (it->f, face, c);
2520 }
2521 }
2522
2523 return face_id;
2524 }
2525
2526
2527 \f
2528 /***********************************************************************
2529 Invisible text
2530 ***********************************************************************/
2531
2532 /* Set up iterator IT from invisible properties at its current
2533 position. Called from handle_stop. */
2534
2535 static enum prop_handled
2536 handle_invisible_prop (it)
2537 struct it *it;
2538 {
2539 enum prop_handled handled = HANDLED_NORMALLY;
2540
2541 if (STRINGP (it->string))
2542 {
2543 extern Lisp_Object Qinvisible;
2544 Lisp_Object prop, end_charpos, limit, charpos;
2545
2546 /* Get the value of the invisible text property at the
2547 current position. Value will be nil if there is no such
2548 property. */
2549 charpos = make_number (IT_STRING_CHARPOS (*it));
2550 prop = Fget_text_property (charpos, Qinvisible, it->string);
2551
2552 if (!NILP (prop)
2553 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2554 {
2555 handled = HANDLED_RECOMPUTE_PROPS;
2556
2557 /* Get the position at which the next change of the
2558 invisible text property can be found in IT->string.
2559 Value will be nil if the property value is the same for
2560 all the rest of IT->string. */
2561 XSETINT (limit, SCHARS (it->string));
2562 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2563 it->string, limit);
2564
2565 /* Text at current position is invisible. The next
2566 change in the property is at position end_charpos.
2567 Move IT's current position to that position. */
2568 if (INTEGERP (end_charpos)
2569 && XFASTINT (end_charpos) < XFASTINT (limit))
2570 {
2571 struct text_pos old;
2572 old = it->current.string_pos;
2573 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2574 compute_string_pos (&it->current.string_pos, old, it->string);
2575 }
2576 else
2577 {
2578 /* The rest of the string is invisible. If this is an
2579 overlay string, proceed with the next overlay string
2580 or whatever comes and return a character from there. */
2581 if (it->current.overlay_string_index >= 0)
2582 {
2583 next_overlay_string (it);
2584 /* Don't check for overlay strings when we just
2585 finished processing them. */
2586 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2587 }
2588 else
2589 {
2590 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
2591 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
2592 }
2593 }
2594 }
2595 }
2596 else
2597 {
2598 int invis_p, newpos, next_stop, start_charpos;
2599 Lisp_Object pos, prop, overlay;
2600
2601 /* First of all, is there invisible text at this position? */
2602 start_charpos = IT_CHARPOS (*it);
2603 pos = make_number (IT_CHARPOS (*it));
2604 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2605 &overlay);
2606 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2607
2608 /* If we are on invisible text, skip over it. */
2609 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
2610 {
2611 /* Record whether we have to display an ellipsis for the
2612 invisible text. */
2613 int display_ellipsis_p = invis_p == 2;
2614
2615 handled = HANDLED_RECOMPUTE_PROPS;
2616
2617 /* Loop skipping over invisible text. The loop is left at
2618 ZV or with IT on the first char being visible again. */
2619 do
2620 {
2621 /* Try to skip some invisible text. Return value is the
2622 position reached which can be equal to IT's position
2623 if there is nothing invisible here. This skips both
2624 over invisible text properties and overlays with
2625 invisible property. */
2626 newpos = skip_invisible (IT_CHARPOS (*it),
2627 &next_stop, ZV, it->window);
2628
2629 /* If we skipped nothing at all we weren't at invisible
2630 text in the first place. If everything to the end of
2631 the buffer was skipped, end the loop. */
2632 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2633 invis_p = 0;
2634 else
2635 {
2636 /* We skipped some characters but not necessarily
2637 all there are. Check if we ended up on visible
2638 text. Fget_char_property returns the property of
2639 the char before the given position, i.e. if we
2640 get invis_p = 0, this means that the char at
2641 newpos is visible. */
2642 pos = make_number (newpos);
2643 prop = Fget_char_property (pos, Qinvisible, it->window);
2644 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2645 }
2646
2647 /* If we ended up on invisible text, proceed to
2648 skip starting with next_stop. */
2649 if (invis_p)
2650 IT_CHARPOS (*it) = next_stop;
2651 }
2652 while (invis_p);
2653
2654 /* The position newpos is now either ZV or on visible text. */
2655 IT_CHARPOS (*it) = newpos;
2656 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2657
2658 /* If there are before-strings at the start of invisible
2659 text, and the text is invisible because of a text
2660 property, arrange to show before-strings because 20.x did
2661 it that way. (If the text is invisible because of an
2662 overlay property instead of a text property, this is
2663 already handled in the overlay code.) */
2664 if (NILP (overlay)
2665 && get_overlay_strings (it, start_charpos))
2666 {
2667 handled = HANDLED_RECOMPUTE_PROPS;
2668 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2669 }
2670 else if (display_ellipsis_p)
2671 setup_for_ellipsis (it);
2672 }
2673 }
2674
2675 return handled;
2676 }
2677
2678
2679 /* Make iterator IT return `...' next. */
2680
2681 static void
2682 setup_for_ellipsis (it)
2683 struct it *it;
2684 {
2685 if (it->dp
2686 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2687 {
2688 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2689 it->dpvec = v->contents;
2690 it->dpend = v->contents + v->size;
2691 }
2692 else
2693 {
2694 /* Default `...'. */
2695 it->dpvec = default_invis_vector;
2696 it->dpend = default_invis_vector + 3;
2697 }
2698
2699 /* The ellipsis display does not replace the display of the
2700 character at the new position. Indicate this by setting
2701 IT->dpvec_char_len to zero. */
2702 it->dpvec_char_len = 0;
2703
2704 it->current.dpvec_index = 0;
2705 it->method = next_element_from_display_vector;
2706 }
2707
2708
2709 \f
2710 /***********************************************************************
2711 'display' property
2712 ***********************************************************************/
2713
2714 /* Set up iterator IT from `display' property at its current position.
2715 Called from handle_stop. */
2716
2717 static enum prop_handled
2718 handle_display_prop (it)
2719 struct it *it;
2720 {
2721 Lisp_Object prop, object;
2722 struct text_pos *position;
2723 int display_replaced_p = 0;
2724
2725 if (STRINGP (it->string))
2726 {
2727 object = it->string;
2728 position = &it->current.string_pos;
2729 }
2730 else
2731 {
2732 object = it->w->buffer;
2733 position = &it->current.pos;
2734 }
2735
2736 /* Reset those iterator values set from display property values. */
2737 it->font_height = Qnil;
2738 it->space_width = Qnil;
2739 it->voffset = 0;
2740
2741 /* We don't support recursive `display' properties, i.e. string
2742 values that have a string `display' property, that have a string
2743 `display' property etc. */
2744 if (!it->string_from_display_prop_p)
2745 it->area = TEXT_AREA;
2746
2747 prop = Fget_char_property (make_number (position->charpos),
2748 Qdisplay, object);
2749 if (NILP (prop))
2750 return HANDLED_NORMALLY;
2751
2752 if (CONSP (prop)
2753 /* Simple properties. */
2754 && !EQ (XCAR (prop), Qimage)
2755 && !EQ (XCAR (prop), Qspace)
2756 && !EQ (XCAR (prop), Qwhen)
2757 && !EQ (XCAR (prop), Qspace_width)
2758 && !EQ (XCAR (prop), Qheight)
2759 && !EQ (XCAR (prop), Qraise)
2760 /* Marginal area specifications. */
2761 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2762 && !NILP (XCAR (prop)))
2763 {
2764 for (; CONSP (prop); prop = XCDR (prop))
2765 {
2766 if (handle_single_display_prop (it, XCAR (prop), object,
2767 position, display_replaced_p))
2768 display_replaced_p = 1;
2769 }
2770 }
2771 else if (VECTORP (prop))
2772 {
2773 int i;
2774 for (i = 0; i < ASIZE (prop); ++i)
2775 if (handle_single_display_prop (it, AREF (prop, i), object,
2776 position, display_replaced_p))
2777 display_replaced_p = 1;
2778 }
2779 else
2780 {
2781 if (handle_single_display_prop (it, prop, object, position, 0))
2782 display_replaced_p = 1;
2783 }
2784
2785 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2786 }
2787
2788
2789 /* Value is the position of the end of the `display' property starting
2790 at START_POS in OBJECT. */
2791
2792 static struct text_pos
2793 display_prop_end (it, object, start_pos)
2794 struct it *it;
2795 Lisp_Object object;
2796 struct text_pos start_pos;
2797 {
2798 Lisp_Object end;
2799 struct text_pos end_pos;
2800
2801 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2802 Qdisplay, object, Qnil);
2803 CHARPOS (end_pos) = XFASTINT (end);
2804 if (STRINGP (object))
2805 compute_string_pos (&end_pos, start_pos, it->string);
2806 else
2807 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2808
2809 return end_pos;
2810 }
2811
2812
2813 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2814 is the object in which the `display' property was found. *POSITION
2815 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2816 means that we previously saw a display sub-property which already
2817 replaced text display with something else, for example an image;
2818 ignore such properties after the first one has been processed.
2819
2820 If PROP is a `space' or `image' sub-property, set *POSITION to the
2821 end position of the `display' property.
2822
2823 Value is non-zero if something was found which replaces the display
2824 of buffer or string text. */
2825
2826 static int
2827 handle_single_display_prop (it, prop, object, position,
2828 display_replaced_before_p)
2829 struct it *it;
2830 Lisp_Object prop;
2831 Lisp_Object object;
2832 struct text_pos *position;
2833 int display_replaced_before_p;
2834 {
2835 Lisp_Object value;
2836 int replaces_text_display_p = 0;
2837 Lisp_Object form;
2838
2839 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2840 evaluated. If the result is nil, VALUE is ignored. */
2841 form = Qt;
2842 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2843 {
2844 prop = XCDR (prop);
2845 if (!CONSP (prop))
2846 return 0;
2847 form = XCAR (prop);
2848 prop = XCDR (prop);
2849 }
2850
2851 if (!NILP (form) && !EQ (form, Qt))
2852 {
2853 int count = BINDING_STACK_SIZE ();
2854 struct gcpro gcpro1;
2855
2856 /* Bind `object' to the object having the `display' property, a
2857 buffer or string. Bind `position' to the position in the
2858 object where the property was found, and `buffer-position'
2859 to the current position in the buffer. */
2860 specbind (Qobject, object);
2861 specbind (Qposition, make_number (CHARPOS (*position)));
2862 specbind (Qbuffer_position,
2863 make_number (STRINGP (object)
2864 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2865 GCPRO1 (form);
2866 form = safe_eval (form);
2867 UNGCPRO;
2868 unbind_to (count, Qnil);
2869 }
2870
2871 if (NILP (form))
2872 return 0;
2873
2874 if (CONSP (prop)
2875 && EQ (XCAR (prop), Qheight)
2876 && CONSP (XCDR (prop)))
2877 {
2878 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2879 return 0;
2880
2881 /* `(height HEIGHT)'. */
2882 it->font_height = XCAR (XCDR (prop));
2883 if (!NILP (it->font_height))
2884 {
2885 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2886 int new_height = -1;
2887
2888 if (CONSP (it->font_height)
2889 && (EQ (XCAR (it->font_height), Qplus)
2890 || EQ (XCAR (it->font_height), Qminus))
2891 && CONSP (XCDR (it->font_height))
2892 && INTEGERP (XCAR (XCDR (it->font_height))))
2893 {
2894 /* `(+ N)' or `(- N)' where N is an integer. */
2895 int steps = XINT (XCAR (XCDR (it->font_height)));
2896 if (EQ (XCAR (it->font_height), Qplus))
2897 steps = - steps;
2898 it->face_id = smaller_face (it->f, it->face_id, steps);
2899 }
2900 else if (FUNCTIONP (it->font_height))
2901 {
2902 /* Call function with current height as argument.
2903 Value is the new height. */
2904 Lisp_Object height;
2905 height = safe_call1 (it->font_height,
2906 face->lface[LFACE_HEIGHT_INDEX]);
2907 if (NUMBERP (height))
2908 new_height = XFLOATINT (height);
2909 }
2910 else if (NUMBERP (it->font_height))
2911 {
2912 /* Value is a multiple of the canonical char height. */
2913 struct face *face;
2914
2915 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2916 new_height = (XFLOATINT (it->font_height)
2917 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2918 }
2919 else
2920 {
2921 /* Evaluate IT->font_height with `height' bound to the
2922 current specified height to get the new height. */
2923 Lisp_Object value;
2924 int count = BINDING_STACK_SIZE ();
2925
2926 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2927 value = safe_eval (it->font_height);
2928 unbind_to (count, Qnil);
2929
2930 if (NUMBERP (value))
2931 new_height = XFLOATINT (value);
2932 }
2933
2934 if (new_height > 0)
2935 it->face_id = face_with_height (it->f, it->face_id, new_height);
2936 }
2937 }
2938 else if (CONSP (prop)
2939 && EQ (XCAR (prop), Qspace_width)
2940 && CONSP (XCDR (prop)))
2941 {
2942 /* `(space_width WIDTH)'. */
2943 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2944 return 0;
2945
2946 value = XCAR (XCDR (prop));
2947 if (NUMBERP (value) && XFLOATINT (value) > 0)
2948 it->space_width = value;
2949 }
2950 else if (CONSP (prop)
2951 && EQ (XCAR (prop), Qraise)
2952 && CONSP (XCDR (prop)))
2953 {
2954 /* `(raise FACTOR)'. */
2955 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2956 return 0;
2957
2958 #ifdef HAVE_WINDOW_SYSTEM
2959 value = XCAR (XCDR (prop));
2960 if (NUMBERP (value))
2961 {
2962 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2963 it->voffset = - (XFLOATINT (value)
2964 * (FONT_HEIGHT (face->font)));
2965 }
2966 #endif /* HAVE_WINDOW_SYSTEM */
2967 }
2968 else if (!it->string_from_display_prop_p)
2969 {
2970 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2971 VALUE) or `((margin nil) VALUE)' or VALUE. */
2972 Lisp_Object location, value;
2973 struct text_pos start_pos;
2974 int valid_p;
2975
2976 /* Characters having this form of property are not displayed, so
2977 we have to find the end of the property. */
2978 start_pos = *position;
2979 *position = display_prop_end (it, object, start_pos);
2980 value = Qnil;
2981
2982 /* Let's stop at the new position and assume that all
2983 text properties change there. */
2984 it->stop_charpos = position->charpos;
2985
2986 location = Qunbound;
2987 if (CONSP (prop) && CONSP (XCAR (prop)))
2988 {
2989 Lisp_Object tem;
2990
2991 value = XCDR (prop);
2992 if (CONSP (value))
2993 value = XCAR (value);
2994
2995 tem = XCAR (prop);
2996 if (EQ (XCAR (tem), Qmargin)
2997 && (tem = XCDR (tem),
2998 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2999 (NILP (tem)
3000 || EQ (tem, Qleft_margin)
3001 || EQ (tem, Qright_margin))))
3002 location = tem;
3003 }
3004
3005 if (EQ (location, Qunbound))
3006 {
3007 location = Qnil;
3008 value = prop;
3009 }
3010
3011 #ifdef HAVE_WINDOW_SYSTEM
3012 if (FRAME_TERMCAP_P (it->f))
3013 valid_p = STRINGP (value);
3014 else
3015 valid_p = (STRINGP (value)
3016 || (CONSP (value) && EQ (XCAR (value), Qspace))
3017 || valid_image_p (value));
3018 #else /* not HAVE_WINDOW_SYSTEM */
3019 valid_p = STRINGP (value);
3020 #endif /* not HAVE_WINDOW_SYSTEM */
3021
3022 if ((EQ (location, Qleft_margin)
3023 || EQ (location, Qright_margin)
3024 || NILP (location))
3025 && valid_p
3026 && !display_replaced_before_p)
3027 {
3028 replaces_text_display_p = 1;
3029
3030 /* Save current settings of IT so that we can restore them
3031 when we are finished with the glyph property value. */
3032 push_it (it);
3033
3034 if (NILP (location))
3035 it->area = TEXT_AREA;
3036 else if (EQ (location, Qleft_margin))
3037 it->area = LEFT_MARGIN_AREA;
3038 else
3039 it->area = RIGHT_MARGIN_AREA;
3040
3041 if (STRINGP (value))
3042 {
3043 it->string = value;
3044 it->multibyte_p = STRING_MULTIBYTE (it->string);
3045 it->current.overlay_string_index = -1;
3046 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3047 it->end_charpos = it->string_nchars = SCHARS (it->string);
3048 it->method = next_element_from_string;
3049 it->stop_charpos = 0;
3050 it->string_from_display_prop_p = 1;
3051 /* Say that we haven't consumed the characters with
3052 `display' property yet. The call to pop_it in
3053 set_iterator_to_next will clean this up. */
3054 *position = start_pos;
3055 }
3056 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3057 {
3058 it->method = next_element_from_stretch;
3059 it->object = value;
3060 it->current.pos = it->position = start_pos;
3061 }
3062 #ifdef HAVE_WINDOW_SYSTEM
3063 else
3064 {
3065 it->what = IT_IMAGE;
3066 it->image_id = lookup_image (it->f, value);
3067 it->position = start_pos;
3068 it->object = NILP (object) ? it->w->buffer : object;
3069 it->method = next_element_from_image;
3070
3071 /* Say that we haven't consumed the characters with
3072 `display' property yet. The call to pop_it in
3073 set_iterator_to_next will clean this up. */
3074 *position = start_pos;
3075 }
3076 #endif /* HAVE_WINDOW_SYSTEM */
3077 }
3078 else
3079 /* Invalid property or property not supported. Restore
3080 the position to what it was before. */
3081 *position = start_pos;
3082 }
3083
3084 return replaces_text_display_p;
3085 }
3086
3087
3088 /* Check if PROP is a display sub-property value whose text should be
3089 treated as intangible. */
3090
3091 static int
3092 single_display_prop_intangible_p (prop)
3093 Lisp_Object prop;
3094 {
3095 /* Skip over `when FORM'. */
3096 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3097 {
3098 prop = XCDR (prop);
3099 if (!CONSP (prop))
3100 return 0;
3101 prop = XCDR (prop);
3102 }
3103
3104 if (!CONSP (prop))
3105 return 0;
3106
3107 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3108 we don't need to treat text as intangible. */
3109 if (EQ (XCAR (prop), Qmargin))
3110 {
3111 prop = XCDR (prop);
3112 if (!CONSP (prop))
3113 return 0;
3114
3115 prop = XCDR (prop);
3116 if (!CONSP (prop)
3117 || EQ (XCAR (prop), Qleft_margin)
3118 || EQ (XCAR (prop), Qright_margin))
3119 return 0;
3120 }
3121
3122 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3123 }
3124
3125
3126 /* Check if PROP is a display property value whose text should be
3127 treated as intangible. */
3128
3129 int
3130 display_prop_intangible_p (prop)
3131 Lisp_Object prop;
3132 {
3133 if (CONSP (prop)
3134 && CONSP (XCAR (prop))
3135 && !EQ (Qmargin, XCAR (XCAR (prop))))
3136 {
3137 /* A list of sub-properties. */
3138 while (CONSP (prop))
3139 {
3140 if (single_display_prop_intangible_p (XCAR (prop)))
3141 return 1;
3142 prop = XCDR (prop);
3143 }
3144 }
3145 else if (VECTORP (prop))
3146 {
3147 /* A vector of sub-properties. */
3148 int i;
3149 for (i = 0; i < ASIZE (prop); ++i)
3150 if (single_display_prop_intangible_p (AREF (prop, i)))
3151 return 1;
3152 }
3153 else
3154 return single_display_prop_intangible_p (prop);
3155
3156 return 0;
3157 }
3158
3159
3160 /* Return 1 if PROP is a display sub-property value containing STRING. */
3161
3162 static int
3163 single_display_prop_string_p (prop, string)
3164 Lisp_Object prop, string;
3165 {
3166 if (EQ (string, prop))
3167 return 1;
3168
3169 /* Skip over `when FORM'. */
3170 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3171 {
3172 prop = XCDR (prop);
3173 if (!CONSP (prop))
3174 return 0;
3175 prop = XCDR (prop);
3176 }
3177
3178 if (CONSP (prop))
3179 /* Skip over `margin LOCATION'. */
3180 if (EQ (XCAR (prop), Qmargin))
3181 {
3182 prop = XCDR (prop);
3183 if (!CONSP (prop))
3184 return 0;
3185
3186 prop = XCDR (prop);
3187 if (!CONSP (prop))
3188 return 0;
3189 }
3190
3191 return CONSP (prop) && EQ (XCAR (prop), string);
3192 }
3193
3194
3195 /* Return 1 if STRING appears in the `display' property PROP. */
3196
3197 static int
3198 display_prop_string_p (prop, string)
3199 Lisp_Object prop, string;
3200 {
3201 if (CONSP (prop)
3202 && CONSP (XCAR (prop))
3203 && !EQ (Qmargin, XCAR (XCAR (prop))))
3204 {
3205 /* A list of sub-properties. */
3206 while (CONSP (prop))
3207 {
3208 if (single_display_prop_string_p (XCAR (prop), string))
3209 return 1;
3210 prop = XCDR (prop);
3211 }
3212 }
3213 else if (VECTORP (prop))
3214 {
3215 /* A vector of sub-properties. */
3216 int i;
3217 for (i = 0; i < ASIZE (prop); ++i)
3218 if (single_display_prop_string_p (AREF (prop, i), string))
3219 return 1;
3220 }
3221 else
3222 return single_display_prop_string_p (prop, string);
3223
3224 return 0;
3225 }
3226
3227
3228 /* Determine from which buffer position in W's buffer STRING comes
3229 from. AROUND_CHARPOS is an approximate position where it could
3230 be from. Value is the buffer position or 0 if it couldn't be
3231 determined.
3232
3233 W's buffer must be current.
3234
3235 This function is necessary because we don't record buffer positions
3236 in glyphs generated from strings (to keep struct glyph small).
3237 This function may only use code that doesn't eval because it is
3238 called asynchronously from note_mouse_highlight. */
3239
3240 int
3241 string_buffer_position (w, string, around_charpos)
3242 struct window *w;
3243 Lisp_Object string;
3244 int around_charpos;
3245 {
3246 Lisp_Object limit, prop, pos;
3247 const int MAX_DISTANCE = 1000;
3248 int found = 0;
3249
3250 pos = make_number (around_charpos);
3251 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3252 while (!found && !EQ (pos, limit))
3253 {
3254 prop = Fget_char_property (pos, Qdisplay, Qnil);
3255 if (!NILP (prop) && display_prop_string_p (prop, string))
3256 found = 1;
3257 else
3258 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3259 }
3260
3261 if (!found)
3262 {
3263 pos = make_number (around_charpos);
3264 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3265 while (!found && !EQ (pos, limit))
3266 {
3267 prop = Fget_char_property (pos, Qdisplay, Qnil);
3268 if (!NILP (prop) && display_prop_string_p (prop, string))
3269 found = 1;
3270 else
3271 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3272 limit);
3273 }
3274 }
3275
3276 return found ? XINT (pos) : 0;
3277 }
3278
3279
3280 \f
3281 /***********************************************************************
3282 `composition' property
3283 ***********************************************************************/
3284
3285 /* Set up iterator IT from `composition' property at its current
3286 position. Called from handle_stop. */
3287
3288 static enum prop_handled
3289 handle_composition_prop (it)
3290 struct it *it;
3291 {
3292 Lisp_Object prop, string;
3293 int pos, pos_byte, end;
3294 enum prop_handled handled = HANDLED_NORMALLY;
3295
3296 if (STRINGP (it->string))
3297 {
3298 pos = IT_STRING_CHARPOS (*it);
3299 pos_byte = IT_STRING_BYTEPOS (*it);
3300 string = it->string;
3301 }
3302 else
3303 {
3304 pos = IT_CHARPOS (*it);
3305 pos_byte = IT_BYTEPOS (*it);
3306 string = Qnil;
3307 }
3308
3309 /* If there's a valid composition and point is not inside of the
3310 composition (in the case that the composition is from the current
3311 buffer), draw a glyph composed from the composition components. */
3312 if (find_composition (pos, -1, &pos, &end, &prop, string)
3313 && COMPOSITION_VALID_P (pos, end, prop)
3314 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3315 {
3316 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3317
3318 if (id >= 0)
3319 {
3320 it->method = next_element_from_composition;
3321 it->cmp_id = id;
3322 it->cmp_len = COMPOSITION_LENGTH (prop);
3323 /* For a terminal, draw only the first character of the
3324 components. */
3325 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3326 it->len = (STRINGP (it->string)
3327 ? string_char_to_byte (it->string, end)
3328 : CHAR_TO_BYTE (end)) - pos_byte;
3329 it->stop_charpos = end;
3330 handled = HANDLED_RETURN;
3331 }
3332 }
3333
3334 return handled;
3335 }
3336
3337
3338 \f
3339 /***********************************************************************
3340 Overlay strings
3341 ***********************************************************************/
3342
3343 /* The following structure is used to record overlay strings for
3344 later sorting in load_overlay_strings. */
3345
3346 struct overlay_entry
3347 {
3348 Lisp_Object overlay;
3349 Lisp_Object string;
3350 int priority;
3351 int after_string_p;
3352 };
3353
3354
3355 /* Set up iterator IT from overlay strings at its current position.
3356 Called from handle_stop. */
3357
3358 static enum prop_handled
3359 handle_overlay_change (it)
3360 struct it *it;
3361 {
3362 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3363 return HANDLED_RECOMPUTE_PROPS;
3364 else
3365 return HANDLED_NORMALLY;
3366 }
3367
3368
3369 /* Set up the next overlay string for delivery by IT, if there is an
3370 overlay string to deliver. Called by set_iterator_to_next when the
3371 end of the current overlay string is reached. If there are more
3372 overlay strings to display, IT->string and
3373 IT->current.overlay_string_index are set appropriately here.
3374 Otherwise IT->string is set to nil. */
3375
3376 static void
3377 next_overlay_string (it)
3378 struct it *it;
3379 {
3380 ++it->current.overlay_string_index;
3381 if (it->current.overlay_string_index == it->n_overlay_strings)
3382 {
3383 /* No more overlay strings. Restore IT's settings to what
3384 they were before overlay strings were processed, and
3385 continue to deliver from current_buffer. */
3386 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3387
3388 pop_it (it);
3389 xassert (it->stop_charpos >= BEGV
3390 && it->stop_charpos <= it->end_charpos);
3391 it->string = Qnil;
3392 it->current.overlay_string_index = -1;
3393 SET_TEXT_POS (it->current.string_pos, -1, -1);
3394 it->n_overlay_strings = 0;
3395 it->method = next_element_from_buffer;
3396
3397 /* If we're at the end of the buffer, record that we have
3398 processed the overlay strings there already, so that
3399 next_element_from_buffer doesn't try it again. */
3400 if (IT_CHARPOS (*it) >= it->end_charpos)
3401 it->overlay_strings_at_end_processed_p = 1;
3402
3403 /* If we have to display `...' for invisible text, set
3404 the iterator up for that. */
3405 if (display_ellipsis_p)
3406 setup_for_ellipsis (it);
3407 }
3408 else
3409 {
3410 /* There are more overlay strings to process. If
3411 IT->current.overlay_string_index has advanced to a position
3412 where we must load IT->overlay_strings with more strings, do
3413 it. */
3414 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3415
3416 if (it->current.overlay_string_index && i == 0)
3417 load_overlay_strings (it, 0);
3418
3419 /* Initialize IT to deliver display elements from the overlay
3420 string. */
3421 it->string = it->overlay_strings[i];
3422 it->multibyte_p = STRING_MULTIBYTE (it->string);
3423 SET_TEXT_POS (it->current.string_pos, 0, 0);
3424 it->method = next_element_from_string;
3425 it->stop_charpos = 0;
3426 }
3427
3428 CHECK_IT (it);
3429 }
3430
3431
3432 /* Compare two overlay_entry structures E1 and E2. Used as a
3433 comparison function for qsort in load_overlay_strings. Overlay
3434 strings for the same position are sorted so that
3435
3436 1. All after-strings come in front of before-strings, except
3437 when they come from the same overlay.
3438
3439 2. Within after-strings, strings are sorted so that overlay strings
3440 from overlays with higher priorities come first.
3441
3442 2. Within before-strings, strings are sorted so that overlay
3443 strings from overlays with higher priorities come last.
3444
3445 Value is analogous to strcmp. */
3446
3447
3448 static int
3449 compare_overlay_entries (e1, e2)
3450 void *e1, *e2;
3451 {
3452 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3453 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3454 int result;
3455
3456 if (entry1->after_string_p != entry2->after_string_p)
3457 {
3458 /* Let after-strings appear in front of before-strings if
3459 they come from different overlays. */
3460 if (EQ (entry1->overlay, entry2->overlay))
3461 result = entry1->after_string_p ? 1 : -1;
3462 else
3463 result = entry1->after_string_p ? -1 : 1;
3464 }
3465 else if (entry1->after_string_p)
3466 /* After-strings sorted in order of decreasing priority. */
3467 result = entry2->priority - entry1->priority;
3468 else
3469 /* Before-strings sorted in order of increasing priority. */
3470 result = entry1->priority - entry2->priority;
3471
3472 return result;
3473 }
3474
3475
3476 /* Load the vector IT->overlay_strings with overlay strings from IT's
3477 current buffer position, or from CHARPOS if that is > 0. Set
3478 IT->n_overlays to the total number of overlay strings found.
3479
3480 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3481 a time. On entry into load_overlay_strings,
3482 IT->current.overlay_string_index gives the number of overlay
3483 strings that have already been loaded by previous calls to this
3484 function.
3485
3486 IT->add_overlay_start contains an additional overlay start
3487 position to consider for taking overlay strings from, if non-zero.
3488 This position comes into play when the overlay has an `invisible'
3489 property, and both before and after-strings. When we've skipped to
3490 the end of the overlay, because of its `invisible' property, we
3491 nevertheless want its before-string to appear.
3492 IT->add_overlay_start will contain the overlay start position
3493 in this case.
3494
3495 Overlay strings are sorted so that after-string strings come in
3496 front of before-string strings. Within before and after-strings,
3497 strings are sorted by overlay priority. See also function
3498 compare_overlay_entries. */
3499
3500 static void
3501 load_overlay_strings (it, charpos)
3502 struct it *it;
3503 int charpos;
3504 {
3505 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3506 Lisp_Object ov, overlay, window, str, invisible;
3507 int start, end;
3508 int size = 20;
3509 int n = 0, i, j, invis_p;
3510 struct overlay_entry *entries
3511 = (struct overlay_entry *) alloca (size * sizeof *entries);
3512
3513 if (charpos <= 0)
3514 charpos = IT_CHARPOS (*it);
3515
3516 /* Append the overlay string STRING of overlay OVERLAY to vector
3517 `entries' which has size `size' and currently contains `n'
3518 elements. AFTER_P non-zero means STRING is an after-string of
3519 OVERLAY. */
3520 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3521 do \
3522 { \
3523 Lisp_Object priority; \
3524 \
3525 if (n == size) \
3526 { \
3527 int new_size = 2 * size; \
3528 struct overlay_entry *old = entries; \
3529 entries = \
3530 (struct overlay_entry *) alloca (new_size \
3531 * sizeof *entries); \
3532 bcopy (old, entries, size * sizeof *entries); \
3533 size = new_size; \
3534 } \
3535 \
3536 entries[n].string = (STRING); \
3537 entries[n].overlay = (OVERLAY); \
3538 priority = Foverlay_get ((OVERLAY), Qpriority); \
3539 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3540 entries[n].after_string_p = (AFTER_P); \
3541 ++n; \
3542 } \
3543 while (0)
3544
3545 /* Process overlay before the overlay center. */
3546 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3547 {
3548 overlay = XCAR (ov);
3549 xassert (OVERLAYP (overlay));
3550 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3551 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3552
3553 if (end < charpos)
3554 break;
3555
3556 /* Skip this overlay if it doesn't start or end at IT's current
3557 position. */
3558 if (end != charpos && start != charpos)
3559 continue;
3560
3561 /* Skip this overlay if it doesn't apply to IT->w. */
3562 window = Foverlay_get (overlay, Qwindow);
3563 if (WINDOWP (window) && XWINDOW (window) != it->w)
3564 continue;
3565
3566 /* If the text ``under'' the overlay is invisible, both before-
3567 and after-strings from this overlay are visible; start and
3568 end position are indistinguishable. */
3569 invisible = Foverlay_get (overlay, Qinvisible);
3570 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3571
3572 /* If overlay has a non-empty before-string, record it. */
3573 if ((start == charpos || (end == charpos && invis_p))
3574 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3575 && SCHARS (str))
3576 RECORD_OVERLAY_STRING (overlay, str, 0);
3577
3578 /* If overlay has a non-empty after-string, record it. */
3579 if ((end == charpos || (start == charpos && invis_p))
3580 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3581 && SCHARS (str))
3582 RECORD_OVERLAY_STRING (overlay, str, 1);
3583 }
3584
3585 /* Process overlays after the overlay center. */
3586 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3587 {
3588 overlay = XCAR (ov);
3589 xassert (OVERLAYP (overlay));
3590 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3591 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3592
3593 if (start > charpos)
3594 break;
3595
3596 /* Skip this overlay if it doesn't start or end at IT's current
3597 position. */
3598 if (end != charpos && start != charpos)
3599 continue;
3600
3601 /* Skip this overlay if it doesn't apply to IT->w. */
3602 window = Foverlay_get (overlay, Qwindow);
3603 if (WINDOWP (window) && XWINDOW (window) != it->w)
3604 continue;
3605
3606 /* If the text ``under'' the overlay is invisible, it has a zero
3607 dimension, and both before- and after-strings apply. */
3608 invisible = Foverlay_get (overlay, Qinvisible);
3609 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3610
3611 /* If overlay has a non-empty before-string, record it. */
3612 if ((start == charpos || (end == charpos && invis_p))
3613 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3614 && SCHARS (str))
3615 RECORD_OVERLAY_STRING (overlay, str, 0);
3616
3617 /* If overlay has a non-empty after-string, record it. */
3618 if ((end == charpos || (start == charpos && invis_p))
3619 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3620 && SCHARS (str))
3621 RECORD_OVERLAY_STRING (overlay, str, 1);
3622 }
3623
3624 #undef RECORD_OVERLAY_STRING
3625
3626 /* Sort entries. */
3627 if (n > 1)
3628 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3629
3630 /* Record the total number of strings to process. */
3631 it->n_overlay_strings = n;
3632
3633 /* IT->current.overlay_string_index is the number of overlay strings
3634 that have already been consumed by IT. Copy some of the
3635 remaining overlay strings to IT->overlay_strings. */
3636 i = 0;
3637 j = it->current.overlay_string_index;
3638 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3639 it->overlay_strings[i++] = entries[j++].string;
3640
3641 CHECK_IT (it);
3642 }
3643
3644
3645 /* Get the first chunk of overlay strings at IT's current buffer
3646 position, or at CHARPOS if that is > 0. Value is non-zero if at
3647 least one overlay string was found. */
3648
3649 static int
3650 get_overlay_strings (it, charpos)
3651 struct it *it;
3652 int charpos;
3653 {
3654 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3655 process. This fills IT->overlay_strings with strings, and sets
3656 IT->n_overlay_strings to the total number of strings to process.
3657 IT->pos.overlay_string_index has to be set temporarily to zero
3658 because load_overlay_strings needs this; it must be set to -1
3659 when no overlay strings are found because a zero value would
3660 indicate a position in the first overlay string. */
3661 it->current.overlay_string_index = 0;
3662 load_overlay_strings (it, charpos);
3663
3664 /* If we found overlay strings, set up IT to deliver display
3665 elements from the first one. Otherwise set up IT to deliver
3666 from current_buffer. */
3667 if (it->n_overlay_strings)
3668 {
3669 /* Make sure we know settings in current_buffer, so that we can
3670 restore meaningful values when we're done with the overlay
3671 strings. */
3672 compute_stop_pos (it);
3673 xassert (it->face_id >= 0);
3674
3675 /* Save IT's settings. They are restored after all overlay
3676 strings have been processed. */
3677 xassert (it->sp == 0);
3678 push_it (it);
3679
3680 /* Set up IT to deliver display elements from the first overlay
3681 string. */
3682 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3683 it->string = it->overlay_strings[0];
3684 it->stop_charpos = 0;
3685 xassert (STRINGP (it->string));
3686 it->end_charpos = SCHARS (it->string);
3687 it->multibyte_p = SMBP (it->string);
3688 it->method = next_element_from_string;
3689 }
3690 else
3691 {
3692 it->string = Qnil;
3693 it->current.overlay_string_index = -1;
3694 it->method = next_element_from_buffer;
3695 }
3696
3697 CHECK_IT (it);
3698
3699 /* Value is non-zero if we found at least one overlay string. */
3700 return STRINGP (it->string);
3701 }
3702
3703
3704 \f
3705 /***********************************************************************
3706 Saving and restoring state
3707 ***********************************************************************/
3708
3709 /* Save current settings of IT on IT->stack. Called, for example,
3710 before setting up IT for an overlay string, to be able to restore
3711 IT's settings to what they were after the overlay string has been
3712 processed. */
3713
3714 static void
3715 push_it (it)
3716 struct it *it;
3717 {
3718 struct iterator_stack_entry *p;
3719
3720 xassert (it->sp < 2);
3721 p = it->stack + it->sp;
3722
3723 p->stop_charpos = it->stop_charpos;
3724 xassert (it->face_id >= 0);
3725 p->face_id = it->face_id;
3726 p->string = it->string;
3727 p->pos = it->current;
3728 p->end_charpos = it->end_charpos;
3729 p->string_nchars = it->string_nchars;
3730 p->area = it->area;
3731 p->multibyte_p = it->multibyte_p;
3732 p->space_width = it->space_width;
3733 p->font_height = it->font_height;
3734 p->voffset = it->voffset;
3735 p->string_from_display_prop_p = it->string_from_display_prop_p;
3736 p->display_ellipsis_p = 0;
3737 ++it->sp;
3738 }
3739
3740
3741 /* Restore IT's settings from IT->stack. Called, for example, when no
3742 more overlay strings must be processed, and we return to delivering
3743 display elements from a buffer, or when the end of a string from a
3744 `display' property is reached and we return to delivering display
3745 elements from an overlay string, or from a buffer. */
3746
3747 static void
3748 pop_it (it)
3749 struct it *it;
3750 {
3751 struct iterator_stack_entry *p;
3752
3753 xassert (it->sp > 0);
3754 --it->sp;
3755 p = it->stack + it->sp;
3756 it->stop_charpos = p->stop_charpos;
3757 it->face_id = p->face_id;
3758 it->string = p->string;
3759 it->current = p->pos;
3760 it->end_charpos = p->end_charpos;
3761 it->string_nchars = p->string_nchars;
3762 it->area = p->area;
3763 it->multibyte_p = p->multibyte_p;
3764 it->space_width = p->space_width;
3765 it->font_height = p->font_height;
3766 it->voffset = p->voffset;
3767 it->string_from_display_prop_p = p->string_from_display_prop_p;
3768 }
3769
3770
3771 \f
3772 /***********************************************************************
3773 Moving over lines
3774 ***********************************************************************/
3775
3776 /* Set IT's current position to the previous line start. */
3777
3778 static void
3779 back_to_previous_line_start (it)
3780 struct it *it;
3781 {
3782 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3783 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3784 }
3785
3786
3787 /* Move IT to the next line start.
3788
3789 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3790 we skipped over part of the text (as opposed to moving the iterator
3791 continuously over the text). Otherwise, don't change the value
3792 of *SKIPPED_P.
3793
3794 Newlines may come from buffer text, overlay strings, or strings
3795 displayed via the `display' property. That's the reason we can't
3796 simply use find_next_newline_no_quit.
3797
3798 Note that this function may not skip over invisible text that is so
3799 because of text properties and immediately follows a newline. If
3800 it would, function reseat_at_next_visible_line_start, when called
3801 from set_iterator_to_next, would effectively make invisible
3802 characters following a newline part of the wrong glyph row, which
3803 leads to wrong cursor motion. */
3804
3805 static int
3806 forward_to_next_line_start (it, skipped_p)
3807 struct it *it;
3808 int *skipped_p;
3809 {
3810 int old_selective, newline_found_p, n;
3811 const int MAX_NEWLINE_DISTANCE = 500;
3812
3813 /* If already on a newline, just consume it to avoid unintended
3814 skipping over invisible text below. */
3815 if (it->what == IT_CHARACTER
3816 && it->c == '\n'
3817 && CHARPOS (it->position) == IT_CHARPOS (*it))
3818 {
3819 set_iterator_to_next (it, 0);
3820 it->c = 0;
3821 return 1;
3822 }
3823
3824 /* Don't handle selective display in the following. It's (a)
3825 unnecessary because it's done by the caller, and (b) leads to an
3826 infinite recursion because next_element_from_ellipsis indirectly
3827 calls this function. */
3828 old_selective = it->selective;
3829 it->selective = 0;
3830
3831 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3832 from buffer text. */
3833 for (n = newline_found_p = 0;
3834 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3835 n += STRINGP (it->string) ? 0 : 1)
3836 {
3837 if (!get_next_display_element (it))
3838 break;
3839 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3840 set_iterator_to_next (it, 0);
3841 }
3842
3843 /* If we didn't find a newline near enough, see if we can use a
3844 short-cut. */
3845 if (n == MAX_NEWLINE_DISTANCE)
3846 {
3847 int start = IT_CHARPOS (*it);
3848 int limit = find_next_newline_no_quit (start, 1);
3849 Lisp_Object pos;
3850
3851 xassert (!STRINGP (it->string));
3852
3853 /* If there isn't any `display' property in sight, and no
3854 overlays, we can just use the position of the newline in
3855 buffer text. */
3856 if (it->stop_charpos >= limit
3857 || ((pos = Fnext_single_property_change (make_number (start),
3858 Qdisplay,
3859 Qnil, make_number (limit)),
3860 NILP (pos))
3861 && next_overlay_change (start) == ZV))
3862 {
3863 IT_CHARPOS (*it) = limit;
3864 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3865 *skipped_p = newline_found_p = 1;
3866 }
3867 else
3868 {
3869 while (get_next_display_element (it)
3870 && !newline_found_p)
3871 {
3872 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3873 set_iterator_to_next (it, 0);
3874 }
3875 }
3876 }
3877
3878 it->selective = old_selective;
3879 return newline_found_p;
3880 }
3881
3882
3883 /* Set IT's current position to the previous visible line start. Skip
3884 invisible text that is so either due to text properties or due to
3885 selective display. Caution: this does not change IT->current_x and
3886 IT->hpos. */
3887
3888 static void
3889 back_to_previous_visible_line_start (it)
3890 struct it *it;
3891 {
3892 int visible_p = 0;
3893
3894 /* Go back one newline if not on BEGV already. */
3895 if (IT_CHARPOS (*it) > BEGV)
3896 back_to_previous_line_start (it);
3897
3898 /* Move over lines that are invisible because of selective display
3899 or text properties. */
3900 while (IT_CHARPOS (*it) > BEGV
3901 && !visible_p)
3902 {
3903 visible_p = 1;
3904
3905 /* If selective > 0, then lines indented more than that values
3906 are invisible. */
3907 if (it->selective > 0
3908 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3909 it->selective))
3910 visible_p = 0;
3911 else
3912 {
3913 Lisp_Object prop;
3914
3915 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3916 Qinvisible, it->window);
3917 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3918 visible_p = 0;
3919 }
3920
3921 /* Back one more newline if the current one is invisible. */
3922 if (!visible_p)
3923 back_to_previous_line_start (it);
3924 }
3925
3926 xassert (IT_CHARPOS (*it) >= BEGV);
3927 xassert (IT_CHARPOS (*it) == BEGV
3928 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3929 CHECK_IT (it);
3930 }
3931
3932
3933 /* Reseat iterator IT at the previous visible line start. Skip
3934 invisible text that is so either due to text properties or due to
3935 selective display. At the end, update IT's overlay information,
3936 face information etc. */
3937
3938 static void
3939 reseat_at_previous_visible_line_start (it)
3940 struct it *it;
3941 {
3942 back_to_previous_visible_line_start (it);
3943 reseat (it, it->current.pos, 1);
3944 CHECK_IT (it);
3945 }
3946
3947
3948 /* Reseat iterator IT on the next visible line start in the current
3949 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3950 preceding the line start. Skip over invisible text that is so
3951 because of selective display. Compute faces, overlays etc at the
3952 new position. Note that this function does not skip over text that
3953 is invisible because of text properties. */
3954
3955 static void
3956 reseat_at_next_visible_line_start (it, on_newline_p)
3957 struct it *it;
3958 int on_newline_p;
3959 {
3960 int newline_found_p, skipped_p = 0;
3961
3962 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3963
3964 /* Skip over lines that are invisible because they are indented
3965 more than the value of IT->selective. */
3966 if (it->selective > 0)
3967 while (IT_CHARPOS (*it) < ZV
3968 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3969 it->selective))
3970 {
3971 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3972 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3973 }
3974
3975 /* Position on the newline if that's what's requested. */
3976 if (on_newline_p && newline_found_p)
3977 {
3978 if (STRINGP (it->string))
3979 {
3980 if (IT_STRING_CHARPOS (*it) > 0)
3981 {
3982 --IT_STRING_CHARPOS (*it);
3983 --IT_STRING_BYTEPOS (*it);
3984 }
3985 }
3986 else if (IT_CHARPOS (*it) > BEGV)
3987 {
3988 --IT_CHARPOS (*it);
3989 --IT_BYTEPOS (*it);
3990 reseat (it, it->current.pos, 0);
3991 }
3992 }
3993 else if (skipped_p)
3994 reseat (it, it->current.pos, 0);
3995
3996 CHECK_IT (it);
3997 }
3998
3999
4000 \f
4001 /***********************************************************************
4002 Changing an iterator's position
4003 ***********************************************************************/
4004
4005 /* Change IT's current position to POS in current_buffer. If FORCE_P
4006 is non-zero, always check for text properties at the new position.
4007 Otherwise, text properties are only looked up if POS >=
4008 IT->check_charpos of a property. */
4009
4010 static void
4011 reseat (it, pos, force_p)
4012 struct it *it;
4013 struct text_pos pos;
4014 int force_p;
4015 {
4016 int original_pos = IT_CHARPOS (*it);
4017
4018 reseat_1 (it, pos, 0);
4019
4020 /* Determine where to check text properties. Avoid doing it
4021 where possible because text property lookup is very expensive. */
4022 if (force_p
4023 || CHARPOS (pos) > it->stop_charpos
4024 || CHARPOS (pos) < original_pos)
4025 handle_stop (it);
4026
4027 CHECK_IT (it);
4028 }
4029
4030
4031 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4032 IT->stop_pos to POS, also. */
4033
4034 static void
4035 reseat_1 (it, pos, set_stop_p)
4036 struct it *it;
4037 struct text_pos pos;
4038 int set_stop_p;
4039 {
4040 /* Don't call this function when scanning a C string. */
4041 xassert (it->s == NULL);
4042
4043 /* POS must be a reasonable value. */
4044 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4045
4046 it->current.pos = it->position = pos;
4047 XSETBUFFER (it->object, current_buffer);
4048 it->end_charpos = ZV;
4049 it->dpvec = NULL;
4050 it->current.dpvec_index = -1;
4051 it->current.overlay_string_index = -1;
4052 IT_STRING_CHARPOS (*it) = -1;
4053 IT_STRING_BYTEPOS (*it) = -1;
4054 it->string = Qnil;
4055 it->method = next_element_from_buffer;
4056 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4057 it->sp = 0;
4058 it->face_before_selective_p = 0;
4059
4060 if (set_stop_p)
4061 it->stop_charpos = CHARPOS (pos);
4062 }
4063
4064
4065 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4066 If S is non-null, it is a C string to iterate over. Otherwise,
4067 STRING gives a Lisp string to iterate over.
4068
4069 If PRECISION > 0, don't return more then PRECISION number of
4070 characters from the string.
4071
4072 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4073 characters have been returned. FIELD_WIDTH < 0 means an infinite
4074 field width.
4075
4076 MULTIBYTE = 0 means disable processing of multibyte characters,
4077 MULTIBYTE > 0 means enable it,
4078 MULTIBYTE < 0 means use IT->multibyte_p.
4079
4080 IT must be initialized via a prior call to init_iterator before
4081 calling this function. */
4082
4083 static void
4084 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4085 struct it *it;
4086 unsigned char *s;
4087 Lisp_Object string;
4088 int charpos;
4089 int precision, field_width, multibyte;
4090 {
4091 /* No region in strings. */
4092 it->region_beg_charpos = it->region_end_charpos = -1;
4093
4094 /* No text property checks performed by default, but see below. */
4095 it->stop_charpos = -1;
4096
4097 /* Set iterator position and end position. */
4098 bzero (&it->current, sizeof it->current);
4099 it->current.overlay_string_index = -1;
4100 it->current.dpvec_index = -1;
4101 xassert (charpos >= 0);
4102
4103 /* If STRING is specified, use its multibyteness, otherwise use the
4104 setting of MULTIBYTE, if specified. */
4105 if (multibyte >= 0)
4106 it->multibyte_p = multibyte > 0;
4107
4108 if (s == NULL)
4109 {
4110 xassert (STRINGP (string));
4111 it->string = string;
4112 it->s = NULL;
4113 it->end_charpos = it->string_nchars = SCHARS (string);
4114 it->method = next_element_from_string;
4115 it->current.string_pos = string_pos (charpos, string);
4116 }
4117 else
4118 {
4119 it->s = s;
4120 it->string = Qnil;
4121
4122 /* Note that we use IT->current.pos, not it->current.string_pos,
4123 for displaying C strings. */
4124 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4125 if (it->multibyte_p)
4126 {
4127 it->current.pos = c_string_pos (charpos, s, 1);
4128 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4129 }
4130 else
4131 {
4132 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4133 it->end_charpos = it->string_nchars = strlen (s);
4134 }
4135
4136 it->method = next_element_from_c_string;
4137 }
4138
4139 /* PRECISION > 0 means don't return more than PRECISION characters
4140 from the string. */
4141 if (precision > 0 && it->end_charpos - charpos > precision)
4142 it->end_charpos = it->string_nchars = charpos + precision;
4143
4144 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4145 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4146 FIELD_WIDTH < 0 means infinite field width. This is useful for
4147 padding with `-' at the end of a mode line. */
4148 if (field_width < 0)
4149 field_width = INFINITY;
4150 if (field_width > it->end_charpos - charpos)
4151 it->end_charpos = charpos + field_width;
4152
4153 /* Use the standard display table for displaying strings. */
4154 if (DISP_TABLE_P (Vstandard_display_table))
4155 it->dp = XCHAR_TABLE (Vstandard_display_table);
4156
4157 it->stop_charpos = charpos;
4158 CHECK_IT (it);
4159 }
4160
4161
4162 \f
4163 /***********************************************************************
4164 Iteration
4165 ***********************************************************************/
4166
4167 /* Load IT's display element fields with information about the next
4168 display element from the current position of IT. Value is zero if
4169 end of buffer (or C string) is reached. */
4170
4171 int
4172 get_next_display_element (it)
4173 struct it *it;
4174 {
4175 /* Non-zero means that we found an display element. Zero means that
4176 we hit the end of what we iterate over. Performance note: the
4177 function pointer `method' used here turns out to be faster than
4178 using a sequence of if-statements. */
4179 int success_p = (*it->method) (it);
4180
4181 if (it->what == IT_CHARACTER)
4182 {
4183 /* Map via display table or translate control characters.
4184 IT->c, IT->len etc. have been set to the next character by
4185 the function call above. If we have a display table, and it
4186 contains an entry for IT->c, translate it. Don't do this if
4187 IT->c itself comes from a display table, otherwise we could
4188 end up in an infinite recursion. (An alternative could be to
4189 count the recursion depth of this function and signal an
4190 error when a certain maximum depth is reached.) Is it worth
4191 it? */
4192 if (success_p && it->dpvec == NULL)
4193 {
4194 Lisp_Object dv;
4195
4196 if (it->dp
4197 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4198 VECTORP (dv)))
4199 {
4200 struct Lisp_Vector *v = XVECTOR (dv);
4201
4202 /* Return the first character from the display table
4203 entry, if not empty. If empty, don't display the
4204 current character. */
4205 if (v->size)
4206 {
4207 it->dpvec_char_len = it->len;
4208 it->dpvec = v->contents;
4209 it->dpend = v->contents + v->size;
4210 it->current.dpvec_index = 0;
4211 it->method = next_element_from_display_vector;
4212 success_p = get_next_display_element (it);
4213 }
4214 else
4215 {
4216 set_iterator_to_next (it, 0);
4217 success_p = get_next_display_element (it);
4218 }
4219 }
4220
4221 /* Translate control characters into `\003' or `^C' form.
4222 Control characters coming from a display table entry are
4223 currently not translated because we use IT->dpvec to hold
4224 the translation. This could easily be changed but I
4225 don't believe that it is worth doing.
4226
4227 Non-printable multibyte characters are also translated
4228 octal form. */
4229 else if ((it->c < ' '
4230 && (it->area != TEXT_AREA
4231 || (it->c != '\n' && it->c != '\t')))
4232 || (it->c >= 127
4233 && it->len == 1)
4234 || !CHAR_PRINTABLE_P (it->c))
4235 {
4236 /* IT->c is a control character which must be displayed
4237 either as '\003' or as `^C' where the '\\' and '^'
4238 can be defined in the display table. Fill
4239 IT->ctl_chars with glyphs for what we have to
4240 display. Then, set IT->dpvec to these glyphs. */
4241 GLYPH g;
4242
4243 if (it->c < 128 && it->ctl_arrow_p)
4244 {
4245 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4246 if (it->dp
4247 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4248 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4249 g = XINT (DISP_CTRL_GLYPH (it->dp));
4250 else
4251 g = FAST_MAKE_GLYPH ('^', 0);
4252 XSETINT (it->ctl_chars[0], g);
4253
4254 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4255 XSETINT (it->ctl_chars[1], g);
4256
4257 /* Set up IT->dpvec and return first character from it. */
4258 it->dpvec_char_len = it->len;
4259 it->dpvec = it->ctl_chars;
4260 it->dpend = it->dpvec + 2;
4261 it->current.dpvec_index = 0;
4262 it->method = next_element_from_display_vector;
4263 get_next_display_element (it);
4264 }
4265 else
4266 {
4267 unsigned char str[MAX_MULTIBYTE_LENGTH];
4268 int len;
4269 int i;
4270 GLYPH escape_glyph;
4271
4272 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4273 if (it->dp
4274 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4275 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4276 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4277 else
4278 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4279
4280 if (SINGLE_BYTE_CHAR_P (it->c))
4281 str[0] = it->c, len = 1;
4282 else
4283 {
4284 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4285 if (len < 0)
4286 {
4287 /* It's an invalid character, which
4288 shouldn't happen actually, but due to
4289 bugs it may happen. Let's print the char
4290 as is, there's not much meaningful we can
4291 do with it. */
4292 str[0] = it->c;
4293 str[1] = it->c >> 8;
4294 str[2] = it->c >> 16;
4295 str[3] = it->c >> 24;
4296 len = 4;
4297 }
4298 }
4299
4300 for (i = 0; i < len; i++)
4301 {
4302 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4303 /* Insert three more glyphs into IT->ctl_chars for
4304 the octal display of the character. */
4305 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4306 XSETINT (it->ctl_chars[i * 4 + 1], g);
4307 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4308 XSETINT (it->ctl_chars[i * 4 + 2], g);
4309 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4310 XSETINT (it->ctl_chars[i * 4 + 3], g);
4311 }
4312
4313 /* Set up IT->dpvec and return the first character
4314 from it. */
4315 it->dpvec_char_len = it->len;
4316 it->dpvec = it->ctl_chars;
4317 it->dpend = it->dpvec + len * 4;
4318 it->current.dpvec_index = 0;
4319 it->method = next_element_from_display_vector;
4320 get_next_display_element (it);
4321 }
4322 }
4323 }
4324
4325 /* Adjust face id for a multibyte character. There are no
4326 multibyte character in unibyte text. */
4327 if (it->multibyte_p
4328 && success_p
4329 && FRAME_WINDOW_P (it->f))
4330 {
4331 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4332 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4333 }
4334 }
4335
4336 /* Is this character the last one of a run of characters with
4337 box? If yes, set IT->end_of_box_run_p to 1. */
4338 if (it->face_box_p
4339 && it->s == NULL)
4340 {
4341 int face_id;
4342 struct face *face;
4343
4344 it->end_of_box_run_p
4345 = ((face_id = face_after_it_pos (it),
4346 face_id != it->face_id)
4347 && (face = FACE_FROM_ID (it->f, face_id),
4348 face->box == FACE_NO_BOX));
4349 }
4350
4351 /* Value is 0 if end of buffer or string reached. */
4352 return success_p;
4353 }
4354
4355
4356 /* Move IT to the next display element.
4357
4358 RESEAT_P non-zero means if called on a newline in buffer text,
4359 skip to the next visible line start.
4360
4361 Functions get_next_display_element and set_iterator_to_next are
4362 separate because I find this arrangement easier to handle than a
4363 get_next_display_element function that also increments IT's
4364 position. The way it is we can first look at an iterator's current
4365 display element, decide whether it fits on a line, and if it does,
4366 increment the iterator position. The other way around we probably
4367 would either need a flag indicating whether the iterator has to be
4368 incremented the next time, or we would have to implement a
4369 decrement position function which would not be easy to write. */
4370
4371 void
4372 set_iterator_to_next (it, reseat_p)
4373 struct it *it;
4374 int reseat_p;
4375 {
4376 /* Reset flags indicating start and end of a sequence of characters
4377 with box. Reset them at the start of this function because
4378 moving the iterator to a new position might set them. */
4379 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4380
4381 if (it->method == next_element_from_buffer)
4382 {
4383 /* The current display element of IT is a character from
4384 current_buffer. Advance in the buffer, and maybe skip over
4385 invisible lines that are so because of selective display. */
4386 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4387 reseat_at_next_visible_line_start (it, 0);
4388 else
4389 {
4390 xassert (it->len != 0);
4391 IT_BYTEPOS (*it) += it->len;
4392 IT_CHARPOS (*it) += 1;
4393 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4394 }
4395 }
4396 else if (it->method == next_element_from_composition)
4397 {
4398 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4399 if (STRINGP (it->string))
4400 {
4401 IT_STRING_BYTEPOS (*it) += it->len;
4402 IT_STRING_CHARPOS (*it) += it->cmp_len;
4403 it->method = next_element_from_string;
4404 goto consider_string_end;
4405 }
4406 else
4407 {
4408 IT_BYTEPOS (*it) += it->len;
4409 IT_CHARPOS (*it) += it->cmp_len;
4410 it->method = next_element_from_buffer;
4411 }
4412 }
4413 else if (it->method == next_element_from_c_string)
4414 {
4415 /* Current display element of IT is from a C string. */
4416 IT_BYTEPOS (*it) += it->len;
4417 IT_CHARPOS (*it) += 1;
4418 }
4419 else if (it->method == next_element_from_display_vector)
4420 {
4421 /* Current display element of IT is from a display table entry.
4422 Advance in the display table definition. Reset it to null if
4423 end reached, and continue with characters from buffers/
4424 strings. */
4425 ++it->current.dpvec_index;
4426
4427 /* Restore face of the iterator to what they were before the
4428 display vector entry (these entries may contain faces). */
4429 it->face_id = it->saved_face_id;
4430
4431 if (it->dpvec + it->current.dpvec_index == it->dpend)
4432 {
4433 if (it->s)
4434 it->method = next_element_from_c_string;
4435 else if (STRINGP (it->string))
4436 it->method = next_element_from_string;
4437 else
4438 it->method = next_element_from_buffer;
4439
4440 it->dpvec = NULL;
4441 it->current.dpvec_index = -1;
4442
4443 /* Skip over characters which were displayed via IT->dpvec. */
4444 if (it->dpvec_char_len < 0)
4445 reseat_at_next_visible_line_start (it, 1);
4446 else if (it->dpvec_char_len > 0)
4447 {
4448 it->len = it->dpvec_char_len;
4449 set_iterator_to_next (it, reseat_p);
4450 }
4451 }
4452 }
4453 else if (it->method == next_element_from_string)
4454 {
4455 /* Current display element is a character from a Lisp string. */
4456 xassert (it->s == NULL && STRINGP (it->string));
4457 IT_STRING_BYTEPOS (*it) += it->len;
4458 IT_STRING_CHARPOS (*it) += 1;
4459
4460 consider_string_end:
4461
4462 if (it->current.overlay_string_index >= 0)
4463 {
4464 /* IT->string is an overlay string. Advance to the
4465 next, if there is one. */
4466 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4467 next_overlay_string (it);
4468 }
4469 else
4470 {
4471 /* IT->string is not an overlay string. If we reached
4472 its end, and there is something on IT->stack, proceed
4473 with what is on the stack. This can be either another
4474 string, this time an overlay string, or a buffer. */
4475 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
4476 && it->sp > 0)
4477 {
4478 pop_it (it);
4479 if (!STRINGP (it->string))
4480 it->method = next_element_from_buffer;
4481 else
4482 goto consider_string_end;
4483 }
4484 }
4485 }
4486 else if (it->method == next_element_from_image
4487 || it->method == next_element_from_stretch)
4488 {
4489 /* The position etc with which we have to proceed are on
4490 the stack. The position may be at the end of a string,
4491 if the `display' property takes up the whole string. */
4492 pop_it (it);
4493 it->image_id = 0;
4494 if (STRINGP (it->string))
4495 {
4496 it->method = next_element_from_string;
4497 goto consider_string_end;
4498 }
4499 else
4500 it->method = next_element_from_buffer;
4501 }
4502 else
4503 /* There are no other methods defined, so this should be a bug. */
4504 abort ();
4505
4506 xassert (it->method != next_element_from_string
4507 || (STRINGP (it->string)
4508 && IT_STRING_CHARPOS (*it) >= 0));
4509 }
4510
4511
4512 /* Load IT's display element fields with information about the next
4513 display element which comes from a display table entry or from the
4514 result of translating a control character to one of the forms `^C'
4515 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4516
4517 static int
4518 next_element_from_display_vector (it)
4519 struct it *it;
4520 {
4521 /* Precondition. */
4522 xassert (it->dpvec && it->current.dpvec_index >= 0);
4523
4524 /* Remember the current face id in case glyphs specify faces.
4525 IT's face is restored in set_iterator_to_next. */
4526 it->saved_face_id = it->face_id;
4527
4528 if (INTEGERP (*it->dpvec)
4529 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4530 {
4531 int lface_id;
4532 GLYPH g;
4533
4534 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4535 it->c = FAST_GLYPH_CHAR (g);
4536 it->len = CHAR_BYTES (it->c);
4537
4538 /* The entry may contain a face id to use. Such a face id is
4539 the id of a Lisp face, not a realized face. A face id of
4540 zero means no face is specified. */
4541 lface_id = FAST_GLYPH_FACE (g);
4542 if (lface_id)
4543 {
4544 /* The function returns -1 if lface_id is invalid. */
4545 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4546 if (face_id >= 0)
4547 it->face_id = face_id;
4548 }
4549 }
4550 else
4551 /* Display table entry is invalid. Return a space. */
4552 it->c = ' ', it->len = 1;
4553
4554 /* Don't change position and object of the iterator here. They are
4555 still the values of the character that had this display table
4556 entry or was translated, and that's what we want. */
4557 it->what = IT_CHARACTER;
4558 return 1;
4559 }
4560
4561
4562 /* Load IT with the next display element from Lisp string IT->string.
4563 IT->current.string_pos is the current position within the string.
4564 If IT->current.overlay_string_index >= 0, the Lisp string is an
4565 overlay string. */
4566
4567 static int
4568 next_element_from_string (it)
4569 struct it *it;
4570 {
4571 struct text_pos position;
4572
4573 xassert (STRINGP (it->string));
4574 xassert (IT_STRING_CHARPOS (*it) >= 0);
4575 position = it->current.string_pos;
4576
4577 /* Time to check for invisible text? */
4578 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4579 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4580 {
4581 handle_stop (it);
4582
4583 /* Since a handler may have changed IT->method, we must
4584 recurse here. */
4585 return get_next_display_element (it);
4586 }
4587
4588 if (it->current.overlay_string_index >= 0)
4589 {
4590 /* Get the next character from an overlay string. In overlay
4591 strings, There is no field width or padding with spaces to
4592 do. */
4593 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4594 {
4595 it->what = IT_EOB;
4596 return 0;
4597 }
4598 else if (STRING_MULTIBYTE (it->string))
4599 {
4600 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4601 unsigned char *s = SDATA (it->string) + IT_STRING_BYTEPOS (*it);
4602 it->c = string_char_and_length (s, remaining, &it->len);
4603 }
4604 else
4605 {
4606 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4607 it->len = 1;
4608 }
4609 }
4610 else
4611 {
4612 /* Get the next character from a Lisp string that is not an
4613 overlay string. Such strings come from the mode line, for
4614 example. We may have to pad with spaces, or truncate the
4615 string. See also next_element_from_c_string. */
4616 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4617 {
4618 it->what = IT_EOB;
4619 return 0;
4620 }
4621 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4622 {
4623 /* Pad with spaces. */
4624 it->c = ' ', it->len = 1;
4625 CHARPOS (position) = BYTEPOS (position) = -1;
4626 }
4627 else if (STRING_MULTIBYTE (it->string))
4628 {
4629 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4630 unsigned char *s = SDATA (it->string) + IT_STRING_BYTEPOS (*it);
4631 it->c = string_char_and_length (s, maxlen, &it->len);
4632 }
4633 else
4634 {
4635 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4636 it->len = 1;
4637 }
4638 }
4639
4640 /* Record what we have and where it came from. Note that we store a
4641 buffer position in IT->position although it could arguably be a
4642 string position. */
4643 it->what = IT_CHARACTER;
4644 it->object = it->string;
4645 it->position = position;
4646 return 1;
4647 }
4648
4649
4650 /* Load IT with next display element from C string IT->s.
4651 IT->string_nchars is the maximum number of characters to return
4652 from the string. IT->end_charpos may be greater than
4653 IT->string_nchars when this function is called, in which case we
4654 may have to return padding spaces. Value is zero if end of string
4655 reached, including padding spaces. */
4656
4657 static int
4658 next_element_from_c_string (it)
4659 struct it *it;
4660 {
4661 int success_p = 1;
4662
4663 xassert (it->s);
4664 it->what = IT_CHARACTER;
4665 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4666 it->object = Qnil;
4667
4668 /* IT's position can be greater IT->string_nchars in case a field
4669 width or precision has been specified when the iterator was
4670 initialized. */
4671 if (IT_CHARPOS (*it) >= it->end_charpos)
4672 {
4673 /* End of the game. */
4674 it->what = IT_EOB;
4675 success_p = 0;
4676 }
4677 else if (IT_CHARPOS (*it) >= it->string_nchars)
4678 {
4679 /* Pad with spaces. */
4680 it->c = ' ', it->len = 1;
4681 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4682 }
4683 else if (it->multibyte_p)
4684 {
4685 /* Implementation note: The calls to strlen apparently aren't a
4686 performance problem because there is no noticeable performance
4687 difference between Emacs running in unibyte or multibyte mode. */
4688 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4689 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4690 maxlen, &it->len);
4691 }
4692 else
4693 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4694
4695 return success_p;
4696 }
4697
4698
4699 /* Set up IT to return characters from an ellipsis, if appropriate.
4700 The definition of the ellipsis glyphs may come from a display table
4701 entry. This function Fills IT with the first glyph from the
4702 ellipsis if an ellipsis is to be displayed. */
4703
4704 static int
4705 next_element_from_ellipsis (it)
4706 struct it *it;
4707 {
4708 if (it->selective_display_ellipsis_p)
4709 {
4710 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4711 {
4712 /* Use the display table definition for `...'. Invalid glyphs
4713 will be handled by the method returning elements from dpvec. */
4714 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4715 it->dpvec_char_len = it->len;
4716 it->dpvec = v->contents;
4717 it->dpend = v->contents + v->size;
4718 it->current.dpvec_index = 0;
4719 it->method = next_element_from_display_vector;
4720 }
4721 else
4722 {
4723 /* Use default `...' which is stored in default_invis_vector. */
4724 it->dpvec_char_len = it->len;
4725 it->dpvec = default_invis_vector;
4726 it->dpend = default_invis_vector + 3;
4727 it->current.dpvec_index = 0;
4728 it->method = next_element_from_display_vector;
4729 }
4730 }
4731 else
4732 {
4733 /* The face at the current position may be different from the
4734 face we find after the invisible text. Remember what it
4735 was in IT->saved_face_id, and signal that it's there by
4736 setting face_before_selective_p. */
4737 it->saved_face_id = it->face_id;
4738 it->method = next_element_from_buffer;
4739 reseat_at_next_visible_line_start (it, 1);
4740 it->face_before_selective_p = 1;
4741 }
4742
4743 return get_next_display_element (it);
4744 }
4745
4746
4747 /* Deliver an image display element. The iterator IT is already
4748 filled with image information (done in handle_display_prop). Value
4749 is always 1. */
4750
4751
4752 static int
4753 next_element_from_image (it)
4754 struct it *it;
4755 {
4756 it->what = IT_IMAGE;
4757 return 1;
4758 }
4759
4760
4761 /* Fill iterator IT with next display element from a stretch glyph
4762 property. IT->object is the value of the text property. Value is
4763 always 1. */
4764
4765 static int
4766 next_element_from_stretch (it)
4767 struct it *it;
4768 {
4769 it->what = IT_STRETCH;
4770 return 1;
4771 }
4772
4773
4774 /* Load IT with the next display element from current_buffer. Value
4775 is zero if end of buffer reached. IT->stop_charpos is the next
4776 position at which to stop and check for text properties or buffer
4777 end. */
4778
4779 static int
4780 next_element_from_buffer (it)
4781 struct it *it;
4782 {
4783 int success_p = 1;
4784
4785 /* Check this assumption, otherwise, we would never enter the
4786 if-statement, below. */
4787 xassert (IT_CHARPOS (*it) >= BEGV
4788 && IT_CHARPOS (*it) <= it->stop_charpos);
4789
4790 if (IT_CHARPOS (*it) >= it->stop_charpos)
4791 {
4792 if (IT_CHARPOS (*it) >= it->end_charpos)
4793 {
4794 int overlay_strings_follow_p;
4795
4796 /* End of the game, except when overlay strings follow that
4797 haven't been returned yet. */
4798 if (it->overlay_strings_at_end_processed_p)
4799 overlay_strings_follow_p = 0;
4800 else
4801 {
4802 it->overlay_strings_at_end_processed_p = 1;
4803 overlay_strings_follow_p = get_overlay_strings (it, 0);
4804 }
4805
4806 if (overlay_strings_follow_p)
4807 success_p = get_next_display_element (it);
4808 else
4809 {
4810 it->what = IT_EOB;
4811 it->position = it->current.pos;
4812 success_p = 0;
4813 }
4814 }
4815 else
4816 {
4817 handle_stop (it);
4818 return get_next_display_element (it);
4819 }
4820 }
4821 else
4822 {
4823 /* No face changes, overlays etc. in sight, so just return a
4824 character from current_buffer. */
4825 unsigned char *p;
4826
4827 /* Maybe run the redisplay end trigger hook. Performance note:
4828 This doesn't seem to cost measurable time. */
4829 if (it->redisplay_end_trigger_charpos
4830 && it->glyph_row
4831 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4832 run_redisplay_end_trigger_hook (it);
4833
4834 /* Get the next character, maybe multibyte. */
4835 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4836 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4837 {
4838 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4839 - IT_BYTEPOS (*it));
4840 it->c = string_char_and_length (p, maxlen, &it->len);
4841 }
4842 else
4843 it->c = *p, it->len = 1;
4844
4845 /* Record what we have and where it came from. */
4846 it->what = IT_CHARACTER;;
4847 it->object = it->w->buffer;
4848 it->position = it->current.pos;
4849
4850 /* Normally we return the character found above, except when we
4851 really want to return an ellipsis for selective display. */
4852 if (it->selective)
4853 {
4854 if (it->c == '\n')
4855 {
4856 /* A value of selective > 0 means hide lines indented more
4857 than that number of columns. */
4858 if (it->selective > 0
4859 && IT_CHARPOS (*it) + 1 < ZV
4860 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4861 IT_BYTEPOS (*it) + 1,
4862 it->selective))
4863 {
4864 success_p = next_element_from_ellipsis (it);
4865 it->dpvec_char_len = -1;
4866 }
4867 }
4868 else if (it->c == '\r' && it->selective == -1)
4869 {
4870 /* A value of selective == -1 means that everything from the
4871 CR to the end of the line is invisible, with maybe an
4872 ellipsis displayed for it. */
4873 success_p = next_element_from_ellipsis (it);
4874 it->dpvec_char_len = -1;
4875 }
4876 }
4877 }
4878
4879 /* Value is zero if end of buffer reached. */
4880 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4881 return success_p;
4882 }
4883
4884
4885 /* Run the redisplay end trigger hook for IT. */
4886
4887 static void
4888 run_redisplay_end_trigger_hook (it)
4889 struct it *it;
4890 {
4891 Lisp_Object args[3];
4892
4893 /* IT->glyph_row should be non-null, i.e. we should be actually
4894 displaying something, or otherwise we should not run the hook. */
4895 xassert (it->glyph_row);
4896
4897 /* Set up hook arguments. */
4898 args[0] = Qredisplay_end_trigger_functions;
4899 args[1] = it->window;
4900 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4901 it->redisplay_end_trigger_charpos = 0;
4902
4903 /* Since we are *trying* to run these functions, don't try to run
4904 them again, even if they get an error. */
4905 it->w->redisplay_end_trigger = Qnil;
4906 Frun_hook_with_args (3, args);
4907
4908 /* Notice if it changed the face of the character we are on. */
4909 handle_face_prop (it);
4910 }
4911
4912
4913 /* Deliver a composition display element. The iterator IT is already
4914 filled with composition information (done in
4915 handle_composition_prop). Value is always 1. */
4916
4917 static int
4918 next_element_from_composition (it)
4919 struct it *it;
4920 {
4921 it->what = IT_COMPOSITION;
4922 it->position = (STRINGP (it->string)
4923 ? it->current.string_pos
4924 : it->current.pos);
4925 return 1;
4926 }
4927
4928
4929 \f
4930 /***********************************************************************
4931 Moving an iterator without producing glyphs
4932 ***********************************************************************/
4933
4934 /* Move iterator IT to a specified buffer or X position within one
4935 line on the display without producing glyphs.
4936
4937 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4938 whichever is reached first.
4939
4940 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4941
4942 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4943 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4944 TO_X includes the amount by which a window is horizontally
4945 scrolled.
4946
4947 Value is
4948
4949 MOVE_POS_MATCH_OR_ZV
4950 - when TO_POS or ZV was reached.
4951
4952 MOVE_X_REACHED
4953 -when TO_X was reached before TO_POS or ZV were reached.
4954
4955 MOVE_LINE_CONTINUED
4956 - when we reached the end of the display area and the line must
4957 be continued.
4958
4959 MOVE_LINE_TRUNCATED
4960 - when we reached the end of the display area and the line is
4961 truncated.
4962
4963 MOVE_NEWLINE_OR_CR
4964 - when we stopped at a line end, i.e. a newline or a CR and selective
4965 display is on. */
4966
4967 static enum move_it_result
4968 move_it_in_display_line_to (it, to_charpos, to_x, op)
4969 struct it *it;
4970 int to_charpos, to_x, op;
4971 {
4972 enum move_it_result result = MOVE_UNDEFINED;
4973 struct glyph_row *saved_glyph_row;
4974
4975 /* Don't produce glyphs in produce_glyphs. */
4976 saved_glyph_row = it->glyph_row;
4977 it->glyph_row = NULL;
4978
4979 while (1)
4980 {
4981 int x, i, ascent = 0, descent = 0;
4982
4983 /* Stop when ZV or TO_CHARPOS reached. */
4984 if (!get_next_display_element (it)
4985 || ((op & MOVE_TO_POS) != 0
4986 && BUFFERP (it->object)
4987 && IT_CHARPOS (*it) >= to_charpos))
4988 {
4989 result = MOVE_POS_MATCH_OR_ZV;
4990 break;
4991 }
4992
4993 /* The call to produce_glyphs will get the metrics of the
4994 display element IT is loaded with. We record in x the
4995 x-position before this display element in case it does not
4996 fit on the line. */
4997 x = it->current_x;
4998
4999 /* Remember the line height so far in case the next element doesn't
5000 fit on the line. */
5001 if (!it->truncate_lines_p)
5002 {
5003 ascent = it->max_ascent;
5004 descent = it->max_descent;
5005 }
5006
5007 PRODUCE_GLYPHS (it);
5008
5009 if (it->area != TEXT_AREA)
5010 {
5011 set_iterator_to_next (it, 1);
5012 continue;
5013 }
5014
5015 /* The number of glyphs we get back in IT->nglyphs will normally
5016 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5017 character on a terminal frame, or (iii) a line end. For the
5018 second case, IT->nglyphs - 1 padding glyphs will be present
5019 (on X frames, there is only one glyph produced for a
5020 composite character.
5021
5022 The behavior implemented below means, for continuation lines,
5023 that as many spaces of a TAB as fit on the current line are
5024 displayed there. For terminal frames, as many glyphs of a
5025 multi-glyph character are displayed in the current line, too.
5026 This is what the old redisplay code did, and we keep it that
5027 way. Under X, the whole shape of a complex character must
5028 fit on the line or it will be completely displayed in the
5029 next line.
5030
5031 Note that both for tabs and padding glyphs, all glyphs have
5032 the same width. */
5033 if (it->nglyphs)
5034 {
5035 /* More than one glyph or glyph doesn't fit on line. All
5036 glyphs have the same width. */
5037 int single_glyph_width = it->pixel_width / it->nglyphs;
5038 int new_x;
5039
5040 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5041 {
5042 new_x = x + single_glyph_width;
5043
5044 /* We want to leave anything reaching TO_X to the caller. */
5045 if ((op & MOVE_TO_X) && new_x > to_x)
5046 {
5047 it->current_x = x;
5048 result = MOVE_X_REACHED;
5049 break;
5050 }
5051 else if (/* Lines are continued. */
5052 !it->truncate_lines_p
5053 && (/* And glyph doesn't fit on the line. */
5054 new_x > it->last_visible_x
5055 /* Or it fits exactly and we're on a window
5056 system frame. */
5057 || (new_x == it->last_visible_x
5058 && FRAME_WINDOW_P (it->f))))
5059 {
5060 if (/* IT->hpos == 0 means the very first glyph
5061 doesn't fit on the line, e.g. a wide image. */
5062 it->hpos == 0
5063 || (new_x == it->last_visible_x
5064 && FRAME_WINDOW_P (it->f)))
5065 {
5066 ++it->hpos;
5067 it->current_x = new_x;
5068 if (i == it->nglyphs - 1)
5069 set_iterator_to_next (it, 1);
5070 }
5071 else
5072 {
5073 it->current_x = x;
5074 it->max_ascent = ascent;
5075 it->max_descent = descent;
5076 }
5077
5078 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5079 IT_CHARPOS (*it)));
5080 result = MOVE_LINE_CONTINUED;
5081 break;
5082 }
5083 else if (new_x > it->first_visible_x)
5084 {
5085 /* Glyph is visible. Increment number of glyphs that
5086 would be displayed. */
5087 ++it->hpos;
5088 }
5089 else
5090 {
5091 /* Glyph is completely off the left margin of the display
5092 area. Nothing to do. */
5093 }
5094 }
5095
5096 if (result != MOVE_UNDEFINED)
5097 break;
5098 }
5099 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5100 {
5101 /* Stop when TO_X specified and reached. This check is
5102 necessary here because of lines consisting of a line end,
5103 only. The line end will not produce any glyphs and we
5104 would never get MOVE_X_REACHED. */
5105 xassert (it->nglyphs == 0);
5106 result = MOVE_X_REACHED;
5107 break;
5108 }
5109
5110 /* Is this a line end? If yes, we're done. */
5111 if (ITERATOR_AT_END_OF_LINE_P (it))
5112 {
5113 result = MOVE_NEWLINE_OR_CR;
5114 break;
5115 }
5116
5117 /* The current display element has been consumed. Advance
5118 to the next. */
5119 set_iterator_to_next (it, 1);
5120
5121 /* Stop if lines are truncated and IT's current x-position is
5122 past the right edge of the window now. */
5123 if (it->truncate_lines_p
5124 && it->current_x >= it->last_visible_x)
5125 {
5126 result = MOVE_LINE_TRUNCATED;
5127 break;
5128 }
5129 }
5130
5131 /* Restore the iterator settings altered at the beginning of this
5132 function. */
5133 it->glyph_row = saved_glyph_row;
5134 return result;
5135 }
5136
5137
5138 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
5139 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
5140 the description of enum move_operation_enum.
5141
5142 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5143 screen line, this function will set IT to the next position >
5144 TO_CHARPOS. */
5145
5146 void
5147 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5148 struct it *it;
5149 int to_charpos, to_x, to_y, to_vpos;
5150 int op;
5151 {
5152 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5153 int line_height;
5154 int reached = 0;
5155
5156 for (;;)
5157 {
5158 if (op & MOVE_TO_VPOS)
5159 {
5160 /* If no TO_CHARPOS and no TO_X specified, stop at the
5161 start of the line TO_VPOS. */
5162 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5163 {
5164 if (it->vpos == to_vpos)
5165 {
5166 reached = 1;
5167 break;
5168 }
5169 else
5170 skip = move_it_in_display_line_to (it, -1, -1, 0);
5171 }
5172 else
5173 {
5174 /* TO_VPOS >= 0 means stop at TO_X in the line at
5175 TO_VPOS, or at TO_POS, whichever comes first. */
5176 if (it->vpos == to_vpos)
5177 {
5178 reached = 2;
5179 break;
5180 }
5181
5182 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5183
5184 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5185 {
5186 reached = 3;
5187 break;
5188 }
5189 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5190 {
5191 /* We have reached TO_X but not in the line we want. */
5192 skip = move_it_in_display_line_to (it, to_charpos,
5193 -1, MOVE_TO_POS);
5194 if (skip == MOVE_POS_MATCH_OR_ZV)
5195 {
5196 reached = 4;
5197 break;
5198 }
5199 }
5200 }
5201 }
5202 else if (op & MOVE_TO_Y)
5203 {
5204 struct it it_backup;
5205
5206 /* TO_Y specified means stop at TO_X in the line containing
5207 TO_Y---or at TO_CHARPOS if this is reached first. The
5208 problem is that we can't really tell whether the line
5209 contains TO_Y before we have completely scanned it, and
5210 this may skip past TO_X. What we do is to first scan to
5211 TO_X.
5212
5213 If TO_X is not specified, use a TO_X of zero. The reason
5214 is to make the outcome of this function more predictable.
5215 If we didn't use TO_X == 0, we would stop at the end of
5216 the line which is probably not what a caller would expect
5217 to happen. */
5218 skip = move_it_in_display_line_to (it, to_charpos,
5219 ((op & MOVE_TO_X)
5220 ? to_x : 0),
5221 (MOVE_TO_X
5222 | (op & MOVE_TO_POS)));
5223
5224 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5225 if (skip == MOVE_POS_MATCH_OR_ZV)
5226 {
5227 reached = 5;
5228 break;
5229 }
5230
5231 /* If TO_X was reached, we would like to know whether TO_Y
5232 is in the line. This can only be said if we know the
5233 total line height which requires us to scan the rest of
5234 the line. */
5235 if (skip == MOVE_X_REACHED)
5236 {
5237 it_backup = *it;
5238 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5239 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5240 op & MOVE_TO_POS);
5241 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5242 }
5243
5244 /* Now, decide whether TO_Y is in this line. */
5245 line_height = it->max_ascent + it->max_descent;
5246 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5247
5248 if (to_y >= it->current_y
5249 && to_y < it->current_y + line_height)
5250 {
5251 if (skip == MOVE_X_REACHED)
5252 /* If TO_Y is in this line and TO_X was reached above,
5253 we scanned too far. We have to restore IT's settings
5254 to the ones before skipping. */
5255 *it = it_backup;
5256 reached = 6;
5257 }
5258 else if (skip == MOVE_X_REACHED)
5259 {
5260 skip = skip2;
5261 if (skip == MOVE_POS_MATCH_OR_ZV)
5262 reached = 7;
5263 }
5264
5265 if (reached)
5266 break;
5267 }
5268 else
5269 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5270
5271 switch (skip)
5272 {
5273 case MOVE_POS_MATCH_OR_ZV:
5274 reached = 8;
5275 goto out;
5276
5277 case MOVE_NEWLINE_OR_CR:
5278 set_iterator_to_next (it, 1);
5279 it->continuation_lines_width = 0;
5280 break;
5281
5282 case MOVE_LINE_TRUNCATED:
5283 it->continuation_lines_width = 0;
5284 reseat_at_next_visible_line_start (it, 0);
5285 if ((op & MOVE_TO_POS) != 0
5286 && IT_CHARPOS (*it) > to_charpos)
5287 {
5288 reached = 9;
5289 goto out;
5290 }
5291 break;
5292
5293 case MOVE_LINE_CONTINUED:
5294 it->continuation_lines_width += it->current_x;
5295 break;
5296
5297 default:
5298 abort ();
5299 }
5300
5301 /* Reset/increment for the next run. */
5302 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5303 it->current_x = it->hpos = 0;
5304 it->current_y += it->max_ascent + it->max_descent;
5305 ++it->vpos;
5306 last_height = it->max_ascent + it->max_descent;
5307 last_max_ascent = it->max_ascent;
5308 it->max_ascent = it->max_descent = 0;
5309 }
5310
5311 out:
5312
5313 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5314 }
5315
5316
5317 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5318
5319 If DY > 0, move IT backward at least that many pixels. DY = 0
5320 means move IT backward to the preceding line start or BEGV. This
5321 function may move over more than DY pixels if IT->current_y - DY
5322 ends up in the middle of a line; in this case IT->current_y will be
5323 set to the top of the line moved to. */
5324
5325 void
5326 move_it_vertically_backward (it, dy)
5327 struct it *it;
5328 int dy;
5329 {
5330 int nlines, h;
5331 struct it it2, it3;
5332 int start_pos = IT_CHARPOS (*it);
5333
5334 xassert (dy >= 0);
5335
5336 /* Estimate how many newlines we must move back. */
5337 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5338
5339 /* Set the iterator's position that many lines back. */
5340 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5341 back_to_previous_visible_line_start (it);
5342
5343 /* Reseat the iterator here. When moving backward, we don't want
5344 reseat to skip forward over invisible text, set up the iterator
5345 to deliver from overlay strings at the new position etc. So,
5346 use reseat_1 here. */
5347 reseat_1 (it, it->current.pos, 1);
5348
5349 /* We are now surely at a line start. */
5350 it->current_x = it->hpos = 0;
5351
5352 /* Move forward and see what y-distance we moved. First move to the
5353 start of the next line so that we get its height. We need this
5354 height to be able to tell whether we reached the specified
5355 y-distance. */
5356 it2 = *it;
5357 it2.max_ascent = it2.max_descent = 0;
5358 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5359 MOVE_TO_POS | MOVE_TO_VPOS);
5360 xassert (IT_CHARPOS (*it) >= BEGV);
5361 it3 = it2;
5362
5363 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5364 xassert (IT_CHARPOS (*it) >= BEGV);
5365 h = it2.current_y - it->current_y;
5366 nlines = it2.vpos - it->vpos;
5367
5368 /* Correct IT's y and vpos position. */
5369 it->vpos -= nlines;
5370 it->current_y -= h;
5371
5372 if (dy == 0)
5373 {
5374 /* DY == 0 means move to the start of the screen line. The
5375 value of nlines is > 0 if continuation lines were involved. */
5376 if (nlines > 0)
5377 move_it_by_lines (it, nlines, 1);
5378 xassert (IT_CHARPOS (*it) <= start_pos);
5379 }
5380 else if (nlines)
5381 {
5382 /* The y-position we try to reach. Note that h has been
5383 subtracted in front of the if-statement. */
5384 int target_y = it->current_y + h - dy;
5385 int y0 = it3.current_y;
5386 int y1 = line_bottom_y (&it3);
5387 int line_height = y1 - y0;
5388
5389 /* If we did not reach target_y, try to move further backward if
5390 we can. If we moved too far backward, try to move forward. */
5391 if (target_y < it->current_y
5392 /* This is heuristic. In a window that's 3 lines high, with
5393 a line height of 13 pixels each, recentering with point
5394 on the bottom line will try to move -39/2 = 19 pixels
5395 backward. Try to avoid moving into the first line. */
5396 && it->current_y - target_y > line_height / 3 * 2
5397 && IT_CHARPOS (*it) > BEGV)
5398 {
5399 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5400 target_y - it->current_y));
5401 move_it_vertically (it, target_y - it->current_y);
5402 xassert (IT_CHARPOS (*it) >= BEGV);
5403 }
5404 else if (target_y >= it->current_y + line_height
5405 && IT_CHARPOS (*it) < ZV)
5406 {
5407 /* Should move forward by at least one line, maybe more. */
5408 do
5409 {
5410 move_it_by_lines (it, 1, 1);
5411 }
5412 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5413
5414 xassert (IT_CHARPOS (*it) >= BEGV);
5415 }
5416 }
5417 }
5418
5419
5420 /* Move IT by a specified amount of pixel lines DY. DY negative means
5421 move backwards. DY = 0 means move to start of screen line. At the
5422 end, IT will be on the start of a screen line. */
5423
5424 void
5425 move_it_vertically (it, dy)
5426 struct it *it;
5427 int dy;
5428 {
5429 if (dy <= 0)
5430 move_it_vertically_backward (it, -dy);
5431 else if (dy > 0)
5432 {
5433 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5434 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5435 MOVE_TO_POS | MOVE_TO_Y);
5436 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5437
5438 /* If buffer ends in ZV without a newline, move to the start of
5439 the line to satisfy the post-condition. */
5440 if (IT_CHARPOS (*it) == ZV
5441 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5442 move_it_by_lines (it, 0, 0);
5443 }
5444 }
5445
5446
5447 /* Move iterator IT past the end of the text line it is in. */
5448
5449 void
5450 move_it_past_eol (it)
5451 struct it *it;
5452 {
5453 enum move_it_result rc;
5454
5455 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5456 if (rc == MOVE_NEWLINE_OR_CR)
5457 set_iterator_to_next (it, 0);
5458 }
5459
5460
5461 #if 0 /* Currently not used. */
5462
5463 /* Return non-zero if some text between buffer positions START_CHARPOS
5464 and END_CHARPOS is invisible. IT->window is the window for text
5465 property lookup. */
5466
5467 static int
5468 invisible_text_between_p (it, start_charpos, end_charpos)
5469 struct it *it;
5470 int start_charpos, end_charpos;
5471 {
5472 Lisp_Object prop, limit;
5473 int invisible_found_p;
5474
5475 xassert (it != NULL && start_charpos <= end_charpos);
5476
5477 /* Is text at START invisible? */
5478 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5479 it->window);
5480 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5481 invisible_found_p = 1;
5482 else
5483 {
5484 limit = Fnext_single_char_property_change (make_number (start_charpos),
5485 Qinvisible, Qnil,
5486 make_number (end_charpos));
5487 invisible_found_p = XFASTINT (limit) < end_charpos;
5488 }
5489
5490 return invisible_found_p;
5491 }
5492
5493 #endif /* 0 */
5494
5495
5496 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5497 negative means move up. DVPOS == 0 means move to the start of the
5498 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5499 NEED_Y_P is zero, IT->current_y will be left unchanged.
5500
5501 Further optimization ideas: If we would know that IT->f doesn't use
5502 a face with proportional font, we could be faster for
5503 truncate-lines nil. */
5504
5505 void
5506 move_it_by_lines (it, dvpos, need_y_p)
5507 struct it *it;
5508 int dvpos, need_y_p;
5509 {
5510 struct position pos;
5511
5512 if (!FRAME_WINDOW_P (it->f))
5513 {
5514 struct text_pos textpos;
5515
5516 /* We can use vmotion on frames without proportional fonts. */
5517 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5518 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5519 reseat (it, textpos, 1);
5520 it->vpos += pos.vpos;
5521 it->current_y += pos.vpos;
5522 }
5523 else if (dvpos == 0)
5524 {
5525 /* DVPOS == 0 means move to the start of the screen line. */
5526 move_it_vertically_backward (it, 0);
5527 xassert (it->current_x == 0 && it->hpos == 0);
5528 }
5529 else if (dvpos > 0)
5530 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5531 else
5532 {
5533 struct it it2;
5534 int start_charpos, i;
5535
5536 /* Start at the beginning of the screen line containing IT's
5537 position. */
5538 move_it_vertically_backward (it, 0);
5539
5540 /* Go back -DVPOS visible lines and reseat the iterator there. */
5541 start_charpos = IT_CHARPOS (*it);
5542 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5543 back_to_previous_visible_line_start (it);
5544 reseat (it, it->current.pos, 1);
5545 it->current_x = it->hpos = 0;
5546
5547 /* Above call may have moved too far if continuation lines
5548 are involved. Scan forward and see if it did. */
5549 it2 = *it;
5550 it2.vpos = it2.current_y = 0;
5551 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5552 it->vpos -= it2.vpos;
5553 it->current_y -= it2.current_y;
5554 it->current_x = it->hpos = 0;
5555
5556 /* If we moved too far, move IT some lines forward. */
5557 if (it2.vpos > -dvpos)
5558 {
5559 int delta = it2.vpos + dvpos;
5560 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5561 }
5562 }
5563 }
5564
5565
5566 \f
5567 /***********************************************************************
5568 Messages
5569 ***********************************************************************/
5570
5571
5572 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5573 to *Messages*. */
5574
5575 void
5576 add_to_log (format, arg1, arg2)
5577 char *format;
5578 Lisp_Object arg1, arg2;
5579 {
5580 Lisp_Object args[3];
5581 Lisp_Object msg, fmt;
5582 char *buffer;
5583 int len;
5584 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5585
5586 /* Do nothing if called asynchronously. Inserting text into
5587 a buffer may call after-change-functions and alike and
5588 that would means running Lisp asynchronously. */
5589 if (handling_signal)
5590 return;
5591
5592 fmt = msg = Qnil;
5593 GCPRO4 (fmt, msg, arg1, arg2);
5594
5595 args[0] = fmt = build_string (format);
5596 args[1] = arg1;
5597 args[2] = arg2;
5598 msg = Fformat (3, args);
5599
5600 len = SBYTES (msg) + 1;
5601 buffer = (char *) alloca (len);
5602 bcopy (SDATA (msg), buffer, len);
5603
5604 message_dolog (buffer, len - 1, 1, 0);
5605 UNGCPRO;
5606 }
5607
5608
5609 /* Output a newline in the *Messages* buffer if "needs" one. */
5610
5611 void
5612 message_log_maybe_newline ()
5613 {
5614 if (message_log_need_newline)
5615 message_dolog ("", 0, 1, 0);
5616 }
5617
5618
5619 /* Add a string M of length NBYTES to the message log, optionally
5620 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5621 nonzero, means interpret the contents of M as multibyte. This
5622 function calls low-level routines in order to bypass text property
5623 hooks, etc. which might not be safe to run. */
5624
5625 void
5626 message_dolog (m, nbytes, nlflag, multibyte)
5627 char *m;
5628 int nbytes, nlflag, multibyte;
5629 {
5630 if (!NILP (Vmessage_log_max))
5631 {
5632 struct buffer *oldbuf;
5633 Lisp_Object oldpoint, oldbegv, oldzv;
5634 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5635 int point_at_end = 0;
5636 int zv_at_end = 0;
5637 Lisp_Object old_deactivate_mark, tem;
5638 struct gcpro gcpro1;
5639
5640 old_deactivate_mark = Vdeactivate_mark;
5641 oldbuf = current_buffer;
5642 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5643 current_buffer->undo_list = Qt;
5644
5645 oldpoint = message_dolog_marker1;
5646 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5647 oldbegv = message_dolog_marker2;
5648 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5649 oldzv = message_dolog_marker3;
5650 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5651 GCPRO1 (old_deactivate_mark);
5652
5653 if (PT == Z)
5654 point_at_end = 1;
5655 if (ZV == Z)
5656 zv_at_end = 1;
5657
5658 BEGV = BEG;
5659 BEGV_BYTE = BEG_BYTE;
5660 ZV = Z;
5661 ZV_BYTE = Z_BYTE;
5662 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5663
5664 /* Insert the string--maybe converting multibyte to single byte
5665 or vice versa, so that all the text fits the buffer. */
5666 if (multibyte
5667 && NILP (current_buffer->enable_multibyte_characters))
5668 {
5669 int i, c, char_bytes;
5670 unsigned char work[1];
5671
5672 /* Convert a multibyte string to single-byte
5673 for the *Message* buffer. */
5674 for (i = 0; i < nbytes; i += nbytes)
5675 {
5676 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5677 work[0] = (SINGLE_BYTE_CHAR_P (c)
5678 ? c
5679 : multibyte_char_to_unibyte (c, Qnil));
5680 insert_1_both (work, 1, 1, 1, 0, 0);
5681 }
5682 }
5683 else if (! multibyte
5684 && ! NILP (current_buffer->enable_multibyte_characters))
5685 {
5686 int i, c, char_bytes;
5687 unsigned char *msg = (unsigned char *) m;
5688 unsigned char str[MAX_MULTIBYTE_LENGTH];
5689 /* Convert a single-byte string to multibyte
5690 for the *Message* buffer. */
5691 for (i = 0; i < nbytes; i++)
5692 {
5693 c = unibyte_char_to_multibyte (msg[i]);
5694 char_bytes = CHAR_STRING (c, str);
5695 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5696 }
5697 }
5698 else if (nbytes)
5699 insert_1 (m, nbytes, 1, 0, 0);
5700
5701 if (nlflag)
5702 {
5703 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5704 insert_1 ("\n", 1, 1, 0, 0);
5705
5706 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5707 this_bol = PT;
5708 this_bol_byte = PT_BYTE;
5709
5710 /* See if this line duplicates the previous one.
5711 If so, combine duplicates. */
5712 if (this_bol > BEG)
5713 {
5714 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5715 prev_bol = PT;
5716 prev_bol_byte = PT_BYTE;
5717
5718 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5719 this_bol, this_bol_byte);
5720 if (dup)
5721 {
5722 del_range_both (prev_bol, prev_bol_byte,
5723 this_bol, this_bol_byte, 0);
5724 if (dup > 1)
5725 {
5726 char dupstr[40];
5727 int duplen;
5728
5729 /* If you change this format, don't forget to also
5730 change message_log_check_duplicate. */
5731 sprintf (dupstr, " [%d times]", dup);
5732 duplen = strlen (dupstr);
5733 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5734 insert_1 (dupstr, duplen, 1, 0, 1);
5735 }
5736 }
5737 }
5738
5739 /* If we have more than the desired maximum number of lines
5740 in the *Messages* buffer now, delete the oldest ones.
5741 This is safe because we don't have undo in this buffer. */
5742
5743 if (NATNUMP (Vmessage_log_max))
5744 {
5745 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5746 -XFASTINT (Vmessage_log_max) - 1, 0);
5747 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5748 }
5749 }
5750 BEGV = XMARKER (oldbegv)->charpos;
5751 BEGV_BYTE = marker_byte_position (oldbegv);
5752
5753 if (zv_at_end)
5754 {
5755 ZV = Z;
5756 ZV_BYTE = Z_BYTE;
5757 }
5758 else
5759 {
5760 ZV = XMARKER (oldzv)->charpos;
5761 ZV_BYTE = marker_byte_position (oldzv);
5762 }
5763
5764 if (point_at_end)
5765 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5766 else
5767 /* We can't do Fgoto_char (oldpoint) because it will run some
5768 Lisp code. */
5769 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5770 XMARKER (oldpoint)->bytepos);
5771
5772 UNGCPRO;
5773 unchain_marker (oldpoint);
5774 unchain_marker (oldbegv);
5775 unchain_marker (oldzv);
5776
5777 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5778 set_buffer_internal (oldbuf);
5779 if (NILP (tem))
5780 windows_or_buffers_changed = old_windows_or_buffers_changed;
5781 message_log_need_newline = !nlflag;
5782 Vdeactivate_mark = old_deactivate_mark;
5783 }
5784 }
5785
5786
5787 /* We are at the end of the buffer after just having inserted a newline.
5788 (Note: We depend on the fact we won't be crossing the gap.)
5789 Check to see if the most recent message looks a lot like the previous one.
5790 Return 0 if different, 1 if the new one should just replace it, or a
5791 value N > 1 if we should also append " [N times]". */
5792
5793 static int
5794 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5795 int prev_bol, this_bol;
5796 int prev_bol_byte, this_bol_byte;
5797 {
5798 int i;
5799 int len = Z_BYTE - 1 - this_bol_byte;
5800 int seen_dots = 0;
5801 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5802 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5803
5804 for (i = 0; i < len; i++)
5805 {
5806 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5807 seen_dots = 1;
5808 if (p1[i] != p2[i])
5809 return seen_dots;
5810 }
5811 p1 += len;
5812 if (*p1 == '\n')
5813 return 2;
5814 if (*p1++ == ' ' && *p1++ == '[')
5815 {
5816 int n = 0;
5817 while (*p1 >= '0' && *p1 <= '9')
5818 n = n * 10 + *p1++ - '0';
5819 if (strncmp (p1, " times]\n", 8) == 0)
5820 return n+1;
5821 }
5822 return 0;
5823 }
5824
5825
5826 /* Display an echo area message M with a specified length of NBYTES
5827 bytes. The string may include null characters. If M is 0, clear
5828 out any existing message, and let the mini-buffer text show
5829 through.
5830
5831 The buffer M must continue to exist until after the echo area gets
5832 cleared or some other message gets displayed there. This means do
5833 not pass text that is stored in a Lisp string; do not pass text in
5834 a buffer that was alloca'd. */
5835
5836 void
5837 message2 (m, nbytes, multibyte)
5838 char *m;
5839 int nbytes;
5840 int multibyte;
5841 {
5842 /* First flush out any partial line written with print. */
5843 message_log_maybe_newline ();
5844 if (m)
5845 message_dolog (m, nbytes, 1, multibyte);
5846 message2_nolog (m, nbytes, multibyte);
5847 }
5848
5849
5850 /* The non-logging counterpart of message2. */
5851
5852 void
5853 message2_nolog (m, nbytes, multibyte)
5854 char *m;
5855 int nbytes;
5856 {
5857 struct frame *sf = SELECTED_FRAME ();
5858 message_enable_multibyte = multibyte;
5859
5860 if (noninteractive)
5861 {
5862 if (noninteractive_need_newline)
5863 putc ('\n', stderr);
5864 noninteractive_need_newline = 0;
5865 if (m)
5866 fwrite (m, nbytes, 1, stderr);
5867 if (cursor_in_echo_area == 0)
5868 fprintf (stderr, "\n");
5869 fflush (stderr);
5870 }
5871 /* A null message buffer means that the frame hasn't really been
5872 initialized yet. Error messages get reported properly by
5873 cmd_error, so this must be just an informative message; toss it. */
5874 else if (INTERACTIVE
5875 && sf->glyphs_initialized_p
5876 && FRAME_MESSAGE_BUF (sf))
5877 {
5878 Lisp_Object mini_window;
5879 struct frame *f;
5880
5881 /* Get the frame containing the mini-buffer
5882 that the selected frame is using. */
5883 mini_window = FRAME_MINIBUF_WINDOW (sf);
5884 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5885
5886 FRAME_SAMPLE_VISIBILITY (f);
5887 if (FRAME_VISIBLE_P (sf)
5888 && ! FRAME_VISIBLE_P (f))
5889 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5890
5891 if (m)
5892 {
5893 set_message (m, Qnil, nbytes, multibyte);
5894 if (minibuffer_auto_raise)
5895 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5896 }
5897 else
5898 clear_message (1, 1);
5899
5900 do_pending_window_change (0);
5901 echo_area_display (1);
5902 do_pending_window_change (0);
5903 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5904 (*frame_up_to_date_hook) (f);
5905 }
5906 }
5907
5908
5909 /* Display an echo area message M with a specified length of NBYTES
5910 bytes. The string may include null characters. If M is not a
5911 string, clear out any existing message, and let the mini-buffer
5912 text show through. */
5913
5914 void
5915 message3 (m, nbytes, multibyte)
5916 Lisp_Object m;
5917 int nbytes;
5918 int multibyte;
5919 {
5920 struct gcpro gcpro1;
5921
5922 GCPRO1 (m);
5923
5924 /* First flush out any partial line written with print. */
5925 message_log_maybe_newline ();
5926 if (STRINGP (m))
5927 message_dolog (SDATA (m), nbytes, 1, multibyte);
5928 message3_nolog (m, nbytes, multibyte);
5929
5930 UNGCPRO;
5931 }
5932
5933
5934 /* The non-logging version of message3. */
5935
5936 void
5937 message3_nolog (m, nbytes, multibyte)
5938 Lisp_Object m;
5939 int nbytes, multibyte;
5940 {
5941 struct frame *sf = SELECTED_FRAME ();
5942 message_enable_multibyte = multibyte;
5943
5944 if (noninteractive)
5945 {
5946 if (noninteractive_need_newline)
5947 putc ('\n', stderr);
5948 noninteractive_need_newline = 0;
5949 if (STRINGP (m))
5950 fwrite (SDATA (m), nbytes, 1, stderr);
5951 if (cursor_in_echo_area == 0)
5952 fprintf (stderr, "\n");
5953 fflush (stderr);
5954 }
5955 /* A null message buffer means that the frame hasn't really been
5956 initialized yet. Error messages get reported properly by
5957 cmd_error, so this must be just an informative message; toss it. */
5958 else if (INTERACTIVE
5959 && sf->glyphs_initialized_p
5960 && FRAME_MESSAGE_BUF (sf))
5961 {
5962 Lisp_Object mini_window;
5963 Lisp_Object frame;
5964 struct frame *f;
5965
5966 /* Get the frame containing the mini-buffer
5967 that the selected frame is using. */
5968 mini_window = FRAME_MINIBUF_WINDOW (sf);
5969 frame = XWINDOW (mini_window)->frame;
5970 f = XFRAME (frame);
5971
5972 FRAME_SAMPLE_VISIBILITY (f);
5973 if (FRAME_VISIBLE_P (sf)
5974 && !FRAME_VISIBLE_P (f))
5975 Fmake_frame_visible (frame);
5976
5977 if (STRINGP (m) && SCHARS (m) > 0)
5978 {
5979 set_message (NULL, m, nbytes, multibyte);
5980 if (minibuffer_auto_raise)
5981 Fraise_frame (frame);
5982 }
5983 else
5984 clear_message (1, 1);
5985
5986 do_pending_window_change (0);
5987 echo_area_display (1);
5988 do_pending_window_change (0);
5989 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5990 (*frame_up_to_date_hook) (f);
5991 }
5992 }
5993
5994
5995 /* Display a null-terminated echo area message M. If M is 0, clear
5996 out any existing message, and let the mini-buffer text show through.
5997
5998 The buffer M must continue to exist until after the echo area gets
5999 cleared or some other message gets displayed there. Do not pass
6000 text that is stored in a Lisp string. Do not pass text in a buffer
6001 that was alloca'd. */
6002
6003 void
6004 message1 (m)
6005 char *m;
6006 {
6007 message2 (m, (m ? strlen (m) : 0), 0);
6008 }
6009
6010
6011 /* The non-logging counterpart of message1. */
6012
6013 void
6014 message1_nolog (m)
6015 char *m;
6016 {
6017 message2_nolog (m, (m ? strlen (m) : 0), 0);
6018 }
6019
6020 /* Display a message M which contains a single %s
6021 which gets replaced with STRING. */
6022
6023 void
6024 message_with_string (m, string, log)
6025 char *m;
6026 Lisp_Object string;
6027 int log;
6028 {
6029 if (noninteractive)
6030 {
6031 if (m)
6032 {
6033 if (noninteractive_need_newline)
6034 putc ('\n', stderr);
6035 noninteractive_need_newline = 0;
6036 fprintf (stderr, m, SDATA (string));
6037 if (cursor_in_echo_area == 0)
6038 fprintf (stderr, "\n");
6039 fflush (stderr);
6040 }
6041 }
6042 else if (INTERACTIVE)
6043 {
6044 /* The frame whose minibuffer we're going to display the message on.
6045 It may be larger than the selected frame, so we need
6046 to use its buffer, not the selected frame's buffer. */
6047 Lisp_Object mini_window;
6048 struct frame *f, *sf = SELECTED_FRAME ();
6049
6050 /* Get the frame containing the minibuffer
6051 that the selected frame is using. */
6052 mini_window = FRAME_MINIBUF_WINDOW (sf);
6053 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6054
6055 /* A null message buffer means that the frame hasn't really been
6056 initialized yet. Error messages get reported properly by
6057 cmd_error, so this must be just an informative message; toss it. */
6058 if (FRAME_MESSAGE_BUF (f))
6059 {
6060 Lisp_Object args[2], message;
6061 struct gcpro gcpro1, gcpro2;
6062
6063 args[0] = build_string (m);
6064 args[1] = message = string;
6065 GCPRO2 (args, message);
6066 gcpro1.nvars = 2;
6067
6068 message = Fformat (2, args);
6069
6070 if (log)
6071 message3 (message, SBYTES (message), SMBP (message));
6072 else
6073 message3_nolog (message, SBYTES (message), SMBP (message));
6074
6075 UNGCPRO;
6076
6077 /* Print should start at the beginning of the message
6078 buffer next time. */
6079 message_buf_print = 0;
6080 }
6081 }
6082 }
6083
6084
6085 /* Dump an informative message to the minibuf. If M is 0, clear out
6086 any existing message, and let the mini-buffer text show through. */
6087
6088 /* VARARGS 1 */
6089 void
6090 message (m, a1, a2, a3)
6091 char *m;
6092 EMACS_INT a1, a2, a3;
6093 {
6094 if (noninteractive)
6095 {
6096 if (m)
6097 {
6098 if (noninteractive_need_newline)
6099 putc ('\n', stderr);
6100 noninteractive_need_newline = 0;
6101 fprintf (stderr, m, a1, a2, a3);
6102 if (cursor_in_echo_area == 0)
6103 fprintf (stderr, "\n");
6104 fflush (stderr);
6105 }
6106 }
6107 else if (INTERACTIVE)
6108 {
6109 /* The frame whose mini-buffer we're going to display the message
6110 on. It may be larger than the selected frame, so we need to
6111 use its buffer, not the selected frame's buffer. */
6112 Lisp_Object mini_window;
6113 struct frame *f, *sf = SELECTED_FRAME ();
6114
6115 /* Get the frame containing the mini-buffer
6116 that the selected frame is using. */
6117 mini_window = FRAME_MINIBUF_WINDOW (sf);
6118 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6119
6120 /* A null message buffer means that the frame hasn't really been
6121 initialized yet. Error messages get reported properly by
6122 cmd_error, so this must be just an informative message; toss
6123 it. */
6124 if (FRAME_MESSAGE_BUF (f))
6125 {
6126 if (m)
6127 {
6128 int len;
6129 #ifdef NO_ARG_ARRAY
6130 char *a[3];
6131 a[0] = (char *) a1;
6132 a[1] = (char *) a2;
6133 a[2] = (char *) a3;
6134
6135 len = doprnt (FRAME_MESSAGE_BUF (f),
6136 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6137 #else
6138 len = doprnt (FRAME_MESSAGE_BUF (f),
6139 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6140 (char **) &a1);
6141 #endif /* NO_ARG_ARRAY */
6142
6143 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6144 }
6145 else
6146 message1 (0);
6147
6148 /* Print should start at the beginning of the message
6149 buffer next time. */
6150 message_buf_print = 0;
6151 }
6152 }
6153 }
6154
6155
6156 /* The non-logging version of message. */
6157
6158 void
6159 message_nolog (m, a1, a2, a3)
6160 char *m;
6161 EMACS_INT a1, a2, a3;
6162 {
6163 Lisp_Object old_log_max;
6164 old_log_max = Vmessage_log_max;
6165 Vmessage_log_max = Qnil;
6166 message (m, a1, a2, a3);
6167 Vmessage_log_max = old_log_max;
6168 }
6169
6170
6171 /* Display the current message in the current mini-buffer. This is
6172 only called from error handlers in process.c, and is not time
6173 critical. */
6174
6175 void
6176 update_echo_area ()
6177 {
6178 if (!NILP (echo_area_buffer[0]))
6179 {
6180 Lisp_Object string;
6181 string = Fcurrent_message ();
6182 message3 (string, SBYTES (string),
6183 !NILP (current_buffer->enable_multibyte_characters));
6184 }
6185 }
6186
6187
6188 /* Make sure echo area buffers in echo_buffers[] are life. If they
6189 aren't, make new ones. */
6190
6191 static void
6192 ensure_echo_area_buffers ()
6193 {
6194 int i;
6195
6196 for (i = 0; i < 2; ++i)
6197 if (!BUFFERP (echo_buffer[i])
6198 || NILP (XBUFFER (echo_buffer[i])->name))
6199 {
6200 char name[30];
6201 Lisp_Object old_buffer;
6202 int j;
6203
6204 old_buffer = echo_buffer[i];
6205 sprintf (name, " *Echo Area %d*", i);
6206 echo_buffer[i] = Fget_buffer_create (build_string (name));
6207 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6208
6209 for (j = 0; j < 2; ++j)
6210 if (EQ (old_buffer, echo_area_buffer[j]))
6211 echo_area_buffer[j] = echo_buffer[i];
6212 }
6213 }
6214
6215
6216 /* Call FN with args A1..A4 with either the current or last displayed
6217 echo_area_buffer as current buffer.
6218
6219 WHICH zero means use the current message buffer
6220 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6221 from echo_buffer[] and clear it.
6222
6223 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6224 suitable buffer from echo_buffer[] and clear it.
6225
6226 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6227 that the current message becomes the last displayed one, make
6228 choose a suitable buffer for echo_area_buffer[0], and clear it.
6229
6230 Value is what FN returns. */
6231
6232 static int
6233 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6234 struct window *w;
6235 int which;
6236 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6237 EMACS_INT a1;
6238 Lisp_Object a2;
6239 EMACS_INT a3, a4;
6240 {
6241 Lisp_Object buffer;
6242 int this_one, the_other, clear_buffer_p, rc;
6243 int count = BINDING_STACK_SIZE ();
6244
6245 /* If buffers aren't life, make new ones. */
6246 ensure_echo_area_buffers ();
6247
6248 clear_buffer_p = 0;
6249
6250 if (which == 0)
6251 this_one = 0, the_other = 1;
6252 else if (which > 0)
6253 this_one = 1, the_other = 0;
6254 else
6255 {
6256 this_one = 0, the_other = 1;
6257 clear_buffer_p = 1;
6258
6259 /* We need a fresh one in case the current echo buffer equals
6260 the one containing the last displayed echo area message. */
6261 if (!NILP (echo_area_buffer[this_one])
6262 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6263 echo_area_buffer[this_one] = Qnil;
6264 }
6265
6266 /* Choose a suitable buffer from echo_buffer[] is we don't
6267 have one. */
6268 if (NILP (echo_area_buffer[this_one]))
6269 {
6270 echo_area_buffer[this_one]
6271 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6272 ? echo_buffer[the_other]
6273 : echo_buffer[this_one]);
6274 clear_buffer_p = 1;
6275 }
6276
6277 buffer = echo_area_buffer[this_one];
6278
6279 /* Don't get confused by reusing the buffer used for echoing
6280 for a different purpose. */
6281 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6282 cancel_echoing ();
6283
6284 record_unwind_protect (unwind_with_echo_area_buffer,
6285 with_echo_area_buffer_unwind_data (w));
6286
6287 /* Make the echo area buffer current. Note that for display
6288 purposes, it is not necessary that the displayed window's buffer
6289 == current_buffer, except for text property lookup. So, let's
6290 only set that buffer temporarily here without doing a full
6291 Fset_window_buffer. We must also change w->pointm, though,
6292 because otherwise an assertions in unshow_buffer fails, and Emacs
6293 aborts. */
6294 set_buffer_internal_1 (XBUFFER (buffer));
6295 if (w)
6296 {
6297 w->buffer = buffer;
6298 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6299 }
6300
6301 current_buffer->undo_list = Qt;
6302 current_buffer->read_only = Qnil;
6303 specbind (Qinhibit_read_only, Qt);
6304 specbind (Qinhibit_modification_hooks, Qt);
6305
6306 if (clear_buffer_p && Z > BEG)
6307 del_range (BEG, Z);
6308
6309 xassert (BEGV >= BEG);
6310 xassert (ZV <= Z && ZV >= BEGV);
6311
6312 rc = fn (a1, a2, a3, a4);
6313
6314 xassert (BEGV >= BEG);
6315 xassert (ZV <= Z && ZV >= BEGV);
6316
6317 unbind_to (count, Qnil);
6318 return rc;
6319 }
6320
6321
6322 /* Save state that should be preserved around the call to the function
6323 FN called in with_echo_area_buffer. */
6324
6325 static Lisp_Object
6326 with_echo_area_buffer_unwind_data (w)
6327 struct window *w;
6328 {
6329 int i = 0;
6330 Lisp_Object vector;
6331
6332 /* Reduce consing by keeping one vector in
6333 Vwith_echo_area_save_vector. */
6334 vector = Vwith_echo_area_save_vector;
6335 Vwith_echo_area_save_vector = Qnil;
6336
6337 if (NILP (vector))
6338 vector = Fmake_vector (make_number (7), Qnil);
6339
6340 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6341 AREF (vector, i) = Vdeactivate_mark, ++i;
6342 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6343
6344 if (w)
6345 {
6346 XSETWINDOW (AREF (vector, i), w); ++i;
6347 AREF (vector, i) = w->buffer; ++i;
6348 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6349 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6350 }
6351 else
6352 {
6353 int end = i + 4;
6354 for (; i < end; ++i)
6355 AREF (vector, i) = Qnil;
6356 }
6357
6358 xassert (i == ASIZE (vector));
6359 return vector;
6360 }
6361
6362
6363 /* Restore global state from VECTOR which was created by
6364 with_echo_area_buffer_unwind_data. */
6365
6366 static Lisp_Object
6367 unwind_with_echo_area_buffer (vector)
6368 Lisp_Object vector;
6369 {
6370 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6371 Vdeactivate_mark = AREF (vector, 1);
6372 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6373
6374 if (WINDOWP (AREF (vector, 3)))
6375 {
6376 struct window *w;
6377 Lisp_Object buffer, charpos, bytepos;
6378
6379 w = XWINDOW (AREF (vector, 3));
6380 buffer = AREF (vector, 4);
6381 charpos = AREF (vector, 5);
6382 bytepos = AREF (vector, 6);
6383
6384 w->buffer = buffer;
6385 set_marker_both (w->pointm, buffer,
6386 XFASTINT (charpos), XFASTINT (bytepos));
6387 }
6388
6389 Vwith_echo_area_save_vector = vector;
6390 return Qnil;
6391 }
6392
6393
6394 /* Set up the echo area for use by print functions. MULTIBYTE_P
6395 non-zero means we will print multibyte. */
6396
6397 void
6398 setup_echo_area_for_printing (multibyte_p)
6399 int multibyte_p;
6400 {
6401 ensure_echo_area_buffers ();
6402
6403 if (!message_buf_print)
6404 {
6405 /* A message has been output since the last time we printed.
6406 Choose a fresh echo area buffer. */
6407 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6408 echo_area_buffer[0] = echo_buffer[1];
6409 else
6410 echo_area_buffer[0] = echo_buffer[0];
6411
6412 /* Switch to that buffer and clear it. */
6413 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6414 current_buffer->truncate_lines = Qnil;
6415
6416 if (Z > BEG)
6417 {
6418 int count = BINDING_STACK_SIZE ();
6419 specbind (Qinhibit_read_only, Qt);
6420 del_range (BEG, Z);
6421 unbind_to (count, Qnil);
6422 }
6423 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6424
6425 /* Set up the buffer for the multibyteness we need. */
6426 if (multibyte_p
6427 != !NILP (current_buffer->enable_multibyte_characters))
6428 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6429
6430 /* Raise the frame containing the echo area. */
6431 if (minibuffer_auto_raise)
6432 {
6433 struct frame *sf = SELECTED_FRAME ();
6434 Lisp_Object mini_window;
6435 mini_window = FRAME_MINIBUF_WINDOW (sf);
6436 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6437 }
6438
6439 message_log_maybe_newline ();
6440 message_buf_print = 1;
6441 }
6442 else
6443 {
6444 if (NILP (echo_area_buffer[0]))
6445 {
6446 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6447 echo_area_buffer[0] = echo_buffer[1];
6448 else
6449 echo_area_buffer[0] = echo_buffer[0];
6450 }
6451
6452 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6453 {
6454 /* Someone switched buffers between print requests. */
6455 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6456 current_buffer->truncate_lines = Qnil;
6457 }
6458 }
6459 }
6460
6461
6462 /* Display an echo area message in window W. Value is non-zero if W's
6463 height is changed. If display_last_displayed_message_p is
6464 non-zero, display the message that was last displayed, otherwise
6465 display the current message. */
6466
6467 static int
6468 display_echo_area (w)
6469 struct window *w;
6470 {
6471 int i, no_message_p, window_height_changed_p, count;
6472
6473 /* Temporarily disable garbage collections while displaying the echo
6474 area. This is done because a GC can print a message itself.
6475 That message would modify the echo area buffer's contents while a
6476 redisplay of the buffer is going on, and seriously confuse
6477 redisplay. */
6478 count = inhibit_garbage_collection ();
6479
6480 /* If there is no message, we must call display_echo_area_1
6481 nevertheless because it resizes the window. But we will have to
6482 reset the echo_area_buffer in question to nil at the end because
6483 with_echo_area_buffer will sets it to an empty buffer. */
6484 i = display_last_displayed_message_p ? 1 : 0;
6485 no_message_p = NILP (echo_area_buffer[i]);
6486
6487 window_height_changed_p
6488 = with_echo_area_buffer (w, display_last_displayed_message_p,
6489 display_echo_area_1,
6490 (EMACS_INT) w, Qnil, 0, 0);
6491
6492 if (no_message_p)
6493 echo_area_buffer[i] = Qnil;
6494
6495 unbind_to (count, Qnil);
6496 return window_height_changed_p;
6497 }
6498
6499
6500 /* Helper for display_echo_area. Display the current buffer which
6501 contains the current echo area message in window W, a mini-window,
6502 a pointer to which is passed in A1. A2..A4 are currently not used.
6503 Change the height of W so that all of the message is displayed.
6504 Value is non-zero if height of W was changed. */
6505
6506 static int
6507 display_echo_area_1 (a1, a2, a3, a4)
6508 EMACS_INT a1;
6509 Lisp_Object a2;
6510 EMACS_INT a3, a4;
6511 {
6512 struct window *w = (struct window *) a1;
6513 Lisp_Object window;
6514 struct text_pos start;
6515 int window_height_changed_p = 0;
6516
6517 /* Do this before displaying, so that we have a large enough glyph
6518 matrix for the display. */
6519 window_height_changed_p = resize_mini_window (w, 0);
6520
6521 /* Display. */
6522 clear_glyph_matrix (w->desired_matrix);
6523 XSETWINDOW (window, w);
6524 SET_TEXT_POS (start, BEG, BEG_BYTE);
6525 try_window (window, start);
6526
6527 return window_height_changed_p;
6528 }
6529
6530
6531 /* Resize the echo area window to exactly the size needed for the
6532 currently displayed message, if there is one. If a mini-buffer
6533 is active, don't shrink it. */
6534
6535 void
6536 resize_echo_area_exactly ()
6537 {
6538 if (BUFFERP (echo_area_buffer[0])
6539 && WINDOWP (echo_area_window))
6540 {
6541 struct window *w = XWINDOW (echo_area_window);
6542 int resized_p;
6543 Lisp_Object resize_exactly;
6544
6545 if (minibuf_level == 0)
6546 resize_exactly = Qt;
6547 else
6548 resize_exactly = Qnil;
6549
6550 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6551 (EMACS_INT) w, resize_exactly, 0, 0);
6552 if (resized_p)
6553 {
6554 ++windows_or_buffers_changed;
6555 ++update_mode_lines;
6556 redisplay_internal (0);
6557 }
6558 }
6559 }
6560
6561
6562 /* Callback function for with_echo_area_buffer, when used from
6563 resize_echo_area_exactly. A1 contains a pointer to the window to
6564 resize, EXACTLY non-nil means resize the mini-window exactly to the
6565 size of the text displayed. A3 and A4 are not used. Value is what
6566 resize_mini_window returns. */
6567
6568 static int
6569 resize_mini_window_1 (a1, exactly, a3, a4)
6570 EMACS_INT a1;
6571 Lisp_Object exactly;
6572 EMACS_INT a3, a4;
6573 {
6574 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6575 }
6576
6577
6578 /* Resize mini-window W to fit the size of its contents. EXACT:P
6579 means size the window exactly to the size needed. Otherwise, it's
6580 only enlarged until W's buffer is empty. Value is non-zero if
6581 the window height has been changed. */
6582
6583 int
6584 resize_mini_window (w, exact_p)
6585 struct window *w;
6586 int exact_p;
6587 {
6588 struct frame *f = XFRAME (w->frame);
6589 int window_height_changed_p = 0;
6590
6591 xassert (MINI_WINDOW_P (w));
6592
6593 /* Don't resize windows while redisplaying a window; it would
6594 confuse redisplay functions when the size of the window they are
6595 displaying changes from under them. Such a resizing can happen,
6596 for instance, when which-func prints a long message while
6597 we are running fontification-functions. We're running these
6598 functions with safe_call which binds inhibit-redisplay to t. */
6599 if (!NILP (Vinhibit_redisplay))
6600 return 0;
6601
6602 /* Nil means don't try to resize. */
6603 if (NILP (Vresize_mini_windows)
6604 || (FRAME_X_P (f) && f->output_data.x == NULL))
6605 return 0;
6606
6607 if (!FRAME_MINIBUF_ONLY_P (f))
6608 {
6609 struct it it;
6610 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6611 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6612 int height, max_height;
6613 int unit = CANON_Y_UNIT (f);
6614 struct text_pos start;
6615 struct buffer *old_current_buffer = NULL;
6616
6617 if (current_buffer != XBUFFER (w->buffer))
6618 {
6619 old_current_buffer = current_buffer;
6620 set_buffer_internal (XBUFFER (w->buffer));
6621 }
6622
6623 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6624
6625 /* Compute the max. number of lines specified by the user. */
6626 if (FLOATP (Vmax_mini_window_height))
6627 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6628 else if (INTEGERP (Vmax_mini_window_height))
6629 max_height = XINT (Vmax_mini_window_height);
6630 else
6631 max_height = total_height / 4;
6632
6633 /* Correct that max. height if it's bogus. */
6634 max_height = max (1, max_height);
6635 max_height = min (total_height, max_height);
6636
6637 /* Find out the height of the text in the window. */
6638 if (it.truncate_lines_p)
6639 height = 1;
6640 else
6641 {
6642 last_height = 0;
6643 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6644 if (it.max_ascent == 0 && it.max_descent == 0)
6645 height = it.current_y + last_height;
6646 else
6647 height = it.current_y + it.max_ascent + it.max_descent;
6648 height -= it.extra_line_spacing;
6649 height = (height + unit - 1) / unit;
6650 }
6651
6652 /* Compute a suitable window start. */
6653 if (height > max_height)
6654 {
6655 height = max_height;
6656 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6657 move_it_vertically_backward (&it, (height - 1) * unit);
6658 start = it.current.pos;
6659 }
6660 else
6661 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6662 SET_MARKER_FROM_TEXT_POS (w->start, start);
6663
6664 if (EQ (Vresize_mini_windows, Qgrow_only))
6665 {
6666 /* Let it grow only, until we display an empty message, in which
6667 case the window shrinks again. */
6668 if (height > XFASTINT (w->height))
6669 {
6670 int old_height = XFASTINT (w->height);
6671 freeze_window_starts (f, 1);
6672 grow_mini_window (w, height - XFASTINT (w->height));
6673 window_height_changed_p = XFASTINT (w->height) != old_height;
6674 }
6675 else if (height < XFASTINT (w->height)
6676 && (exact_p || BEGV == ZV))
6677 {
6678 int old_height = XFASTINT (w->height);
6679 freeze_window_starts (f, 0);
6680 shrink_mini_window (w);
6681 window_height_changed_p = XFASTINT (w->height) != old_height;
6682 }
6683 }
6684 else
6685 {
6686 /* Always resize to exact size needed. */
6687 if (height > XFASTINT (w->height))
6688 {
6689 int old_height = XFASTINT (w->height);
6690 freeze_window_starts (f, 1);
6691 grow_mini_window (w, height - XFASTINT (w->height));
6692 window_height_changed_p = XFASTINT (w->height) != old_height;
6693 }
6694 else if (height < XFASTINT (w->height))
6695 {
6696 int old_height = XFASTINT (w->height);
6697 freeze_window_starts (f, 0);
6698 shrink_mini_window (w);
6699
6700 if (height)
6701 {
6702 freeze_window_starts (f, 1);
6703 grow_mini_window (w, height - XFASTINT (w->height));
6704 }
6705
6706 window_height_changed_p = XFASTINT (w->height) != old_height;
6707 }
6708 }
6709
6710 if (old_current_buffer)
6711 set_buffer_internal (old_current_buffer);
6712 }
6713
6714 return window_height_changed_p;
6715 }
6716
6717
6718 /* Value is the current message, a string, or nil if there is no
6719 current message. */
6720
6721 Lisp_Object
6722 current_message ()
6723 {
6724 Lisp_Object msg;
6725
6726 if (NILP (echo_area_buffer[0]))
6727 msg = Qnil;
6728 else
6729 {
6730 with_echo_area_buffer (0, 0, current_message_1,
6731 (EMACS_INT) &msg, Qnil, 0, 0);
6732 if (NILP (msg))
6733 echo_area_buffer[0] = Qnil;
6734 }
6735
6736 return msg;
6737 }
6738
6739
6740 static int
6741 current_message_1 (a1, a2, a3, a4)
6742 EMACS_INT a1;
6743 Lisp_Object a2;
6744 EMACS_INT a3, a4;
6745 {
6746 Lisp_Object *msg = (Lisp_Object *) a1;
6747
6748 if (Z > BEG)
6749 *msg = make_buffer_string (BEG, Z, 1);
6750 else
6751 *msg = Qnil;
6752 return 0;
6753 }
6754
6755
6756 /* Push the current message on Vmessage_stack for later restauration
6757 by restore_message. Value is non-zero if the current message isn't
6758 empty. This is a relatively infrequent operation, so it's not
6759 worth optimizing. */
6760
6761 int
6762 push_message ()
6763 {
6764 Lisp_Object msg;
6765 msg = current_message ();
6766 Vmessage_stack = Fcons (msg, Vmessage_stack);
6767 return STRINGP (msg);
6768 }
6769
6770
6771 /* Handler for record_unwind_protect calling pop_message. */
6772
6773 Lisp_Object
6774 push_message_unwind (dummy)
6775 Lisp_Object dummy;
6776 {
6777 pop_message ();
6778 return Qnil;
6779 }
6780
6781
6782 /* Restore message display from the top of Vmessage_stack. */
6783
6784 void
6785 restore_message ()
6786 {
6787 Lisp_Object msg;
6788
6789 xassert (CONSP (Vmessage_stack));
6790 msg = XCAR (Vmessage_stack);
6791 if (STRINGP (msg))
6792 message3_nolog (msg, SBYTES (msg), SMBP (msg));
6793 else
6794 message3_nolog (msg, 0, 0);
6795 }
6796
6797
6798 /* Pop the top-most entry off Vmessage_stack. */
6799
6800 void
6801 pop_message ()
6802 {
6803 xassert (CONSP (Vmessage_stack));
6804 Vmessage_stack = XCDR (Vmessage_stack);
6805 }
6806
6807
6808 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6809 exits. If the stack is not empty, we have a missing pop_message
6810 somewhere. */
6811
6812 void
6813 check_message_stack ()
6814 {
6815 if (!NILP (Vmessage_stack))
6816 abort ();
6817 }
6818
6819
6820 /* Truncate to NCHARS what will be displayed in the echo area the next
6821 time we display it---but don't redisplay it now. */
6822
6823 void
6824 truncate_echo_area (nchars)
6825 int nchars;
6826 {
6827 if (nchars == 0)
6828 echo_area_buffer[0] = Qnil;
6829 /* A null message buffer means that the frame hasn't really been
6830 initialized yet. Error messages get reported properly by
6831 cmd_error, so this must be just an informative message; toss it. */
6832 else if (!noninteractive
6833 && INTERACTIVE
6834 && !NILP (echo_area_buffer[0]))
6835 {
6836 struct frame *sf = SELECTED_FRAME ();
6837 if (FRAME_MESSAGE_BUF (sf))
6838 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6839 }
6840 }
6841
6842
6843 /* Helper function for truncate_echo_area. Truncate the current
6844 message to at most NCHARS characters. */
6845
6846 static int
6847 truncate_message_1 (nchars, a2, a3, a4)
6848 EMACS_INT nchars;
6849 Lisp_Object a2;
6850 EMACS_INT a3, a4;
6851 {
6852 if (BEG + nchars < Z)
6853 del_range (BEG + nchars, Z);
6854 if (Z == BEG)
6855 echo_area_buffer[0] = Qnil;
6856 return 0;
6857 }
6858
6859
6860 /* Set the current message to a substring of S or STRING.
6861
6862 If STRING is a Lisp string, set the message to the first NBYTES
6863 bytes from STRING. NBYTES zero means use the whole string. If
6864 STRING is multibyte, the message will be displayed multibyte.
6865
6866 If S is not null, set the message to the first LEN bytes of S. LEN
6867 zero means use the whole string. MULTIBYTE_P non-zero means S is
6868 multibyte. Display the message multibyte in that case. */
6869
6870 void
6871 set_message (s, string, nbytes, multibyte_p)
6872 char *s;
6873 Lisp_Object string;
6874 int nbytes;
6875 {
6876 message_enable_multibyte
6877 = ((s && multibyte_p)
6878 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6879
6880 with_echo_area_buffer (0, -1, set_message_1,
6881 (EMACS_INT) s, string, nbytes, multibyte_p);
6882 message_buf_print = 0;
6883 help_echo_showing_p = 0;
6884 }
6885
6886
6887 /* Helper function for set_message. Arguments have the same meaning
6888 as there, with A1 corresponding to S and A2 corresponding to STRING
6889 This function is called with the echo area buffer being
6890 current. */
6891
6892 static int
6893 set_message_1 (a1, a2, nbytes, multibyte_p)
6894 EMACS_INT a1;
6895 Lisp_Object a2;
6896 EMACS_INT nbytes, multibyte_p;
6897 {
6898 char *s = (char *) a1;
6899 Lisp_Object string = a2;
6900
6901 xassert (BEG == Z);
6902
6903 /* Change multibyteness of the echo buffer appropriately. */
6904 if (message_enable_multibyte
6905 != !NILP (current_buffer->enable_multibyte_characters))
6906 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6907
6908 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6909
6910 /* Insert new message at BEG. */
6911 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6912
6913 if (STRINGP (string))
6914 {
6915 int nchars;
6916
6917 if (nbytes == 0)
6918 nbytes = SBYTES (string);
6919 nchars = string_byte_to_char (string, nbytes);
6920
6921 /* This function takes care of single/multibyte conversion. We
6922 just have to ensure that the echo area buffer has the right
6923 setting of enable_multibyte_characters. */
6924 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6925 }
6926 else if (s)
6927 {
6928 if (nbytes == 0)
6929 nbytes = strlen (s);
6930
6931 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6932 {
6933 /* Convert from multi-byte to single-byte. */
6934 int i, c, n;
6935 unsigned char work[1];
6936
6937 /* Convert a multibyte string to single-byte. */
6938 for (i = 0; i < nbytes; i += n)
6939 {
6940 c = string_char_and_length (s + i, nbytes - i, &n);
6941 work[0] = (SINGLE_BYTE_CHAR_P (c)
6942 ? c
6943 : multibyte_char_to_unibyte (c, Qnil));
6944 insert_1_both (work, 1, 1, 1, 0, 0);
6945 }
6946 }
6947 else if (!multibyte_p
6948 && !NILP (current_buffer->enable_multibyte_characters))
6949 {
6950 /* Convert from single-byte to multi-byte. */
6951 int i, c, n;
6952 unsigned char *msg = (unsigned char *) s;
6953 unsigned char str[MAX_MULTIBYTE_LENGTH];
6954
6955 /* Convert a single-byte string to multibyte. */
6956 for (i = 0; i < nbytes; i++)
6957 {
6958 c = unibyte_char_to_multibyte (msg[i]);
6959 n = CHAR_STRING (c, str);
6960 insert_1_both (str, 1, n, 1, 0, 0);
6961 }
6962 }
6963 else
6964 insert_1 (s, nbytes, 1, 0, 0);
6965 }
6966
6967 return 0;
6968 }
6969
6970
6971 /* Clear messages. CURRENT_P non-zero means clear the current
6972 message. LAST_DISPLAYED_P non-zero means clear the message
6973 last displayed. */
6974
6975 void
6976 clear_message (current_p, last_displayed_p)
6977 int current_p, last_displayed_p;
6978 {
6979 if (current_p)
6980 {
6981 echo_area_buffer[0] = Qnil;
6982 message_cleared_p = 1;
6983 }
6984
6985 if (last_displayed_p)
6986 echo_area_buffer[1] = Qnil;
6987
6988 message_buf_print = 0;
6989 }
6990
6991 /* Clear garbaged frames.
6992
6993 This function is used where the old redisplay called
6994 redraw_garbaged_frames which in turn called redraw_frame which in
6995 turn called clear_frame. The call to clear_frame was a source of
6996 flickering. I believe a clear_frame is not necessary. It should
6997 suffice in the new redisplay to invalidate all current matrices,
6998 and ensure a complete redisplay of all windows. */
6999
7000 static void
7001 clear_garbaged_frames ()
7002 {
7003 if (frame_garbaged)
7004 {
7005 Lisp_Object tail, frame;
7006
7007 FOR_EACH_FRAME (tail, frame)
7008 {
7009 struct frame *f = XFRAME (frame);
7010
7011 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7012 {
7013 if (f->resized_p)
7014 Fredraw_frame (frame);
7015 clear_current_matrices (f);
7016 f->garbaged = 0;
7017 f->resized_p = 0;
7018 }
7019 }
7020
7021 frame_garbaged = 0;
7022 ++windows_or_buffers_changed;
7023 }
7024 }
7025
7026
7027 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7028 is non-zero update selected_frame. Value is non-zero if the
7029 mini-windows height has been changed. */
7030
7031 static int
7032 echo_area_display (update_frame_p)
7033 int update_frame_p;
7034 {
7035 Lisp_Object mini_window;
7036 struct window *w;
7037 struct frame *f;
7038 int window_height_changed_p = 0;
7039 struct frame *sf = SELECTED_FRAME ();
7040
7041 mini_window = FRAME_MINIBUF_WINDOW (sf);
7042 w = XWINDOW (mini_window);
7043 f = XFRAME (WINDOW_FRAME (w));
7044
7045 /* Don't display if frame is invisible or not yet initialized. */
7046 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7047 return 0;
7048
7049 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7050 #ifndef macintosh
7051 #ifdef HAVE_WINDOW_SYSTEM
7052 /* When Emacs starts, selected_frame may be a visible terminal
7053 frame, even if we run under a window system. If we let this
7054 through, a message would be displayed on the terminal. */
7055 if (EQ (selected_frame, Vterminal_frame)
7056 && !NILP (Vwindow_system))
7057 return 0;
7058 #endif /* HAVE_WINDOW_SYSTEM */
7059 #endif
7060
7061 /* Redraw garbaged frames. */
7062 if (frame_garbaged)
7063 clear_garbaged_frames ();
7064
7065 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7066 {
7067 echo_area_window = mini_window;
7068 window_height_changed_p = display_echo_area (w);
7069 w->must_be_updated_p = 1;
7070
7071 /* Update the display, unless called from redisplay_internal.
7072 Also don't update the screen during redisplay itself. The
7073 update will happen at the end of redisplay, and an update
7074 here could cause confusion. */
7075 if (update_frame_p && !redisplaying_p)
7076 {
7077 int n = 0;
7078
7079 /* If the display update has been interrupted by pending
7080 input, update mode lines in the frame. Due to the
7081 pending input, it might have been that redisplay hasn't
7082 been called, so that mode lines above the echo area are
7083 garbaged. This looks odd, so we prevent it here. */
7084 if (!display_completed)
7085 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7086
7087 if (window_height_changed_p
7088 /* Don't do this if Emacs is shutting down. Redisplay
7089 needs to run hooks. */
7090 && !NILP (Vrun_hooks))
7091 {
7092 /* Must update other windows. Likewise as in other
7093 cases, don't let this update be interrupted by
7094 pending input. */
7095 int count = BINDING_STACK_SIZE ();
7096 specbind (Qredisplay_dont_pause, Qt);
7097 windows_or_buffers_changed = 1;
7098 redisplay_internal (0);
7099 unbind_to (count, Qnil);
7100 }
7101 else if (FRAME_WINDOW_P (f) && n == 0)
7102 {
7103 /* Window configuration is the same as before.
7104 Can do with a display update of the echo area,
7105 unless we displayed some mode lines. */
7106 update_single_window (w, 1);
7107 rif->flush_display (f);
7108 }
7109 else
7110 update_frame (f, 1, 1);
7111
7112 /* If cursor is in the echo area, make sure that the next
7113 redisplay displays the minibuffer, so that the cursor will
7114 be replaced with what the minibuffer wants. */
7115 if (cursor_in_echo_area)
7116 ++windows_or_buffers_changed;
7117 }
7118 }
7119 else if (!EQ (mini_window, selected_window))
7120 windows_or_buffers_changed++;
7121
7122 /* Last displayed message is now the current message. */
7123 echo_area_buffer[1] = echo_area_buffer[0];
7124
7125 /* Prevent redisplay optimization in redisplay_internal by resetting
7126 this_line_start_pos. This is done because the mini-buffer now
7127 displays the message instead of its buffer text. */
7128 if (EQ (mini_window, selected_window))
7129 CHARPOS (this_line_start_pos) = 0;
7130
7131 return window_height_changed_p;
7132 }
7133
7134
7135 \f
7136 /***********************************************************************
7137 Frame Titles
7138 ***********************************************************************/
7139
7140
7141 #ifdef HAVE_WINDOW_SYSTEM
7142
7143 /* A buffer for constructing frame titles in it; allocated from the
7144 heap in init_xdisp and resized as needed in store_frame_title_char. */
7145
7146 static char *frame_title_buf;
7147
7148 /* The buffer's end, and a current output position in it. */
7149
7150 static char *frame_title_buf_end;
7151 static char *frame_title_ptr;
7152
7153
7154 /* Store a single character C for the frame title in frame_title_buf.
7155 Re-allocate frame_title_buf if necessary. */
7156
7157 static void
7158 store_frame_title_char (c)
7159 char c;
7160 {
7161 /* If output position has reached the end of the allocated buffer,
7162 double the buffer's size. */
7163 if (frame_title_ptr == frame_title_buf_end)
7164 {
7165 int len = frame_title_ptr - frame_title_buf;
7166 int new_size = 2 * len * sizeof *frame_title_buf;
7167 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7168 frame_title_buf_end = frame_title_buf + new_size;
7169 frame_title_ptr = frame_title_buf + len;
7170 }
7171
7172 *frame_title_ptr++ = c;
7173 }
7174
7175
7176 /* Store part of a frame title in frame_title_buf, beginning at
7177 frame_title_ptr. STR is the string to store. Do not copy
7178 characters that yield more columns than PRECISION; PRECISION <= 0
7179 means copy the whole string. Pad with spaces until FIELD_WIDTH
7180 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7181 pad. Called from display_mode_element when it is used to build a
7182 frame title. */
7183
7184 static int
7185 store_frame_title (str, field_width, precision)
7186 unsigned char *str;
7187 int field_width, precision;
7188 {
7189 int n = 0;
7190 int dummy, nbytes;
7191
7192 /* Copy at most PRECISION chars from STR. */
7193 nbytes = strlen (str);
7194 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7195 while (nbytes--)
7196 store_frame_title_char (*str++);
7197
7198 /* Fill up with spaces until FIELD_WIDTH reached. */
7199 while (field_width > 0
7200 && n < field_width)
7201 {
7202 store_frame_title_char (' ');
7203 ++n;
7204 }
7205
7206 return n;
7207 }
7208
7209
7210 /* Set the title of FRAME, if it has changed. The title format is
7211 Vicon_title_format if FRAME is iconified, otherwise it is
7212 frame_title_format. */
7213
7214 static void
7215 x_consider_frame_title (frame)
7216 Lisp_Object frame;
7217 {
7218 struct frame *f = XFRAME (frame);
7219
7220 if (FRAME_WINDOW_P (f)
7221 || FRAME_MINIBUF_ONLY_P (f)
7222 || f->explicit_name)
7223 {
7224 /* Do we have more than one visible frame on this X display? */
7225 Lisp_Object tail;
7226 Lisp_Object fmt;
7227 struct buffer *obuf;
7228 int len;
7229 struct it it;
7230
7231 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7232 {
7233 Lisp_Object other_frame = XCAR (tail);
7234 struct frame *tf = XFRAME (other_frame);
7235
7236 if (tf != f
7237 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7238 && !FRAME_MINIBUF_ONLY_P (tf)
7239 && !EQ (other_frame, tip_frame)
7240 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7241 break;
7242 }
7243
7244 /* Set global variable indicating that multiple frames exist. */
7245 multiple_frames = CONSP (tail);
7246
7247 /* Switch to the buffer of selected window of the frame. Set up
7248 frame_title_ptr so that display_mode_element will output into it;
7249 then display the title. */
7250 obuf = current_buffer;
7251 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7252 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7253 frame_title_ptr = frame_title_buf;
7254 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7255 NULL, DEFAULT_FACE_ID);
7256 display_mode_element (&it, 0, -1, -1, fmt, Qnil);
7257 len = frame_title_ptr - frame_title_buf;
7258 frame_title_ptr = NULL;
7259 set_buffer_internal_1 (obuf);
7260
7261 /* Set the title only if it's changed. This avoids consing in
7262 the common case where it hasn't. (If it turns out that we've
7263 already wasted too much time by walking through the list with
7264 display_mode_element, then we might need to optimize at a
7265 higher level than this.) */
7266 if (! STRINGP (f->name)
7267 || SBYTES (f->name) != len
7268 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7269 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7270 }
7271 }
7272
7273 #else /* not HAVE_WINDOW_SYSTEM */
7274
7275 #define frame_title_ptr ((char *)0)
7276 #define store_frame_title(str, mincol, maxcol) 0
7277
7278 #endif /* not HAVE_WINDOW_SYSTEM */
7279
7280
7281
7282 \f
7283 /***********************************************************************
7284 Menu Bars
7285 ***********************************************************************/
7286
7287
7288 /* Prepare for redisplay by updating menu-bar item lists when
7289 appropriate. This can call eval. */
7290
7291 void
7292 prepare_menu_bars ()
7293 {
7294 int all_windows;
7295 struct gcpro gcpro1, gcpro2;
7296 struct frame *f;
7297 Lisp_Object tooltip_frame;
7298
7299 #ifdef HAVE_WINDOW_SYSTEM
7300 tooltip_frame = tip_frame;
7301 #else
7302 tooltip_frame = Qnil;
7303 #endif
7304
7305 /* Update all frame titles based on their buffer names, etc. We do
7306 this before the menu bars so that the buffer-menu will show the
7307 up-to-date frame titles. */
7308 #ifdef HAVE_WINDOW_SYSTEM
7309 if (windows_or_buffers_changed || update_mode_lines)
7310 {
7311 Lisp_Object tail, frame;
7312
7313 FOR_EACH_FRAME (tail, frame)
7314 {
7315 f = XFRAME (frame);
7316 if (!EQ (frame, tooltip_frame)
7317 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7318 x_consider_frame_title (frame);
7319 }
7320 }
7321 #endif /* HAVE_WINDOW_SYSTEM */
7322
7323 /* Update the menu bar item lists, if appropriate. This has to be
7324 done before any actual redisplay or generation of display lines. */
7325 all_windows = (update_mode_lines
7326 || buffer_shared > 1
7327 || windows_or_buffers_changed);
7328 if (all_windows)
7329 {
7330 Lisp_Object tail, frame;
7331 int count = BINDING_STACK_SIZE ();
7332
7333 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7334
7335 FOR_EACH_FRAME (tail, frame)
7336 {
7337 f = XFRAME (frame);
7338
7339 /* Ignore tooltip frame. */
7340 if (EQ (frame, tooltip_frame))
7341 continue;
7342
7343 /* If a window on this frame changed size, report that to
7344 the user and clear the size-change flag. */
7345 if (FRAME_WINDOW_SIZES_CHANGED (f))
7346 {
7347 Lisp_Object functions;
7348
7349 /* Clear flag first in case we get an error below. */
7350 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7351 functions = Vwindow_size_change_functions;
7352 GCPRO2 (tail, functions);
7353
7354 while (CONSP (functions))
7355 {
7356 call1 (XCAR (functions), frame);
7357 functions = XCDR (functions);
7358 }
7359 UNGCPRO;
7360 }
7361
7362 GCPRO1 (tail);
7363 update_menu_bar (f, 0);
7364 #ifdef HAVE_WINDOW_SYSTEM
7365 update_tool_bar (f, 0);
7366 #endif
7367 UNGCPRO;
7368 }
7369
7370 unbind_to (count, Qnil);
7371 }
7372 else
7373 {
7374 struct frame *sf = SELECTED_FRAME ();
7375 update_menu_bar (sf, 1);
7376 #ifdef HAVE_WINDOW_SYSTEM
7377 update_tool_bar (sf, 1);
7378 #endif
7379 }
7380
7381 /* Motif needs this. See comment in xmenu.c. Turn it off when
7382 pending_menu_activation is not defined. */
7383 #ifdef USE_X_TOOLKIT
7384 pending_menu_activation = 0;
7385 #endif
7386 }
7387
7388
7389 /* Update the menu bar item list for frame F. This has to be done
7390 before we start to fill in any display lines, because it can call
7391 eval.
7392
7393 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7394
7395 static void
7396 update_menu_bar (f, save_match_data)
7397 struct frame *f;
7398 int save_match_data;
7399 {
7400 Lisp_Object window;
7401 register struct window *w;
7402
7403 /* If called recursively during a menu update, do nothing. This can
7404 happen when, for instance, an activate-menubar-hook causes a
7405 redisplay. */
7406 if (inhibit_menubar_update)
7407 return;
7408
7409 window = FRAME_SELECTED_WINDOW (f);
7410 w = XWINDOW (window);
7411
7412 if (update_mode_lines)
7413 w->update_mode_line = Qt;
7414
7415 if (FRAME_WINDOW_P (f)
7416 ?
7417 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7418 FRAME_EXTERNAL_MENU_BAR (f)
7419 #else
7420 FRAME_MENU_BAR_LINES (f) > 0
7421 #endif
7422 : FRAME_MENU_BAR_LINES (f) > 0)
7423 {
7424 /* If the user has switched buffers or windows, we need to
7425 recompute to reflect the new bindings. But we'll
7426 recompute when update_mode_lines is set too; that means
7427 that people can use force-mode-line-update to request
7428 that the menu bar be recomputed. The adverse effect on
7429 the rest of the redisplay algorithm is about the same as
7430 windows_or_buffers_changed anyway. */
7431 if (windows_or_buffers_changed
7432 || !NILP (w->update_mode_line)
7433 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7434 < BUF_MODIFF (XBUFFER (w->buffer)))
7435 != !NILP (w->last_had_star))
7436 || ((!NILP (Vtransient_mark_mode)
7437 && !NILP (XBUFFER (w->buffer)->mark_active))
7438 != !NILP (w->region_showing)))
7439 {
7440 struct buffer *prev = current_buffer;
7441 int count = BINDING_STACK_SIZE ();
7442
7443 specbind (Qinhibit_menubar_update, Qt);
7444
7445 set_buffer_internal_1 (XBUFFER (w->buffer));
7446 if (save_match_data)
7447 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7448 if (NILP (Voverriding_local_map_menu_flag))
7449 {
7450 specbind (Qoverriding_terminal_local_map, Qnil);
7451 specbind (Qoverriding_local_map, Qnil);
7452 }
7453
7454 /* Run the Lucid hook. */
7455 safe_run_hooks (Qactivate_menubar_hook);
7456
7457 /* If it has changed current-menubar from previous value,
7458 really recompute the menu-bar from the value. */
7459 if (! NILP (Vlucid_menu_bar_dirty_flag))
7460 call0 (Qrecompute_lucid_menubar);
7461
7462 safe_run_hooks (Qmenu_bar_update_hook);
7463 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7464
7465 /* Redisplay the menu bar in case we changed it. */
7466 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7467 if (FRAME_WINDOW_P (f)
7468 #if defined (macintosh)
7469 /* All frames on Mac OS share the same menubar. So only the
7470 selected frame should be allowed to set it. */
7471 && f == SELECTED_FRAME ()
7472 #endif
7473 )
7474 set_frame_menubar (f, 0, 0);
7475 else
7476 /* On a terminal screen, the menu bar is an ordinary screen
7477 line, and this makes it get updated. */
7478 w->update_mode_line = Qt;
7479 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7480 /* In the non-toolkit version, the menu bar is an ordinary screen
7481 line, and this makes it get updated. */
7482 w->update_mode_line = Qt;
7483 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7484
7485 unbind_to (count, Qnil);
7486 set_buffer_internal_1 (prev);
7487 }
7488 }
7489 }
7490
7491
7492 \f
7493 /***********************************************************************
7494 Tool-bars
7495 ***********************************************************************/
7496
7497 #ifdef HAVE_WINDOW_SYSTEM
7498
7499 /* Update the tool-bar item list for frame F. This has to be done
7500 before we start to fill in any display lines. Called from
7501 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7502 and restore it here. */
7503
7504 static void
7505 update_tool_bar (f, save_match_data)
7506 struct frame *f;
7507 int save_match_data;
7508 {
7509 if (WINDOWP (f->tool_bar_window)
7510 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7511 {
7512 Lisp_Object window;
7513 struct window *w;
7514
7515 window = FRAME_SELECTED_WINDOW (f);
7516 w = XWINDOW (window);
7517
7518 /* If the user has switched buffers or windows, we need to
7519 recompute to reflect the new bindings. But we'll
7520 recompute when update_mode_lines is set too; that means
7521 that people can use force-mode-line-update to request
7522 that the menu bar be recomputed. The adverse effect on
7523 the rest of the redisplay algorithm is about the same as
7524 windows_or_buffers_changed anyway. */
7525 if (windows_or_buffers_changed
7526 || !NILP (w->update_mode_line)
7527 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7528 < BUF_MODIFF (XBUFFER (w->buffer)))
7529 != !NILP (w->last_had_star))
7530 || ((!NILP (Vtransient_mark_mode)
7531 && !NILP (XBUFFER (w->buffer)->mark_active))
7532 != !NILP (w->region_showing)))
7533 {
7534 struct buffer *prev = current_buffer;
7535 int count = BINDING_STACK_SIZE ();
7536
7537 /* Set current_buffer to the buffer of the selected
7538 window of the frame, so that we get the right local
7539 keymaps. */
7540 set_buffer_internal_1 (XBUFFER (w->buffer));
7541
7542 /* Save match data, if we must. */
7543 if (save_match_data)
7544 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7545
7546 /* Make sure that we don't accidentally use bogus keymaps. */
7547 if (NILP (Voverriding_local_map_menu_flag))
7548 {
7549 specbind (Qoverriding_terminal_local_map, Qnil);
7550 specbind (Qoverriding_local_map, Qnil);
7551 }
7552
7553 /* Build desired tool-bar items from keymaps. */
7554 f->tool_bar_items
7555 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7556
7557 /* Redisplay the tool-bar in case we changed it. */
7558 w->update_mode_line = Qt;
7559
7560 unbind_to (count, Qnil);
7561 set_buffer_internal_1 (prev);
7562 }
7563 }
7564 }
7565
7566
7567 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7568 F's desired tool-bar contents. F->tool_bar_items must have
7569 been set up previously by calling prepare_menu_bars. */
7570
7571 static void
7572 build_desired_tool_bar_string (f)
7573 struct frame *f;
7574 {
7575 int i, size, size_needed;
7576 struct gcpro gcpro1, gcpro2, gcpro3;
7577 Lisp_Object image, plist, props;
7578
7579 image = plist = props = Qnil;
7580 GCPRO3 (image, plist, props);
7581
7582 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7583 Otherwise, make a new string. */
7584
7585 /* The size of the string we might be able to reuse. */
7586 size = (STRINGP (f->desired_tool_bar_string)
7587 ? SCHARS (f->desired_tool_bar_string)
7588 : 0);
7589
7590 /* We need one space in the string for each image. */
7591 size_needed = f->n_tool_bar_items;
7592
7593 /* Reuse f->desired_tool_bar_string, if possible. */
7594 if (size < size_needed || NILP (f->desired_tool_bar_string))
7595 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7596 make_number (' '));
7597 else
7598 {
7599 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7600 Fremove_text_properties (make_number (0), make_number (size),
7601 props, f->desired_tool_bar_string);
7602 }
7603
7604 /* Put a `display' property on the string for the images to display,
7605 put a `menu_item' property on tool-bar items with a value that
7606 is the index of the item in F's tool-bar item vector. */
7607 for (i = 0; i < f->n_tool_bar_items; ++i)
7608 {
7609 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7610
7611 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7612 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7613 int hmargin, vmargin, relief, idx, end;
7614 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7615
7616 /* If image is a vector, choose the image according to the
7617 button state. */
7618 image = PROP (TOOL_BAR_ITEM_IMAGES);
7619 if (VECTORP (image))
7620 {
7621 if (enabled_p)
7622 idx = (selected_p
7623 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7624 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7625 else
7626 idx = (selected_p
7627 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7628 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7629
7630 xassert (ASIZE (image) >= idx);
7631 image = AREF (image, idx);
7632 }
7633 else
7634 idx = -1;
7635
7636 /* Ignore invalid image specifications. */
7637 if (!valid_image_p (image))
7638 continue;
7639
7640 /* Display the tool-bar button pressed, or depressed. */
7641 plist = Fcopy_sequence (XCDR (image));
7642
7643 /* Compute margin and relief to draw. */
7644 relief = (tool_bar_button_relief >= 0
7645 ? tool_bar_button_relief
7646 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7647 hmargin = vmargin = relief;
7648
7649 if (INTEGERP (Vtool_bar_button_margin)
7650 && XINT (Vtool_bar_button_margin) > 0)
7651 {
7652 hmargin += XFASTINT (Vtool_bar_button_margin);
7653 vmargin += XFASTINT (Vtool_bar_button_margin);
7654 }
7655 else if (CONSP (Vtool_bar_button_margin))
7656 {
7657 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7658 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7659 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7660
7661 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7662 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7663 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7664 }
7665
7666 if (auto_raise_tool_bar_buttons_p)
7667 {
7668 /* Add a `:relief' property to the image spec if the item is
7669 selected. */
7670 if (selected_p)
7671 {
7672 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7673 hmargin -= relief;
7674 vmargin -= relief;
7675 }
7676 }
7677 else
7678 {
7679 /* If image is selected, display it pressed, i.e. with a
7680 negative relief. If it's not selected, display it with a
7681 raised relief. */
7682 plist = Fplist_put (plist, QCrelief,
7683 (selected_p
7684 ? make_number (-relief)
7685 : make_number (relief)));
7686 hmargin -= relief;
7687 vmargin -= relief;
7688 }
7689
7690 /* Put a margin around the image. */
7691 if (hmargin || vmargin)
7692 {
7693 if (hmargin == vmargin)
7694 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7695 else
7696 plist = Fplist_put (plist, QCmargin,
7697 Fcons (make_number (hmargin),
7698 make_number (vmargin)));
7699 }
7700
7701 /* If button is not enabled, and we don't have special images
7702 for the disabled state, make the image appear disabled by
7703 applying an appropriate algorithm to it. */
7704 if (!enabled_p && idx < 0)
7705 plist = Fplist_put (plist, QCconversion, Qdisabled);
7706
7707 /* Put a `display' text property on the string for the image to
7708 display. Put a `menu-item' property on the string that gives
7709 the start of this item's properties in the tool-bar items
7710 vector. */
7711 image = Fcons (Qimage, plist);
7712 props = list4 (Qdisplay, image,
7713 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7714
7715 /* Let the last image hide all remaining spaces in the tool bar
7716 string. The string can be longer than needed when we reuse a
7717 previous string. */
7718 if (i + 1 == f->n_tool_bar_items)
7719 end = SCHARS (f->desired_tool_bar_string);
7720 else
7721 end = i + 1;
7722 Fadd_text_properties (make_number (i), make_number (end),
7723 props, f->desired_tool_bar_string);
7724 #undef PROP
7725 }
7726
7727 UNGCPRO;
7728 }
7729
7730
7731 /* Display one line of the tool-bar of frame IT->f. */
7732
7733 static void
7734 display_tool_bar_line (it)
7735 struct it *it;
7736 {
7737 struct glyph_row *row = it->glyph_row;
7738 int max_x = it->last_visible_x;
7739 struct glyph *last;
7740
7741 prepare_desired_row (row);
7742 row->y = it->current_y;
7743
7744 /* Note that this isn't made use of if the face hasn't a box,
7745 so there's no need to check the face here. */
7746 it->start_of_box_run_p = 1;
7747
7748 while (it->current_x < max_x)
7749 {
7750 int x_before, x, n_glyphs_before, i, nglyphs;
7751
7752 /* Get the next display element. */
7753 if (!get_next_display_element (it))
7754 break;
7755
7756 /* Produce glyphs. */
7757 x_before = it->current_x;
7758 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7759 PRODUCE_GLYPHS (it);
7760
7761 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7762 i = 0;
7763 x = x_before;
7764 while (i < nglyphs)
7765 {
7766 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7767
7768 if (x + glyph->pixel_width > max_x)
7769 {
7770 /* Glyph doesn't fit on line. */
7771 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7772 it->current_x = x;
7773 goto out;
7774 }
7775
7776 ++it->hpos;
7777 x += glyph->pixel_width;
7778 ++i;
7779 }
7780
7781 /* Stop at line ends. */
7782 if (ITERATOR_AT_END_OF_LINE_P (it))
7783 break;
7784
7785 set_iterator_to_next (it, 1);
7786 }
7787
7788 out:;
7789
7790 row->displays_text_p = row->used[TEXT_AREA] != 0;
7791 extend_face_to_end_of_line (it);
7792 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7793 last->right_box_line_p = 1;
7794 if (last == row->glyphs[TEXT_AREA])
7795 last->left_box_line_p = 1;
7796 compute_line_metrics (it);
7797
7798 /* If line is empty, make it occupy the rest of the tool-bar. */
7799 if (!row->displays_text_p)
7800 {
7801 row->height = row->phys_height = it->last_visible_y - row->y;
7802 row->ascent = row->phys_ascent = 0;
7803 }
7804
7805 row->full_width_p = 1;
7806 row->continued_p = 0;
7807 row->truncated_on_left_p = 0;
7808 row->truncated_on_right_p = 0;
7809
7810 it->current_x = it->hpos = 0;
7811 it->current_y += row->height;
7812 ++it->vpos;
7813 ++it->glyph_row;
7814 }
7815
7816
7817 /* Value is the number of screen lines needed to make all tool-bar
7818 items of frame F visible. */
7819
7820 static int
7821 tool_bar_lines_needed (f)
7822 struct frame *f;
7823 {
7824 struct window *w = XWINDOW (f->tool_bar_window);
7825 struct it it;
7826
7827 /* Initialize an iterator for iteration over
7828 F->desired_tool_bar_string in the tool-bar window of frame F. */
7829 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7830 it.first_visible_x = 0;
7831 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7832 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7833
7834 while (!ITERATOR_AT_END_P (&it))
7835 {
7836 it.glyph_row = w->desired_matrix->rows;
7837 clear_glyph_row (it.glyph_row);
7838 display_tool_bar_line (&it);
7839 }
7840
7841 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7842 }
7843
7844
7845 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7846 0, 1, 0,
7847 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7848 (frame)
7849 Lisp_Object frame;
7850 {
7851 struct frame *f;
7852 struct window *w;
7853 int nlines = 0;
7854
7855 if (NILP (frame))
7856 frame = selected_frame;
7857 else
7858 CHECK_FRAME (frame);
7859 f = XFRAME (frame);
7860
7861 if (WINDOWP (f->tool_bar_window)
7862 || (w = XWINDOW (f->tool_bar_window),
7863 XFASTINT (w->height) > 0))
7864 {
7865 update_tool_bar (f, 1);
7866 if (f->n_tool_bar_items)
7867 {
7868 build_desired_tool_bar_string (f);
7869 nlines = tool_bar_lines_needed (f);
7870 }
7871 }
7872
7873 return make_number (nlines);
7874 }
7875
7876
7877 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7878 height should be changed. */
7879
7880 static int
7881 redisplay_tool_bar (f)
7882 struct frame *f;
7883 {
7884 struct window *w;
7885 struct it it;
7886 struct glyph_row *row;
7887 int change_height_p = 0;
7888
7889 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7890 do anything. This means you must start with tool-bar-lines
7891 non-zero to get the auto-sizing effect. Or in other words, you
7892 can turn off tool-bars by specifying tool-bar-lines zero. */
7893 if (!WINDOWP (f->tool_bar_window)
7894 || (w = XWINDOW (f->tool_bar_window),
7895 XFASTINT (w->height) == 0))
7896 return 0;
7897
7898 /* Set up an iterator for the tool-bar window. */
7899 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7900 it.first_visible_x = 0;
7901 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7902 row = it.glyph_row;
7903
7904 /* Build a string that represents the contents of the tool-bar. */
7905 build_desired_tool_bar_string (f);
7906 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7907
7908 /* Display as many lines as needed to display all tool-bar items. */
7909 while (it.current_y < it.last_visible_y)
7910 display_tool_bar_line (&it);
7911
7912 /* It doesn't make much sense to try scrolling in the tool-bar
7913 window, so don't do it. */
7914 w->desired_matrix->no_scrolling_p = 1;
7915 w->must_be_updated_p = 1;
7916
7917 if (auto_resize_tool_bars_p)
7918 {
7919 int nlines;
7920
7921 /* If we couldn't display everything, change the tool-bar's
7922 height. */
7923 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7924 change_height_p = 1;
7925
7926 /* If there are blank lines at the end, except for a partially
7927 visible blank line at the end that is smaller than
7928 CANON_Y_UNIT, change the tool-bar's height. */
7929 row = it.glyph_row - 1;
7930 if (!row->displays_text_p
7931 && row->height >= CANON_Y_UNIT (f))
7932 change_height_p = 1;
7933
7934 /* If row displays tool-bar items, but is partially visible,
7935 change the tool-bar's height. */
7936 if (row->displays_text_p
7937 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7938 change_height_p = 1;
7939
7940 /* Resize windows as needed by changing the `tool-bar-lines'
7941 frame parameter. */
7942 if (change_height_p
7943 && (nlines = tool_bar_lines_needed (f),
7944 nlines != XFASTINT (w->height)))
7945 {
7946 extern Lisp_Object Qtool_bar_lines;
7947 Lisp_Object frame;
7948 int old_height = XFASTINT (w->height);
7949
7950 XSETFRAME (frame, f);
7951 clear_glyph_matrix (w->desired_matrix);
7952 Fmodify_frame_parameters (frame,
7953 Fcons (Fcons (Qtool_bar_lines,
7954 make_number (nlines)),
7955 Qnil));
7956 if (XFASTINT (w->height) != old_height)
7957 fonts_changed_p = 1;
7958 }
7959 }
7960
7961 return change_height_p;
7962 }
7963
7964
7965 /* Get information about the tool-bar item which is displayed in GLYPH
7966 on frame F. Return in *PROP_IDX the index where tool-bar item
7967 properties start in F->tool_bar_items. Value is zero if
7968 GLYPH doesn't display a tool-bar item. */
7969
7970 int
7971 tool_bar_item_info (f, glyph, prop_idx)
7972 struct frame *f;
7973 struct glyph *glyph;
7974 int *prop_idx;
7975 {
7976 Lisp_Object prop;
7977 int success_p;
7978 int charpos;
7979
7980 /* This function can be called asynchronously, which means we must
7981 exclude any possibility that Fget_text_property signals an
7982 error. */
7983 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
7984 charpos = max (0, charpos);
7985
7986 /* Get the text property `menu-item' at pos. The value of that
7987 property is the start index of this item's properties in
7988 F->tool_bar_items. */
7989 prop = Fget_text_property (make_number (charpos),
7990 Qmenu_item, f->current_tool_bar_string);
7991 if (INTEGERP (prop))
7992 {
7993 *prop_idx = XINT (prop);
7994 success_p = 1;
7995 }
7996 else
7997 success_p = 0;
7998
7999 return success_p;
8000 }
8001
8002 #endif /* HAVE_WINDOW_SYSTEM */
8003
8004
8005 \f
8006 /************************************************************************
8007 Horizontal scrolling
8008 ************************************************************************/
8009
8010 static int hscroll_window_tree P_ ((Lisp_Object));
8011 static int hscroll_windows P_ ((Lisp_Object));
8012
8013 /* For all leaf windows in the window tree rooted at WINDOW, set their
8014 hscroll value so that PT is (i) visible in the window, and (ii) so
8015 that it is not within a certain margin at the window's left and
8016 right border. Value is non-zero if any window's hscroll has been
8017 changed. */
8018
8019 static int
8020 hscroll_window_tree (window)
8021 Lisp_Object window;
8022 {
8023 int hscrolled_p = 0;
8024 int hscroll_relative_p = FLOATP (Vhscroll_step);
8025 int hscroll_step_abs = 0;
8026 double hscroll_step_rel = 0;
8027
8028 if (hscroll_relative_p)
8029 {
8030 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
8031 if (hscroll_step_rel < 0)
8032 {
8033 hscroll_relative_p = 0;
8034 hscroll_step_abs = 0;
8035 }
8036 }
8037 else if (INTEGERP (Vhscroll_step))
8038 {
8039 hscroll_step_abs = XINT (Vhscroll_step);
8040 if (hscroll_step_abs < 0)
8041 hscroll_step_abs = 0;
8042 }
8043 else
8044 hscroll_step_abs = 0;
8045
8046 while (WINDOWP (window))
8047 {
8048 struct window *w = XWINDOW (window);
8049
8050 if (WINDOWP (w->hchild))
8051 hscrolled_p |= hscroll_window_tree (w->hchild);
8052 else if (WINDOWP (w->vchild))
8053 hscrolled_p |= hscroll_window_tree (w->vchild);
8054 else if (w->cursor.vpos >= 0)
8055 {
8056 int h_margin, text_area_x, text_area_y;
8057 int text_area_width, text_area_height;
8058 struct glyph_row *current_cursor_row
8059 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8060 struct glyph_row *desired_cursor_row
8061 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8062 struct glyph_row *cursor_row
8063 = (desired_cursor_row->enabled_p
8064 ? desired_cursor_row
8065 : current_cursor_row);
8066
8067 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8068 &text_area_width, &text_area_height);
8069
8070 /* Scroll when cursor is inside this scroll margin. */
8071 h_margin = hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
8072
8073 if ((XFASTINT (w->hscroll)
8074 && w->cursor.x <= h_margin)
8075 || (cursor_row->enabled_p
8076 && cursor_row->truncated_on_right_p
8077 && (w->cursor.x >= text_area_width - h_margin)))
8078 {
8079 struct it it;
8080 int hscroll;
8081 struct buffer *saved_current_buffer;
8082 int pt;
8083 int wanted_x;
8084
8085 /* Find point in a display of infinite width. */
8086 saved_current_buffer = current_buffer;
8087 current_buffer = XBUFFER (w->buffer);
8088
8089 if (w == XWINDOW (selected_window))
8090 pt = BUF_PT (current_buffer);
8091 else
8092 {
8093 pt = marker_position (w->pointm);
8094 pt = max (BEGV, pt);
8095 pt = min (ZV, pt);
8096 }
8097
8098 /* Move iterator to pt starting at cursor_row->start in
8099 a line with infinite width. */
8100 init_to_row_start (&it, w, cursor_row);
8101 it.last_visible_x = INFINITY;
8102 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8103 current_buffer = saved_current_buffer;
8104
8105 /* Position cursor in window. */
8106 if (!hscroll_relative_p && hscroll_step_abs == 0)
8107 hscroll = max (0, it.current_x - text_area_width / 2)
8108 / CANON_X_UNIT (it.f);
8109 else if (w->cursor.x >= text_area_width - h_margin)
8110 {
8111 if (hscroll_relative_p)
8112 wanted_x = text_area_width * (1 - hscroll_step_rel)
8113 - h_margin;
8114 else
8115 wanted_x = text_area_width
8116 - hscroll_step_abs * CANON_X_UNIT (it.f)
8117 - h_margin;
8118 hscroll
8119 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8120 }
8121 else
8122 {
8123 if (hscroll_relative_p)
8124 wanted_x = text_area_width * hscroll_step_rel
8125 + h_margin;
8126 else
8127 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
8128 + h_margin;
8129 hscroll
8130 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8131 }
8132 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8133
8134 /* Don't call Fset_window_hscroll if value hasn't
8135 changed because it will prevent redisplay
8136 optimizations. */
8137 if (XFASTINT (w->hscroll) != hscroll)
8138 {
8139 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8140 w->hscroll = make_number (hscroll);
8141 hscrolled_p = 1;
8142 }
8143 }
8144 }
8145
8146 window = w->next;
8147 }
8148
8149 /* Value is non-zero if hscroll of any leaf window has been changed. */
8150 return hscrolled_p;
8151 }
8152
8153
8154 /* Set hscroll so that cursor is visible and not inside horizontal
8155 scroll margins for all windows in the tree rooted at WINDOW. See
8156 also hscroll_window_tree above. Value is non-zero if any window's
8157 hscroll has been changed. If it has, desired matrices on the frame
8158 of WINDOW are cleared. */
8159
8160 static int
8161 hscroll_windows (window)
8162 Lisp_Object window;
8163 {
8164 int hscrolled_p;
8165
8166 if (automatic_hscrolling_p)
8167 {
8168 hscrolled_p = hscroll_window_tree (window);
8169 if (hscrolled_p)
8170 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8171 }
8172 else
8173 hscrolled_p = 0;
8174 return hscrolled_p;
8175 }
8176
8177
8178 \f
8179 /************************************************************************
8180 Redisplay
8181 ************************************************************************/
8182
8183 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8184 to a non-zero value. This is sometimes handy to have in a debugger
8185 session. */
8186
8187 #if GLYPH_DEBUG
8188
8189 /* First and last unchanged row for try_window_id. */
8190
8191 int debug_first_unchanged_at_end_vpos;
8192 int debug_last_unchanged_at_beg_vpos;
8193
8194 /* Delta vpos and y. */
8195
8196 int debug_dvpos, debug_dy;
8197
8198 /* Delta in characters and bytes for try_window_id. */
8199
8200 int debug_delta, debug_delta_bytes;
8201
8202 /* Values of window_end_pos and window_end_vpos at the end of
8203 try_window_id. */
8204
8205 EMACS_INT debug_end_pos, debug_end_vpos;
8206
8207 /* Append a string to W->desired_matrix->method. FMT is a printf
8208 format string. A1...A9 are a supplement for a variable-length
8209 argument list. If trace_redisplay_p is non-zero also printf the
8210 resulting string to stderr. */
8211
8212 static void
8213 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8214 struct window *w;
8215 char *fmt;
8216 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8217 {
8218 char buffer[512];
8219 char *method = w->desired_matrix->method;
8220 int len = strlen (method);
8221 int size = sizeof w->desired_matrix->method;
8222 int remaining = size - len - 1;
8223
8224 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8225 if (len && remaining)
8226 {
8227 method[len] = '|';
8228 --remaining, ++len;
8229 }
8230
8231 strncpy (method + len, buffer, remaining);
8232
8233 if (trace_redisplay_p)
8234 fprintf (stderr, "%p (%s): %s\n",
8235 w,
8236 ((BUFFERP (w->buffer)
8237 && STRINGP (XBUFFER (w->buffer)->name))
8238 ? (char *) SDATA (XBUFFER (w->buffer)->name)
8239 : "no buffer"),
8240 buffer);
8241 }
8242
8243 #endif /* GLYPH_DEBUG */
8244
8245
8246 /* This counter is used to clear the face cache every once in a while
8247 in redisplay_internal. It is incremented for each redisplay.
8248 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8249 cleared. */
8250
8251 #define CLEAR_FACE_CACHE_COUNT 500
8252 static int clear_face_cache_count;
8253
8254 /* Record the previous terminal frame we displayed. */
8255
8256 static struct frame *previous_terminal_frame;
8257
8258 /* Non-zero while redisplay_internal is in progress. */
8259
8260 int redisplaying_p;
8261
8262
8263 /* Value is non-zero if all changes in window W, which displays
8264 current_buffer, are in the text between START and END. START is a
8265 buffer position, END is given as a distance from Z. Used in
8266 redisplay_internal for display optimization. */
8267
8268 static INLINE int
8269 text_outside_line_unchanged_p (w, start, end)
8270 struct window *w;
8271 int start, end;
8272 {
8273 int unchanged_p = 1;
8274
8275 /* If text or overlays have changed, see where. */
8276 if (XFASTINT (w->last_modified) < MODIFF
8277 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8278 {
8279 /* Gap in the line? */
8280 if (GPT < start || Z - GPT < end)
8281 unchanged_p = 0;
8282
8283 /* Changes start in front of the line, or end after it? */
8284 if (unchanged_p
8285 && (BEG_UNCHANGED < start - 1
8286 || END_UNCHANGED < end))
8287 unchanged_p = 0;
8288
8289 /* If selective display, can't optimize if changes start at the
8290 beginning of the line. */
8291 if (unchanged_p
8292 && INTEGERP (current_buffer->selective_display)
8293 && XINT (current_buffer->selective_display) > 0
8294 && (BEG_UNCHANGED < start || GPT <= start))
8295 unchanged_p = 0;
8296
8297 /* If there are overlays at the start or end of the line, these
8298 may have overlay strings with newlines in them. A change at
8299 START, for instance, may actually concern the display of such
8300 overlay strings as well, and they are displayed on different
8301 lines. So, quickly rule out this case. (For the future, it
8302 might be desirable to implement something more telling than
8303 just BEG/END_UNCHANGED.) */
8304 if (unchanged_p)
8305 {
8306 if (BEG + BEG_UNCHANGED == start
8307 && overlay_touches_p (start))
8308 unchanged_p = 0;
8309 if (END_UNCHANGED == end
8310 && overlay_touches_p (Z - end))
8311 unchanged_p = 0;
8312 }
8313 }
8314
8315 return unchanged_p;
8316 }
8317
8318
8319 /* Do a frame update, taking possible shortcuts into account. This is
8320 the main external entry point for redisplay.
8321
8322 If the last redisplay displayed an echo area message and that message
8323 is no longer requested, we clear the echo area or bring back the
8324 mini-buffer if that is in use. */
8325
8326 void
8327 redisplay ()
8328 {
8329 redisplay_internal (0);
8330 }
8331
8332
8333 /* Return 1 if point moved out of or into a composition. Otherwise
8334 return 0. PREV_BUF and PREV_PT are the last point buffer and
8335 position. BUF and PT are the current point buffer and position. */
8336
8337 int
8338 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8339 struct buffer *prev_buf, *buf;
8340 int prev_pt, pt;
8341 {
8342 int start, end;
8343 Lisp_Object prop;
8344 Lisp_Object buffer;
8345
8346 XSETBUFFER (buffer, buf);
8347 /* Check a composition at the last point if point moved within the
8348 same buffer. */
8349 if (prev_buf == buf)
8350 {
8351 if (prev_pt == pt)
8352 /* Point didn't move. */
8353 return 0;
8354
8355 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8356 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8357 && COMPOSITION_VALID_P (start, end, prop)
8358 && start < prev_pt && end > prev_pt)
8359 /* The last point was within the composition. Return 1 iff
8360 point moved out of the composition. */
8361 return (pt <= start || pt >= end);
8362 }
8363
8364 /* Check a composition at the current point. */
8365 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8366 && find_composition (pt, -1, &start, &end, &prop, buffer)
8367 && COMPOSITION_VALID_P (start, end, prop)
8368 && start < pt && end > pt);
8369 }
8370
8371
8372 /* Reconsider the setting of B->clip_changed which is displayed
8373 in window W. */
8374
8375 static INLINE void
8376 reconsider_clip_changes (w, b)
8377 struct window *w;
8378 struct buffer *b;
8379 {
8380 if (b->prevent_redisplay_optimizations_p)
8381 b->clip_changed = 1;
8382 else if (b->clip_changed
8383 && !NILP (w->window_end_valid)
8384 && w->current_matrix->buffer == b
8385 && w->current_matrix->zv == BUF_ZV (b)
8386 && w->current_matrix->begv == BUF_BEGV (b))
8387 b->clip_changed = 0;
8388
8389 /* If display wasn't paused, and W is not a tool bar window, see if
8390 point has been moved into or out of a composition. In that case,
8391 we set b->clip_changed to 1 to force updating the screen. If
8392 b->clip_changed has already been set to 1, we can skip this
8393 check. */
8394 if (!b->clip_changed
8395 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8396 {
8397 int pt;
8398
8399 if (w == XWINDOW (selected_window))
8400 pt = BUF_PT (current_buffer);
8401 else
8402 pt = marker_position (w->pointm);
8403
8404 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8405 || pt != XINT (w->last_point))
8406 && check_point_in_composition (w->current_matrix->buffer,
8407 XINT (w->last_point),
8408 XBUFFER (w->buffer), pt))
8409 b->clip_changed = 1;
8410 }
8411 }
8412
8413
8414 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8415 response to any user action; therefore, we should preserve the echo
8416 area. (Actually, our caller does that job.) Perhaps in the future
8417 avoid recentering windows if it is not necessary; currently that
8418 causes some problems. */
8419
8420 static void
8421 redisplay_internal (preserve_echo_area)
8422 int preserve_echo_area;
8423 {
8424 struct window *w = XWINDOW (selected_window);
8425 struct frame *f = XFRAME (w->frame);
8426 int pause;
8427 int must_finish = 0;
8428 struct text_pos tlbufpos, tlendpos;
8429 int number_of_visible_frames;
8430 int count;
8431 struct frame *sf = SELECTED_FRAME ();
8432
8433 /* Non-zero means redisplay has to consider all windows on all
8434 frames. Zero means, only selected_window is considered. */
8435 int consider_all_windows_p;
8436
8437 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8438
8439 /* No redisplay if running in batch mode or frame is not yet fully
8440 initialized, or redisplay is explicitly turned off by setting
8441 Vinhibit_redisplay. */
8442 if (noninteractive
8443 || !NILP (Vinhibit_redisplay)
8444 || !f->glyphs_initialized_p)
8445 return;
8446
8447 /* The flag redisplay_performed_directly_p is set by
8448 direct_output_for_insert when it already did the whole screen
8449 update necessary. */
8450 if (redisplay_performed_directly_p)
8451 {
8452 redisplay_performed_directly_p = 0;
8453 if (!hscroll_windows (selected_window))
8454 return;
8455 }
8456
8457 #ifdef USE_X_TOOLKIT
8458 if (popup_activated ())
8459 return;
8460 #endif
8461
8462 /* I don't think this happens but let's be paranoid. */
8463 if (redisplaying_p)
8464 return;
8465
8466 /* Record a function that resets redisplaying_p to its old value
8467 when we leave this function. */
8468 count = BINDING_STACK_SIZE ();
8469 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8470 ++redisplaying_p;
8471
8472 retry:
8473 pause = 0;
8474 reconsider_clip_changes (w, current_buffer);
8475
8476 /* If new fonts have been loaded that make a glyph matrix adjustment
8477 necessary, do it. */
8478 if (fonts_changed_p)
8479 {
8480 adjust_glyphs (NULL);
8481 ++windows_or_buffers_changed;
8482 fonts_changed_p = 0;
8483 }
8484
8485 /* If face_change_count is non-zero, init_iterator will free all
8486 realized faces, which includes the faces referenced from current
8487 matrices. So, we can't reuse current matrices in this case. */
8488 if (face_change_count)
8489 ++windows_or_buffers_changed;
8490
8491 if (! FRAME_WINDOW_P (sf)
8492 && previous_terminal_frame != sf)
8493 {
8494 /* Since frames on an ASCII terminal share the same display
8495 area, displaying a different frame means redisplay the whole
8496 thing. */
8497 windows_or_buffers_changed++;
8498 SET_FRAME_GARBAGED (sf);
8499 XSETFRAME (Vterminal_frame, sf);
8500 }
8501 previous_terminal_frame = sf;
8502
8503 /* Set the visible flags for all frames. Do this before checking
8504 for resized or garbaged frames; they want to know if their frames
8505 are visible. See the comment in frame.h for
8506 FRAME_SAMPLE_VISIBILITY. */
8507 {
8508 Lisp_Object tail, frame;
8509
8510 number_of_visible_frames = 0;
8511
8512 FOR_EACH_FRAME (tail, frame)
8513 {
8514 struct frame *f = XFRAME (frame);
8515
8516 FRAME_SAMPLE_VISIBILITY (f);
8517 if (FRAME_VISIBLE_P (f))
8518 ++number_of_visible_frames;
8519 clear_desired_matrices (f);
8520 }
8521 }
8522
8523 /* Notice any pending interrupt request to change frame size. */
8524 do_pending_window_change (1);
8525
8526 /* Clear frames marked as garbaged. */
8527 if (frame_garbaged)
8528 clear_garbaged_frames ();
8529
8530 /* Build menubar and tool-bar items. */
8531 prepare_menu_bars ();
8532
8533 if (windows_or_buffers_changed)
8534 update_mode_lines++;
8535
8536 /* Detect case that we need to write or remove a star in the mode line. */
8537 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8538 {
8539 w->update_mode_line = Qt;
8540 if (buffer_shared > 1)
8541 update_mode_lines++;
8542 }
8543
8544 /* If %c is in the mode line, update it if needed. */
8545 if (!NILP (w->column_number_displayed)
8546 /* This alternative quickly identifies a common case
8547 where no change is needed. */
8548 && !(PT == XFASTINT (w->last_point)
8549 && XFASTINT (w->last_modified) >= MODIFF
8550 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8551 && XFASTINT (w->column_number_displayed) != current_column ())
8552 w->update_mode_line = Qt;
8553
8554 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8555
8556 /* The variable buffer_shared is set in redisplay_window and
8557 indicates that we redisplay a buffer in different windows. See
8558 there. */
8559 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8560
8561 /* If specs for an arrow have changed, do thorough redisplay
8562 to ensure we remove any arrow that should no longer exist. */
8563 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8564 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8565 consider_all_windows_p = windows_or_buffers_changed = 1;
8566
8567 /* Normally the message* functions will have already displayed and
8568 updated the echo area, but the frame may have been trashed, or
8569 the update may have been preempted, so display the echo area
8570 again here. Checking message_cleared_p captures the case that
8571 the echo area should be cleared. */
8572 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8573 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8574 || (message_cleared_p
8575 && minibuf_level == 0
8576 /* If the mini-window is currently selected, this means the
8577 echo-area doesn't show through. */
8578 && !MINI_WINDOW_P (XWINDOW (selected_window))))
8579 {
8580 int window_height_changed_p = echo_area_display (0);
8581 must_finish = 1;
8582
8583 /* If we don't display the current message, don't clear the
8584 message_cleared_p flag, because, if we did, we wouldn't clear
8585 the echo area in the next redisplay which doesn't preserve
8586 the echo area. */
8587 if (!display_last_displayed_message_p)
8588 message_cleared_p = 0;
8589
8590 if (fonts_changed_p)
8591 goto retry;
8592 else if (window_height_changed_p)
8593 {
8594 consider_all_windows_p = 1;
8595 ++update_mode_lines;
8596 ++windows_or_buffers_changed;
8597
8598 /* If window configuration was changed, frames may have been
8599 marked garbaged. Clear them or we will experience
8600 surprises wrt scrolling. */
8601 if (frame_garbaged)
8602 clear_garbaged_frames ();
8603 }
8604 }
8605 else if (EQ (selected_window, minibuf_window)
8606 && (current_buffer->clip_changed
8607 || XFASTINT (w->last_modified) < MODIFF
8608 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8609 && resize_mini_window (w, 0))
8610 {
8611 /* Resized active mini-window to fit the size of what it is
8612 showing if its contents might have changed. */
8613 must_finish = 1;
8614 consider_all_windows_p = 1;
8615 ++windows_or_buffers_changed;
8616 ++update_mode_lines;
8617
8618 /* If window configuration was changed, frames may have been
8619 marked garbaged. Clear them or we will experience
8620 surprises wrt scrolling. */
8621 if (frame_garbaged)
8622 clear_garbaged_frames ();
8623 }
8624
8625
8626 /* If showing the region, and mark has changed, we must redisplay
8627 the whole window. The assignment to this_line_start_pos prevents
8628 the optimization directly below this if-statement. */
8629 if (((!NILP (Vtransient_mark_mode)
8630 && !NILP (XBUFFER (w->buffer)->mark_active))
8631 != !NILP (w->region_showing))
8632 || (!NILP (w->region_showing)
8633 && !EQ (w->region_showing,
8634 Fmarker_position (XBUFFER (w->buffer)->mark))))
8635 CHARPOS (this_line_start_pos) = 0;
8636
8637 /* Optimize the case that only the line containing the cursor in the
8638 selected window has changed. Variables starting with this_ are
8639 set in display_line and record information about the line
8640 containing the cursor. */
8641 tlbufpos = this_line_start_pos;
8642 tlendpos = this_line_end_pos;
8643 if (!consider_all_windows_p
8644 && CHARPOS (tlbufpos) > 0
8645 && NILP (w->update_mode_line)
8646 && !current_buffer->clip_changed
8647 && FRAME_VISIBLE_P (XFRAME (w->frame))
8648 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8649 /* Make sure recorded data applies to current buffer, etc. */
8650 && this_line_buffer == current_buffer
8651 && current_buffer == XBUFFER (w->buffer)
8652 && NILP (w->force_start)
8653 /* Point must be on the line that we have info recorded about. */
8654 && PT >= CHARPOS (tlbufpos)
8655 && PT <= Z - CHARPOS (tlendpos)
8656 /* All text outside that line, including its final newline,
8657 must be unchanged */
8658 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8659 CHARPOS (tlendpos)))
8660 {
8661 if (CHARPOS (tlbufpos) > BEGV
8662 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8663 && (CHARPOS (tlbufpos) == ZV
8664 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8665 /* Former continuation line has disappeared by becoming empty */
8666 goto cancel;
8667 else if (XFASTINT (w->last_modified) < MODIFF
8668 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8669 || MINI_WINDOW_P (w))
8670 {
8671 /* We have to handle the case of continuation around a
8672 wide-column character (See the comment in indent.c around
8673 line 885).
8674
8675 For instance, in the following case:
8676
8677 -------- Insert --------
8678 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8679 J_I_ ==> J_I_ `^^' are cursors.
8680 ^^ ^^
8681 -------- --------
8682
8683 As we have to redraw the line above, we should goto cancel. */
8684
8685 struct it it;
8686 int line_height_before = this_line_pixel_height;
8687
8688 /* Note that start_display will handle the case that the
8689 line starting at tlbufpos is a continuation lines. */
8690 start_display (&it, w, tlbufpos);
8691
8692 /* Implementation note: It this still necessary? */
8693 if (it.current_x != this_line_start_x)
8694 goto cancel;
8695
8696 TRACE ((stderr, "trying display optimization 1\n"));
8697 w->cursor.vpos = -1;
8698 overlay_arrow_seen = 0;
8699 it.vpos = this_line_vpos;
8700 it.current_y = this_line_y;
8701 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8702 display_line (&it);
8703
8704 /* If line contains point, is not continued,
8705 and ends at same distance from eob as before, we win */
8706 if (w->cursor.vpos >= 0
8707 /* Line is not continued, otherwise this_line_start_pos
8708 would have been set to 0 in display_line. */
8709 && CHARPOS (this_line_start_pos)
8710 /* Line ends as before. */
8711 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8712 /* Line has same height as before. Otherwise other lines
8713 would have to be shifted up or down. */
8714 && this_line_pixel_height == line_height_before)
8715 {
8716 /* If this is not the window's last line, we must adjust
8717 the charstarts of the lines below. */
8718 if (it.current_y < it.last_visible_y)
8719 {
8720 struct glyph_row *row
8721 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8722 int delta, delta_bytes;
8723
8724 if (Z - CHARPOS (tlendpos) == ZV)
8725 {
8726 /* This line ends at end of (accessible part of)
8727 buffer. There is no newline to count. */
8728 delta = (Z
8729 - CHARPOS (tlendpos)
8730 - MATRIX_ROW_START_CHARPOS (row));
8731 delta_bytes = (Z_BYTE
8732 - BYTEPOS (tlendpos)
8733 - MATRIX_ROW_START_BYTEPOS (row));
8734 }
8735 else
8736 {
8737 /* This line ends in a newline. Must take
8738 account of the newline and the rest of the
8739 text that follows. */
8740 delta = (Z
8741 - CHARPOS (tlendpos)
8742 - MATRIX_ROW_START_CHARPOS (row));
8743 delta_bytes = (Z_BYTE
8744 - BYTEPOS (tlendpos)
8745 - MATRIX_ROW_START_BYTEPOS (row));
8746 }
8747
8748 increment_matrix_positions (w->current_matrix,
8749 this_line_vpos + 1,
8750 w->current_matrix->nrows,
8751 delta, delta_bytes);
8752 }
8753
8754 /* If this row displays text now but previously didn't,
8755 or vice versa, w->window_end_vpos may have to be
8756 adjusted. */
8757 if ((it.glyph_row - 1)->displays_text_p)
8758 {
8759 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8760 XSETINT (w->window_end_vpos, this_line_vpos);
8761 }
8762 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8763 && this_line_vpos > 0)
8764 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8765 w->window_end_valid = Qnil;
8766
8767 /* Update hint: No need to try to scroll in update_window. */
8768 w->desired_matrix->no_scrolling_p = 1;
8769
8770 #if GLYPH_DEBUG
8771 *w->desired_matrix->method = 0;
8772 debug_method_add (w, "optimization 1");
8773 #endif
8774 goto update;
8775 }
8776 else
8777 goto cancel;
8778 }
8779 else if (/* Cursor position hasn't changed. */
8780 PT == XFASTINT (w->last_point)
8781 /* Make sure the cursor was last displayed
8782 in this window. Otherwise we have to reposition it. */
8783 && 0 <= w->cursor.vpos
8784 && XINT (w->height) > w->cursor.vpos)
8785 {
8786 if (!must_finish)
8787 {
8788 do_pending_window_change (1);
8789
8790 /* We used to always goto end_of_redisplay here, but this
8791 isn't enough if we have a blinking cursor. */
8792 if (w->cursor_off_p == w->last_cursor_off_p)
8793 goto end_of_redisplay;
8794 }
8795 goto update;
8796 }
8797 /* If highlighting the region, or if the cursor is in the echo area,
8798 then we can't just move the cursor. */
8799 else if (! (!NILP (Vtransient_mark_mode)
8800 && !NILP (current_buffer->mark_active))
8801 && (EQ (selected_window, current_buffer->last_selected_window)
8802 || highlight_nonselected_windows)
8803 && NILP (w->region_showing)
8804 && NILP (Vshow_trailing_whitespace)
8805 && !cursor_in_echo_area)
8806 {
8807 struct it it;
8808 struct glyph_row *row;
8809
8810 /* Skip from tlbufpos to PT and see where it is. Note that
8811 PT may be in invisible text. If so, we will end at the
8812 next visible position. */
8813 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8814 NULL, DEFAULT_FACE_ID);
8815 it.current_x = this_line_start_x;
8816 it.current_y = this_line_y;
8817 it.vpos = this_line_vpos;
8818
8819 /* The call to move_it_to stops in front of PT, but
8820 moves over before-strings. */
8821 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8822
8823 if (it.vpos == this_line_vpos
8824 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8825 row->enabled_p))
8826 {
8827 xassert (this_line_vpos == it.vpos);
8828 xassert (this_line_y == it.current_y);
8829 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8830 #if GLYPH_DEBUG
8831 *w->desired_matrix->method = 0;
8832 debug_method_add (w, "optimization 3");
8833 #endif
8834 goto update;
8835 }
8836 else
8837 goto cancel;
8838 }
8839
8840 cancel:
8841 /* Text changed drastically or point moved off of line. */
8842 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8843 }
8844
8845 CHARPOS (this_line_start_pos) = 0;
8846 consider_all_windows_p |= buffer_shared > 1;
8847 ++clear_face_cache_count;
8848
8849
8850 /* Build desired matrices, and update the display. If
8851 consider_all_windows_p is non-zero, do it for all windows on all
8852 frames. Otherwise do it for selected_window, only. */
8853
8854 if (consider_all_windows_p)
8855 {
8856 Lisp_Object tail, frame;
8857 int i, n = 0, size = 50;
8858 struct frame **updated
8859 = (struct frame **) alloca (size * sizeof *updated);
8860
8861 /* Clear the face cache eventually. */
8862 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8863 {
8864 clear_face_cache (0);
8865 clear_face_cache_count = 0;
8866 }
8867
8868 /* Recompute # windows showing selected buffer. This will be
8869 incremented each time such a window is displayed. */
8870 buffer_shared = 0;
8871
8872 FOR_EACH_FRAME (tail, frame)
8873 {
8874 struct frame *f = XFRAME (frame);
8875
8876 if (FRAME_WINDOW_P (f) || f == sf)
8877 {
8878 #ifdef HAVE_WINDOW_SYSTEM
8879 if (clear_face_cache_count % 50 == 0
8880 && FRAME_WINDOW_P (f))
8881 clear_image_cache (f, 0);
8882 #endif /* HAVE_WINDOW_SYSTEM */
8883
8884 /* Mark all the scroll bars to be removed; we'll redeem
8885 the ones we want when we redisplay their windows. */
8886 if (condemn_scroll_bars_hook)
8887 condemn_scroll_bars_hook (f);
8888
8889 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8890 redisplay_windows (FRAME_ROOT_WINDOW (f));
8891
8892 /* Any scroll bars which redisplay_windows should have
8893 nuked should now go away. */
8894 if (judge_scroll_bars_hook)
8895 judge_scroll_bars_hook (f);
8896
8897 /* If fonts changed, display again. */
8898 if (fonts_changed_p)
8899 goto retry;
8900
8901 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8902 {
8903 /* See if we have to hscroll. */
8904 if (hscroll_windows (f->root_window))
8905 goto retry;
8906
8907 /* Prevent various kinds of signals during display
8908 update. stdio is not robust about handling
8909 signals, which can cause an apparent I/O
8910 error. */
8911 if (interrupt_input)
8912 unrequest_sigio ();
8913 stop_polling ();
8914
8915 /* Update the display. */
8916 set_window_update_flags (XWINDOW (f->root_window), 1);
8917 pause |= update_frame (f, 0, 0);
8918 if (pause)
8919 break;
8920
8921 if (n == size)
8922 {
8923 int nbytes = size * sizeof *updated;
8924 struct frame **p = (struct frame **) alloca (2 * nbytes);
8925 bcopy (updated, p, nbytes);
8926 size *= 2;
8927 }
8928
8929 updated[n++] = f;
8930 }
8931 }
8932 }
8933
8934 /* Do the mark_window_display_accurate after all windows have
8935 been redisplayed because this call resets flags in buffers
8936 which are needed for proper redisplay. */
8937 for (i = 0; i < n; ++i)
8938 {
8939 struct frame *f = updated[i];
8940 mark_window_display_accurate (f->root_window, 1);
8941 if (frame_up_to_date_hook)
8942 frame_up_to_date_hook (f);
8943 }
8944 }
8945 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8946 {
8947 Lisp_Object mini_window;
8948 struct frame *mini_frame;
8949
8950 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
8951 internal_condition_case_1 (redisplay_window_1, selected_window, Qerror,
8952 redisplay_window_error);
8953
8954 /* Compare desired and current matrices, perform output. */
8955 update:
8956
8957 /* If fonts changed, display again. */
8958 if (fonts_changed_p)
8959 goto retry;
8960
8961 /* Prevent various kinds of signals during display update.
8962 stdio is not robust about handling signals,
8963 which can cause an apparent I/O error. */
8964 if (interrupt_input)
8965 unrequest_sigio ();
8966 stop_polling ();
8967
8968 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8969 {
8970 if (hscroll_windows (selected_window))
8971 goto retry;
8972
8973 XWINDOW (selected_window)->must_be_updated_p = 1;
8974 pause = update_frame (sf, 0, 0);
8975 }
8976
8977 /* We may have called echo_area_display at the top of this
8978 function. If the echo area is on another frame, that may
8979 have put text on a frame other than the selected one, so the
8980 above call to update_frame would not have caught it. Catch
8981 it here. */
8982 mini_window = FRAME_MINIBUF_WINDOW (sf);
8983 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8984
8985 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8986 {
8987 XWINDOW (mini_window)->must_be_updated_p = 1;
8988 pause |= update_frame (mini_frame, 0, 0);
8989 if (!pause && hscroll_windows (mini_window))
8990 goto retry;
8991 }
8992 }
8993
8994 /* If display was paused because of pending input, make sure we do a
8995 thorough update the next time. */
8996 if (pause)
8997 {
8998 /* Prevent the optimization at the beginning of
8999 redisplay_internal that tries a single-line update of the
9000 line containing the cursor in the selected window. */
9001 CHARPOS (this_line_start_pos) = 0;
9002
9003 /* Let the overlay arrow be updated the next time. */
9004 if (!NILP (last_arrow_position))
9005 {
9006 last_arrow_position = Qt;
9007 last_arrow_string = Qt;
9008 }
9009
9010 /* If we pause after scrolling, some rows in the current
9011 matrices of some windows are not valid. */
9012 if (!WINDOW_FULL_WIDTH_P (w)
9013 && !FRAME_WINDOW_P (XFRAME (w->frame)))
9014 update_mode_lines = 1;
9015 }
9016 else
9017 {
9018 if (!consider_all_windows_p)
9019 {
9020 /* This has already been done above if
9021 consider_all_windows_p is set. */
9022 mark_window_display_accurate_1 (w, 1);
9023
9024 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9025 last_arrow_string = Voverlay_arrow_string;
9026
9027 if (frame_up_to_date_hook != 0)
9028 frame_up_to_date_hook (sf);
9029 }
9030
9031 update_mode_lines = 0;
9032 windows_or_buffers_changed = 0;
9033 }
9034
9035 /* Start SIGIO interrupts coming again. Having them off during the
9036 code above makes it less likely one will discard output, but not
9037 impossible, since there might be stuff in the system buffer here.
9038 But it is much hairier to try to do anything about that. */
9039 if (interrupt_input)
9040 request_sigio ();
9041 start_polling ();
9042
9043 /* If a frame has become visible which was not before, redisplay
9044 again, so that we display it. Expose events for such a frame
9045 (which it gets when becoming visible) don't call the parts of
9046 redisplay constructing glyphs, so simply exposing a frame won't
9047 display anything in this case. So, we have to display these
9048 frames here explicitly. */
9049 if (!pause)
9050 {
9051 Lisp_Object tail, frame;
9052 int new_count = 0;
9053
9054 FOR_EACH_FRAME (tail, frame)
9055 {
9056 int this_is_visible = 0;
9057
9058 if (XFRAME (frame)->visible)
9059 this_is_visible = 1;
9060 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9061 if (XFRAME (frame)->visible)
9062 this_is_visible = 1;
9063
9064 if (this_is_visible)
9065 new_count++;
9066 }
9067
9068 if (new_count != number_of_visible_frames)
9069 windows_or_buffers_changed++;
9070 }
9071
9072 /* Change frame size now if a change is pending. */
9073 do_pending_window_change (1);
9074
9075 /* If we just did a pending size change, or have additional
9076 visible frames, redisplay again. */
9077 if (windows_or_buffers_changed && !pause)
9078 goto retry;
9079
9080 end_of_redisplay:;
9081
9082 unbind_to (count, Qnil);
9083 }
9084
9085
9086 /* Redisplay, but leave alone any recent echo area message unless
9087 another message has been requested in its place.
9088
9089 This is useful in situations where you need to redisplay but no
9090 user action has occurred, making it inappropriate for the message
9091 area to be cleared. See tracking_off and
9092 wait_reading_process_input for examples of these situations.
9093
9094 FROM_WHERE is an integer saying from where this function was
9095 called. This is useful for debugging. */
9096
9097 void
9098 redisplay_preserve_echo_area (from_where)
9099 int from_where;
9100 {
9101 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9102
9103 if (!NILP (echo_area_buffer[1]))
9104 {
9105 /* We have a previously displayed message, but no current
9106 message. Redisplay the previous message. */
9107 display_last_displayed_message_p = 1;
9108 redisplay_internal (1);
9109 display_last_displayed_message_p = 0;
9110 }
9111 else
9112 redisplay_internal (1);
9113 }
9114
9115
9116 /* Function registered with record_unwind_protect in
9117 redisplay_internal. Clears the flag indicating that a redisplay is
9118 in progress. */
9119
9120 static Lisp_Object
9121 unwind_redisplay (old_redisplaying_p)
9122 Lisp_Object old_redisplaying_p;
9123 {
9124 redisplaying_p = XFASTINT (old_redisplaying_p);
9125 return Qnil;
9126 }
9127
9128
9129 /* Mark the display of window W as accurate or inaccurate. If
9130 ACCURATE_P is non-zero mark display of W as accurate. If
9131 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9132 redisplay_internal is called. */
9133
9134 static void
9135 mark_window_display_accurate_1 (w, accurate_p)
9136 struct window *w;
9137 int accurate_p;
9138 {
9139 if (BUFFERP (w->buffer))
9140 {
9141 struct buffer *b = XBUFFER (w->buffer);
9142
9143 w->last_modified
9144 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9145 w->last_overlay_modified
9146 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9147 w->last_had_star
9148 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9149
9150 if (accurate_p)
9151 {
9152 b->clip_changed = 0;
9153 b->prevent_redisplay_optimizations_p = 0;
9154
9155 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9156 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9157 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9158 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9159
9160 w->current_matrix->buffer = b;
9161 w->current_matrix->begv = BUF_BEGV (b);
9162 w->current_matrix->zv = BUF_ZV (b);
9163
9164 w->last_cursor = w->cursor;
9165 w->last_cursor_off_p = w->cursor_off_p;
9166
9167 if (w == XWINDOW (selected_window))
9168 w->last_point = make_number (BUF_PT (b));
9169 else
9170 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9171 }
9172 }
9173
9174 if (accurate_p)
9175 {
9176 w->window_end_valid = w->buffer;
9177 #if 0 /* This is incorrect with variable-height lines. */
9178 xassert (XINT (w->window_end_vpos)
9179 < (XINT (w->height)
9180 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9181 #endif
9182 w->update_mode_line = Qnil;
9183 }
9184 }
9185
9186
9187 /* Mark the display of windows in the window tree rooted at WINDOW as
9188 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9189 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9190 be redisplayed the next time redisplay_internal is called. */
9191
9192 void
9193 mark_window_display_accurate (window, accurate_p)
9194 Lisp_Object window;
9195 int accurate_p;
9196 {
9197 struct window *w;
9198
9199 for (; !NILP (window); window = w->next)
9200 {
9201 w = XWINDOW (window);
9202 mark_window_display_accurate_1 (w, accurate_p);
9203
9204 if (!NILP (w->vchild))
9205 mark_window_display_accurate (w->vchild, accurate_p);
9206 if (!NILP (w->hchild))
9207 mark_window_display_accurate (w->hchild, accurate_p);
9208 }
9209
9210 if (accurate_p)
9211 {
9212 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9213 last_arrow_string = Voverlay_arrow_string;
9214 }
9215 else
9216 {
9217 /* Force a thorough redisplay the next time by setting
9218 last_arrow_position and last_arrow_string to t, which is
9219 unequal to any useful value of Voverlay_arrow_... */
9220 last_arrow_position = Qt;
9221 last_arrow_string = Qt;
9222 }
9223 }
9224
9225
9226 /* Return value in display table DP (Lisp_Char_Table *) for character
9227 C. Since a display table doesn't have any parent, we don't have to
9228 follow parent. Do not call this function directly but use the
9229 macro DISP_CHAR_VECTOR. */
9230
9231 Lisp_Object
9232 disp_char_vector (dp, c)
9233 struct Lisp_Char_Table *dp;
9234 int c;
9235 {
9236 int code[4], i;
9237 Lisp_Object val;
9238
9239 if (SINGLE_BYTE_CHAR_P (c))
9240 return (dp->contents[c]);
9241
9242 SPLIT_CHAR (c, code[0], code[1], code[2]);
9243 if (code[1] < 32)
9244 code[1] = -1;
9245 else if (code[2] < 32)
9246 code[2] = -1;
9247
9248 /* Here, the possible range of code[0] (== charset ID) is
9249 128..max_charset. Since the top level char table contains data
9250 for multibyte characters after 256th element, we must increment
9251 code[0] by 128 to get a correct index. */
9252 code[0] += 128;
9253 code[3] = -1; /* anchor */
9254
9255 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9256 {
9257 val = dp->contents[code[i]];
9258 if (!SUB_CHAR_TABLE_P (val))
9259 return (NILP (val) ? dp->defalt : val);
9260 }
9261
9262 /* Here, val is a sub char table. We return the default value of
9263 it. */
9264 return (dp->defalt);
9265 }
9266
9267
9268 \f
9269 /***********************************************************************
9270 Window Redisplay
9271 ***********************************************************************/
9272
9273 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9274
9275 static void
9276 redisplay_windows (window)
9277 Lisp_Object window;
9278 {
9279 while (!NILP (window))
9280 {
9281 struct window *w = XWINDOW (window);
9282
9283 if (!NILP (w->hchild))
9284 redisplay_windows (w->hchild);
9285 else if (!NILP (w->vchild))
9286 redisplay_windows (w->vchild);
9287 else
9288 {
9289 displayed_buffer = XBUFFER (w->buffer);
9290 internal_condition_case_1 (redisplay_window_0, window, Qerror,
9291 redisplay_window_error);
9292 }
9293
9294 window = w->next;
9295 }
9296 }
9297
9298 static Lisp_Object
9299 redisplay_window_error ()
9300 {
9301 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9302 return Qnil;
9303 }
9304
9305 static Lisp_Object
9306 redisplay_window_0 (window)
9307 Lisp_Object window;
9308 {
9309 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9310 redisplay_window (window, 0);
9311 return Qnil;
9312 }
9313
9314 static Lisp_Object
9315 redisplay_window_1 (window)
9316 Lisp_Object window;
9317 {
9318 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9319 redisplay_window (window, 1);
9320 return Qnil;
9321 }
9322 \f
9323 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9324 DELTA is the number of bytes by which positions recorded in ROW
9325 differ from current buffer positions. */
9326
9327 void
9328 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9329 struct window *w;
9330 struct glyph_row *row;
9331 struct glyph_matrix *matrix;
9332 int delta, delta_bytes, dy, dvpos;
9333 {
9334 struct glyph *glyph = row->glyphs[TEXT_AREA];
9335 struct glyph *end = glyph + row->used[TEXT_AREA];
9336 int x = row->x;
9337 int pt_old = PT - delta;
9338
9339 /* Skip over glyphs not having an object at the start of the row.
9340 These are special glyphs like truncation marks on terminal
9341 frames. */
9342 if (row->displays_text_p)
9343 while (glyph < end
9344 && INTEGERP (glyph->object)
9345 && glyph->charpos < 0)
9346 {
9347 x += glyph->pixel_width;
9348 ++glyph;
9349 }
9350
9351 while (glyph < end
9352 && !INTEGERP (glyph->object)
9353 && (!BUFFERP (glyph->object)
9354 || glyph->charpos < pt_old))
9355 {
9356 x += glyph->pixel_width;
9357 ++glyph;
9358 }
9359
9360 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9361 w->cursor.x = x;
9362 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9363 w->cursor.y = row->y + dy;
9364
9365 if (w == XWINDOW (selected_window))
9366 {
9367 if (!row->continued_p
9368 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9369 && row->x == 0)
9370 {
9371 this_line_buffer = XBUFFER (w->buffer);
9372
9373 CHARPOS (this_line_start_pos)
9374 = MATRIX_ROW_START_CHARPOS (row) + delta;
9375 BYTEPOS (this_line_start_pos)
9376 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9377
9378 CHARPOS (this_line_end_pos)
9379 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9380 BYTEPOS (this_line_end_pos)
9381 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9382
9383 this_line_y = w->cursor.y;
9384 this_line_pixel_height = row->height;
9385 this_line_vpos = w->cursor.vpos;
9386 this_line_start_x = row->x;
9387 }
9388 else
9389 CHARPOS (this_line_start_pos) = 0;
9390 }
9391 }
9392
9393
9394 /* Run window scroll functions, if any, for WINDOW with new window
9395 start STARTP. Sets the window start of WINDOW to that position.
9396
9397 We assume that the window's buffer is really current. */
9398
9399 static INLINE struct text_pos
9400 run_window_scroll_functions (window, startp)
9401 Lisp_Object window;
9402 struct text_pos startp;
9403 {
9404 struct window *w = XWINDOW (window);
9405 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9406
9407 if (current_buffer != XBUFFER (w->buffer))
9408 abort ();
9409
9410 if (!NILP (Vwindow_scroll_functions))
9411 {
9412 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9413 make_number (CHARPOS (startp)));
9414 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9415 /* In case the hook functions switch buffers. */
9416 if (current_buffer != XBUFFER (w->buffer))
9417 set_buffer_internal_1 (XBUFFER (w->buffer));
9418 }
9419
9420 return startp;
9421 }
9422
9423
9424 /* Modify the desired matrix of window W and W->vscroll so that the
9425 line containing the cursor is fully visible. If this requires
9426 larger matrices than are allocated, set fonts_changed_p and return
9427 0. */
9428
9429 static int
9430 make_cursor_line_fully_visible (w)
9431 struct window *w;
9432 {
9433 struct glyph_matrix *matrix;
9434 struct glyph_row *row;
9435 int window_height;
9436
9437 /* It's not always possible to find the cursor, e.g, when a window
9438 is full of overlay strings. Don't do anything in that case. */
9439 if (w->cursor.vpos < 0)
9440 return 1;
9441
9442 matrix = w->desired_matrix;
9443 row = MATRIX_ROW (matrix, w->cursor.vpos);
9444
9445 /* If the cursor row is not partially visible, there's nothing
9446 to do. */
9447 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9448 return 1;
9449
9450 /* If the row the cursor is in is taller than the window's height,
9451 it's not clear what to do, so do nothing. */
9452 window_height = window_box_height (w);
9453 if (row->height >= window_height)
9454 return 1;
9455
9456 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9457 {
9458 int dy = row->height - row->visible_height;
9459 w->vscroll = 0;
9460 w->cursor.y += dy;
9461 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9462 }
9463 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9464 {
9465 int dy = - (row->height - row->visible_height);
9466 w->vscroll = dy;
9467 w->cursor.y += dy;
9468 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9469 }
9470
9471 /* When we change the cursor y-position of the selected window,
9472 change this_line_y as well so that the display optimization for
9473 the cursor line of the selected window in redisplay_internal uses
9474 the correct y-position. */
9475 if (w == XWINDOW (selected_window))
9476 this_line_y = w->cursor.y;
9477
9478 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9479 redisplay with larger matrices. */
9480 if (matrix->nrows < required_matrix_height (w))
9481 {
9482 fonts_changed_p = 1;
9483 return 0;
9484 }
9485
9486 return 1;
9487 }
9488
9489
9490 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9491 non-zero means only WINDOW is redisplayed in redisplay_internal.
9492 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9493 in redisplay_window to bring a partially visible line into view in
9494 the case that only the cursor has moved.
9495
9496 Value is
9497
9498 1 if scrolling succeeded
9499
9500 0 if scrolling didn't find point.
9501
9502 -1 if new fonts have been loaded so that we must interrupt
9503 redisplay, adjust glyph matrices, and try again. */
9504
9505 enum
9506 {
9507 SCROLLING_SUCCESS,
9508 SCROLLING_FAILED,
9509 SCROLLING_NEED_LARGER_MATRICES
9510 };
9511
9512 static int
9513 try_scrolling (window, just_this_one_p, scroll_conservatively,
9514 scroll_step, temp_scroll_step)
9515 Lisp_Object window;
9516 int just_this_one_p;
9517 EMACS_INT scroll_conservatively, scroll_step;
9518 int temp_scroll_step;
9519 {
9520 struct window *w = XWINDOW (window);
9521 struct frame *f = XFRAME (w->frame);
9522 struct text_pos scroll_margin_pos;
9523 struct text_pos pos;
9524 struct text_pos startp;
9525 struct it it;
9526 Lisp_Object window_end;
9527 int this_scroll_margin;
9528 int dy = 0;
9529 int scroll_max;
9530 int rc;
9531 int amount_to_scroll = 0;
9532 Lisp_Object aggressive;
9533 int height;
9534
9535 #if GLYPH_DEBUG
9536 debug_method_add (w, "try_scrolling");
9537 #endif
9538
9539 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9540
9541 /* Compute scroll margin height in pixels. We scroll when point is
9542 within this distance from the top or bottom of the window. */
9543 if (scroll_margin > 0)
9544 {
9545 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9546 this_scroll_margin *= CANON_Y_UNIT (f);
9547 }
9548 else
9549 this_scroll_margin = 0;
9550
9551 /* Compute how much we should try to scroll maximally to bring point
9552 into view. */
9553 if (scroll_step || scroll_conservatively || temp_scroll_step)
9554 scroll_max = max (scroll_step,
9555 max (scroll_conservatively, temp_scroll_step));
9556 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9557 || NUMBERP (current_buffer->scroll_up_aggressively))
9558 /* We're trying to scroll because of aggressive scrolling
9559 but no scroll_step is set. Choose an arbitrary one. Maybe
9560 there should be a variable for this. */
9561 scroll_max = 10;
9562 else
9563 scroll_max = 0;
9564 scroll_max *= CANON_Y_UNIT (f);
9565
9566 /* Decide whether we have to scroll down. Start at the window end
9567 and move this_scroll_margin up to find the position of the scroll
9568 margin. */
9569 window_end = Fwindow_end (window, Qt);
9570 CHARPOS (scroll_margin_pos) = XINT (window_end);
9571 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9572 if (this_scroll_margin)
9573 {
9574 start_display (&it, w, scroll_margin_pos);
9575 move_it_vertically (&it, - this_scroll_margin);
9576 scroll_margin_pos = it.current.pos;
9577 }
9578
9579 if (PT >= CHARPOS (scroll_margin_pos))
9580 {
9581 int y0;
9582
9583 /* Point is in the scroll margin at the bottom of the window, or
9584 below. Compute a new window start that makes point visible. */
9585
9586 /* Compute the distance from the scroll margin to PT.
9587 Give up if the distance is greater than scroll_max. */
9588 start_display (&it, w, scroll_margin_pos);
9589 y0 = it.current_y;
9590 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9591 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9592
9593 /* To make point visible, we have to move the window start
9594 down so that the line the cursor is in is visible, which
9595 means we have to add in the height of the cursor line. */
9596 dy = line_bottom_y (&it) - y0;
9597
9598 if (dy > scroll_max)
9599 return SCROLLING_FAILED;
9600
9601 /* Move the window start down. If scrolling conservatively,
9602 move it just enough down to make point visible. If
9603 scroll_step is set, move it down by scroll_step. */
9604 start_display (&it, w, startp);
9605
9606 if (scroll_conservatively)
9607 amount_to_scroll
9608 = max (max (dy, CANON_Y_UNIT (f)),
9609 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9610 else if (scroll_step || temp_scroll_step)
9611 amount_to_scroll = scroll_max;
9612 else
9613 {
9614 aggressive = current_buffer->scroll_up_aggressively;
9615 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9616 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9617 if (NUMBERP (aggressive))
9618 amount_to_scroll = XFLOATINT (aggressive) * height;
9619 }
9620
9621 if (amount_to_scroll <= 0)
9622 return SCROLLING_FAILED;
9623
9624 move_it_vertically (&it, amount_to_scroll);
9625 startp = it.current.pos;
9626 }
9627 else
9628 {
9629 /* See if point is inside the scroll margin at the top of the
9630 window. */
9631 scroll_margin_pos = startp;
9632 if (this_scroll_margin)
9633 {
9634 start_display (&it, w, startp);
9635 move_it_vertically (&it, this_scroll_margin);
9636 scroll_margin_pos = it.current.pos;
9637 }
9638
9639 if (PT < CHARPOS (scroll_margin_pos))
9640 {
9641 /* Point is in the scroll margin at the top of the window or
9642 above what is displayed in the window. */
9643 int y0;
9644
9645 /* Compute the vertical distance from PT to the scroll
9646 margin position. Give up if distance is greater than
9647 scroll_max. */
9648 SET_TEXT_POS (pos, PT, PT_BYTE);
9649 start_display (&it, w, pos);
9650 y0 = it.current_y;
9651 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9652 it.last_visible_y, -1,
9653 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9654 dy = it.current_y - y0;
9655 if (dy > scroll_max)
9656 return SCROLLING_FAILED;
9657
9658 /* Compute new window start. */
9659 start_display (&it, w, startp);
9660
9661 if (scroll_conservatively)
9662 amount_to_scroll =
9663 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9664 else if (scroll_step || temp_scroll_step)
9665 amount_to_scroll = scroll_max;
9666 else
9667 {
9668 aggressive = current_buffer->scroll_down_aggressively;
9669 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9670 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9671 if (NUMBERP (aggressive))
9672 amount_to_scroll = XFLOATINT (aggressive) * height;
9673 }
9674
9675 if (amount_to_scroll <= 0)
9676 return SCROLLING_FAILED;
9677
9678 move_it_vertically (&it, - amount_to_scroll);
9679 startp = it.current.pos;
9680 }
9681 }
9682
9683 /* Run window scroll functions. */
9684 startp = run_window_scroll_functions (window, startp);
9685
9686 /* Display the window. Give up if new fonts are loaded, or if point
9687 doesn't appear. */
9688 if (!try_window (window, startp))
9689 rc = SCROLLING_NEED_LARGER_MATRICES;
9690 else if (w->cursor.vpos < 0)
9691 {
9692 clear_glyph_matrix (w->desired_matrix);
9693 rc = SCROLLING_FAILED;
9694 }
9695 else
9696 {
9697 /* Maybe forget recorded base line for line number display. */
9698 if (!just_this_one_p
9699 || current_buffer->clip_changed
9700 || BEG_UNCHANGED < CHARPOS (startp))
9701 w->base_line_number = Qnil;
9702
9703 /* If cursor ends up on a partially visible line, shift display
9704 lines up or down. If that fails because we need larger
9705 matrices, give up. */
9706 if (!make_cursor_line_fully_visible (w))
9707 rc = SCROLLING_NEED_LARGER_MATRICES;
9708 else
9709 rc = SCROLLING_SUCCESS;
9710 }
9711
9712 return rc;
9713 }
9714
9715
9716 /* Compute a suitable window start for window W if display of W starts
9717 on a continuation line. Value is non-zero if a new window start
9718 was computed.
9719
9720 The new window start will be computed, based on W's width, starting
9721 from the start of the continued line. It is the start of the
9722 screen line with the minimum distance from the old start W->start. */
9723
9724 static int
9725 compute_window_start_on_continuation_line (w)
9726 struct window *w;
9727 {
9728 struct text_pos pos, start_pos;
9729 int window_start_changed_p = 0;
9730
9731 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9732
9733 /* If window start is on a continuation line... Window start may be
9734 < BEGV in case there's invisible text at the start of the
9735 buffer (M-x rmail, for example). */
9736 if (CHARPOS (start_pos) > BEGV
9737 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9738 {
9739 struct it it;
9740 struct glyph_row *row;
9741
9742 /* Handle the case that the window start is out of range. */
9743 if (CHARPOS (start_pos) < BEGV)
9744 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9745 else if (CHARPOS (start_pos) > ZV)
9746 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9747
9748 /* Find the start of the continued line. This should be fast
9749 because scan_buffer is fast (newline cache). */
9750 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9751 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9752 row, DEFAULT_FACE_ID);
9753 reseat_at_previous_visible_line_start (&it);
9754
9755 /* If the line start is "too far" away from the window start,
9756 say it takes too much time to compute a new window start. */
9757 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9758 < XFASTINT (w->height) * XFASTINT (w->width))
9759 {
9760 int min_distance, distance;
9761
9762 /* Move forward by display lines to find the new window
9763 start. If window width was enlarged, the new start can
9764 be expected to be > the old start. If window width was
9765 decreased, the new window start will be < the old start.
9766 So, we're looking for the display line start with the
9767 minimum distance from the old window start. */
9768 pos = it.current.pos;
9769 min_distance = INFINITY;
9770 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9771 distance < min_distance)
9772 {
9773 min_distance = distance;
9774 pos = it.current.pos;
9775 move_it_by_lines (&it, 1, 0);
9776 }
9777
9778 /* Set the window start there. */
9779 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9780 window_start_changed_p = 1;
9781 }
9782 }
9783
9784 return window_start_changed_p;
9785 }
9786
9787
9788 /* Try cursor movement in case text has not changes in window WINDOW,
9789 with window start STARTP. Value is
9790
9791 CURSOR_MOVEMENT_SUCCESS if successful
9792
9793 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9794
9795 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9796 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9797 we want to scroll as if scroll-step were set to 1. See the code.
9798
9799 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9800 which case we have to abort this redisplay, and adjust matrices
9801 first. */
9802
9803 enum
9804 {
9805 CURSOR_MOVEMENT_SUCCESS,
9806 CURSOR_MOVEMENT_CANNOT_BE_USED,
9807 CURSOR_MOVEMENT_MUST_SCROLL,
9808 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9809 };
9810
9811 static int
9812 try_cursor_movement (window, startp, scroll_step)
9813 Lisp_Object window;
9814 struct text_pos startp;
9815 int *scroll_step;
9816 {
9817 struct window *w = XWINDOW (window);
9818 struct frame *f = XFRAME (w->frame);
9819 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
9820
9821 #if GLYPH_DEBUG
9822 if (inhibit_try_cursor_movement)
9823 return rc;
9824 #endif
9825
9826 /* Handle case where text has not changed, only point, and it has
9827 not moved off the frame. */
9828 if (/* Point may be in this window. */
9829 PT >= CHARPOS (startp)
9830 /* Selective display hasn't changed. */
9831 && !current_buffer->clip_changed
9832 /* Function force-mode-line-update is used to force a thorough
9833 redisplay. It sets either windows_or_buffers_changed or
9834 update_mode_lines. So don't take a shortcut here for these
9835 cases. */
9836 && !update_mode_lines
9837 && !windows_or_buffers_changed
9838 /* Can't use this case if highlighting a region. When a
9839 region exists, cursor movement has to do more than just
9840 set the cursor. */
9841 && !(!NILP (Vtransient_mark_mode)
9842 && !NILP (current_buffer->mark_active))
9843 && NILP (w->region_showing)
9844 && NILP (Vshow_trailing_whitespace)
9845 /* Right after splitting windows, last_point may be nil. */
9846 && INTEGERP (w->last_point)
9847 /* This code is not used for mini-buffer for the sake of the case
9848 of redisplaying to replace an echo area message; since in
9849 that case the mini-buffer contents per se are usually
9850 unchanged. This code is of no real use in the mini-buffer
9851 since the handling of this_line_start_pos, etc., in redisplay
9852 handles the same cases. */
9853 && !EQ (window, minibuf_window)
9854 /* When splitting windows or for new windows, it happens that
9855 redisplay is called with a nil window_end_vpos or one being
9856 larger than the window. This should really be fixed in
9857 window.c. I don't have this on my list, now, so we do
9858 approximately the same as the old redisplay code. --gerd. */
9859 && INTEGERP (w->window_end_vpos)
9860 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9861 && (FRAME_WINDOW_P (f)
9862 || !MARKERP (Voverlay_arrow_position)
9863 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9864 {
9865 int this_scroll_margin;
9866 struct glyph_row *row = NULL;
9867
9868 #if GLYPH_DEBUG
9869 debug_method_add (w, "cursor movement");
9870 #endif
9871
9872 /* Scroll if point within this distance from the top or bottom
9873 of the window. This is a pixel value. */
9874 this_scroll_margin = max (0, scroll_margin);
9875 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9876 this_scroll_margin *= CANON_Y_UNIT (f);
9877
9878 /* Start with the row the cursor was displayed during the last
9879 not paused redisplay. Give up if that row is not valid. */
9880 if (w->last_cursor.vpos < 0
9881 || w->last_cursor.vpos >= w->current_matrix->nrows)
9882 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9883 else
9884 {
9885 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9886 if (row->mode_line_p)
9887 ++row;
9888 if (!row->enabled_p)
9889 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9890 }
9891
9892 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
9893 {
9894 int scroll_p = 0;
9895 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9896
9897 if (PT > XFASTINT (w->last_point))
9898 {
9899 /* Point has moved forward. */
9900 while (MATRIX_ROW_END_CHARPOS (row) < PT
9901 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9902 {
9903 xassert (row->enabled_p);
9904 ++row;
9905 }
9906
9907 /* The end position of a row equals the start position
9908 of the next row. If PT is there, we would rather
9909 display it in the next line. */
9910 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9911 && MATRIX_ROW_END_CHARPOS (row) == PT
9912 && !cursor_row_p (w, row))
9913 ++row;
9914
9915 /* If within the scroll margin, scroll. Note that
9916 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9917 the next line would be drawn, and that
9918 this_scroll_margin can be zero. */
9919 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9920 || PT > MATRIX_ROW_END_CHARPOS (row)
9921 /* Line is completely visible last line in window
9922 and PT is to be set in the next line. */
9923 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9924 && PT == MATRIX_ROW_END_CHARPOS (row)
9925 && !row->ends_at_zv_p
9926 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9927 scroll_p = 1;
9928 }
9929 else if (PT < XFASTINT (w->last_point))
9930 {
9931 /* Cursor has to be moved backward. Note that PT >=
9932 CHARPOS (startp) because of the outer
9933 if-statement. */
9934 while (!row->mode_line_p
9935 && (MATRIX_ROW_START_CHARPOS (row) > PT
9936 || (MATRIX_ROW_START_CHARPOS (row) == PT
9937 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9938 && (row->y > this_scroll_margin
9939 || CHARPOS (startp) == BEGV))
9940 {
9941 xassert (row->enabled_p);
9942 --row;
9943 }
9944
9945 /* Consider the following case: Window starts at BEGV,
9946 there is invisible, intangible text at BEGV, so that
9947 display starts at some point START > BEGV. It can
9948 happen that we are called with PT somewhere between
9949 BEGV and START. Try to handle that case. */
9950 if (row < w->current_matrix->rows
9951 || row->mode_line_p)
9952 {
9953 row = w->current_matrix->rows;
9954 if (row->mode_line_p)
9955 ++row;
9956 }
9957
9958 /* Due to newlines in overlay strings, we may have to
9959 skip forward over overlay strings. */
9960 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9961 && MATRIX_ROW_END_CHARPOS (row) == PT
9962 && !cursor_row_p (w, row))
9963 ++row;
9964
9965 /* If within the scroll margin, scroll. */
9966 if (row->y < this_scroll_margin
9967 && CHARPOS (startp) != BEGV)
9968 scroll_p = 1;
9969 }
9970
9971 if (PT < MATRIX_ROW_START_CHARPOS (row)
9972 || PT > MATRIX_ROW_END_CHARPOS (row))
9973 {
9974 /* if PT is not in the glyph row, give up. */
9975 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9976 }
9977 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9978 {
9979 if (PT == MATRIX_ROW_END_CHARPOS (row)
9980 && !row->ends_at_zv_p
9981 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
9982 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9983 else if (row->height > window_box_height (w))
9984 {
9985 /* If we end up in a partially visible line, let's
9986 make it fully visible, except when it's taller
9987 than the window, in which case we can't do much
9988 about it. */
9989 *scroll_step = 1;
9990 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9991 }
9992 else
9993 {
9994 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9995 try_window (window, startp);
9996 if (!make_cursor_line_fully_visible (w))
9997 rc = CURSOR_MOVEMENT_NEED_LARGER_MATRICES;
9998 else
9999 rc = CURSOR_MOVEMENT_SUCCESS;
10000 }
10001 }
10002 else if (scroll_p)
10003 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10004 else
10005 {
10006 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10007 rc = CURSOR_MOVEMENT_SUCCESS;
10008 }
10009 }
10010 }
10011
10012 return rc;
10013 }
10014
10015
10016 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
10017 selected_window is redisplayed. */
10018
10019 static void
10020 redisplay_window (window, just_this_one_p)
10021 Lisp_Object window;
10022 int just_this_one_p;
10023 {
10024 struct window *w = XWINDOW (window);
10025 struct frame *f = XFRAME (w->frame);
10026 struct buffer *buffer = XBUFFER (w->buffer);
10027 struct buffer *old = current_buffer;
10028 struct text_pos lpoint, opoint, startp;
10029 int update_mode_line;
10030 int tem;
10031 struct it it;
10032 /* Record it now because it's overwritten. */
10033 int current_matrix_up_to_date_p = 0;
10034 int temp_scroll_step = 0;
10035 int count = BINDING_STACK_SIZE ();
10036 int rc;
10037
10038 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10039 opoint = lpoint;
10040
10041 /* W must be a leaf window here. */
10042 xassert (!NILP (w->buffer));
10043 #if GLYPH_DEBUG
10044 *w->desired_matrix->method = 0;
10045 #endif
10046
10047 specbind (Qinhibit_point_motion_hooks, Qt);
10048
10049 reconsider_clip_changes (w, buffer);
10050
10051 /* Has the mode line to be updated? */
10052 update_mode_line = (!NILP (w->update_mode_line)
10053 || update_mode_lines
10054 || buffer->clip_changed);
10055
10056 if (MINI_WINDOW_P (w))
10057 {
10058 if (w == XWINDOW (echo_area_window)
10059 && !NILP (echo_area_buffer[0]))
10060 {
10061 if (update_mode_line)
10062 /* We may have to update a tty frame's menu bar or a
10063 tool-bar. Example `M-x C-h C-h C-g'. */
10064 goto finish_menu_bars;
10065 else
10066 /* We've already displayed the echo area glyphs in this window. */
10067 goto finish_scroll_bars;
10068 }
10069 else if (w != XWINDOW (minibuf_window))
10070 {
10071 /* W is a mini-buffer window, but it's not the currently
10072 active one, so clear it. */
10073 int yb = window_text_bottom_y (w);
10074 struct glyph_row *row;
10075 int y;
10076
10077 for (y = 0, row = w->desired_matrix->rows;
10078 y < yb;
10079 y += row->height, ++row)
10080 blank_row (w, row, y);
10081 goto finish_scroll_bars;
10082 }
10083
10084 clear_glyph_matrix (w->desired_matrix);
10085 }
10086
10087 /* Otherwise set up data on this window; select its buffer and point
10088 value. */
10089 /* Really select the buffer, for the sake of buffer-local
10090 variables. */
10091 set_buffer_internal_1 (XBUFFER (w->buffer));
10092 SET_TEXT_POS (opoint, PT, PT_BYTE);
10093
10094 current_matrix_up_to_date_p
10095 = (!NILP (w->window_end_valid)
10096 && !current_buffer->clip_changed
10097 && XFASTINT (w->last_modified) >= MODIFF
10098 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10099
10100 /* When windows_or_buffers_changed is non-zero, we can't rely on
10101 the window end being valid, so set it to nil there. */
10102 if (windows_or_buffers_changed)
10103 {
10104 /* If window starts on a continuation line, maybe adjust the
10105 window start in case the window's width changed. */
10106 if (XMARKER (w->start)->buffer == current_buffer)
10107 compute_window_start_on_continuation_line (w);
10108
10109 w->window_end_valid = Qnil;
10110 }
10111
10112 /* Some sanity checks. */
10113 CHECK_WINDOW_END (w);
10114 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
10115 abort ();
10116 if (BYTEPOS (opoint) < CHARPOS (opoint))
10117 abort ();
10118
10119 /* If %c is in mode line, update it if needed. */
10120 if (!NILP (w->column_number_displayed)
10121 /* This alternative quickly identifies a common case
10122 where no change is needed. */
10123 && !(PT == XFASTINT (w->last_point)
10124 && XFASTINT (w->last_modified) >= MODIFF
10125 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10126 && XFASTINT (w->column_number_displayed) != current_column ())
10127 update_mode_line = 1;
10128
10129 /* Count number of windows showing the selected buffer. An indirect
10130 buffer counts as its base buffer. */
10131 if (!just_this_one_p)
10132 {
10133 struct buffer *current_base, *window_base;
10134 current_base = current_buffer;
10135 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10136 if (current_base->base_buffer)
10137 current_base = current_base->base_buffer;
10138 if (window_base->base_buffer)
10139 window_base = window_base->base_buffer;
10140 if (current_base == window_base)
10141 buffer_shared++;
10142 }
10143
10144 /* Point refers normally to the selected window. For any other
10145 window, set up appropriate value. */
10146 if (!EQ (window, selected_window))
10147 {
10148 int new_pt = XMARKER (w->pointm)->charpos;
10149 int new_pt_byte = marker_byte_position (w->pointm);
10150 if (new_pt < BEGV)
10151 {
10152 new_pt = BEGV;
10153 new_pt_byte = BEGV_BYTE;
10154 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10155 }
10156 else if (new_pt > (ZV - 1))
10157 {
10158 new_pt = ZV;
10159 new_pt_byte = ZV_BYTE;
10160 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10161 }
10162
10163 /* We don't use SET_PT so that the point-motion hooks don't run. */
10164 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10165 }
10166
10167 /* If any of the character widths specified in the display table
10168 have changed, invalidate the width run cache. It's true that
10169 this may be a bit late to catch such changes, but the rest of
10170 redisplay goes (non-fatally) haywire when the display table is
10171 changed, so why should we worry about doing any better? */
10172 if (current_buffer->width_run_cache)
10173 {
10174 struct Lisp_Char_Table *disptab = buffer_display_table ();
10175
10176 if (! disptab_matches_widthtab (disptab,
10177 XVECTOR (current_buffer->width_table)))
10178 {
10179 invalidate_region_cache (current_buffer,
10180 current_buffer->width_run_cache,
10181 BEG, Z);
10182 recompute_width_table (current_buffer, disptab);
10183 }
10184 }
10185
10186 /* If window-start is screwed up, choose a new one. */
10187 if (XMARKER (w->start)->buffer != current_buffer)
10188 goto recenter;
10189
10190 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10191
10192 /* If someone specified a new starting point but did not insist,
10193 check whether it can be used. */
10194 if (!NILP (w->optional_new_start)
10195 && CHARPOS (startp) >= BEGV
10196 && CHARPOS (startp) <= ZV)
10197 {
10198 w->optional_new_start = Qnil;
10199 start_display (&it, w, startp);
10200 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10201 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10202 if (IT_CHARPOS (it) == PT)
10203 w->force_start = Qt;
10204 }
10205
10206 /* Handle case where place to start displaying has been specified,
10207 unless the specified location is outside the accessible range. */
10208 if (!NILP (w->force_start)
10209 || w->frozen_window_start_p)
10210 {
10211 w->force_start = Qnil;
10212 w->vscroll = 0;
10213 w->window_end_valid = Qnil;
10214
10215 /* Forget any recorded base line for line number display. */
10216 if (!current_matrix_up_to_date_p
10217 || current_buffer->clip_changed)
10218 w->base_line_number = Qnil;
10219
10220 /* Redisplay the mode line. Select the buffer properly for that.
10221 Also, run the hook window-scroll-functions
10222 because we have scrolled. */
10223 /* Note, we do this after clearing force_start because
10224 if there's an error, it is better to forget about force_start
10225 than to get into an infinite loop calling the hook functions
10226 and having them get more errors. */
10227 if (!update_mode_line
10228 || ! NILP (Vwindow_scroll_functions))
10229 {
10230 update_mode_line = 1;
10231 w->update_mode_line = Qt;
10232 startp = run_window_scroll_functions (window, startp);
10233 }
10234
10235 w->last_modified = make_number (0);
10236 w->last_overlay_modified = make_number (0);
10237 if (CHARPOS (startp) < BEGV)
10238 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10239 else if (CHARPOS (startp) > ZV)
10240 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10241
10242 /* Redisplay, then check if cursor has been set during the
10243 redisplay. Give up if new fonts were loaded. */
10244 if (!try_window (window, startp))
10245 {
10246 w->force_start = Qt;
10247 clear_glyph_matrix (w->desired_matrix);
10248 goto finish_scroll_bars;
10249 }
10250
10251 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10252 {
10253 /* If point does not appear, try to move point so it does
10254 appear. The desired matrix has been built above, so we
10255 can use it here. */
10256 int window_height;
10257 struct glyph_row *row;
10258
10259 window_height = window_box_height (w) / 2;
10260 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10261 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
10262 ++row;
10263
10264 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10265 MATRIX_ROW_START_BYTEPOS (row));
10266
10267 if (w != XWINDOW (selected_window))
10268 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10269 else if (current_buffer == old)
10270 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10271
10272 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10273
10274 /* If we are highlighting the region, then we just changed
10275 the region, so redisplay to show it. */
10276 if (!NILP (Vtransient_mark_mode)
10277 && !NILP (current_buffer->mark_active))
10278 {
10279 clear_glyph_matrix (w->desired_matrix);
10280 if (!try_window (window, startp))
10281 goto need_larger_matrices;
10282 }
10283 }
10284
10285 if (!make_cursor_line_fully_visible (w))
10286 goto need_larger_matrices;
10287 #if GLYPH_DEBUG
10288 debug_method_add (w, "forced window start");
10289 #endif
10290 goto done;
10291 }
10292
10293 /* Handle case where text has not changed, only point, and it has
10294 not moved off the frame. */
10295 if (current_matrix_up_to_date_p
10296 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10297 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10298 {
10299 switch (rc)
10300 {
10301 case CURSOR_MOVEMENT_SUCCESS:
10302 goto done;
10303
10304 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10305 goto need_larger_matrices;
10306
10307 case CURSOR_MOVEMENT_MUST_SCROLL:
10308 goto try_to_scroll;
10309
10310 default:
10311 abort ();
10312 }
10313 }
10314 /* If current starting point was originally the beginning of a line
10315 but no longer is, find a new starting point. */
10316 else if (!NILP (w->start_at_line_beg)
10317 && !(CHARPOS (startp) <= BEGV
10318 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10319 {
10320 #if GLYPH_DEBUG
10321 debug_method_add (w, "recenter 1");
10322 #endif
10323 goto recenter;
10324 }
10325
10326 /* Try scrolling with try_window_id. Value is > 0 if update has
10327 been done, it is -1 if we know that the same window start will
10328 not work. It is 0 if unsuccessful for some other reason. */
10329 else if ((tem = try_window_id (w)) != 0)
10330 {
10331 #if GLYPH_DEBUG
10332 debug_method_add (w, "try_window_id %d", tem);
10333 #endif
10334
10335 if (fonts_changed_p)
10336 goto need_larger_matrices;
10337 if (tem > 0)
10338 goto done;
10339
10340 /* Otherwise try_window_id has returned -1 which means that we
10341 don't want the alternative below this comment to execute. */
10342 }
10343 else if (CHARPOS (startp) >= BEGV
10344 && CHARPOS (startp) <= ZV
10345 && PT >= CHARPOS (startp)
10346 && (CHARPOS (startp) < ZV
10347 /* Avoid starting at end of buffer. */
10348 || CHARPOS (startp) == BEGV
10349 || (XFASTINT (w->last_modified) >= MODIFF
10350 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10351 {
10352 #if GLYPH_DEBUG
10353 debug_method_add (w, "same window start");
10354 #endif
10355
10356 /* Try to redisplay starting at same place as before.
10357 If point has not moved off frame, accept the results. */
10358 if (!current_matrix_up_to_date_p
10359 /* Don't use try_window_reusing_current_matrix in this case
10360 because a window scroll function can have changed the
10361 buffer. */
10362 || !NILP (Vwindow_scroll_functions)
10363 || MINI_WINDOW_P (w)
10364 || !try_window_reusing_current_matrix (w))
10365 {
10366 IF_DEBUG (debug_method_add (w, "1"));
10367 try_window (window, startp);
10368 }
10369
10370 if (fonts_changed_p)
10371 goto need_larger_matrices;
10372
10373 if (w->cursor.vpos >= 0)
10374 {
10375 if (!just_this_one_p
10376 || current_buffer->clip_changed
10377 || BEG_UNCHANGED < CHARPOS (startp))
10378 /* Forget any recorded base line for line number display. */
10379 w->base_line_number = Qnil;
10380
10381 if (!make_cursor_line_fully_visible (w))
10382 goto need_larger_matrices;
10383 goto done;
10384 }
10385 else
10386 clear_glyph_matrix (w->desired_matrix);
10387 }
10388
10389 try_to_scroll:
10390
10391 w->last_modified = make_number (0);
10392 w->last_overlay_modified = make_number (0);
10393
10394 /* Redisplay the mode line. Select the buffer properly for that. */
10395 if (!update_mode_line)
10396 {
10397 update_mode_line = 1;
10398 w->update_mode_line = Qt;
10399 }
10400
10401 /* Try to scroll by specified few lines. */
10402 if ((scroll_conservatively
10403 || scroll_step
10404 || temp_scroll_step
10405 || NUMBERP (current_buffer->scroll_up_aggressively)
10406 || NUMBERP (current_buffer->scroll_down_aggressively))
10407 && !current_buffer->clip_changed
10408 && CHARPOS (startp) >= BEGV
10409 && CHARPOS (startp) <= ZV)
10410 {
10411 /* The function returns -1 if new fonts were loaded, 1 if
10412 successful, 0 if not successful. */
10413 int rc = try_scrolling (window, just_this_one_p,
10414 scroll_conservatively,
10415 scroll_step,
10416 temp_scroll_step);
10417 switch (rc)
10418 {
10419 case SCROLLING_SUCCESS:
10420 goto done;
10421
10422 case SCROLLING_NEED_LARGER_MATRICES:
10423 goto need_larger_matrices;
10424
10425 case SCROLLING_FAILED:
10426 break;
10427
10428 default:
10429 abort ();
10430 }
10431 }
10432
10433 /* Finally, just choose place to start which centers point */
10434
10435 recenter:
10436
10437 #if GLYPH_DEBUG
10438 debug_method_add (w, "recenter");
10439 #endif
10440
10441 /* w->vscroll = 0; */
10442
10443 /* Forget any previously recorded base line for line number display. */
10444 if (!current_matrix_up_to_date_p
10445 || current_buffer->clip_changed)
10446 w->base_line_number = Qnil;
10447
10448 /* Move backward half the height of the window. */
10449 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10450 it.current_y = it.last_visible_y;
10451 move_it_vertically_backward (&it, window_box_height (w) / 2);
10452 xassert (IT_CHARPOS (it) >= BEGV);
10453
10454 /* The function move_it_vertically_backward may move over more
10455 than the specified y-distance. If it->w is small, e.g. a
10456 mini-buffer window, we may end up in front of the window's
10457 display area. Start displaying at the start of the line
10458 containing PT in this case. */
10459 if (it.current_y <= 0)
10460 {
10461 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10462 move_it_vertically (&it, 0);
10463 xassert (IT_CHARPOS (it) <= PT);
10464 it.current_y = 0;
10465 }
10466
10467 it.current_x = it.hpos = 0;
10468
10469 /* Set startp here explicitly in case that helps avoid an infinite loop
10470 in case the window-scroll-functions functions get errors. */
10471 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10472
10473 /* Run scroll hooks. */
10474 startp = run_window_scroll_functions (window, it.current.pos);
10475
10476 /* Redisplay the window. */
10477 if (!current_matrix_up_to_date_p
10478 || windows_or_buffers_changed
10479 /* Don't use try_window_reusing_current_matrix in this case
10480 because it can have changed the buffer. */
10481 || !NILP (Vwindow_scroll_functions)
10482 || !just_this_one_p
10483 || MINI_WINDOW_P (w)
10484 || !try_window_reusing_current_matrix (w))
10485 try_window (window, startp);
10486
10487 /* If new fonts have been loaded (due to fontsets), give up. We
10488 have to start a new redisplay since we need to re-adjust glyph
10489 matrices. */
10490 if (fonts_changed_p)
10491 goto need_larger_matrices;
10492
10493 /* If cursor did not appear assume that the middle of the window is
10494 in the first line of the window. Do it again with the next line.
10495 (Imagine a window of height 100, displaying two lines of height
10496 60. Moving back 50 from it->last_visible_y will end in the first
10497 line.) */
10498 if (w->cursor.vpos < 0)
10499 {
10500 if (!NILP (w->window_end_valid)
10501 && PT >= Z - XFASTINT (w->window_end_pos))
10502 {
10503 clear_glyph_matrix (w->desired_matrix);
10504 move_it_by_lines (&it, 1, 0);
10505 try_window (window, it.current.pos);
10506 }
10507 else if (PT < IT_CHARPOS (it))
10508 {
10509 clear_glyph_matrix (w->desired_matrix);
10510 move_it_by_lines (&it, -1, 0);
10511 try_window (window, it.current.pos);
10512 }
10513 else
10514 {
10515 /* Not much we can do about it. */
10516 }
10517 }
10518
10519 /* Consider the following case: Window starts at BEGV, there is
10520 invisible, intangible text at BEGV, so that display starts at
10521 some point START > BEGV. It can happen that we are called with
10522 PT somewhere between BEGV and START. Try to handle that case. */
10523 if (w->cursor.vpos < 0)
10524 {
10525 struct glyph_row *row = w->current_matrix->rows;
10526 if (row->mode_line_p)
10527 ++row;
10528 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10529 }
10530
10531 if (!make_cursor_line_fully_visible (w))
10532 goto need_larger_matrices;
10533
10534 done:
10535
10536 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10537 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10538 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10539 ? Qt : Qnil);
10540
10541 /* Display the mode line, if we must. */
10542 if ((update_mode_line
10543 /* If window not full width, must redo its mode line
10544 if (a) the window to its side is being redone and
10545 (b) we do a frame-based redisplay. This is a consequence
10546 of how inverted lines are drawn in frame-based redisplay. */
10547 || (!just_this_one_p
10548 && !FRAME_WINDOW_P (f)
10549 && !WINDOW_FULL_WIDTH_P (w))
10550 /* Line number to display. */
10551 || INTEGERP (w->base_line_pos)
10552 /* Column number is displayed and different from the one displayed. */
10553 || (!NILP (w->column_number_displayed)
10554 && XFASTINT (w->column_number_displayed) != current_column ()))
10555 /* This means that the window has a mode line. */
10556 && (WINDOW_WANTS_MODELINE_P (w)
10557 || WINDOW_WANTS_HEADER_LINE_P (w)))
10558 {
10559 display_mode_lines (w);
10560
10561 /* If mode line height has changed, arrange for a thorough
10562 immediate redisplay using the correct mode line height. */
10563 if (WINDOW_WANTS_MODELINE_P (w)
10564 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10565 {
10566 fonts_changed_p = 1;
10567 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10568 = DESIRED_MODE_LINE_HEIGHT (w);
10569 }
10570
10571 /* If top line height has changed, arrange for a thorough
10572 immediate redisplay using the correct mode line height. */
10573 if (WINDOW_WANTS_HEADER_LINE_P (w)
10574 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10575 {
10576 fonts_changed_p = 1;
10577 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10578 = DESIRED_HEADER_LINE_HEIGHT (w);
10579 }
10580
10581 if (fonts_changed_p)
10582 goto need_larger_matrices;
10583 }
10584
10585 if (!line_number_displayed
10586 && !BUFFERP (w->base_line_pos))
10587 {
10588 w->base_line_pos = Qnil;
10589 w->base_line_number = Qnil;
10590 }
10591
10592 finish_menu_bars:
10593
10594 /* When we reach a frame's selected window, redo the frame's menu bar. */
10595 if (update_mode_line
10596 && EQ (FRAME_SELECTED_WINDOW (f), window))
10597 {
10598 int redisplay_menu_p = 0;
10599
10600 if (FRAME_WINDOW_P (f))
10601 {
10602 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10603 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10604 #else
10605 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10606 #endif
10607 }
10608 else
10609 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10610
10611 if (redisplay_menu_p)
10612 display_menu_bar (w);
10613
10614 #ifdef HAVE_WINDOW_SYSTEM
10615 if (WINDOWP (f->tool_bar_window)
10616 && (FRAME_TOOL_BAR_LINES (f) > 0
10617 || auto_resize_tool_bars_p))
10618 redisplay_tool_bar (f);
10619 #endif
10620 }
10621
10622 need_larger_matrices:
10623 ;
10624 finish_scroll_bars:
10625
10626 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10627 {
10628 int start, end, whole;
10629
10630 /* Calculate the start and end positions for the current window.
10631 At some point, it would be nice to choose between scrollbars
10632 which reflect the whole buffer size, with special markers
10633 indicating narrowing, and scrollbars which reflect only the
10634 visible region.
10635
10636 Note that mini-buffers sometimes aren't displaying any text. */
10637 if (!MINI_WINDOW_P (w)
10638 || (w == XWINDOW (minibuf_window)
10639 && NILP (echo_area_buffer[0])))
10640 {
10641 whole = ZV - BEGV;
10642 start = marker_position (w->start) - BEGV;
10643 /* I don't think this is guaranteed to be right. For the
10644 moment, we'll pretend it is. */
10645 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10646
10647 if (end < start)
10648 end = start;
10649 if (whole < (end - start))
10650 whole = end - start;
10651 }
10652 else
10653 start = end = whole = 0;
10654
10655 /* Indicate what this scroll bar ought to be displaying now. */
10656 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10657
10658 /* Note that we actually used the scroll bar attached to this
10659 window, so it shouldn't be deleted at the end of redisplay. */
10660 redeem_scroll_bar_hook (w);
10661 }
10662
10663 /* Restore current_buffer and value of point in it. */
10664 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10665 set_buffer_internal_1 (old);
10666 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10667
10668 unbind_to (count, Qnil);
10669 }
10670
10671
10672 /* Build the complete desired matrix of WINDOW with a window start
10673 buffer position POS. Value is non-zero if successful. It is zero
10674 if fonts were loaded during redisplay which makes re-adjusting
10675 glyph matrices necessary. */
10676
10677 int
10678 try_window (window, pos)
10679 Lisp_Object window;
10680 struct text_pos pos;
10681 {
10682 struct window *w = XWINDOW (window);
10683 struct it it;
10684 struct glyph_row *last_text_row = NULL;
10685
10686 /* Make POS the new window start. */
10687 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10688
10689 /* Mark cursor position as unknown. No overlay arrow seen. */
10690 w->cursor.vpos = -1;
10691 overlay_arrow_seen = 0;
10692
10693 /* Initialize iterator and info to start at POS. */
10694 start_display (&it, w, pos);
10695
10696 /* Display all lines of W. */
10697 while (it.current_y < it.last_visible_y)
10698 {
10699 if (display_line (&it))
10700 last_text_row = it.glyph_row - 1;
10701 if (fonts_changed_p)
10702 return 0;
10703 }
10704
10705 /* If bottom moved off end of frame, change mode line percentage. */
10706 if (XFASTINT (w->window_end_pos) <= 0
10707 && Z != IT_CHARPOS (it))
10708 w->update_mode_line = Qt;
10709
10710 /* Set window_end_pos to the offset of the last character displayed
10711 on the window from the end of current_buffer. Set
10712 window_end_vpos to its row number. */
10713 if (last_text_row)
10714 {
10715 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10716 w->window_end_bytepos
10717 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10718 w->window_end_pos
10719 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10720 w->window_end_vpos
10721 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10722 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10723 ->displays_text_p);
10724 }
10725 else
10726 {
10727 w->window_end_bytepos = 0;
10728 w->window_end_pos = w->window_end_vpos = make_number (0);
10729 }
10730
10731 /* But that is not valid info until redisplay finishes. */
10732 w->window_end_valid = Qnil;
10733 return 1;
10734 }
10735
10736
10737 \f
10738 /************************************************************************
10739 Window redisplay reusing current matrix when buffer has not changed
10740 ************************************************************************/
10741
10742 /* Try redisplay of window W showing an unchanged buffer with a
10743 different window start than the last time it was displayed by
10744 reusing its current matrix. Value is non-zero if successful.
10745 W->start is the new window start. */
10746
10747 static int
10748 try_window_reusing_current_matrix (w)
10749 struct window *w;
10750 {
10751 struct frame *f = XFRAME (w->frame);
10752 struct glyph_row *row, *bottom_row;
10753 struct it it;
10754 struct run run;
10755 struct text_pos start, new_start;
10756 int nrows_scrolled, i;
10757 struct glyph_row *last_text_row;
10758 struct glyph_row *last_reused_text_row;
10759 struct glyph_row *start_row;
10760 int start_vpos, min_y, max_y;
10761
10762 #if GLYPH_DEBUG
10763 if (inhibit_try_window_reusing)
10764 return 0;
10765 #endif
10766
10767 if (/* This function doesn't handle terminal frames. */
10768 !FRAME_WINDOW_P (f)
10769 /* Don't try to reuse the display if windows have been split
10770 or such. */
10771 || windows_or_buffers_changed)
10772 return 0;
10773
10774 /* Can't do this if region may have changed. */
10775 if ((!NILP (Vtransient_mark_mode)
10776 && !NILP (current_buffer->mark_active))
10777 || !NILP (w->region_showing)
10778 || !NILP (Vshow_trailing_whitespace))
10779 return 0;
10780
10781 /* If top-line visibility has changed, give up. */
10782 if (WINDOW_WANTS_HEADER_LINE_P (w)
10783 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10784 return 0;
10785
10786 /* Give up if old or new display is scrolled vertically. We could
10787 make this function handle this, but right now it doesn't. */
10788 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10789 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10790 return 0;
10791
10792 /* The variable new_start now holds the new window start. The old
10793 start `start' can be determined from the current matrix. */
10794 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10795 start = start_row->start.pos;
10796 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10797
10798 /* Clear the desired matrix for the display below. */
10799 clear_glyph_matrix (w->desired_matrix);
10800
10801 if (CHARPOS (new_start) <= CHARPOS (start))
10802 {
10803 int first_row_y;
10804
10805 /* Don't use this method if the display starts with an ellipsis
10806 displayed for invisible text. It's not easy to handle that case
10807 below, and it's certainly not worth the effort since this is
10808 not a frequent case. */
10809 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10810 return 0;
10811
10812 IF_DEBUG (debug_method_add (w, "twu1"));
10813
10814 /* Display up to a row that can be reused. The variable
10815 last_text_row is set to the last row displayed that displays
10816 text. Note that it.vpos == 0 if or if not there is a
10817 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10818 start_display (&it, w, new_start);
10819 first_row_y = it.current_y;
10820 w->cursor.vpos = -1;
10821 last_text_row = last_reused_text_row = NULL;
10822
10823 while (it.current_y < it.last_visible_y
10824 && IT_CHARPOS (it) < CHARPOS (start)
10825 && !fonts_changed_p)
10826 if (display_line (&it))
10827 last_text_row = it.glyph_row - 1;
10828
10829 /* A value of current_y < last_visible_y means that we stopped
10830 at the previous window start, which in turn means that we
10831 have at least one reusable row. */
10832 if (it.current_y < it.last_visible_y)
10833 {
10834 /* IT.vpos always starts from 0; it counts text lines. */
10835 nrows_scrolled = it.vpos;
10836
10837 /* Find PT if not already found in the lines displayed. */
10838 if (w->cursor.vpos < 0)
10839 {
10840 int dy = it.current_y - first_row_y;
10841
10842 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10843 row = row_containing_pos (w, PT, row, NULL, dy);
10844 if (row)
10845 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10846 dy, nrows_scrolled);
10847 else
10848 {
10849 clear_glyph_matrix (w->desired_matrix);
10850 return 0;
10851 }
10852 }
10853
10854 /* Scroll the display. Do it before the current matrix is
10855 changed. The problem here is that update has not yet
10856 run, i.e. part of the current matrix is not up to date.
10857 scroll_run_hook will clear the cursor, and use the
10858 current matrix to get the height of the row the cursor is
10859 in. */
10860 run.current_y = first_row_y;
10861 run.desired_y = it.current_y;
10862 run.height = it.last_visible_y - it.current_y;
10863
10864 if (run.height > 0 && run.current_y != run.desired_y)
10865 {
10866 update_begin (f);
10867 rif->update_window_begin_hook (w);
10868 rif->clear_mouse_face (w);
10869 rif->scroll_run_hook (w, &run);
10870 rif->update_window_end_hook (w, 0, 0);
10871 update_end (f);
10872 }
10873
10874 /* Shift current matrix down by nrows_scrolled lines. */
10875 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10876 rotate_matrix (w->current_matrix,
10877 start_vpos,
10878 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10879 nrows_scrolled);
10880
10881 /* Disable lines that must be updated. */
10882 for (i = 0; i < it.vpos; ++i)
10883 (start_row + i)->enabled_p = 0;
10884
10885 /* Re-compute Y positions. */
10886 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10887 max_y = it.last_visible_y;
10888 for (row = start_row + nrows_scrolled;
10889 row < bottom_row;
10890 ++row)
10891 {
10892 row->y = it.current_y;
10893 row->visible_height = row->height;
10894
10895 if (row->y < min_y)
10896 row->visible_height -= min_y - row->y;
10897 if (row->y + row->height > max_y)
10898 row->visible_height -= row->y + row->height - max_y;
10899
10900 it.current_y += row->height;
10901
10902 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10903 last_reused_text_row = row;
10904 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10905 break;
10906 }
10907
10908 /* Disable lines in the current matrix which are now
10909 below the window. */
10910 for (++row; row < bottom_row; ++row)
10911 row->enabled_p = 0;
10912 }
10913
10914 /* Update window_end_pos etc.; last_reused_text_row is the last
10915 reused row from the current matrix containing text, if any.
10916 The value of last_text_row is the last displayed line
10917 containing text. */
10918 if (last_reused_text_row)
10919 {
10920 w->window_end_bytepos
10921 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10922 w->window_end_pos
10923 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10924 w->window_end_vpos
10925 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10926 w->current_matrix));
10927 }
10928 else if (last_text_row)
10929 {
10930 w->window_end_bytepos
10931 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10932 w->window_end_pos
10933 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10934 w->window_end_vpos
10935 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10936 }
10937 else
10938 {
10939 /* This window must be completely empty. */
10940 w->window_end_bytepos = 0;
10941 w->window_end_pos = w->window_end_vpos = make_number (0);
10942 }
10943 w->window_end_valid = Qnil;
10944
10945 /* Update hint: don't try scrolling again in update_window. */
10946 w->desired_matrix->no_scrolling_p = 1;
10947
10948 #if GLYPH_DEBUG
10949 debug_method_add (w, "try_window_reusing_current_matrix 1");
10950 #endif
10951 return 1;
10952 }
10953 else if (CHARPOS (new_start) > CHARPOS (start))
10954 {
10955 struct glyph_row *pt_row, *row;
10956 struct glyph_row *first_reusable_row;
10957 struct glyph_row *first_row_to_display;
10958 int dy;
10959 int yb = window_text_bottom_y (w);
10960
10961 /* Find the row starting at new_start, if there is one. Don't
10962 reuse a partially visible line at the end. */
10963 first_reusable_row = start_row;
10964 while (first_reusable_row->enabled_p
10965 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10966 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10967 < CHARPOS (new_start)))
10968 ++first_reusable_row;
10969
10970 /* Give up if there is no row to reuse. */
10971 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10972 || !first_reusable_row->enabled_p
10973 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10974 != CHARPOS (new_start)))
10975 return 0;
10976
10977 /* We can reuse fully visible rows beginning with
10978 first_reusable_row to the end of the window. Set
10979 first_row_to_display to the first row that cannot be reused.
10980 Set pt_row to the row containing point, if there is any. */
10981 pt_row = NULL;
10982 for (first_row_to_display = first_reusable_row;
10983 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
10984 ++first_row_to_display)
10985 {
10986 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10987 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10988 pt_row = first_row_to_display;
10989 }
10990
10991 /* Start displaying at the start of first_row_to_display. */
10992 xassert (first_row_to_display->y < yb);
10993 init_to_row_start (&it, w, first_row_to_display);
10994
10995 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10996 - start_vpos);
10997 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10998 - nrows_scrolled);
10999 it.current_y = (first_row_to_display->y - first_reusable_row->y
11000 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
11001
11002 /* Display lines beginning with first_row_to_display in the
11003 desired matrix. Set last_text_row to the last row displayed
11004 that displays text. */
11005 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
11006 if (pt_row == NULL)
11007 w->cursor.vpos = -1;
11008 last_text_row = NULL;
11009 while (it.current_y < it.last_visible_y && !fonts_changed_p)
11010 if (display_line (&it))
11011 last_text_row = it.glyph_row - 1;
11012
11013 /* Give up If point isn't in a row displayed or reused. */
11014 if (w->cursor.vpos < 0)
11015 {
11016 clear_glyph_matrix (w->desired_matrix);
11017 return 0;
11018 }
11019
11020 /* If point is in a reused row, adjust y and vpos of the cursor
11021 position. */
11022 if (pt_row)
11023 {
11024 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
11025 w->current_matrix);
11026 w->cursor.y -= first_reusable_row->y;
11027 }
11028
11029 /* Scroll the display. */
11030 run.current_y = first_reusable_row->y;
11031 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11032 run.height = it.last_visible_y - run.current_y;
11033 dy = run.current_y - run.desired_y;
11034
11035 if (run.height)
11036 {
11037 struct frame *f = XFRAME (WINDOW_FRAME (w));
11038 update_begin (f);
11039 rif->update_window_begin_hook (w);
11040 rif->clear_mouse_face (w);
11041 rif->scroll_run_hook (w, &run);
11042 rif->update_window_end_hook (w, 0, 0);
11043 update_end (f);
11044 }
11045
11046 /* Adjust Y positions of reused rows. */
11047 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11048 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11049 max_y = it.last_visible_y;
11050 for (row = first_reusable_row; row < first_row_to_display; ++row)
11051 {
11052 row->y -= dy;
11053 row->visible_height = row->height;
11054 if (row->y < min_y)
11055 row->visible_height -= min_y - row->y;
11056 if (row->y + row->height > max_y)
11057 row->visible_height -= row->y + row->height - max_y;
11058 }
11059
11060 /* Scroll the current matrix. */
11061 xassert (nrows_scrolled > 0);
11062 rotate_matrix (w->current_matrix,
11063 start_vpos,
11064 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11065 -nrows_scrolled);
11066
11067 /* Disable rows not reused. */
11068 for (row -= nrows_scrolled; row < bottom_row; ++row)
11069 row->enabled_p = 0;
11070
11071 /* Adjust window end. A null value of last_text_row means that
11072 the window end is in reused rows which in turn means that
11073 only its vpos can have changed. */
11074 if (last_text_row)
11075 {
11076 w->window_end_bytepos
11077 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11078 w->window_end_pos
11079 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11080 w->window_end_vpos
11081 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11082 }
11083 else
11084 {
11085 w->window_end_vpos
11086 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
11087 }
11088
11089 w->window_end_valid = Qnil;
11090 w->desired_matrix->no_scrolling_p = 1;
11091
11092 #if GLYPH_DEBUG
11093 debug_method_add (w, "try_window_reusing_current_matrix 2");
11094 #endif
11095 return 1;
11096 }
11097
11098 return 0;
11099 }
11100
11101
11102 \f
11103 /************************************************************************
11104 Window redisplay reusing current matrix when buffer has changed
11105 ************************************************************************/
11106
11107 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11108 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
11109 int *, int *));
11110 static struct glyph_row *
11111 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11112 struct glyph_row *));
11113
11114
11115 /* Return the last row in MATRIX displaying text. If row START is
11116 non-null, start searching with that row. IT gives the dimensions
11117 of the display. Value is null if matrix is empty; otherwise it is
11118 a pointer to the row found. */
11119
11120 static struct glyph_row *
11121 find_last_row_displaying_text (matrix, it, start)
11122 struct glyph_matrix *matrix;
11123 struct it *it;
11124 struct glyph_row *start;
11125 {
11126 struct glyph_row *row, *row_found;
11127
11128 /* Set row_found to the last row in IT->w's current matrix
11129 displaying text. The loop looks funny but think of partially
11130 visible lines. */
11131 row_found = NULL;
11132 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11133 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11134 {
11135 xassert (row->enabled_p);
11136 row_found = row;
11137 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11138 break;
11139 ++row;
11140 }
11141
11142 return row_found;
11143 }
11144
11145
11146 /* Return the last row in the current matrix of W that is not affected
11147 by changes at the start of current_buffer that occurred since W's
11148 current matrix was built. Value is null if no such row exists.
11149
11150 BEG_UNCHANGED us the number of characters unchanged at the start of
11151 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11152 first changed character in current_buffer. Characters at positions <
11153 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11154 when the current matrix was built. */
11155
11156 static struct glyph_row *
11157 find_last_unchanged_at_beg_row (w)
11158 struct window *w;
11159 {
11160 int first_changed_pos = BEG + BEG_UNCHANGED;
11161 struct glyph_row *row;
11162 struct glyph_row *row_found = NULL;
11163 int yb = window_text_bottom_y (w);
11164
11165 /* Find the last row displaying unchanged text. */
11166 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11167 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11168 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11169 {
11170 if (/* If row ends before first_changed_pos, it is unchanged,
11171 except in some case. */
11172 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11173 /* When row ends in ZV and we write at ZV it is not
11174 unchanged. */
11175 && !row->ends_at_zv_p
11176 /* When first_changed_pos is the end of a continued line,
11177 row is not unchanged because it may be no longer
11178 continued. */
11179 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11180 && row->continued_p))
11181 row_found = row;
11182
11183 /* Stop if last visible row. */
11184 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11185 break;
11186
11187 ++row;
11188 }
11189
11190 return row_found;
11191 }
11192
11193
11194 /* Find the first glyph row in the current matrix of W that is not
11195 affected by changes at the end of current_buffer since the
11196 time W's current matrix was built.
11197
11198 Return in *DELTA the number of chars by which buffer positions in
11199 unchanged text at the end of current_buffer must be adjusted.
11200
11201 Return in *DELTA_BYTES the corresponding number of bytes.
11202
11203 Value is null if no such row exists, i.e. all rows are affected by
11204 changes. */
11205
11206 static struct glyph_row *
11207 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11208 struct window *w;
11209 int *delta, *delta_bytes;
11210 {
11211 struct glyph_row *row;
11212 struct glyph_row *row_found = NULL;
11213
11214 *delta = *delta_bytes = 0;
11215
11216 /* Display must not have been paused, otherwise the current matrix
11217 is not up to date. */
11218 if (NILP (w->window_end_valid))
11219 abort ();
11220
11221 /* A value of window_end_pos >= END_UNCHANGED means that the window
11222 end is in the range of changed text. If so, there is no
11223 unchanged row at the end of W's current matrix. */
11224 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11225 return NULL;
11226
11227 /* Set row to the last row in W's current matrix displaying text. */
11228 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11229
11230 /* If matrix is entirely empty, no unchanged row exists. */
11231 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11232 {
11233 /* The value of row is the last glyph row in the matrix having a
11234 meaningful buffer position in it. The end position of row
11235 corresponds to window_end_pos. This allows us to translate
11236 buffer positions in the current matrix to current buffer
11237 positions for characters not in changed text. */
11238 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11239 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11240 int last_unchanged_pos, last_unchanged_pos_old;
11241 struct glyph_row *first_text_row
11242 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11243
11244 *delta = Z - Z_old;
11245 *delta_bytes = Z_BYTE - Z_BYTE_old;
11246
11247 /* Set last_unchanged_pos to the buffer position of the last
11248 character in the buffer that has not been changed. Z is the
11249 index + 1 of the last character in current_buffer, i.e. by
11250 subtracting END_UNCHANGED we get the index of the last
11251 unchanged character, and we have to add BEG to get its buffer
11252 position. */
11253 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11254 last_unchanged_pos_old = last_unchanged_pos - *delta;
11255
11256 /* Search backward from ROW for a row displaying a line that
11257 starts at a minimum position >= last_unchanged_pos_old. */
11258 for (; row > first_text_row; --row)
11259 {
11260 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11261 abort ();
11262
11263 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11264 row_found = row;
11265 }
11266 }
11267
11268 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11269 abort ();
11270
11271 return row_found;
11272 }
11273
11274
11275 /* Make sure that glyph rows in the current matrix of window W
11276 reference the same glyph memory as corresponding rows in the
11277 frame's frame matrix. This function is called after scrolling W's
11278 current matrix on a terminal frame in try_window_id and
11279 try_window_reusing_current_matrix. */
11280
11281 static void
11282 sync_frame_with_window_matrix_rows (w)
11283 struct window *w;
11284 {
11285 struct frame *f = XFRAME (w->frame);
11286 struct glyph_row *window_row, *window_row_end, *frame_row;
11287
11288 /* Preconditions: W must be a leaf window and full-width. Its frame
11289 must have a frame matrix. */
11290 xassert (NILP (w->hchild) && NILP (w->vchild));
11291 xassert (WINDOW_FULL_WIDTH_P (w));
11292 xassert (!FRAME_WINDOW_P (f));
11293
11294 /* If W is a full-width window, glyph pointers in W's current matrix
11295 have, by definition, to be the same as glyph pointers in the
11296 corresponding frame matrix. */
11297 window_row = w->current_matrix->rows;
11298 window_row_end = window_row + w->current_matrix->nrows;
11299 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11300 while (window_row < window_row_end)
11301 {
11302 int area;
11303
11304 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
11305 frame_row->glyphs[area] = window_row->glyphs[area];
11306
11307 /* Disable frame rows whose corresponding window rows have
11308 been disabled in try_window_id. */
11309 if (!window_row->enabled_p)
11310 frame_row->enabled_p = 0;
11311
11312 ++window_row, ++frame_row;
11313 }
11314 }
11315
11316
11317 /* Find the glyph row in window W containing CHARPOS. Consider all
11318 rows between START and END (not inclusive). END null means search
11319 all rows to the end of the display area of W. Value is the row
11320 containing CHARPOS or null. */
11321
11322 struct glyph_row *
11323 row_containing_pos (w, charpos, start, end, dy)
11324 struct window *w;
11325 int charpos;
11326 struct glyph_row *start, *end;
11327 int dy;
11328 {
11329 struct glyph_row *row = start;
11330 int last_y;
11331
11332 /* If we happen to start on a header-line, skip that. */
11333 if (row->mode_line_p)
11334 ++row;
11335
11336 if ((end && row >= end) || !row->enabled_p)
11337 return NULL;
11338
11339 last_y = window_text_bottom_y (w) - dy;
11340
11341 while ((end == NULL || row < end)
11342 && MATRIX_ROW_BOTTOM_Y (row) < last_y
11343 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11344 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11345 /* The end position of a row equals the start
11346 position of the next row. If CHARPOS is there, we
11347 would rather display it in the next line, except
11348 when this line ends in ZV. */
11349 && !row->ends_at_zv_p
11350 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))
11351 ++row;
11352
11353 /* Give up if CHARPOS not found. */
11354 if ((end && row >= end)
11355 || charpos < MATRIX_ROW_START_CHARPOS (row)
11356 || charpos > MATRIX_ROW_END_CHARPOS (row))
11357 row = NULL;
11358
11359 return row;
11360 }
11361
11362
11363 /* Try to redisplay window W by reusing its existing display. W's
11364 current matrix must be up to date when this function is called,
11365 i.e. window_end_valid must not be nil.
11366
11367 Value is
11368
11369 1 if display has been updated
11370 0 if otherwise unsuccessful
11371 -1 if redisplay with same window start is known not to succeed
11372
11373 The following steps are performed:
11374
11375 1. Find the last row in the current matrix of W that is not
11376 affected by changes at the start of current_buffer. If no such row
11377 is found, give up.
11378
11379 2. Find the first row in W's current matrix that is not affected by
11380 changes at the end of current_buffer. Maybe there is no such row.
11381
11382 3. Display lines beginning with the row + 1 found in step 1 to the
11383 row found in step 2 or, if step 2 didn't find a row, to the end of
11384 the window.
11385
11386 4. If cursor is not known to appear on the window, give up.
11387
11388 5. If display stopped at the row found in step 2, scroll the
11389 display and current matrix as needed.
11390
11391 6. Maybe display some lines at the end of W, if we must. This can
11392 happen under various circumstances, like a partially visible line
11393 becoming fully visible, or because newly displayed lines are displayed
11394 in smaller font sizes.
11395
11396 7. Update W's window end information. */
11397
11398 static int
11399 try_window_id (w)
11400 struct window *w;
11401 {
11402 struct frame *f = XFRAME (w->frame);
11403 struct glyph_matrix *current_matrix = w->current_matrix;
11404 struct glyph_matrix *desired_matrix = w->desired_matrix;
11405 struct glyph_row *last_unchanged_at_beg_row;
11406 struct glyph_row *first_unchanged_at_end_row;
11407 struct glyph_row *row;
11408 struct glyph_row *bottom_row;
11409 int bottom_vpos;
11410 struct it it;
11411 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11412 struct text_pos start_pos;
11413 struct run run;
11414 int first_unchanged_at_end_vpos = 0;
11415 struct glyph_row *last_text_row, *last_text_row_at_end;
11416 struct text_pos start;
11417 int first_changed_charpos, last_changed_charpos;
11418
11419 #if GLYPH_DEBUG
11420 if (inhibit_try_window_id)
11421 return 0;
11422 #endif
11423
11424 /* This is handy for debugging. */
11425 #if 0
11426 #define GIVE_UP(X) \
11427 do { \
11428 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11429 return 0; \
11430 } while (0)
11431 #else
11432 #define GIVE_UP(X) return 0
11433 #endif
11434
11435 SET_TEXT_POS_FROM_MARKER (start, w->start);
11436
11437 /* Don't use this for mini-windows because these can show
11438 messages and mini-buffers, and we don't handle that here. */
11439 if (MINI_WINDOW_P (w))
11440 GIVE_UP (1);
11441
11442 /* This flag is used to prevent redisplay optimizations. */
11443 if (windows_or_buffers_changed)
11444 GIVE_UP (2);
11445
11446 /* Verify that narrowing has not changed. This flag is also set to prevent
11447 redisplay optimizations. It would be nice to further
11448 reduce the number of cases where this prevents try_window_id. */
11449 if (current_buffer->clip_changed)
11450 GIVE_UP (3);
11451
11452 /* Window must either use window-based redisplay or be full width. */
11453 if (!FRAME_WINDOW_P (f)
11454 && (!line_ins_del_ok
11455 || !WINDOW_FULL_WIDTH_P (w)))
11456 GIVE_UP (4);
11457
11458 /* Give up if point is not known NOT to appear in W. */
11459 if (PT < CHARPOS (start))
11460 GIVE_UP (5);
11461
11462 /* Another way to prevent redisplay optimizations. */
11463 if (XFASTINT (w->last_modified) == 0)
11464 GIVE_UP (6);
11465
11466 /* Verify that window is not hscrolled. */
11467 if (XFASTINT (w->hscroll) != 0)
11468 GIVE_UP (7);
11469
11470 /* Verify that display wasn't paused. */
11471 if (NILP (w->window_end_valid))
11472 GIVE_UP (8);
11473
11474 /* Can't use this if highlighting a region because a cursor movement
11475 will do more than just set the cursor. */
11476 if (!NILP (Vtransient_mark_mode)
11477 && !NILP (current_buffer->mark_active))
11478 GIVE_UP (9);
11479
11480 /* Likewise if highlighting trailing whitespace. */
11481 if (!NILP (Vshow_trailing_whitespace))
11482 GIVE_UP (11);
11483
11484 /* Likewise if showing a region. */
11485 if (!NILP (w->region_showing))
11486 GIVE_UP (10);
11487
11488 /* Can use this if overlay arrow position and or string have changed. */
11489 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11490 || !EQ (last_arrow_string, Voverlay_arrow_string))
11491 GIVE_UP (12);
11492
11493
11494 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11495 only if buffer has really changed. The reason is that the gap is
11496 initially at Z for freshly visited files. The code below would
11497 set end_unchanged to 0 in that case. */
11498 if (MODIFF > SAVE_MODIFF
11499 /* This seems to happen sometimes after saving a buffer. */
11500 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11501 {
11502 if (GPT - BEG < BEG_UNCHANGED)
11503 BEG_UNCHANGED = GPT - BEG;
11504 if (Z - GPT < END_UNCHANGED)
11505 END_UNCHANGED = Z - GPT;
11506 }
11507
11508 /* The position of the first and last character that has been changed. */
11509 first_changed_charpos = BEG + BEG_UNCHANGED;
11510 last_changed_charpos = Z - END_UNCHANGED;
11511
11512 /* If window starts after a line end, and the last change is in
11513 front of that newline, then changes don't affect the display.
11514 This case happens with stealth-fontification. Note that although
11515 the display is unchanged, glyph positions in the matrix have to
11516 be adjusted, of course. */
11517 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11518 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11519 && ((last_changed_charpos < CHARPOS (start)
11520 && CHARPOS (start) == BEGV)
11521 || (last_changed_charpos < CHARPOS (start) - 1
11522 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11523 {
11524 int Z_old, delta, Z_BYTE_old, delta_bytes;
11525 struct glyph_row *r0;
11526
11527 /* Compute how many chars/bytes have been added to or removed
11528 from the buffer. */
11529 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11530 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11531 delta = Z - Z_old;
11532 delta_bytes = Z_BYTE - Z_BYTE_old;
11533
11534 /* Give up if PT is not in the window. Note that it already has
11535 been checked at the start of try_window_id that PT is not in
11536 front of the window start. */
11537 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11538 GIVE_UP (13);
11539
11540 /* If window start is unchanged, we can reuse the whole matrix
11541 as is, after adjusting glyph positions. No need to compute
11542 the window end again, since its offset from Z hasn't changed. */
11543 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11544 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11545 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11546 {
11547 /* Adjust positions in the glyph matrix. */
11548 if (delta || delta_bytes)
11549 {
11550 struct glyph_row *r1
11551 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11552 increment_matrix_positions (w->current_matrix,
11553 MATRIX_ROW_VPOS (r0, current_matrix),
11554 MATRIX_ROW_VPOS (r1, current_matrix),
11555 delta, delta_bytes);
11556 }
11557
11558 /* Set the cursor. */
11559 row = row_containing_pos (w, PT, r0, NULL, 0);
11560 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11561 return 1;
11562 }
11563 }
11564
11565 /* Handle the case that changes are all below what is displayed in
11566 the window, and that PT is in the window. This shortcut cannot
11567 be taken if ZV is visible in the window, and text has been added
11568 there that is visible in the window. */
11569 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11570 /* ZV is not visible in the window, or there are no
11571 changes at ZV, actually. */
11572 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11573 || first_changed_charpos == last_changed_charpos))
11574 {
11575 struct glyph_row *r0;
11576
11577 /* Give up if PT is not in the window. Note that it already has
11578 been checked at the start of try_window_id that PT is not in
11579 front of the window start. */
11580 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11581 GIVE_UP (14);
11582
11583 /* If window start is unchanged, we can reuse the whole matrix
11584 as is, without changing glyph positions since no text has
11585 been added/removed in front of the window end. */
11586 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11587 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11588 {
11589 /* We have to compute the window end anew since text
11590 can have been added/removed after it. */
11591 w->window_end_pos
11592 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11593 w->window_end_bytepos
11594 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11595
11596 /* Set the cursor. */
11597 row = row_containing_pos (w, PT, r0, NULL, 0);
11598 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11599 return 2;
11600 }
11601 }
11602
11603 /* Give up if window start is in the changed area.
11604
11605 The condition used to read
11606
11607 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11608
11609 but why that was tested escapes me at the moment. */
11610 if (CHARPOS (start) >= first_changed_charpos
11611 && CHARPOS (start) <= last_changed_charpos)
11612 GIVE_UP (15);
11613
11614 /* Check that window start agrees with the start of the first glyph
11615 row in its current matrix. Check this after we know the window
11616 start is not in changed text, otherwise positions would not be
11617 comparable. */
11618 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11619 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11620 GIVE_UP (16);
11621
11622 /* Give up if the window ends in strings. Overlay strings
11623 at the end are difficult to handle, so don't try. */
11624 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11625 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11626 GIVE_UP (20);
11627
11628 /* Compute the position at which we have to start displaying new
11629 lines. Some of the lines at the top of the window might be
11630 reusable because they are not displaying changed text. Find the
11631 last row in W's current matrix not affected by changes at the
11632 start of current_buffer. Value is null if changes start in the
11633 first line of window. */
11634 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11635 if (last_unchanged_at_beg_row)
11636 {
11637 /* Avoid starting to display in the moddle of a character, a TAB
11638 for instance. This is easier than to set up the iterator
11639 exactly, and it's not a frequent case, so the additional
11640 effort wouldn't really pay off. */
11641 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11642 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
11643 && last_unchanged_at_beg_row > w->current_matrix->rows)
11644 --last_unchanged_at_beg_row;
11645
11646 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11647 GIVE_UP (17);
11648
11649 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11650 GIVE_UP (18);
11651 start_pos = it.current.pos;
11652
11653 /* Start displaying new lines in the desired matrix at the same
11654 vpos we would use in the current matrix, i.e. below
11655 last_unchanged_at_beg_row. */
11656 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11657 current_matrix);
11658 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11659 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11660
11661 xassert (it.hpos == 0 && it.current_x == 0);
11662 }
11663 else
11664 {
11665 /* There are no reusable lines at the start of the window.
11666 Start displaying in the first line. */
11667 start_display (&it, w, start);
11668 start_pos = it.current.pos;
11669 }
11670
11671 /* Find the first row that is not affected by changes at the end of
11672 the buffer. Value will be null if there is no unchanged row, in
11673 which case we must redisplay to the end of the window. delta
11674 will be set to the value by which buffer positions beginning with
11675 first_unchanged_at_end_row have to be adjusted due to text
11676 changes. */
11677 first_unchanged_at_end_row
11678 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11679 IF_DEBUG (debug_delta = delta);
11680 IF_DEBUG (debug_delta_bytes = delta_bytes);
11681
11682 /* Set stop_pos to the buffer position up to which we will have to
11683 display new lines. If first_unchanged_at_end_row != NULL, this
11684 is the buffer position of the start of the line displayed in that
11685 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11686 that we don't stop at a buffer position. */
11687 stop_pos = 0;
11688 if (first_unchanged_at_end_row)
11689 {
11690 xassert (last_unchanged_at_beg_row == NULL
11691 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11692
11693 /* If this is a continuation line, move forward to the next one
11694 that isn't. Changes in lines above affect this line.
11695 Caution: this may move first_unchanged_at_end_row to a row
11696 not displaying text. */
11697 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11698 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11699 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11700 < it.last_visible_y))
11701 ++first_unchanged_at_end_row;
11702
11703 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11704 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11705 >= it.last_visible_y))
11706 first_unchanged_at_end_row = NULL;
11707 else
11708 {
11709 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11710 + delta);
11711 first_unchanged_at_end_vpos
11712 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11713 xassert (stop_pos >= Z - END_UNCHANGED);
11714 }
11715 }
11716 else if (last_unchanged_at_beg_row == NULL)
11717 GIVE_UP (19);
11718
11719
11720 #if GLYPH_DEBUG
11721
11722 /* Either there is no unchanged row at the end, or the one we have
11723 now displays text. This is a necessary condition for the window
11724 end pos calculation at the end of this function. */
11725 xassert (first_unchanged_at_end_row == NULL
11726 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11727
11728 debug_last_unchanged_at_beg_vpos
11729 = (last_unchanged_at_beg_row
11730 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11731 : -1);
11732 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11733
11734 #endif /* GLYPH_DEBUG != 0 */
11735
11736
11737 /* Display new lines. Set last_text_row to the last new line
11738 displayed which has text on it, i.e. might end up as being the
11739 line where the window_end_vpos is. */
11740 w->cursor.vpos = -1;
11741 last_text_row = NULL;
11742 overlay_arrow_seen = 0;
11743 while (it.current_y < it.last_visible_y
11744 && !fonts_changed_p
11745 && (first_unchanged_at_end_row == NULL
11746 || IT_CHARPOS (it) < stop_pos))
11747 {
11748 if (display_line (&it))
11749 last_text_row = it.glyph_row - 1;
11750 }
11751
11752 if (fonts_changed_p)
11753 return -1;
11754
11755
11756 /* Compute differences in buffer positions, y-positions etc. for
11757 lines reused at the bottom of the window. Compute what we can
11758 scroll. */
11759 if (first_unchanged_at_end_row
11760 /* No lines reused because we displayed everything up to the
11761 bottom of the window. */
11762 && it.current_y < it.last_visible_y)
11763 {
11764 dvpos = (it.vpos
11765 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11766 current_matrix));
11767 dy = it.current_y - first_unchanged_at_end_row->y;
11768 run.current_y = first_unchanged_at_end_row->y;
11769 run.desired_y = run.current_y + dy;
11770 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11771 }
11772 else
11773 {
11774 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11775 first_unchanged_at_end_row = NULL;
11776 }
11777 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11778
11779
11780 /* Find the cursor if not already found. We have to decide whether
11781 PT will appear on this window (it sometimes doesn't, but this is
11782 not a very frequent case.) This decision has to be made before
11783 the current matrix is altered. A value of cursor.vpos < 0 means
11784 that PT is either in one of the lines beginning at
11785 first_unchanged_at_end_row or below the window. Don't care for
11786 lines that might be displayed later at the window end; as
11787 mentioned, this is not a frequent case. */
11788 if (w->cursor.vpos < 0)
11789 {
11790 /* Cursor in unchanged rows at the top? */
11791 if (PT < CHARPOS (start_pos)
11792 && last_unchanged_at_beg_row)
11793 {
11794 row = row_containing_pos (w, PT,
11795 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11796 last_unchanged_at_beg_row + 1, 0);
11797 if (row)
11798 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11799 }
11800
11801 /* Start from first_unchanged_at_end_row looking for PT. */
11802 else if (first_unchanged_at_end_row)
11803 {
11804 row = row_containing_pos (w, PT - delta,
11805 first_unchanged_at_end_row, NULL, 0);
11806 if (row)
11807 set_cursor_from_row (w, row, w->current_matrix, delta,
11808 delta_bytes, dy, dvpos);
11809 }
11810
11811 /* Give up if cursor was not found. */
11812 if (w->cursor.vpos < 0)
11813 {
11814 clear_glyph_matrix (w->desired_matrix);
11815 return -1;
11816 }
11817 }
11818
11819 /* Don't let the cursor end in the scroll margins. */
11820 {
11821 int this_scroll_margin, cursor_height;
11822
11823 this_scroll_margin = max (0, scroll_margin);
11824 this_scroll_margin = min (this_scroll_margin,
11825 XFASTINT (w->height) / 4);
11826 this_scroll_margin *= CANON_Y_UNIT (it.f);
11827 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11828
11829 if ((w->cursor.y < this_scroll_margin
11830 && CHARPOS (start) > BEGV)
11831 /* Don't take scroll margin into account at the bottom because
11832 old redisplay didn't do it either. */
11833 || w->cursor.y + cursor_height > it.last_visible_y)
11834 {
11835 w->cursor.vpos = -1;
11836 clear_glyph_matrix (w->desired_matrix);
11837 return -1;
11838 }
11839 }
11840
11841 /* Scroll the display. Do it before changing the current matrix so
11842 that xterm.c doesn't get confused about where the cursor glyph is
11843 found. */
11844 if (dy && run.height)
11845 {
11846 update_begin (f);
11847
11848 if (FRAME_WINDOW_P (f))
11849 {
11850 rif->update_window_begin_hook (w);
11851 rif->clear_mouse_face (w);
11852 rif->scroll_run_hook (w, &run);
11853 rif->update_window_end_hook (w, 0, 0);
11854 }
11855 else
11856 {
11857 /* Terminal frame. In this case, dvpos gives the number of
11858 lines to scroll by; dvpos < 0 means scroll up. */
11859 int first_unchanged_at_end_vpos
11860 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11861 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11862 int end = (XFASTINT (w->top)
11863 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11864 + window_internal_height (w));
11865
11866 /* Perform the operation on the screen. */
11867 if (dvpos > 0)
11868 {
11869 /* Scroll last_unchanged_at_beg_row to the end of the
11870 window down dvpos lines. */
11871 set_terminal_window (end);
11872
11873 /* On dumb terminals delete dvpos lines at the end
11874 before inserting dvpos empty lines. */
11875 if (!scroll_region_ok)
11876 ins_del_lines (end - dvpos, -dvpos);
11877
11878 /* Insert dvpos empty lines in front of
11879 last_unchanged_at_beg_row. */
11880 ins_del_lines (from, dvpos);
11881 }
11882 else if (dvpos < 0)
11883 {
11884 /* Scroll up last_unchanged_at_beg_vpos to the end of
11885 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11886 set_terminal_window (end);
11887
11888 /* Delete dvpos lines in front of
11889 last_unchanged_at_beg_vpos. ins_del_lines will set
11890 the cursor to the given vpos and emit |dvpos| delete
11891 line sequences. */
11892 ins_del_lines (from + dvpos, dvpos);
11893
11894 /* On a dumb terminal insert dvpos empty lines at the
11895 end. */
11896 if (!scroll_region_ok)
11897 ins_del_lines (end + dvpos, -dvpos);
11898 }
11899
11900 set_terminal_window (0);
11901 }
11902
11903 update_end (f);
11904 }
11905
11906 /* Shift reused rows of the current matrix to the right position.
11907 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11908 text. */
11909 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11910 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11911 if (dvpos < 0)
11912 {
11913 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11914 bottom_vpos, dvpos);
11915 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11916 bottom_vpos, 0);
11917 }
11918 else if (dvpos > 0)
11919 {
11920 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11921 bottom_vpos, dvpos);
11922 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11923 first_unchanged_at_end_vpos + dvpos, 0);
11924 }
11925
11926 /* For frame-based redisplay, make sure that current frame and window
11927 matrix are in sync with respect to glyph memory. */
11928 if (!FRAME_WINDOW_P (f))
11929 sync_frame_with_window_matrix_rows (w);
11930
11931 /* Adjust buffer positions in reused rows. */
11932 if (delta)
11933 increment_matrix_positions (current_matrix,
11934 first_unchanged_at_end_vpos + dvpos,
11935 bottom_vpos, delta, delta_bytes);
11936
11937 /* Adjust Y positions. */
11938 if (dy)
11939 shift_glyph_matrix (w, current_matrix,
11940 first_unchanged_at_end_vpos + dvpos,
11941 bottom_vpos, dy);
11942
11943 if (first_unchanged_at_end_row)
11944 first_unchanged_at_end_row += dvpos;
11945
11946 /* If scrolling up, there may be some lines to display at the end of
11947 the window. */
11948 last_text_row_at_end = NULL;
11949 if (dy < 0)
11950 {
11951 /* Scrolling up can leave for example a partially visible line
11952 at the end of the window to be redisplayed. */
11953 /* Set last_row to the glyph row in the current matrix where the
11954 window end line is found. It has been moved up or down in
11955 the matrix by dvpos. */
11956 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11957 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11958
11959 /* If last_row is the window end line, it should display text. */
11960 xassert (last_row->displays_text_p);
11961
11962 /* If window end line was partially visible before, begin
11963 displaying at that line. Otherwise begin displaying with the
11964 line following it. */
11965 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11966 {
11967 init_to_row_start (&it, w, last_row);
11968 it.vpos = last_vpos;
11969 it.current_y = last_row->y;
11970 }
11971 else
11972 {
11973 init_to_row_end (&it, w, last_row);
11974 it.vpos = 1 + last_vpos;
11975 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11976 ++last_row;
11977 }
11978
11979 /* We may start in a continuation line. If so, we have to
11980 get the right continuation_lines_width and current_x. */
11981 it.continuation_lines_width = last_row->continuation_lines_width;
11982 it.hpos = it.current_x = 0;
11983
11984 /* Display the rest of the lines at the window end. */
11985 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11986 while (it.current_y < it.last_visible_y
11987 && !fonts_changed_p)
11988 {
11989 /* Is it always sure that the display agrees with lines in
11990 the current matrix? I don't think so, so we mark rows
11991 displayed invalid in the current matrix by setting their
11992 enabled_p flag to zero. */
11993 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11994 if (display_line (&it))
11995 last_text_row_at_end = it.glyph_row - 1;
11996 }
11997 }
11998
11999 /* Update window_end_pos and window_end_vpos. */
12000 if (first_unchanged_at_end_row
12001 && first_unchanged_at_end_row->y < it.last_visible_y
12002 && !last_text_row_at_end)
12003 {
12004 /* Window end line if one of the preserved rows from the current
12005 matrix. Set row to the last row displaying text in current
12006 matrix starting at first_unchanged_at_end_row, after
12007 scrolling. */
12008 xassert (first_unchanged_at_end_row->displays_text_p);
12009 row = find_last_row_displaying_text (w->current_matrix, &it,
12010 first_unchanged_at_end_row);
12011 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
12012
12013 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12014 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12015 w->window_end_vpos
12016 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
12017 xassert (w->window_end_bytepos >= 0);
12018 IF_DEBUG (debug_method_add (w, "A"));
12019 }
12020 else if (last_text_row_at_end)
12021 {
12022 w->window_end_pos
12023 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
12024 w->window_end_bytepos
12025 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
12026 w->window_end_vpos
12027 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
12028 xassert (w->window_end_bytepos >= 0);
12029 IF_DEBUG (debug_method_add (w, "B"));
12030 }
12031 else if (last_text_row)
12032 {
12033 /* We have displayed either to the end of the window or at the
12034 end of the window, i.e. the last row with text is to be found
12035 in the desired matrix. */
12036 w->window_end_pos
12037 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12038 w->window_end_bytepos
12039 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12040 w->window_end_vpos
12041 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
12042 xassert (w->window_end_bytepos >= 0);
12043 }
12044 else if (first_unchanged_at_end_row == NULL
12045 && last_text_row == NULL
12046 && last_text_row_at_end == NULL)
12047 {
12048 /* Displayed to end of window, but no line containing text was
12049 displayed. Lines were deleted at the end of the window. */
12050 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12051 int vpos = XFASTINT (w->window_end_vpos);
12052 struct glyph_row *current_row = current_matrix->rows + vpos;
12053 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12054
12055 for (row = NULL;
12056 row == NULL && vpos >= first_vpos;
12057 --vpos, --current_row, --desired_row)
12058 {
12059 if (desired_row->enabled_p)
12060 {
12061 if (desired_row->displays_text_p)
12062 row = desired_row;
12063 }
12064 else if (current_row->displays_text_p)
12065 row = current_row;
12066 }
12067
12068 xassert (row != NULL);
12069 w->window_end_vpos = make_number (vpos + 1);
12070 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12071 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12072 xassert (w->window_end_bytepos >= 0);
12073 IF_DEBUG (debug_method_add (w, "C"));
12074 }
12075 else
12076 abort ();
12077
12078 #if 0 /* This leads to problems, for instance when the cursor is
12079 at ZV, and the cursor line displays no text. */
12080 /* Disable rows below what's displayed in the window. This makes
12081 debugging easier. */
12082 enable_glyph_matrix_rows (current_matrix,
12083 XFASTINT (w->window_end_vpos) + 1,
12084 bottom_vpos, 0);
12085 #endif
12086
12087 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12088 debug_end_vpos = XFASTINT (w->window_end_vpos));
12089
12090 /* Record that display has not been completed. */
12091 w->window_end_valid = Qnil;
12092 w->desired_matrix->no_scrolling_p = 1;
12093 return 3;
12094
12095 #undef GIVE_UP
12096 }
12097
12098
12099 \f
12100 /***********************************************************************
12101 More debugging support
12102 ***********************************************************************/
12103
12104 #if GLYPH_DEBUG
12105
12106 void dump_glyph_row P_ ((struct glyph_row *, int, int));
12107 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
12108 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
12109
12110
12111 /* Dump the contents of glyph matrix MATRIX on stderr.
12112
12113 GLYPHS 0 means don't show glyph contents.
12114 GLYPHS 1 means show glyphs in short form
12115 GLYPHS > 1 means show glyphs in long form. */
12116
12117 void
12118 dump_glyph_matrix (matrix, glyphs)
12119 struct glyph_matrix *matrix;
12120 int glyphs;
12121 {
12122 int i;
12123 for (i = 0; i < matrix->nrows; ++i)
12124 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
12125 }
12126
12127
12128 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12129 the glyph row and area where the glyph comes from. */
12130
12131 void
12132 dump_glyph (row, glyph, area)
12133 struct glyph_row *row;
12134 struct glyph *glyph;
12135 int area;
12136 {
12137 if (glyph->type == CHAR_GLYPH)
12138 {
12139 fprintf (stderr,
12140 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12141 glyph - row->glyphs[TEXT_AREA],
12142 'C',
12143 glyph->charpos,
12144 (BUFFERP (glyph->object)
12145 ? 'B'
12146 : (STRINGP (glyph->object)
12147 ? 'S'
12148 : '-')),
12149 glyph->pixel_width,
12150 glyph->u.ch,
12151 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12152 ? glyph->u.ch
12153 : '.'),
12154 glyph->face_id,
12155 glyph->left_box_line_p,
12156 glyph->right_box_line_p);
12157 }
12158 else if (glyph->type == STRETCH_GLYPH)
12159 {
12160 fprintf (stderr,
12161 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12162 glyph - row->glyphs[TEXT_AREA],
12163 'S',
12164 glyph->charpos,
12165 (BUFFERP (glyph->object)
12166 ? 'B'
12167 : (STRINGP (glyph->object)
12168 ? 'S'
12169 : '-')),
12170 glyph->pixel_width,
12171 0,
12172 '.',
12173 glyph->face_id,
12174 glyph->left_box_line_p,
12175 glyph->right_box_line_p);
12176 }
12177 else if (glyph->type == IMAGE_GLYPH)
12178 {
12179 fprintf (stderr,
12180 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12181 glyph - row->glyphs[TEXT_AREA],
12182 'I',
12183 glyph->charpos,
12184 (BUFFERP (glyph->object)
12185 ? 'B'
12186 : (STRINGP (glyph->object)
12187 ? 'S'
12188 : '-')),
12189 glyph->pixel_width,
12190 glyph->u.img_id,
12191 '.',
12192 glyph->face_id,
12193 glyph->left_box_line_p,
12194 glyph->right_box_line_p);
12195 }
12196 }
12197
12198
12199 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12200 GLYPHS 0 means don't show glyph contents.
12201 GLYPHS 1 means show glyphs in short form
12202 GLYPHS > 1 means show glyphs in long form. */
12203
12204 void
12205 dump_glyph_row (row, vpos, glyphs)
12206 struct glyph_row *row;
12207 int vpos, glyphs;
12208 {
12209 if (glyphs != 1)
12210 {
12211 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12212 fprintf (stderr, "=======================================================================\n");
12213
12214 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
12215 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12216 vpos,
12217 MATRIX_ROW_START_CHARPOS (row),
12218 MATRIX_ROW_END_CHARPOS (row),
12219 row->used[TEXT_AREA],
12220 row->contains_overlapping_glyphs_p,
12221 row->enabled_p,
12222 row->truncated_on_left_p,
12223 row->truncated_on_right_p,
12224 row->overlay_arrow_p,
12225 row->continued_p,
12226 MATRIX_ROW_CONTINUATION_LINE_P (row),
12227 row->displays_text_p,
12228 row->ends_at_zv_p,
12229 row->fill_line_p,
12230 row->ends_in_middle_of_char_p,
12231 row->starts_in_middle_of_char_p,
12232 row->mouse_face_p,
12233 row->x,
12234 row->y,
12235 row->pixel_width,
12236 row->height,
12237 row->visible_height,
12238 row->ascent,
12239 row->phys_ascent);
12240 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12241 row->end.overlay_string_index,
12242 row->continuation_lines_width);
12243 fprintf (stderr, "%9d %5d\n",
12244 CHARPOS (row->start.string_pos),
12245 CHARPOS (row->end.string_pos));
12246 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12247 row->end.dpvec_index);
12248 }
12249
12250 if (glyphs > 1)
12251 {
12252 int area;
12253
12254 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12255 {
12256 struct glyph *glyph = row->glyphs[area];
12257 struct glyph *glyph_end = glyph + row->used[area];
12258
12259 /* Glyph for a line end in text. */
12260 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12261 ++glyph_end;
12262
12263 if (glyph < glyph_end)
12264 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12265
12266 for (; glyph < glyph_end; ++glyph)
12267 dump_glyph (row, glyph, area);
12268 }
12269 }
12270 else if (glyphs == 1)
12271 {
12272 int area;
12273
12274 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12275 {
12276 char *s = (char *) alloca (row->used[area] + 1);
12277 int i;
12278
12279 for (i = 0; i < row->used[area]; ++i)
12280 {
12281 struct glyph *glyph = row->glyphs[area] + i;
12282 if (glyph->type == CHAR_GLYPH
12283 && glyph->u.ch < 0x80
12284 && glyph->u.ch >= ' ')
12285 s[i] = glyph->u.ch;
12286 else
12287 s[i] = '.';
12288 }
12289
12290 s[i] = '\0';
12291 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12292 }
12293 }
12294 }
12295
12296
12297 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12298 Sdump_glyph_matrix, 0, 1, "p",
12299 doc: /* Dump the current matrix of the selected window to stderr.
12300 Shows contents of glyph row structures. With non-nil
12301 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12302 glyphs in short form, otherwise show glyphs in long form. */)
12303 (glyphs)
12304 Lisp_Object glyphs;
12305 {
12306 struct window *w = XWINDOW (selected_window);
12307 struct buffer *buffer = XBUFFER (w->buffer);
12308
12309 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12310 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12311 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12312 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12313 fprintf (stderr, "=============================================\n");
12314 dump_glyph_matrix (w->current_matrix,
12315 NILP (glyphs) ? 0 : XINT (glyphs));
12316 return Qnil;
12317 }
12318
12319
12320 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12321 doc: /* Dump glyph row ROW to stderr.
12322 GLYPH 0 means don't dump glyphs.
12323 GLYPH 1 means dump glyphs in short form.
12324 GLYPH > 1 or omitted means dump glyphs in long form. */)
12325 (row, glyphs)
12326 Lisp_Object row, glyphs;
12327 {
12328 struct glyph_matrix *matrix;
12329 int vpos;
12330
12331 CHECK_NUMBER (row);
12332 matrix = XWINDOW (selected_window)->current_matrix;
12333 vpos = XINT (row);
12334 if (vpos >= 0 && vpos < matrix->nrows)
12335 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12336 vpos,
12337 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12338 return Qnil;
12339 }
12340
12341
12342 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12343 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12344 GLYPH 0 means don't dump glyphs.
12345 GLYPH 1 means dump glyphs in short form.
12346 GLYPH > 1 or omitted means dump glyphs in long form. */)
12347 (row, glyphs)
12348 Lisp_Object row, glyphs;
12349 {
12350 struct frame *sf = SELECTED_FRAME ();
12351 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12352 int vpos;
12353
12354 CHECK_NUMBER (row);
12355 vpos = XINT (row);
12356 if (vpos >= 0 && vpos < m->nrows)
12357 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12358 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12359 return Qnil;
12360 }
12361
12362
12363 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12364 doc: /* Toggle tracing of redisplay.
12365 With ARG, turn tracing on if and only if ARG is positive. */)
12366 (arg)
12367 Lisp_Object arg;
12368 {
12369 if (NILP (arg))
12370 trace_redisplay_p = !trace_redisplay_p;
12371 else
12372 {
12373 arg = Fprefix_numeric_value (arg);
12374 trace_redisplay_p = XINT (arg) > 0;
12375 }
12376
12377 return Qnil;
12378 }
12379
12380
12381 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12382 doc: /* Like `format', but print result to stderr. */)
12383 (nargs, args)
12384 int nargs;
12385 Lisp_Object *args;
12386 {
12387 Lisp_Object s = Fformat (nargs, args);
12388 fprintf (stderr, "%s", SDATA (s));
12389 return Qnil;
12390 }
12391
12392 #endif /* GLYPH_DEBUG */
12393
12394
12395 \f
12396 /***********************************************************************
12397 Building Desired Matrix Rows
12398 ***********************************************************************/
12399
12400 /* Return a temporary glyph row holding the glyphs of an overlay
12401 arrow. Only used for non-window-redisplay windows. */
12402
12403 static struct glyph_row *
12404 get_overlay_arrow_glyph_row (w)
12405 struct window *w;
12406 {
12407 struct frame *f = XFRAME (WINDOW_FRAME (w));
12408 struct buffer *buffer = XBUFFER (w->buffer);
12409 struct buffer *old = current_buffer;
12410 unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
12411 int arrow_len = SCHARS (Voverlay_arrow_string);
12412 unsigned char *arrow_end = arrow_string + arrow_len;
12413 unsigned char *p;
12414 struct it it;
12415 int multibyte_p;
12416 int n_glyphs_before;
12417
12418 set_buffer_temp (buffer);
12419 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12420 it.glyph_row->used[TEXT_AREA] = 0;
12421 SET_TEXT_POS (it.position, 0, 0);
12422
12423 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12424 p = arrow_string;
12425 while (p < arrow_end)
12426 {
12427 Lisp_Object face, ilisp;
12428
12429 /* Get the next character. */
12430 if (multibyte_p)
12431 it.c = string_char_and_length (p, arrow_len, &it.len);
12432 else
12433 it.c = *p, it.len = 1;
12434 p += it.len;
12435
12436 /* Get its face. */
12437 ilisp = make_number (p - arrow_string);
12438 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12439 it.face_id = compute_char_face (f, it.c, face);
12440
12441 /* Compute its width, get its glyphs. */
12442 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12443 SET_TEXT_POS (it.position, -1, -1);
12444 PRODUCE_GLYPHS (&it);
12445
12446 /* If this character doesn't fit any more in the line, we have
12447 to remove some glyphs. */
12448 if (it.current_x > it.last_visible_x)
12449 {
12450 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12451 break;
12452 }
12453 }
12454
12455 set_buffer_temp (old);
12456 return it.glyph_row;
12457 }
12458
12459
12460 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12461 glyphs are only inserted for terminal frames since we can't really
12462 win with truncation glyphs when partially visible glyphs are
12463 involved. Which glyphs to insert is determined by
12464 produce_special_glyphs. */
12465
12466 static void
12467 insert_left_trunc_glyphs (it)
12468 struct it *it;
12469 {
12470 struct it truncate_it;
12471 struct glyph *from, *end, *to, *toend;
12472
12473 xassert (!FRAME_WINDOW_P (it->f));
12474
12475 /* Get the truncation glyphs. */
12476 truncate_it = *it;
12477 truncate_it.current_x = 0;
12478 truncate_it.face_id = DEFAULT_FACE_ID;
12479 truncate_it.glyph_row = &scratch_glyph_row;
12480 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12481 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12482 truncate_it.object = make_number (0);
12483 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12484
12485 /* Overwrite glyphs from IT with truncation glyphs. */
12486 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12487 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12488 to = it->glyph_row->glyphs[TEXT_AREA];
12489 toend = to + it->glyph_row->used[TEXT_AREA];
12490
12491 while (from < end)
12492 *to++ = *from++;
12493
12494 /* There may be padding glyphs left over. Overwrite them too. */
12495 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12496 {
12497 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12498 while (from < end)
12499 *to++ = *from++;
12500 }
12501
12502 if (to > toend)
12503 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12504 }
12505
12506
12507 /* Compute the pixel height and width of IT->glyph_row.
12508
12509 Most of the time, ascent and height of a display line will be equal
12510 to the max_ascent and max_height values of the display iterator
12511 structure. This is not the case if
12512
12513 1. We hit ZV without displaying anything. In this case, max_ascent
12514 and max_height will be zero.
12515
12516 2. We have some glyphs that don't contribute to the line height.
12517 (The glyph row flag contributes_to_line_height_p is for future
12518 pixmap extensions).
12519
12520 The first case is easily covered by using default values because in
12521 these cases, the line height does not really matter, except that it
12522 must not be zero. */
12523
12524 static void
12525 compute_line_metrics (it)
12526 struct it *it;
12527 {
12528 struct glyph_row *row = it->glyph_row;
12529 int area, i;
12530
12531 if (FRAME_WINDOW_P (it->f))
12532 {
12533 int i, min_y, max_y;
12534
12535 /* The line may consist of one space only, that was added to
12536 place the cursor on it. If so, the row's height hasn't been
12537 computed yet. */
12538 if (row->height == 0)
12539 {
12540 if (it->max_ascent + it->max_descent == 0)
12541 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12542 row->ascent = it->max_ascent;
12543 row->height = it->max_ascent + it->max_descent;
12544 row->phys_ascent = it->max_phys_ascent;
12545 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12546 }
12547
12548 /* Compute the width of this line. */
12549 row->pixel_width = row->x;
12550 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12551 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12552
12553 xassert (row->pixel_width >= 0);
12554 xassert (row->ascent >= 0 && row->height > 0);
12555
12556 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12557 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12558
12559 /* If first line's physical ascent is larger than its logical
12560 ascent, use the physical ascent, and make the row taller.
12561 This makes accented characters fully visible. */
12562 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12563 && row->phys_ascent > row->ascent)
12564 {
12565 row->height += row->phys_ascent - row->ascent;
12566 row->ascent = row->phys_ascent;
12567 }
12568
12569 /* Compute how much of the line is visible. */
12570 row->visible_height = row->height;
12571
12572 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12573 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12574
12575 if (row->y < min_y)
12576 row->visible_height -= min_y - row->y;
12577 if (row->y + row->height > max_y)
12578 row->visible_height -= row->y + row->height - max_y;
12579 }
12580 else
12581 {
12582 row->pixel_width = row->used[TEXT_AREA];
12583 if (row->continued_p)
12584 row->pixel_width -= it->continuation_pixel_width;
12585 else if (row->truncated_on_right_p)
12586 row->pixel_width -= it->truncation_pixel_width;
12587 row->ascent = row->phys_ascent = 0;
12588 row->height = row->phys_height = row->visible_height = 1;
12589 }
12590
12591 /* Compute a hash code for this row. */
12592 row->hash = 0;
12593 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12594 for (i = 0; i < row->used[area]; ++i)
12595 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12596 + row->glyphs[area][i].u.val
12597 + row->glyphs[area][i].face_id
12598 + row->glyphs[area][i].padding_p
12599 + (row->glyphs[area][i].type << 2));
12600
12601 it->max_ascent = it->max_descent = 0;
12602 it->max_phys_ascent = it->max_phys_descent = 0;
12603 }
12604
12605
12606 /* Append one space to the glyph row of iterator IT if doing a
12607 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12608 space have the default face, otherwise let it have the same face as
12609 IT->face_id. Value is non-zero if a space was added.
12610
12611 This function is called to make sure that there is always one glyph
12612 at the end of a glyph row that the cursor can be set on under
12613 window-systems. (If there weren't such a glyph we would not know
12614 how wide and tall a box cursor should be displayed).
12615
12616 At the same time this space let's a nicely handle clearing to the
12617 end of the line if the row ends in italic text. */
12618
12619 static int
12620 append_space (it, default_face_p)
12621 struct it *it;
12622 int default_face_p;
12623 {
12624 if (FRAME_WINDOW_P (it->f))
12625 {
12626 int n = it->glyph_row->used[TEXT_AREA];
12627
12628 if (it->glyph_row->glyphs[TEXT_AREA] + n
12629 < it->glyph_row->glyphs[1 + TEXT_AREA])
12630 {
12631 /* Save some values that must not be changed.
12632 Must save IT->c and IT->len because otherwise
12633 ITERATOR_AT_END_P wouldn't work anymore after
12634 append_space has been called. */
12635 enum display_element_type saved_what = it->what;
12636 int saved_c = it->c, saved_len = it->len;
12637 int saved_x = it->current_x;
12638 int saved_face_id = it->face_id;
12639 struct text_pos saved_pos;
12640 Lisp_Object saved_object;
12641 struct face *face;
12642
12643 saved_object = it->object;
12644 saved_pos = it->position;
12645
12646 it->what = IT_CHARACTER;
12647 bzero (&it->position, sizeof it->position);
12648 it->object = make_number (0);
12649 it->c = ' ';
12650 it->len = 1;
12651
12652 if (default_face_p)
12653 it->face_id = DEFAULT_FACE_ID;
12654 else if (it->face_before_selective_p)
12655 it->face_id = it->saved_face_id;
12656 face = FACE_FROM_ID (it->f, it->face_id);
12657 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12658
12659 PRODUCE_GLYPHS (it);
12660
12661 it->current_x = saved_x;
12662 it->object = saved_object;
12663 it->position = saved_pos;
12664 it->what = saved_what;
12665 it->face_id = saved_face_id;
12666 it->len = saved_len;
12667 it->c = saved_c;
12668 return 1;
12669 }
12670 }
12671
12672 return 0;
12673 }
12674
12675
12676 /* Extend the face of the last glyph in the text area of IT->glyph_row
12677 to the end of the display line. Called from display_line.
12678 If the glyph row is empty, add a space glyph to it so that we
12679 know the face to draw. Set the glyph row flag fill_line_p. */
12680
12681 static void
12682 extend_face_to_end_of_line (it)
12683 struct it *it;
12684 {
12685 struct face *face;
12686 struct frame *f = it->f;
12687
12688 /* If line is already filled, do nothing. */
12689 if (it->current_x >= it->last_visible_x)
12690 return;
12691
12692 /* Face extension extends the background and box of IT->face_id
12693 to the end of the line. If the background equals the background
12694 of the frame, we don't have to do anything. */
12695 if (it->face_before_selective_p)
12696 face = FACE_FROM_ID (it->f, it->saved_face_id);
12697 else
12698 face = FACE_FROM_ID (f, it->face_id);
12699
12700 if (FRAME_WINDOW_P (f)
12701 && face->box == FACE_NO_BOX
12702 && face->background == FRAME_BACKGROUND_PIXEL (f)
12703 && !face->stipple)
12704 return;
12705
12706 /* Set the glyph row flag indicating that the face of the last glyph
12707 in the text area has to be drawn to the end of the text area. */
12708 it->glyph_row->fill_line_p = 1;
12709
12710 /* If current character of IT is not ASCII, make sure we have the
12711 ASCII face. This will be automatically undone the next time
12712 get_next_display_element returns a multibyte character. Note
12713 that the character will always be single byte in unibyte text. */
12714 if (!SINGLE_BYTE_CHAR_P (it->c))
12715 {
12716 it->face_id = FACE_FOR_CHAR (f, face, 0);
12717 }
12718
12719 if (FRAME_WINDOW_P (f))
12720 {
12721 /* If the row is empty, add a space with the current face of IT,
12722 so that we know which face to draw. */
12723 if (it->glyph_row->used[TEXT_AREA] == 0)
12724 {
12725 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12726 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12727 it->glyph_row->used[TEXT_AREA] = 1;
12728 }
12729 }
12730 else
12731 {
12732 /* Save some values that must not be changed. */
12733 int saved_x = it->current_x;
12734 struct text_pos saved_pos;
12735 Lisp_Object saved_object;
12736 enum display_element_type saved_what = it->what;
12737 int saved_face_id = it->face_id;
12738
12739 saved_object = it->object;
12740 saved_pos = it->position;
12741
12742 it->what = IT_CHARACTER;
12743 bzero (&it->position, sizeof it->position);
12744 it->object = make_number (0);
12745 it->c = ' ';
12746 it->len = 1;
12747 it->face_id = face->id;
12748
12749 PRODUCE_GLYPHS (it);
12750
12751 while (it->current_x <= it->last_visible_x)
12752 PRODUCE_GLYPHS (it);
12753
12754 /* Don't count these blanks really. It would let us insert a left
12755 truncation glyph below and make us set the cursor on them, maybe. */
12756 it->current_x = saved_x;
12757 it->object = saved_object;
12758 it->position = saved_pos;
12759 it->what = saved_what;
12760 it->face_id = saved_face_id;
12761 }
12762 }
12763
12764
12765 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12766 trailing whitespace. */
12767
12768 static int
12769 trailing_whitespace_p (charpos)
12770 int charpos;
12771 {
12772 int bytepos = CHAR_TO_BYTE (charpos);
12773 int c = 0;
12774
12775 while (bytepos < ZV_BYTE
12776 && (c = FETCH_CHAR (bytepos),
12777 c == ' ' || c == '\t'))
12778 ++bytepos;
12779
12780 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12781 {
12782 if (bytepos != PT_BYTE)
12783 return 1;
12784 }
12785 return 0;
12786 }
12787
12788
12789 /* Highlight trailing whitespace, if any, in ROW. */
12790
12791 void
12792 highlight_trailing_whitespace (f, row)
12793 struct frame *f;
12794 struct glyph_row *row;
12795 {
12796 int used = row->used[TEXT_AREA];
12797
12798 if (used)
12799 {
12800 struct glyph *start = row->glyphs[TEXT_AREA];
12801 struct glyph *glyph = start + used - 1;
12802
12803 /* Skip over glyphs inserted to display the cursor at the
12804 end of a line, for extending the face of the last glyph
12805 to the end of the line on terminals, and for truncation
12806 and continuation glyphs. */
12807 while (glyph >= start
12808 && glyph->type == CHAR_GLYPH
12809 && INTEGERP (glyph->object))
12810 --glyph;
12811
12812 /* If last glyph is a space or stretch, and it's trailing
12813 whitespace, set the face of all trailing whitespace glyphs in
12814 IT->glyph_row to `trailing-whitespace'. */
12815 if (glyph >= start
12816 && BUFFERP (glyph->object)
12817 && (glyph->type == STRETCH_GLYPH
12818 || (glyph->type == CHAR_GLYPH
12819 && glyph->u.ch == ' '))
12820 && trailing_whitespace_p (glyph->charpos))
12821 {
12822 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12823
12824 while (glyph >= start
12825 && BUFFERP (glyph->object)
12826 && (glyph->type == STRETCH_GLYPH
12827 || (glyph->type == CHAR_GLYPH
12828 && glyph->u.ch == ' ')))
12829 (glyph--)->face_id = face_id;
12830 }
12831 }
12832 }
12833
12834
12835 /* Value is non-zero if glyph row ROW in window W should be
12836 used to hold the cursor. */
12837
12838 static int
12839 cursor_row_p (w, row)
12840 struct window *w;
12841 struct glyph_row *row;
12842 {
12843 int cursor_row_p = 1;
12844
12845 if (PT == MATRIX_ROW_END_CHARPOS (row))
12846 {
12847 /* If the row ends with a newline from a string, we don't want
12848 the cursor there (if the row is continued it doesn't end in a
12849 newline). */
12850 if (CHARPOS (row->end.string_pos) >= 0
12851 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12852 cursor_row_p = row->continued_p;
12853
12854 /* If the row ends at ZV, display the cursor at the end of that
12855 row instead of at the start of the row below. */
12856 else if (row->ends_at_zv_p)
12857 cursor_row_p = 1;
12858 else
12859 cursor_row_p = 0;
12860 }
12861
12862 return cursor_row_p;
12863 }
12864
12865
12866 /* Construct the glyph row IT->glyph_row in the desired matrix of
12867 IT->w from text at the current position of IT. See dispextern.h
12868 for an overview of struct it. Value is non-zero if
12869 IT->glyph_row displays text, as opposed to a line displaying ZV
12870 only. */
12871
12872 static int
12873 display_line (it)
12874 struct it *it;
12875 {
12876 struct glyph_row *row = it->glyph_row;
12877
12878 /* We always start displaying at hpos zero even if hscrolled. */
12879 xassert (it->hpos == 0 && it->current_x == 0);
12880
12881 /* We must not display in a row that's not a text row. */
12882 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12883 < it->w->desired_matrix->nrows);
12884
12885 /* Is IT->w showing the region? */
12886 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12887
12888 /* Clear the result glyph row and enable it. */
12889 prepare_desired_row (row);
12890
12891 row->y = it->current_y;
12892 row->start = it->current;
12893 row->continuation_lines_width = it->continuation_lines_width;
12894 row->displays_text_p = 1;
12895 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12896 it->starts_in_middle_of_char_p = 0;
12897
12898 /* Arrange the overlays nicely for our purposes. Usually, we call
12899 display_line on only one line at a time, in which case this
12900 can't really hurt too much, or we call it on lines which appear
12901 one after another in the buffer, in which case all calls to
12902 recenter_overlay_lists but the first will be pretty cheap. */
12903 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12904
12905 /* Move over display elements that are not visible because we are
12906 hscrolled. This may stop at an x-position < IT->first_visible_x
12907 if the first glyph is partially visible or if we hit a line end. */
12908 if (it->current_x < it->first_visible_x)
12909 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12910 MOVE_TO_POS | MOVE_TO_X);
12911
12912 /* Get the initial row height. This is either the height of the
12913 text hscrolled, if there is any, or zero. */
12914 row->ascent = it->max_ascent;
12915 row->height = it->max_ascent + it->max_descent;
12916 row->phys_ascent = it->max_phys_ascent;
12917 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12918
12919 /* Loop generating characters. The loop is left with IT on the next
12920 character to display. */
12921 while (1)
12922 {
12923 int n_glyphs_before, hpos_before, x_before;
12924 int x, i, nglyphs;
12925 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12926
12927 /* Retrieve the next thing to display. Value is zero if end of
12928 buffer reached. */
12929 if (!get_next_display_element (it))
12930 {
12931 /* Maybe add a space at the end of this line that is used to
12932 display the cursor there under X. Set the charpos of the
12933 first glyph of blank lines not corresponding to any text
12934 to -1. */
12935 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12936 || row->used[TEXT_AREA] == 0)
12937 {
12938 row->glyphs[TEXT_AREA]->charpos = -1;
12939 row->displays_text_p = 0;
12940
12941 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
12942 && (!MINI_WINDOW_P (it->w)
12943 || (minibuf_level && EQ (it->window, minibuf_window))))
12944 row->indicate_empty_line_p = 1;
12945 }
12946
12947 it->continuation_lines_width = 0;
12948 row->ends_at_zv_p = 1;
12949 break;
12950 }
12951
12952 /* Now, get the metrics of what we want to display. This also
12953 generates glyphs in `row' (which is IT->glyph_row). */
12954 n_glyphs_before = row->used[TEXT_AREA];
12955 x = it->current_x;
12956
12957 /* Remember the line height so far in case the next element doesn't
12958 fit on the line. */
12959 if (!it->truncate_lines_p)
12960 {
12961 ascent = it->max_ascent;
12962 descent = it->max_descent;
12963 phys_ascent = it->max_phys_ascent;
12964 phys_descent = it->max_phys_descent;
12965 }
12966
12967 PRODUCE_GLYPHS (it);
12968
12969 /* If this display element was in marginal areas, continue with
12970 the next one. */
12971 if (it->area != TEXT_AREA)
12972 {
12973 row->ascent = max (row->ascent, it->max_ascent);
12974 row->height = max (row->height, it->max_ascent + it->max_descent);
12975 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12976 row->phys_height = max (row->phys_height,
12977 it->max_phys_ascent + it->max_phys_descent);
12978 set_iterator_to_next (it, 1);
12979 continue;
12980 }
12981
12982 /* Does the display element fit on the line? If we truncate
12983 lines, we should draw past the right edge of the window. If
12984 we don't truncate, we want to stop so that we can display the
12985 continuation glyph before the right margin. If lines are
12986 continued, there are two possible strategies for characters
12987 resulting in more than 1 glyph (e.g. tabs): Display as many
12988 glyphs as possible in this line and leave the rest for the
12989 continuation line, or display the whole element in the next
12990 line. Original redisplay did the former, so we do it also. */
12991 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12992 hpos_before = it->hpos;
12993 x_before = x;
12994
12995 if (/* Not a newline. */
12996 nglyphs > 0
12997 /* Glyphs produced fit entirely in the line. */
12998 && it->current_x < it->last_visible_x)
12999 {
13000 it->hpos += nglyphs;
13001 row->ascent = max (row->ascent, it->max_ascent);
13002 row->height = max (row->height, it->max_ascent + it->max_descent);
13003 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13004 row->phys_height = max (row->phys_height,
13005 it->max_phys_ascent + it->max_phys_descent);
13006 if (it->current_x - it->pixel_width < it->first_visible_x)
13007 row->x = x - it->first_visible_x;
13008 }
13009 else
13010 {
13011 int new_x;
13012 struct glyph *glyph;
13013
13014 for (i = 0; i < nglyphs; ++i, x = new_x)
13015 {
13016 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13017 new_x = x + glyph->pixel_width;
13018
13019 if (/* Lines are continued. */
13020 !it->truncate_lines_p
13021 && (/* Glyph doesn't fit on the line. */
13022 new_x > it->last_visible_x
13023 /* Or it fits exactly on a window system frame. */
13024 || (new_x == it->last_visible_x
13025 && FRAME_WINDOW_P (it->f))))
13026 {
13027 /* End of a continued line. */
13028
13029 if (it->hpos == 0
13030 || (new_x == it->last_visible_x
13031 && FRAME_WINDOW_P (it->f)))
13032 {
13033 /* Current glyph is the only one on the line or
13034 fits exactly on the line. We must continue
13035 the line because we can't draw the cursor
13036 after the glyph. */
13037 row->continued_p = 1;
13038 it->current_x = new_x;
13039 it->continuation_lines_width += new_x;
13040 ++it->hpos;
13041 if (i == nglyphs - 1)
13042 set_iterator_to_next (it, 1);
13043 }
13044 else if (CHAR_GLYPH_PADDING_P (*glyph)
13045 && !FRAME_WINDOW_P (it->f))
13046 {
13047 /* A padding glyph that doesn't fit on this line.
13048 This means the whole character doesn't fit
13049 on the line. */
13050 row->used[TEXT_AREA] = n_glyphs_before;
13051
13052 /* Fill the rest of the row with continuation
13053 glyphs like in 20.x. */
13054 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13055 < row->glyphs[1 + TEXT_AREA])
13056 produce_special_glyphs (it, IT_CONTINUATION);
13057
13058 row->continued_p = 1;
13059 it->current_x = x_before;
13060 it->continuation_lines_width += x_before;
13061
13062 /* Restore the height to what it was before the
13063 element not fitting on the line. */
13064 it->max_ascent = ascent;
13065 it->max_descent = descent;
13066 it->max_phys_ascent = phys_ascent;
13067 it->max_phys_descent = phys_descent;
13068 }
13069 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13070 {
13071 /* A TAB that extends past the right edge of the
13072 window. This produces a single glyph on
13073 window system frames. We leave the glyph in
13074 this row and let it fill the row, but don't
13075 consume the TAB. */
13076 it->continuation_lines_width += it->last_visible_x;
13077 row->ends_in_middle_of_char_p = 1;
13078 row->continued_p = 1;
13079 glyph->pixel_width = it->last_visible_x - x;
13080 it->starts_in_middle_of_char_p = 1;
13081 }
13082 else
13083 {
13084 /* Something other than a TAB that draws past
13085 the right edge of the window. Restore
13086 positions to values before the element. */
13087 row->used[TEXT_AREA] = n_glyphs_before + i;
13088
13089 /* Display continuation glyphs. */
13090 if (!FRAME_WINDOW_P (it->f))
13091 produce_special_glyphs (it, IT_CONTINUATION);
13092 row->continued_p = 1;
13093
13094 it->continuation_lines_width += x;
13095
13096 if (nglyphs > 1 && i > 0)
13097 {
13098 row->ends_in_middle_of_char_p = 1;
13099 it->starts_in_middle_of_char_p = 1;
13100 }
13101
13102 /* Restore the height to what it was before the
13103 element not fitting on the line. */
13104 it->max_ascent = ascent;
13105 it->max_descent = descent;
13106 it->max_phys_ascent = phys_ascent;
13107 it->max_phys_descent = phys_descent;
13108 }
13109
13110 break;
13111 }
13112 else if (new_x > it->first_visible_x)
13113 {
13114 /* Increment number of glyphs actually displayed. */
13115 ++it->hpos;
13116
13117 if (x < it->first_visible_x)
13118 /* Glyph is partially visible, i.e. row starts at
13119 negative X position. */
13120 row->x = x - it->first_visible_x;
13121 }
13122 else
13123 {
13124 /* Glyph is completely off the left margin of the
13125 window. This should not happen because of the
13126 move_it_in_display_line at the start of
13127 this function. */
13128 abort ();
13129 }
13130 }
13131
13132 row->ascent = max (row->ascent, it->max_ascent);
13133 row->height = max (row->height, it->max_ascent + it->max_descent);
13134 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13135 row->phys_height = max (row->phys_height,
13136 it->max_phys_ascent + it->max_phys_descent);
13137
13138 /* End of this display line if row is continued. */
13139 if (row->continued_p)
13140 break;
13141 }
13142
13143 /* Is this a line end? If yes, we're also done, after making
13144 sure that a non-default face is extended up to the right
13145 margin of the window. */
13146 if (ITERATOR_AT_END_OF_LINE_P (it))
13147 {
13148 int used_before = row->used[TEXT_AREA];
13149
13150 row->ends_in_newline_from_string_p = STRINGP (it->object);
13151
13152 /* Add a space at the end of the line that is used to
13153 display the cursor there. */
13154 append_space (it, 0);
13155
13156 /* Extend the face to the end of the line. */
13157 extend_face_to_end_of_line (it);
13158
13159 /* Make sure we have the position. */
13160 if (used_before == 0)
13161 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13162
13163 /* Consume the line end. This skips over invisible lines. */
13164 set_iterator_to_next (it, 1);
13165 it->continuation_lines_width = 0;
13166 break;
13167 }
13168
13169 /* Proceed with next display element. Note that this skips
13170 over lines invisible because of selective display. */
13171 set_iterator_to_next (it, 1);
13172
13173 /* If we truncate lines, we are done when the last displayed
13174 glyphs reach past the right margin of the window. */
13175 if (it->truncate_lines_p
13176 && (FRAME_WINDOW_P (it->f)
13177 ? (it->current_x >= it->last_visible_x)
13178 : (it->current_x > it->last_visible_x)))
13179 {
13180 /* Maybe add truncation glyphs. */
13181 if (!FRAME_WINDOW_P (it->f))
13182 {
13183 int i, n;
13184
13185 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13186 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13187 break;
13188
13189 for (n = row->used[TEXT_AREA]; i < n; ++i)
13190 {
13191 row->used[TEXT_AREA] = i;
13192 produce_special_glyphs (it, IT_TRUNCATION);
13193 }
13194 }
13195
13196 row->truncated_on_right_p = 1;
13197 it->continuation_lines_width = 0;
13198 reseat_at_next_visible_line_start (it, 0);
13199 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13200 it->hpos = hpos_before;
13201 it->current_x = x_before;
13202 break;
13203 }
13204 }
13205
13206 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13207 at the left window margin. */
13208 if (it->first_visible_x
13209 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13210 {
13211 if (!FRAME_WINDOW_P (it->f))
13212 insert_left_trunc_glyphs (it);
13213 row->truncated_on_left_p = 1;
13214 }
13215
13216 /* If the start of this line is the overlay arrow-position, then
13217 mark this glyph row as the one containing the overlay arrow.
13218 This is clearly a mess with variable size fonts. It would be
13219 better to let it be displayed like cursors under X. */
13220 if (MARKERP (Voverlay_arrow_position)
13221 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13222 && (MATRIX_ROW_START_CHARPOS (row)
13223 == marker_position (Voverlay_arrow_position))
13224 && STRINGP (Voverlay_arrow_string)
13225 && ! overlay_arrow_seen)
13226 {
13227 /* Overlay arrow in window redisplay is a fringe bitmap. */
13228 if (!FRAME_WINDOW_P (it->f))
13229 {
13230 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13231 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13232 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13233 struct glyph *p = row->glyphs[TEXT_AREA];
13234 struct glyph *p2, *end;
13235
13236 /* Copy the arrow glyphs. */
13237 while (glyph < arrow_end)
13238 *p++ = *glyph++;
13239
13240 /* Throw away padding glyphs. */
13241 p2 = p;
13242 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13243 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13244 ++p2;
13245 if (p2 > p)
13246 {
13247 while (p2 < end)
13248 *p++ = *p2++;
13249 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13250 }
13251 }
13252
13253 overlay_arrow_seen = 1;
13254 row->overlay_arrow_p = 1;
13255 }
13256
13257 /* Compute pixel dimensions of this line. */
13258 compute_line_metrics (it);
13259
13260 /* Remember the position at which this line ends. */
13261 row->end = it->current;
13262
13263 /* Maybe set the cursor. */
13264 if (it->w->cursor.vpos < 0
13265 && PT >= MATRIX_ROW_START_CHARPOS (row)
13266 && PT <= MATRIX_ROW_END_CHARPOS (row)
13267 && cursor_row_p (it->w, row))
13268 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13269
13270 /* Highlight trailing whitespace. */
13271 if (!NILP (Vshow_trailing_whitespace))
13272 highlight_trailing_whitespace (it->f, it->glyph_row);
13273
13274 /* Prepare for the next line. This line starts horizontally at (X
13275 HPOS) = (0 0). Vertical positions are incremented. As a
13276 convenience for the caller, IT->glyph_row is set to the next
13277 row to be used. */
13278 it->current_x = it->hpos = 0;
13279 it->current_y += row->height;
13280 ++it->vpos;
13281 ++it->glyph_row;
13282 return row->displays_text_p;
13283 }
13284
13285
13286 \f
13287 /***********************************************************************
13288 Menu Bar
13289 ***********************************************************************/
13290
13291 /* Redisplay the menu bar in the frame for window W.
13292
13293 The menu bar of X frames that don't have X toolkit support is
13294 displayed in a special window W->frame->menu_bar_window.
13295
13296 The menu bar of terminal frames is treated specially as far as
13297 glyph matrices are concerned. Menu bar lines are not part of
13298 windows, so the update is done directly on the frame matrix rows
13299 for the menu bar. */
13300
13301 static void
13302 display_menu_bar (w)
13303 struct window *w;
13304 {
13305 struct frame *f = XFRAME (WINDOW_FRAME (w));
13306 struct it it;
13307 Lisp_Object items;
13308 int i;
13309
13310 /* Don't do all this for graphical frames. */
13311 #ifdef HAVE_NTGUI
13312 if (!NILP (Vwindow_system))
13313 return;
13314 #endif
13315 #ifdef USE_X_TOOLKIT
13316 if (FRAME_X_P (f))
13317 return;
13318 #endif
13319 #ifdef macintosh
13320 if (FRAME_MAC_P (f))
13321 return;
13322 #endif
13323
13324 #ifdef USE_X_TOOLKIT
13325 xassert (!FRAME_WINDOW_P (f));
13326 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13327 it.first_visible_x = 0;
13328 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13329 #else /* not USE_X_TOOLKIT */
13330 if (FRAME_WINDOW_P (f))
13331 {
13332 /* Menu bar lines are displayed in the desired matrix of the
13333 dummy window menu_bar_window. */
13334 struct window *menu_w;
13335 xassert (WINDOWP (f->menu_bar_window));
13336 menu_w = XWINDOW (f->menu_bar_window);
13337 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13338 MENU_FACE_ID);
13339 it.first_visible_x = 0;
13340 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13341 }
13342 else
13343 {
13344 /* This is a TTY frame, i.e. character hpos/vpos are used as
13345 pixel x/y. */
13346 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13347 MENU_FACE_ID);
13348 it.first_visible_x = 0;
13349 it.last_visible_x = FRAME_WIDTH (f);
13350 }
13351 #endif /* not USE_X_TOOLKIT */
13352
13353 if (! mode_line_inverse_video)
13354 /* Force the menu-bar to be displayed in the default face. */
13355 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13356
13357 /* Clear all rows of the menu bar. */
13358 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13359 {
13360 struct glyph_row *row = it.glyph_row + i;
13361 clear_glyph_row (row);
13362 row->enabled_p = 1;
13363 row->full_width_p = 1;
13364 }
13365
13366 /* Display all items of the menu bar. */
13367 items = FRAME_MENU_BAR_ITEMS (it.f);
13368 for (i = 0; i < XVECTOR (items)->size; i += 4)
13369 {
13370 Lisp_Object string;
13371
13372 /* Stop at nil string. */
13373 string = AREF (items, i + 1);
13374 if (NILP (string))
13375 break;
13376
13377 /* Remember where item was displayed. */
13378 AREF (items, i + 3) = make_number (it.hpos);
13379
13380 /* Display the item, pad with one space. */
13381 if (it.current_x < it.last_visible_x)
13382 display_string (NULL, string, Qnil, 0, 0, &it,
13383 SCHARS (string) + 1, 0, 0, -1);
13384 }
13385
13386 /* Fill out the line with spaces. */
13387 if (it.current_x < it.last_visible_x)
13388 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13389
13390 /* Compute the total height of the lines. */
13391 compute_line_metrics (&it);
13392 }
13393
13394
13395 \f
13396 /***********************************************************************
13397 Mode Line
13398 ***********************************************************************/
13399
13400 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13401 FORCE is non-zero, redisplay mode lines unconditionally.
13402 Otherwise, redisplay only mode lines that are garbaged. Value is
13403 the number of windows whose mode lines were redisplayed. */
13404
13405 static int
13406 redisplay_mode_lines (window, force)
13407 Lisp_Object window;
13408 int force;
13409 {
13410 int nwindows = 0;
13411
13412 while (!NILP (window))
13413 {
13414 struct window *w = XWINDOW (window);
13415
13416 if (WINDOWP (w->hchild))
13417 nwindows += redisplay_mode_lines (w->hchild, force);
13418 else if (WINDOWP (w->vchild))
13419 nwindows += redisplay_mode_lines (w->vchild, force);
13420 else if (force
13421 || FRAME_GARBAGED_P (XFRAME (w->frame))
13422 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13423 {
13424 struct text_pos lpoint;
13425 struct buffer *old = current_buffer;
13426
13427 /* Set the window's buffer for the mode line display. */
13428 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13429 set_buffer_internal_1 (XBUFFER (w->buffer));
13430
13431 /* Point refers normally to the selected window. For any
13432 other window, set up appropriate value. */
13433 if (!EQ (window, selected_window))
13434 {
13435 struct text_pos pt;
13436
13437 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13438 if (CHARPOS (pt) < BEGV)
13439 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13440 else if (CHARPOS (pt) > (ZV - 1))
13441 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13442 else
13443 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13444 }
13445
13446 /* Display mode lines. */
13447 clear_glyph_matrix (w->desired_matrix);
13448 if (display_mode_lines (w))
13449 {
13450 ++nwindows;
13451 w->must_be_updated_p = 1;
13452 }
13453
13454 /* Restore old settings. */
13455 set_buffer_internal_1 (old);
13456 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13457 }
13458
13459 window = w->next;
13460 }
13461
13462 return nwindows;
13463 }
13464
13465
13466 /* Display the mode and/or top line of window W. Value is the number
13467 of mode lines displayed. */
13468
13469 static int
13470 display_mode_lines (w)
13471 struct window *w;
13472 {
13473 Lisp_Object old_selected_window, old_selected_frame;
13474 int n = 0;
13475
13476 old_selected_frame = selected_frame;
13477 selected_frame = w->frame;
13478 old_selected_window = selected_window;
13479 XSETWINDOW (selected_window, w);
13480
13481 /* These will be set while the mode line specs are processed. */
13482 line_number_displayed = 0;
13483 w->column_number_displayed = Qnil;
13484
13485 if (WINDOW_WANTS_MODELINE_P (w))
13486 {
13487 struct window *sel_w = XWINDOW (old_selected_window);
13488
13489 /* Select mode line face based on the real selected window. */
13490 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
13491 current_buffer->mode_line_format);
13492 ++n;
13493 }
13494
13495 if (WINDOW_WANTS_HEADER_LINE_P (w))
13496 {
13497 display_mode_line (w, HEADER_LINE_FACE_ID,
13498 current_buffer->header_line_format);
13499 ++n;
13500 }
13501
13502 selected_frame = old_selected_frame;
13503 selected_window = old_selected_window;
13504 return n;
13505 }
13506
13507
13508 /* Display mode or top line of window W. FACE_ID specifies which line
13509 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13510 FORMAT is the mode line format to display. Value is the pixel
13511 height of the mode line displayed. */
13512
13513 static int
13514 display_mode_line (w, face_id, format)
13515 struct window *w;
13516 enum face_id face_id;
13517 Lisp_Object format;
13518 {
13519 struct it it;
13520 struct face *face;
13521
13522 init_iterator (&it, w, -1, -1, NULL, face_id);
13523 prepare_desired_row (it.glyph_row);
13524
13525 if (! mode_line_inverse_video)
13526 /* Force the mode-line to be displayed in the default face. */
13527 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13528
13529 /* Temporarily make frame's keyboard the current kboard so that
13530 kboard-local variables in the mode_line_format will get the right
13531 values. */
13532 push_frame_kboard (it.f);
13533 display_mode_element (&it, 0, 0, 0, format, Qnil);
13534 pop_frame_kboard ();
13535
13536 /* Fill up with spaces. */
13537 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13538
13539 compute_line_metrics (&it);
13540 it.glyph_row->full_width_p = 1;
13541 it.glyph_row->mode_line_p = 1;
13542 it.glyph_row->continued_p = 0;
13543 it.glyph_row->truncated_on_left_p = 0;
13544 it.glyph_row->truncated_on_right_p = 0;
13545
13546 /* Make a 3D mode-line have a shadow at its right end. */
13547 face = FACE_FROM_ID (it.f, face_id);
13548 extend_face_to_end_of_line (&it);
13549 if (face->box != FACE_NO_BOX)
13550 {
13551 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13552 + it.glyph_row->used[TEXT_AREA] - 1);
13553 last->right_box_line_p = 1;
13554 }
13555
13556 return it.glyph_row->height;
13557 }
13558
13559 /* Alist that caches the results of :propertize.
13560 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
13561 Lisp_Object mode_line_proptrans_alist;
13562
13563 /* Contribute ELT to the mode line for window IT->w. How it
13564 translates into text depends on its data type.
13565
13566 IT describes the display environment in which we display, as usual.
13567
13568 DEPTH is the depth in recursion. It is used to prevent
13569 infinite recursion here.
13570
13571 FIELD_WIDTH is the number of characters the display of ELT should
13572 occupy in the mode line, and PRECISION is the maximum number of
13573 characters to display from ELT's representation. See
13574 display_string for details.
13575
13576 Returns the hpos of the end of the text generated by ELT. */
13577
13578 static int
13579 display_mode_element (it, depth, field_width, precision, elt, props)
13580 struct it *it;
13581 int depth;
13582 int field_width, precision;
13583 Lisp_Object elt, props;
13584 {
13585 int n = 0, field, prec;
13586 int literal = 0;
13587
13588 tail_recurse:
13589 if (depth > 10)
13590 goto invalid;
13591
13592 depth++;
13593
13594 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13595 {
13596 case Lisp_String:
13597 {
13598 /* A string: output it and check for %-constructs within it. */
13599 unsigned char c;
13600 unsigned char *this, *lisp_string;
13601
13602 if (!NILP (props))
13603 {
13604 Lisp_Object oprops, aelt;
13605 oprops = Ftext_properties_at (make_number (0), elt);
13606 if (NILP (Fequal (props, oprops)))
13607 {
13608 /* If the starting string has properties,
13609 merge the specified ones onto the existing ones. */
13610 if (! NILP (oprops))
13611 {
13612 Lisp_Object tem;
13613
13614 oprops = Fcopy_sequence (oprops);
13615 tem = props;
13616 while (CONSP (tem))
13617 {
13618 oprops = Fplist_put (oprops, XCAR (tem),
13619 XCAR (XCDR (tem)));
13620 tem = XCDR (XCDR (tem));
13621 }
13622 props = oprops;
13623 }
13624
13625 aelt = Fassoc (elt, mode_line_proptrans_alist);
13626 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
13627 elt = XCAR (aelt);
13628 else
13629 {
13630 elt = Fcopy_sequence (elt);
13631 Fset_text_properties (0, Flength (elt), props, elt);
13632 mode_line_proptrans_alist
13633 = Fcons (Fcons (elt, props),
13634 mode_line_proptrans_alist);
13635 }
13636 }
13637 }
13638
13639 this = SDATA (elt);
13640 lisp_string = this;
13641
13642 if (literal)
13643 {
13644 prec = precision - n;
13645 if (frame_title_ptr)
13646 n += store_frame_title (SDATA (elt), -1, prec);
13647 else
13648 n += display_string (NULL, elt, Qnil, 0, 0, it,
13649 0, prec, 0, STRING_MULTIBYTE (elt));
13650
13651 break;
13652 }
13653
13654 while ((precision <= 0 || n < precision)
13655 && *this
13656 && (frame_title_ptr
13657 || it->current_x < it->last_visible_x))
13658 {
13659 unsigned char *last = this;
13660
13661 /* Advance to end of string or next format specifier. */
13662 while ((c = *this++) != '\0' && c != '%')
13663 ;
13664
13665 if (this - 1 != last)
13666 {
13667 /* Output to end of string or up to '%'. Field width
13668 is length of string. Don't output more than
13669 PRECISION allows us. */
13670 --this;
13671
13672 prec = chars_in_text (last, this - last);
13673 if (precision > 0 && prec > precision - n)
13674 prec = precision - n;
13675
13676 if (frame_title_ptr)
13677 n += store_frame_title (last, 0, prec);
13678 else
13679 {
13680 int bytepos = last - lisp_string;
13681 int charpos = string_byte_to_char (elt, bytepos);
13682 n += display_string (NULL, elt, Qnil, 0, charpos,
13683 it, 0, prec, 0,
13684 STRING_MULTIBYTE (elt));
13685 }
13686 }
13687 else /* c == '%' */
13688 {
13689 unsigned char *percent_position = this;
13690
13691 /* Get the specified minimum width. Zero means
13692 don't pad. */
13693 field = 0;
13694 while ((c = *this++) >= '0' && c <= '9')
13695 field = field * 10 + c - '0';
13696
13697 /* Don't pad beyond the total padding allowed. */
13698 if (field_width - n > 0 && field > field_width - n)
13699 field = field_width - n;
13700
13701 /* Note that either PRECISION <= 0 or N < PRECISION. */
13702 prec = precision - n;
13703
13704 if (c == 'M')
13705 n += display_mode_element (it, depth, field, prec,
13706 Vglobal_mode_string, props);
13707 else if (c != 0)
13708 {
13709 int multibyte;
13710 int bytepos, charpos;
13711 unsigned char *spec;
13712
13713 bytepos = percent_position - lisp_string;
13714 charpos = (STRING_MULTIBYTE (elt)
13715 ? string_byte_to_char (elt, bytepos)
13716 : bytepos);
13717
13718 spec
13719 = decode_mode_spec (it->w, c, field, prec, &multibyte);
13720
13721 if (frame_title_ptr)
13722 n += store_frame_title (spec, field, prec);
13723 else
13724 {
13725 int nglyphs_before, nwritten;
13726
13727 nglyphs_before = it->glyph_row->used[TEXT_AREA];
13728 nwritten = display_string (spec, Qnil, elt,
13729 charpos, 0, it,
13730 field, prec, 0,
13731 multibyte);
13732
13733 /* Assign to the glyphs written above the
13734 string where the `%x' came from, position
13735 of the `%'. */
13736 if (nwritten > 0)
13737 {
13738 struct glyph *glyph
13739 = (it->glyph_row->glyphs[TEXT_AREA]
13740 + nglyphs_before);
13741 int i;
13742
13743 for (i = 0; i < nwritten; ++i)
13744 {
13745 glyph[i].object = elt;
13746 glyph[i].charpos = charpos;
13747 }
13748
13749 n += nwritten;
13750 }
13751 }
13752 }
13753 else /* c == 0 */
13754 break;
13755 }
13756 }
13757 }
13758 break;
13759
13760 case Lisp_Symbol:
13761 /* A symbol: process the value of the symbol recursively
13762 as if it appeared here directly. Avoid error if symbol void.
13763 Special case: if value of symbol is a string, output the string
13764 literally. */
13765 {
13766 register Lisp_Object tem;
13767 tem = Fboundp (elt);
13768 if (!NILP (tem))
13769 {
13770 tem = Fsymbol_value (elt);
13771 /* If value is a string, output that string literally:
13772 don't check for % within it. */
13773 if (STRINGP (tem))
13774 literal = 1;
13775
13776 if (!EQ (tem, elt))
13777 {
13778 /* Give up right away for nil or t. */
13779 elt = tem;
13780 goto tail_recurse;
13781 }
13782 }
13783 }
13784 break;
13785
13786 case Lisp_Cons:
13787 {
13788 register Lisp_Object car, tem;
13789
13790 /* A cons cell: five distinct cases.
13791 If first element is :eval or :propertize, do something special.
13792 If first element is a string or a cons, process all the elements
13793 and effectively concatenate them.
13794 If first element is a negative number, truncate displaying cdr to
13795 at most that many characters. If positive, pad (with spaces)
13796 to at least that many characters.
13797 If first element is a symbol, process the cadr or caddr recursively
13798 according to whether the symbol's value is non-nil or nil. */
13799 car = XCAR (elt);
13800 if (EQ (car, QCeval))
13801 {
13802 /* An element of the form (:eval FORM) means evaluate FORM
13803 and use the result as mode line elements. */
13804
13805 if (CONSP (XCDR (elt)))
13806 {
13807 Lisp_Object spec;
13808 spec = safe_eval (XCAR (XCDR (elt)));
13809 n += display_mode_element (it, depth, field_width - n,
13810 precision - n, spec, props);
13811 }
13812 }
13813 else if (EQ (car, QCpropertize))
13814 {
13815 if (CONSP (XCDR (elt)))
13816 {
13817 /* An element of the form (:propertize ELT PROPS...)
13818 means display ELT but applying properties PROPS. */
13819 n += display_mode_element (it, depth, field_width - n,
13820 precision - n, XCAR (XCDR (elt)),
13821 XCDR (XCDR (elt)));
13822 }
13823 }
13824 else if (SYMBOLP (car))
13825 {
13826 tem = Fboundp (car);
13827 elt = XCDR (elt);
13828 if (!CONSP (elt))
13829 goto invalid;
13830 /* elt is now the cdr, and we know it is a cons cell.
13831 Use its car if CAR has a non-nil value. */
13832 if (!NILP (tem))
13833 {
13834 tem = Fsymbol_value (car);
13835 if (!NILP (tem))
13836 {
13837 elt = XCAR (elt);
13838 goto tail_recurse;
13839 }
13840 }
13841 /* Symbol's value is nil (or symbol is unbound)
13842 Get the cddr of the original list
13843 and if possible find the caddr and use that. */
13844 elt = XCDR (elt);
13845 if (NILP (elt))
13846 break;
13847 else if (!CONSP (elt))
13848 goto invalid;
13849 elt = XCAR (elt);
13850 goto tail_recurse;
13851 }
13852 else if (INTEGERP (car))
13853 {
13854 register int lim = XINT (car);
13855 elt = XCDR (elt);
13856 if (lim < 0)
13857 {
13858 /* Negative int means reduce maximum width. */
13859 if (precision <= 0)
13860 precision = -lim;
13861 else
13862 precision = min (precision, -lim);
13863 }
13864 else if (lim > 0)
13865 {
13866 /* Padding specified. Don't let it be more than
13867 current maximum. */
13868 if (precision > 0)
13869 lim = min (precision, lim);
13870
13871 /* If that's more padding than already wanted, queue it.
13872 But don't reduce padding already specified even if
13873 that is beyond the current truncation point. */
13874 field_width = max (lim, field_width);
13875 }
13876 goto tail_recurse;
13877 }
13878 else if (STRINGP (car) || CONSP (car))
13879 {
13880 register int limit = 50;
13881 /* Limit is to protect against circular lists. */
13882 while (CONSP (elt)
13883 && --limit > 0
13884 && (precision <= 0 || n < precision))
13885 {
13886 n += display_mode_element (it, depth, field_width - n,
13887 precision - n, XCAR (elt), props);
13888 elt = XCDR (elt);
13889 }
13890 }
13891 }
13892 break;
13893
13894 default:
13895 invalid:
13896 if (frame_title_ptr)
13897 n += store_frame_title ("*invalid*", 0, precision - n);
13898 else
13899 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13900 precision - n, 0, 0);
13901 return n;
13902 }
13903
13904 /* Pad to FIELD_WIDTH. */
13905 if (field_width > 0 && n < field_width)
13906 {
13907 if (frame_title_ptr)
13908 n += store_frame_title ("", field_width - n, 0);
13909 else
13910 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13911 0, 0, 0);
13912 }
13913
13914 return n;
13915 }
13916
13917
13918 /* Write a null-terminated, right justified decimal representation of
13919 the positive integer D to BUF using a minimal field width WIDTH. */
13920
13921 static void
13922 pint2str (buf, width, d)
13923 register char *buf;
13924 register int width;
13925 register int d;
13926 {
13927 register char *p = buf;
13928
13929 if (d <= 0)
13930 *p++ = '0';
13931 else
13932 {
13933 while (d > 0)
13934 {
13935 *p++ = d % 10 + '0';
13936 d /= 10;
13937 }
13938 }
13939
13940 for (width -= (int) (p - buf); width > 0; --width)
13941 *p++ = ' ';
13942 *p-- = '\0';
13943 while (p > buf)
13944 {
13945 d = *buf;
13946 *buf++ = *p;
13947 *p-- = d;
13948 }
13949 }
13950
13951 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
13952 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13953 type of CODING_SYSTEM. Return updated pointer into BUF. */
13954
13955 static unsigned char invalid_eol_type[] = "(*invalid*)";
13956
13957 static char *
13958 decode_mode_spec_coding (coding_system, buf, eol_flag)
13959 Lisp_Object coding_system;
13960 register char *buf;
13961 int eol_flag;
13962 {
13963 Lisp_Object val;
13964 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
13965 unsigned char *eol_str;
13966 int eol_str_len;
13967 /* The EOL conversion we are using. */
13968 Lisp_Object eoltype;
13969
13970 val = Fget (coding_system, Qcoding_system);
13971 eoltype = Qnil;
13972
13973 if (!VECTORP (val)) /* Not yet decided. */
13974 {
13975 if (multibyte)
13976 *buf++ = '-';
13977 if (eol_flag)
13978 eoltype = eol_mnemonic_undecided;
13979 /* Don't mention EOL conversion if it isn't decided. */
13980 }
13981 else
13982 {
13983 Lisp_Object eolvalue;
13984
13985 eolvalue = Fget (coding_system, Qeol_type);
13986
13987 if (multibyte)
13988 *buf++ = XFASTINT (AREF (val, 1));
13989
13990 if (eol_flag)
13991 {
13992 /* The EOL conversion that is normal on this system. */
13993
13994 if (NILP (eolvalue)) /* Not yet decided. */
13995 eoltype = eol_mnemonic_undecided;
13996 else if (VECTORP (eolvalue)) /* Not yet decided. */
13997 eoltype = eol_mnemonic_undecided;
13998 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
13999 eoltype = (XFASTINT (eolvalue) == 0
14000 ? eol_mnemonic_unix
14001 : (XFASTINT (eolvalue) == 1
14002 ? eol_mnemonic_dos : eol_mnemonic_mac));
14003 }
14004 }
14005
14006 if (eol_flag)
14007 {
14008 /* Mention the EOL conversion if it is not the usual one. */
14009 if (STRINGP (eoltype))
14010 {
14011 eol_str = SDATA (eoltype);
14012 eol_str_len = SBYTES (eoltype);
14013 }
14014 else if (INTEGERP (eoltype)
14015 && CHAR_VALID_P (XINT (eoltype), 0))
14016 {
14017 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
14018 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
14019 }
14020 else
14021 {
14022 eol_str = invalid_eol_type;
14023 eol_str_len = sizeof (invalid_eol_type) - 1;
14024 }
14025 bcopy (eol_str, buf, eol_str_len);
14026 buf += eol_str_len;
14027 }
14028
14029 return buf;
14030 }
14031
14032 /* Return a string for the output of a mode line %-spec for window W,
14033 generated by character C. PRECISION >= 0 means don't return a
14034 string longer than that value. FIELD_WIDTH > 0 means pad the
14035 string returned with spaces to that value. Return 1 in *MULTIBYTE
14036 if the result is multibyte text. */
14037
14038 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
14039
14040 static char *
14041 decode_mode_spec (w, c, field_width, precision, multibyte)
14042 struct window *w;
14043 register int c;
14044 int field_width, precision;
14045 int *multibyte;
14046 {
14047 Lisp_Object obj;
14048 struct frame *f = XFRAME (WINDOW_FRAME (w));
14049 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
14050 struct buffer *b = XBUFFER (w->buffer);
14051
14052 obj = Qnil;
14053 *multibyte = 0;
14054
14055 switch (c)
14056 {
14057 case '*':
14058 if (!NILP (b->read_only))
14059 return "%";
14060 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14061 return "*";
14062 return "-";
14063
14064 case '+':
14065 /* This differs from %* only for a modified read-only buffer. */
14066 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14067 return "*";
14068 if (!NILP (b->read_only))
14069 return "%";
14070 return "-";
14071
14072 case '&':
14073 /* This differs from %* in ignoring read-only-ness. */
14074 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14075 return "*";
14076 return "-";
14077
14078 case '%':
14079 return "%";
14080
14081 case '[':
14082 {
14083 int i;
14084 char *p;
14085
14086 if (command_loop_level > 5)
14087 return "[[[... ";
14088 p = decode_mode_spec_buf;
14089 for (i = 0; i < command_loop_level; i++)
14090 *p++ = '[';
14091 *p = 0;
14092 return decode_mode_spec_buf;
14093 }
14094
14095 case ']':
14096 {
14097 int i;
14098 char *p;
14099
14100 if (command_loop_level > 5)
14101 return " ...]]]";
14102 p = decode_mode_spec_buf;
14103 for (i = 0; i < command_loop_level; i++)
14104 *p++ = ']';
14105 *p = 0;
14106 return decode_mode_spec_buf;
14107 }
14108
14109 case '-':
14110 {
14111 register int i;
14112
14113 /* Let lots_of_dashes be a string of infinite length. */
14114 if (field_width <= 0
14115 || field_width > sizeof (lots_of_dashes))
14116 {
14117 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
14118 decode_mode_spec_buf[i] = '-';
14119 decode_mode_spec_buf[i] = '\0';
14120 return decode_mode_spec_buf;
14121 }
14122 else
14123 return lots_of_dashes;
14124 }
14125
14126 case 'b':
14127 obj = b->name;
14128 break;
14129
14130 case 'c':
14131 {
14132 int col = current_column ();
14133 w->column_number_displayed = make_number (col);
14134 pint2str (decode_mode_spec_buf, field_width, col);
14135 return decode_mode_spec_buf;
14136 }
14137
14138 case 'F':
14139 /* %F displays the frame name. */
14140 if (!NILP (f->title))
14141 return (char *) SDATA (f->title);
14142 if (f->explicit_name || ! FRAME_WINDOW_P (f))
14143 return (char *) SDATA (f->name);
14144 return "Emacs";
14145
14146 case 'f':
14147 obj = b->filename;
14148 break;
14149
14150 case 'l':
14151 {
14152 int startpos = XMARKER (w->start)->charpos;
14153 int startpos_byte = marker_byte_position (w->start);
14154 int line, linepos, linepos_byte, topline;
14155 int nlines, junk;
14156 int height = XFASTINT (w->height);
14157
14158 /* If we decided that this buffer isn't suitable for line numbers,
14159 don't forget that too fast. */
14160 if (EQ (w->base_line_pos, w->buffer))
14161 goto no_value;
14162 /* But do forget it, if the window shows a different buffer now. */
14163 else if (BUFFERP (w->base_line_pos))
14164 w->base_line_pos = Qnil;
14165
14166 /* If the buffer is very big, don't waste time. */
14167 if (INTEGERP (Vline_number_display_limit)
14168 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
14169 {
14170 w->base_line_pos = Qnil;
14171 w->base_line_number = Qnil;
14172 goto no_value;
14173 }
14174
14175 if (!NILP (w->base_line_number)
14176 && !NILP (w->base_line_pos)
14177 && XFASTINT (w->base_line_pos) <= startpos)
14178 {
14179 line = XFASTINT (w->base_line_number);
14180 linepos = XFASTINT (w->base_line_pos);
14181 linepos_byte = buf_charpos_to_bytepos (b, linepos);
14182 }
14183 else
14184 {
14185 line = 1;
14186 linepos = BUF_BEGV (b);
14187 linepos_byte = BUF_BEGV_BYTE (b);
14188 }
14189
14190 /* Count lines from base line to window start position. */
14191 nlines = display_count_lines (linepos, linepos_byte,
14192 startpos_byte,
14193 startpos, &junk);
14194
14195 topline = nlines + line;
14196
14197 /* Determine a new base line, if the old one is too close
14198 or too far away, or if we did not have one.
14199 "Too close" means it's plausible a scroll-down would
14200 go back past it. */
14201 if (startpos == BUF_BEGV (b))
14202 {
14203 w->base_line_number = make_number (topline);
14204 w->base_line_pos = make_number (BUF_BEGV (b));
14205 }
14206 else if (nlines < height + 25 || nlines > height * 3 + 50
14207 || linepos == BUF_BEGV (b))
14208 {
14209 int limit = BUF_BEGV (b);
14210 int limit_byte = BUF_BEGV_BYTE (b);
14211 int position;
14212 int distance = (height * 2 + 30) * line_number_display_limit_width;
14213
14214 if (startpos - distance > limit)
14215 {
14216 limit = startpos - distance;
14217 limit_byte = CHAR_TO_BYTE (limit);
14218 }
14219
14220 nlines = display_count_lines (startpos, startpos_byte,
14221 limit_byte,
14222 - (height * 2 + 30),
14223 &position);
14224 /* If we couldn't find the lines we wanted within
14225 line_number_display_limit_width chars per line,
14226 give up on line numbers for this window. */
14227 if (position == limit_byte && limit == startpos - distance)
14228 {
14229 w->base_line_pos = w->buffer;
14230 w->base_line_number = Qnil;
14231 goto no_value;
14232 }
14233
14234 w->base_line_number = make_number (topline - nlines);
14235 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
14236 }
14237
14238 /* Now count lines from the start pos to point. */
14239 nlines = display_count_lines (startpos, startpos_byte,
14240 PT_BYTE, PT, &junk);
14241
14242 /* Record that we did display the line number. */
14243 line_number_displayed = 1;
14244
14245 /* Make the string to show. */
14246 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
14247 return decode_mode_spec_buf;
14248 no_value:
14249 {
14250 char* p = decode_mode_spec_buf;
14251 int pad = field_width - 2;
14252 while (pad-- > 0)
14253 *p++ = ' ';
14254 *p++ = '?';
14255 *p++ = '?';
14256 *p = '\0';
14257 return decode_mode_spec_buf;
14258 }
14259 }
14260 break;
14261
14262 case 'm':
14263 obj = b->mode_name;
14264 break;
14265
14266 case 'n':
14267 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
14268 return " Narrow";
14269 break;
14270
14271 case 'p':
14272 {
14273 int pos = marker_position (w->start);
14274 int total = BUF_ZV (b) - BUF_BEGV (b);
14275
14276 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
14277 {
14278 if (pos <= BUF_BEGV (b))
14279 return "All";
14280 else
14281 return "Bottom";
14282 }
14283 else if (pos <= BUF_BEGV (b))
14284 return "Top";
14285 else
14286 {
14287 if (total > 1000000)
14288 /* Do it differently for a large value, to avoid overflow. */
14289 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14290 else
14291 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14292 /* We can't normally display a 3-digit number,
14293 so get us a 2-digit number that is close. */
14294 if (total == 100)
14295 total = 99;
14296 sprintf (decode_mode_spec_buf, "%2d%%", total);
14297 return decode_mode_spec_buf;
14298 }
14299 }
14300
14301 /* Display percentage of size above the bottom of the screen. */
14302 case 'P':
14303 {
14304 int toppos = marker_position (w->start);
14305 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14306 int total = BUF_ZV (b) - BUF_BEGV (b);
14307
14308 if (botpos >= BUF_ZV (b))
14309 {
14310 if (toppos <= BUF_BEGV (b))
14311 return "All";
14312 else
14313 return "Bottom";
14314 }
14315 else
14316 {
14317 if (total > 1000000)
14318 /* Do it differently for a large value, to avoid overflow. */
14319 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14320 else
14321 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14322 /* We can't normally display a 3-digit number,
14323 so get us a 2-digit number that is close. */
14324 if (total == 100)
14325 total = 99;
14326 if (toppos <= BUF_BEGV (b))
14327 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14328 else
14329 sprintf (decode_mode_spec_buf, "%2d%%", total);
14330 return decode_mode_spec_buf;
14331 }
14332 }
14333
14334 case 's':
14335 /* status of process */
14336 obj = Fget_buffer_process (w->buffer);
14337 if (NILP (obj))
14338 return "no process";
14339 #ifdef subprocesses
14340 obj = Fsymbol_name (Fprocess_status (obj));
14341 #endif
14342 break;
14343
14344 case 't': /* indicate TEXT or BINARY */
14345 #ifdef MODE_LINE_BINARY_TEXT
14346 return MODE_LINE_BINARY_TEXT (b);
14347 #else
14348 return "T";
14349 #endif
14350
14351 case 'z':
14352 /* coding-system (not including end-of-line format) */
14353 case 'Z':
14354 /* coding-system (including end-of-line type) */
14355 {
14356 int eol_flag = (c == 'Z');
14357 char *p = decode_mode_spec_buf;
14358
14359 if (! FRAME_WINDOW_P (f))
14360 {
14361 /* No need to mention EOL here--the terminal never needs
14362 to do EOL conversion. */
14363 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14364 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
14365 }
14366 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14367 p, eol_flag);
14368
14369 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14370 #ifdef subprocesses
14371 obj = Fget_buffer_process (Fcurrent_buffer ());
14372 if (PROCESSP (obj))
14373 {
14374 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14375 p, eol_flag);
14376 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14377 p, eol_flag);
14378 }
14379 #endif /* subprocesses */
14380 #endif /* 0 */
14381 *p = 0;
14382 return decode_mode_spec_buf;
14383 }
14384 }
14385
14386 if (STRINGP (obj))
14387 {
14388 *multibyte = STRING_MULTIBYTE (obj);
14389 return (char *) SDATA (obj);
14390 }
14391 else
14392 return "";
14393 }
14394
14395
14396 /* Count up to COUNT lines starting from START / START_BYTE.
14397 But don't go beyond LIMIT_BYTE.
14398 Return the number of lines thus found (always nonnegative).
14399
14400 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14401
14402 static int
14403 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14404 int start, start_byte, limit_byte, count;
14405 int *byte_pos_ptr;
14406 {
14407 register unsigned char *cursor;
14408 unsigned char *base;
14409
14410 register int ceiling;
14411 register unsigned char *ceiling_addr;
14412 int orig_count = count;
14413
14414 /* If we are not in selective display mode,
14415 check only for newlines. */
14416 int selective_display = (!NILP (current_buffer->selective_display)
14417 && !INTEGERP (current_buffer->selective_display));
14418
14419 if (count > 0)
14420 {
14421 while (start_byte < limit_byte)
14422 {
14423 ceiling = BUFFER_CEILING_OF (start_byte);
14424 ceiling = min (limit_byte - 1, ceiling);
14425 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14426 base = (cursor = BYTE_POS_ADDR (start_byte));
14427 while (1)
14428 {
14429 if (selective_display)
14430 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14431 ;
14432 else
14433 while (*cursor != '\n' && ++cursor != ceiling_addr)
14434 ;
14435
14436 if (cursor != ceiling_addr)
14437 {
14438 if (--count == 0)
14439 {
14440 start_byte += cursor - base + 1;
14441 *byte_pos_ptr = start_byte;
14442 return orig_count;
14443 }
14444 else
14445 if (++cursor == ceiling_addr)
14446 break;
14447 }
14448 else
14449 break;
14450 }
14451 start_byte += cursor - base;
14452 }
14453 }
14454 else
14455 {
14456 while (start_byte > limit_byte)
14457 {
14458 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14459 ceiling = max (limit_byte, ceiling);
14460 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14461 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14462 while (1)
14463 {
14464 if (selective_display)
14465 while (--cursor != ceiling_addr
14466 && *cursor != '\n' && *cursor != 015)
14467 ;
14468 else
14469 while (--cursor != ceiling_addr && *cursor != '\n')
14470 ;
14471
14472 if (cursor != ceiling_addr)
14473 {
14474 if (++count == 0)
14475 {
14476 start_byte += cursor - base + 1;
14477 *byte_pos_ptr = start_byte;
14478 /* When scanning backwards, we should
14479 not count the newline posterior to which we stop. */
14480 return - orig_count - 1;
14481 }
14482 }
14483 else
14484 break;
14485 }
14486 /* Here we add 1 to compensate for the last decrement
14487 of CURSOR, which took it past the valid range. */
14488 start_byte += cursor - base + 1;
14489 }
14490 }
14491
14492 *byte_pos_ptr = limit_byte;
14493
14494 if (count < 0)
14495 return - orig_count + count;
14496 return orig_count - count;
14497
14498 }
14499
14500
14501 \f
14502 /***********************************************************************
14503 Displaying strings
14504 ***********************************************************************/
14505
14506 /* Display a NUL-terminated string, starting with index START.
14507
14508 If STRING is non-null, display that C string. Otherwise, the Lisp
14509 string LISP_STRING is displayed.
14510
14511 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14512 FACE_STRING. Display STRING or LISP_STRING with the face at
14513 FACE_STRING_POS in FACE_STRING:
14514
14515 Display the string in the environment given by IT, but use the
14516 standard display table, temporarily.
14517
14518 FIELD_WIDTH is the minimum number of output glyphs to produce.
14519 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14520 with spaces. If STRING has more characters, more than FIELD_WIDTH
14521 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14522
14523 PRECISION is the maximum number of characters to output from
14524 STRING. PRECISION < 0 means don't truncate the string.
14525
14526 This is roughly equivalent to printf format specifiers:
14527
14528 FIELD_WIDTH PRECISION PRINTF
14529 ----------------------------------------
14530 -1 -1 %s
14531 -1 10 %.10s
14532 10 -1 %10s
14533 20 10 %20.10s
14534
14535 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14536 display them, and < 0 means obey the current buffer's value of
14537 enable_multibyte_characters.
14538
14539 Value is the number of glyphs produced. */
14540
14541 static int
14542 display_string (string, lisp_string, face_string, face_string_pos,
14543 start, it, field_width, precision, max_x, multibyte)
14544 unsigned char *string;
14545 Lisp_Object lisp_string;
14546 Lisp_Object face_string;
14547 int face_string_pos;
14548 int start;
14549 struct it *it;
14550 int field_width, precision, max_x;
14551 int multibyte;
14552 {
14553 int hpos_at_start = it->hpos;
14554 int saved_face_id = it->face_id;
14555 struct glyph_row *row = it->glyph_row;
14556
14557 /* Initialize the iterator IT for iteration over STRING beginning
14558 with index START. */
14559 reseat_to_string (it, string, lisp_string, start,
14560 precision, field_width, multibyte);
14561
14562 /* If displaying STRING, set up the face of the iterator
14563 from LISP_STRING, if that's given. */
14564 if (STRINGP (face_string))
14565 {
14566 int endptr;
14567 struct face *face;
14568
14569 it->face_id
14570 = face_at_string_position (it->w, face_string, face_string_pos,
14571 0, it->region_beg_charpos,
14572 it->region_end_charpos,
14573 &endptr, it->base_face_id, 0);
14574 face = FACE_FROM_ID (it->f, it->face_id);
14575 it->face_box_p = face->box != FACE_NO_BOX;
14576 }
14577
14578 /* Set max_x to the maximum allowed X position. Don't let it go
14579 beyond the right edge of the window. */
14580 if (max_x <= 0)
14581 max_x = it->last_visible_x;
14582 else
14583 max_x = min (max_x, it->last_visible_x);
14584
14585 /* Skip over display elements that are not visible. because IT->w is
14586 hscrolled. */
14587 if (it->current_x < it->first_visible_x)
14588 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14589 MOVE_TO_POS | MOVE_TO_X);
14590
14591 row->ascent = it->max_ascent;
14592 row->height = it->max_ascent + it->max_descent;
14593 row->phys_ascent = it->max_phys_ascent;
14594 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14595
14596 /* This condition is for the case that we are called with current_x
14597 past last_visible_x. */
14598 while (it->current_x < max_x)
14599 {
14600 int x_before, x, n_glyphs_before, i, nglyphs;
14601
14602 /* Get the next display element. */
14603 if (!get_next_display_element (it))
14604 break;
14605
14606 /* Produce glyphs. */
14607 x_before = it->current_x;
14608 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14609 PRODUCE_GLYPHS (it);
14610
14611 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14612 i = 0;
14613 x = x_before;
14614 while (i < nglyphs)
14615 {
14616 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14617
14618 if (!it->truncate_lines_p
14619 && x + glyph->pixel_width > max_x)
14620 {
14621 /* End of continued line or max_x reached. */
14622 if (CHAR_GLYPH_PADDING_P (*glyph))
14623 {
14624 /* A wide character is unbreakable. */
14625 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14626 it->current_x = x_before;
14627 }
14628 else
14629 {
14630 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14631 it->current_x = x;
14632 }
14633 break;
14634 }
14635 else if (x + glyph->pixel_width > it->first_visible_x)
14636 {
14637 /* Glyph is at least partially visible. */
14638 ++it->hpos;
14639 if (x < it->first_visible_x)
14640 it->glyph_row->x = x - it->first_visible_x;
14641 }
14642 else
14643 {
14644 /* Glyph is off the left margin of the display area.
14645 Should not happen. */
14646 abort ();
14647 }
14648
14649 row->ascent = max (row->ascent, it->max_ascent);
14650 row->height = max (row->height, it->max_ascent + it->max_descent);
14651 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14652 row->phys_height = max (row->phys_height,
14653 it->max_phys_ascent + it->max_phys_descent);
14654 x += glyph->pixel_width;
14655 ++i;
14656 }
14657
14658 /* Stop if max_x reached. */
14659 if (i < nglyphs)
14660 break;
14661
14662 /* Stop at line ends. */
14663 if (ITERATOR_AT_END_OF_LINE_P (it))
14664 {
14665 it->continuation_lines_width = 0;
14666 break;
14667 }
14668
14669 set_iterator_to_next (it, 1);
14670
14671 /* Stop if truncating at the right edge. */
14672 if (it->truncate_lines_p
14673 && it->current_x >= it->last_visible_x)
14674 {
14675 /* Add truncation mark, but don't do it if the line is
14676 truncated at a padding space. */
14677 if (IT_CHARPOS (*it) < it->string_nchars)
14678 {
14679 if (!FRAME_WINDOW_P (it->f))
14680 {
14681 int i, n;
14682
14683 if (it->current_x > it->last_visible_x)
14684 {
14685 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14686 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14687 break;
14688 for (n = row->used[TEXT_AREA]; i < n; ++i)
14689 {
14690 row->used[TEXT_AREA] = i;
14691 produce_special_glyphs (it, IT_TRUNCATION);
14692 }
14693 }
14694 produce_special_glyphs (it, IT_TRUNCATION);
14695 }
14696 it->glyph_row->truncated_on_right_p = 1;
14697 }
14698 break;
14699 }
14700 }
14701
14702 /* Maybe insert a truncation at the left. */
14703 if (it->first_visible_x
14704 && IT_CHARPOS (*it) > 0)
14705 {
14706 if (!FRAME_WINDOW_P (it->f))
14707 insert_left_trunc_glyphs (it);
14708 it->glyph_row->truncated_on_left_p = 1;
14709 }
14710
14711 it->face_id = saved_face_id;
14712
14713 /* Value is number of columns displayed. */
14714 return it->hpos - hpos_at_start;
14715 }
14716
14717
14718 \f
14719 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
14720 appears as an element of LIST or as the car of an element of LIST.
14721 If PROPVAL is a list, compare each element against LIST in that
14722 way, and return 1/2 if any element of PROPVAL is found in LIST.
14723 Otherwise return 0. This function cannot quit.
14724 The return value is 2 if the text is invisible but with an ellipsis
14725 and 1 if it's invisible and without an ellipsis. */
14726
14727 int
14728 invisible_p (propval, list)
14729 register Lisp_Object propval;
14730 Lisp_Object list;
14731 {
14732 register Lisp_Object tail, proptail;
14733
14734 for (tail = list; CONSP (tail); tail = XCDR (tail))
14735 {
14736 register Lisp_Object tem;
14737 tem = XCAR (tail);
14738 if (EQ (propval, tem))
14739 return 1;
14740 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14741 return NILP (XCDR (tem)) ? 1 : 2;
14742 }
14743
14744 if (CONSP (propval))
14745 {
14746 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14747 {
14748 Lisp_Object propelt;
14749 propelt = XCAR (proptail);
14750 for (tail = list; CONSP (tail); tail = XCDR (tail))
14751 {
14752 register Lisp_Object tem;
14753 tem = XCAR (tail);
14754 if (EQ (propelt, tem))
14755 return 1;
14756 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14757 return NILP (XCDR (tem)) ? 1 : 2;
14758 }
14759 }
14760 }
14761
14762 return 0;
14763 }
14764
14765 \f
14766 /***********************************************************************
14767 Initialization
14768 ***********************************************************************/
14769
14770 void
14771 syms_of_xdisp ()
14772 {
14773 Vwith_echo_area_save_vector = Qnil;
14774 staticpro (&Vwith_echo_area_save_vector);
14775
14776 Vmessage_stack = Qnil;
14777 staticpro (&Vmessage_stack);
14778
14779 Qinhibit_redisplay = intern ("inhibit-redisplay");
14780 staticpro (&Qinhibit_redisplay);
14781
14782 message_dolog_marker1 = Fmake_marker ();
14783 staticpro (&message_dolog_marker1);
14784 message_dolog_marker2 = Fmake_marker ();
14785 staticpro (&message_dolog_marker2);
14786 message_dolog_marker3 = Fmake_marker ();
14787 staticpro (&message_dolog_marker3);
14788
14789 #if GLYPH_DEBUG
14790 defsubr (&Sdump_glyph_matrix);
14791 defsubr (&Sdump_glyph_row);
14792 defsubr (&Sdump_tool_bar_row);
14793 defsubr (&Strace_redisplay);
14794 defsubr (&Strace_to_stderr);
14795 #endif
14796 #ifdef HAVE_WINDOW_SYSTEM
14797 defsubr (&Stool_bar_lines_needed);
14798 #endif
14799
14800 staticpro (&Qmenu_bar_update_hook);
14801 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14802
14803 staticpro (&Qoverriding_terminal_local_map);
14804 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14805
14806 staticpro (&Qoverriding_local_map);
14807 Qoverriding_local_map = intern ("overriding-local-map");
14808
14809 staticpro (&Qwindow_scroll_functions);
14810 Qwindow_scroll_functions = intern ("window-scroll-functions");
14811
14812 staticpro (&Qredisplay_end_trigger_functions);
14813 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14814
14815 staticpro (&Qinhibit_point_motion_hooks);
14816 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14817
14818 QCdata = intern (":data");
14819 staticpro (&QCdata);
14820 Qdisplay = intern ("display");
14821 staticpro (&Qdisplay);
14822 Qspace_width = intern ("space-width");
14823 staticpro (&Qspace_width);
14824 Qraise = intern ("raise");
14825 staticpro (&Qraise);
14826 Qspace = intern ("space");
14827 staticpro (&Qspace);
14828 Qmargin = intern ("margin");
14829 staticpro (&Qmargin);
14830 Qleft_margin = intern ("left-margin");
14831 staticpro (&Qleft_margin);
14832 Qright_margin = intern ("right-margin");
14833 staticpro (&Qright_margin);
14834 Qalign_to = intern ("align-to");
14835 staticpro (&Qalign_to);
14836 QCalign_to = intern (":align-to");
14837 staticpro (&QCalign_to);
14838 Qrelative_width = intern ("relative-width");
14839 staticpro (&Qrelative_width);
14840 QCrelative_width = intern (":relative-width");
14841 staticpro (&QCrelative_width);
14842 QCrelative_height = intern (":relative-height");
14843 staticpro (&QCrelative_height);
14844 QCeval = intern (":eval");
14845 staticpro (&QCeval);
14846 QCpropertize = intern (":propertize");
14847 staticpro (&QCpropertize);
14848 Qwhen = intern ("when");
14849 staticpro (&Qwhen);
14850 QCfile = intern (":file");
14851 staticpro (&QCfile);
14852 Qfontified = intern ("fontified");
14853 staticpro (&Qfontified);
14854 Qfontification_functions = intern ("fontification-functions");
14855 staticpro (&Qfontification_functions);
14856 Qtrailing_whitespace = intern ("trailing-whitespace");
14857 staticpro (&Qtrailing_whitespace);
14858 Qimage = intern ("image");
14859 staticpro (&Qimage);
14860 Qmessage_truncate_lines = intern ("message-truncate-lines");
14861 staticpro (&Qmessage_truncate_lines);
14862 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
14863 staticpro (&Qcursor_in_non_selected_windows);
14864 Qgrow_only = intern ("grow-only");
14865 staticpro (&Qgrow_only);
14866 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14867 staticpro (&Qinhibit_menubar_update);
14868 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14869 staticpro (&Qinhibit_eval_during_redisplay);
14870 Qposition = intern ("position");
14871 staticpro (&Qposition);
14872 Qbuffer_position = intern ("buffer-position");
14873 staticpro (&Qbuffer_position);
14874 Qobject = intern ("object");
14875 staticpro (&Qobject);
14876
14877 last_arrow_position = Qnil;
14878 last_arrow_string = Qnil;
14879 staticpro (&last_arrow_position);
14880 staticpro (&last_arrow_string);
14881
14882 echo_buffer[0] = echo_buffer[1] = Qnil;
14883 staticpro (&echo_buffer[0]);
14884 staticpro (&echo_buffer[1]);
14885
14886 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14887 staticpro (&echo_area_buffer[0]);
14888 staticpro (&echo_area_buffer[1]);
14889
14890 Vmessages_buffer_name = build_string ("*Messages*");
14891 staticpro (&Vmessages_buffer_name);
14892
14893 mode_line_proptrans_alist = Qnil;
14894 staticpro (&mode_line_proptrans_alist);
14895
14896 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14897 doc: /* Non-nil means highlight trailing whitespace.
14898 The face used for trailing whitespace is `trailing-whitespace'. */);
14899 Vshow_trailing_whitespace = Qnil;
14900
14901 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14902 doc: /* Non-nil means don't actually do any redisplay.
14903 This is used for internal purposes. */);
14904 Vinhibit_redisplay = Qnil;
14905
14906 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14907 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
14908 Vglobal_mode_string = Qnil;
14909
14910 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14911 doc: /* Marker for where to display an arrow on top of the buffer text.
14912 This must be the beginning of a line in order to work.
14913 See also `overlay-arrow-string'. */);
14914 Voverlay_arrow_position = Qnil;
14915
14916 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14917 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
14918 Voverlay_arrow_string = Qnil;
14919
14920 DEFVAR_INT ("scroll-step", &scroll_step,
14921 doc: /* *The number of lines to try scrolling a window by when point moves out.
14922 If that fails to bring point back on frame, point is centered instead.
14923 If this is zero, point is always centered after it moves off frame.
14924 If you want scrolling to always be a line at a time, you should set
14925 `scroll-conservatively' to a large value rather than set this to 1. */);
14926
14927 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14928 doc: /* *Scroll up to this many lines, to bring point back on screen.
14929 A value of zero means to scroll the text to center point vertically
14930 in the window. */);
14931 scroll_conservatively = 0;
14932
14933 DEFVAR_INT ("scroll-margin", &scroll_margin,
14934 doc: /* *Number of lines of margin at the top and bottom of a window.
14935 Recenter the window whenever point gets within this many lines
14936 of the top or bottom of the window. */);
14937 scroll_margin = 0;
14938
14939 #if GLYPH_DEBUG
14940 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
14941 #endif
14942
14943 DEFVAR_BOOL ("truncate-partial-width-windows",
14944 &truncate_partial_width_windows,
14945 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
14946 truncate_partial_width_windows = 1;
14947
14948 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14949 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
14950 Any other value means to use the appropriate face, `mode-line',
14951 `header-line', or `menu' respectively.
14952
14953 This variable is deprecated; please change the above faces instead. */);
14954 mode_line_inverse_video = 1;
14955
14956 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14957 doc: /* *Maximum buffer size for which line number should be displayed.
14958 If the buffer is bigger than this, the line number does not appear
14959 in the mode line. A value of nil means no limit. */);
14960 Vline_number_display_limit = Qnil;
14961
14962 DEFVAR_INT ("line-number-display-limit-width",
14963 &line_number_display_limit_width,
14964 doc: /* *Maximum line width (in characters) for line number display.
14965 If the average length of the lines near point is bigger than this, then the
14966 line number may be omitted from the mode line. */);
14967 line_number_display_limit_width = 200;
14968
14969 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14970 doc: /* *Non-nil means highlight region even in nonselected windows. */);
14971 highlight_nonselected_windows = 0;
14972
14973 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14974 doc: /* Non-nil if more than one frame is visible on this display.
14975 Minibuffer-only frames don't count, but iconified frames do.
14976 This variable is not guaranteed to be accurate except while processing
14977 `frame-title-format' and `icon-title-format'. */);
14978
14979 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14980 doc: /* Template for displaying the title bar of visible frames.
14981 \(Assuming the window manager supports this feature.)
14982 This variable has the same structure as `mode-line-format' (which see),
14983 and is used only on frames for which no explicit name has been set
14984 \(see `modify-frame-parameters'). */);
14985 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
14986 doc: /* Template for displaying the title bar of an iconified frame.
14987 \(Assuming the window manager supports this feature.)
14988 This variable has the same structure as `mode-line-format' (which see),
14989 and is used only on frames for which no explicit name has been set
14990 \(see `modify-frame-parameters'). */);
14991 Vicon_title_format
14992 = Vframe_title_format
14993 = Fcons (intern ("multiple-frames"),
14994 Fcons (build_string ("%b"),
14995 Fcons (Fcons (empty_string,
14996 Fcons (intern ("invocation-name"),
14997 Fcons (build_string ("@"),
14998 Fcons (intern ("system-name"),
14999 Qnil)))),
15000 Qnil)));
15001
15002 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
15003 doc: /* Maximum number of lines to keep in the message log buffer.
15004 If nil, disable message logging. If t, log messages but don't truncate
15005 the buffer when it becomes large. */);
15006 Vmessage_log_max = make_number (50);
15007
15008 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
15009 doc: /* Functions called before redisplay, if window sizes have changed.
15010 The value should be a list of functions that take one argument.
15011 Just before redisplay, for each frame, if any of its windows have changed
15012 size since the last redisplay, or have been split or deleted,
15013 all the functions in the list are called, with the frame as argument. */);
15014 Vwindow_size_change_functions = Qnil;
15015
15016 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
15017 doc: /* List of Functions to call before redisplaying a window with scrolling.
15018 Each function is called with two arguments, the window
15019 and its new display-start position. Note that the value of `window-end'
15020 is not valid when these functions are called. */);
15021 Vwindow_scroll_functions = Qnil;
15022
15023 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
15024 doc: /* *Non-nil means automatically resize tool-bars.
15025 This increases a tool-bar's height if not all tool-bar items are visible.
15026 It decreases a tool-bar's height when it would display blank lines
15027 otherwise. */);
15028 auto_resize_tool_bars_p = 1;
15029
15030 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
15031 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
15032 auto_raise_tool_bar_buttons_p = 1;
15033
15034 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
15035 doc: /* *Margin around tool-bar buttons in pixels.
15036 If an integer, use that for both horizontal and vertical margins.
15037 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
15038 HORZ specifying the horizontal margin, and VERT specifying the
15039 vertical margin. */);
15040 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
15041
15042 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
15043 doc: /* *Relief thickness of tool-bar buttons. */);
15044 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
15045
15046 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
15047 doc: /* List of functions to call to fontify regions of text.
15048 Each function is called with one argument POS. Functions must
15049 fontify a region starting at POS in the current buffer, and give
15050 fontified regions the property `fontified'. */);
15051 Vfontification_functions = Qnil;
15052 Fmake_variable_buffer_local (Qfontification_functions);
15053
15054 DEFVAR_BOOL ("unibyte-display-via-language-environment",
15055 &unibyte_display_via_language_environment,
15056 doc: /* *Non-nil means display unibyte text according to language environment.
15057 Specifically this means that unibyte non-ASCII characters
15058 are displayed by converting them to the equivalent multibyte characters
15059 according to the current language environment. As a result, they are
15060 displayed according to the current fontset. */);
15061 unibyte_display_via_language_environment = 0;
15062
15063 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
15064 doc: /* *Maximum height for resizing mini-windows.
15065 If a float, it specifies a fraction of the mini-window frame's height.
15066 If an integer, it specifies a number of lines. */);
15067 Vmax_mini_window_height = make_float (0.25);
15068
15069 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
15070 doc: /* *How to resize mini-windows.
15071 A value of nil means don't automatically resize mini-windows.
15072 A value of t means resize them to fit the text displayed in them.
15073 A value of `grow-only', the default, means let mini-windows grow
15074 only, until their display becomes empty, at which point the windows
15075 go back to their normal size. */);
15076 Vresize_mini_windows = Qgrow_only;
15077
15078 DEFVAR_BOOL ("cursor-in-non-selected-windows",
15079 &cursor_in_non_selected_windows,
15080 doc: /* *Non-nil means display a hollow cursor in non-selected windows.
15081 nil means don't display a cursor there. */);
15082 cursor_in_non_selected_windows = 1;
15083
15084 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
15085 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
15086 automatic_hscrolling_p = 1;
15087
15088 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
15089 doc: /* *How many columns away from the window edge point is allowed to get
15090 before automatic hscrolling will horizontally scroll the window. */);
15091 hscroll_margin = 5;
15092
15093 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
15094 doc: /* *How many columns to scroll the window when point gets too close to the edge.
15095 When point is less than `automatic-hscroll-margin' columns from the window
15096 edge, automatic hscrolling will scroll the window by the amount of columns
15097 determined by this variable. If its value is a positive integer, scroll that
15098 many columns. If it's a positive floating-point number, it specifies the
15099 fraction of the window's width to scroll. If it's nil or zero, point will be
15100 centered horizontally after the scroll. Any other value, including negative
15101 numbers, are treated as if the value were zero.
15102
15103 Automatic hscrolling always moves point outside the scroll margin, so if
15104 point was more than scroll step columns inside the margin, the window will
15105 scroll more than the value given by the scroll step.
15106
15107 Note that the lower bound for automatic hscrolling specified by `scroll-left'
15108 and `scroll-right' overrides this variable's effect. */);
15109 Vhscroll_step = make_number (0);
15110
15111 DEFVAR_LISP ("image-types", &Vimage_types,
15112 doc: /* List of supported image types.
15113 Each element of the list is a symbol for a supported image type. */);
15114 Vimage_types = Qnil;
15115
15116 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
15117 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
15118 Bind this around calls to `message' to let it take effect. */);
15119 message_truncate_lines = 0;
15120
15121 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
15122 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
15123 Can be used to update submenus whose contents should vary. */);
15124 Vmenu_bar_update_hook = Qnil;
15125
15126 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
15127 doc: /* Non-nil means don't update menu bars. Internal use only. */);
15128 inhibit_menubar_update = 0;
15129
15130 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
15131 doc: /* Non-nil means don't eval Lisp during redisplay. */);
15132 inhibit_eval_during_redisplay = 0;
15133
15134 #if GLYPH_DEBUG
15135 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
15136 doc: /* Inhibit try_window_id display optimization. */);
15137 inhibit_try_window_id = 0;
15138
15139 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
15140 doc: /* Inhibit try_window_reusing display optimization. */);
15141 inhibit_try_window_reusing = 0;
15142
15143 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
15144 doc: /* Inhibit try_cursor_movement display optimization. */);
15145 inhibit_try_cursor_movement = 0;
15146 #endif /* GLYPH_DEBUG */
15147 }
15148
15149
15150 /* Initialize this module when Emacs starts. */
15151
15152 void
15153 init_xdisp ()
15154 {
15155 Lisp_Object root_window;
15156 struct window *mini_w;
15157
15158 current_header_line_height = current_mode_line_height = -1;
15159
15160 CHARPOS (this_line_start_pos) = 0;
15161
15162 mini_w = XWINDOW (minibuf_window);
15163 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
15164
15165 if (!noninteractive)
15166 {
15167 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
15168 int i;
15169
15170 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
15171 set_window_height (root_window,
15172 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
15173 0);
15174 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
15175 set_window_height (minibuf_window, 1, 0);
15176
15177 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
15178 mini_w->width = make_number (FRAME_WIDTH (f));
15179
15180 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
15181 scratch_glyph_row.glyphs[TEXT_AREA + 1]
15182 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
15183
15184 /* The default ellipsis glyphs `...'. */
15185 for (i = 0; i < 3; ++i)
15186 default_invis_vector[i] = make_number ('.');
15187 }
15188
15189 #ifdef HAVE_WINDOW_SYSTEM
15190 {
15191 /* Allocate the buffer for frame titles. */
15192 int size = 100;
15193 frame_title_buf = (char *) xmalloc (size);
15194 frame_title_buf_end = frame_title_buf + size;
15195 frame_title_ptr = NULL;
15196 }
15197 #endif /* HAVE_WINDOW_SYSTEM */
15198
15199 help_echo_showing_p = 0;
15200 }
15201
15202