]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(display_mode_lines): Fix last change.
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the 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;
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 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 int scroll_step;
387
388 /* Nonzero means scroll just far enough to bring point back on the
389 screen, when appropriate. */
390
391 static 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 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 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 /* A list of symbols, one for each supported image type. */
586
587 Lisp_Object Vimage_types;
588
589 /* The variable `resize-mini-windows'. If nil, don't resize
590 mini-windows. If t, always resize them to fit the text they
591 display. If `grow-only', let mini-windows grow only until they
592 become empty. */
593
594 Lisp_Object Vresize_mini_windows;
595
596 /* Buffer being redisplayed -- for redisplay_window_error. */
597
598 struct buffer *displayed_buffer;
599
600 /* Value returned from text property handlers (see below). */
601
602 enum prop_handled
603 {
604 HANDLED_NORMALLY,
605 HANDLED_RECOMPUTE_PROPS,
606 HANDLED_OVERLAY_STRING_CONSUMED,
607 HANDLED_RETURN
608 };
609
610 /* A description of text properties that redisplay is interested
611 in. */
612
613 struct props
614 {
615 /* The name of the property. */
616 Lisp_Object *name;
617
618 /* A unique index for the property. */
619 enum prop_idx idx;
620
621 /* A handler function called to set up iterator IT from the property
622 at IT's current position. Value is used to steer handle_stop. */
623 enum prop_handled (*handler) P_ ((struct it *it));
624 };
625
626 static enum prop_handled handle_face_prop P_ ((struct it *));
627 static enum prop_handled handle_invisible_prop P_ ((struct it *));
628 static enum prop_handled handle_display_prop P_ ((struct it *));
629 static enum prop_handled handle_composition_prop P_ ((struct it *));
630 static enum prop_handled handle_overlay_change P_ ((struct it *));
631 static enum prop_handled handle_fontified_prop P_ ((struct it *));
632
633 /* Properties handled by iterators. */
634
635 static struct props it_props[] =
636 {
637 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
638 /* Handle `face' before `display' because some sub-properties of
639 `display' need to know the face. */
640 {&Qface, FACE_PROP_IDX, handle_face_prop},
641 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
642 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
643 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
644 {NULL, 0, NULL}
645 };
646
647 /* Value is the position described by X. If X is a marker, value is
648 the marker_position of X. Otherwise, value is X. */
649
650 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
651
652 /* Enumeration returned by some move_it_.* functions internally. */
653
654 enum move_it_result
655 {
656 /* Not used. Undefined value. */
657 MOVE_UNDEFINED,
658
659 /* Move ended at the requested buffer position or ZV. */
660 MOVE_POS_MATCH_OR_ZV,
661
662 /* Move ended at the requested X pixel position. */
663 MOVE_X_REACHED,
664
665 /* Move within a line ended at the end of a line that must be
666 continued. */
667 MOVE_LINE_CONTINUED,
668
669 /* Move within a line ended at the end of a line that would
670 be displayed truncated. */
671 MOVE_LINE_TRUNCATED,
672
673 /* Move within a line ended at a line end. */
674 MOVE_NEWLINE_OR_CR
675 };
676
677
678 \f
679 /* Function prototypes. */
680
681 static void setup_for_ellipsis P_ ((struct it *));
682 static void mark_window_display_accurate_1 P_ ((struct window *, int));
683 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
684 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
685 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
686 static int redisplay_mode_lines P_ ((Lisp_Object, int));
687 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
688
689 #if 0
690 static int invisible_text_between_p P_ ((struct it *, int, int));
691 #endif
692
693 static int next_element_from_ellipsis P_ ((struct it *));
694 static void pint2str P_ ((char *, int, int));
695 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
696 struct text_pos));
697 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
698 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
699 static void store_frame_title_char P_ ((char));
700 static int store_frame_title P_ ((unsigned char *, int, int));
701 static void x_consider_frame_title P_ ((Lisp_Object));
702 static void handle_stop P_ ((struct it *));
703 static int tool_bar_lines_needed P_ ((struct frame *));
704 static int single_display_prop_intangible_p P_ ((Lisp_Object));
705 static void ensure_echo_area_buffers P_ ((void));
706 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
707 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
708 static int with_echo_area_buffer P_ ((struct window *, int,
709 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
710 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
711 static void clear_garbaged_frames P_ ((void));
712 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
713 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
714 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
715 static int display_echo_area P_ ((struct window *));
716 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
717 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
718 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
719 static int string_char_and_length P_ ((unsigned char *, int, int *));
720 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
721 struct text_pos));
722 static int compute_window_start_on_continuation_line P_ ((struct window *));
723 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
724 static void insert_left_trunc_glyphs P_ ((struct it *));
725 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
726 static void extend_face_to_end_of_line P_ ((struct it *));
727 static int append_space P_ ((struct it *, int));
728 static int make_cursor_line_fully_visible P_ ((struct window *));
729 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
730 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
731 static int trailing_whitespace_p P_ ((int));
732 static int message_log_check_duplicate P_ ((int, int, int, int));
733 static void push_it P_ ((struct it *));
734 static void pop_it P_ ((struct it *));
735 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
736 static void redisplay_internal P_ ((int));
737 static int echo_area_display P_ ((int));
738 static void redisplay_windows P_ ((Lisp_Object));
739 static void redisplay_window P_ ((Lisp_Object, int));
740 static Lisp_Object redisplay_window_error ();
741 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
742 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
743 static void update_menu_bar P_ ((struct frame *, int));
744 static int try_window_reusing_current_matrix P_ ((struct window *));
745 static int try_window_id P_ ((struct window *));
746 static int display_line P_ ((struct it *));
747 static int display_mode_lines P_ ((struct window *));
748 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
749 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
750 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
751 static void display_menu_bar P_ ((struct window *));
752 static int display_count_lines P_ ((int, int, int, int, int *));
753 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
754 int, int, struct it *, int, int, int, int));
755 static void compute_line_metrics P_ ((struct it *));
756 static void run_redisplay_end_trigger_hook P_ ((struct it *));
757 static int get_overlay_strings P_ ((struct it *, int));
758 static void next_overlay_string P_ ((struct it *));
759 static void reseat P_ ((struct it *, struct text_pos, int));
760 static void reseat_1 P_ ((struct it *, struct text_pos, int));
761 static void back_to_previous_visible_line_start P_ ((struct it *));
762 static void reseat_at_previous_visible_line_start P_ ((struct it *));
763 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
764 static int next_element_from_display_vector P_ ((struct it *));
765 static int next_element_from_string P_ ((struct it *));
766 static int next_element_from_c_string P_ ((struct it *));
767 static int next_element_from_buffer P_ ((struct it *));
768 static int next_element_from_composition P_ ((struct it *));
769 static int next_element_from_image P_ ((struct it *));
770 static int next_element_from_stretch P_ ((struct it *));
771 static void load_overlay_strings P_ ((struct it *, int));
772 static int init_from_display_pos P_ ((struct it *, struct window *,
773 struct display_pos *));
774 static void reseat_to_string P_ ((struct it *, unsigned char *,
775 Lisp_Object, int, int, int, int));
776 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
777 int, int, int));
778 void move_it_vertically_backward P_ ((struct it *, int));
779 static void init_to_row_start P_ ((struct it *, struct window *,
780 struct glyph_row *));
781 static int init_to_row_end P_ ((struct it *, struct window *,
782 struct glyph_row *));
783 static void back_to_previous_line_start P_ ((struct it *));
784 static int forward_to_next_line_start P_ ((struct it *, int *));
785 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
786 Lisp_Object, int));
787 static struct text_pos string_pos P_ ((int, Lisp_Object));
788 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
789 static int number_of_chars P_ ((unsigned char *, int));
790 static void compute_stop_pos P_ ((struct it *));
791 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
792 Lisp_Object));
793 static int face_before_or_after_it_pos P_ ((struct it *, int));
794 static int next_overlay_change P_ ((int));
795 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
796 Lisp_Object, struct text_pos *,
797 int));
798 static int underlying_face_id P_ ((struct it *));
799 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
800 struct window *));
801
802 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
803 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
804
805 #ifdef HAVE_WINDOW_SYSTEM
806
807 static void update_tool_bar P_ ((struct frame *, int));
808 static void build_desired_tool_bar_string P_ ((struct frame *f));
809 static int redisplay_tool_bar P_ ((struct frame *));
810 static void display_tool_bar_line P_ ((struct it *));
811
812 #endif /* HAVE_WINDOW_SYSTEM */
813
814 \f
815 /***********************************************************************
816 Window display dimensions
817 ***********************************************************************/
818
819 /* Return the window-relative maximum y + 1 for glyph rows displaying
820 text in window W. This is the height of W minus the height of a
821 mode line, if any. */
822
823 INLINE int
824 window_text_bottom_y (w)
825 struct window *w;
826 {
827 struct frame *f = XFRAME (w->frame);
828 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
829
830 if (WINDOW_WANTS_MODELINE_P (w))
831 height -= CURRENT_MODE_LINE_HEIGHT (w);
832 return height;
833 }
834
835
836 /* Return the pixel width of display area AREA of window W. AREA < 0
837 means return the total width of W, not including fringes to
838 the left and right of the window. */
839
840 INLINE int
841 window_box_width (w, area)
842 struct window *w;
843 int area;
844 {
845 struct frame *f = XFRAME (w->frame);
846 int width = XFASTINT (w->width);
847
848 if (!w->pseudo_window_p)
849 {
850 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
851
852 if (area == TEXT_AREA)
853 {
854 if (INTEGERP (w->left_margin_width))
855 width -= XFASTINT (w->left_margin_width);
856 if (INTEGERP (w->right_margin_width))
857 width -= XFASTINT (w->right_margin_width);
858 }
859 else if (area == LEFT_MARGIN_AREA)
860 width = (INTEGERP (w->left_margin_width)
861 ? XFASTINT (w->left_margin_width) : 0);
862 else if (area == RIGHT_MARGIN_AREA)
863 width = (INTEGERP (w->right_margin_width)
864 ? XFASTINT (w->right_margin_width) : 0);
865 }
866
867 return width * CANON_X_UNIT (f);
868 }
869
870
871 /* Return the pixel height of the display area of window W, not
872 including mode lines of W, if any. */
873
874 INLINE int
875 window_box_height (w)
876 struct window *w;
877 {
878 struct frame *f = XFRAME (w->frame);
879 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
880
881 xassert (height >= 0);
882
883 /* Note: the code below that determines the mode-line/header-line
884 height is essentially the same as that contained in the macro
885 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
886 the appropriate glyph row has its `mode_line_p' flag set,
887 and if it doesn't, uses estimate_mode_line_height instead. */
888
889 if (WINDOW_WANTS_MODELINE_P (w))
890 {
891 struct glyph_row *ml_row
892 = (w->current_matrix && w->current_matrix->rows
893 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
894 : 0);
895 if (ml_row && ml_row->mode_line_p)
896 height -= ml_row->height;
897 else
898 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
899 }
900
901 if (WINDOW_WANTS_HEADER_LINE_P (w))
902 {
903 struct glyph_row *hl_row
904 = (w->current_matrix && w->current_matrix->rows
905 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
906 : 0);
907 if (hl_row && hl_row->mode_line_p)
908 height -= hl_row->height;
909 else
910 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
911 }
912
913 /* With a very small font and a mode-line that's taller than
914 default, we might end up with a negative height. */
915 return max (0, height);
916 }
917
918
919 /* Return the frame-relative coordinate of the left edge of display
920 area AREA of window W. AREA < 0 means return the left edge of the
921 whole window, to the right of the left fringe of W. */
922
923 INLINE int
924 window_box_left (w, area)
925 struct window *w;
926 int area;
927 {
928 struct frame *f = XFRAME (w->frame);
929 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
930
931 if (!w->pseudo_window_p)
932 {
933 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
934 + FRAME_LEFT_FRINGE_WIDTH (f));
935
936 if (area == TEXT_AREA)
937 x += window_box_width (w, LEFT_MARGIN_AREA);
938 else if (area == RIGHT_MARGIN_AREA)
939 x += (window_box_width (w, LEFT_MARGIN_AREA)
940 + window_box_width (w, TEXT_AREA));
941 }
942
943 return x;
944 }
945
946
947 /* Return the frame-relative coordinate of the right edge of display
948 area AREA of window W. AREA < 0 means return the left edge of the
949 whole window, to the left of the right fringe of W. */
950
951 INLINE int
952 window_box_right (w, area)
953 struct window *w;
954 int area;
955 {
956 return window_box_left (w, area) + window_box_width (w, area);
957 }
958
959
960 /* Get the bounding box of the display area AREA of window W, without
961 mode lines, in frame-relative coordinates. AREA < 0 means the
962 whole window, not including the left and right fringes of
963 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
964 coordinates of the upper-left corner of the box. Return in
965 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
966
967 INLINE void
968 window_box (w, area, box_x, box_y, box_width, box_height)
969 struct window *w;
970 int area;
971 int *box_x, *box_y, *box_width, *box_height;
972 {
973 struct frame *f = XFRAME (w->frame);
974
975 *box_width = window_box_width (w, area);
976 *box_height = window_box_height (w);
977 *box_x = window_box_left (w, area);
978 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
979 + XFASTINT (w->top) * CANON_Y_UNIT (f));
980 if (WINDOW_WANTS_HEADER_LINE_P (w))
981 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
982 }
983
984
985 /* Get the bounding box of the display area AREA of window W, without
986 mode lines. AREA < 0 means the whole window, not including the
987 left and right fringe of the window. Return in *TOP_LEFT_X
988 and TOP_LEFT_Y the frame-relative pixel coordinates of the
989 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
990 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
991 box. */
992
993 INLINE void
994 window_box_edges (w, area, top_left_x, top_left_y,
995 bottom_right_x, bottom_right_y)
996 struct window *w;
997 int area;
998 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
999 {
1000 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1001 bottom_right_y);
1002 *bottom_right_x += *top_left_x;
1003 *bottom_right_y += *top_left_y;
1004 }
1005
1006
1007 \f
1008 /***********************************************************************
1009 Utilities
1010 ***********************************************************************/
1011
1012 /* Return the bottom y-position of the line the iterator IT is in.
1013 This can modify IT's settings. */
1014
1015 int
1016 line_bottom_y (it)
1017 struct it *it;
1018 {
1019 int line_height = it->max_ascent + it->max_descent;
1020 int line_top_y = it->current_y;
1021
1022 if (line_height == 0)
1023 {
1024 if (last_height)
1025 line_height = last_height;
1026 else if (IT_CHARPOS (*it) < ZV)
1027 {
1028 move_it_by_lines (it, 1, 1);
1029 line_height = (it->max_ascent || it->max_descent
1030 ? it->max_ascent + it->max_descent
1031 : last_height);
1032 }
1033 else
1034 {
1035 struct glyph_row *row = it->glyph_row;
1036
1037 /* Use the default character height. */
1038 it->glyph_row = NULL;
1039 it->what = IT_CHARACTER;
1040 it->c = ' ';
1041 it->len = 1;
1042 PRODUCE_GLYPHS (it);
1043 line_height = it->ascent + it->descent;
1044 it->glyph_row = row;
1045 }
1046 }
1047
1048 return line_top_y + line_height;
1049 }
1050
1051
1052 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1053 1 if POS is visible and the line containing POS is fully visible.
1054 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1055 and header-lines heights. */
1056
1057 int
1058 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1059 struct window *w;
1060 int charpos, *fully, exact_mode_line_heights_p;
1061 {
1062 struct it it;
1063 struct text_pos top;
1064 int visible_p;
1065 struct buffer *old_buffer = NULL;
1066
1067 if (XBUFFER (w->buffer) != current_buffer)
1068 {
1069 old_buffer = current_buffer;
1070 set_buffer_internal_1 (XBUFFER (w->buffer));
1071 }
1072
1073 *fully = visible_p = 0;
1074 SET_TEXT_POS_FROM_MARKER (top, w->start);
1075
1076 /* Compute exact mode line heights, if requested. */
1077 if (exact_mode_line_heights_p)
1078 {
1079 if (WINDOW_WANTS_MODELINE_P (w))
1080 current_mode_line_height
1081 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1082 current_buffer->mode_line_format);
1083
1084 if (WINDOW_WANTS_HEADER_LINE_P (w))
1085 current_header_line_height
1086 = display_mode_line (w, HEADER_LINE_FACE_ID,
1087 current_buffer->header_line_format);
1088 }
1089
1090 start_display (&it, w, top);
1091 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1092 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1093
1094 /* Note that we may overshoot because of invisible text. */
1095 if (IT_CHARPOS (it) >= charpos)
1096 {
1097 int top_y = it.current_y;
1098 int bottom_y = line_bottom_y (&it);
1099 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1100
1101 if (top_y < window_top_y)
1102 visible_p = bottom_y > window_top_y;
1103 else if (top_y < it.last_visible_y)
1104 {
1105 visible_p = 1;
1106 *fully = bottom_y <= it.last_visible_y;
1107 }
1108 }
1109 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1110 {
1111 move_it_by_lines (&it, 1, 0);
1112 if (charpos < IT_CHARPOS (it))
1113 {
1114 visible_p = 1;
1115 *fully = 0;
1116 }
1117 }
1118
1119 if (old_buffer)
1120 set_buffer_internal_1 (old_buffer);
1121
1122 current_header_line_height = current_mode_line_height = -1;
1123 return visible_p;
1124 }
1125
1126
1127 /* Return the next character from STR which is MAXLEN bytes long.
1128 Return in *LEN the length of the character. This is like
1129 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1130 we find one, we return a `?', but with the length of the invalid
1131 character. */
1132
1133 static INLINE int
1134 string_char_and_length (str, maxlen, len)
1135 unsigned char *str;
1136 int maxlen, *len;
1137 {
1138 int c;
1139
1140 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1141 if (!CHAR_VALID_P (c, 1))
1142 /* We may not change the length here because other places in Emacs
1143 don't use this function, i.e. they silently accept invalid
1144 characters. */
1145 c = '?';
1146
1147 return c;
1148 }
1149
1150
1151
1152 /* Given a position POS containing a valid character and byte position
1153 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1154
1155 static struct text_pos
1156 string_pos_nchars_ahead (pos, string, nchars)
1157 struct text_pos pos;
1158 Lisp_Object string;
1159 int nchars;
1160 {
1161 xassert (STRINGP (string) && nchars >= 0);
1162
1163 if (STRING_MULTIBYTE (string))
1164 {
1165 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1166 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1167 int len;
1168
1169 while (nchars--)
1170 {
1171 string_char_and_length (p, rest, &len);
1172 p += len, rest -= len;
1173 xassert (rest >= 0);
1174 CHARPOS (pos) += 1;
1175 BYTEPOS (pos) += len;
1176 }
1177 }
1178 else
1179 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1180
1181 return pos;
1182 }
1183
1184
1185 /* Value is the text position, i.e. character and byte position,
1186 for character position CHARPOS in STRING. */
1187
1188 static INLINE struct text_pos
1189 string_pos (charpos, string)
1190 int charpos;
1191 Lisp_Object string;
1192 {
1193 struct text_pos pos;
1194 xassert (STRINGP (string));
1195 xassert (charpos >= 0);
1196 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1197 return pos;
1198 }
1199
1200
1201 /* Value is a text position, i.e. character and byte position, for
1202 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1203 means recognize multibyte characters. */
1204
1205 static struct text_pos
1206 c_string_pos (charpos, s, multibyte_p)
1207 int charpos;
1208 unsigned char *s;
1209 int multibyte_p;
1210 {
1211 struct text_pos pos;
1212
1213 xassert (s != NULL);
1214 xassert (charpos >= 0);
1215
1216 if (multibyte_p)
1217 {
1218 int rest = strlen (s), len;
1219
1220 SET_TEXT_POS (pos, 0, 0);
1221 while (charpos--)
1222 {
1223 string_char_and_length (s, rest, &len);
1224 s += len, rest -= len;
1225 xassert (rest >= 0);
1226 CHARPOS (pos) += 1;
1227 BYTEPOS (pos) += len;
1228 }
1229 }
1230 else
1231 SET_TEXT_POS (pos, charpos, charpos);
1232
1233 return pos;
1234 }
1235
1236
1237 /* Value is the number of characters in C string S. MULTIBYTE_P
1238 non-zero means recognize multibyte characters. */
1239
1240 static int
1241 number_of_chars (s, multibyte_p)
1242 unsigned char *s;
1243 int multibyte_p;
1244 {
1245 int nchars;
1246
1247 if (multibyte_p)
1248 {
1249 int rest = strlen (s), len;
1250 unsigned char *p = (unsigned char *) s;
1251
1252 for (nchars = 0; rest > 0; ++nchars)
1253 {
1254 string_char_and_length (p, rest, &len);
1255 rest -= len, p += len;
1256 }
1257 }
1258 else
1259 nchars = strlen (s);
1260
1261 return nchars;
1262 }
1263
1264
1265 /* Compute byte position NEWPOS->bytepos corresponding to
1266 NEWPOS->charpos. POS is a known position in string STRING.
1267 NEWPOS->charpos must be >= POS.charpos. */
1268
1269 static void
1270 compute_string_pos (newpos, pos, string)
1271 struct text_pos *newpos, pos;
1272 Lisp_Object string;
1273 {
1274 xassert (STRINGP (string));
1275 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1276
1277 if (STRING_MULTIBYTE (string))
1278 *newpos = string_pos_nchars_ahead (pos, string,
1279 CHARPOS (*newpos) - CHARPOS (pos));
1280 else
1281 BYTEPOS (*newpos) = CHARPOS (*newpos);
1282 }
1283
1284
1285 \f
1286 /***********************************************************************
1287 Lisp form evaluation
1288 ***********************************************************************/
1289
1290 /* Error handler for safe_eval and safe_call. */
1291
1292 static Lisp_Object
1293 safe_eval_handler (arg)
1294 Lisp_Object arg;
1295 {
1296 add_to_log ("Error during redisplay: %s", arg, Qnil);
1297 return Qnil;
1298 }
1299
1300
1301 /* Evaluate SEXPR and return the result, or nil if something went
1302 wrong. Prevent redisplay during the evaluation. */
1303
1304 Lisp_Object
1305 safe_eval (sexpr)
1306 Lisp_Object sexpr;
1307 {
1308 Lisp_Object val;
1309
1310 if (inhibit_eval_during_redisplay)
1311 val = Qnil;
1312 else
1313 {
1314 int count = BINDING_STACK_SIZE ();
1315 struct gcpro gcpro1;
1316
1317 GCPRO1 (sexpr);
1318 specbind (Qinhibit_redisplay, Qt);
1319 val = internal_condition_case_1 (Feval, sexpr, Qerror,
1320 safe_eval_handler);
1321 UNGCPRO;
1322 val = unbind_to (count, val);
1323 }
1324
1325 return val;
1326 }
1327
1328
1329 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1330 Return the result, or nil if something went wrong. Prevent
1331 redisplay during the evaluation. */
1332
1333 Lisp_Object
1334 safe_call (nargs, args)
1335 int nargs;
1336 Lisp_Object *args;
1337 {
1338 Lisp_Object val;
1339
1340 if (inhibit_eval_during_redisplay)
1341 val = Qnil;
1342 else
1343 {
1344 int count = BINDING_STACK_SIZE ();
1345 struct gcpro gcpro1;
1346
1347 GCPRO1 (args[0]);
1348 gcpro1.nvars = nargs;
1349 specbind (Qinhibit_redisplay, Qt);
1350 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1351 safe_eval_handler);
1352 UNGCPRO;
1353 val = unbind_to (count, val);
1354 }
1355
1356 return val;
1357 }
1358
1359
1360 /* Call function FN with one argument ARG.
1361 Return the result, or nil if something went wrong. */
1362
1363 Lisp_Object
1364 safe_call1 (fn, arg)
1365 Lisp_Object fn, arg;
1366 {
1367 Lisp_Object args[2];
1368 args[0] = fn;
1369 args[1] = arg;
1370 return safe_call (2, args);
1371 }
1372
1373
1374 \f
1375 /***********************************************************************
1376 Debugging
1377 ***********************************************************************/
1378
1379 #if 0
1380
1381 /* Define CHECK_IT to perform sanity checks on iterators.
1382 This is for debugging. It is too slow to do unconditionally. */
1383
1384 static void
1385 check_it (it)
1386 struct it *it;
1387 {
1388 if (it->method == next_element_from_string)
1389 {
1390 xassert (STRINGP (it->string));
1391 xassert (IT_STRING_CHARPOS (*it) >= 0);
1392 }
1393 else if (it->method == next_element_from_buffer)
1394 {
1395 /* Check that character and byte positions agree. */
1396 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1397 }
1398
1399 if (it->dpvec)
1400 xassert (it->current.dpvec_index >= 0);
1401 else
1402 xassert (it->current.dpvec_index < 0);
1403 }
1404
1405 #define CHECK_IT(IT) check_it ((IT))
1406
1407 #else /* not 0 */
1408
1409 #define CHECK_IT(IT) (void) 0
1410
1411 #endif /* not 0 */
1412
1413
1414 #if GLYPH_DEBUG
1415
1416 /* Check that the window end of window W is what we expect it
1417 to be---the last row in the current matrix displaying text. */
1418
1419 static void
1420 check_window_end (w)
1421 struct window *w;
1422 {
1423 if (!MINI_WINDOW_P (w)
1424 && !NILP (w->window_end_valid))
1425 {
1426 struct glyph_row *row;
1427 xassert ((row = MATRIX_ROW (w->current_matrix,
1428 XFASTINT (w->window_end_vpos)),
1429 !row->enabled_p
1430 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1431 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1432 }
1433 }
1434
1435 #define CHECK_WINDOW_END(W) check_window_end ((W))
1436
1437 #else /* not GLYPH_DEBUG */
1438
1439 #define CHECK_WINDOW_END(W) (void) 0
1440
1441 #endif /* not GLYPH_DEBUG */
1442
1443
1444 \f
1445 /***********************************************************************
1446 Iterator initialization
1447 ***********************************************************************/
1448
1449 /* Initialize IT for displaying current_buffer in window W, starting
1450 at character position CHARPOS. CHARPOS < 0 means that no buffer
1451 position is specified which is useful when the iterator is assigned
1452 a position later. BYTEPOS is the byte position corresponding to
1453 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1454
1455 If ROW is not null, calls to produce_glyphs with IT as parameter
1456 will produce glyphs in that row.
1457
1458 BASE_FACE_ID is the id of a base face to use. It must be one of
1459 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1460 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1461 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1462
1463 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1464 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1465 will be initialized to use the corresponding mode line glyph row of
1466 the desired matrix of W. */
1467
1468 void
1469 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1470 struct it *it;
1471 struct window *w;
1472 int charpos, bytepos;
1473 struct glyph_row *row;
1474 enum face_id base_face_id;
1475 {
1476 int highlight_region_p;
1477
1478 /* Some precondition checks. */
1479 xassert (w != NULL && it != NULL);
1480 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1481 && charpos <= ZV));
1482
1483 /* If face attributes have been changed since the last redisplay,
1484 free realized faces now because they depend on face definitions
1485 that might have changed. */
1486 if (face_change_count)
1487 {
1488 face_change_count = 0;
1489 free_all_realized_faces (Qnil);
1490 }
1491
1492 /* Use one of the mode line rows of W's desired matrix if
1493 appropriate. */
1494 if (row == NULL)
1495 {
1496 if (base_face_id == MODE_LINE_FACE_ID
1497 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1498 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1499 else if (base_face_id == HEADER_LINE_FACE_ID)
1500 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1501 }
1502
1503 /* Clear IT. */
1504 bzero (it, sizeof *it);
1505 it->current.overlay_string_index = -1;
1506 it->current.dpvec_index = -1;
1507 it->base_face_id = base_face_id;
1508
1509 /* The window in which we iterate over current_buffer: */
1510 XSETWINDOW (it->window, w);
1511 it->w = w;
1512 it->f = XFRAME (w->frame);
1513
1514 /* Extra space between lines (on window systems only). */
1515 if (base_face_id == DEFAULT_FACE_ID
1516 && FRAME_WINDOW_P (it->f))
1517 {
1518 if (NATNUMP (current_buffer->extra_line_spacing))
1519 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1520 else if (it->f->extra_line_spacing > 0)
1521 it->extra_line_spacing = it->f->extra_line_spacing;
1522 }
1523
1524 /* If realized faces have been removed, e.g. because of face
1525 attribute changes of named faces, recompute them. When running
1526 in batch mode, the face cache of Vterminal_frame is null. If
1527 we happen to get called, make a dummy face cache. */
1528 if (
1529 #ifndef WINDOWSNT
1530 noninteractive &&
1531 #endif
1532 FRAME_FACE_CACHE (it->f) == NULL)
1533 init_frame_faces (it->f);
1534 if (FRAME_FACE_CACHE (it->f)->used == 0)
1535 recompute_basic_faces (it->f);
1536
1537 /* Current value of the `space-width', and 'height' properties. */
1538 it->space_width = Qnil;
1539 it->font_height = Qnil;
1540
1541 /* Are control characters displayed as `^C'? */
1542 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1543
1544 /* -1 means everything between a CR and the following line end
1545 is invisible. >0 means lines indented more than this value are
1546 invisible. */
1547 it->selective = (INTEGERP (current_buffer->selective_display)
1548 ? XFASTINT (current_buffer->selective_display)
1549 : (!NILP (current_buffer->selective_display)
1550 ? -1 : 0));
1551 it->selective_display_ellipsis_p
1552 = !NILP (current_buffer->selective_display_ellipses);
1553
1554 /* Display table to use. */
1555 it->dp = window_display_table (w);
1556
1557 /* Are multibyte characters enabled in current_buffer? */
1558 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1559
1560 /* Non-zero if we should highlight the region. */
1561 highlight_region_p
1562 = (!NILP (Vtransient_mark_mode)
1563 && !NILP (current_buffer->mark_active)
1564 && XMARKER (current_buffer->mark)->buffer != 0);
1565
1566 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1567 start and end of a visible region in window IT->w. Set both to
1568 -1 to indicate no region. */
1569 if (highlight_region_p
1570 /* Maybe highlight only in selected window. */
1571 && (/* Either show region everywhere. */
1572 highlight_nonselected_windows
1573 /* Or show region in the selected window. */
1574 || w == XWINDOW (selected_window)
1575 /* Or show the region if we are in the mini-buffer and W is
1576 the window the mini-buffer refers to. */
1577 || (MINI_WINDOW_P (XWINDOW (selected_window))
1578 && WINDOWP (Vminibuf_scroll_window)
1579 && w == XWINDOW (Vminibuf_scroll_window))))
1580 {
1581 int charpos = marker_position (current_buffer->mark);
1582 it->region_beg_charpos = min (PT, charpos);
1583 it->region_end_charpos = max (PT, charpos);
1584 }
1585 else
1586 it->region_beg_charpos = it->region_end_charpos = -1;
1587
1588 /* Get the position at which the redisplay_end_trigger hook should
1589 be run, if it is to be run at all. */
1590 if (MARKERP (w->redisplay_end_trigger)
1591 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1592 it->redisplay_end_trigger_charpos
1593 = marker_position (w->redisplay_end_trigger);
1594 else if (INTEGERP (w->redisplay_end_trigger))
1595 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1596
1597 /* Correct bogus values of tab_width. */
1598 it->tab_width = XINT (current_buffer->tab_width);
1599 if (it->tab_width <= 0 || it->tab_width > 1000)
1600 it->tab_width = 8;
1601
1602 /* Are lines in the display truncated? */
1603 it->truncate_lines_p
1604 = (base_face_id != DEFAULT_FACE_ID
1605 || XINT (it->w->hscroll)
1606 || (truncate_partial_width_windows
1607 && !WINDOW_FULL_WIDTH_P (it->w))
1608 || !NILP (current_buffer->truncate_lines));
1609
1610 /* Get dimensions of truncation and continuation glyphs. These are
1611 displayed as fringe bitmaps under X, so we don't need them for such
1612 frames. */
1613 if (!FRAME_WINDOW_P (it->f))
1614 {
1615 if (it->truncate_lines_p)
1616 {
1617 /* We will need the truncation glyph. */
1618 xassert (it->glyph_row == NULL);
1619 produce_special_glyphs (it, IT_TRUNCATION);
1620 it->truncation_pixel_width = it->pixel_width;
1621 }
1622 else
1623 {
1624 /* We will need the continuation glyph. */
1625 xassert (it->glyph_row == NULL);
1626 produce_special_glyphs (it, IT_CONTINUATION);
1627 it->continuation_pixel_width = it->pixel_width;
1628 }
1629
1630 /* Reset these values to zero because the produce_special_glyphs
1631 above has changed them. */
1632 it->pixel_width = it->ascent = it->descent = 0;
1633 it->phys_ascent = it->phys_descent = 0;
1634 }
1635
1636 /* Set this after getting the dimensions of truncation and
1637 continuation glyphs, so that we don't produce glyphs when calling
1638 produce_special_glyphs, above. */
1639 it->glyph_row = row;
1640 it->area = TEXT_AREA;
1641
1642 /* Get the dimensions of the display area. The display area
1643 consists of the visible window area plus a horizontally scrolled
1644 part to the left of the window. All x-values are relative to the
1645 start of this total display area. */
1646 if (base_face_id != DEFAULT_FACE_ID)
1647 {
1648 /* Mode lines, menu bar in terminal frames. */
1649 it->first_visible_x = 0;
1650 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1651 }
1652 else
1653 {
1654 it->first_visible_x
1655 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1656 it->last_visible_x = (it->first_visible_x
1657 + window_box_width (w, TEXT_AREA));
1658
1659 /* If we truncate lines, leave room for the truncator glyph(s) at
1660 the right margin. Otherwise, leave room for the continuation
1661 glyph(s). Truncation and continuation glyphs are not inserted
1662 for window-based redisplay. */
1663 if (!FRAME_WINDOW_P (it->f))
1664 {
1665 if (it->truncate_lines_p)
1666 it->last_visible_x -= it->truncation_pixel_width;
1667 else
1668 it->last_visible_x -= it->continuation_pixel_width;
1669 }
1670
1671 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1672 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1673 }
1674
1675 /* Leave room for a border glyph. */
1676 if (!FRAME_WINDOW_P (it->f)
1677 && !WINDOW_RIGHTMOST_P (it->w))
1678 it->last_visible_x -= 1;
1679
1680 it->last_visible_y = window_text_bottom_y (w);
1681
1682 /* For mode lines and alike, arrange for the first glyph having a
1683 left box line if the face specifies a box. */
1684 if (base_face_id != DEFAULT_FACE_ID)
1685 {
1686 struct face *face;
1687
1688 it->face_id = base_face_id;
1689
1690 /* If we have a boxed mode line, make the first character appear
1691 with a left box line. */
1692 face = FACE_FROM_ID (it->f, base_face_id);
1693 if (face->box != FACE_NO_BOX)
1694 it->start_of_box_run_p = 1;
1695 }
1696
1697 /* If a buffer position was specified, set the iterator there,
1698 getting overlays and face properties from that position. */
1699 if (charpos >= BUF_BEG (current_buffer))
1700 {
1701 it->end_charpos = ZV;
1702 it->face_id = -1;
1703 IT_CHARPOS (*it) = charpos;
1704
1705 /* Compute byte position if not specified. */
1706 if (bytepos < charpos)
1707 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1708 else
1709 IT_BYTEPOS (*it) = bytepos;
1710
1711 /* Compute faces etc. */
1712 reseat (it, it->current.pos, 1);
1713 }
1714
1715 CHECK_IT (it);
1716 }
1717
1718
1719 /* Initialize IT for the display of window W with window start POS. */
1720
1721 void
1722 start_display (it, w, pos)
1723 struct it *it;
1724 struct window *w;
1725 struct text_pos pos;
1726 {
1727 struct glyph_row *row;
1728 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1729
1730 row = w->desired_matrix->rows + first_vpos;
1731 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1732
1733 if (!it->truncate_lines_p)
1734 {
1735 int start_at_line_beg_p;
1736 int first_y = it->current_y;
1737
1738 /* If window start is not at a line start, skip forward to POS to
1739 get the correct continuation lines width. */
1740 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1741 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1742 if (!start_at_line_beg_p)
1743 {
1744 reseat_at_previous_visible_line_start (it);
1745 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1746
1747 /* If lines are continued, this line may end in the middle
1748 of a multi-glyph character (e.g. a control character
1749 displayed as \003, or in the middle of an overlay
1750 string). In this case move_it_to above will not have
1751 taken us to the start of the continuation line but to the
1752 end of the continued line. */
1753 if (it->current_x > 0)
1754 {
1755 if (it->current.dpvec_index >= 0
1756 || it->current.overlay_string_index >= 0)
1757 {
1758 set_iterator_to_next (it, 1);
1759 move_it_in_display_line_to (it, -1, -1, 0);
1760 }
1761
1762 it->continuation_lines_width += it->current_x;
1763 }
1764
1765 /* We're starting a new display line, not affected by the
1766 height of the continued line, so clear the appropriate
1767 fields in the iterator structure. */
1768 it->max_ascent = it->max_descent = 0;
1769 it->max_phys_ascent = it->max_phys_descent = 0;
1770
1771 it->current_y = first_y;
1772 it->vpos = 0;
1773 it->current_x = it->hpos = 0;
1774 }
1775 }
1776
1777 #if 0 /* Don't assert the following because start_display is sometimes
1778 called intentionally with a window start that is not at a
1779 line start. Please leave this code in as a comment. */
1780
1781 /* Window start should be on a line start, now. */
1782 xassert (it->continuation_lines_width
1783 || IT_CHARPOS (it) == BEGV
1784 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1785 #endif /* 0 */
1786 }
1787
1788
1789 /* Return 1 if POS is a position in ellipses displayed for invisible
1790 text. W is the window we display, for text property lookup. */
1791
1792 static int
1793 in_ellipses_for_invisible_text_p (pos, w)
1794 struct display_pos *pos;
1795 struct window *w;
1796 {
1797 Lisp_Object prop, window;
1798 int ellipses_p = 0;
1799 int charpos = CHARPOS (pos->pos);
1800
1801 /* If POS specifies a position in a display vector, this might
1802 be for an ellipsis displayed for invisible text. We won't
1803 get the iterator set up for delivering that ellipsis unless
1804 we make sure that it gets aware of the invisible text. */
1805 if (pos->dpvec_index >= 0
1806 && pos->overlay_string_index < 0
1807 && CHARPOS (pos->string_pos) < 0
1808 && charpos > BEGV
1809 && (XSETWINDOW (window, w),
1810 prop = Fget_char_property (make_number (charpos),
1811 Qinvisible, window),
1812 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1813 {
1814 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1815 window);
1816 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
1817 }
1818
1819 return ellipses_p;
1820 }
1821
1822
1823 /* Initialize IT for stepping through current_buffer in window W,
1824 starting at position POS that includes overlay string and display
1825 vector/ control character translation position information. Value
1826 is zero if there are overlay strings with newlines at POS. */
1827
1828 static int
1829 init_from_display_pos (it, w, pos)
1830 struct it *it;
1831 struct window *w;
1832 struct display_pos *pos;
1833 {
1834 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1835 int i, overlay_strings_with_newlines = 0;
1836
1837 /* If POS specifies a position in a display vector, this might
1838 be for an ellipsis displayed for invisible text. We won't
1839 get the iterator set up for delivering that ellipsis unless
1840 we make sure that it gets aware of the invisible text. */
1841 if (in_ellipses_for_invisible_text_p (pos, w))
1842 {
1843 --charpos;
1844 bytepos = 0;
1845 }
1846
1847 /* Keep in mind: the call to reseat in init_iterator skips invisible
1848 text, so we might end up at a position different from POS. This
1849 is only a problem when POS is a row start after a newline and an
1850 overlay starts there with an after-string, and the overlay has an
1851 invisible property. Since we don't skip invisible text in
1852 display_line and elsewhere immediately after consuming the
1853 newline before the row start, such a POS will not be in a string,
1854 but the call to init_iterator below will move us to the
1855 after-string. */
1856 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1857
1858 for (i = 0; i < it->n_overlay_strings; ++i)
1859 {
1860 char *s = XSTRING (it->overlay_strings[i])->data;
1861 char *e = s + STRING_BYTES (XSTRING (it->overlay_strings[i]));
1862
1863 while (s < e && *s != '\n')
1864 ++s;
1865
1866 if (s < e)
1867 {
1868 overlay_strings_with_newlines = 1;
1869 break;
1870 }
1871 }
1872
1873 /* If position is within an overlay string, set up IT to the right
1874 overlay string. */
1875 if (pos->overlay_string_index >= 0)
1876 {
1877 int relative_index;
1878
1879 /* If the first overlay string happens to have a `display'
1880 property for an image, the iterator will be set up for that
1881 image, and we have to undo that setup first before we can
1882 correct the overlay string index. */
1883 if (it->method == next_element_from_image)
1884 pop_it (it);
1885
1886 /* We already have the first chunk of overlay strings in
1887 IT->overlay_strings. Load more until the one for
1888 pos->overlay_string_index is in IT->overlay_strings. */
1889 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1890 {
1891 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1892 it->current.overlay_string_index = 0;
1893 while (n--)
1894 {
1895 load_overlay_strings (it, 0);
1896 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1897 }
1898 }
1899
1900 it->current.overlay_string_index = pos->overlay_string_index;
1901 relative_index = (it->current.overlay_string_index
1902 % OVERLAY_STRING_CHUNK_SIZE);
1903 it->string = it->overlay_strings[relative_index];
1904 xassert (STRINGP (it->string));
1905 it->current.string_pos = pos->string_pos;
1906 it->method = next_element_from_string;
1907 }
1908
1909 #if 0 /* This is bogus because POS not having an overlay string
1910 position does not mean it's after the string. Example: A
1911 line starting with a before-string and initialization of IT
1912 to the previous row's end position. */
1913 else if (it->current.overlay_string_index >= 0)
1914 {
1915 /* If POS says we're already after an overlay string ending at
1916 POS, make sure to pop the iterator because it will be in
1917 front of that overlay string. When POS is ZV, we've thereby
1918 also ``processed'' overlay strings at ZV. */
1919 while (it->sp)
1920 pop_it (it);
1921 it->current.overlay_string_index = -1;
1922 it->method = next_element_from_buffer;
1923 if (CHARPOS (pos->pos) == ZV)
1924 it->overlay_strings_at_end_processed_p = 1;
1925 }
1926 #endif /* 0 */
1927
1928 if (CHARPOS (pos->string_pos) >= 0)
1929 {
1930 /* Recorded position is not in an overlay string, but in another
1931 string. This can only be a string from a `display' property.
1932 IT should already be filled with that string. */
1933 it->current.string_pos = pos->string_pos;
1934 xassert (STRINGP (it->string));
1935 }
1936
1937 /* Restore position in display vector translations, control
1938 character translations or ellipses. */
1939 if (pos->dpvec_index >= 0)
1940 {
1941 if (it->dpvec == NULL)
1942 get_next_display_element (it);
1943 xassert (it->dpvec && it->current.dpvec_index == 0);
1944 it->current.dpvec_index = pos->dpvec_index;
1945 }
1946
1947 CHECK_IT (it);
1948 return !overlay_strings_with_newlines;
1949 }
1950
1951
1952 /* Initialize IT for stepping through current_buffer in window W
1953 starting at ROW->start. */
1954
1955 static void
1956 init_to_row_start (it, w, row)
1957 struct it *it;
1958 struct window *w;
1959 struct glyph_row *row;
1960 {
1961 init_from_display_pos (it, w, &row->start);
1962 it->continuation_lines_width = row->continuation_lines_width;
1963 CHECK_IT (it);
1964 }
1965
1966
1967 /* Initialize IT for stepping through current_buffer in window W
1968 starting in the line following ROW, i.e. starting at ROW->end.
1969 Value is zero if there are overlay strings with newlines at ROW's
1970 end position. */
1971
1972 static int
1973 init_to_row_end (it, w, row)
1974 struct it *it;
1975 struct window *w;
1976 struct glyph_row *row;
1977 {
1978 int success = 0;
1979
1980 if (init_from_display_pos (it, w, &row->end))
1981 {
1982 if (row->continued_p)
1983 it->continuation_lines_width
1984 = row->continuation_lines_width + row->pixel_width;
1985 CHECK_IT (it);
1986 success = 1;
1987 }
1988
1989 return success;
1990 }
1991
1992
1993
1994 \f
1995 /***********************************************************************
1996 Text properties
1997 ***********************************************************************/
1998
1999 /* Called when IT reaches IT->stop_charpos. Handle text property and
2000 overlay changes. Set IT->stop_charpos to the next position where
2001 to stop. */
2002
2003 static void
2004 handle_stop (it)
2005 struct it *it;
2006 {
2007 enum prop_handled handled;
2008 int handle_overlay_change_p = 1;
2009 struct props *p;
2010
2011 it->dpvec = NULL;
2012 it->current.dpvec_index = -1;
2013
2014 do
2015 {
2016 handled = HANDLED_NORMALLY;
2017
2018 /* Call text property handlers. */
2019 for (p = it_props; p->handler; ++p)
2020 {
2021 handled = p->handler (it);
2022
2023 if (handled == HANDLED_RECOMPUTE_PROPS)
2024 break;
2025 else if (handled == HANDLED_RETURN)
2026 return;
2027 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2028 handle_overlay_change_p = 0;
2029 }
2030
2031 if (handled != HANDLED_RECOMPUTE_PROPS)
2032 {
2033 /* Don't check for overlay strings below when set to deliver
2034 characters from a display vector. */
2035 if (it->method == next_element_from_display_vector)
2036 handle_overlay_change_p = 0;
2037
2038 /* Handle overlay changes. */
2039 if (handle_overlay_change_p)
2040 handled = handle_overlay_change (it);
2041
2042 /* Determine where to stop next. */
2043 if (handled == HANDLED_NORMALLY)
2044 compute_stop_pos (it);
2045 }
2046 }
2047 while (handled == HANDLED_RECOMPUTE_PROPS);
2048 }
2049
2050
2051 /* Compute IT->stop_charpos from text property and overlay change
2052 information for IT's current position. */
2053
2054 static void
2055 compute_stop_pos (it)
2056 struct it *it;
2057 {
2058 register INTERVAL iv, next_iv;
2059 Lisp_Object object, limit, position;
2060
2061 /* If nowhere else, stop at the end. */
2062 it->stop_charpos = it->end_charpos;
2063
2064 if (STRINGP (it->string))
2065 {
2066 /* Strings are usually short, so don't limit the search for
2067 properties. */
2068 object = it->string;
2069 limit = Qnil;
2070 position = make_number (IT_STRING_CHARPOS (*it));
2071 }
2072 else
2073 {
2074 int charpos;
2075
2076 /* If next overlay change is in front of the current stop pos
2077 (which is IT->end_charpos), stop there. Note: value of
2078 next_overlay_change is point-max if no overlay change
2079 follows. */
2080 charpos = next_overlay_change (IT_CHARPOS (*it));
2081 if (charpos < it->stop_charpos)
2082 it->stop_charpos = charpos;
2083
2084 /* If showing the region, we have to stop at the region
2085 start or end because the face might change there. */
2086 if (it->region_beg_charpos > 0)
2087 {
2088 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2089 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2090 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2091 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2092 }
2093
2094 /* Set up variables for computing the stop position from text
2095 property changes. */
2096 XSETBUFFER (object, current_buffer);
2097 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2098 position = make_number (IT_CHARPOS (*it));
2099
2100 }
2101
2102 /* Get the interval containing IT's position. Value is a null
2103 interval if there isn't such an interval. */
2104 iv = validate_interval_range (object, &position, &position, 0);
2105 if (!NULL_INTERVAL_P (iv))
2106 {
2107 Lisp_Object values_here[LAST_PROP_IDX];
2108 struct props *p;
2109
2110 /* Get properties here. */
2111 for (p = it_props; p->handler; ++p)
2112 values_here[p->idx] = textget (iv->plist, *p->name);
2113
2114 /* Look for an interval following iv that has different
2115 properties. */
2116 for (next_iv = next_interval (iv);
2117 (!NULL_INTERVAL_P (next_iv)
2118 && (NILP (limit)
2119 || XFASTINT (limit) > next_iv->position));
2120 next_iv = next_interval (next_iv))
2121 {
2122 for (p = it_props; p->handler; ++p)
2123 {
2124 Lisp_Object new_value;
2125
2126 new_value = textget (next_iv->plist, *p->name);
2127 if (!EQ (values_here[p->idx], new_value))
2128 break;
2129 }
2130
2131 if (p->handler)
2132 break;
2133 }
2134
2135 if (!NULL_INTERVAL_P (next_iv))
2136 {
2137 if (INTEGERP (limit)
2138 && next_iv->position >= XFASTINT (limit))
2139 /* No text property change up to limit. */
2140 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2141 else
2142 /* Text properties change in next_iv. */
2143 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2144 }
2145 }
2146
2147 xassert (STRINGP (it->string)
2148 || (it->stop_charpos >= BEGV
2149 && it->stop_charpos >= IT_CHARPOS (*it)));
2150 }
2151
2152
2153 /* Return the position of the next overlay change after POS in
2154 current_buffer. Value is point-max if no overlay change
2155 follows. This is like `next-overlay-change' but doesn't use
2156 xmalloc. */
2157
2158 static int
2159 next_overlay_change (pos)
2160 int pos;
2161 {
2162 int noverlays;
2163 int endpos;
2164 Lisp_Object *overlays;
2165 int len;
2166 int i;
2167
2168 /* Get all overlays at the given position. */
2169 len = 10;
2170 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2171 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2172 if (noverlays > len)
2173 {
2174 len = noverlays;
2175 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2176 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2177 }
2178
2179 /* If any of these overlays ends before endpos,
2180 use its ending point instead. */
2181 for (i = 0; i < noverlays; ++i)
2182 {
2183 Lisp_Object oend;
2184 int oendpos;
2185
2186 oend = OVERLAY_END (overlays[i]);
2187 oendpos = OVERLAY_POSITION (oend);
2188 endpos = min (endpos, oendpos);
2189 }
2190
2191 return endpos;
2192 }
2193
2194
2195 \f
2196 /***********************************************************************
2197 Fontification
2198 ***********************************************************************/
2199
2200 /* Handle changes in the `fontified' property of the current buffer by
2201 calling hook functions from Qfontification_functions to fontify
2202 regions of text. */
2203
2204 static enum prop_handled
2205 handle_fontified_prop (it)
2206 struct it *it;
2207 {
2208 Lisp_Object prop, pos;
2209 enum prop_handled handled = HANDLED_NORMALLY;
2210
2211 /* Get the value of the `fontified' property at IT's current buffer
2212 position. (The `fontified' property doesn't have a special
2213 meaning in strings.) If the value is nil, call functions from
2214 Qfontification_functions. */
2215 if (!STRINGP (it->string)
2216 && it->s == NULL
2217 && !NILP (Vfontification_functions)
2218 && !NILP (Vrun_hooks)
2219 && (pos = make_number (IT_CHARPOS (*it)),
2220 prop = Fget_char_property (pos, Qfontified, Qnil),
2221 NILP (prop)))
2222 {
2223 int count = BINDING_STACK_SIZE ();
2224 Lisp_Object val;
2225
2226 val = Vfontification_functions;
2227 specbind (Qfontification_functions, Qnil);
2228
2229 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2230 safe_call1 (val, pos);
2231 else
2232 {
2233 Lisp_Object globals, fn;
2234 struct gcpro gcpro1, gcpro2;
2235
2236 globals = Qnil;
2237 GCPRO2 (val, globals);
2238
2239 for (; CONSP (val); val = XCDR (val))
2240 {
2241 fn = XCAR (val);
2242
2243 if (EQ (fn, Qt))
2244 {
2245 /* A value of t indicates this hook has a local
2246 binding; it means to run the global binding too.
2247 In a global value, t should not occur. If it
2248 does, we must ignore it to avoid an endless
2249 loop. */
2250 for (globals = Fdefault_value (Qfontification_functions);
2251 CONSP (globals);
2252 globals = XCDR (globals))
2253 {
2254 fn = XCAR (globals);
2255 if (!EQ (fn, Qt))
2256 safe_call1 (fn, pos);
2257 }
2258 }
2259 else
2260 safe_call1 (fn, pos);
2261 }
2262
2263 UNGCPRO;
2264 }
2265
2266 unbind_to (count, Qnil);
2267
2268 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2269 something. This avoids an endless loop if they failed to
2270 fontify the text for which reason ever. */
2271 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2272 handled = HANDLED_RECOMPUTE_PROPS;
2273 }
2274
2275 return handled;
2276 }
2277
2278
2279 \f
2280 /***********************************************************************
2281 Faces
2282 ***********************************************************************/
2283
2284 /* Set up iterator IT from face properties at its current position.
2285 Called from handle_stop. */
2286
2287 static enum prop_handled
2288 handle_face_prop (it)
2289 struct it *it;
2290 {
2291 int new_face_id, next_stop;
2292
2293 if (!STRINGP (it->string))
2294 {
2295 new_face_id
2296 = face_at_buffer_position (it->w,
2297 IT_CHARPOS (*it),
2298 it->region_beg_charpos,
2299 it->region_end_charpos,
2300 &next_stop,
2301 (IT_CHARPOS (*it)
2302 + TEXT_PROP_DISTANCE_LIMIT),
2303 0);
2304
2305 /* Is this a start of a run of characters with box face?
2306 Caveat: this can be called for a freshly initialized
2307 iterator; face_id is -1 in this case. We know that the new
2308 face will not change until limit, i.e. if the new face has a
2309 box, all characters up to limit will have one. But, as
2310 usual, we don't know whether limit is really the end. */
2311 if (new_face_id != it->face_id)
2312 {
2313 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2314
2315 /* If new face has a box but old face has not, this is
2316 the start of a run of characters with box, i.e. it has
2317 a shadow on the left side. The value of face_id of the
2318 iterator will be -1 if this is the initial call that gets
2319 the face. In this case, we have to look in front of IT's
2320 position and see whether there is a face != new_face_id. */
2321 it->start_of_box_run_p
2322 = (new_face->box != FACE_NO_BOX
2323 && (it->face_id >= 0
2324 || IT_CHARPOS (*it) == BEG
2325 || new_face_id != face_before_it_pos (it)));
2326 it->face_box_p = new_face->box != FACE_NO_BOX;
2327 }
2328 }
2329 else
2330 {
2331 int base_face_id, bufpos;
2332
2333 if (it->current.overlay_string_index >= 0)
2334 bufpos = IT_CHARPOS (*it);
2335 else
2336 bufpos = 0;
2337
2338 /* For strings from a buffer, i.e. overlay strings or strings
2339 from a `display' property, use the face at IT's current
2340 buffer position as the base face to merge with, so that
2341 overlay strings appear in the same face as surrounding
2342 text, unless they specify their own faces. */
2343 base_face_id = underlying_face_id (it);
2344
2345 new_face_id = face_at_string_position (it->w,
2346 it->string,
2347 IT_STRING_CHARPOS (*it),
2348 bufpos,
2349 it->region_beg_charpos,
2350 it->region_end_charpos,
2351 &next_stop,
2352 base_face_id, 0);
2353
2354 #if 0 /* This shouldn't be neccessary. Let's check it. */
2355 /* If IT is used to display a mode line we would really like to
2356 use the mode line face instead of the frame's default face. */
2357 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2358 && new_face_id == DEFAULT_FACE_ID)
2359 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2360 #endif
2361
2362 /* Is this a start of a run of characters with box? Caveat:
2363 this can be called for a freshly allocated iterator; face_id
2364 is -1 is this case. We know that the new face will not
2365 change until the next check pos, i.e. if the new face has a
2366 box, all characters up to that position will have a
2367 box. But, as usual, we don't know whether that position
2368 is really the end. */
2369 if (new_face_id != it->face_id)
2370 {
2371 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2372 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2373
2374 /* If new face has a box but old face hasn't, this is the
2375 start of a run of characters with box, i.e. it has a
2376 shadow on the left side. */
2377 it->start_of_box_run_p
2378 = new_face->box && (old_face == NULL || !old_face->box);
2379 it->face_box_p = new_face->box != FACE_NO_BOX;
2380 }
2381 }
2382
2383 it->face_id = new_face_id;
2384 return HANDLED_NORMALLY;
2385 }
2386
2387
2388 /* Return the ID of the face ``underlying'' IT's current position,
2389 which is in a string. If the iterator is associated with a
2390 buffer, return the face at IT's current buffer position.
2391 Otherwise, use the iterator's base_face_id. */
2392
2393 static int
2394 underlying_face_id (it)
2395 struct it *it;
2396 {
2397 int face_id = it->base_face_id, i;
2398
2399 xassert (STRINGP (it->string));
2400
2401 for (i = it->sp - 1; i >= 0; --i)
2402 if (NILP (it->stack[i].string))
2403 face_id = it->stack[i].face_id;
2404
2405 return face_id;
2406 }
2407
2408
2409 /* Compute the face one character before or after the current position
2410 of IT. BEFORE_P non-zero means get the face in front of IT's
2411 position. Value is the id of the face. */
2412
2413 static int
2414 face_before_or_after_it_pos (it, before_p)
2415 struct it *it;
2416 int before_p;
2417 {
2418 int face_id, limit;
2419 int next_check_charpos;
2420 struct text_pos pos;
2421
2422 xassert (it->s == NULL);
2423
2424 if (STRINGP (it->string))
2425 {
2426 int bufpos, base_face_id;
2427
2428 /* No face change past the end of the string (for the case
2429 we are padding with spaces). No face change before the
2430 string start. */
2431 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2432 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2433 return it->face_id;
2434
2435 /* Set pos to the position before or after IT's current position. */
2436 if (before_p)
2437 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2438 else
2439 /* For composition, we must check the character after the
2440 composition. */
2441 pos = (it->what == IT_COMPOSITION
2442 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2443 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2444
2445 if (it->current.overlay_string_index >= 0)
2446 bufpos = IT_CHARPOS (*it);
2447 else
2448 bufpos = 0;
2449
2450 base_face_id = underlying_face_id (it);
2451
2452 /* Get the face for ASCII, or unibyte. */
2453 face_id = face_at_string_position (it->w,
2454 it->string,
2455 CHARPOS (pos),
2456 bufpos,
2457 it->region_beg_charpos,
2458 it->region_end_charpos,
2459 &next_check_charpos,
2460 base_face_id, 0);
2461
2462 /* Correct the face for charsets different from ASCII. Do it
2463 for the multibyte case only. The face returned above is
2464 suitable for unibyte text if IT->string is unibyte. */
2465 if (STRING_MULTIBYTE (it->string))
2466 {
2467 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2468 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2469 int c, len;
2470 struct face *face = FACE_FROM_ID (it->f, face_id);
2471
2472 c = string_char_and_length (p, rest, &len);
2473 face_id = FACE_FOR_CHAR (it->f, face, c);
2474 }
2475 }
2476 else
2477 {
2478 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2479 || (IT_CHARPOS (*it) <= BEGV && before_p))
2480 return it->face_id;
2481
2482 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2483 pos = it->current.pos;
2484
2485 if (before_p)
2486 DEC_TEXT_POS (pos, it->multibyte_p);
2487 else
2488 {
2489 if (it->what == IT_COMPOSITION)
2490 /* For composition, we must check the position after the
2491 composition. */
2492 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2493 else
2494 INC_TEXT_POS (pos, it->multibyte_p);
2495 }
2496
2497 /* Determine face for CHARSET_ASCII, or unibyte. */
2498 face_id = face_at_buffer_position (it->w,
2499 CHARPOS (pos),
2500 it->region_beg_charpos,
2501 it->region_end_charpos,
2502 &next_check_charpos,
2503 limit, 0);
2504
2505 /* Correct the face for charsets different from ASCII. Do it
2506 for the multibyte case only. The face returned above is
2507 suitable for unibyte text if current_buffer is unibyte. */
2508 if (it->multibyte_p)
2509 {
2510 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2511 struct face *face = FACE_FROM_ID (it->f, face_id);
2512 face_id = FACE_FOR_CHAR (it->f, face, c);
2513 }
2514 }
2515
2516 return face_id;
2517 }
2518
2519
2520 \f
2521 /***********************************************************************
2522 Invisible text
2523 ***********************************************************************/
2524
2525 /* Set up iterator IT from invisible properties at its current
2526 position. Called from handle_stop. */
2527
2528 static enum prop_handled
2529 handle_invisible_prop (it)
2530 struct it *it;
2531 {
2532 enum prop_handled handled = HANDLED_NORMALLY;
2533
2534 if (STRINGP (it->string))
2535 {
2536 extern Lisp_Object Qinvisible;
2537 Lisp_Object prop, end_charpos, limit, charpos;
2538
2539 /* Get the value of the invisible text property at the
2540 current position. Value will be nil if there is no such
2541 property. */
2542 charpos = make_number (IT_STRING_CHARPOS (*it));
2543 prop = Fget_text_property (charpos, Qinvisible, it->string);
2544
2545 if (!NILP (prop)
2546 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2547 {
2548 handled = HANDLED_RECOMPUTE_PROPS;
2549
2550 /* Get the position at which the next change of the
2551 invisible text property can be found in IT->string.
2552 Value will be nil if the property value is the same for
2553 all the rest of IT->string. */
2554 XSETINT (limit, XSTRING (it->string)->size);
2555 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2556 it->string, limit);
2557
2558 /* Text at current position is invisible. The next
2559 change in the property is at position end_charpos.
2560 Move IT's current position to that position. */
2561 if (INTEGERP (end_charpos)
2562 && XFASTINT (end_charpos) < XFASTINT (limit))
2563 {
2564 struct text_pos old;
2565 old = it->current.string_pos;
2566 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2567 compute_string_pos (&it->current.string_pos, old, it->string);
2568 }
2569 else
2570 {
2571 /* The rest of the string is invisible. If this is an
2572 overlay string, proceed with the next overlay string
2573 or whatever comes and return a character from there. */
2574 if (it->current.overlay_string_index >= 0)
2575 {
2576 next_overlay_string (it);
2577 /* Don't check for overlay strings when we just
2578 finished processing them. */
2579 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2580 }
2581 else
2582 {
2583 struct Lisp_String *s = XSTRING (it->string);
2584 IT_STRING_CHARPOS (*it) = s->size;
2585 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2586 }
2587 }
2588 }
2589 }
2590 else
2591 {
2592 int invis_p, newpos, next_stop, start_charpos;
2593 Lisp_Object pos, prop, overlay;
2594
2595 /* First of all, is there invisible text at this position? */
2596 start_charpos = IT_CHARPOS (*it);
2597 pos = make_number (IT_CHARPOS (*it));
2598 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2599 &overlay);
2600 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2601
2602 /* If we are on invisible text, skip over it. */
2603 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
2604 {
2605 /* Record whether we have to display an ellipsis for the
2606 invisible text. */
2607 int display_ellipsis_p = invis_p == 2;
2608
2609 handled = HANDLED_RECOMPUTE_PROPS;
2610
2611 /* Loop skipping over invisible text. The loop is left at
2612 ZV or with IT on the first char being visible again. */
2613 do
2614 {
2615 /* Try to skip some invisible text. Return value is the
2616 position reached which can be equal to IT's position
2617 if there is nothing invisible here. This skips both
2618 over invisible text properties and overlays with
2619 invisible property. */
2620 newpos = skip_invisible (IT_CHARPOS (*it),
2621 &next_stop, ZV, it->window);
2622
2623 /* If we skipped nothing at all we weren't at invisible
2624 text in the first place. If everything to the end of
2625 the buffer was skipped, end the loop. */
2626 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2627 invis_p = 0;
2628 else
2629 {
2630 /* We skipped some characters but not necessarily
2631 all there are. Check if we ended up on visible
2632 text. Fget_char_property returns the property of
2633 the char before the given position, i.e. if we
2634 get invis_p = 0, this means that the char at
2635 newpos is visible. */
2636 pos = make_number (newpos);
2637 prop = Fget_char_property (pos, Qinvisible, it->window);
2638 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2639 }
2640
2641 /* If we ended up on invisible text, proceed to
2642 skip starting with next_stop. */
2643 if (invis_p)
2644 IT_CHARPOS (*it) = next_stop;
2645 }
2646 while (invis_p);
2647
2648 /* The position newpos is now either ZV or on visible text. */
2649 IT_CHARPOS (*it) = newpos;
2650 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2651
2652 /* If there are before-strings at the start of invisible
2653 text, and the text is invisible because of a text
2654 property, arrange to show before-strings because 20.x did
2655 it that way. (If the text is invisible because of an
2656 overlay property instead of a text property, this is
2657 already handled in the overlay code.) */
2658 if (NILP (overlay)
2659 && get_overlay_strings (it, start_charpos))
2660 {
2661 handled = HANDLED_RECOMPUTE_PROPS;
2662 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2663 }
2664 else if (display_ellipsis_p)
2665 setup_for_ellipsis (it);
2666 }
2667 }
2668
2669 return handled;
2670 }
2671
2672
2673 /* Make iterator IT return `...' next. */
2674
2675 static void
2676 setup_for_ellipsis (it)
2677 struct it *it;
2678 {
2679 if (it->dp
2680 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2681 {
2682 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2683 it->dpvec = v->contents;
2684 it->dpend = v->contents + v->size;
2685 }
2686 else
2687 {
2688 /* Default `...'. */
2689 it->dpvec = default_invis_vector;
2690 it->dpend = default_invis_vector + 3;
2691 }
2692
2693 /* The ellipsis display does not replace the display of the
2694 character at the new position. Indicate this by setting
2695 IT->dpvec_char_len to zero. */
2696 it->dpvec_char_len = 0;
2697
2698 it->current.dpvec_index = 0;
2699 it->method = next_element_from_display_vector;
2700 }
2701
2702
2703 \f
2704 /***********************************************************************
2705 'display' property
2706 ***********************************************************************/
2707
2708 /* Set up iterator IT from `display' property at its current position.
2709 Called from handle_stop. */
2710
2711 static enum prop_handled
2712 handle_display_prop (it)
2713 struct it *it;
2714 {
2715 Lisp_Object prop, object;
2716 struct text_pos *position;
2717 int display_replaced_p = 0;
2718
2719 if (STRINGP (it->string))
2720 {
2721 object = it->string;
2722 position = &it->current.string_pos;
2723 }
2724 else
2725 {
2726 object = it->w->buffer;
2727 position = &it->current.pos;
2728 }
2729
2730 /* Reset those iterator values set from display property values. */
2731 it->font_height = Qnil;
2732 it->space_width = Qnil;
2733 it->voffset = 0;
2734
2735 /* We don't support recursive `display' properties, i.e. string
2736 values that have a string `display' property, that have a string
2737 `display' property etc. */
2738 if (!it->string_from_display_prop_p)
2739 it->area = TEXT_AREA;
2740
2741 prop = Fget_char_property (make_number (position->charpos),
2742 Qdisplay, object);
2743 if (NILP (prop))
2744 return HANDLED_NORMALLY;
2745
2746 if (CONSP (prop)
2747 /* Simple properties. */
2748 && !EQ (XCAR (prop), Qimage)
2749 && !EQ (XCAR (prop), Qspace)
2750 && !EQ (XCAR (prop), Qwhen)
2751 && !EQ (XCAR (prop), Qspace_width)
2752 && !EQ (XCAR (prop), Qheight)
2753 && !EQ (XCAR (prop), Qraise)
2754 /* Marginal area specifications. */
2755 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2756 && !NILP (XCAR (prop)))
2757 {
2758 for (; CONSP (prop); prop = XCDR (prop))
2759 {
2760 if (handle_single_display_prop (it, XCAR (prop), object,
2761 position, display_replaced_p))
2762 display_replaced_p = 1;
2763 }
2764 }
2765 else if (VECTORP (prop))
2766 {
2767 int i;
2768 for (i = 0; i < ASIZE (prop); ++i)
2769 if (handle_single_display_prop (it, AREF (prop, i), object,
2770 position, display_replaced_p))
2771 display_replaced_p = 1;
2772 }
2773 else
2774 {
2775 if (handle_single_display_prop (it, prop, object, position, 0))
2776 display_replaced_p = 1;
2777 }
2778
2779 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2780 }
2781
2782
2783 /* Value is the position of the end of the `display' property starting
2784 at START_POS in OBJECT. */
2785
2786 static struct text_pos
2787 display_prop_end (it, object, start_pos)
2788 struct it *it;
2789 Lisp_Object object;
2790 struct text_pos start_pos;
2791 {
2792 Lisp_Object end;
2793 struct text_pos end_pos;
2794
2795 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2796 Qdisplay, object, Qnil);
2797 CHARPOS (end_pos) = XFASTINT (end);
2798 if (STRINGP (object))
2799 compute_string_pos (&end_pos, start_pos, it->string);
2800 else
2801 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2802
2803 return end_pos;
2804 }
2805
2806
2807 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2808 is the object in which the `display' property was found. *POSITION
2809 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2810 means that we previously saw a display sub-property which already
2811 replaced text display with something else, for example an image;
2812 ignore such properties after the first one has been processed.
2813
2814 If PROP is a `space' or `image' sub-property, set *POSITION to the
2815 end position of the `display' property.
2816
2817 Value is non-zero if something was found which replaces the display
2818 of buffer or string text. */
2819
2820 static int
2821 handle_single_display_prop (it, prop, object, position,
2822 display_replaced_before_p)
2823 struct it *it;
2824 Lisp_Object prop;
2825 Lisp_Object object;
2826 struct text_pos *position;
2827 int display_replaced_before_p;
2828 {
2829 Lisp_Object value;
2830 int replaces_text_display_p = 0;
2831 Lisp_Object form;
2832
2833 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2834 evaluated. If the result is nil, VALUE is ignored. */
2835 form = Qt;
2836 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2837 {
2838 prop = XCDR (prop);
2839 if (!CONSP (prop))
2840 return 0;
2841 form = XCAR (prop);
2842 prop = XCDR (prop);
2843 }
2844
2845 if (!NILP (form) && !EQ (form, Qt))
2846 {
2847 int count = BINDING_STACK_SIZE ();
2848 struct gcpro gcpro1;
2849
2850 /* Bind `object' to the object having the `display' property, a
2851 buffer or string. Bind `position' to the position in the
2852 object where the property was found, and `buffer-position'
2853 to the current position in the buffer. */
2854 specbind (Qobject, object);
2855 specbind (Qposition, make_number (CHARPOS (*position)));
2856 specbind (Qbuffer_position,
2857 make_number (STRINGP (object)
2858 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2859 GCPRO1 (form);
2860 form = safe_eval (form);
2861 UNGCPRO;
2862 unbind_to (count, Qnil);
2863 }
2864
2865 if (NILP (form))
2866 return 0;
2867
2868 if (CONSP (prop)
2869 && EQ (XCAR (prop), Qheight)
2870 && CONSP (XCDR (prop)))
2871 {
2872 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2873 return 0;
2874
2875 /* `(height HEIGHT)'. */
2876 it->font_height = XCAR (XCDR (prop));
2877 if (!NILP (it->font_height))
2878 {
2879 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2880 int new_height = -1;
2881
2882 if (CONSP (it->font_height)
2883 && (EQ (XCAR (it->font_height), Qplus)
2884 || EQ (XCAR (it->font_height), Qminus))
2885 && CONSP (XCDR (it->font_height))
2886 && INTEGERP (XCAR (XCDR (it->font_height))))
2887 {
2888 /* `(+ N)' or `(- N)' where N is an integer. */
2889 int steps = XINT (XCAR (XCDR (it->font_height)));
2890 if (EQ (XCAR (it->font_height), Qplus))
2891 steps = - steps;
2892 it->face_id = smaller_face (it->f, it->face_id, steps);
2893 }
2894 else if (FUNCTIONP (it->font_height))
2895 {
2896 /* Call function with current height as argument.
2897 Value is the new height. */
2898 Lisp_Object height;
2899 height = safe_call1 (it->font_height,
2900 face->lface[LFACE_HEIGHT_INDEX]);
2901 if (NUMBERP (height))
2902 new_height = XFLOATINT (height);
2903 }
2904 else if (NUMBERP (it->font_height))
2905 {
2906 /* Value is a multiple of the canonical char height. */
2907 struct face *face;
2908
2909 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2910 new_height = (XFLOATINT (it->font_height)
2911 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2912 }
2913 else
2914 {
2915 /* Evaluate IT->font_height with `height' bound to the
2916 current specified height to get the new height. */
2917 Lisp_Object value;
2918 int count = BINDING_STACK_SIZE ();
2919
2920 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2921 value = safe_eval (it->font_height);
2922 unbind_to (count, Qnil);
2923
2924 if (NUMBERP (value))
2925 new_height = XFLOATINT (value);
2926 }
2927
2928 if (new_height > 0)
2929 it->face_id = face_with_height (it->f, it->face_id, new_height);
2930 }
2931 }
2932 else if (CONSP (prop)
2933 && EQ (XCAR (prop), Qspace_width)
2934 && CONSP (XCDR (prop)))
2935 {
2936 /* `(space_width WIDTH)'. */
2937 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2938 return 0;
2939
2940 value = XCAR (XCDR (prop));
2941 if (NUMBERP (value) && XFLOATINT (value) > 0)
2942 it->space_width = value;
2943 }
2944 else if (CONSP (prop)
2945 && EQ (XCAR (prop), Qraise)
2946 && CONSP (XCDR (prop)))
2947 {
2948 /* `(raise FACTOR)'. */
2949 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2950 return 0;
2951
2952 #ifdef HAVE_WINDOW_SYSTEM
2953 value = XCAR (XCDR (prop));
2954 if (NUMBERP (value))
2955 {
2956 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2957 it->voffset = - (XFLOATINT (value)
2958 * (FONT_HEIGHT (face->font)));
2959 }
2960 #endif /* HAVE_WINDOW_SYSTEM */
2961 }
2962 else if (!it->string_from_display_prop_p)
2963 {
2964 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2965 VALUE) or `((margin nil) VALUE)' or VALUE. */
2966 Lisp_Object location, value;
2967 struct text_pos start_pos;
2968 int valid_p;
2969
2970 /* Characters having this form of property are not displayed, so
2971 we have to find the end of the property. */
2972 start_pos = *position;
2973 *position = display_prop_end (it, object, start_pos);
2974 value = Qnil;
2975
2976 /* Let's stop at the new position and assume that all
2977 text properties change there. */
2978 it->stop_charpos = position->charpos;
2979
2980 location = Qunbound;
2981 if (CONSP (prop) && CONSP (XCAR (prop)))
2982 {
2983 Lisp_Object tem;
2984
2985 value = XCDR (prop);
2986 if (CONSP (value))
2987 value = XCAR (value);
2988
2989 tem = XCAR (prop);
2990 if (EQ (XCAR (tem), Qmargin)
2991 && (tem = XCDR (tem),
2992 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2993 (NILP (tem)
2994 || EQ (tem, Qleft_margin)
2995 || EQ (tem, Qright_margin))))
2996 location = tem;
2997 }
2998
2999 if (EQ (location, Qunbound))
3000 {
3001 location = Qnil;
3002 value = prop;
3003 }
3004
3005 #ifdef HAVE_WINDOW_SYSTEM
3006 if (FRAME_TERMCAP_P (it->f))
3007 valid_p = STRINGP (value);
3008 else
3009 valid_p = (STRINGP (value)
3010 || (CONSP (value) && EQ (XCAR (value), Qspace))
3011 || valid_image_p (value));
3012 #else /* not HAVE_WINDOW_SYSTEM */
3013 valid_p = STRINGP (value);
3014 #endif /* not HAVE_WINDOW_SYSTEM */
3015
3016 if ((EQ (location, Qleft_margin)
3017 || EQ (location, Qright_margin)
3018 || NILP (location))
3019 && valid_p
3020 && !display_replaced_before_p)
3021 {
3022 replaces_text_display_p = 1;
3023
3024 /* Save current settings of IT so that we can restore them
3025 when we are finished with the glyph property value. */
3026 push_it (it);
3027
3028 if (NILP (location))
3029 it->area = TEXT_AREA;
3030 else if (EQ (location, Qleft_margin))
3031 it->area = LEFT_MARGIN_AREA;
3032 else
3033 it->area = RIGHT_MARGIN_AREA;
3034
3035 if (STRINGP (value))
3036 {
3037 it->string = value;
3038 it->multibyte_p = STRING_MULTIBYTE (it->string);
3039 it->current.overlay_string_index = -1;
3040 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3041 it->end_charpos = it->string_nchars
3042 = XSTRING (it->string)->size;
3043 it->method = next_element_from_string;
3044 it->stop_charpos = 0;
3045 it->string_from_display_prop_p = 1;
3046 /* Say that we haven't consumed the characters with
3047 `display' property yet. The call to pop_it in
3048 set_iterator_to_next will clean this up. */
3049 *position = start_pos;
3050 }
3051 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3052 {
3053 it->method = next_element_from_stretch;
3054 it->object = value;
3055 it->current.pos = it->position = start_pos;
3056 }
3057 #ifdef HAVE_WINDOW_SYSTEM
3058 else
3059 {
3060 it->what = IT_IMAGE;
3061 it->image_id = lookup_image (it->f, value);
3062 it->position = start_pos;
3063 it->object = NILP (object) ? it->w->buffer : object;
3064 it->method = next_element_from_image;
3065
3066 /* Say that we haven't consumed the characters with
3067 `display' property yet. The call to pop_it in
3068 set_iterator_to_next will clean this up. */
3069 *position = start_pos;
3070 }
3071 #endif /* HAVE_WINDOW_SYSTEM */
3072 }
3073 else
3074 /* Invalid property or property not supported. Restore
3075 the position to what it was before. */
3076 *position = start_pos;
3077 }
3078
3079 return replaces_text_display_p;
3080 }
3081
3082
3083 /* Check if PROP is a display sub-property value whose text should be
3084 treated as intangible. */
3085
3086 static int
3087 single_display_prop_intangible_p (prop)
3088 Lisp_Object prop;
3089 {
3090 /* Skip over `when FORM'. */
3091 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3092 {
3093 prop = XCDR (prop);
3094 if (!CONSP (prop))
3095 return 0;
3096 prop = XCDR (prop);
3097 }
3098
3099 if (!CONSP (prop))
3100 return 0;
3101
3102 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3103 we don't need to treat text as intangible. */
3104 if (EQ (XCAR (prop), Qmargin))
3105 {
3106 prop = XCDR (prop);
3107 if (!CONSP (prop))
3108 return 0;
3109
3110 prop = XCDR (prop);
3111 if (!CONSP (prop)
3112 || EQ (XCAR (prop), Qleft_margin)
3113 || EQ (XCAR (prop), Qright_margin))
3114 return 0;
3115 }
3116
3117 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3118 }
3119
3120
3121 /* Check if PROP is a display property value whose text should be
3122 treated as intangible. */
3123
3124 int
3125 display_prop_intangible_p (prop)
3126 Lisp_Object prop;
3127 {
3128 if (CONSP (prop)
3129 && CONSP (XCAR (prop))
3130 && !EQ (Qmargin, XCAR (XCAR (prop))))
3131 {
3132 /* A list of sub-properties. */
3133 while (CONSP (prop))
3134 {
3135 if (single_display_prop_intangible_p (XCAR (prop)))
3136 return 1;
3137 prop = XCDR (prop);
3138 }
3139 }
3140 else if (VECTORP (prop))
3141 {
3142 /* A vector of sub-properties. */
3143 int i;
3144 for (i = 0; i < ASIZE (prop); ++i)
3145 if (single_display_prop_intangible_p (AREF (prop, i)))
3146 return 1;
3147 }
3148 else
3149 return single_display_prop_intangible_p (prop);
3150
3151 return 0;
3152 }
3153
3154
3155 /* Return 1 if PROP is a display sub-property value containing STRING. */
3156
3157 static int
3158 single_display_prop_string_p (prop, string)
3159 Lisp_Object prop, string;
3160 {
3161 if (EQ (string, prop))
3162 return 1;
3163
3164 /* Skip over `when FORM'. */
3165 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3166 {
3167 prop = XCDR (prop);
3168 if (!CONSP (prop))
3169 return 0;
3170 prop = XCDR (prop);
3171 }
3172
3173 if (CONSP (prop))
3174 /* Skip over `margin LOCATION'. */
3175 if (EQ (XCAR (prop), Qmargin))
3176 {
3177 prop = XCDR (prop);
3178 if (!CONSP (prop))
3179 return 0;
3180
3181 prop = XCDR (prop);
3182 if (!CONSP (prop))
3183 return 0;
3184 }
3185
3186 return CONSP (prop) && EQ (XCAR (prop), string);
3187 }
3188
3189
3190 /* Return 1 if STRING appears in the `display' property PROP. */
3191
3192 static int
3193 display_prop_string_p (prop, string)
3194 Lisp_Object prop, string;
3195 {
3196 if (CONSP (prop)
3197 && CONSP (XCAR (prop))
3198 && !EQ (Qmargin, XCAR (XCAR (prop))))
3199 {
3200 /* A list of sub-properties. */
3201 while (CONSP (prop))
3202 {
3203 if (single_display_prop_string_p (XCAR (prop), string))
3204 return 1;
3205 prop = XCDR (prop);
3206 }
3207 }
3208 else if (VECTORP (prop))
3209 {
3210 /* A vector of sub-properties. */
3211 int i;
3212 for (i = 0; i < ASIZE (prop); ++i)
3213 if (single_display_prop_string_p (AREF (prop, i), string))
3214 return 1;
3215 }
3216 else
3217 return single_display_prop_string_p (prop, string);
3218
3219 return 0;
3220 }
3221
3222
3223 /* Determine from which buffer position in W's buffer STRING comes
3224 from. AROUND_CHARPOS is an approximate position where it could
3225 be from. Value is the buffer position or 0 if it couldn't be
3226 determined.
3227
3228 W's buffer must be current.
3229
3230 This function is necessary because we don't record buffer positions
3231 in glyphs generated from strings (to keep struct glyph small).
3232 This function may only use code that doesn't eval because it is
3233 called asynchronously from note_mouse_highlight. */
3234
3235 int
3236 string_buffer_position (w, string, around_charpos)
3237 struct window *w;
3238 Lisp_Object string;
3239 int around_charpos;
3240 {
3241 Lisp_Object limit, prop, pos;
3242 const int MAX_DISTANCE = 1000;
3243 int found = 0;
3244
3245 pos = make_number (around_charpos);
3246 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3247 while (!found && !EQ (pos, limit))
3248 {
3249 prop = Fget_char_property (pos, Qdisplay, Qnil);
3250 if (!NILP (prop) && display_prop_string_p (prop, string))
3251 found = 1;
3252 else
3253 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3254 }
3255
3256 if (!found)
3257 {
3258 pos = make_number (around_charpos);
3259 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3260 while (!found && !EQ (pos, limit))
3261 {
3262 prop = Fget_char_property (pos, Qdisplay, Qnil);
3263 if (!NILP (prop) && display_prop_string_p (prop, string))
3264 found = 1;
3265 else
3266 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3267 limit);
3268 }
3269 }
3270
3271 return found ? XINT (pos) : 0;
3272 }
3273
3274
3275 \f
3276 /***********************************************************************
3277 `composition' property
3278 ***********************************************************************/
3279
3280 /* Set up iterator IT from `composition' property at its current
3281 position. Called from handle_stop. */
3282
3283 static enum prop_handled
3284 handle_composition_prop (it)
3285 struct it *it;
3286 {
3287 Lisp_Object prop, string;
3288 int pos, pos_byte, end;
3289 enum prop_handled handled = HANDLED_NORMALLY;
3290
3291 if (STRINGP (it->string))
3292 {
3293 pos = IT_STRING_CHARPOS (*it);
3294 pos_byte = IT_STRING_BYTEPOS (*it);
3295 string = it->string;
3296 }
3297 else
3298 {
3299 pos = IT_CHARPOS (*it);
3300 pos_byte = IT_BYTEPOS (*it);
3301 string = Qnil;
3302 }
3303
3304 /* If there's a valid composition and point is not inside of the
3305 composition (in the case that the composition is from the current
3306 buffer), draw a glyph composed from the composition components. */
3307 if (find_composition (pos, -1, &pos, &end, &prop, string)
3308 && COMPOSITION_VALID_P (pos, end, prop)
3309 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3310 {
3311 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3312
3313 if (id >= 0)
3314 {
3315 it->method = next_element_from_composition;
3316 it->cmp_id = id;
3317 it->cmp_len = COMPOSITION_LENGTH (prop);
3318 /* For a terminal, draw only the first character of the
3319 components. */
3320 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3321 it->len = (STRINGP (it->string)
3322 ? string_char_to_byte (it->string, end)
3323 : CHAR_TO_BYTE (end)) - pos_byte;
3324 it->stop_charpos = end;
3325 handled = HANDLED_RETURN;
3326 }
3327 }
3328
3329 return handled;
3330 }
3331
3332
3333 \f
3334 /***********************************************************************
3335 Overlay strings
3336 ***********************************************************************/
3337
3338 /* The following structure is used to record overlay strings for
3339 later sorting in load_overlay_strings. */
3340
3341 struct overlay_entry
3342 {
3343 Lisp_Object overlay;
3344 Lisp_Object string;
3345 int priority;
3346 int after_string_p;
3347 };
3348
3349
3350 /* Set up iterator IT from overlay strings at its current position.
3351 Called from handle_stop. */
3352
3353 static enum prop_handled
3354 handle_overlay_change (it)
3355 struct it *it;
3356 {
3357 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3358 return HANDLED_RECOMPUTE_PROPS;
3359 else
3360 return HANDLED_NORMALLY;
3361 }
3362
3363
3364 /* Set up the next overlay string for delivery by IT, if there is an
3365 overlay string to deliver. Called by set_iterator_to_next when the
3366 end of the current overlay string is reached. If there are more
3367 overlay strings to display, IT->string and
3368 IT->current.overlay_string_index are set appropriately here.
3369 Otherwise IT->string is set to nil. */
3370
3371 static void
3372 next_overlay_string (it)
3373 struct it *it;
3374 {
3375 ++it->current.overlay_string_index;
3376 if (it->current.overlay_string_index == it->n_overlay_strings)
3377 {
3378 /* No more overlay strings. Restore IT's settings to what
3379 they were before overlay strings were processed, and
3380 continue to deliver from current_buffer. */
3381 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3382
3383 pop_it (it);
3384 xassert (it->stop_charpos >= BEGV
3385 && it->stop_charpos <= it->end_charpos);
3386 it->string = Qnil;
3387 it->current.overlay_string_index = -1;
3388 SET_TEXT_POS (it->current.string_pos, -1, -1);
3389 it->n_overlay_strings = 0;
3390 it->method = next_element_from_buffer;
3391
3392 /* If we're at the end of the buffer, record that we have
3393 processed the overlay strings there already, so that
3394 next_element_from_buffer doesn't try it again. */
3395 if (IT_CHARPOS (*it) >= it->end_charpos)
3396 it->overlay_strings_at_end_processed_p = 1;
3397
3398 /* If we have to display `...' for invisible text, set
3399 the iterator up for that. */
3400 if (display_ellipsis_p)
3401 setup_for_ellipsis (it);
3402 }
3403 else
3404 {
3405 /* There are more overlay strings to process. If
3406 IT->current.overlay_string_index has advanced to a position
3407 where we must load IT->overlay_strings with more strings, do
3408 it. */
3409 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3410
3411 if (it->current.overlay_string_index && i == 0)
3412 load_overlay_strings (it, 0);
3413
3414 /* Initialize IT to deliver display elements from the overlay
3415 string. */
3416 it->string = it->overlay_strings[i];
3417 it->multibyte_p = STRING_MULTIBYTE (it->string);
3418 SET_TEXT_POS (it->current.string_pos, 0, 0);
3419 it->method = next_element_from_string;
3420 it->stop_charpos = 0;
3421 }
3422
3423 CHECK_IT (it);
3424 }
3425
3426
3427 /* Compare two overlay_entry structures E1 and E2. Used as a
3428 comparison function for qsort in load_overlay_strings. Overlay
3429 strings for the same position are sorted so that
3430
3431 1. All after-strings come in front of before-strings, except
3432 when they come from the same overlay.
3433
3434 2. Within after-strings, strings are sorted so that overlay strings
3435 from overlays with higher priorities come first.
3436
3437 2. Within before-strings, strings are sorted so that overlay
3438 strings from overlays with higher priorities come last.
3439
3440 Value is analogous to strcmp. */
3441
3442
3443 static int
3444 compare_overlay_entries (e1, e2)
3445 void *e1, *e2;
3446 {
3447 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3448 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3449 int result;
3450
3451 if (entry1->after_string_p != entry2->after_string_p)
3452 {
3453 /* Let after-strings appear in front of before-strings if
3454 they come from different overlays. */
3455 if (EQ (entry1->overlay, entry2->overlay))
3456 result = entry1->after_string_p ? 1 : -1;
3457 else
3458 result = entry1->after_string_p ? -1 : 1;
3459 }
3460 else if (entry1->after_string_p)
3461 /* After-strings sorted in order of decreasing priority. */
3462 result = entry2->priority - entry1->priority;
3463 else
3464 /* Before-strings sorted in order of increasing priority. */
3465 result = entry1->priority - entry2->priority;
3466
3467 return result;
3468 }
3469
3470
3471 /* Load the vector IT->overlay_strings with overlay strings from IT's
3472 current buffer position, or from CHARPOS if that is > 0. Set
3473 IT->n_overlays to the total number of overlay strings found.
3474
3475 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3476 a time. On entry into load_overlay_strings,
3477 IT->current.overlay_string_index gives the number of overlay
3478 strings that have already been loaded by previous calls to this
3479 function.
3480
3481 IT->add_overlay_start contains an additional overlay start
3482 position to consider for taking overlay strings from, if non-zero.
3483 This position comes into play when the overlay has an `invisible'
3484 property, and both before and after-strings. When we've skipped to
3485 the end of the overlay, because of its `invisible' property, we
3486 nevertheless want its before-string to appear.
3487 IT->add_overlay_start will contain the overlay start position
3488 in this case.
3489
3490 Overlay strings are sorted so that after-string strings come in
3491 front of before-string strings. Within before and after-strings,
3492 strings are sorted by overlay priority. See also function
3493 compare_overlay_entries. */
3494
3495 static void
3496 load_overlay_strings (it, charpos)
3497 struct it *it;
3498 int charpos;
3499 {
3500 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3501 Lisp_Object ov, overlay, window, str, invisible;
3502 int start, end;
3503 int size = 20;
3504 int n = 0, i, j, invis_p;
3505 struct overlay_entry *entries
3506 = (struct overlay_entry *) alloca (size * sizeof *entries);
3507
3508 if (charpos <= 0)
3509 charpos = IT_CHARPOS (*it);
3510
3511 /* Append the overlay string STRING of overlay OVERLAY to vector
3512 `entries' which has size `size' and currently contains `n'
3513 elements. AFTER_P non-zero means STRING is an after-string of
3514 OVERLAY. */
3515 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3516 do \
3517 { \
3518 Lisp_Object priority; \
3519 \
3520 if (n == size) \
3521 { \
3522 int new_size = 2 * size; \
3523 struct overlay_entry *old = entries; \
3524 entries = \
3525 (struct overlay_entry *) alloca (new_size \
3526 * sizeof *entries); \
3527 bcopy (old, entries, size * sizeof *entries); \
3528 size = new_size; \
3529 } \
3530 \
3531 entries[n].string = (STRING); \
3532 entries[n].overlay = (OVERLAY); \
3533 priority = Foverlay_get ((OVERLAY), Qpriority); \
3534 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3535 entries[n].after_string_p = (AFTER_P); \
3536 ++n; \
3537 } \
3538 while (0)
3539
3540 /* Process overlay before the overlay center. */
3541 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3542 {
3543 overlay = XCAR (ov);
3544 xassert (OVERLAYP (overlay));
3545 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3546 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3547
3548 if (end < charpos)
3549 break;
3550
3551 /* Skip this overlay if it doesn't start or end at IT's current
3552 position. */
3553 if (end != charpos && start != charpos)
3554 continue;
3555
3556 /* Skip this overlay if it doesn't apply to IT->w. */
3557 window = Foverlay_get (overlay, Qwindow);
3558 if (WINDOWP (window) && XWINDOW (window) != it->w)
3559 continue;
3560
3561 /* If the text ``under'' the overlay is invisible, both before-
3562 and after-strings from this overlay are visible; start and
3563 end position are indistinguishable. */
3564 invisible = Foverlay_get (overlay, Qinvisible);
3565 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3566
3567 /* If overlay has a non-empty before-string, record it. */
3568 if ((start == charpos || (end == charpos && invis_p))
3569 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3570 && XSTRING (str)->size)
3571 RECORD_OVERLAY_STRING (overlay, str, 0);
3572
3573 /* If overlay has a non-empty after-string, record it. */
3574 if ((end == charpos || (start == charpos && invis_p))
3575 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3576 && XSTRING (str)->size)
3577 RECORD_OVERLAY_STRING (overlay, str, 1);
3578 }
3579
3580 /* Process overlays after the overlay center. */
3581 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3582 {
3583 overlay = XCAR (ov);
3584 xassert (OVERLAYP (overlay));
3585 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3586 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3587
3588 if (start > charpos)
3589 break;
3590
3591 /* Skip this overlay if it doesn't start or end at IT's current
3592 position. */
3593 if (end != charpos && start != charpos)
3594 continue;
3595
3596 /* Skip this overlay if it doesn't apply to IT->w. */
3597 window = Foverlay_get (overlay, Qwindow);
3598 if (WINDOWP (window) && XWINDOW (window) != it->w)
3599 continue;
3600
3601 /* If the text ``under'' the overlay is invisible, it has a zero
3602 dimension, and both before- and after-strings apply. */
3603 invisible = Foverlay_get (overlay, Qinvisible);
3604 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3605
3606 /* If overlay has a non-empty before-string, record it. */
3607 if ((start == charpos || (end == charpos && invis_p))
3608 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3609 && XSTRING (str)->size)
3610 RECORD_OVERLAY_STRING (overlay, str, 0);
3611
3612 /* If overlay has a non-empty after-string, record it. */
3613 if ((end == charpos || (start == charpos && invis_p))
3614 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3615 && XSTRING (str)->size)
3616 RECORD_OVERLAY_STRING (overlay, str, 1);
3617 }
3618
3619 #undef RECORD_OVERLAY_STRING
3620
3621 /* Sort entries. */
3622 if (n > 1)
3623 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3624
3625 /* Record the total number of strings to process. */
3626 it->n_overlay_strings = n;
3627
3628 /* IT->current.overlay_string_index is the number of overlay strings
3629 that have already been consumed by IT. Copy some of the
3630 remaining overlay strings to IT->overlay_strings. */
3631 i = 0;
3632 j = it->current.overlay_string_index;
3633 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3634 it->overlay_strings[i++] = entries[j++].string;
3635
3636 CHECK_IT (it);
3637 }
3638
3639
3640 /* Get the first chunk of overlay strings at IT's current buffer
3641 position, or at CHARPOS if that is > 0. Value is non-zero if at
3642 least one overlay string was found. */
3643
3644 static int
3645 get_overlay_strings (it, charpos)
3646 struct it *it;
3647 int charpos;
3648 {
3649 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3650 process. This fills IT->overlay_strings with strings, and sets
3651 IT->n_overlay_strings to the total number of strings to process.
3652 IT->pos.overlay_string_index has to be set temporarily to zero
3653 because load_overlay_strings needs this; it must be set to -1
3654 when no overlay strings are found because a zero value would
3655 indicate a position in the first overlay string. */
3656 it->current.overlay_string_index = 0;
3657 load_overlay_strings (it, charpos);
3658
3659 /* If we found overlay strings, set up IT to deliver display
3660 elements from the first one. Otherwise set up IT to deliver
3661 from current_buffer. */
3662 if (it->n_overlay_strings)
3663 {
3664 /* Make sure we know settings in current_buffer, so that we can
3665 restore meaningful values when we're done with the overlay
3666 strings. */
3667 compute_stop_pos (it);
3668 xassert (it->face_id >= 0);
3669
3670 /* Save IT's settings. They are restored after all overlay
3671 strings have been processed. */
3672 xassert (it->sp == 0);
3673 push_it (it);
3674
3675 /* Set up IT to deliver display elements from the first overlay
3676 string. */
3677 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3678 it->string = it->overlay_strings[0];
3679 it->stop_charpos = 0;
3680 it->end_charpos = XSTRING (it->string)->size;
3681 it->multibyte_p = STRING_MULTIBYTE (it->string);
3682 xassert (STRINGP (it->string));
3683 it->method = next_element_from_string;
3684 }
3685 else
3686 {
3687 it->string = Qnil;
3688 it->current.overlay_string_index = -1;
3689 it->method = next_element_from_buffer;
3690 }
3691
3692 CHECK_IT (it);
3693
3694 /* Value is non-zero if we found at least one overlay string. */
3695 return STRINGP (it->string);
3696 }
3697
3698
3699 \f
3700 /***********************************************************************
3701 Saving and restoring state
3702 ***********************************************************************/
3703
3704 /* Save current settings of IT on IT->stack. Called, for example,
3705 before setting up IT for an overlay string, to be able to restore
3706 IT's settings to what they were after the overlay string has been
3707 processed. */
3708
3709 static void
3710 push_it (it)
3711 struct it *it;
3712 {
3713 struct iterator_stack_entry *p;
3714
3715 xassert (it->sp < 2);
3716 p = it->stack + it->sp;
3717
3718 p->stop_charpos = it->stop_charpos;
3719 xassert (it->face_id >= 0);
3720 p->face_id = it->face_id;
3721 p->string = it->string;
3722 p->pos = it->current;
3723 p->end_charpos = it->end_charpos;
3724 p->string_nchars = it->string_nchars;
3725 p->area = it->area;
3726 p->multibyte_p = it->multibyte_p;
3727 p->space_width = it->space_width;
3728 p->font_height = it->font_height;
3729 p->voffset = it->voffset;
3730 p->string_from_display_prop_p = it->string_from_display_prop_p;
3731 p->display_ellipsis_p = 0;
3732 ++it->sp;
3733 }
3734
3735
3736 /* Restore IT's settings from IT->stack. Called, for example, when no
3737 more overlay strings must be processed, and we return to delivering
3738 display elements from a buffer, or when the end of a string from a
3739 `display' property is reached and we return to delivering display
3740 elements from an overlay string, or from a buffer. */
3741
3742 static void
3743 pop_it (it)
3744 struct it *it;
3745 {
3746 struct iterator_stack_entry *p;
3747
3748 xassert (it->sp > 0);
3749 --it->sp;
3750 p = it->stack + it->sp;
3751 it->stop_charpos = p->stop_charpos;
3752 it->face_id = p->face_id;
3753 it->string = p->string;
3754 it->current = p->pos;
3755 it->end_charpos = p->end_charpos;
3756 it->string_nchars = p->string_nchars;
3757 it->area = p->area;
3758 it->multibyte_p = p->multibyte_p;
3759 it->space_width = p->space_width;
3760 it->font_height = p->font_height;
3761 it->voffset = p->voffset;
3762 it->string_from_display_prop_p = p->string_from_display_prop_p;
3763 }
3764
3765
3766 \f
3767 /***********************************************************************
3768 Moving over lines
3769 ***********************************************************************/
3770
3771 /* Set IT's current position to the previous line start. */
3772
3773 static void
3774 back_to_previous_line_start (it)
3775 struct it *it;
3776 {
3777 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3778 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3779 }
3780
3781
3782 /* Move IT to the next line start.
3783
3784 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3785 we skipped over part of the text (as opposed to moving the iterator
3786 continuously over the text). Otherwise, don't change the value
3787 of *SKIPPED_P.
3788
3789 Newlines may come from buffer text, overlay strings, or strings
3790 displayed via the `display' property. That's the reason we can't
3791 simply use find_next_newline_no_quit.
3792
3793 Note that this function may not skip over invisible text that is so
3794 because of text properties and immediately follows a newline. If
3795 it would, function reseat_at_next_visible_line_start, when called
3796 from set_iterator_to_next, would effectively make invisible
3797 characters following a newline part of the wrong glyph row, which
3798 leads to wrong cursor motion. */
3799
3800 static int
3801 forward_to_next_line_start (it, skipped_p)
3802 struct it *it;
3803 int *skipped_p;
3804 {
3805 int old_selective, newline_found_p, n;
3806 const int MAX_NEWLINE_DISTANCE = 500;
3807
3808 /* If already on a newline, just consume it to avoid unintended
3809 skipping over invisible text below. */
3810 if (it->what == IT_CHARACTER
3811 && it->c == '\n'
3812 && CHARPOS (it->position) == IT_CHARPOS (*it))
3813 {
3814 set_iterator_to_next (it, 0);
3815 it->c = 0;
3816 return 1;
3817 }
3818
3819 /* Don't handle selective display in the following. It's (a)
3820 unnecessary because it's done by the caller, and (b) leads to an
3821 infinite recursion because next_element_from_ellipsis indirectly
3822 calls this function. */
3823 old_selective = it->selective;
3824 it->selective = 0;
3825
3826 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3827 from buffer text. */
3828 for (n = newline_found_p = 0;
3829 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3830 n += STRINGP (it->string) ? 0 : 1)
3831 {
3832 if (!get_next_display_element (it))
3833 break;
3834 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3835 set_iterator_to_next (it, 0);
3836 }
3837
3838 /* If we didn't find a newline near enough, see if we can use a
3839 short-cut. */
3840 if (n == MAX_NEWLINE_DISTANCE)
3841 {
3842 int start = IT_CHARPOS (*it);
3843 int limit = find_next_newline_no_quit (start, 1);
3844 Lisp_Object pos;
3845
3846 xassert (!STRINGP (it->string));
3847
3848 /* If there isn't any `display' property in sight, and no
3849 overlays, we can just use the position of the newline in
3850 buffer text. */
3851 if (it->stop_charpos >= limit
3852 || ((pos = Fnext_single_property_change (make_number (start),
3853 Qdisplay,
3854 Qnil, make_number (limit)),
3855 NILP (pos))
3856 && next_overlay_change (start) == ZV))
3857 {
3858 IT_CHARPOS (*it) = limit;
3859 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3860 *skipped_p = newline_found_p = 1;
3861 }
3862 else
3863 {
3864 while (get_next_display_element (it)
3865 && !newline_found_p)
3866 {
3867 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3868 set_iterator_to_next (it, 0);
3869 }
3870 }
3871 }
3872
3873 it->selective = old_selective;
3874 return newline_found_p;
3875 }
3876
3877
3878 /* Set IT's current position to the previous visible line start. Skip
3879 invisible text that is so either due to text properties or due to
3880 selective display. Caution: this does not change IT->current_x and
3881 IT->hpos. */
3882
3883 static void
3884 back_to_previous_visible_line_start (it)
3885 struct it *it;
3886 {
3887 int visible_p = 0;
3888
3889 /* Go back one newline if not on BEGV already. */
3890 if (IT_CHARPOS (*it) > BEGV)
3891 back_to_previous_line_start (it);
3892
3893 /* Move over lines that are invisible because of selective display
3894 or text properties. */
3895 while (IT_CHARPOS (*it) > BEGV
3896 && !visible_p)
3897 {
3898 visible_p = 1;
3899
3900 /* If selective > 0, then lines indented more than that values
3901 are invisible. */
3902 if (it->selective > 0
3903 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3904 it->selective))
3905 visible_p = 0;
3906 else
3907 {
3908 Lisp_Object prop;
3909
3910 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3911 Qinvisible, it->window);
3912 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3913 visible_p = 0;
3914 }
3915
3916 /* Back one more newline if the current one is invisible. */
3917 if (!visible_p)
3918 back_to_previous_line_start (it);
3919 }
3920
3921 xassert (IT_CHARPOS (*it) >= BEGV);
3922 xassert (IT_CHARPOS (*it) == BEGV
3923 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3924 CHECK_IT (it);
3925 }
3926
3927
3928 /* Reseat iterator IT at the previous visible line start. Skip
3929 invisible text that is so either due to text properties or due to
3930 selective display. At the end, update IT's overlay information,
3931 face information etc. */
3932
3933 static void
3934 reseat_at_previous_visible_line_start (it)
3935 struct it *it;
3936 {
3937 back_to_previous_visible_line_start (it);
3938 reseat (it, it->current.pos, 1);
3939 CHECK_IT (it);
3940 }
3941
3942
3943 /* Reseat iterator IT on the next visible line start in the current
3944 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3945 preceding the line start. Skip over invisible text that is so
3946 because of selective display. Compute faces, overlays etc at the
3947 new position. Note that this function does not skip over text that
3948 is invisible because of text properties. */
3949
3950 static void
3951 reseat_at_next_visible_line_start (it, on_newline_p)
3952 struct it *it;
3953 int on_newline_p;
3954 {
3955 int newline_found_p, skipped_p = 0;
3956
3957 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3958
3959 /* Skip over lines that are invisible because they are indented
3960 more than the value of IT->selective. */
3961 if (it->selective > 0)
3962 while (IT_CHARPOS (*it) < ZV
3963 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3964 it->selective))
3965 {
3966 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3967 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3968 }
3969
3970 /* Position on the newline if that's what's requested. */
3971 if (on_newline_p && newline_found_p)
3972 {
3973 if (STRINGP (it->string))
3974 {
3975 if (IT_STRING_CHARPOS (*it) > 0)
3976 {
3977 --IT_STRING_CHARPOS (*it);
3978 --IT_STRING_BYTEPOS (*it);
3979 }
3980 }
3981 else if (IT_CHARPOS (*it) > BEGV)
3982 {
3983 --IT_CHARPOS (*it);
3984 --IT_BYTEPOS (*it);
3985 reseat (it, it->current.pos, 0);
3986 }
3987 }
3988 else if (skipped_p)
3989 reseat (it, it->current.pos, 0);
3990
3991 CHECK_IT (it);
3992 }
3993
3994
3995 \f
3996 /***********************************************************************
3997 Changing an iterator's position
3998 ***********************************************************************/
3999
4000 /* Change IT's current position to POS in current_buffer. If FORCE_P
4001 is non-zero, always check for text properties at the new position.
4002 Otherwise, text properties are only looked up if POS >=
4003 IT->check_charpos of a property. */
4004
4005 static void
4006 reseat (it, pos, force_p)
4007 struct it *it;
4008 struct text_pos pos;
4009 int force_p;
4010 {
4011 int original_pos = IT_CHARPOS (*it);
4012
4013 reseat_1 (it, pos, 0);
4014
4015 /* Determine where to check text properties. Avoid doing it
4016 where possible because text property lookup is very expensive. */
4017 if (force_p
4018 || CHARPOS (pos) > it->stop_charpos
4019 || CHARPOS (pos) < original_pos)
4020 handle_stop (it);
4021
4022 CHECK_IT (it);
4023 }
4024
4025
4026 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4027 IT->stop_pos to POS, also. */
4028
4029 static void
4030 reseat_1 (it, pos, set_stop_p)
4031 struct it *it;
4032 struct text_pos pos;
4033 int set_stop_p;
4034 {
4035 /* Don't call this function when scanning a C string. */
4036 xassert (it->s == NULL);
4037
4038 /* POS must be a reasonable value. */
4039 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4040
4041 it->current.pos = it->position = pos;
4042 XSETBUFFER (it->object, current_buffer);
4043 it->end_charpos = ZV;
4044 it->dpvec = NULL;
4045 it->current.dpvec_index = -1;
4046 it->current.overlay_string_index = -1;
4047 IT_STRING_CHARPOS (*it) = -1;
4048 IT_STRING_BYTEPOS (*it) = -1;
4049 it->string = Qnil;
4050 it->method = next_element_from_buffer;
4051 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4052 it->sp = 0;
4053 it->face_before_selective_p = 0;
4054
4055 if (set_stop_p)
4056 it->stop_charpos = CHARPOS (pos);
4057 }
4058
4059
4060 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4061 If S is non-null, it is a C string to iterate over. Otherwise,
4062 STRING gives a Lisp string to iterate over.
4063
4064 If PRECISION > 0, don't return more then PRECISION number of
4065 characters from the string.
4066
4067 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4068 characters have been returned. FIELD_WIDTH < 0 means an infinite
4069 field width.
4070
4071 MULTIBYTE = 0 means disable processing of multibyte characters,
4072 MULTIBYTE > 0 means enable it,
4073 MULTIBYTE < 0 means use IT->multibyte_p.
4074
4075 IT must be initialized via a prior call to init_iterator before
4076 calling this function. */
4077
4078 static void
4079 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4080 struct it *it;
4081 unsigned char *s;
4082 Lisp_Object string;
4083 int charpos;
4084 int precision, field_width, multibyte;
4085 {
4086 /* No region in strings. */
4087 it->region_beg_charpos = it->region_end_charpos = -1;
4088
4089 /* No text property checks performed by default, but see below. */
4090 it->stop_charpos = -1;
4091
4092 /* Set iterator position and end position. */
4093 bzero (&it->current, sizeof it->current);
4094 it->current.overlay_string_index = -1;
4095 it->current.dpvec_index = -1;
4096 xassert (charpos >= 0);
4097
4098 /* If STRING is specified, use its multibyteness, otherwise use the
4099 setting of MULTIBYTE, if specified. */
4100 if (multibyte >= 0)
4101 it->multibyte_p = multibyte > 0;
4102
4103 if (s == NULL)
4104 {
4105 xassert (STRINGP (string));
4106 it->string = string;
4107 it->s = NULL;
4108 it->end_charpos = it->string_nchars = XSTRING (string)->size;
4109 it->method = next_element_from_string;
4110 it->current.string_pos = string_pos (charpos, string);
4111 }
4112 else
4113 {
4114 it->s = s;
4115 it->string = Qnil;
4116
4117 /* Note that we use IT->current.pos, not it->current.string_pos,
4118 for displaying C strings. */
4119 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4120 if (it->multibyte_p)
4121 {
4122 it->current.pos = c_string_pos (charpos, s, 1);
4123 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4124 }
4125 else
4126 {
4127 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4128 it->end_charpos = it->string_nchars = strlen (s);
4129 }
4130
4131 it->method = next_element_from_c_string;
4132 }
4133
4134 /* PRECISION > 0 means don't return more than PRECISION characters
4135 from the string. */
4136 if (precision > 0 && it->end_charpos - charpos > precision)
4137 it->end_charpos = it->string_nchars = charpos + precision;
4138
4139 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4140 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4141 FIELD_WIDTH < 0 means infinite field width. This is useful for
4142 padding with `-' at the end of a mode line. */
4143 if (field_width < 0)
4144 field_width = INFINITY;
4145 if (field_width > it->end_charpos - charpos)
4146 it->end_charpos = charpos + field_width;
4147
4148 /* Use the standard display table for displaying strings. */
4149 if (DISP_TABLE_P (Vstandard_display_table))
4150 it->dp = XCHAR_TABLE (Vstandard_display_table);
4151
4152 it->stop_charpos = charpos;
4153 CHECK_IT (it);
4154 }
4155
4156
4157 \f
4158 /***********************************************************************
4159 Iteration
4160 ***********************************************************************/
4161
4162 /* Load IT's display element fields with information about the next
4163 display element from the current position of IT. Value is zero if
4164 end of buffer (or C string) is reached. */
4165
4166 int
4167 get_next_display_element (it)
4168 struct it *it;
4169 {
4170 /* Non-zero means that we found an display element. Zero means that
4171 we hit the end of what we iterate over. Performance note: the
4172 function pointer `method' used here turns out to be faster than
4173 using a sequence of if-statements. */
4174 int success_p = (*it->method) (it);
4175
4176 if (it->what == IT_CHARACTER)
4177 {
4178 /* Map via display table or translate control characters.
4179 IT->c, IT->len etc. have been set to the next character by
4180 the function call above. If we have a display table, and it
4181 contains an entry for IT->c, translate it. Don't do this if
4182 IT->c itself comes from a display table, otherwise we could
4183 end up in an infinite recursion. (An alternative could be to
4184 count the recursion depth of this function and signal an
4185 error when a certain maximum depth is reached.) Is it worth
4186 it? */
4187 if (success_p && it->dpvec == NULL)
4188 {
4189 Lisp_Object dv;
4190
4191 if (it->dp
4192 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4193 VECTORP (dv)))
4194 {
4195 struct Lisp_Vector *v = XVECTOR (dv);
4196
4197 /* Return the first character from the display table
4198 entry, if not empty. If empty, don't display the
4199 current character. */
4200 if (v->size)
4201 {
4202 it->dpvec_char_len = it->len;
4203 it->dpvec = v->contents;
4204 it->dpend = v->contents + v->size;
4205 it->current.dpvec_index = 0;
4206 it->method = next_element_from_display_vector;
4207 success_p = get_next_display_element (it);
4208 }
4209 else
4210 {
4211 set_iterator_to_next (it, 0);
4212 success_p = get_next_display_element (it);
4213 }
4214 }
4215
4216 /* Translate control characters into `\003' or `^C' form.
4217 Control characters coming from a display table entry are
4218 currently not translated because we use IT->dpvec to hold
4219 the translation. This could easily be changed but I
4220 don't believe that it is worth doing.
4221
4222 Non-printable multibyte characters are also translated
4223 octal form. */
4224 else if ((it->c < ' '
4225 && (it->area != TEXT_AREA
4226 || (it->c != '\n' && it->c != '\t')))
4227 || (it->c >= 127
4228 && it->len == 1)
4229 || !CHAR_PRINTABLE_P (it->c))
4230 {
4231 /* IT->c is a control character which must be displayed
4232 either as '\003' or as `^C' where the '\\' and '^'
4233 can be defined in the display table. Fill
4234 IT->ctl_chars with glyphs for what we have to
4235 display. Then, set IT->dpvec to these glyphs. */
4236 GLYPH g;
4237
4238 if (it->c < 128 && it->ctl_arrow_p)
4239 {
4240 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4241 if (it->dp
4242 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4243 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4244 g = XINT (DISP_CTRL_GLYPH (it->dp));
4245 else
4246 g = FAST_MAKE_GLYPH ('^', 0);
4247 XSETINT (it->ctl_chars[0], g);
4248
4249 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4250 XSETINT (it->ctl_chars[1], g);
4251
4252 /* Set up IT->dpvec and return first character from it. */
4253 it->dpvec_char_len = it->len;
4254 it->dpvec = it->ctl_chars;
4255 it->dpend = it->dpvec + 2;
4256 it->current.dpvec_index = 0;
4257 it->method = next_element_from_display_vector;
4258 get_next_display_element (it);
4259 }
4260 else
4261 {
4262 unsigned char str[MAX_MULTIBYTE_LENGTH];
4263 int len;
4264 int i;
4265 GLYPH escape_glyph;
4266
4267 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4268 if (it->dp
4269 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4270 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4271 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4272 else
4273 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4274
4275 if (SINGLE_BYTE_CHAR_P (it->c))
4276 str[0] = it->c, len = 1;
4277 else
4278 {
4279 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4280 if (len < 0)
4281 {
4282 /* It's an invalid character, which
4283 shouldn't happen actually, but due to
4284 bugs it may happen. Let's print the char
4285 as is, there's not much meaningful we can
4286 do with it. */
4287 str[0] = it->c;
4288 str[1] = it->c >> 8;
4289 str[2] = it->c >> 16;
4290 str[3] = it->c >> 24;
4291 len = 4;
4292 }
4293 }
4294
4295 for (i = 0; i < len; i++)
4296 {
4297 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4298 /* Insert three more glyphs into IT->ctl_chars for
4299 the octal display of the character. */
4300 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4301 XSETINT (it->ctl_chars[i * 4 + 1], g);
4302 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4303 XSETINT (it->ctl_chars[i * 4 + 2], g);
4304 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4305 XSETINT (it->ctl_chars[i * 4 + 3], g);
4306 }
4307
4308 /* Set up IT->dpvec and return the first character
4309 from it. */
4310 it->dpvec_char_len = it->len;
4311 it->dpvec = it->ctl_chars;
4312 it->dpend = it->dpvec + len * 4;
4313 it->current.dpvec_index = 0;
4314 it->method = next_element_from_display_vector;
4315 get_next_display_element (it);
4316 }
4317 }
4318 }
4319
4320 /* Adjust face id for a multibyte character. There are no
4321 multibyte character in unibyte text. */
4322 if (it->multibyte_p
4323 && success_p
4324 && FRAME_WINDOW_P (it->f))
4325 {
4326 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4327 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4328 }
4329 }
4330
4331 /* Is this character the last one of a run of characters with
4332 box? If yes, set IT->end_of_box_run_p to 1. */
4333 if (it->face_box_p
4334 && it->s == NULL)
4335 {
4336 int face_id;
4337 struct face *face;
4338
4339 it->end_of_box_run_p
4340 = ((face_id = face_after_it_pos (it),
4341 face_id != it->face_id)
4342 && (face = FACE_FROM_ID (it->f, face_id),
4343 face->box == FACE_NO_BOX));
4344 }
4345
4346 /* Value is 0 if end of buffer or string reached. */
4347 return success_p;
4348 }
4349
4350
4351 /* Move IT to the next display element.
4352
4353 RESEAT_P non-zero means if called on a newline in buffer text,
4354 skip to the next visible line start.
4355
4356 Functions get_next_display_element and set_iterator_to_next are
4357 separate because I find this arrangement easier to handle than a
4358 get_next_display_element function that also increments IT's
4359 position. The way it is we can first look at an iterator's current
4360 display element, decide whether it fits on a line, and if it does,
4361 increment the iterator position. The other way around we probably
4362 would either need a flag indicating whether the iterator has to be
4363 incremented the next time, or we would have to implement a
4364 decrement position function which would not be easy to write. */
4365
4366 void
4367 set_iterator_to_next (it, reseat_p)
4368 struct it *it;
4369 int reseat_p;
4370 {
4371 /* Reset flags indicating start and end of a sequence of characters
4372 with box. Reset them at the start of this function because
4373 moving the iterator to a new position might set them. */
4374 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4375
4376 if (it->method == next_element_from_buffer)
4377 {
4378 /* The current display element of IT is a character from
4379 current_buffer. Advance in the buffer, and maybe skip over
4380 invisible lines that are so because of selective display. */
4381 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4382 reseat_at_next_visible_line_start (it, 0);
4383 else
4384 {
4385 xassert (it->len != 0);
4386 IT_BYTEPOS (*it) += it->len;
4387 IT_CHARPOS (*it) += 1;
4388 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4389 }
4390 }
4391 else if (it->method == next_element_from_composition)
4392 {
4393 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4394 if (STRINGP (it->string))
4395 {
4396 IT_STRING_BYTEPOS (*it) += it->len;
4397 IT_STRING_CHARPOS (*it) += it->cmp_len;
4398 it->method = next_element_from_string;
4399 goto consider_string_end;
4400 }
4401 else
4402 {
4403 IT_BYTEPOS (*it) += it->len;
4404 IT_CHARPOS (*it) += it->cmp_len;
4405 it->method = next_element_from_buffer;
4406 }
4407 }
4408 else if (it->method == next_element_from_c_string)
4409 {
4410 /* Current display element of IT is from a C string. */
4411 IT_BYTEPOS (*it) += it->len;
4412 IT_CHARPOS (*it) += 1;
4413 }
4414 else if (it->method == next_element_from_display_vector)
4415 {
4416 /* Current display element of IT is from a display table entry.
4417 Advance in the display table definition. Reset it to null if
4418 end reached, and continue with characters from buffers/
4419 strings. */
4420 ++it->current.dpvec_index;
4421
4422 /* Restore face of the iterator to what they were before the
4423 display vector entry (these entries may contain faces). */
4424 it->face_id = it->saved_face_id;
4425
4426 if (it->dpvec + it->current.dpvec_index == it->dpend)
4427 {
4428 if (it->s)
4429 it->method = next_element_from_c_string;
4430 else if (STRINGP (it->string))
4431 it->method = next_element_from_string;
4432 else
4433 it->method = next_element_from_buffer;
4434
4435 it->dpvec = NULL;
4436 it->current.dpvec_index = -1;
4437
4438 /* Skip over characters which were displayed via IT->dpvec. */
4439 if (it->dpvec_char_len < 0)
4440 reseat_at_next_visible_line_start (it, 1);
4441 else if (it->dpvec_char_len > 0)
4442 {
4443 it->len = it->dpvec_char_len;
4444 set_iterator_to_next (it, reseat_p);
4445 }
4446 }
4447 }
4448 else if (it->method == next_element_from_string)
4449 {
4450 /* Current display element is a character from a Lisp string. */
4451 xassert (it->s == NULL && STRINGP (it->string));
4452 IT_STRING_BYTEPOS (*it) += it->len;
4453 IT_STRING_CHARPOS (*it) += 1;
4454
4455 consider_string_end:
4456
4457 if (it->current.overlay_string_index >= 0)
4458 {
4459 /* IT->string is an overlay string. Advance to the
4460 next, if there is one. */
4461 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4462 next_overlay_string (it);
4463 }
4464 else
4465 {
4466 /* IT->string is not an overlay string. If we reached
4467 its end, and there is something on IT->stack, proceed
4468 with what is on the stack. This can be either another
4469 string, this time an overlay string, or a buffer. */
4470 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4471 && it->sp > 0)
4472 {
4473 pop_it (it);
4474 if (!STRINGP (it->string))
4475 it->method = next_element_from_buffer;
4476 else
4477 goto consider_string_end;
4478 }
4479 }
4480 }
4481 else if (it->method == next_element_from_image
4482 || it->method == next_element_from_stretch)
4483 {
4484 /* The position etc with which we have to proceed are on
4485 the stack. The position may be at the end of a string,
4486 if the `display' property takes up the whole string. */
4487 pop_it (it);
4488 it->image_id = 0;
4489 if (STRINGP (it->string))
4490 {
4491 it->method = next_element_from_string;
4492 goto consider_string_end;
4493 }
4494 else
4495 it->method = next_element_from_buffer;
4496 }
4497 else
4498 /* There are no other methods defined, so this should be a bug. */
4499 abort ();
4500
4501 xassert (it->method != next_element_from_string
4502 || (STRINGP (it->string)
4503 && IT_STRING_CHARPOS (*it) >= 0));
4504 }
4505
4506
4507 /* Load IT's display element fields with information about the next
4508 display element which comes from a display table entry or from the
4509 result of translating a control character to one of the forms `^C'
4510 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4511
4512 static int
4513 next_element_from_display_vector (it)
4514 struct it *it;
4515 {
4516 /* Precondition. */
4517 xassert (it->dpvec && it->current.dpvec_index >= 0);
4518
4519 /* Remember the current face id in case glyphs specify faces.
4520 IT's face is restored in set_iterator_to_next. */
4521 it->saved_face_id = it->face_id;
4522
4523 if (INTEGERP (*it->dpvec)
4524 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4525 {
4526 int lface_id;
4527 GLYPH g;
4528
4529 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4530 it->c = FAST_GLYPH_CHAR (g);
4531 it->len = CHAR_BYTES (it->c);
4532
4533 /* The entry may contain a face id to use. Such a face id is
4534 the id of a Lisp face, not a realized face. A face id of
4535 zero means no face is specified. */
4536 lface_id = FAST_GLYPH_FACE (g);
4537 if (lface_id)
4538 {
4539 /* The function returns -1 if lface_id is invalid. */
4540 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4541 if (face_id >= 0)
4542 it->face_id = face_id;
4543 }
4544 }
4545 else
4546 /* Display table entry is invalid. Return a space. */
4547 it->c = ' ', it->len = 1;
4548
4549 /* Don't change position and object of the iterator here. They are
4550 still the values of the character that had this display table
4551 entry or was translated, and that's what we want. */
4552 it->what = IT_CHARACTER;
4553 return 1;
4554 }
4555
4556
4557 /* Load IT with the next display element from Lisp string IT->string.
4558 IT->current.string_pos is the current position within the string.
4559 If IT->current.overlay_string_index >= 0, the Lisp string is an
4560 overlay string. */
4561
4562 static int
4563 next_element_from_string (it)
4564 struct it *it;
4565 {
4566 struct text_pos position;
4567
4568 xassert (STRINGP (it->string));
4569 xassert (IT_STRING_CHARPOS (*it) >= 0);
4570 position = it->current.string_pos;
4571
4572 /* Time to check for invisible text? */
4573 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4574 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4575 {
4576 handle_stop (it);
4577
4578 /* Since a handler may have changed IT->method, we must
4579 recurse here. */
4580 return get_next_display_element (it);
4581 }
4582
4583 if (it->current.overlay_string_index >= 0)
4584 {
4585 /* Get the next character from an overlay string. In overlay
4586 strings, There is no field width or padding with spaces to
4587 do. */
4588 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4589 {
4590 it->what = IT_EOB;
4591 return 0;
4592 }
4593 else if (STRING_MULTIBYTE (it->string))
4594 {
4595 int remaining = (STRING_BYTES (XSTRING (it->string))
4596 - IT_STRING_BYTEPOS (*it));
4597 unsigned char *s = (XSTRING (it->string)->data
4598 + IT_STRING_BYTEPOS (*it));
4599 it->c = string_char_and_length (s, remaining, &it->len);
4600 }
4601 else
4602 {
4603 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4604 it->len = 1;
4605 }
4606 }
4607 else
4608 {
4609 /* Get the next character from a Lisp string that is not an
4610 overlay string. Such strings come from the mode line, for
4611 example. We may have to pad with spaces, or truncate the
4612 string. See also next_element_from_c_string. */
4613 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4614 {
4615 it->what = IT_EOB;
4616 return 0;
4617 }
4618 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4619 {
4620 /* Pad with spaces. */
4621 it->c = ' ', it->len = 1;
4622 CHARPOS (position) = BYTEPOS (position) = -1;
4623 }
4624 else if (STRING_MULTIBYTE (it->string))
4625 {
4626 int maxlen = (STRING_BYTES (XSTRING (it->string))
4627 - IT_STRING_BYTEPOS (*it));
4628 unsigned char *s = (XSTRING (it->string)->data
4629 + IT_STRING_BYTEPOS (*it));
4630 it->c = string_char_and_length (s, maxlen, &it->len);
4631 }
4632 else
4633 {
4634 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4635 it->len = 1;
4636 }
4637 }
4638
4639 /* Record what we have and where it came from. Note that we store a
4640 buffer position in IT->position although it could arguably be a
4641 string position. */
4642 it->what = IT_CHARACTER;
4643 it->object = it->string;
4644 it->position = position;
4645 return 1;
4646 }
4647
4648
4649 /* Load IT with next display element from C string IT->s.
4650 IT->string_nchars is the maximum number of characters to return
4651 from the string. IT->end_charpos may be greater than
4652 IT->string_nchars when this function is called, in which case we
4653 may have to return padding spaces. Value is zero if end of string
4654 reached, including padding spaces. */
4655
4656 static int
4657 next_element_from_c_string (it)
4658 struct it *it;
4659 {
4660 int success_p = 1;
4661
4662 xassert (it->s);
4663 it->what = IT_CHARACTER;
4664 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4665 it->object = Qnil;
4666
4667 /* IT's position can be greater IT->string_nchars in case a field
4668 width or precision has been specified when the iterator was
4669 initialized. */
4670 if (IT_CHARPOS (*it) >= it->end_charpos)
4671 {
4672 /* End of the game. */
4673 it->what = IT_EOB;
4674 success_p = 0;
4675 }
4676 else if (IT_CHARPOS (*it) >= it->string_nchars)
4677 {
4678 /* Pad with spaces. */
4679 it->c = ' ', it->len = 1;
4680 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4681 }
4682 else if (it->multibyte_p)
4683 {
4684 /* Implementation note: The calls to strlen apparently aren't a
4685 performance problem because there is no noticeable performance
4686 difference between Emacs running in unibyte or multibyte mode. */
4687 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4688 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4689 maxlen, &it->len);
4690 }
4691 else
4692 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4693
4694 return success_p;
4695 }
4696
4697
4698 /* Set up IT to return characters from an ellipsis, if appropriate.
4699 The definition of the ellipsis glyphs may come from a display table
4700 entry. This function Fills IT with the first glyph from the
4701 ellipsis if an ellipsis is to be displayed. */
4702
4703 static int
4704 next_element_from_ellipsis (it)
4705 struct it *it;
4706 {
4707 if (it->selective_display_ellipsis_p)
4708 {
4709 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4710 {
4711 /* Use the display table definition for `...'. Invalid glyphs
4712 will be handled by the method returning elements from dpvec. */
4713 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4714 it->dpvec_char_len = it->len;
4715 it->dpvec = v->contents;
4716 it->dpend = v->contents + v->size;
4717 it->current.dpvec_index = 0;
4718 it->method = next_element_from_display_vector;
4719 }
4720 else
4721 {
4722 /* Use default `...' which is stored in default_invis_vector. */
4723 it->dpvec_char_len = it->len;
4724 it->dpvec = default_invis_vector;
4725 it->dpend = default_invis_vector + 3;
4726 it->current.dpvec_index = 0;
4727 it->method = next_element_from_display_vector;
4728 }
4729 }
4730 else
4731 {
4732 /* The face at the current position may be different from the
4733 face we find after the invisible text. Remember what it
4734 was in IT->saved_face_id, and signal that it's there by
4735 setting face_before_selective_p. */
4736 it->saved_face_id = it->face_id;
4737 it->method = next_element_from_buffer;
4738 reseat_at_next_visible_line_start (it, 1);
4739 it->face_before_selective_p = 1;
4740 }
4741
4742 return get_next_display_element (it);
4743 }
4744
4745
4746 /* Deliver an image display element. The iterator IT is already
4747 filled with image information (done in handle_display_prop). Value
4748 is always 1. */
4749
4750
4751 static int
4752 next_element_from_image (it)
4753 struct it *it;
4754 {
4755 it->what = IT_IMAGE;
4756 return 1;
4757 }
4758
4759
4760 /* Fill iterator IT with next display element from a stretch glyph
4761 property. IT->object is the value of the text property. Value is
4762 always 1. */
4763
4764 static int
4765 next_element_from_stretch (it)
4766 struct it *it;
4767 {
4768 it->what = IT_STRETCH;
4769 return 1;
4770 }
4771
4772
4773 /* Load IT with the next display element from current_buffer. Value
4774 is zero if end of buffer reached. IT->stop_charpos is the next
4775 position at which to stop and check for text properties or buffer
4776 end. */
4777
4778 static int
4779 next_element_from_buffer (it)
4780 struct it *it;
4781 {
4782 int success_p = 1;
4783
4784 /* Check this assumption, otherwise, we would never enter the
4785 if-statement, below. */
4786 xassert (IT_CHARPOS (*it) >= BEGV
4787 && IT_CHARPOS (*it) <= it->stop_charpos);
4788
4789 if (IT_CHARPOS (*it) >= it->stop_charpos)
4790 {
4791 if (IT_CHARPOS (*it) >= it->end_charpos)
4792 {
4793 int overlay_strings_follow_p;
4794
4795 /* End of the game, except when overlay strings follow that
4796 haven't been returned yet. */
4797 if (it->overlay_strings_at_end_processed_p)
4798 overlay_strings_follow_p = 0;
4799 else
4800 {
4801 it->overlay_strings_at_end_processed_p = 1;
4802 overlay_strings_follow_p = get_overlay_strings (it, 0);
4803 }
4804
4805 if (overlay_strings_follow_p)
4806 success_p = get_next_display_element (it);
4807 else
4808 {
4809 it->what = IT_EOB;
4810 it->position = it->current.pos;
4811 success_p = 0;
4812 }
4813 }
4814 else
4815 {
4816 handle_stop (it);
4817 return get_next_display_element (it);
4818 }
4819 }
4820 else
4821 {
4822 /* No face changes, overlays etc. in sight, so just return a
4823 character from current_buffer. */
4824 unsigned char *p;
4825
4826 /* Maybe run the redisplay end trigger hook. Performance note:
4827 This doesn't seem to cost measurable time. */
4828 if (it->redisplay_end_trigger_charpos
4829 && it->glyph_row
4830 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4831 run_redisplay_end_trigger_hook (it);
4832
4833 /* Get the next character, maybe multibyte. */
4834 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4835 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4836 {
4837 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4838 - IT_BYTEPOS (*it));
4839 it->c = string_char_and_length (p, maxlen, &it->len);
4840 }
4841 else
4842 it->c = *p, it->len = 1;
4843
4844 /* Record what we have and where it came from. */
4845 it->what = IT_CHARACTER;;
4846 it->object = it->w->buffer;
4847 it->position = it->current.pos;
4848
4849 /* Normally we return the character found above, except when we
4850 really want to return an ellipsis for selective display. */
4851 if (it->selective)
4852 {
4853 if (it->c == '\n')
4854 {
4855 /* A value of selective > 0 means hide lines indented more
4856 than that number of columns. */
4857 if (it->selective > 0
4858 && IT_CHARPOS (*it) + 1 < ZV
4859 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4860 IT_BYTEPOS (*it) + 1,
4861 it->selective))
4862 {
4863 success_p = next_element_from_ellipsis (it);
4864 it->dpvec_char_len = -1;
4865 }
4866 }
4867 else if (it->c == '\r' && it->selective == -1)
4868 {
4869 /* A value of selective == -1 means that everything from the
4870 CR to the end of the line is invisible, with maybe an
4871 ellipsis displayed for it. */
4872 success_p = next_element_from_ellipsis (it);
4873 it->dpvec_char_len = -1;
4874 }
4875 }
4876 }
4877
4878 /* Value is zero if end of buffer reached. */
4879 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4880 return success_p;
4881 }
4882
4883
4884 /* Run the redisplay end trigger hook for IT. */
4885
4886 static void
4887 run_redisplay_end_trigger_hook (it)
4888 struct it *it;
4889 {
4890 Lisp_Object args[3];
4891
4892 /* IT->glyph_row should be non-null, i.e. we should be actually
4893 displaying something, or otherwise we should not run the hook. */
4894 xassert (it->glyph_row);
4895
4896 /* Set up hook arguments. */
4897 args[0] = Qredisplay_end_trigger_functions;
4898 args[1] = it->window;
4899 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4900 it->redisplay_end_trigger_charpos = 0;
4901
4902 /* Since we are *trying* to run these functions, don't try to run
4903 them again, even if they get an error. */
4904 it->w->redisplay_end_trigger = Qnil;
4905 Frun_hook_with_args (3, args);
4906
4907 /* Notice if it changed the face of the character we are on. */
4908 handle_face_prop (it);
4909 }
4910
4911
4912 /* Deliver a composition display element. The iterator IT is already
4913 filled with composition information (done in
4914 handle_composition_prop). Value is always 1. */
4915
4916 static int
4917 next_element_from_composition (it)
4918 struct it *it;
4919 {
4920 it->what = IT_COMPOSITION;
4921 it->position = (STRINGP (it->string)
4922 ? it->current.string_pos
4923 : it->current.pos);
4924 return 1;
4925 }
4926
4927
4928 \f
4929 /***********************************************************************
4930 Moving an iterator without producing glyphs
4931 ***********************************************************************/
4932
4933 /* Move iterator IT to a specified buffer or X position within one
4934 line on the display without producing glyphs.
4935
4936 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4937 whichever is reached first.
4938
4939 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4940
4941 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4942 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4943 TO_X includes the amount by which a window is horizontally
4944 scrolled.
4945
4946 Value is
4947
4948 MOVE_POS_MATCH_OR_ZV
4949 - when TO_POS or ZV was reached.
4950
4951 MOVE_X_REACHED
4952 -when TO_X was reached before TO_POS or ZV were reached.
4953
4954 MOVE_LINE_CONTINUED
4955 - when we reached the end of the display area and the line must
4956 be continued.
4957
4958 MOVE_LINE_TRUNCATED
4959 - when we reached the end of the display area and the line is
4960 truncated.
4961
4962 MOVE_NEWLINE_OR_CR
4963 - when we stopped at a line end, i.e. a newline or a CR and selective
4964 display is on. */
4965
4966 static enum move_it_result
4967 move_it_in_display_line_to (it, to_charpos, to_x, op)
4968 struct it *it;
4969 int to_charpos, to_x, op;
4970 {
4971 enum move_it_result result = MOVE_UNDEFINED;
4972 struct glyph_row *saved_glyph_row;
4973
4974 /* Don't produce glyphs in produce_glyphs. */
4975 saved_glyph_row = it->glyph_row;
4976 it->glyph_row = NULL;
4977
4978 while (1)
4979 {
4980 int x, i, ascent = 0, descent = 0;
4981
4982 /* Stop when ZV or TO_CHARPOS reached. */
4983 if (!get_next_display_element (it)
4984 || ((op & MOVE_TO_POS) != 0
4985 && BUFFERP (it->object)
4986 && IT_CHARPOS (*it) >= to_charpos))
4987 {
4988 result = MOVE_POS_MATCH_OR_ZV;
4989 break;
4990 }
4991
4992 /* The call to produce_glyphs will get the metrics of the
4993 display element IT is loaded with. We record in x the
4994 x-position before this display element in case it does not
4995 fit on the line. */
4996 x = it->current_x;
4997
4998 /* Remember the line height so far in case the next element doesn't
4999 fit on the line. */
5000 if (!it->truncate_lines_p)
5001 {
5002 ascent = it->max_ascent;
5003 descent = it->max_descent;
5004 }
5005
5006 PRODUCE_GLYPHS (it);
5007
5008 if (it->area != TEXT_AREA)
5009 {
5010 set_iterator_to_next (it, 1);
5011 continue;
5012 }
5013
5014 /* The number of glyphs we get back in IT->nglyphs will normally
5015 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5016 character on a terminal frame, or (iii) a line end. For the
5017 second case, IT->nglyphs - 1 padding glyphs will be present
5018 (on X frames, there is only one glyph produced for a
5019 composite character.
5020
5021 The behavior implemented below means, for continuation lines,
5022 that as many spaces of a TAB as fit on the current line are
5023 displayed there. For terminal frames, as many glyphs of a
5024 multi-glyph character are displayed in the current line, too.
5025 This is what the old redisplay code did, and we keep it that
5026 way. Under X, the whole shape of a complex character must
5027 fit on the line or it will be completely displayed in the
5028 next line.
5029
5030 Note that both for tabs and padding glyphs, all glyphs have
5031 the same width. */
5032 if (it->nglyphs)
5033 {
5034 /* More than one glyph or glyph doesn't fit on line. All
5035 glyphs have the same width. */
5036 int single_glyph_width = it->pixel_width / it->nglyphs;
5037 int new_x;
5038
5039 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5040 {
5041 new_x = x + single_glyph_width;
5042
5043 /* We want to leave anything reaching TO_X to the caller. */
5044 if ((op & MOVE_TO_X) && new_x > to_x)
5045 {
5046 it->current_x = x;
5047 result = MOVE_X_REACHED;
5048 break;
5049 }
5050 else if (/* Lines are continued. */
5051 !it->truncate_lines_p
5052 && (/* And glyph doesn't fit on the line. */
5053 new_x > it->last_visible_x
5054 /* Or it fits exactly and we're on a window
5055 system frame. */
5056 || (new_x == it->last_visible_x
5057 && FRAME_WINDOW_P (it->f))))
5058 {
5059 if (/* IT->hpos == 0 means the very first glyph
5060 doesn't fit on the line, e.g. a wide image. */
5061 it->hpos == 0
5062 || (new_x == it->last_visible_x
5063 && FRAME_WINDOW_P (it->f)))
5064 {
5065 ++it->hpos;
5066 it->current_x = new_x;
5067 if (i == it->nglyphs - 1)
5068 set_iterator_to_next (it, 1);
5069 }
5070 else
5071 {
5072 it->current_x = x;
5073 it->max_ascent = ascent;
5074 it->max_descent = descent;
5075 }
5076
5077 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5078 IT_CHARPOS (*it)));
5079 result = MOVE_LINE_CONTINUED;
5080 break;
5081 }
5082 else if (new_x > it->first_visible_x)
5083 {
5084 /* Glyph is visible. Increment number of glyphs that
5085 would be displayed. */
5086 ++it->hpos;
5087 }
5088 else
5089 {
5090 /* Glyph is completely off the left margin of the display
5091 area. Nothing to do. */
5092 }
5093 }
5094
5095 if (result != MOVE_UNDEFINED)
5096 break;
5097 }
5098 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5099 {
5100 /* Stop when TO_X specified and reached. This check is
5101 necessary here because of lines consisting of a line end,
5102 only. The line end will not produce any glyphs and we
5103 would never get MOVE_X_REACHED. */
5104 xassert (it->nglyphs == 0);
5105 result = MOVE_X_REACHED;
5106 break;
5107 }
5108
5109 /* Is this a line end? If yes, we're done. */
5110 if (ITERATOR_AT_END_OF_LINE_P (it))
5111 {
5112 result = MOVE_NEWLINE_OR_CR;
5113 break;
5114 }
5115
5116 /* The current display element has been consumed. Advance
5117 to the next. */
5118 set_iterator_to_next (it, 1);
5119
5120 /* Stop if lines are truncated and IT's current x-position is
5121 past the right edge of the window now. */
5122 if (it->truncate_lines_p
5123 && it->current_x >= it->last_visible_x)
5124 {
5125 result = MOVE_LINE_TRUNCATED;
5126 break;
5127 }
5128 }
5129
5130 /* Restore the iterator settings altered at the beginning of this
5131 function. */
5132 it->glyph_row = saved_glyph_row;
5133 return result;
5134 }
5135
5136
5137 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
5138 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
5139 the description of enum move_operation_enum.
5140
5141 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5142 screen line, this function will set IT to the next position >
5143 TO_CHARPOS. */
5144
5145 void
5146 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5147 struct it *it;
5148 int to_charpos, to_x, to_y, to_vpos;
5149 int op;
5150 {
5151 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5152 int line_height;
5153 int reached = 0;
5154
5155 for (;;)
5156 {
5157 if (op & MOVE_TO_VPOS)
5158 {
5159 /* If no TO_CHARPOS and no TO_X specified, stop at the
5160 start of the line TO_VPOS. */
5161 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5162 {
5163 if (it->vpos == to_vpos)
5164 {
5165 reached = 1;
5166 break;
5167 }
5168 else
5169 skip = move_it_in_display_line_to (it, -1, -1, 0);
5170 }
5171 else
5172 {
5173 /* TO_VPOS >= 0 means stop at TO_X in the line at
5174 TO_VPOS, or at TO_POS, whichever comes first. */
5175 if (it->vpos == to_vpos)
5176 {
5177 reached = 2;
5178 break;
5179 }
5180
5181 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5182
5183 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5184 {
5185 reached = 3;
5186 break;
5187 }
5188 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5189 {
5190 /* We have reached TO_X but not in the line we want. */
5191 skip = move_it_in_display_line_to (it, to_charpos,
5192 -1, MOVE_TO_POS);
5193 if (skip == MOVE_POS_MATCH_OR_ZV)
5194 {
5195 reached = 4;
5196 break;
5197 }
5198 }
5199 }
5200 }
5201 else if (op & MOVE_TO_Y)
5202 {
5203 struct it it_backup;
5204
5205 /* TO_Y specified means stop at TO_X in the line containing
5206 TO_Y---or at TO_CHARPOS if this is reached first. The
5207 problem is that we can't really tell whether the line
5208 contains TO_Y before we have completely scanned it, and
5209 this may skip past TO_X. What we do is to first scan to
5210 TO_X.
5211
5212 If TO_X is not specified, use a TO_X of zero. The reason
5213 is to make the outcome of this function more predictable.
5214 If we didn't use TO_X == 0, we would stop at the end of
5215 the line which is probably not what a caller would expect
5216 to happen. */
5217 skip = move_it_in_display_line_to (it, to_charpos,
5218 ((op & MOVE_TO_X)
5219 ? to_x : 0),
5220 (MOVE_TO_X
5221 | (op & MOVE_TO_POS)));
5222
5223 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5224 if (skip == MOVE_POS_MATCH_OR_ZV)
5225 {
5226 reached = 5;
5227 break;
5228 }
5229
5230 /* If TO_X was reached, we would like to know whether TO_Y
5231 is in the line. This can only be said if we know the
5232 total line height which requires us to scan the rest of
5233 the line. */
5234 if (skip == MOVE_X_REACHED)
5235 {
5236 it_backup = *it;
5237 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5238 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5239 op & MOVE_TO_POS);
5240 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5241 }
5242
5243 /* Now, decide whether TO_Y is in this line. */
5244 line_height = it->max_ascent + it->max_descent;
5245 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5246
5247 if (to_y >= it->current_y
5248 && to_y < it->current_y + line_height)
5249 {
5250 if (skip == MOVE_X_REACHED)
5251 /* If TO_Y is in this line and TO_X was reached above,
5252 we scanned too far. We have to restore IT's settings
5253 to the ones before skipping. */
5254 *it = it_backup;
5255 reached = 6;
5256 }
5257 else if (skip == MOVE_X_REACHED)
5258 {
5259 skip = skip2;
5260 if (skip == MOVE_POS_MATCH_OR_ZV)
5261 reached = 7;
5262 }
5263
5264 if (reached)
5265 break;
5266 }
5267 else
5268 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5269
5270 switch (skip)
5271 {
5272 case MOVE_POS_MATCH_OR_ZV:
5273 reached = 8;
5274 goto out;
5275
5276 case MOVE_NEWLINE_OR_CR:
5277 set_iterator_to_next (it, 1);
5278 it->continuation_lines_width = 0;
5279 break;
5280
5281 case MOVE_LINE_TRUNCATED:
5282 it->continuation_lines_width = 0;
5283 reseat_at_next_visible_line_start (it, 0);
5284 if ((op & MOVE_TO_POS) != 0
5285 && IT_CHARPOS (*it) > to_charpos)
5286 {
5287 reached = 9;
5288 goto out;
5289 }
5290 break;
5291
5292 case MOVE_LINE_CONTINUED:
5293 it->continuation_lines_width += it->current_x;
5294 break;
5295
5296 default:
5297 abort ();
5298 }
5299
5300 /* Reset/increment for the next run. */
5301 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5302 it->current_x = it->hpos = 0;
5303 it->current_y += it->max_ascent + it->max_descent;
5304 ++it->vpos;
5305 last_height = it->max_ascent + it->max_descent;
5306 last_max_ascent = it->max_ascent;
5307 it->max_ascent = it->max_descent = 0;
5308 }
5309
5310 out:
5311
5312 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5313 }
5314
5315
5316 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5317
5318 If DY > 0, move IT backward at least that many pixels. DY = 0
5319 means move IT backward to the preceding line start or BEGV. This
5320 function may move over more than DY pixels if IT->current_y - DY
5321 ends up in the middle of a line; in this case IT->current_y will be
5322 set to the top of the line moved to. */
5323
5324 void
5325 move_it_vertically_backward (it, dy)
5326 struct it *it;
5327 int dy;
5328 {
5329 int nlines, h;
5330 struct it it2, it3;
5331 int start_pos = IT_CHARPOS (*it);
5332
5333 xassert (dy >= 0);
5334
5335 /* Estimate how many newlines we must move back. */
5336 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5337
5338 /* Set the iterator's position that many lines back. */
5339 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5340 back_to_previous_visible_line_start (it);
5341
5342 /* Reseat the iterator here. When moving backward, we don't want
5343 reseat to skip forward over invisible text, set up the iterator
5344 to deliver from overlay strings at the new position etc. So,
5345 use reseat_1 here. */
5346 reseat_1 (it, it->current.pos, 1);
5347
5348 /* We are now surely at a line start. */
5349 it->current_x = it->hpos = 0;
5350
5351 /* Move forward and see what y-distance we moved. First move to the
5352 start of the next line so that we get its height. We need this
5353 height to be able to tell whether we reached the specified
5354 y-distance. */
5355 it2 = *it;
5356 it2.max_ascent = it2.max_descent = 0;
5357 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5358 MOVE_TO_POS | MOVE_TO_VPOS);
5359 xassert (IT_CHARPOS (*it) >= BEGV);
5360 it3 = it2;
5361
5362 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5363 xassert (IT_CHARPOS (*it) >= BEGV);
5364 h = it2.current_y - it->current_y;
5365 nlines = it2.vpos - it->vpos;
5366
5367 /* Correct IT's y and vpos position. */
5368 it->vpos -= nlines;
5369 it->current_y -= h;
5370
5371 if (dy == 0)
5372 {
5373 /* DY == 0 means move to the start of the screen line. The
5374 value of nlines is > 0 if continuation lines were involved. */
5375 if (nlines > 0)
5376 move_it_by_lines (it, nlines, 1);
5377 xassert (IT_CHARPOS (*it) <= start_pos);
5378 }
5379 else if (nlines)
5380 {
5381 /* The y-position we try to reach. Note that h has been
5382 subtracted in front of the if-statement. */
5383 int target_y = it->current_y + h - dy;
5384 int y0 = it3.current_y;
5385 int y1 = line_bottom_y (&it3);
5386 int line_height = y1 - y0;
5387
5388 /* If we did not reach target_y, try to move further backward if
5389 we can. If we moved too far backward, try to move forward. */
5390 if (target_y < it->current_y
5391 /* This is heuristic. In a window that's 3 lines high, with
5392 a line height of 13 pixels each, recentering with point
5393 on the bottom line will try to move -39/2 = 19 pixels
5394 backward. Try to avoid moving into the first line. */
5395 && it->current_y - target_y > line_height / 3 * 2
5396 && IT_CHARPOS (*it) > BEGV)
5397 {
5398 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5399 target_y - it->current_y));
5400 move_it_vertically (it, target_y - it->current_y);
5401 xassert (IT_CHARPOS (*it) >= BEGV);
5402 }
5403 else if (target_y >= it->current_y + line_height
5404 && IT_CHARPOS (*it) < ZV)
5405 {
5406 /* Should move forward by at least one line, maybe more. */
5407 do
5408 {
5409 move_it_by_lines (it, 1, 1);
5410 }
5411 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5412
5413 xassert (IT_CHARPOS (*it) >= BEGV);
5414 }
5415 }
5416 }
5417
5418
5419 /* Move IT by a specified amount of pixel lines DY. DY negative means
5420 move backwards. DY = 0 means move to start of screen line. At the
5421 end, IT will be on the start of a screen line. */
5422
5423 void
5424 move_it_vertically (it, dy)
5425 struct it *it;
5426 int dy;
5427 {
5428 if (dy <= 0)
5429 move_it_vertically_backward (it, -dy);
5430 else if (dy > 0)
5431 {
5432 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5433 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5434 MOVE_TO_POS | MOVE_TO_Y);
5435 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5436
5437 /* If buffer ends in ZV without a newline, move to the start of
5438 the line to satisfy the post-condition. */
5439 if (IT_CHARPOS (*it) == ZV
5440 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5441 move_it_by_lines (it, 0, 0);
5442 }
5443 }
5444
5445
5446 /* Move iterator IT past the end of the text line it is in. */
5447
5448 void
5449 move_it_past_eol (it)
5450 struct it *it;
5451 {
5452 enum move_it_result rc;
5453
5454 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5455 if (rc == MOVE_NEWLINE_OR_CR)
5456 set_iterator_to_next (it, 0);
5457 }
5458
5459
5460 #if 0 /* Currently not used. */
5461
5462 /* Return non-zero if some text between buffer positions START_CHARPOS
5463 and END_CHARPOS is invisible. IT->window is the window for text
5464 property lookup. */
5465
5466 static int
5467 invisible_text_between_p (it, start_charpos, end_charpos)
5468 struct it *it;
5469 int start_charpos, end_charpos;
5470 {
5471 Lisp_Object prop, limit;
5472 int invisible_found_p;
5473
5474 xassert (it != NULL && start_charpos <= end_charpos);
5475
5476 /* Is text at START invisible? */
5477 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5478 it->window);
5479 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5480 invisible_found_p = 1;
5481 else
5482 {
5483 limit = Fnext_single_char_property_change (make_number (start_charpos),
5484 Qinvisible, Qnil,
5485 make_number (end_charpos));
5486 invisible_found_p = XFASTINT (limit) < end_charpos;
5487 }
5488
5489 return invisible_found_p;
5490 }
5491
5492 #endif /* 0 */
5493
5494
5495 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5496 negative means move up. DVPOS == 0 means move to the start of the
5497 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5498 NEED_Y_P is zero, IT->current_y will be left unchanged.
5499
5500 Further optimization ideas: If we would know that IT->f doesn't use
5501 a face with proportional font, we could be faster for
5502 truncate-lines nil. */
5503
5504 void
5505 move_it_by_lines (it, dvpos, need_y_p)
5506 struct it *it;
5507 int dvpos, need_y_p;
5508 {
5509 struct position pos;
5510
5511 if (!FRAME_WINDOW_P (it->f))
5512 {
5513 struct text_pos textpos;
5514
5515 /* We can use vmotion on frames without proportional fonts. */
5516 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5517 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5518 reseat (it, textpos, 1);
5519 it->vpos += pos.vpos;
5520 it->current_y += pos.vpos;
5521 }
5522 else if (dvpos == 0)
5523 {
5524 /* DVPOS == 0 means move to the start of the screen line. */
5525 move_it_vertically_backward (it, 0);
5526 xassert (it->current_x == 0 && it->hpos == 0);
5527 }
5528 else if (dvpos > 0)
5529 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5530 else
5531 {
5532 struct it it2;
5533 int start_charpos, i;
5534
5535 /* Start at the beginning of the screen line containing IT's
5536 position. */
5537 move_it_vertically_backward (it, 0);
5538
5539 /* Go back -DVPOS visible lines and reseat the iterator there. */
5540 start_charpos = IT_CHARPOS (*it);
5541 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5542 back_to_previous_visible_line_start (it);
5543 reseat (it, it->current.pos, 1);
5544 it->current_x = it->hpos = 0;
5545
5546 /* Above call may have moved too far if continuation lines
5547 are involved. Scan forward and see if it did. */
5548 it2 = *it;
5549 it2.vpos = it2.current_y = 0;
5550 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5551 it->vpos -= it2.vpos;
5552 it->current_y -= it2.current_y;
5553 it->current_x = it->hpos = 0;
5554
5555 /* If we moved too far, move IT some lines forward. */
5556 if (it2.vpos > -dvpos)
5557 {
5558 int delta = it2.vpos + dvpos;
5559 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5560 }
5561 }
5562 }
5563
5564
5565 \f
5566 /***********************************************************************
5567 Messages
5568 ***********************************************************************/
5569
5570
5571 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5572 to *Messages*. */
5573
5574 void
5575 add_to_log (format, arg1, arg2)
5576 char *format;
5577 Lisp_Object arg1, arg2;
5578 {
5579 Lisp_Object args[3];
5580 Lisp_Object msg, fmt;
5581 char *buffer;
5582 int len;
5583 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5584
5585 /* Do nothing if called asynchronously. Inserting text into
5586 a buffer may call after-change-functions and alike and
5587 that would means running Lisp asynchronously. */
5588 if (handling_signal)
5589 return;
5590
5591 fmt = msg = Qnil;
5592 GCPRO4 (fmt, msg, arg1, arg2);
5593
5594 args[0] = fmt = build_string (format);
5595 args[1] = arg1;
5596 args[2] = arg2;
5597 msg = Fformat (3, args);
5598
5599 len = STRING_BYTES (XSTRING (msg)) + 1;
5600 buffer = (char *) alloca (len);
5601 bcopy (XSTRING (msg)->data, buffer, len);
5602
5603 message_dolog (buffer, len - 1, 1, 0);
5604 UNGCPRO;
5605 }
5606
5607
5608 /* Output a newline in the *Messages* buffer if "needs" one. */
5609
5610 void
5611 message_log_maybe_newline ()
5612 {
5613 if (message_log_need_newline)
5614 message_dolog ("", 0, 1, 0);
5615 }
5616
5617
5618 /* Add a string M of length NBYTES to the message log, optionally
5619 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5620 nonzero, means interpret the contents of M as multibyte. This
5621 function calls low-level routines in order to bypass text property
5622 hooks, etc. which might not be safe to run. */
5623
5624 void
5625 message_dolog (m, nbytes, nlflag, multibyte)
5626 char *m;
5627 int nbytes, nlflag, multibyte;
5628 {
5629 if (!NILP (Vmessage_log_max))
5630 {
5631 struct buffer *oldbuf;
5632 Lisp_Object oldpoint, oldbegv, oldzv;
5633 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5634 int point_at_end = 0;
5635 int zv_at_end = 0;
5636 Lisp_Object old_deactivate_mark, tem;
5637 struct gcpro gcpro1;
5638
5639 old_deactivate_mark = Vdeactivate_mark;
5640 oldbuf = current_buffer;
5641 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5642 current_buffer->undo_list = Qt;
5643
5644 oldpoint = message_dolog_marker1;
5645 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5646 oldbegv = message_dolog_marker2;
5647 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5648 oldzv = message_dolog_marker3;
5649 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5650 GCPRO1 (old_deactivate_mark);
5651
5652 if (PT == Z)
5653 point_at_end = 1;
5654 if (ZV == Z)
5655 zv_at_end = 1;
5656
5657 BEGV = BEG;
5658 BEGV_BYTE = BEG_BYTE;
5659 ZV = Z;
5660 ZV_BYTE = Z_BYTE;
5661 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5662
5663 /* Insert the string--maybe converting multibyte to single byte
5664 or vice versa, so that all the text fits the buffer. */
5665 if (multibyte
5666 && NILP (current_buffer->enable_multibyte_characters))
5667 {
5668 int i, c, char_bytes;
5669 unsigned char work[1];
5670
5671 /* Convert a multibyte string to single-byte
5672 for the *Message* buffer. */
5673 for (i = 0; i < nbytes; i += nbytes)
5674 {
5675 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5676 work[0] = (SINGLE_BYTE_CHAR_P (c)
5677 ? c
5678 : multibyte_char_to_unibyte (c, Qnil));
5679 insert_1_both (work, 1, 1, 1, 0, 0);
5680 }
5681 }
5682 else if (! multibyte
5683 && ! NILP (current_buffer->enable_multibyte_characters))
5684 {
5685 int i, c, char_bytes;
5686 unsigned char *msg = (unsigned char *) m;
5687 unsigned char str[MAX_MULTIBYTE_LENGTH];
5688 /* Convert a single-byte string to multibyte
5689 for the *Message* buffer. */
5690 for (i = 0; i < nbytes; i++)
5691 {
5692 c = unibyte_char_to_multibyte (msg[i]);
5693 char_bytes = CHAR_STRING (c, str);
5694 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5695 }
5696 }
5697 else if (nbytes)
5698 insert_1 (m, nbytes, 1, 0, 0);
5699
5700 if (nlflag)
5701 {
5702 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5703 insert_1 ("\n", 1, 1, 0, 0);
5704
5705 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5706 this_bol = PT;
5707 this_bol_byte = PT_BYTE;
5708
5709 /* See if this line duplicates the previous one.
5710 If so, combine duplicates. */
5711 if (this_bol > BEG)
5712 {
5713 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5714 prev_bol = PT;
5715 prev_bol_byte = PT_BYTE;
5716
5717 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5718 this_bol, this_bol_byte);
5719 if (dup)
5720 {
5721 del_range_both (prev_bol, prev_bol_byte,
5722 this_bol, this_bol_byte, 0);
5723 if (dup > 1)
5724 {
5725 char dupstr[40];
5726 int duplen;
5727
5728 /* If you change this format, don't forget to also
5729 change message_log_check_duplicate. */
5730 sprintf (dupstr, " [%d times]", dup);
5731 duplen = strlen (dupstr);
5732 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5733 insert_1 (dupstr, duplen, 1, 0, 1);
5734 }
5735 }
5736 }
5737
5738 /* If we have more than the desired maximum number of lines
5739 in the *Messages* buffer now, delete the oldest ones.
5740 This is safe because we don't have undo in this buffer. */
5741
5742 if (NATNUMP (Vmessage_log_max))
5743 {
5744 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5745 -XFASTINT (Vmessage_log_max) - 1, 0);
5746 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5747 }
5748 }
5749 BEGV = XMARKER (oldbegv)->charpos;
5750 BEGV_BYTE = marker_byte_position (oldbegv);
5751
5752 if (zv_at_end)
5753 {
5754 ZV = Z;
5755 ZV_BYTE = Z_BYTE;
5756 }
5757 else
5758 {
5759 ZV = XMARKER (oldzv)->charpos;
5760 ZV_BYTE = marker_byte_position (oldzv);
5761 }
5762
5763 if (point_at_end)
5764 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5765 else
5766 /* We can't do Fgoto_char (oldpoint) because it will run some
5767 Lisp code. */
5768 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5769 XMARKER (oldpoint)->bytepos);
5770
5771 UNGCPRO;
5772 unchain_marker (oldpoint);
5773 unchain_marker (oldbegv);
5774 unchain_marker (oldzv);
5775
5776 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5777 set_buffer_internal (oldbuf);
5778 if (NILP (tem))
5779 windows_or_buffers_changed = old_windows_or_buffers_changed;
5780 message_log_need_newline = !nlflag;
5781 Vdeactivate_mark = old_deactivate_mark;
5782 }
5783 }
5784
5785
5786 /* We are at the end of the buffer after just having inserted a newline.
5787 (Note: We depend on the fact we won't be crossing the gap.)
5788 Check to see if the most recent message looks a lot like the previous one.
5789 Return 0 if different, 1 if the new one should just replace it, or a
5790 value N > 1 if we should also append " [N times]". */
5791
5792 static int
5793 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5794 int prev_bol, this_bol;
5795 int prev_bol_byte, this_bol_byte;
5796 {
5797 int i;
5798 int len = Z_BYTE - 1 - this_bol_byte;
5799 int seen_dots = 0;
5800 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5801 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5802
5803 for (i = 0; i < len; i++)
5804 {
5805 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5806 seen_dots = 1;
5807 if (p1[i] != p2[i])
5808 return seen_dots;
5809 }
5810 p1 += len;
5811 if (*p1 == '\n')
5812 return 2;
5813 if (*p1++ == ' ' && *p1++ == '[')
5814 {
5815 int n = 0;
5816 while (*p1 >= '0' && *p1 <= '9')
5817 n = n * 10 + *p1++ - '0';
5818 if (strncmp (p1, " times]\n", 8) == 0)
5819 return n+1;
5820 }
5821 return 0;
5822 }
5823
5824
5825 /* Display an echo area message M with a specified length of NBYTES
5826 bytes. The string may include null characters. If M is 0, clear
5827 out any existing message, and let the mini-buffer text show
5828 through.
5829
5830 The buffer M must continue to exist until after the echo area gets
5831 cleared or some other message gets displayed there. This means do
5832 not pass text that is stored in a Lisp string; do not pass text in
5833 a buffer that was alloca'd. */
5834
5835 void
5836 message2 (m, nbytes, multibyte)
5837 char *m;
5838 int nbytes;
5839 int multibyte;
5840 {
5841 /* First flush out any partial line written with print. */
5842 message_log_maybe_newline ();
5843 if (m)
5844 message_dolog (m, nbytes, 1, multibyte);
5845 message2_nolog (m, nbytes, multibyte);
5846 }
5847
5848
5849 /* The non-logging counterpart of message2. */
5850
5851 void
5852 message2_nolog (m, nbytes, multibyte)
5853 char *m;
5854 int nbytes;
5855 {
5856 struct frame *sf = SELECTED_FRAME ();
5857 message_enable_multibyte = multibyte;
5858
5859 if (noninteractive)
5860 {
5861 if (noninteractive_need_newline)
5862 putc ('\n', stderr);
5863 noninteractive_need_newline = 0;
5864 if (m)
5865 fwrite (m, nbytes, 1, stderr);
5866 if (cursor_in_echo_area == 0)
5867 fprintf (stderr, "\n");
5868 fflush (stderr);
5869 }
5870 /* A null message buffer means that the frame hasn't really been
5871 initialized yet. Error messages get reported properly by
5872 cmd_error, so this must be just an informative message; toss it. */
5873 else if (INTERACTIVE
5874 && sf->glyphs_initialized_p
5875 && FRAME_MESSAGE_BUF (sf))
5876 {
5877 Lisp_Object mini_window;
5878 struct frame *f;
5879
5880 /* Get the frame containing the mini-buffer
5881 that the selected frame is using. */
5882 mini_window = FRAME_MINIBUF_WINDOW (sf);
5883 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5884
5885 FRAME_SAMPLE_VISIBILITY (f);
5886 if (FRAME_VISIBLE_P (sf)
5887 && ! FRAME_VISIBLE_P (f))
5888 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5889
5890 if (m)
5891 {
5892 set_message (m, Qnil, nbytes, multibyte);
5893 if (minibuffer_auto_raise)
5894 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5895 }
5896 else
5897 clear_message (1, 1);
5898
5899 do_pending_window_change (0);
5900 echo_area_display (1);
5901 do_pending_window_change (0);
5902 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5903 (*frame_up_to_date_hook) (f);
5904 }
5905 }
5906
5907
5908 /* Display an echo area message M with a specified length of NBYTES
5909 bytes. The string may include null characters. If M is not a
5910 string, clear out any existing message, and let the mini-buffer
5911 text show through. */
5912
5913 void
5914 message3 (m, nbytes, multibyte)
5915 Lisp_Object m;
5916 int nbytes;
5917 int multibyte;
5918 {
5919 struct gcpro gcpro1;
5920
5921 GCPRO1 (m);
5922
5923 /* First flush out any partial line written with print. */
5924 message_log_maybe_newline ();
5925 if (STRINGP (m))
5926 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5927 message3_nolog (m, nbytes, multibyte);
5928
5929 UNGCPRO;
5930 }
5931
5932
5933 /* The non-logging version of message3. */
5934
5935 void
5936 message3_nolog (m, nbytes, multibyte)
5937 Lisp_Object m;
5938 int nbytes, multibyte;
5939 {
5940 struct frame *sf = SELECTED_FRAME ();
5941 message_enable_multibyte = multibyte;
5942
5943 if (noninteractive)
5944 {
5945 if (noninteractive_need_newline)
5946 putc ('\n', stderr);
5947 noninteractive_need_newline = 0;
5948 if (STRINGP (m))
5949 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5950 if (cursor_in_echo_area == 0)
5951 fprintf (stderr, "\n");
5952 fflush (stderr);
5953 }
5954 /* A null message buffer means that the frame hasn't really been
5955 initialized yet. Error messages get reported properly by
5956 cmd_error, so this must be just an informative message; toss it. */
5957 else if (INTERACTIVE
5958 && sf->glyphs_initialized_p
5959 && FRAME_MESSAGE_BUF (sf))
5960 {
5961 Lisp_Object mini_window;
5962 Lisp_Object frame;
5963 struct frame *f;
5964
5965 /* Get the frame containing the mini-buffer
5966 that the selected frame is using. */
5967 mini_window = FRAME_MINIBUF_WINDOW (sf);
5968 frame = XWINDOW (mini_window)->frame;
5969 f = XFRAME (frame);
5970
5971 FRAME_SAMPLE_VISIBILITY (f);
5972 if (FRAME_VISIBLE_P (sf)
5973 && !FRAME_VISIBLE_P (f))
5974 Fmake_frame_visible (frame);
5975
5976 if (STRINGP (m) && XSTRING (m)->size)
5977 {
5978 set_message (NULL, m, nbytes, multibyte);
5979 if (minibuffer_auto_raise)
5980 Fraise_frame (frame);
5981 }
5982 else
5983 clear_message (1, 1);
5984
5985 do_pending_window_change (0);
5986 echo_area_display (1);
5987 do_pending_window_change (0);
5988 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5989 (*frame_up_to_date_hook) (f);
5990 }
5991 }
5992
5993
5994 /* Display a null-terminated echo area message M. If M is 0, clear
5995 out any existing message, and let the mini-buffer text show through.
5996
5997 The buffer M must continue to exist until after the echo area gets
5998 cleared or some other message gets displayed there. Do not pass
5999 text that is stored in a Lisp string. Do not pass text in a buffer
6000 that was alloca'd. */
6001
6002 void
6003 message1 (m)
6004 char *m;
6005 {
6006 message2 (m, (m ? strlen (m) : 0), 0);
6007 }
6008
6009
6010 /* The non-logging counterpart of message1. */
6011
6012 void
6013 message1_nolog (m)
6014 char *m;
6015 {
6016 message2_nolog (m, (m ? strlen (m) : 0), 0);
6017 }
6018
6019 /* Display a message M which contains a single %s
6020 which gets replaced with STRING. */
6021
6022 void
6023 message_with_string (m, string, log)
6024 char *m;
6025 Lisp_Object string;
6026 int log;
6027 {
6028 if (noninteractive)
6029 {
6030 if (m)
6031 {
6032 if (noninteractive_need_newline)
6033 putc ('\n', stderr);
6034 noninteractive_need_newline = 0;
6035 fprintf (stderr, m, XSTRING (string)->data);
6036 if (cursor_in_echo_area == 0)
6037 fprintf (stderr, "\n");
6038 fflush (stderr);
6039 }
6040 }
6041 else if (INTERACTIVE)
6042 {
6043 /* The frame whose minibuffer we're going to display the message on.
6044 It may be larger than the selected frame, so we need
6045 to use its buffer, not the selected frame's buffer. */
6046 Lisp_Object mini_window;
6047 struct frame *f, *sf = SELECTED_FRAME ();
6048
6049 /* Get the frame containing the minibuffer
6050 that the selected frame is using. */
6051 mini_window = FRAME_MINIBUF_WINDOW (sf);
6052 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6053
6054 /* A null message buffer means that the frame hasn't really been
6055 initialized yet. Error messages get reported properly by
6056 cmd_error, so this must be just an informative message; toss it. */
6057 if (FRAME_MESSAGE_BUF (f))
6058 {
6059 int len;
6060 char *a[1];
6061 a[0] = (char *) XSTRING (string)->data;
6062
6063 len = doprnt (FRAME_MESSAGE_BUF (f),
6064 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6065
6066 if (log)
6067 message2 (FRAME_MESSAGE_BUF (f), len,
6068 STRING_MULTIBYTE (string));
6069 else
6070 message2_nolog (FRAME_MESSAGE_BUF (f), len,
6071 STRING_MULTIBYTE (string));
6072
6073 /* Print should start at the beginning of the message
6074 buffer next time. */
6075 message_buf_print = 0;
6076 }
6077 }
6078 }
6079
6080
6081 /* Dump an informative message to the minibuf. If M is 0, clear out
6082 any existing message, and let the mini-buffer text show through. */
6083
6084 /* VARARGS 1 */
6085 void
6086 message (m, a1, a2, a3)
6087 char *m;
6088 EMACS_INT a1, a2, a3;
6089 {
6090 if (noninteractive)
6091 {
6092 if (m)
6093 {
6094 if (noninteractive_need_newline)
6095 putc ('\n', stderr);
6096 noninteractive_need_newline = 0;
6097 fprintf (stderr, m, a1, a2, a3);
6098 if (cursor_in_echo_area == 0)
6099 fprintf (stderr, "\n");
6100 fflush (stderr);
6101 }
6102 }
6103 else if (INTERACTIVE)
6104 {
6105 /* The frame whose mini-buffer we're going to display the message
6106 on. It may be larger than the selected frame, so we need to
6107 use its buffer, not the selected frame's buffer. */
6108 Lisp_Object mini_window;
6109 struct frame *f, *sf = SELECTED_FRAME ();
6110
6111 /* Get the frame containing the mini-buffer
6112 that the selected frame is using. */
6113 mini_window = FRAME_MINIBUF_WINDOW (sf);
6114 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6115
6116 /* A null message buffer means that the frame hasn't really been
6117 initialized yet. Error messages get reported properly by
6118 cmd_error, so this must be just an informative message; toss
6119 it. */
6120 if (FRAME_MESSAGE_BUF (f))
6121 {
6122 if (m)
6123 {
6124 int len;
6125 #ifdef NO_ARG_ARRAY
6126 char *a[3];
6127 a[0] = (char *) a1;
6128 a[1] = (char *) a2;
6129 a[2] = (char *) a3;
6130
6131 len = doprnt (FRAME_MESSAGE_BUF (f),
6132 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6133 #else
6134 len = doprnt (FRAME_MESSAGE_BUF (f),
6135 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6136 (char **) &a1);
6137 #endif /* NO_ARG_ARRAY */
6138
6139 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6140 }
6141 else
6142 message1 (0);
6143
6144 /* Print should start at the beginning of the message
6145 buffer next time. */
6146 message_buf_print = 0;
6147 }
6148 }
6149 }
6150
6151
6152 /* The non-logging version of message. */
6153
6154 void
6155 message_nolog (m, a1, a2, a3)
6156 char *m;
6157 EMACS_INT a1, a2, a3;
6158 {
6159 Lisp_Object old_log_max;
6160 old_log_max = Vmessage_log_max;
6161 Vmessage_log_max = Qnil;
6162 message (m, a1, a2, a3);
6163 Vmessage_log_max = old_log_max;
6164 }
6165
6166
6167 /* Display the current message in the current mini-buffer. This is
6168 only called from error handlers in process.c, and is not time
6169 critical. */
6170
6171 void
6172 update_echo_area ()
6173 {
6174 if (!NILP (echo_area_buffer[0]))
6175 {
6176 Lisp_Object string;
6177 string = Fcurrent_message ();
6178 message3 (string, XSTRING (string)->size,
6179 !NILP (current_buffer->enable_multibyte_characters));
6180 }
6181 }
6182
6183
6184 /* Make sure echo area buffers in echo_buffers[] are life. If they
6185 aren't, make new ones. */
6186
6187 static void
6188 ensure_echo_area_buffers ()
6189 {
6190 int i;
6191
6192 for (i = 0; i < 2; ++i)
6193 if (!BUFFERP (echo_buffer[i])
6194 || NILP (XBUFFER (echo_buffer[i])->name))
6195 {
6196 char name[30];
6197 Lisp_Object old_buffer;
6198 int j;
6199
6200 old_buffer = echo_buffer[i];
6201 sprintf (name, " *Echo Area %d*", i);
6202 echo_buffer[i] = Fget_buffer_create (build_string (name));
6203 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6204
6205 for (j = 0; j < 2; ++j)
6206 if (EQ (old_buffer, echo_area_buffer[j]))
6207 echo_area_buffer[j] = echo_buffer[i];
6208 }
6209 }
6210
6211
6212 /* Call FN with args A1..A4 with either the current or last displayed
6213 echo_area_buffer as current buffer.
6214
6215 WHICH zero means use the current message buffer
6216 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6217 from echo_buffer[] and clear it.
6218
6219 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6220 suitable buffer from echo_buffer[] and clear it.
6221
6222 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6223 that the current message becomes the last displayed one, make
6224 choose a suitable buffer for echo_area_buffer[0], and clear it.
6225
6226 Value is what FN returns. */
6227
6228 static int
6229 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6230 struct window *w;
6231 int which;
6232 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6233 EMACS_INT a1;
6234 Lisp_Object a2;
6235 EMACS_INT a3, a4;
6236 {
6237 Lisp_Object buffer;
6238 int this_one, the_other, clear_buffer_p, rc;
6239 int count = BINDING_STACK_SIZE ();
6240
6241 /* If buffers aren't life, make new ones. */
6242 ensure_echo_area_buffers ();
6243
6244 clear_buffer_p = 0;
6245
6246 if (which == 0)
6247 this_one = 0, the_other = 1;
6248 else if (which > 0)
6249 this_one = 1, the_other = 0;
6250 else
6251 {
6252 this_one = 0, the_other = 1;
6253 clear_buffer_p = 1;
6254
6255 /* We need a fresh one in case the current echo buffer equals
6256 the one containing the last displayed echo area message. */
6257 if (!NILP (echo_area_buffer[this_one])
6258 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6259 echo_area_buffer[this_one] = Qnil;
6260 }
6261
6262 /* Choose a suitable buffer from echo_buffer[] is we don't
6263 have one. */
6264 if (NILP (echo_area_buffer[this_one]))
6265 {
6266 echo_area_buffer[this_one]
6267 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6268 ? echo_buffer[the_other]
6269 : echo_buffer[this_one]);
6270 clear_buffer_p = 1;
6271 }
6272
6273 buffer = echo_area_buffer[this_one];
6274
6275 /* Don't get confused by reusing the buffer used for echoing
6276 for a different purpose. */
6277 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6278 cancel_echoing ();
6279
6280 record_unwind_protect (unwind_with_echo_area_buffer,
6281 with_echo_area_buffer_unwind_data (w));
6282
6283 /* Make the echo area buffer current. Note that for display
6284 purposes, it is not necessary that the displayed window's buffer
6285 == current_buffer, except for text property lookup. So, let's
6286 only set that buffer temporarily here without doing a full
6287 Fset_window_buffer. We must also change w->pointm, though,
6288 because otherwise an assertions in unshow_buffer fails, and Emacs
6289 aborts. */
6290 set_buffer_internal_1 (XBUFFER (buffer));
6291 if (w)
6292 {
6293 w->buffer = buffer;
6294 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6295 }
6296
6297 current_buffer->undo_list = Qt;
6298 current_buffer->read_only = Qnil;
6299 specbind (Qinhibit_read_only, Qt);
6300 specbind (Qinhibit_modification_hooks, Qt);
6301
6302 if (clear_buffer_p && Z > BEG)
6303 del_range (BEG, Z);
6304
6305 xassert (BEGV >= BEG);
6306 xassert (ZV <= Z && ZV >= BEGV);
6307
6308 rc = fn (a1, a2, a3, a4);
6309
6310 xassert (BEGV >= BEG);
6311 xassert (ZV <= Z && ZV >= BEGV);
6312
6313 unbind_to (count, Qnil);
6314 return rc;
6315 }
6316
6317
6318 /* Save state that should be preserved around the call to the function
6319 FN called in with_echo_area_buffer. */
6320
6321 static Lisp_Object
6322 with_echo_area_buffer_unwind_data (w)
6323 struct window *w;
6324 {
6325 int i = 0;
6326 Lisp_Object vector;
6327
6328 /* Reduce consing by keeping one vector in
6329 Vwith_echo_area_save_vector. */
6330 vector = Vwith_echo_area_save_vector;
6331 Vwith_echo_area_save_vector = Qnil;
6332
6333 if (NILP (vector))
6334 vector = Fmake_vector (make_number (7), Qnil);
6335
6336 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6337 AREF (vector, i) = Vdeactivate_mark, ++i;
6338 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6339
6340 if (w)
6341 {
6342 XSETWINDOW (AREF (vector, i), w); ++i;
6343 AREF (vector, i) = w->buffer; ++i;
6344 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6345 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6346 }
6347 else
6348 {
6349 int end = i + 4;
6350 for (; i < end; ++i)
6351 AREF (vector, i) = Qnil;
6352 }
6353
6354 xassert (i == ASIZE (vector));
6355 return vector;
6356 }
6357
6358
6359 /* Restore global state from VECTOR which was created by
6360 with_echo_area_buffer_unwind_data. */
6361
6362 static Lisp_Object
6363 unwind_with_echo_area_buffer (vector)
6364 Lisp_Object vector;
6365 {
6366 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6367 Vdeactivate_mark = AREF (vector, 1);
6368 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6369
6370 if (WINDOWP (AREF (vector, 3)))
6371 {
6372 struct window *w;
6373 Lisp_Object buffer, charpos, bytepos;
6374
6375 w = XWINDOW (AREF (vector, 3));
6376 buffer = AREF (vector, 4);
6377 charpos = AREF (vector, 5);
6378 bytepos = AREF (vector, 6);
6379
6380 w->buffer = buffer;
6381 set_marker_both (w->pointm, buffer,
6382 XFASTINT (charpos), XFASTINT (bytepos));
6383 }
6384
6385 Vwith_echo_area_save_vector = vector;
6386 return Qnil;
6387 }
6388
6389
6390 /* Set up the echo area for use by print functions. MULTIBYTE_P
6391 non-zero means we will print multibyte. */
6392
6393 void
6394 setup_echo_area_for_printing (multibyte_p)
6395 int multibyte_p;
6396 {
6397 ensure_echo_area_buffers ();
6398
6399 if (!message_buf_print)
6400 {
6401 /* A message has been output since the last time we printed.
6402 Choose a fresh echo area buffer. */
6403 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6404 echo_area_buffer[0] = echo_buffer[1];
6405 else
6406 echo_area_buffer[0] = echo_buffer[0];
6407
6408 /* Switch to that buffer and clear it. */
6409 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6410 current_buffer->truncate_lines = Qnil;
6411
6412 if (Z > BEG)
6413 {
6414 int count = BINDING_STACK_SIZE ();
6415 specbind (Qinhibit_read_only, Qt);
6416 del_range (BEG, Z);
6417 unbind_to (count, Qnil);
6418 }
6419 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6420
6421 /* Set up the buffer for the multibyteness we need. */
6422 if (multibyte_p
6423 != !NILP (current_buffer->enable_multibyte_characters))
6424 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6425
6426 /* Raise the frame containing the echo area. */
6427 if (minibuffer_auto_raise)
6428 {
6429 struct frame *sf = SELECTED_FRAME ();
6430 Lisp_Object mini_window;
6431 mini_window = FRAME_MINIBUF_WINDOW (sf);
6432 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6433 }
6434
6435 message_log_maybe_newline ();
6436 message_buf_print = 1;
6437 }
6438 else
6439 {
6440 if (NILP (echo_area_buffer[0]))
6441 {
6442 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6443 echo_area_buffer[0] = echo_buffer[1];
6444 else
6445 echo_area_buffer[0] = echo_buffer[0];
6446 }
6447
6448 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6449 {
6450 /* Someone switched buffers between print requests. */
6451 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6452 current_buffer->truncate_lines = Qnil;
6453 }
6454 }
6455 }
6456
6457
6458 /* Display an echo area message in window W. Value is non-zero if W's
6459 height is changed. If display_last_displayed_message_p is
6460 non-zero, display the message that was last displayed, otherwise
6461 display the current message. */
6462
6463 static int
6464 display_echo_area (w)
6465 struct window *w;
6466 {
6467 int i, no_message_p, window_height_changed_p, count;
6468
6469 /* Temporarily disable garbage collections while displaying the echo
6470 area. This is done because a GC can print a message itself.
6471 That message would modify the echo area buffer's contents while a
6472 redisplay of the buffer is going on, and seriously confuse
6473 redisplay. */
6474 count = inhibit_garbage_collection ();
6475
6476 /* If there is no message, we must call display_echo_area_1
6477 nevertheless because it resizes the window. But we will have to
6478 reset the echo_area_buffer in question to nil at the end because
6479 with_echo_area_buffer will sets it to an empty buffer. */
6480 i = display_last_displayed_message_p ? 1 : 0;
6481 no_message_p = NILP (echo_area_buffer[i]);
6482
6483 window_height_changed_p
6484 = with_echo_area_buffer (w, display_last_displayed_message_p,
6485 display_echo_area_1,
6486 (EMACS_INT) w, Qnil, 0, 0);
6487
6488 if (no_message_p)
6489 echo_area_buffer[i] = Qnil;
6490
6491 unbind_to (count, Qnil);
6492 return window_height_changed_p;
6493 }
6494
6495
6496 /* Helper for display_echo_area. Display the current buffer which
6497 contains the current echo area message in window W, a mini-window,
6498 a pointer to which is passed in A1. A2..A4 are currently not used.
6499 Change the height of W so that all of the message is displayed.
6500 Value is non-zero if height of W was changed. */
6501
6502 static int
6503 display_echo_area_1 (a1, a2, a3, a4)
6504 EMACS_INT a1;
6505 Lisp_Object a2;
6506 EMACS_INT a3, a4;
6507 {
6508 struct window *w = (struct window *) a1;
6509 Lisp_Object window;
6510 struct text_pos start;
6511 int window_height_changed_p = 0;
6512
6513 /* Do this before displaying, so that we have a large enough glyph
6514 matrix for the display. */
6515 window_height_changed_p = resize_mini_window (w, 0);
6516
6517 /* Display. */
6518 clear_glyph_matrix (w->desired_matrix);
6519 XSETWINDOW (window, w);
6520 SET_TEXT_POS (start, BEG, BEG_BYTE);
6521 try_window (window, start);
6522
6523 return window_height_changed_p;
6524 }
6525
6526
6527 /* Resize the echo area window to exactly the size needed for the
6528 currently displayed message, if there is one. If a mini-buffer
6529 is active, don't shrink it. */
6530
6531 void
6532 resize_echo_area_exactly ()
6533 {
6534 if (BUFFERP (echo_area_buffer[0])
6535 && WINDOWP (echo_area_window))
6536 {
6537 struct window *w = XWINDOW (echo_area_window);
6538 int resized_p;
6539 Lisp_Object resize_exactly;
6540
6541 if (minibuf_level == 0)
6542 resize_exactly = Qt;
6543 else
6544 resize_exactly = Qnil;
6545
6546 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6547 (EMACS_INT) w, resize_exactly, 0, 0);
6548 if (resized_p)
6549 {
6550 ++windows_or_buffers_changed;
6551 ++update_mode_lines;
6552 redisplay_internal (0);
6553 }
6554 }
6555 }
6556
6557
6558 /* Callback function for with_echo_area_buffer, when used from
6559 resize_echo_area_exactly. A1 contains a pointer to the window to
6560 resize, EXACTLY non-nil means resize the mini-window exactly to the
6561 size of the text displayed. A3 and A4 are not used. Value is what
6562 resize_mini_window returns. */
6563
6564 static int
6565 resize_mini_window_1 (a1, exactly, a3, a4)
6566 EMACS_INT a1;
6567 Lisp_Object exactly;
6568 EMACS_INT a3, a4;
6569 {
6570 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6571 }
6572
6573
6574 /* Resize mini-window W to fit the size of its contents. EXACT:P
6575 means size the window exactly to the size needed. Otherwise, it's
6576 only enlarged until W's buffer is empty. Value is non-zero if
6577 the window height has been changed. */
6578
6579 int
6580 resize_mini_window (w, exact_p)
6581 struct window *w;
6582 int exact_p;
6583 {
6584 struct frame *f = XFRAME (w->frame);
6585 int window_height_changed_p = 0;
6586
6587 xassert (MINI_WINDOW_P (w));
6588
6589 /* Don't resize windows while redisplaying a window; it would
6590 confuse redisplay functions when the size of the window they are
6591 displaying changes from under them. Such a resizing can happen,
6592 for instance, when which-func prints a long message while
6593 we are running fontification-functions. We're running these
6594 functions with safe_call which binds inhibit-redisplay to t. */
6595 if (!NILP (Vinhibit_redisplay))
6596 return 0;
6597
6598 /* Nil means don't try to resize. */
6599 if (NILP (Vresize_mini_windows)
6600 || (FRAME_X_P (f) && f->output_data.x == NULL))
6601 return 0;
6602
6603 if (!FRAME_MINIBUF_ONLY_P (f))
6604 {
6605 struct it it;
6606 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6607 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6608 int height, max_height;
6609 int unit = CANON_Y_UNIT (f);
6610 struct text_pos start;
6611 struct buffer *old_current_buffer = NULL;
6612
6613 if (current_buffer != XBUFFER (w->buffer))
6614 {
6615 old_current_buffer = current_buffer;
6616 set_buffer_internal (XBUFFER (w->buffer));
6617 }
6618
6619 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6620
6621 /* Compute the max. number of lines specified by the user. */
6622 if (FLOATP (Vmax_mini_window_height))
6623 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6624 else if (INTEGERP (Vmax_mini_window_height))
6625 max_height = XINT (Vmax_mini_window_height);
6626 else
6627 max_height = total_height / 4;
6628
6629 /* Correct that max. height if it's bogus. */
6630 max_height = max (1, max_height);
6631 max_height = min (total_height, max_height);
6632
6633 /* Find out the height of the text in the window. */
6634 if (it.truncate_lines_p)
6635 height = 1;
6636 else
6637 {
6638 last_height = 0;
6639 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6640 if (it.max_ascent == 0 && it.max_descent == 0)
6641 height = it.current_y + last_height;
6642 else
6643 height = it.current_y + it.max_ascent + it.max_descent;
6644 height -= it.extra_line_spacing;
6645 height = (height + unit - 1) / unit;
6646 }
6647
6648 /* Compute a suitable window start. */
6649 if (height > max_height)
6650 {
6651 height = max_height;
6652 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6653 move_it_vertically_backward (&it, (height - 1) * unit);
6654 start = it.current.pos;
6655 }
6656 else
6657 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6658 SET_MARKER_FROM_TEXT_POS (w->start, start);
6659
6660 if (EQ (Vresize_mini_windows, Qgrow_only))
6661 {
6662 /* Let it grow only, until we display an empty message, in which
6663 case the window shrinks again. */
6664 if (height > XFASTINT (w->height))
6665 {
6666 int old_height = XFASTINT (w->height);
6667 freeze_window_starts (f, 1);
6668 grow_mini_window (w, height - XFASTINT (w->height));
6669 window_height_changed_p = XFASTINT (w->height) != old_height;
6670 }
6671 else if (height < XFASTINT (w->height)
6672 && (exact_p || BEGV == ZV))
6673 {
6674 int old_height = XFASTINT (w->height);
6675 freeze_window_starts (f, 0);
6676 shrink_mini_window (w);
6677 window_height_changed_p = XFASTINT (w->height) != old_height;
6678 }
6679 }
6680 else
6681 {
6682 /* Always resize to exact size needed. */
6683 if (height > XFASTINT (w->height))
6684 {
6685 int old_height = XFASTINT (w->height);
6686 freeze_window_starts (f, 1);
6687 grow_mini_window (w, height - XFASTINT (w->height));
6688 window_height_changed_p = XFASTINT (w->height) != old_height;
6689 }
6690 else if (height < XFASTINT (w->height))
6691 {
6692 int old_height = XFASTINT (w->height);
6693 freeze_window_starts (f, 0);
6694 shrink_mini_window (w);
6695
6696 if (height)
6697 {
6698 freeze_window_starts (f, 1);
6699 grow_mini_window (w, height - XFASTINT (w->height));
6700 }
6701
6702 window_height_changed_p = XFASTINT (w->height) != old_height;
6703 }
6704 }
6705
6706 if (old_current_buffer)
6707 set_buffer_internal (old_current_buffer);
6708 }
6709
6710 return window_height_changed_p;
6711 }
6712
6713
6714 /* Value is the current message, a string, or nil if there is no
6715 current message. */
6716
6717 Lisp_Object
6718 current_message ()
6719 {
6720 Lisp_Object msg;
6721
6722 if (NILP (echo_area_buffer[0]))
6723 msg = Qnil;
6724 else
6725 {
6726 with_echo_area_buffer (0, 0, current_message_1,
6727 (EMACS_INT) &msg, Qnil, 0, 0);
6728 if (NILP (msg))
6729 echo_area_buffer[0] = Qnil;
6730 }
6731
6732 return msg;
6733 }
6734
6735
6736 static int
6737 current_message_1 (a1, a2, a3, a4)
6738 EMACS_INT a1;
6739 Lisp_Object a2;
6740 EMACS_INT a3, a4;
6741 {
6742 Lisp_Object *msg = (Lisp_Object *) a1;
6743
6744 if (Z > BEG)
6745 *msg = make_buffer_string (BEG, Z, 1);
6746 else
6747 *msg = Qnil;
6748 return 0;
6749 }
6750
6751
6752 /* Push the current message on Vmessage_stack for later restauration
6753 by restore_message. Value is non-zero if the current message isn't
6754 empty. This is a relatively infrequent operation, so it's not
6755 worth optimizing. */
6756
6757 int
6758 push_message ()
6759 {
6760 Lisp_Object msg;
6761 msg = current_message ();
6762 Vmessage_stack = Fcons (msg, Vmessage_stack);
6763 return STRINGP (msg);
6764 }
6765
6766
6767 /* Handler for record_unwind_protect calling pop_message. */
6768
6769 Lisp_Object
6770 push_message_unwind (dummy)
6771 Lisp_Object dummy;
6772 {
6773 pop_message ();
6774 return Qnil;
6775 }
6776
6777
6778 /* Restore message display from the top of Vmessage_stack. */
6779
6780 void
6781 restore_message ()
6782 {
6783 Lisp_Object msg;
6784
6785 xassert (CONSP (Vmessage_stack));
6786 msg = XCAR (Vmessage_stack);
6787 if (STRINGP (msg))
6788 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6789 else
6790 message3_nolog (msg, 0, 0);
6791 }
6792
6793
6794 /* Pop the top-most entry off Vmessage_stack. */
6795
6796 void
6797 pop_message ()
6798 {
6799 xassert (CONSP (Vmessage_stack));
6800 Vmessage_stack = XCDR (Vmessage_stack);
6801 }
6802
6803
6804 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6805 exits. If the stack is not empty, we have a missing pop_message
6806 somewhere. */
6807
6808 void
6809 check_message_stack ()
6810 {
6811 if (!NILP (Vmessage_stack))
6812 abort ();
6813 }
6814
6815
6816 /* Truncate to NCHARS what will be displayed in the echo area the next
6817 time we display it---but don't redisplay it now. */
6818
6819 void
6820 truncate_echo_area (nchars)
6821 int nchars;
6822 {
6823 if (nchars == 0)
6824 echo_area_buffer[0] = Qnil;
6825 /* A null message buffer means that the frame hasn't really been
6826 initialized yet. Error messages get reported properly by
6827 cmd_error, so this must be just an informative message; toss it. */
6828 else if (!noninteractive
6829 && INTERACTIVE
6830 && !NILP (echo_area_buffer[0]))
6831 {
6832 struct frame *sf = SELECTED_FRAME ();
6833 if (FRAME_MESSAGE_BUF (sf))
6834 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6835 }
6836 }
6837
6838
6839 /* Helper function for truncate_echo_area. Truncate the current
6840 message to at most NCHARS characters. */
6841
6842 static int
6843 truncate_message_1 (nchars, a2, a3, a4)
6844 EMACS_INT nchars;
6845 Lisp_Object a2;
6846 EMACS_INT a3, a4;
6847 {
6848 if (BEG + nchars < Z)
6849 del_range (BEG + nchars, Z);
6850 if (Z == BEG)
6851 echo_area_buffer[0] = Qnil;
6852 return 0;
6853 }
6854
6855
6856 /* Set the current message to a substring of S or STRING.
6857
6858 If STRING is a Lisp string, set the message to the first NBYTES
6859 bytes from STRING. NBYTES zero means use the whole string. If
6860 STRING is multibyte, the message will be displayed multibyte.
6861
6862 If S is not null, set the message to the first LEN bytes of S. LEN
6863 zero means use the whole string. MULTIBYTE_P non-zero means S is
6864 multibyte. Display the message multibyte in that case. */
6865
6866 void
6867 set_message (s, string, nbytes, multibyte_p)
6868 char *s;
6869 Lisp_Object string;
6870 int nbytes;
6871 {
6872 message_enable_multibyte
6873 = ((s && multibyte_p)
6874 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6875
6876 with_echo_area_buffer (0, -1, set_message_1,
6877 (EMACS_INT) s, string, nbytes, multibyte_p);
6878 message_buf_print = 0;
6879 help_echo_showing_p = 0;
6880 }
6881
6882
6883 /* Helper function for set_message. Arguments have the same meaning
6884 as there, with A1 corresponding to S and A2 corresponding to STRING
6885 This function is called with the echo area buffer being
6886 current. */
6887
6888 static int
6889 set_message_1 (a1, a2, nbytes, multibyte_p)
6890 EMACS_INT a1;
6891 Lisp_Object a2;
6892 EMACS_INT nbytes, multibyte_p;
6893 {
6894 char *s = (char *) a1;
6895 Lisp_Object string = a2;
6896
6897 xassert (BEG == Z);
6898
6899 /* Change multibyteness of the echo buffer appropriately. */
6900 if (message_enable_multibyte
6901 != !NILP (current_buffer->enable_multibyte_characters))
6902 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6903
6904 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6905
6906 /* Insert new message at BEG. */
6907 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6908
6909 if (STRINGP (string))
6910 {
6911 int nchars;
6912
6913 if (nbytes == 0)
6914 nbytes = XSTRING (string)->size_byte;
6915 nchars = string_byte_to_char (string, nbytes);
6916
6917 /* This function takes care of single/multibyte conversion. We
6918 just have to ensure that the echo area buffer has the right
6919 setting of enable_multibyte_characters. */
6920 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6921 }
6922 else if (s)
6923 {
6924 if (nbytes == 0)
6925 nbytes = strlen (s);
6926
6927 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6928 {
6929 /* Convert from multi-byte to single-byte. */
6930 int i, c, n;
6931 unsigned char work[1];
6932
6933 /* Convert a multibyte string to single-byte. */
6934 for (i = 0; i < nbytes; i += n)
6935 {
6936 c = string_char_and_length (s + i, nbytes - i, &n);
6937 work[0] = (SINGLE_BYTE_CHAR_P (c)
6938 ? c
6939 : multibyte_char_to_unibyte (c, Qnil));
6940 insert_1_both (work, 1, 1, 1, 0, 0);
6941 }
6942 }
6943 else if (!multibyte_p
6944 && !NILP (current_buffer->enable_multibyte_characters))
6945 {
6946 /* Convert from single-byte to multi-byte. */
6947 int i, c, n;
6948 unsigned char *msg = (unsigned char *) s;
6949 unsigned char str[MAX_MULTIBYTE_LENGTH];
6950
6951 /* Convert a single-byte string to multibyte. */
6952 for (i = 0; i < nbytes; i++)
6953 {
6954 c = unibyte_char_to_multibyte (msg[i]);
6955 n = CHAR_STRING (c, str);
6956 insert_1_both (str, 1, n, 1, 0, 0);
6957 }
6958 }
6959 else
6960 insert_1 (s, nbytes, 1, 0, 0);
6961 }
6962
6963 return 0;
6964 }
6965
6966
6967 /* Clear messages. CURRENT_P non-zero means clear the current
6968 message. LAST_DISPLAYED_P non-zero means clear the message
6969 last displayed. */
6970
6971 void
6972 clear_message (current_p, last_displayed_p)
6973 int current_p, last_displayed_p;
6974 {
6975 if (current_p)
6976 {
6977 echo_area_buffer[0] = Qnil;
6978 message_cleared_p = 1;
6979 }
6980
6981 if (last_displayed_p)
6982 echo_area_buffer[1] = Qnil;
6983
6984 message_buf_print = 0;
6985 }
6986
6987 /* Clear garbaged frames.
6988
6989 This function is used where the old redisplay called
6990 redraw_garbaged_frames which in turn called redraw_frame which in
6991 turn called clear_frame. The call to clear_frame was a source of
6992 flickering. I believe a clear_frame is not necessary. It should
6993 suffice in the new redisplay to invalidate all current matrices,
6994 and ensure a complete redisplay of all windows. */
6995
6996 static void
6997 clear_garbaged_frames ()
6998 {
6999 if (frame_garbaged)
7000 {
7001 Lisp_Object tail, frame;
7002
7003 FOR_EACH_FRAME (tail, frame)
7004 {
7005 struct frame *f = XFRAME (frame);
7006
7007 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7008 {
7009 if (f->resized_p)
7010 Fredraw_frame (frame);
7011 clear_current_matrices (f);
7012 f->garbaged = 0;
7013 f->resized_p = 0;
7014 }
7015 }
7016
7017 frame_garbaged = 0;
7018 ++windows_or_buffers_changed;
7019 }
7020 }
7021
7022
7023 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7024 is non-zero update selected_frame. Value is non-zero if the
7025 mini-windows height has been changed. */
7026
7027 static int
7028 echo_area_display (update_frame_p)
7029 int update_frame_p;
7030 {
7031 Lisp_Object mini_window;
7032 struct window *w;
7033 struct frame *f;
7034 int window_height_changed_p = 0;
7035 struct frame *sf = SELECTED_FRAME ();
7036
7037 mini_window = FRAME_MINIBUF_WINDOW (sf);
7038 w = XWINDOW (mini_window);
7039 f = XFRAME (WINDOW_FRAME (w));
7040
7041 /* Don't display if frame is invisible or not yet initialized. */
7042 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7043 return 0;
7044
7045 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7046 #ifndef macintosh
7047 #ifdef HAVE_WINDOW_SYSTEM
7048 /* When Emacs starts, selected_frame may be a visible terminal
7049 frame, even if we run under a window system. If we let this
7050 through, a message would be displayed on the terminal. */
7051 if (EQ (selected_frame, Vterminal_frame)
7052 && !NILP (Vwindow_system))
7053 return 0;
7054 #endif /* HAVE_WINDOW_SYSTEM */
7055 #endif
7056
7057 /* Redraw garbaged frames. */
7058 if (frame_garbaged)
7059 clear_garbaged_frames ();
7060
7061 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7062 {
7063 echo_area_window = mini_window;
7064 window_height_changed_p = display_echo_area (w);
7065 w->must_be_updated_p = 1;
7066
7067 /* Update the display, unless called from redisplay_internal.
7068 Also don't update the screen during redisplay itself. The
7069 update will happen at the end of redisplay, and an update
7070 here could cause confusion. */
7071 if (update_frame_p && !redisplaying_p)
7072 {
7073 int n = 0;
7074
7075 /* If the display update has been interrupted by pending
7076 input, update mode lines in the frame. Due to the
7077 pending input, it might have been that redisplay hasn't
7078 been called, so that mode lines above the echo area are
7079 garbaged. This looks odd, so we prevent it here. */
7080 if (!display_completed)
7081 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7082
7083 if (window_height_changed_p
7084 /* Don't do this if Emacs is shutting down. Redisplay
7085 needs to run hooks. */
7086 && !NILP (Vrun_hooks))
7087 {
7088 /* Must update other windows. Likewise as in other
7089 cases, don't let this update be interrupted by
7090 pending input. */
7091 int count = BINDING_STACK_SIZE ();
7092 specbind (Qredisplay_dont_pause, Qt);
7093 windows_or_buffers_changed = 1;
7094 redisplay_internal (0);
7095 unbind_to (count, Qnil);
7096 }
7097 else if (FRAME_WINDOW_P (f) && n == 0)
7098 {
7099 /* Window configuration is the same as before.
7100 Can do with a display update of the echo area,
7101 unless we displayed some mode lines. */
7102 update_single_window (w, 1);
7103 rif->flush_display (f);
7104 }
7105 else
7106 update_frame (f, 1, 1);
7107
7108 /* If cursor is in the echo area, make sure that the next
7109 redisplay displays the minibuffer, so that the cursor will
7110 be replaced with what the minibuffer wants. */
7111 if (cursor_in_echo_area)
7112 ++windows_or_buffers_changed;
7113 }
7114 }
7115 else if (!EQ (mini_window, selected_window))
7116 windows_or_buffers_changed++;
7117
7118 /* Last displayed message is now the current message. */
7119 echo_area_buffer[1] = echo_area_buffer[0];
7120
7121 /* Prevent redisplay optimization in redisplay_internal by resetting
7122 this_line_start_pos. This is done because the mini-buffer now
7123 displays the message instead of its buffer text. */
7124 if (EQ (mini_window, selected_window))
7125 CHARPOS (this_line_start_pos) = 0;
7126
7127 return window_height_changed_p;
7128 }
7129
7130
7131 \f
7132 /***********************************************************************
7133 Frame Titles
7134 ***********************************************************************/
7135
7136
7137 #ifdef HAVE_WINDOW_SYSTEM
7138
7139 /* A buffer for constructing frame titles in it; allocated from the
7140 heap in init_xdisp and resized as needed in store_frame_title_char. */
7141
7142 static char *frame_title_buf;
7143
7144 /* The buffer's end, and a current output position in it. */
7145
7146 static char *frame_title_buf_end;
7147 static char *frame_title_ptr;
7148
7149
7150 /* Store a single character C for the frame title in frame_title_buf.
7151 Re-allocate frame_title_buf if necessary. */
7152
7153 static void
7154 store_frame_title_char (c)
7155 char c;
7156 {
7157 /* If output position has reached the end of the allocated buffer,
7158 double the buffer's size. */
7159 if (frame_title_ptr == frame_title_buf_end)
7160 {
7161 int len = frame_title_ptr - frame_title_buf;
7162 int new_size = 2 * len * sizeof *frame_title_buf;
7163 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7164 frame_title_buf_end = frame_title_buf + new_size;
7165 frame_title_ptr = frame_title_buf + len;
7166 }
7167
7168 *frame_title_ptr++ = c;
7169 }
7170
7171
7172 /* Store part of a frame title in frame_title_buf, beginning at
7173 frame_title_ptr. STR is the string to store. Do not copy
7174 characters that yield more columns than PRECISION; PRECISION <= 0
7175 means copy the whole string. Pad with spaces until FIELD_WIDTH
7176 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7177 pad. Called from display_mode_element when it is used to build a
7178 frame title. */
7179
7180 static int
7181 store_frame_title (str, field_width, precision)
7182 unsigned char *str;
7183 int field_width, precision;
7184 {
7185 int n = 0;
7186 int dummy, nbytes;
7187
7188 /* Copy at most PRECISION chars from STR. */
7189 nbytes = strlen (str);
7190 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7191 while (nbytes--)
7192 store_frame_title_char (*str++);
7193
7194 /* Fill up with spaces until FIELD_WIDTH reached. */
7195 while (field_width > 0
7196 && n < field_width)
7197 {
7198 store_frame_title_char (' ');
7199 ++n;
7200 }
7201
7202 return n;
7203 }
7204
7205
7206 /* Set the title of FRAME, if it has changed. The title format is
7207 Vicon_title_format if FRAME is iconified, otherwise it is
7208 frame_title_format. */
7209
7210 static void
7211 x_consider_frame_title (frame)
7212 Lisp_Object frame;
7213 {
7214 struct frame *f = XFRAME (frame);
7215
7216 if (FRAME_WINDOW_P (f)
7217 || FRAME_MINIBUF_ONLY_P (f)
7218 || f->explicit_name)
7219 {
7220 /* Do we have more than one visible frame on this X display? */
7221 Lisp_Object tail;
7222 Lisp_Object fmt;
7223 struct buffer *obuf;
7224 int len;
7225 struct it it;
7226
7227 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7228 {
7229 Lisp_Object other_frame = XCAR (tail);
7230 struct frame *tf = XFRAME (other_frame);
7231
7232 if (tf != f
7233 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7234 && !FRAME_MINIBUF_ONLY_P (tf)
7235 && !EQ (other_frame, tip_frame)
7236 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7237 break;
7238 }
7239
7240 /* Set global variable indicating that multiple frames exist. */
7241 multiple_frames = CONSP (tail);
7242
7243 /* Switch to the buffer of selected window of the frame. Set up
7244 frame_title_ptr so that display_mode_element will output into it;
7245 then display the title. */
7246 obuf = current_buffer;
7247 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7248 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7249 frame_title_ptr = frame_title_buf;
7250 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7251 NULL, DEFAULT_FACE_ID);
7252 display_mode_element (&it, 0, -1, -1, fmt);
7253 len = frame_title_ptr - frame_title_buf;
7254 frame_title_ptr = NULL;
7255 set_buffer_internal_1 (obuf);
7256
7257 /* Set the title only if it's changed. This avoids consing in
7258 the common case where it hasn't. (If it turns out that we've
7259 already wasted too much time by walking through the list with
7260 display_mode_element, then we might need to optimize at a
7261 higher level than this.) */
7262 if (! STRINGP (f->name)
7263 || STRING_BYTES (XSTRING (f->name)) != len
7264 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7265 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7266 }
7267 }
7268
7269 #else /* not HAVE_WINDOW_SYSTEM */
7270
7271 #define frame_title_ptr ((char *)0)
7272 #define store_frame_title(str, mincol, maxcol) 0
7273
7274 #endif /* not HAVE_WINDOW_SYSTEM */
7275
7276
7277
7278 \f
7279 /***********************************************************************
7280 Menu Bars
7281 ***********************************************************************/
7282
7283
7284 /* Prepare for redisplay by updating menu-bar item lists when
7285 appropriate. This can call eval. */
7286
7287 void
7288 prepare_menu_bars ()
7289 {
7290 int all_windows;
7291 struct gcpro gcpro1, gcpro2;
7292 struct frame *f;
7293 Lisp_Object tooltip_frame;
7294
7295 #ifdef HAVE_WINDOW_SYSTEM
7296 tooltip_frame = tip_frame;
7297 #else
7298 tooltip_frame = Qnil;
7299 #endif
7300
7301 /* Update all frame titles based on their buffer names, etc. We do
7302 this before the menu bars so that the buffer-menu will show the
7303 up-to-date frame titles. */
7304 #ifdef HAVE_WINDOW_SYSTEM
7305 if (windows_or_buffers_changed || update_mode_lines)
7306 {
7307 Lisp_Object tail, frame;
7308
7309 FOR_EACH_FRAME (tail, frame)
7310 {
7311 f = XFRAME (frame);
7312 if (!EQ (frame, tooltip_frame)
7313 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7314 x_consider_frame_title (frame);
7315 }
7316 }
7317 #endif /* HAVE_WINDOW_SYSTEM */
7318
7319 /* Update the menu bar item lists, if appropriate. This has to be
7320 done before any actual redisplay or generation of display lines. */
7321 all_windows = (update_mode_lines
7322 || buffer_shared > 1
7323 || windows_or_buffers_changed);
7324 if (all_windows)
7325 {
7326 Lisp_Object tail, frame;
7327 int count = BINDING_STACK_SIZE ();
7328
7329 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7330
7331 FOR_EACH_FRAME (tail, frame)
7332 {
7333 f = XFRAME (frame);
7334
7335 /* Ignore tooltip frame. */
7336 if (EQ (frame, tooltip_frame))
7337 continue;
7338
7339 /* If a window on this frame changed size, report that to
7340 the user and clear the size-change flag. */
7341 if (FRAME_WINDOW_SIZES_CHANGED (f))
7342 {
7343 Lisp_Object functions;
7344
7345 /* Clear flag first in case we get an error below. */
7346 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7347 functions = Vwindow_size_change_functions;
7348 GCPRO2 (tail, functions);
7349
7350 while (CONSP (functions))
7351 {
7352 call1 (XCAR (functions), frame);
7353 functions = XCDR (functions);
7354 }
7355 UNGCPRO;
7356 }
7357
7358 GCPRO1 (tail);
7359 update_menu_bar (f, 0);
7360 #ifdef HAVE_WINDOW_SYSTEM
7361 update_tool_bar (f, 0);
7362 #endif
7363 UNGCPRO;
7364 }
7365
7366 unbind_to (count, Qnil);
7367 }
7368 else
7369 {
7370 struct frame *sf = SELECTED_FRAME ();
7371 update_menu_bar (sf, 1);
7372 #ifdef HAVE_WINDOW_SYSTEM
7373 update_tool_bar (sf, 1);
7374 #endif
7375 }
7376
7377 /* Motif needs this. See comment in xmenu.c. Turn it off when
7378 pending_menu_activation is not defined. */
7379 #ifdef USE_X_TOOLKIT
7380 pending_menu_activation = 0;
7381 #endif
7382 }
7383
7384
7385 /* Update the menu bar item list for frame F. This has to be done
7386 before we start to fill in any display lines, because it can call
7387 eval.
7388
7389 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7390
7391 static void
7392 update_menu_bar (f, save_match_data)
7393 struct frame *f;
7394 int save_match_data;
7395 {
7396 Lisp_Object window;
7397 register struct window *w;
7398
7399 /* If called recursively during a menu update, do nothing. This can
7400 happen when, for instance, an activate-menubar-hook causes a
7401 redisplay. */
7402 if (inhibit_menubar_update)
7403 return;
7404
7405 window = FRAME_SELECTED_WINDOW (f);
7406 w = XWINDOW (window);
7407
7408 if (update_mode_lines)
7409 w->update_mode_line = Qt;
7410
7411 if (FRAME_WINDOW_P (f)
7412 ?
7413 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7414 FRAME_EXTERNAL_MENU_BAR (f)
7415 #else
7416 FRAME_MENU_BAR_LINES (f) > 0
7417 #endif
7418 : FRAME_MENU_BAR_LINES (f) > 0)
7419 {
7420 /* If the user has switched buffers or windows, we need to
7421 recompute to reflect the new bindings. But we'll
7422 recompute when update_mode_lines is set too; that means
7423 that people can use force-mode-line-update to request
7424 that the menu bar be recomputed. The adverse effect on
7425 the rest of the redisplay algorithm is about the same as
7426 windows_or_buffers_changed anyway. */
7427 if (windows_or_buffers_changed
7428 || !NILP (w->update_mode_line)
7429 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7430 < BUF_MODIFF (XBUFFER (w->buffer)))
7431 != !NILP (w->last_had_star))
7432 || ((!NILP (Vtransient_mark_mode)
7433 && !NILP (XBUFFER (w->buffer)->mark_active))
7434 != !NILP (w->region_showing)))
7435 {
7436 struct buffer *prev = current_buffer;
7437 int count = BINDING_STACK_SIZE ();
7438
7439 specbind (Qinhibit_menubar_update, Qt);
7440
7441 set_buffer_internal_1 (XBUFFER (w->buffer));
7442 if (save_match_data)
7443 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7444 if (NILP (Voverriding_local_map_menu_flag))
7445 {
7446 specbind (Qoverriding_terminal_local_map, Qnil);
7447 specbind (Qoverriding_local_map, Qnil);
7448 }
7449
7450 /* Run the Lucid hook. */
7451 safe_run_hooks (Qactivate_menubar_hook);
7452
7453 /* If it has changed current-menubar from previous value,
7454 really recompute the menu-bar from the value. */
7455 if (! NILP (Vlucid_menu_bar_dirty_flag))
7456 call0 (Qrecompute_lucid_menubar);
7457
7458 safe_run_hooks (Qmenu_bar_update_hook);
7459 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7460
7461 /* Redisplay the menu bar in case we changed it. */
7462 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7463 if (FRAME_WINDOW_P (f)
7464 #if defined (macintosh)
7465 /* All frames on Mac OS share the same menubar. So only the
7466 selected frame should be allowed to set it. */
7467 && f == SELECTED_FRAME ()
7468 #endif
7469 )
7470 set_frame_menubar (f, 0, 0);
7471 else
7472 /* On a terminal screen, the menu bar is an ordinary screen
7473 line, and this makes it get updated. */
7474 w->update_mode_line = Qt;
7475 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7476 /* In the non-toolkit version, the menu bar is an ordinary screen
7477 line, and this makes it get updated. */
7478 w->update_mode_line = Qt;
7479 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7480
7481 unbind_to (count, Qnil);
7482 set_buffer_internal_1 (prev);
7483 }
7484 }
7485 }
7486
7487
7488 \f
7489 /***********************************************************************
7490 Tool-bars
7491 ***********************************************************************/
7492
7493 #ifdef HAVE_WINDOW_SYSTEM
7494
7495 /* Update the tool-bar item list for frame F. This has to be done
7496 before we start to fill in any display lines. Called from
7497 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7498 and restore it here. */
7499
7500 static void
7501 update_tool_bar (f, save_match_data)
7502 struct frame *f;
7503 int save_match_data;
7504 {
7505 if (WINDOWP (f->tool_bar_window)
7506 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7507 {
7508 Lisp_Object window;
7509 struct window *w;
7510
7511 window = FRAME_SELECTED_WINDOW (f);
7512 w = XWINDOW (window);
7513
7514 /* If the user has switched buffers or windows, we need to
7515 recompute to reflect the new bindings. But we'll
7516 recompute when update_mode_lines is set too; that means
7517 that people can use force-mode-line-update to request
7518 that the menu bar be recomputed. The adverse effect on
7519 the rest of the redisplay algorithm is about the same as
7520 windows_or_buffers_changed anyway. */
7521 if (windows_or_buffers_changed
7522 || !NILP (w->update_mode_line)
7523 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7524 < BUF_MODIFF (XBUFFER (w->buffer)))
7525 != !NILP (w->last_had_star))
7526 || ((!NILP (Vtransient_mark_mode)
7527 && !NILP (XBUFFER (w->buffer)->mark_active))
7528 != !NILP (w->region_showing)))
7529 {
7530 struct buffer *prev = current_buffer;
7531 int count = BINDING_STACK_SIZE ();
7532
7533 /* Set current_buffer to the buffer of the selected
7534 window of the frame, so that we get the right local
7535 keymaps. */
7536 set_buffer_internal_1 (XBUFFER (w->buffer));
7537
7538 /* Save match data, if we must. */
7539 if (save_match_data)
7540 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7541
7542 /* Make sure that we don't accidentally use bogus keymaps. */
7543 if (NILP (Voverriding_local_map_menu_flag))
7544 {
7545 specbind (Qoverriding_terminal_local_map, Qnil);
7546 specbind (Qoverriding_local_map, Qnil);
7547 }
7548
7549 /* Build desired tool-bar items from keymaps. */
7550 f->tool_bar_items
7551 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7552
7553 /* Redisplay the tool-bar in case we changed it. */
7554 w->update_mode_line = Qt;
7555
7556 unbind_to (count, Qnil);
7557 set_buffer_internal_1 (prev);
7558 }
7559 }
7560 }
7561
7562
7563 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7564 F's desired tool-bar contents. F->tool_bar_items must have
7565 been set up previously by calling prepare_menu_bars. */
7566
7567 static void
7568 build_desired_tool_bar_string (f)
7569 struct frame *f;
7570 {
7571 int i, size, size_needed;
7572 struct gcpro gcpro1, gcpro2, gcpro3;
7573 Lisp_Object image, plist, props;
7574
7575 image = plist = props = Qnil;
7576 GCPRO3 (image, plist, props);
7577
7578 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7579 Otherwise, make a new string. */
7580
7581 /* The size of the string we might be able to reuse. */
7582 size = (STRINGP (f->desired_tool_bar_string)
7583 ? XSTRING (f->desired_tool_bar_string)->size
7584 : 0);
7585
7586 /* We need one space in the string for each image. */
7587 size_needed = f->n_tool_bar_items;
7588
7589 /* Reuse f->desired_tool_bar_string, if possible. */
7590 if (size < size_needed || NILP (f->desired_tool_bar_string))
7591 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7592 make_number (' '));
7593 else
7594 {
7595 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7596 Fremove_text_properties (make_number (0), make_number (size),
7597 props, f->desired_tool_bar_string);
7598 }
7599
7600 /* Put a `display' property on the string for the images to display,
7601 put a `menu_item' property on tool-bar items with a value that
7602 is the index of the item in F's tool-bar item vector. */
7603 for (i = 0; i < f->n_tool_bar_items; ++i)
7604 {
7605 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7606
7607 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7608 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7609 int hmargin, vmargin, relief, idx, end;
7610 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7611
7612 /* If image is a vector, choose the image according to the
7613 button state. */
7614 image = PROP (TOOL_BAR_ITEM_IMAGES);
7615 if (VECTORP (image))
7616 {
7617 if (enabled_p)
7618 idx = (selected_p
7619 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7620 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7621 else
7622 idx = (selected_p
7623 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7624 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7625
7626 xassert (ASIZE (image) >= idx);
7627 image = AREF (image, idx);
7628 }
7629 else
7630 idx = -1;
7631
7632 /* Ignore invalid image specifications. */
7633 if (!valid_image_p (image))
7634 continue;
7635
7636 /* Display the tool-bar button pressed, or depressed. */
7637 plist = Fcopy_sequence (XCDR (image));
7638
7639 /* Compute margin and relief to draw. */
7640 relief = (tool_bar_button_relief >= 0
7641 ? tool_bar_button_relief
7642 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7643 hmargin = vmargin = relief;
7644
7645 if (INTEGERP (Vtool_bar_button_margin)
7646 && XINT (Vtool_bar_button_margin) > 0)
7647 {
7648 hmargin += XFASTINT (Vtool_bar_button_margin);
7649 vmargin += XFASTINT (Vtool_bar_button_margin);
7650 }
7651 else if (CONSP (Vtool_bar_button_margin))
7652 {
7653 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7654 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7655 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7656
7657 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7658 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7659 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7660 }
7661
7662 if (auto_raise_tool_bar_buttons_p)
7663 {
7664 /* Add a `:relief' property to the image spec if the item is
7665 selected. */
7666 if (selected_p)
7667 {
7668 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7669 hmargin -= relief;
7670 vmargin -= relief;
7671 }
7672 }
7673 else
7674 {
7675 /* If image is selected, display it pressed, i.e. with a
7676 negative relief. If it's not selected, display it with a
7677 raised relief. */
7678 plist = Fplist_put (plist, QCrelief,
7679 (selected_p
7680 ? make_number (-relief)
7681 : make_number (relief)));
7682 hmargin -= relief;
7683 vmargin -= relief;
7684 }
7685
7686 /* Put a margin around the image. */
7687 if (hmargin || vmargin)
7688 {
7689 if (hmargin == vmargin)
7690 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7691 else
7692 plist = Fplist_put (plist, QCmargin,
7693 Fcons (make_number (hmargin),
7694 make_number (vmargin)));
7695 }
7696
7697 /* If button is not enabled, and we don't have special images
7698 for the disabled state, make the image appear disabled by
7699 applying an appropriate algorithm to it. */
7700 if (!enabled_p && idx < 0)
7701 plist = Fplist_put (plist, QCconversion, Qdisabled);
7702
7703 /* Put a `display' text property on the string for the image to
7704 display. Put a `menu-item' property on the string that gives
7705 the start of this item's properties in the tool-bar items
7706 vector. */
7707 image = Fcons (Qimage, plist);
7708 props = list4 (Qdisplay, image,
7709 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7710
7711 /* Let the last image hide all remaining spaces in the tool bar
7712 string. The string can be longer than needed when we reuse a
7713 previous string. */
7714 if (i + 1 == f->n_tool_bar_items)
7715 end = XSTRING (f->desired_tool_bar_string)->size;
7716 else
7717 end = i + 1;
7718 Fadd_text_properties (make_number (i), make_number (end),
7719 props, f->desired_tool_bar_string);
7720 #undef PROP
7721 }
7722
7723 UNGCPRO;
7724 }
7725
7726
7727 /* Display one line of the tool-bar of frame IT->f. */
7728
7729 static void
7730 display_tool_bar_line (it)
7731 struct it *it;
7732 {
7733 struct glyph_row *row = it->glyph_row;
7734 int max_x = it->last_visible_x;
7735 struct glyph *last;
7736
7737 prepare_desired_row (row);
7738 row->y = it->current_y;
7739
7740 /* Note that this isn't made use of if the face hasn't a box,
7741 so there's no need to check the face here. */
7742 it->start_of_box_run_p = 1;
7743
7744 while (it->current_x < max_x)
7745 {
7746 int x_before, x, n_glyphs_before, i, nglyphs;
7747
7748 /* Get the next display element. */
7749 if (!get_next_display_element (it))
7750 break;
7751
7752 /* Produce glyphs. */
7753 x_before = it->current_x;
7754 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7755 PRODUCE_GLYPHS (it);
7756
7757 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7758 i = 0;
7759 x = x_before;
7760 while (i < nglyphs)
7761 {
7762 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7763
7764 if (x + glyph->pixel_width > max_x)
7765 {
7766 /* Glyph doesn't fit on line. */
7767 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7768 it->current_x = x;
7769 goto out;
7770 }
7771
7772 ++it->hpos;
7773 x += glyph->pixel_width;
7774 ++i;
7775 }
7776
7777 /* Stop at line ends. */
7778 if (ITERATOR_AT_END_OF_LINE_P (it))
7779 break;
7780
7781 set_iterator_to_next (it, 1);
7782 }
7783
7784 out:;
7785
7786 row->displays_text_p = row->used[TEXT_AREA] != 0;
7787 extend_face_to_end_of_line (it);
7788 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7789 last->right_box_line_p = 1;
7790 if (last == row->glyphs[TEXT_AREA])
7791 last->left_box_line_p = 1;
7792 compute_line_metrics (it);
7793
7794 /* If line is empty, make it occupy the rest of the tool-bar. */
7795 if (!row->displays_text_p)
7796 {
7797 row->height = row->phys_height = it->last_visible_y - row->y;
7798 row->ascent = row->phys_ascent = 0;
7799 }
7800
7801 row->full_width_p = 1;
7802 row->continued_p = 0;
7803 row->truncated_on_left_p = 0;
7804 row->truncated_on_right_p = 0;
7805
7806 it->current_x = it->hpos = 0;
7807 it->current_y += row->height;
7808 ++it->vpos;
7809 ++it->glyph_row;
7810 }
7811
7812
7813 /* Value is the number of screen lines needed to make all tool-bar
7814 items of frame F visible. */
7815
7816 static int
7817 tool_bar_lines_needed (f)
7818 struct frame *f;
7819 {
7820 struct window *w = XWINDOW (f->tool_bar_window);
7821 struct it it;
7822
7823 /* Initialize an iterator for iteration over
7824 F->desired_tool_bar_string in the tool-bar window of frame F. */
7825 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7826 it.first_visible_x = 0;
7827 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7828 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7829
7830 while (!ITERATOR_AT_END_P (&it))
7831 {
7832 it.glyph_row = w->desired_matrix->rows;
7833 clear_glyph_row (it.glyph_row);
7834 display_tool_bar_line (&it);
7835 }
7836
7837 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7838 }
7839
7840
7841 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7842 0, 1, 0,
7843 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7844 (frame)
7845 Lisp_Object frame;
7846 {
7847 struct frame *f;
7848 struct window *w;
7849 int nlines = 0;
7850
7851 if (NILP (frame))
7852 frame = selected_frame;
7853 else
7854 CHECK_FRAME (frame);
7855 f = XFRAME (frame);
7856
7857 if (WINDOWP (f->tool_bar_window)
7858 || (w = XWINDOW (f->tool_bar_window),
7859 XFASTINT (w->height) > 0))
7860 {
7861 update_tool_bar (f, 1);
7862 if (f->n_tool_bar_items)
7863 {
7864 build_desired_tool_bar_string (f);
7865 nlines = tool_bar_lines_needed (f);
7866 }
7867 }
7868
7869 return make_number (nlines);
7870 }
7871
7872
7873 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7874 height should be changed. */
7875
7876 static int
7877 redisplay_tool_bar (f)
7878 struct frame *f;
7879 {
7880 struct window *w;
7881 struct it it;
7882 struct glyph_row *row;
7883 int change_height_p = 0;
7884
7885 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7886 do anything. This means you must start with tool-bar-lines
7887 non-zero to get the auto-sizing effect. Or in other words, you
7888 can turn off tool-bars by specifying tool-bar-lines zero. */
7889 if (!WINDOWP (f->tool_bar_window)
7890 || (w = XWINDOW (f->tool_bar_window),
7891 XFASTINT (w->height) == 0))
7892 return 0;
7893
7894 /* Set up an iterator for the tool-bar window. */
7895 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7896 it.first_visible_x = 0;
7897 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7898 row = it.glyph_row;
7899
7900 /* Build a string that represents the contents of the tool-bar. */
7901 build_desired_tool_bar_string (f);
7902 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7903
7904 /* Display as many lines as needed to display all tool-bar items. */
7905 while (it.current_y < it.last_visible_y)
7906 display_tool_bar_line (&it);
7907
7908 /* It doesn't make much sense to try scrolling in the tool-bar
7909 window, so don't do it. */
7910 w->desired_matrix->no_scrolling_p = 1;
7911 w->must_be_updated_p = 1;
7912
7913 if (auto_resize_tool_bars_p)
7914 {
7915 int nlines;
7916
7917 /* If we couldn't display everything, change the tool-bar's
7918 height. */
7919 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7920 change_height_p = 1;
7921
7922 /* If there are blank lines at the end, except for a partially
7923 visible blank line at the end that is smaller than
7924 CANON_Y_UNIT, change the tool-bar's height. */
7925 row = it.glyph_row - 1;
7926 if (!row->displays_text_p
7927 && row->height >= CANON_Y_UNIT (f))
7928 change_height_p = 1;
7929
7930 /* If row displays tool-bar items, but is partially visible,
7931 change the tool-bar's height. */
7932 if (row->displays_text_p
7933 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7934 change_height_p = 1;
7935
7936 /* Resize windows as needed by changing the `tool-bar-lines'
7937 frame parameter. */
7938 if (change_height_p
7939 && (nlines = tool_bar_lines_needed (f),
7940 nlines != XFASTINT (w->height)))
7941 {
7942 extern Lisp_Object Qtool_bar_lines;
7943 Lisp_Object frame;
7944 int old_height = XFASTINT (w->height);
7945
7946 XSETFRAME (frame, f);
7947 clear_glyph_matrix (w->desired_matrix);
7948 Fmodify_frame_parameters (frame,
7949 Fcons (Fcons (Qtool_bar_lines,
7950 make_number (nlines)),
7951 Qnil));
7952 if (XFASTINT (w->height) != old_height)
7953 fonts_changed_p = 1;
7954 }
7955 }
7956
7957 return change_height_p;
7958 }
7959
7960
7961 /* Get information about the tool-bar item which is displayed in GLYPH
7962 on frame F. Return in *PROP_IDX the index where tool-bar item
7963 properties start in F->tool_bar_items. Value is zero if
7964 GLYPH doesn't display a tool-bar item. */
7965
7966 int
7967 tool_bar_item_info (f, glyph, prop_idx)
7968 struct frame *f;
7969 struct glyph *glyph;
7970 int *prop_idx;
7971 {
7972 Lisp_Object prop;
7973 int success_p;
7974 int charpos;
7975
7976 /* This function can be called asynchronously, which means we must
7977 exclude any possibility that Fget_text_property signals an
7978 error. */
7979 charpos = min (XSTRING (f->current_tool_bar_string)->size, glyph->charpos);
7980 charpos = max (0, charpos);
7981
7982 /* Get the text property `menu-item' at pos. The value of that
7983 property is the start index of this item's properties in
7984 F->tool_bar_items. */
7985 prop = Fget_text_property (make_number (charpos),
7986 Qmenu_item, f->current_tool_bar_string);
7987 if (INTEGERP (prop))
7988 {
7989 *prop_idx = XINT (prop);
7990 success_p = 1;
7991 }
7992 else
7993 success_p = 0;
7994
7995 return success_p;
7996 }
7997
7998 #endif /* HAVE_WINDOW_SYSTEM */
7999
8000
8001 \f
8002 /************************************************************************
8003 Horizontal scrolling
8004 ************************************************************************/
8005
8006 static int hscroll_window_tree P_ ((Lisp_Object));
8007 static int hscroll_windows P_ ((Lisp_Object));
8008
8009 /* For all leaf windows in the window tree rooted at WINDOW, set their
8010 hscroll value so that PT is (i) visible in the window, and (ii) so
8011 that it is not within a certain margin at the window's left and
8012 right border. Value is non-zero if any window's hscroll has been
8013 changed. */
8014
8015 static int
8016 hscroll_window_tree (window)
8017 Lisp_Object window;
8018 {
8019 int hscrolled_p = 0;
8020
8021 while (WINDOWP (window))
8022 {
8023 struct window *w = XWINDOW (window);
8024
8025 if (WINDOWP (w->hchild))
8026 hscrolled_p |= hscroll_window_tree (w->hchild);
8027 else if (WINDOWP (w->vchild))
8028 hscrolled_p |= hscroll_window_tree (w->vchild);
8029 else if (w->cursor.vpos >= 0)
8030 {
8031 int hscroll_margin, text_area_x, text_area_y;
8032 int text_area_width, text_area_height;
8033 struct glyph_row *current_cursor_row
8034 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8035 struct glyph_row *desired_cursor_row
8036 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8037 struct glyph_row *cursor_row
8038 = (desired_cursor_row->enabled_p
8039 ? desired_cursor_row
8040 : current_cursor_row);
8041
8042 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8043 &text_area_width, &text_area_height);
8044
8045 /* Scroll when cursor is inside this scroll margin. */
8046 /* Shouldn't we export this `5' for customization ? -stef */
8047 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
8048
8049 if ((XFASTINT (w->hscroll)
8050 && w->cursor.x < hscroll_margin)
8051 || (cursor_row->enabled_p
8052 && cursor_row->truncated_on_right_p
8053 && (w->cursor.x > text_area_width - hscroll_margin)))
8054 {
8055 struct it it;
8056 int hscroll;
8057 struct buffer *saved_current_buffer;
8058 int pt;
8059
8060 /* Find point in a display of infinite width. */
8061 saved_current_buffer = current_buffer;
8062 current_buffer = XBUFFER (w->buffer);
8063
8064 if (w == XWINDOW (selected_window))
8065 pt = BUF_PT (current_buffer);
8066 else
8067 {
8068 pt = marker_position (w->pointm);
8069 pt = max (BEGV, pt);
8070 pt = min (ZV, pt);
8071 }
8072
8073 /* Move iterator to pt starting at cursor_row->start in
8074 a line with infinite width. */
8075 init_to_row_start (&it, w, cursor_row);
8076 it.last_visible_x = INFINITY;
8077 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8078 current_buffer = saved_current_buffer;
8079
8080 /* Center cursor in window. */
8081 hscroll = (max (0, it.current_x - text_area_width / 2)
8082 / CANON_X_UNIT (it.f));
8083 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8084
8085 /* Don't call Fset_window_hscroll if value hasn't
8086 changed because it will prevent redisplay
8087 optimizations. */
8088 if (XFASTINT (w->hscroll) != hscroll)
8089 {
8090 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8091 w->hscroll = make_number (hscroll);
8092 hscrolled_p = 1;
8093 }
8094 }
8095 }
8096
8097 window = w->next;
8098 }
8099
8100 /* Value is non-zero if hscroll of any leaf window has been changed. */
8101 return hscrolled_p;
8102 }
8103
8104
8105 /* Set hscroll so that cursor is visible and not inside horizontal
8106 scroll margins for all windows in the tree rooted at WINDOW. See
8107 also hscroll_window_tree above. Value is non-zero if any window's
8108 hscroll has been changed. If it has, desired matrices on the frame
8109 of WINDOW are cleared. */
8110
8111 static int
8112 hscroll_windows (window)
8113 Lisp_Object window;
8114 {
8115 int hscrolled_p;
8116
8117 if (automatic_hscrolling_p)
8118 {
8119 hscrolled_p = hscroll_window_tree (window);
8120 if (hscrolled_p)
8121 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8122 }
8123 else
8124 hscrolled_p = 0;
8125 return hscrolled_p;
8126 }
8127
8128
8129 \f
8130 /************************************************************************
8131 Redisplay
8132 ************************************************************************/
8133
8134 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8135 to a non-zero value. This is sometimes handy to have in a debugger
8136 session. */
8137
8138 #if GLYPH_DEBUG
8139
8140 /* First and last unchanged row for try_window_id. */
8141
8142 int debug_first_unchanged_at_end_vpos;
8143 int debug_last_unchanged_at_beg_vpos;
8144
8145 /* Delta vpos and y. */
8146
8147 int debug_dvpos, debug_dy;
8148
8149 /* Delta in characters and bytes for try_window_id. */
8150
8151 int debug_delta, debug_delta_bytes;
8152
8153 /* Values of window_end_pos and window_end_vpos at the end of
8154 try_window_id. */
8155
8156 int debug_end_pos, debug_end_vpos;
8157
8158 /* Append a string to W->desired_matrix->method. FMT is a printf
8159 format string. A1...A9 are a supplement for a variable-length
8160 argument list. If trace_redisplay_p is non-zero also printf the
8161 resulting string to stderr. */
8162
8163 static void
8164 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8165 struct window *w;
8166 char *fmt;
8167 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8168 {
8169 char buffer[512];
8170 char *method = w->desired_matrix->method;
8171 int len = strlen (method);
8172 int size = sizeof w->desired_matrix->method;
8173 int remaining = size - len - 1;
8174
8175 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8176 if (len && remaining)
8177 {
8178 method[len] = '|';
8179 --remaining, ++len;
8180 }
8181
8182 strncpy (method + len, buffer, remaining);
8183
8184 if (trace_redisplay_p)
8185 fprintf (stderr, "%p (%s): %s\n",
8186 w,
8187 ((BUFFERP (w->buffer)
8188 && STRINGP (XBUFFER (w->buffer)->name))
8189 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
8190 : "no buffer"),
8191 buffer);
8192 }
8193
8194 #endif /* GLYPH_DEBUG */
8195
8196
8197 /* This counter is used to clear the face cache every once in a while
8198 in redisplay_internal. It is incremented for each redisplay.
8199 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8200 cleared. */
8201
8202 #define CLEAR_FACE_CACHE_COUNT 500
8203 static int clear_face_cache_count;
8204
8205 /* Record the previous terminal frame we displayed. */
8206
8207 static struct frame *previous_terminal_frame;
8208
8209 /* Non-zero while redisplay_internal is in progress. */
8210
8211 int redisplaying_p;
8212
8213
8214 /* Value is non-zero if all changes in window W, which displays
8215 current_buffer, are in the text between START and END. START is a
8216 buffer position, END is given as a distance from Z. Used in
8217 redisplay_internal for display optimization. */
8218
8219 static INLINE int
8220 text_outside_line_unchanged_p (w, start, end)
8221 struct window *w;
8222 int start, end;
8223 {
8224 int unchanged_p = 1;
8225
8226 /* If text or overlays have changed, see where. */
8227 if (XFASTINT (w->last_modified) < MODIFF
8228 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8229 {
8230 /* Gap in the line? */
8231 if (GPT < start || Z - GPT < end)
8232 unchanged_p = 0;
8233
8234 /* Changes start in front of the line, or end after it? */
8235 if (unchanged_p
8236 && (BEG_UNCHANGED < start - 1
8237 || END_UNCHANGED < end))
8238 unchanged_p = 0;
8239
8240 /* If selective display, can't optimize if changes start at the
8241 beginning of the line. */
8242 if (unchanged_p
8243 && INTEGERP (current_buffer->selective_display)
8244 && XINT (current_buffer->selective_display) > 0
8245 && (BEG_UNCHANGED < start || GPT <= start))
8246 unchanged_p = 0;
8247
8248 /* If there are overlays at the start or end of the line, these
8249 may have overlay strings with newlines in them. A change at
8250 START, for instance, may actually concern the display of such
8251 overlay strings as well, and they are displayed on different
8252 lines. So, quickly rule out this case. (For the future, it
8253 might be desirable to implement something more telling than
8254 just BEG/END_UNCHANGED.) */
8255 if (unchanged_p)
8256 {
8257 if (BEG + BEG_UNCHANGED == start
8258 && overlay_touches_p (start))
8259 unchanged_p = 0;
8260 if (END_UNCHANGED == end
8261 && overlay_touches_p (Z - end))
8262 unchanged_p = 0;
8263 }
8264 }
8265
8266 return unchanged_p;
8267 }
8268
8269
8270 /* Do a frame update, taking possible shortcuts into account. This is
8271 the main external entry point for redisplay.
8272
8273 If the last redisplay displayed an echo area message and that message
8274 is no longer requested, we clear the echo area or bring back the
8275 mini-buffer if that is in use. */
8276
8277 void
8278 redisplay ()
8279 {
8280 redisplay_internal (0);
8281 }
8282
8283
8284 /* Return 1 if point moved out of or into a composition. Otherwise
8285 return 0. PREV_BUF and PREV_PT are the last point buffer and
8286 position. BUF and PT are the current point buffer and position. */
8287
8288 int
8289 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8290 struct buffer *prev_buf, *buf;
8291 int prev_pt, pt;
8292 {
8293 int start, end;
8294 Lisp_Object prop;
8295 Lisp_Object buffer;
8296
8297 XSETBUFFER (buffer, buf);
8298 /* Check a composition at the last point if point moved within the
8299 same buffer. */
8300 if (prev_buf == buf)
8301 {
8302 if (prev_pt == pt)
8303 /* Point didn't move. */
8304 return 0;
8305
8306 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8307 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8308 && COMPOSITION_VALID_P (start, end, prop)
8309 && start < prev_pt && end > prev_pt)
8310 /* The last point was within the composition. Return 1 iff
8311 point moved out of the composition. */
8312 return (pt <= start || pt >= end);
8313 }
8314
8315 /* Check a composition at the current point. */
8316 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8317 && find_composition (pt, -1, &start, &end, &prop, buffer)
8318 && COMPOSITION_VALID_P (start, end, prop)
8319 && start < pt && end > pt);
8320 }
8321
8322
8323 /* Reconsider the setting of B->clip_changed which is displayed
8324 in window W. */
8325
8326 static INLINE void
8327 reconsider_clip_changes (w, b)
8328 struct window *w;
8329 struct buffer *b;
8330 {
8331 if (b->prevent_redisplay_optimizations_p)
8332 b->clip_changed = 1;
8333 else if (b->clip_changed
8334 && !NILP (w->window_end_valid)
8335 && w->current_matrix->buffer == b
8336 && w->current_matrix->zv == BUF_ZV (b)
8337 && w->current_matrix->begv == BUF_BEGV (b))
8338 b->clip_changed = 0;
8339
8340 /* If display wasn't paused, and W is not a tool bar window, see if
8341 point has been moved into or out of a composition. In that case,
8342 we set b->clip_changed to 1 to force updating the screen. If
8343 b->clip_changed has already been set to 1, we can skip this
8344 check. */
8345 if (!b->clip_changed
8346 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8347 {
8348 int pt;
8349
8350 if (w == XWINDOW (selected_window))
8351 pt = BUF_PT (current_buffer);
8352 else
8353 pt = marker_position (w->pointm);
8354
8355 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8356 || pt != XINT (w->last_point))
8357 && check_point_in_composition (w->current_matrix->buffer,
8358 XINT (w->last_point),
8359 XBUFFER (w->buffer), pt))
8360 b->clip_changed = 1;
8361 }
8362 }
8363
8364
8365 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8366 response to any user action; therefore, we should preserve the echo
8367 area. (Actually, our caller does that job.) Perhaps in the future
8368 avoid recentering windows if it is not necessary; currently that
8369 causes some problems. */
8370
8371 static void
8372 redisplay_internal (preserve_echo_area)
8373 int preserve_echo_area;
8374 {
8375 struct window *w = XWINDOW (selected_window);
8376 struct frame *f = XFRAME (w->frame);
8377 int pause;
8378 int must_finish = 0;
8379 struct text_pos tlbufpos, tlendpos;
8380 int number_of_visible_frames;
8381 int count;
8382 struct frame *sf = SELECTED_FRAME ();
8383
8384 /* Non-zero means redisplay has to consider all windows on all
8385 frames. Zero means, only selected_window is considered. */
8386 int consider_all_windows_p;
8387
8388 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8389
8390 /* No redisplay if running in batch mode or frame is not yet fully
8391 initialized, or redisplay is explicitly turned off by setting
8392 Vinhibit_redisplay. */
8393 if (noninteractive
8394 || !NILP (Vinhibit_redisplay)
8395 || !f->glyphs_initialized_p)
8396 return;
8397
8398 /* The flag redisplay_performed_directly_p is set by
8399 direct_output_for_insert when it already did the whole screen
8400 update necessary. */
8401 if (redisplay_performed_directly_p)
8402 {
8403 redisplay_performed_directly_p = 0;
8404 if (!hscroll_windows (selected_window))
8405 return;
8406 }
8407
8408 #ifdef USE_X_TOOLKIT
8409 if (popup_activated ())
8410 return;
8411 #endif
8412
8413 /* I don't think this happens but let's be paranoid. */
8414 if (redisplaying_p)
8415 return;
8416
8417 /* Record a function that resets redisplaying_p to its old value
8418 when we leave this function. */
8419 count = BINDING_STACK_SIZE ();
8420 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8421 ++redisplaying_p;
8422
8423 retry:
8424 pause = 0;
8425 reconsider_clip_changes (w, current_buffer);
8426
8427 /* If new fonts have been loaded that make a glyph matrix adjustment
8428 necessary, do it. */
8429 if (fonts_changed_p)
8430 {
8431 adjust_glyphs (NULL);
8432 ++windows_or_buffers_changed;
8433 fonts_changed_p = 0;
8434 }
8435
8436 /* If face_change_count is non-zero, init_iterator will free all
8437 realized faces, which includes the faces referenced from current
8438 matrices. So, we can't reuse current matrices in this case. */
8439 if (face_change_count)
8440 ++windows_or_buffers_changed;
8441
8442 if (! FRAME_WINDOW_P (sf)
8443 && previous_terminal_frame != sf)
8444 {
8445 /* Since frames on an ASCII terminal share the same display
8446 area, displaying a different frame means redisplay the whole
8447 thing. */
8448 windows_or_buffers_changed++;
8449 SET_FRAME_GARBAGED (sf);
8450 XSETFRAME (Vterminal_frame, sf);
8451 }
8452 previous_terminal_frame = sf;
8453
8454 /* Set the visible flags for all frames. Do this before checking
8455 for resized or garbaged frames; they want to know if their frames
8456 are visible. See the comment in frame.h for
8457 FRAME_SAMPLE_VISIBILITY. */
8458 {
8459 Lisp_Object tail, frame;
8460
8461 number_of_visible_frames = 0;
8462
8463 FOR_EACH_FRAME (tail, frame)
8464 {
8465 struct frame *f = XFRAME (frame);
8466
8467 FRAME_SAMPLE_VISIBILITY (f);
8468 if (FRAME_VISIBLE_P (f))
8469 ++number_of_visible_frames;
8470 clear_desired_matrices (f);
8471 }
8472 }
8473
8474 /* Notice any pending interrupt request to change frame size. */
8475 do_pending_window_change (1);
8476
8477 /* Clear frames marked as garbaged. */
8478 if (frame_garbaged)
8479 clear_garbaged_frames ();
8480
8481 /* Build menubar and tool-bar items. */
8482 prepare_menu_bars ();
8483
8484 if (windows_or_buffers_changed)
8485 update_mode_lines++;
8486
8487 /* Detect case that we need to write or remove a star in the mode line. */
8488 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8489 {
8490 w->update_mode_line = Qt;
8491 if (buffer_shared > 1)
8492 update_mode_lines++;
8493 }
8494
8495 /* If %c is in the mode line, update it if needed. */
8496 if (!NILP (w->column_number_displayed)
8497 /* This alternative quickly identifies a common case
8498 where no change is needed. */
8499 && !(PT == XFASTINT (w->last_point)
8500 && XFASTINT (w->last_modified) >= MODIFF
8501 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8502 && XFASTINT (w->column_number_displayed) != current_column ())
8503 w->update_mode_line = Qt;
8504
8505 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8506
8507 /* The variable buffer_shared is set in redisplay_window and
8508 indicates that we redisplay a buffer in different windows. See
8509 there. */
8510 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8511
8512 /* If specs for an arrow have changed, do thorough redisplay
8513 to ensure we remove any arrow that should no longer exist. */
8514 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8515 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8516 consider_all_windows_p = windows_or_buffers_changed = 1;
8517
8518 /* Normally the message* functions will have already displayed and
8519 updated the echo area, but the frame may have been trashed, or
8520 the update may have been preempted, so display the echo area
8521 again here. Checking message_cleared_p captures the case that
8522 the echo area should be cleared. */
8523 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8524 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8525 || (message_cleared_p
8526 && minibuf_level == 0
8527 /* If the mini-window is currently selected, this means the
8528 echo-area doesn't show through. */
8529 && !MINI_WINDOW_P (XWINDOW (selected_window))))
8530 {
8531 int window_height_changed_p = echo_area_display (0);
8532 must_finish = 1;
8533
8534 /* If we don't display the current message, don't clear the
8535 message_cleared_p flag, because, if we did, we wouldn't clear
8536 the echo area in the next redisplay which doesn't preserve
8537 the echo area. */
8538 if (!display_last_displayed_message_p)
8539 message_cleared_p = 0;
8540
8541 if (fonts_changed_p)
8542 goto retry;
8543 else if (window_height_changed_p)
8544 {
8545 consider_all_windows_p = 1;
8546 ++update_mode_lines;
8547 ++windows_or_buffers_changed;
8548
8549 /* If window configuration was changed, frames may have been
8550 marked garbaged. Clear them or we will experience
8551 surprises wrt scrolling. */
8552 if (frame_garbaged)
8553 clear_garbaged_frames ();
8554 }
8555 }
8556 else if (EQ (selected_window, minibuf_window)
8557 && (current_buffer->clip_changed
8558 || XFASTINT (w->last_modified) < MODIFF
8559 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8560 && resize_mini_window (w, 0))
8561 {
8562 /* Resized active mini-window to fit the size of what it is
8563 showing if its contents might have changed. */
8564 must_finish = 1;
8565 consider_all_windows_p = 1;
8566 ++windows_or_buffers_changed;
8567 ++update_mode_lines;
8568
8569 /* If window configuration was changed, frames may have been
8570 marked garbaged. Clear them or we will experience
8571 surprises wrt scrolling. */
8572 if (frame_garbaged)
8573 clear_garbaged_frames ();
8574 }
8575
8576
8577 /* If showing the region, and mark has changed, we must redisplay
8578 the whole window. The assignment to this_line_start_pos prevents
8579 the optimization directly below this if-statement. */
8580 if (((!NILP (Vtransient_mark_mode)
8581 && !NILP (XBUFFER (w->buffer)->mark_active))
8582 != !NILP (w->region_showing))
8583 || (!NILP (w->region_showing)
8584 && !EQ (w->region_showing,
8585 Fmarker_position (XBUFFER (w->buffer)->mark))))
8586 CHARPOS (this_line_start_pos) = 0;
8587
8588 /* Optimize the case that only the line containing the cursor in the
8589 selected window has changed. Variables starting with this_ are
8590 set in display_line and record information about the line
8591 containing the cursor. */
8592 tlbufpos = this_line_start_pos;
8593 tlendpos = this_line_end_pos;
8594 if (!consider_all_windows_p
8595 && CHARPOS (tlbufpos) > 0
8596 && NILP (w->update_mode_line)
8597 && !current_buffer->clip_changed
8598 && FRAME_VISIBLE_P (XFRAME (w->frame))
8599 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8600 /* Make sure recorded data applies to current buffer, etc. */
8601 && this_line_buffer == current_buffer
8602 && current_buffer == XBUFFER (w->buffer)
8603 && NILP (w->force_start)
8604 /* Point must be on the line that we have info recorded about. */
8605 && PT >= CHARPOS (tlbufpos)
8606 && PT <= Z - CHARPOS (tlendpos)
8607 /* All text outside that line, including its final newline,
8608 must be unchanged */
8609 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8610 CHARPOS (tlendpos)))
8611 {
8612 if (CHARPOS (tlbufpos) > BEGV
8613 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8614 && (CHARPOS (tlbufpos) == ZV
8615 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8616 /* Former continuation line has disappeared by becoming empty */
8617 goto cancel;
8618 else if (XFASTINT (w->last_modified) < MODIFF
8619 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8620 || MINI_WINDOW_P (w))
8621 {
8622 /* We have to handle the case of continuation around a
8623 wide-column character (See the comment in indent.c around
8624 line 885).
8625
8626 For instance, in the following case:
8627
8628 -------- Insert --------
8629 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8630 J_I_ ==> J_I_ `^^' are cursors.
8631 ^^ ^^
8632 -------- --------
8633
8634 As we have to redraw the line above, we should goto cancel. */
8635
8636 struct it it;
8637 int line_height_before = this_line_pixel_height;
8638
8639 /* Note that start_display will handle the case that the
8640 line starting at tlbufpos is a continuation lines. */
8641 start_display (&it, w, tlbufpos);
8642
8643 /* Implementation note: It this still necessary? */
8644 if (it.current_x != this_line_start_x)
8645 goto cancel;
8646
8647 TRACE ((stderr, "trying display optimization 1\n"));
8648 w->cursor.vpos = -1;
8649 overlay_arrow_seen = 0;
8650 it.vpos = this_line_vpos;
8651 it.current_y = this_line_y;
8652 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8653 display_line (&it);
8654
8655 /* If line contains point, is not continued,
8656 and ends at same distance from eob as before, we win */
8657 if (w->cursor.vpos >= 0
8658 /* Line is not continued, otherwise this_line_start_pos
8659 would have been set to 0 in display_line. */
8660 && CHARPOS (this_line_start_pos)
8661 /* Line ends as before. */
8662 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8663 /* Line has same height as before. Otherwise other lines
8664 would have to be shifted up or down. */
8665 && this_line_pixel_height == line_height_before)
8666 {
8667 /* If this is not the window's last line, we must adjust
8668 the charstarts of the lines below. */
8669 if (it.current_y < it.last_visible_y)
8670 {
8671 struct glyph_row *row
8672 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8673 int delta, delta_bytes;
8674
8675 if (Z - CHARPOS (tlendpos) == ZV)
8676 {
8677 /* This line ends at end of (accessible part of)
8678 buffer. There is no newline to count. */
8679 delta = (Z
8680 - CHARPOS (tlendpos)
8681 - MATRIX_ROW_START_CHARPOS (row));
8682 delta_bytes = (Z_BYTE
8683 - BYTEPOS (tlendpos)
8684 - MATRIX_ROW_START_BYTEPOS (row));
8685 }
8686 else
8687 {
8688 /* This line ends in a newline. Must take
8689 account of the newline and the rest of the
8690 text that follows. */
8691 delta = (Z
8692 - CHARPOS (tlendpos)
8693 - MATRIX_ROW_START_CHARPOS (row));
8694 delta_bytes = (Z_BYTE
8695 - BYTEPOS (tlendpos)
8696 - MATRIX_ROW_START_BYTEPOS (row));
8697 }
8698
8699 increment_matrix_positions (w->current_matrix,
8700 this_line_vpos + 1,
8701 w->current_matrix->nrows,
8702 delta, delta_bytes);
8703 }
8704
8705 /* If this row displays text now but previously didn't,
8706 or vice versa, w->window_end_vpos may have to be
8707 adjusted. */
8708 if ((it.glyph_row - 1)->displays_text_p)
8709 {
8710 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8711 XSETINT (w->window_end_vpos, this_line_vpos);
8712 }
8713 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8714 && this_line_vpos > 0)
8715 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8716 w->window_end_valid = Qnil;
8717
8718 /* Update hint: No need to try to scroll in update_window. */
8719 w->desired_matrix->no_scrolling_p = 1;
8720
8721 #if GLYPH_DEBUG
8722 *w->desired_matrix->method = 0;
8723 debug_method_add (w, "optimization 1");
8724 #endif
8725 goto update;
8726 }
8727 else
8728 goto cancel;
8729 }
8730 else if (/* Cursor position hasn't changed. */
8731 PT == XFASTINT (w->last_point)
8732 /* Make sure the cursor was last displayed
8733 in this window. Otherwise we have to reposition it. */
8734 && 0 <= w->cursor.vpos
8735 && XINT (w->height) > w->cursor.vpos)
8736 {
8737 if (!must_finish)
8738 {
8739 do_pending_window_change (1);
8740
8741 /* We used to always goto end_of_redisplay here, but this
8742 isn't enough if we have a blinking cursor. */
8743 if (w->cursor_off_p == w->last_cursor_off_p)
8744 goto end_of_redisplay;
8745 }
8746 goto update;
8747 }
8748 /* If highlighting the region, or if the cursor is in the echo area,
8749 then we can't just move the cursor. */
8750 else if (! (!NILP (Vtransient_mark_mode)
8751 && !NILP (current_buffer->mark_active))
8752 && (EQ (selected_window, current_buffer->last_selected_window)
8753 || highlight_nonselected_windows)
8754 && NILP (w->region_showing)
8755 && NILP (Vshow_trailing_whitespace)
8756 && !cursor_in_echo_area)
8757 {
8758 struct it it;
8759 struct glyph_row *row;
8760
8761 /* Skip from tlbufpos to PT and see where it is. Note that
8762 PT may be in invisible text. If so, we will end at the
8763 next visible position. */
8764 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8765 NULL, DEFAULT_FACE_ID);
8766 it.current_x = this_line_start_x;
8767 it.current_y = this_line_y;
8768 it.vpos = this_line_vpos;
8769
8770 /* The call to move_it_to stops in front of PT, but
8771 moves over before-strings. */
8772 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8773
8774 if (it.vpos == this_line_vpos
8775 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8776 row->enabled_p))
8777 {
8778 xassert (this_line_vpos == it.vpos);
8779 xassert (this_line_y == it.current_y);
8780 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8781 #if GLYPH_DEBUG
8782 *w->desired_matrix->method = 0;
8783 debug_method_add (w, "optimization 3");
8784 #endif
8785 goto update;
8786 }
8787 else
8788 goto cancel;
8789 }
8790
8791 cancel:
8792 /* Text changed drastically or point moved off of line. */
8793 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8794 }
8795
8796 CHARPOS (this_line_start_pos) = 0;
8797 consider_all_windows_p |= buffer_shared > 1;
8798 ++clear_face_cache_count;
8799
8800
8801 /* Build desired matrices, and update the display. If
8802 consider_all_windows_p is non-zero, do it for all windows on all
8803 frames. Otherwise do it for selected_window, only. */
8804
8805 if (consider_all_windows_p)
8806 {
8807 Lisp_Object tail, frame;
8808 int i, n = 0, size = 50;
8809 struct frame **updated
8810 = (struct frame **) alloca (size * sizeof *updated);
8811
8812 /* Clear the face cache eventually. */
8813 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8814 {
8815 clear_face_cache (0);
8816 clear_face_cache_count = 0;
8817 }
8818
8819 /* Recompute # windows showing selected buffer. This will be
8820 incremented each time such a window is displayed. */
8821 buffer_shared = 0;
8822
8823 FOR_EACH_FRAME (tail, frame)
8824 {
8825 struct frame *f = XFRAME (frame);
8826
8827 if (FRAME_WINDOW_P (f) || f == sf)
8828 {
8829 if (clear_face_cache_count % 50 == 0
8830 && FRAME_WINDOW_P (f))
8831 clear_image_cache (f, 0);
8832
8833 /* Mark all the scroll bars to be removed; we'll redeem
8834 the ones we want when we redisplay their windows. */
8835 if (condemn_scroll_bars_hook)
8836 condemn_scroll_bars_hook (f);
8837
8838 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8839 redisplay_windows (FRAME_ROOT_WINDOW (f));
8840
8841 /* Any scroll bars which redisplay_windows should have
8842 nuked should now go away. */
8843 if (judge_scroll_bars_hook)
8844 judge_scroll_bars_hook (f);
8845
8846 /* If fonts changed, display again. */
8847 if (fonts_changed_p)
8848 goto retry;
8849
8850 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8851 {
8852 /* See if we have to hscroll. */
8853 if (hscroll_windows (f->root_window))
8854 goto retry;
8855
8856 /* Prevent various kinds of signals during display
8857 update. stdio is not robust about handling
8858 signals, which can cause an apparent I/O
8859 error. */
8860 if (interrupt_input)
8861 unrequest_sigio ();
8862 stop_polling ();
8863
8864 /* Update the display. */
8865 set_window_update_flags (XWINDOW (f->root_window), 1);
8866 pause |= update_frame (f, 0, 0);
8867 if (pause)
8868 break;
8869
8870 if (n == size)
8871 {
8872 int nbytes = size * sizeof *updated;
8873 struct frame **p = (struct frame **) alloca (2 * nbytes);
8874 bcopy (updated, p, nbytes);
8875 size *= 2;
8876 }
8877
8878 updated[n++] = f;
8879 }
8880 }
8881 }
8882
8883 /* Do the mark_window_display_accurate after all windows have
8884 been redisplayed because this call resets flags in buffers
8885 which are needed for proper redisplay. */
8886 for (i = 0; i < n; ++i)
8887 {
8888 struct frame *f = updated[i];
8889 mark_window_display_accurate (f->root_window, 1);
8890 if (frame_up_to_date_hook)
8891 frame_up_to_date_hook (f);
8892 }
8893 }
8894 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8895 {
8896 Lisp_Object mini_window;
8897 struct frame *mini_frame;
8898
8899 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
8900 internal_condition_case_1 (redisplay_window_1, selected_window, Qerror,
8901 redisplay_window_error);
8902
8903 /* Compare desired and current matrices, perform output. */
8904 update:
8905
8906 /* If fonts changed, display again. */
8907 if (fonts_changed_p)
8908 goto retry;
8909
8910 /* Prevent various kinds of signals during display update.
8911 stdio is not robust about handling signals,
8912 which can cause an apparent I/O error. */
8913 if (interrupt_input)
8914 unrequest_sigio ();
8915 stop_polling ();
8916
8917 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8918 {
8919 if (hscroll_windows (selected_window))
8920 goto retry;
8921
8922 XWINDOW (selected_window)->must_be_updated_p = 1;
8923 pause = update_frame (sf, 0, 0);
8924 }
8925
8926 /* We may have called echo_area_display at the top of this
8927 function. If the echo area is on another frame, that may
8928 have put text on a frame other than the selected one, so the
8929 above call to update_frame would not have caught it. Catch
8930 it here. */
8931 mini_window = FRAME_MINIBUF_WINDOW (sf);
8932 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8933
8934 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8935 {
8936 XWINDOW (mini_window)->must_be_updated_p = 1;
8937 pause |= update_frame (mini_frame, 0, 0);
8938 if (!pause && hscroll_windows (mini_window))
8939 goto retry;
8940 }
8941 }
8942
8943 /* If display was paused because of pending input, make sure we do a
8944 thorough update the next time. */
8945 if (pause)
8946 {
8947 /* Prevent the optimization at the beginning of
8948 redisplay_internal that tries a single-line update of the
8949 line containing the cursor in the selected window. */
8950 CHARPOS (this_line_start_pos) = 0;
8951
8952 /* Let the overlay arrow be updated the next time. */
8953 if (!NILP (last_arrow_position))
8954 {
8955 last_arrow_position = Qt;
8956 last_arrow_string = Qt;
8957 }
8958
8959 /* If we pause after scrolling, some rows in the current
8960 matrices of some windows are not valid. */
8961 if (!WINDOW_FULL_WIDTH_P (w)
8962 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8963 update_mode_lines = 1;
8964 }
8965 else
8966 {
8967 if (!consider_all_windows_p)
8968 {
8969 /* This has already been done above if
8970 consider_all_windows_p is set. */
8971 mark_window_display_accurate_1 (w, 1);
8972
8973 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8974 last_arrow_string = Voverlay_arrow_string;
8975
8976 if (frame_up_to_date_hook != 0)
8977 frame_up_to_date_hook (sf);
8978 }
8979
8980 update_mode_lines = 0;
8981 windows_or_buffers_changed = 0;
8982 }
8983
8984 /* Start SIGIO interrupts coming again. Having them off during the
8985 code above makes it less likely one will discard output, but not
8986 impossible, since there might be stuff in the system buffer here.
8987 But it is much hairier to try to do anything about that. */
8988 if (interrupt_input)
8989 request_sigio ();
8990 start_polling ();
8991
8992 /* If a frame has become visible which was not before, redisplay
8993 again, so that we display it. Expose events for such a frame
8994 (which it gets when becoming visible) don't call the parts of
8995 redisplay constructing glyphs, so simply exposing a frame won't
8996 display anything in this case. So, we have to display these
8997 frames here explicitly. */
8998 if (!pause)
8999 {
9000 Lisp_Object tail, frame;
9001 int new_count = 0;
9002
9003 FOR_EACH_FRAME (tail, frame)
9004 {
9005 int this_is_visible = 0;
9006
9007 if (XFRAME (frame)->visible)
9008 this_is_visible = 1;
9009 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9010 if (XFRAME (frame)->visible)
9011 this_is_visible = 1;
9012
9013 if (this_is_visible)
9014 new_count++;
9015 }
9016
9017 if (new_count != number_of_visible_frames)
9018 windows_or_buffers_changed++;
9019 }
9020
9021 /* Change frame size now if a change is pending. */
9022 do_pending_window_change (1);
9023
9024 /* If we just did a pending size change, or have additional
9025 visible frames, redisplay again. */
9026 if (windows_or_buffers_changed && !pause)
9027 goto retry;
9028
9029 end_of_redisplay:;
9030
9031 unbind_to (count, Qnil);
9032 }
9033
9034
9035 /* Redisplay, but leave alone any recent echo area message unless
9036 another message has been requested in its place.
9037
9038 This is useful in situations where you need to redisplay but no
9039 user action has occurred, making it inappropriate for the message
9040 area to be cleared. See tracking_off and
9041 wait_reading_process_input for examples of these situations.
9042
9043 FROM_WHERE is an integer saying from where this function was
9044 called. This is useful for debugging. */
9045
9046 void
9047 redisplay_preserve_echo_area (from_where)
9048 int from_where;
9049 {
9050 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9051
9052 if (!NILP (echo_area_buffer[1]))
9053 {
9054 /* We have a previously displayed message, but no current
9055 message. Redisplay the previous message. */
9056 display_last_displayed_message_p = 1;
9057 redisplay_internal (1);
9058 display_last_displayed_message_p = 0;
9059 }
9060 else
9061 redisplay_internal (1);
9062 }
9063
9064
9065 /* Function registered with record_unwind_protect in
9066 redisplay_internal. Clears the flag indicating that a redisplay is
9067 in progress. */
9068
9069 static Lisp_Object
9070 unwind_redisplay (old_redisplaying_p)
9071 Lisp_Object old_redisplaying_p;
9072 {
9073 redisplaying_p = XFASTINT (old_redisplaying_p);
9074 return Qnil;
9075 }
9076
9077
9078 /* Mark the display of window W as accurate or inaccurate. If
9079 ACCURATE_P is non-zero mark display of W as accurate. If
9080 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9081 redisplay_internal is called. */
9082
9083 static void
9084 mark_window_display_accurate_1 (w, accurate_p)
9085 struct window *w;
9086 int accurate_p;
9087 {
9088 if (BUFFERP (w->buffer))
9089 {
9090 struct buffer *b = XBUFFER (w->buffer);
9091
9092 w->last_modified
9093 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9094 w->last_overlay_modified
9095 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9096 w->last_had_star
9097 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9098
9099 if (accurate_p)
9100 {
9101 b->clip_changed = 0;
9102 b->prevent_redisplay_optimizations_p = 0;
9103
9104 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9105 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9106 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9107 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9108
9109 w->current_matrix->buffer = b;
9110 w->current_matrix->begv = BUF_BEGV (b);
9111 w->current_matrix->zv = BUF_ZV (b);
9112
9113 w->last_cursor = w->cursor;
9114 w->last_cursor_off_p = w->cursor_off_p;
9115
9116 if (w == XWINDOW (selected_window))
9117 w->last_point = make_number (BUF_PT (b));
9118 else
9119 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9120 }
9121 }
9122
9123 if (accurate_p)
9124 {
9125 w->window_end_valid = w->buffer;
9126 #if 0 /* This is incorrect with variable-height lines. */
9127 xassert (XINT (w->window_end_vpos)
9128 < (XINT (w->height)
9129 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9130 #endif
9131 w->update_mode_line = Qnil;
9132 }
9133 }
9134
9135
9136 /* Mark the display of windows in the window tree rooted at WINDOW as
9137 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9138 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9139 be redisplayed the next time redisplay_internal is called. */
9140
9141 void
9142 mark_window_display_accurate (window, accurate_p)
9143 Lisp_Object window;
9144 int accurate_p;
9145 {
9146 struct window *w;
9147
9148 for (; !NILP (window); window = w->next)
9149 {
9150 w = XWINDOW (window);
9151 mark_window_display_accurate_1 (w, accurate_p);
9152
9153 if (!NILP (w->vchild))
9154 mark_window_display_accurate (w->vchild, accurate_p);
9155 if (!NILP (w->hchild))
9156 mark_window_display_accurate (w->hchild, accurate_p);
9157 }
9158
9159 if (accurate_p)
9160 {
9161 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9162 last_arrow_string = Voverlay_arrow_string;
9163 }
9164 else
9165 {
9166 /* Force a thorough redisplay the next time by setting
9167 last_arrow_position and last_arrow_string to t, which is
9168 unequal to any useful value of Voverlay_arrow_... */
9169 last_arrow_position = Qt;
9170 last_arrow_string = Qt;
9171 }
9172 }
9173
9174
9175 /* Return value in display table DP (Lisp_Char_Table *) for character
9176 C. Since a display table doesn't have any parent, we don't have to
9177 follow parent. Do not call this function directly but use the
9178 macro DISP_CHAR_VECTOR. */
9179
9180 Lisp_Object
9181 disp_char_vector (dp, c)
9182 struct Lisp_Char_Table *dp;
9183 int c;
9184 {
9185 int code[4], i;
9186 Lisp_Object val;
9187
9188 if (SINGLE_BYTE_CHAR_P (c))
9189 return (dp->contents[c]);
9190
9191 SPLIT_CHAR (c, code[0], code[1], code[2]);
9192 if (code[1] < 32)
9193 code[1] = -1;
9194 else if (code[2] < 32)
9195 code[2] = -1;
9196
9197 /* Here, the possible range of code[0] (== charset ID) is
9198 128..max_charset. Since the top level char table contains data
9199 for multibyte characters after 256th element, we must increment
9200 code[0] by 128 to get a correct index. */
9201 code[0] += 128;
9202 code[3] = -1; /* anchor */
9203
9204 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9205 {
9206 val = dp->contents[code[i]];
9207 if (!SUB_CHAR_TABLE_P (val))
9208 return (NILP (val) ? dp->defalt : val);
9209 }
9210
9211 /* Here, val is a sub char table. We return the default value of
9212 it. */
9213 return (dp->defalt);
9214 }
9215
9216
9217 \f
9218 /***********************************************************************
9219 Window Redisplay
9220 ***********************************************************************/
9221
9222 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9223
9224 static void
9225 redisplay_windows (window)
9226 Lisp_Object window;
9227 {
9228 while (!NILP (window))
9229 {
9230 struct window *w = XWINDOW (window);
9231
9232 if (!NILP (w->hchild))
9233 redisplay_windows (w->hchild);
9234 else if (!NILP (w->vchild))
9235 redisplay_windows (w->vchild);
9236 else
9237 {
9238 displayed_buffer = XBUFFER (w->buffer);
9239 internal_condition_case_1 (redisplay_window_0, window, Qerror,
9240 redisplay_window_error);
9241 }
9242
9243 window = w->next;
9244 }
9245 }
9246
9247 static Lisp_Object
9248 redisplay_window_error ()
9249 {
9250 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9251 return Qnil;
9252 }
9253
9254 static Lisp_Object
9255 redisplay_window_0 (window)
9256 Lisp_Object window;
9257 {
9258 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9259 redisplay_window (window, 0);
9260 return Qnil;
9261 }
9262
9263 static Lisp_Object
9264 redisplay_window_1 (window)
9265 Lisp_Object window;
9266 {
9267 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9268 redisplay_window (window, 1);
9269 return Qnil;
9270 }
9271 \f
9272 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9273 DELTA is the number of bytes by which positions recorded in ROW
9274 differ from current buffer positions. */
9275
9276 void
9277 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9278 struct window *w;
9279 struct glyph_row *row;
9280 struct glyph_matrix *matrix;
9281 int delta, delta_bytes, dy, dvpos;
9282 {
9283 struct glyph *glyph = row->glyphs[TEXT_AREA];
9284 struct glyph *end = glyph + row->used[TEXT_AREA];
9285 int x = row->x;
9286 int pt_old = PT - delta;
9287
9288 /* Skip over glyphs not having an object at the start of the row.
9289 These are special glyphs like truncation marks on terminal
9290 frames. */
9291 if (row->displays_text_p)
9292 while (glyph < end
9293 && INTEGERP (glyph->object)
9294 && glyph->charpos < 0)
9295 {
9296 x += glyph->pixel_width;
9297 ++glyph;
9298 }
9299
9300 while (glyph < end
9301 && !INTEGERP (glyph->object)
9302 && (!BUFFERP (glyph->object)
9303 || glyph->charpos < pt_old))
9304 {
9305 x += glyph->pixel_width;
9306 ++glyph;
9307 }
9308
9309 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9310 w->cursor.x = x;
9311 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9312 w->cursor.y = row->y + dy;
9313
9314 if (w == XWINDOW (selected_window))
9315 {
9316 if (!row->continued_p
9317 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9318 && row->x == 0)
9319 {
9320 this_line_buffer = XBUFFER (w->buffer);
9321
9322 CHARPOS (this_line_start_pos)
9323 = MATRIX_ROW_START_CHARPOS (row) + delta;
9324 BYTEPOS (this_line_start_pos)
9325 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9326
9327 CHARPOS (this_line_end_pos)
9328 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9329 BYTEPOS (this_line_end_pos)
9330 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9331
9332 this_line_y = w->cursor.y;
9333 this_line_pixel_height = row->height;
9334 this_line_vpos = w->cursor.vpos;
9335 this_line_start_x = row->x;
9336 }
9337 else
9338 CHARPOS (this_line_start_pos) = 0;
9339 }
9340 }
9341
9342
9343 /* Run window scroll functions, if any, for WINDOW with new window
9344 start STARTP. Sets the window start of WINDOW to that position.
9345
9346 We assume that the window's buffer is really current. */
9347
9348 static INLINE struct text_pos
9349 run_window_scroll_functions (window, startp)
9350 Lisp_Object window;
9351 struct text_pos startp;
9352 {
9353 struct window *w = XWINDOW (window);
9354 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9355
9356 if (current_buffer != XBUFFER (w->buffer))
9357 abort ();
9358
9359 if (!NILP (Vwindow_scroll_functions))
9360 {
9361 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9362 make_number (CHARPOS (startp)));
9363 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9364 /* In case the hook functions switch buffers. */
9365 if (current_buffer != XBUFFER (w->buffer))
9366 set_buffer_internal_1 (XBUFFER (w->buffer));
9367 }
9368
9369 return startp;
9370 }
9371
9372
9373 /* Modify the desired matrix of window W and W->vscroll so that the
9374 line containing the cursor is fully visible. If this requires
9375 larger matrices than are allocated, set fonts_changed_p and return
9376 0. */
9377
9378 static int
9379 make_cursor_line_fully_visible (w)
9380 struct window *w;
9381 {
9382 struct glyph_matrix *matrix;
9383 struct glyph_row *row;
9384 int window_height;
9385
9386 /* It's not always possible to find the cursor, e.g, when a window
9387 is full of overlay strings. Don't do anything in that case. */
9388 if (w->cursor.vpos < 0)
9389 return 1;
9390
9391 matrix = w->desired_matrix;
9392 row = MATRIX_ROW (matrix, w->cursor.vpos);
9393
9394 /* If the cursor row is not partially visible, there's nothing
9395 to do. */
9396 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9397 return 1;
9398
9399 /* If the row the cursor is in is taller than the window's height,
9400 it's not clear what to do, so do nothing. */
9401 window_height = window_box_height (w);
9402 if (row->height >= window_height)
9403 return 1;
9404
9405 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9406 {
9407 int dy = row->height - row->visible_height;
9408 w->vscroll = 0;
9409 w->cursor.y += dy;
9410 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9411 }
9412 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9413 {
9414 int dy = - (row->height - row->visible_height);
9415 w->vscroll = dy;
9416 w->cursor.y += dy;
9417 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9418 }
9419
9420 /* When we change the cursor y-position of the selected window,
9421 change this_line_y as well so that the display optimization for
9422 the cursor line of the selected window in redisplay_internal uses
9423 the correct y-position. */
9424 if (w == XWINDOW (selected_window))
9425 this_line_y = w->cursor.y;
9426
9427 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9428 redisplay with larger matrices. */
9429 if (matrix->nrows < required_matrix_height (w))
9430 {
9431 fonts_changed_p = 1;
9432 return 0;
9433 }
9434
9435 return 1;
9436 }
9437
9438
9439 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9440 non-zero means only WINDOW is redisplayed in redisplay_internal.
9441 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9442 in redisplay_window to bring a partially visible line into view in
9443 the case that only the cursor has moved.
9444
9445 Value is
9446
9447 1 if scrolling succeeded
9448
9449 0 if scrolling didn't find point.
9450
9451 -1 if new fonts have been loaded so that we must interrupt
9452 redisplay, adjust glyph matrices, and try again. */
9453
9454 enum
9455 {
9456 SCROLLING_SUCCESS,
9457 SCROLLING_FAILED,
9458 SCROLLING_NEED_LARGER_MATRICES
9459 };
9460
9461 static int
9462 try_scrolling (window, just_this_one_p, scroll_conservatively,
9463 scroll_step, temp_scroll_step)
9464 Lisp_Object window;
9465 int just_this_one_p;
9466 int scroll_conservatively, scroll_step;
9467 int temp_scroll_step;
9468 {
9469 struct window *w = XWINDOW (window);
9470 struct frame *f = XFRAME (w->frame);
9471 struct text_pos scroll_margin_pos;
9472 struct text_pos pos;
9473 struct text_pos startp;
9474 struct it it;
9475 Lisp_Object window_end;
9476 int this_scroll_margin;
9477 int dy = 0;
9478 int scroll_max;
9479 int rc;
9480 int amount_to_scroll = 0;
9481 Lisp_Object aggressive;
9482 int height;
9483
9484 #if GLYPH_DEBUG
9485 debug_method_add (w, "try_scrolling");
9486 #endif
9487
9488 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9489
9490 /* Compute scroll margin height in pixels. We scroll when point is
9491 within this distance from the top or bottom of the window. */
9492 if (scroll_margin > 0)
9493 {
9494 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9495 this_scroll_margin *= CANON_Y_UNIT (f);
9496 }
9497 else
9498 this_scroll_margin = 0;
9499
9500 /* Compute how much we should try to scroll maximally to bring point
9501 into view. */
9502 if (scroll_step || scroll_conservatively || temp_scroll_step)
9503 scroll_max = max (scroll_step,
9504 max (scroll_conservatively, temp_scroll_step));
9505 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9506 || NUMBERP (current_buffer->scroll_up_aggressively))
9507 /* We're trying to scroll because of aggressive scrolling
9508 but no scroll_step is set. Choose an arbitrary one. Maybe
9509 there should be a variable for this. */
9510 scroll_max = 10;
9511 else
9512 scroll_max = 0;
9513 scroll_max *= CANON_Y_UNIT (f);
9514
9515 /* Decide whether we have to scroll down. Start at the window end
9516 and move this_scroll_margin up to find the position of the scroll
9517 margin. */
9518 window_end = Fwindow_end (window, Qt);
9519 CHARPOS (scroll_margin_pos) = XINT (window_end);
9520 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9521 if (this_scroll_margin)
9522 {
9523 start_display (&it, w, scroll_margin_pos);
9524 move_it_vertically (&it, - this_scroll_margin);
9525 scroll_margin_pos = it.current.pos;
9526 }
9527
9528 if (PT >= CHARPOS (scroll_margin_pos))
9529 {
9530 int y0;
9531
9532 /* Point is in the scroll margin at the bottom of the window, or
9533 below. Compute a new window start that makes point visible. */
9534
9535 /* Compute the distance from the scroll margin to PT.
9536 Give up if the distance is greater than scroll_max. */
9537 start_display (&it, w, scroll_margin_pos);
9538 y0 = it.current_y;
9539 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9540 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9541
9542 /* To make point visible, we have to move the window start
9543 down so that the line the cursor is in is visible, which
9544 means we have to add in the height of the cursor line. */
9545 dy = line_bottom_y (&it) - y0;
9546
9547 if (dy > scroll_max)
9548 return SCROLLING_FAILED;
9549
9550 /* Move the window start down. If scrolling conservatively,
9551 move it just enough down to make point visible. If
9552 scroll_step is set, move it down by scroll_step. */
9553 start_display (&it, w, startp);
9554
9555 if (scroll_conservatively)
9556 amount_to_scroll
9557 = max (max (dy, CANON_Y_UNIT (f)),
9558 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9559 else if (scroll_step || temp_scroll_step)
9560 amount_to_scroll = scroll_max;
9561 else
9562 {
9563 aggressive = current_buffer->scroll_up_aggressively;
9564 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9565 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9566 if (NUMBERP (aggressive))
9567 amount_to_scroll = XFLOATINT (aggressive) * height;
9568 }
9569
9570 if (amount_to_scroll <= 0)
9571 return SCROLLING_FAILED;
9572
9573 move_it_vertically (&it, amount_to_scroll);
9574 startp = it.current.pos;
9575 }
9576 else
9577 {
9578 /* See if point is inside the scroll margin at the top of the
9579 window. */
9580 scroll_margin_pos = startp;
9581 if (this_scroll_margin)
9582 {
9583 start_display (&it, w, startp);
9584 move_it_vertically (&it, this_scroll_margin);
9585 scroll_margin_pos = it.current.pos;
9586 }
9587
9588 if (PT < CHARPOS (scroll_margin_pos))
9589 {
9590 /* Point is in the scroll margin at the top of the window or
9591 above what is displayed in the window. */
9592 int y0;
9593
9594 /* Compute the vertical distance from PT to the scroll
9595 margin position. Give up if distance is greater than
9596 scroll_max. */
9597 SET_TEXT_POS (pos, PT, PT_BYTE);
9598 start_display (&it, w, pos);
9599 y0 = it.current_y;
9600 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9601 it.last_visible_y, -1,
9602 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9603 dy = it.current_y - y0;
9604 if (dy > scroll_max)
9605 return SCROLLING_FAILED;
9606
9607 /* Compute new window start. */
9608 start_display (&it, w, startp);
9609
9610 if (scroll_conservatively)
9611 amount_to_scroll =
9612 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9613 else if (scroll_step || temp_scroll_step)
9614 amount_to_scroll = scroll_max;
9615 else
9616 {
9617 aggressive = current_buffer->scroll_down_aggressively;
9618 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9619 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9620 if (NUMBERP (aggressive))
9621 amount_to_scroll = XFLOATINT (aggressive) * height;
9622 }
9623
9624 if (amount_to_scroll <= 0)
9625 return SCROLLING_FAILED;
9626
9627 move_it_vertically (&it, - amount_to_scroll);
9628 startp = it.current.pos;
9629 }
9630 }
9631
9632 /* Run window scroll functions. */
9633 startp = run_window_scroll_functions (window, startp);
9634
9635 /* Display the window. Give up if new fonts are loaded, or if point
9636 doesn't appear. */
9637 if (!try_window (window, startp))
9638 rc = SCROLLING_NEED_LARGER_MATRICES;
9639 else if (w->cursor.vpos < 0)
9640 {
9641 clear_glyph_matrix (w->desired_matrix);
9642 rc = SCROLLING_FAILED;
9643 }
9644 else
9645 {
9646 /* Maybe forget recorded base line for line number display. */
9647 if (!just_this_one_p
9648 || current_buffer->clip_changed
9649 || BEG_UNCHANGED < CHARPOS (startp))
9650 w->base_line_number = Qnil;
9651
9652 /* If cursor ends up on a partially visible line, shift display
9653 lines up or down. If that fails because we need larger
9654 matrices, give up. */
9655 if (!make_cursor_line_fully_visible (w))
9656 rc = SCROLLING_NEED_LARGER_MATRICES;
9657 else
9658 rc = SCROLLING_SUCCESS;
9659 }
9660
9661 return rc;
9662 }
9663
9664
9665 /* Compute a suitable window start for window W if display of W starts
9666 on a continuation line. Value is non-zero if a new window start
9667 was computed.
9668
9669 The new window start will be computed, based on W's width, starting
9670 from the start of the continued line. It is the start of the
9671 screen line with the minimum distance from the old start W->start. */
9672
9673 static int
9674 compute_window_start_on_continuation_line (w)
9675 struct window *w;
9676 {
9677 struct text_pos pos, start_pos;
9678 int window_start_changed_p = 0;
9679
9680 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9681
9682 /* If window start is on a continuation line... Window start may be
9683 < BEGV in case there's invisible text at the start of the
9684 buffer (M-x rmail, for example). */
9685 if (CHARPOS (start_pos) > BEGV
9686 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9687 {
9688 struct it it;
9689 struct glyph_row *row;
9690
9691 /* Handle the case that the window start is out of range. */
9692 if (CHARPOS (start_pos) < BEGV)
9693 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9694 else if (CHARPOS (start_pos) > ZV)
9695 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9696
9697 /* Find the start of the continued line. This should be fast
9698 because scan_buffer is fast (newline cache). */
9699 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9700 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9701 row, DEFAULT_FACE_ID);
9702 reseat_at_previous_visible_line_start (&it);
9703
9704 /* If the line start is "too far" away from the window start,
9705 say it takes too much time to compute a new window start. */
9706 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9707 < XFASTINT (w->height) * XFASTINT (w->width))
9708 {
9709 int min_distance, distance;
9710
9711 /* Move forward by display lines to find the new window
9712 start. If window width was enlarged, the new start can
9713 be expected to be > the old start. If window width was
9714 decreased, the new window start will be < the old start.
9715 So, we're looking for the display line start with the
9716 minimum distance from the old window start. */
9717 pos = it.current.pos;
9718 min_distance = INFINITY;
9719 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9720 distance < min_distance)
9721 {
9722 min_distance = distance;
9723 pos = it.current.pos;
9724 move_it_by_lines (&it, 1, 0);
9725 }
9726
9727 /* Set the window start there. */
9728 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9729 window_start_changed_p = 1;
9730 }
9731 }
9732
9733 return window_start_changed_p;
9734 }
9735
9736
9737 /* Try cursor movement in case text has not changes in window WINDOW,
9738 with window start STARTP. Value is
9739
9740 CURSOR_MOVEMENT_SUCCESS if successful
9741
9742 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9743
9744 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9745 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9746 we want to scroll as if scroll-step were set to 1. See the code.
9747
9748 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9749 which case we have to abort this redisplay, and adjust matrices
9750 first. */
9751
9752 enum
9753 {
9754 CURSOR_MOVEMENT_SUCCESS,
9755 CURSOR_MOVEMENT_CANNOT_BE_USED,
9756 CURSOR_MOVEMENT_MUST_SCROLL,
9757 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9758 };
9759
9760 static int
9761 try_cursor_movement (window, startp, scroll_step)
9762 Lisp_Object window;
9763 struct text_pos startp;
9764 int *scroll_step;
9765 {
9766 struct window *w = XWINDOW (window);
9767 struct frame *f = XFRAME (w->frame);
9768 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
9769
9770 #if GLYPH_DEBUG
9771 if (inhibit_try_cursor_movement)
9772 return rc;
9773 #endif
9774
9775 /* Handle case where text has not changed, only point, and it has
9776 not moved off the frame. */
9777 if (/* Point may be in this window. */
9778 PT >= CHARPOS (startp)
9779 /* Selective display hasn't changed. */
9780 && !current_buffer->clip_changed
9781 /* Function force-mode-line-update is used to force a thorough
9782 redisplay. It sets either windows_or_buffers_changed or
9783 update_mode_lines. So don't take a shortcut here for these
9784 cases. */
9785 && !update_mode_lines
9786 && !windows_or_buffers_changed
9787 /* Can't use this case if highlighting a region. When a
9788 region exists, cursor movement has to do more than just
9789 set the cursor. */
9790 && !(!NILP (Vtransient_mark_mode)
9791 && !NILP (current_buffer->mark_active))
9792 && NILP (w->region_showing)
9793 && NILP (Vshow_trailing_whitespace)
9794 /* Right after splitting windows, last_point may be nil. */
9795 && INTEGERP (w->last_point)
9796 /* This code is not used for mini-buffer for the sake of the case
9797 of redisplaying to replace an echo area message; since in
9798 that case the mini-buffer contents per se are usually
9799 unchanged. This code is of no real use in the mini-buffer
9800 since the handling of this_line_start_pos, etc., in redisplay
9801 handles the same cases. */
9802 && !EQ (window, minibuf_window)
9803 /* When splitting windows or for new windows, it happens that
9804 redisplay is called with a nil window_end_vpos or one being
9805 larger than the window. This should really be fixed in
9806 window.c. I don't have this on my list, now, so we do
9807 approximately the same as the old redisplay code. --gerd. */
9808 && INTEGERP (w->window_end_vpos)
9809 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9810 && (FRAME_WINDOW_P (f)
9811 || !MARKERP (Voverlay_arrow_position)
9812 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9813 {
9814 int this_scroll_margin;
9815 struct glyph_row *row = NULL;
9816
9817 #if GLYPH_DEBUG
9818 debug_method_add (w, "cursor movement");
9819 #endif
9820
9821 /* Scroll if point within this distance from the top or bottom
9822 of the window. This is a pixel value. */
9823 this_scroll_margin = max (0, scroll_margin);
9824 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9825 this_scroll_margin *= CANON_Y_UNIT (f);
9826
9827 /* Start with the row the cursor was displayed during the last
9828 not paused redisplay. Give up if that row is not valid. */
9829 if (w->last_cursor.vpos < 0
9830 || w->last_cursor.vpos >= w->current_matrix->nrows)
9831 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9832 else
9833 {
9834 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9835 if (row->mode_line_p)
9836 ++row;
9837 if (!row->enabled_p)
9838 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9839 }
9840
9841 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
9842 {
9843 int scroll_p = 0;
9844 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9845
9846 if (PT > XFASTINT (w->last_point))
9847 {
9848 /* Point has moved forward. */
9849 while (MATRIX_ROW_END_CHARPOS (row) < PT
9850 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9851 {
9852 xassert (row->enabled_p);
9853 ++row;
9854 }
9855
9856 /* The end position of a row equals the start position
9857 of the next row. If PT is there, we would rather
9858 display it in the next line. */
9859 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9860 && MATRIX_ROW_END_CHARPOS (row) == PT
9861 && !cursor_row_p (w, row))
9862 ++row;
9863
9864 /* If within the scroll margin, scroll. Note that
9865 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9866 the next line would be drawn, and that
9867 this_scroll_margin can be zero. */
9868 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9869 || PT > MATRIX_ROW_END_CHARPOS (row)
9870 /* Line is completely visible last line in window
9871 and PT is to be set in the next line. */
9872 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9873 && PT == MATRIX_ROW_END_CHARPOS (row)
9874 && !row->ends_at_zv_p
9875 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9876 scroll_p = 1;
9877 }
9878 else if (PT < XFASTINT (w->last_point))
9879 {
9880 /* Cursor has to be moved backward. Note that PT >=
9881 CHARPOS (startp) because of the outer
9882 if-statement. */
9883 while (!row->mode_line_p
9884 && (MATRIX_ROW_START_CHARPOS (row) > PT
9885 || (MATRIX_ROW_START_CHARPOS (row) == PT
9886 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9887 && (row->y > this_scroll_margin
9888 || CHARPOS (startp) == BEGV))
9889 {
9890 xassert (row->enabled_p);
9891 --row;
9892 }
9893
9894 /* Consider the following case: Window starts at BEGV,
9895 there is invisible, intangible text at BEGV, so that
9896 display starts at some point START > BEGV. It can
9897 happen that we are called with PT somewhere between
9898 BEGV and START. Try to handle that case. */
9899 if (row < w->current_matrix->rows
9900 || row->mode_line_p)
9901 {
9902 row = w->current_matrix->rows;
9903 if (row->mode_line_p)
9904 ++row;
9905 }
9906
9907 /* Due to newlines in overlay strings, we may have to
9908 skip forward over overlay strings. */
9909 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9910 && MATRIX_ROW_END_CHARPOS (row) == PT
9911 && !cursor_row_p (w, row))
9912 ++row;
9913
9914 /* If within the scroll margin, scroll. */
9915 if (row->y < this_scroll_margin
9916 && CHARPOS (startp) != BEGV)
9917 scroll_p = 1;
9918 }
9919
9920 if (PT < MATRIX_ROW_START_CHARPOS (row)
9921 || PT > MATRIX_ROW_END_CHARPOS (row))
9922 {
9923 /* if PT is not in the glyph row, give up. */
9924 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9925 }
9926 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9927 {
9928 if (PT == MATRIX_ROW_END_CHARPOS (row)
9929 && !row->ends_at_zv_p
9930 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
9931 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9932 else if (row->height > window_box_height (w))
9933 {
9934 /* If we end up in a partially visible line, let's
9935 make it fully visible, except when it's taller
9936 than the window, in which case we can't do much
9937 about it. */
9938 *scroll_step = 1;
9939 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9940 }
9941 else
9942 {
9943 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9944 try_window (window, startp);
9945 if (!make_cursor_line_fully_visible (w))
9946 rc = CURSOR_MOVEMENT_NEED_LARGER_MATRICES;
9947 else
9948 rc = CURSOR_MOVEMENT_SUCCESS;
9949 }
9950 }
9951 else if (scroll_p)
9952 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9953 else
9954 {
9955 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9956 rc = CURSOR_MOVEMENT_SUCCESS;
9957 }
9958 }
9959 }
9960
9961 return rc;
9962 }
9963
9964
9965 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9966 selected_window is redisplayed. */
9967
9968 static void
9969 redisplay_window (window, just_this_one_p)
9970 Lisp_Object window;
9971 int just_this_one_p;
9972 {
9973 struct window *w = XWINDOW (window);
9974 struct frame *f = XFRAME (w->frame);
9975 struct buffer *buffer = XBUFFER (w->buffer);
9976 struct buffer *old = current_buffer;
9977 struct text_pos lpoint, opoint, startp;
9978 int update_mode_line;
9979 int tem;
9980 struct it it;
9981 /* Record it now because it's overwritten. */
9982 int current_matrix_up_to_date_p = 0;
9983 int temp_scroll_step = 0;
9984 int count = BINDING_STACK_SIZE ();
9985 int rc;
9986
9987 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9988 opoint = lpoint;
9989
9990 /* W must be a leaf window here. */
9991 xassert (!NILP (w->buffer));
9992 #if GLYPH_DEBUG
9993 *w->desired_matrix->method = 0;
9994 #endif
9995
9996 specbind (Qinhibit_point_motion_hooks, Qt);
9997
9998 reconsider_clip_changes (w, buffer);
9999
10000 /* Has the mode line to be updated? */
10001 update_mode_line = (!NILP (w->update_mode_line)
10002 || update_mode_lines
10003 || buffer->clip_changed);
10004
10005 if (MINI_WINDOW_P (w))
10006 {
10007 if (w == XWINDOW (echo_area_window)
10008 && !NILP (echo_area_buffer[0]))
10009 {
10010 if (update_mode_line)
10011 /* We may have to update a tty frame's menu bar or a
10012 tool-bar. Example `M-x C-h C-h C-g'. */
10013 goto finish_menu_bars;
10014 else
10015 /* We've already displayed the echo area glyphs in this window. */
10016 goto finish_scroll_bars;
10017 }
10018 else if (w != XWINDOW (minibuf_window))
10019 {
10020 /* W is a mini-buffer window, but it's not the currently
10021 active one, so clear it. */
10022 int yb = window_text_bottom_y (w);
10023 struct glyph_row *row;
10024 int y;
10025
10026 for (y = 0, row = w->desired_matrix->rows;
10027 y < yb;
10028 y += row->height, ++row)
10029 blank_row (w, row, y);
10030 goto finish_scroll_bars;
10031 }
10032
10033 clear_glyph_matrix (w->desired_matrix);
10034 }
10035
10036 /* Otherwise set up data on this window; select its buffer and point
10037 value. */
10038 /* Really select the buffer, for the sake of buffer-local
10039 variables. */
10040 set_buffer_internal_1 (XBUFFER (w->buffer));
10041 SET_TEXT_POS (opoint, PT, PT_BYTE);
10042
10043 current_matrix_up_to_date_p
10044 = (!NILP (w->window_end_valid)
10045 && !current_buffer->clip_changed
10046 && XFASTINT (w->last_modified) >= MODIFF
10047 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10048
10049 /* When windows_or_buffers_changed is non-zero, we can't rely on
10050 the window end being valid, so set it to nil there. */
10051 if (windows_or_buffers_changed)
10052 {
10053 /* If window starts on a continuation line, maybe adjust the
10054 window start in case the window's width changed. */
10055 if (XMARKER (w->start)->buffer == current_buffer)
10056 compute_window_start_on_continuation_line (w);
10057
10058 w->window_end_valid = Qnil;
10059 }
10060
10061 /* Some sanity checks. */
10062 CHECK_WINDOW_END (w);
10063 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
10064 abort ();
10065 if (BYTEPOS (opoint) < CHARPOS (opoint))
10066 abort ();
10067
10068 /* If %c is in mode line, update it if needed. */
10069 if (!NILP (w->column_number_displayed)
10070 /* This alternative quickly identifies a common case
10071 where no change is needed. */
10072 && !(PT == XFASTINT (w->last_point)
10073 && XFASTINT (w->last_modified) >= MODIFF
10074 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10075 && XFASTINT (w->column_number_displayed) != current_column ())
10076 update_mode_line = 1;
10077
10078 /* Count number of windows showing the selected buffer. An indirect
10079 buffer counts as its base buffer. */
10080 if (!just_this_one_p)
10081 {
10082 struct buffer *current_base, *window_base;
10083 current_base = current_buffer;
10084 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10085 if (current_base->base_buffer)
10086 current_base = current_base->base_buffer;
10087 if (window_base->base_buffer)
10088 window_base = window_base->base_buffer;
10089 if (current_base == window_base)
10090 buffer_shared++;
10091 }
10092
10093 /* Point refers normally to the selected window. For any other
10094 window, set up appropriate value. */
10095 if (!EQ (window, selected_window))
10096 {
10097 int new_pt = XMARKER (w->pointm)->charpos;
10098 int new_pt_byte = marker_byte_position (w->pointm);
10099 if (new_pt < BEGV)
10100 {
10101 new_pt = BEGV;
10102 new_pt_byte = BEGV_BYTE;
10103 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10104 }
10105 else if (new_pt > (ZV - 1))
10106 {
10107 new_pt = ZV;
10108 new_pt_byte = ZV_BYTE;
10109 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10110 }
10111
10112 /* We don't use SET_PT so that the point-motion hooks don't run. */
10113 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10114 }
10115
10116 /* If any of the character widths specified in the display table
10117 have changed, invalidate the width run cache. It's true that
10118 this may be a bit late to catch such changes, but the rest of
10119 redisplay goes (non-fatally) haywire when the display table is
10120 changed, so why should we worry about doing any better? */
10121 if (current_buffer->width_run_cache)
10122 {
10123 struct Lisp_Char_Table *disptab = buffer_display_table ();
10124
10125 if (! disptab_matches_widthtab (disptab,
10126 XVECTOR (current_buffer->width_table)))
10127 {
10128 invalidate_region_cache (current_buffer,
10129 current_buffer->width_run_cache,
10130 BEG, Z);
10131 recompute_width_table (current_buffer, disptab);
10132 }
10133 }
10134
10135 /* If window-start is screwed up, choose a new one. */
10136 if (XMARKER (w->start)->buffer != current_buffer)
10137 goto recenter;
10138
10139 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10140
10141 /* If someone specified a new starting point but did not insist,
10142 check whether it can be used. */
10143 if (!NILP (w->optional_new_start)
10144 && CHARPOS (startp) >= BEGV
10145 && CHARPOS (startp) <= ZV)
10146 {
10147 w->optional_new_start = Qnil;
10148 start_display (&it, w, startp);
10149 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10150 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10151 if (IT_CHARPOS (it) == PT)
10152 w->force_start = Qt;
10153 }
10154
10155 /* Handle case where place to start displaying has been specified,
10156 unless the specified location is outside the accessible range. */
10157 if (!NILP (w->force_start)
10158 || w->frozen_window_start_p)
10159 {
10160 w->force_start = Qnil;
10161 w->vscroll = 0;
10162 w->window_end_valid = Qnil;
10163
10164 /* Forget any recorded base line for line number display. */
10165 if (!current_matrix_up_to_date_p
10166 || current_buffer->clip_changed)
10167 w->base_line_number = Qnil;
10168
10169 /* Redisplay the mode line. Select the buffer properly for that.
10170 Also, run the hook window-scroll-functions
10171 because we have scrolled. */
10172 /* Note, we do this after clearing force_start because
10173 if there's an error, it is better to forget about force_start
10174 than to get into an infinite loop calling the hook functions
10175 and having them get more errors. */
10176 if (!update_mode_line
10177 || ! NILP (Vwindow_scroll_functions))
10178 {
10179 update_mode_line = 1;
10180 w->update_mode_line = Qt;
10181 startp = run_window_scroll_functions (window, startp);
10182 }
10183
10184 w->last_modified = make_number (0);
10185 w->last_overlay_modified = make_number (0);
10186 if (CHARPOS (startp) < BEGV)
10187 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10188 else if (CHARPOS (startp) > ZV)
10189 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10190
10191 /* Redisplay, then check if cursor has been set during the
10192 redisplay. Give up if new fonts were loaded. */
10193 if (!try_window (window, startp))
10194 {
10195 w->force_start = Qt;
10196 clear_glyph_matrix (w->desired_matrix);
10197 goto finish_scroll_bars;
10198 }
10199
10200 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10201 {
10202 /* If point does not appear, try to move point so it does
10203 appear. The desired matrix has been built above, so we
10204 can use it here. */
10205 int window_height;
10206 struct glyph_row *row;
10207
10208 window_height = window_box_height (w) / 2;
10209 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10210 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
10211 ++row;
10212
10213 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10214 MATRIX_ROW_START_BYTEPOS (row));
10215
10216 if (w != XWINDOW (selected_window))
10217 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10218 else if (current_buffer == old)
10219 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10220
10221 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10222
10223 /* If we are highlighting the region, then we just changed
10224 the region, so redisplay to show it. */
10225 if (!NILP (Vtransient_mark_mode)
10226 && !NILP (current_buffer->mark_active))
10227 {
10228 clear_glyph_matrix (w->desired_matrix);
10229 if (!try_window (window, startp))
10230 goto need_larger_matrices;
10231 }
10232 }
10233
10234 if (!make_cursor_line_fully_visible (w))
10235 goto need_larger_matrices;
10236 #if GLYPH_DEBUG
10237 debug_method_add (w, "forced window start");
10238 #endif
10239 goto done;
10240 }
10241
10242 /* Handle case where text has not changed, only point, and it has
10243 not moved off the frame. */
10244 if (current_matrix_up_to_date_p
10245 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10246 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10247 {
10248 switch (rc)
10249 {
10250 case CURSOR_MOVEMENT_SUCCESS:
10251 goto done;
10252
10253 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10254 goto need_larger_matrices;
10255
10256 case CURSOR_MOVEMENT_MUST_SCROLL:
10257 goto try_to_scroll;
10258
10259 default:
10260 abort ();
10261 }
10262 }
10263 /* If current starting point was originally the beginning of a line
10264 but no longer is, find a new starting point. */
10265 else if (!NILP (w->start_at_line_beg)
10266 && !(CHARPOS (startp) <= BEGV
10267 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10268 {
10269 #if GLYPH_DEBUG
10270 debug_method_add (w, "recenter 1");
10271 #endif
10272 goto recenter;
10273 }
10274
10275 /* Try scrolling with try_window_id. Value is > 0 if update has
10276 been done, it is -1 if we know that the same window start will
10277 not work. It is 0 if unsuccessful for some other reason. */
10278 else if ((tem = try_window_id (w)) != 0)
10279 {
10280 #if GLYPH_DEBUG
10281 debug_method_add (w, "try_window_id %d", tem);
10282 #endif
10283
10284 if (fonts_changed_p)
10285 goto need_larger_matrices;
10286 if (tem > 0)
10287 goto done;
10288
10289 /* Otherwise try_window_id has returned -1 which means that we
10290 don't want the alternative below this comment to execute. */
10291 }
10292 else if (CHARPOS (startp) >= BEGV
10293 && CHARPOS (startp) <= ZV
10294 && PT >= CHARPOS (startp)
10295 && (CHARPOS (startp) < ZV
10296 /* Avoid starting at end of buffer. */
10297 || CHARPOS (startp) == BEGV
10298 || (XFASTINT (w->last_modified) >= MODIFF
10299 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10300 {
10301 #if GLYPH_DEBUG
10302 debug_method_add (w, "same window start");
10303 #endif
10304
10305 /* Try to redisplay starting at same place as before.
10306 If point has not moved off frame, accept the results. */
10307 if (!current_matrix_up_to_date_p
10308 /* Don't use try_window_reusing_current_matrix in this case
10309 because a window scroll function can have changed the
10310 buffer. */
10311 || !NILP (Vwindow_scroll_functions)
10312 || MINI_WINDOW_P (w)
10313 || !try_window_reusing_current_matrix (w))
10314 {
10315 IF_DEBUG (debug_method_add (w, "1"));
10316 try_window (window, startp);
10317 }
10318
10319 if (fonts_changed_p)
10320 goto need_larger_matrices;
10321
10322 if (w->cursor.vpos >= 0)
10323 {
10324 if (!just_this_one_p
10325 || current_buffer->clip_changed
10326 || BEG_UNCHANGED < CHARPOS (startp))
10327 /* Forget any recorded base line for line number display. */
10328 w->base_line_number = Qnil;
10329
10330 if (!make_cursor_line_fully_visible (w))
10331 goto need_larger_matrices;
10332 goto done;
10333 }
10334 else
10335 clear_glyph_matrix (w->desired_matrix);
10336 }
10337
10338 try_to_scroll:
10339
10340 w->last_modified = make_number (0);
10341 w->last_overlay_modified = make_number (0);
10342
10343 /* Redisplay the mode line. Select the buffer properly for that. */
10344 if (!update_mode_line)
10345 {
10346 update_mode_line = 1;
10347 w->update_mode_line = Qt;
10348 }
10349
10350 /* Try to scroll by specified few lines. */
10351 if ((scroll_conservatively
10352 || scroll_step
10353 || temp_scroll_step
10354 || NUMBERP (current_buffer->scroll_up_aggressively)
10355 || NUMBERP (current_buffer->scroll_down_aggressively))
10356 && !current_buffer->clip_changed
10357 && CHARPOS (startp) >= BEGV
10358 && CHARPOS (startp) <= ZV)
10359 {
10360 /* The function returns -1 if new fonts were loaded, 1 if
10361 successful, 0 if not successful. */
10362 int rc = try_scrolling (window, just_this_one_p,
10363 scroll_conservatively,
10364 scroll_step,
10365 temp_scroll_step);
10366 switch (rc)
10367 {
10368 case SCROLLING_SUCCESS:
10369 goto done;
10370
10371 case SCROLLING_NEED_LARGER_MATRICES:
10372 goto need_larger_matrices;
10373
10374 case SCROLLING_FAILED:
10375 break;
10376
10377 default:
10378 abort ();
10379 }
10380 }
10381
10382 /* Finally, just choose place to start which centers point */
10383
10384 recenter:
10385
10386 #if GLYPH_DEBUG
10387 debug_method_add (w, "recenter");
10388 #endif
10389
10390 /* w->vscroll = 0; */
10391
10392 /* Forget any previously recorded base line for line number display. */
10393 if (!current_matrix_up_to_date_p
10394 || current_buffer->clip_changed)
10395 w->base_line_number = Qnil;
10396
10397 /* Move backward half the height of the window. */
10398 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10399 it.current_y = it.last_visible_y;
10400 move_it_vertically_backward (&it, window_box_height (w) / 2);
10401 xassert (IT_CHARPOS (it) >= BEGV);
10402
10403 /* The function move_it_vertically_backward may move over more
10404 than the specified y-distance. If it->w is small, e.g. a
10405 mini-buffer window, we may end up in front of the window's
10406 display area. Start displaying at the start of the line
10407 containing PT in this case. */
10408 if (it.current_y <= 0)
10409 {
10410 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10411 move_it_vertically (&it, 0);
10412 xassert (IT_CHARPOS (it) <= PT);
10413 it.current_y = 0;
10414 }
10415
10416 it.current_x = it.hpos = 0;
10417
10418 /* Set startp here explicitly in case that helps avoid an infinite loop
10419 in case the window-scroll-functions functions get errors. */
10420 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10421
10422 /* Run scroll hooks. */
10423 startp = run_window_scroll_functions (window, it.current.pos);
10424
10425 /* Redisplay the window. */
10426 if (!current_matrix_up_to_date_p
10427 || windows_or_buffers_changed
10428 /* Don't use try_window_reusing_current_matrix in this case
10429 because it can have changed the buffer. */
10430 || !NILP (Vwindow_scroll_functions)
10431 || !just_this_one_p
10432 || MINI_WINDOW_P (w)
10433 || !try_window_reusing_current_matrix (w))
10434 try_window (window, startp);
10435
10436 /* If new fonts have been loaded (due to fontsets), give up. We
10437 have to start a new redisplay since we need to re-adjust glyph
10438 matrices. */
10439 if (fonts_changed_p)
10440 goto need_larger_matrices;
10441
10442 /* If cursor did not appear assume that the middle of the window is
10443 in the first line of the window. Do it again with the next line.
10444 (Imagine a window of height 100, displaying two lines of height
10445 60. Moving back 50 from it->last_visible_y will end in the first
10446 line.) */
10447 if (w->cursor.vpos < 0)
10448 {
10449 if (!NILP (w->window_end_valid)
10450 && PT >= Z - XFASTINT (w->window_end_pos))
10451 {
10452 clear_glyph_matrix (w->desired_matrix);
10453 move_it_by_lines (&it, 1, 0);
10454 try_window (window, it.current.pos);
10455 }
10456 else if (PT < IT_CHARPOS (it))
10457 {
10458 clear_glyph_matrix (w->desired_matrix);
10459 move_it_by_lines (&it, -1, 0);
10460 try_window (window, it.current.pos);
10461 }
10462 else
10463 {
10464 /* Not much we can do about it. */
10465 }
10466 }
10467
10468 /* Consider the following case: Window starts at BEGV, there is
10469 invisible, intangible text at BEGV, so that display starts at
10470 some point START > BEGV. It can happen that we are called with
10471 PT somewhere between BEGV and START. Try to handle that case. */
10472 if (w->cursor.vpos < 0)
10473 {
10474 struct glyph_row *row = w->current_matrix->rows;
10475 if (row->mode_line_p)
10476 ++row;
10477 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10478 }
10479
10480 if (!make_cursor_line_fully_visible (w))
10481 goto need_larger_matrices;
10482
10483 done:
10484
10485 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10486 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10487 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10488 ? Qt : Qnil);
10489
10490 /* Display the mode line, if we must. */
10491 if ((update_mode_line
10492 /* If window not full width, must redo its mode line
10493 if (a) the window to its side is being redone and
10494 (b) we do a frame-based redisplay. This is a consequence
10495 of how inverted lines are drawn in frame-based redisplay. */
10496 || (!just_this_one_p
10497 && !FRAME_WINDOW_P (f)
10498 && !WINDOW_FULL_WIDTH_P (w))
10499 /* Line number to display. */
10500 || INTEGERP (w->base_line_pos)
10501 /* Column number is displayed and different from the one displayed. */
10502 || (!NILP (w->column_number_displayed)
10503 && XFASTINT (w->column_number_displayed) != current_column ()))
10504 /* This means that the window has a mode line. */
10505 && (WINDOW_WANTS_MODELINE_P (w)
10506 || WINDOW_WANTS_HEADER_LINE_P (w)))
10507 {
10508 display_mode_lines (w);
10509
10510 /* If mode line height has changed, arrange for a thorough
10511 immediate redisplay using the correct mode line height. */
10512 if (WINDOW_WANTS_MODELINE_P (w)
10513 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10514 {
10515 fonts_changed_p = 1;
10516 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10517 = DESIRED_MODE_LINE_HEIGHT (w);
10518 }
10519
10520 /* If top line height has changed, arrange for a thorough
10521 immediate redisplay using the correct mode line height. */
10522 if (WINDOW_WANTS_HEADER_LINE_P (w)
10523 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10524 {
10525 fonts_changed_p = 1;
10526 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10527 = DESIRED_HEADER_LINE_HEIGHT (w);
10528 }
10529
10530 if (fonts_changed_p)
10531 goto need_larger_matrices;
10532 }
10533
10534 if (!line_number_displayed
10535 && !BUFFERP (w->base_line_pos))
10536 {
10537 w->base_line_pos = Qnil;
10538 w->base_line_number = Qnil;
10539 }
10540
10541 finish_menu_bars:
10542
10543 /* When we reach a frame's selected window, redo the frame's menu bar. */
10544 if (update_mode_line
10545 && EQ (FRAME_SELECTED_WINDOW (f), window))
10546 {
10547 int redisplay_menu_p = 0;
10548
10549 if (FRAME_WINDOW_P (f))
10550 {
10551 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10552 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10553 #else
10554 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10555 #endif
10556 }
10557 else
10558 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10559
10560 if (redisplay_menu_p)
10561 display_menu_bar (w);
10562
10563 #ifdef HAVE_WINDOW_SYSTEM
10564 if (WINDOWP (f->tool_bar_window)
10565 && (FRAME_TOOL_BAR_LINES (f) > 0
10566 || auto_resize_tool_bars_p))
10567 redisplay_tool_bar (f);
10568 #endif
10569 }
10570
10571 need_larger_matrices:
10572 ;
10573 finish_scroll_bars:
10574
10575 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10576 {
10577 int start, end, whole;
10578
10579 /* Calculate the start and end positions for the current window.
10580 At some point, it would be nice to choose between scrollbars
10581 which reflect the whole buffer size, with special markers
10582 indicating narrowing, and scrollbars which reflect only the
10583 visible region.
10584
10585 Note that mini-buffers sometimes aren't displaying any text. */
10586 if (!MINI_WINDOW_P (w)
10587 || (w == XWINDOW (minibuf_window)
10588 && NILP (echo_area_buffer[0])))
10589 {
10590 whole = ZV - BEGV;
10591 start = marker_position (w->start) - BEGV;
10592 /* I don't think this is guaranteed to be right. For the
10593 moment, we'll pretend it is. */
10594 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10595
10596 if (end < start)
10597 end = start;
10598 if (whole < (end - start))
10599 whole = end - start;
10600 }
10601 else
10602 start = end = whole = 0;
10603
10604 /* Indicate what this scroll bar ought to be displaying now. */
10605 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10606
10607 /* Note that we actually used the scroll bar attached to this
10608 window, so it shouldn't be deleted at the end of redisplay. */
10609 redeem_scroll_bar_hook (w);
10610 }
10611
10612 /* Restore current_buffer and value of point in it. */
10613 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10614 set_buffer_internal_1 (old);
10615 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10616
10617 unbind_to (count, Qnil);
10618 }
10619
10620
10621 /* Build the complete desired matrix of WINDOW with a window start
10622 buffer position POS. Value is non-zero if successful. It is zero
10623 if fonts were loaded during redisplay which makes re-adjusting
10624 glyph matrices necessary. */
10625
10626 int
10627 try_window (window, pos)
10628 Lisp_Object window;
10629 struct text_pos pos;
10630 {
10631 struct window *w = XWINDOW (window);
10632 struct it it;
10633 struct glyph_row *last_text_row = NULL;
10634
10635 /* Make POS the new window start. */
10636 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10637
10638 /* Mark cursor position as unknown. No overlay arrow seen. */
10639 w->cursor.vpos = -1;
10640 overlay_arrow_seen = 0;
10641
10642 /* Initialize iterator and info to start at POS. */
10643 start_display (&it, w, pos);
10644
10645 /* Display all lines of W. */
10646 while (it.current_y < it.last_visible_y)
10647 {
10648 if (display_line (&it))
10649 last_text_row = it.glyph_row - 1;
10650 if (fonts_changed_p)
10651 return 0;
10652 }
10653
10654 /* If bottom moved off end of frame, change mode line percentage. */
10655 if (XFASTINT (w->window_end_pos) <= 0
10656 && Z != IT_CHARPOS (it))
10657 w->update_mode_line = Qt;
10658
10659 /* Set window_end_pos to the offset of the last character displayed
10660 on the window from the end of current_buffer. Set
10661 window_end_vpos to its row number. */
10662 if (last_text_row)
10663 {
10664 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10665 w->window_end_bytepos
10666 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10667 w->window_end_pos
10668 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10669 w->window_end_vpos
10670 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10671 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10672 ->displays_text_p);
10673 }
10674 else
10675 {
10676 w->window_end_bytepos = 0;
10677 w->window_end_pos = w->window_end_vpos = make_number (0);
10678 }
10679
10680 /* But that is not valid info until redisplay finishes. */
10681 w->window_end_valid = Qnil;
10682 return 1;
10683 }
10684
10685
10686 \f
10687 /************************************************************************
10688 Window redisplay reusing current matrix when buffer has not changed
10689 ************************************************************************/
10690
10691 /* Try redisplay of window W showing an unchanged buffer with a
10692 different window start than the last time it was displayed by
10693 reusing its current matrix. Value is non-zero if successful.
10694 W->start is the new window start. */
10695
10696 static int
10697 try_window_reusing_current_matrix (w)
10698 struct window *w;
10699 {
10700 struct frame *f = XFRAME (w->frame);
10701 struct glyph_row *row, *bottom_row;
10702 struct it it;
10703 struct run run;
10704 struct text_pos start, new_start;
10705 int nrows_scrolled, i;
10706 struct glyph_row *last_text_row;
10707 struct glyph_row *last_reused_text_row;
10708 struct glyph_row *start_row;
10709 int start_vpos, min_y, max_y;
10710
10711 #if GLYPH_DEBUG
10712 if (inhibit_try_window_reusing)
10713 return 0;
10714 #endif
10715
10716 if (/* This function doesn't handle terminal frames. */
10717 !FRAME_WINDOW_P (f)
10718 /* Don't try to reuse the display if windows have been split
10719 or such. */
10720 || windows_or_buffers_changed)
10721 return 0;
10722
10723 /* Can't do this if region may have changed. */
10724 if ((!NILP (Vtransient_mark_mode)
10725 && !NILP (current_buffer->mark_active))
10726 || !NILP (w->region_showing)
10727 || !NILP (Vshow_trailing_whitespace))
10728 return 0;
10729
10730 /* If top-line visibility has changed, give up. */
10731 if (WINDOW_WANTS_HEADER_LINE_P (w)
10732 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10733 return 0;
10734
10735 /* Give up if old or new display is scrolled vertically. We could
10736 make this function handle this, but right now it doesn't. */
10737 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10738 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10739 return 0;
10740
10741 /* The variable new_start now holds the new window start. The old
10742 start `start' can be determined from the current matrix. */
10743 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10744 start = start_row->start.pos;
10745 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10746
10747 /* Clear the desired matrix for the display below. */
10748 clear_glyph_matrix (w->desired_matrix);
10749
10750 if (CHARPOS (new_start) <= CHARPOS (start))
10751 {
10752 int first_row_y;
10753
10754 /* Don't use this method if the display starts with an ellipsis
10755 displayed for invisible text. It's not easy to handle that case
10756 below, and it's certainly not worth the effort since this is
10757 not a frequent case. */
10758 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10759 return 0;
10760
10761 IF_DEBUG (debug_method_add (w, "twu1"));
10762
10763 /* Display up to a row that can be reused. The variable
10764 last_text_row is set to the last row displayed that displays
10765 text. Note that it.vpos == 0 if or if not there is a
10766 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10767 start_display (&it, w, new_start);
10768 first_row_y = it.current_y;
10769 w->cursor.vpos = -1;
10770 last_text_row = last_reused_text_row = NULL;
10771
10772 while (it.current_y < it.last_visible_y
10773 && IT_CHARPOS (it) < CHARPOS (start)
10774 && !fonts_changed_p)
10775 if (display_line (&it))
10776 last_text_row = it.glyph_row - 1;
10777
10778 /* A value of current_y < last_visible_y means that we stopped
10779 at the previous window start, which in turn means that we
10780 have at least one reusable row. */
10781 if (it.current_y < it.last_visible_y)
10782 {
10783 /* IT.vpos always starts from 0; it counts text lines. */
10784 nrows_scrolled = it.vpos;
10785
10786 /* Find PT if not already found in the lines displayed. */
10787 if (w->cursor.vpos < 0)
10788 {
10789 int dy = it.current_y - first_row_y;
10790
10791 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10792 row = row_containing_pos (w, PT, row, NULL, dy);
10793 if (row)
10794 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10795 dy, nrows_scrolled);
10796 else
10797 {
10798 clear_glyph_matrix (w->desired_matrix);
10799 return 0;
10800 }
10801 }
10802
10803 /* Scroll the display. Do it before the current matrix is
10804 changed. The problem here is that update has not yet
10805 run, i.e. part of the current matrix is not up to date.
10806 scroll_run_hook will clear the cursor, and use the
10807 current matrix to get the height of the row the cursor is
10808 in. */
10809 run.current_y = first_row_y;
10810 run.desired_y = it.current_y;
10811 run.height = it.last_visible_y - it.current_y;
10812
10813 if (run.height > 0 && run.current_y != run.desired_y)
10814 {
10815 update_begin (f);
10816 rif->update_window_begin_hook (w);
10817 rif->clear_mouse_face (w);
10818 rif->scroll_run_hook (w, &run);
10819 rif->update_window_end_hook (w, 0, 0);
10820 update_end (f);
10821 }
10822
10823 /* Shift current matrix down by nrows_scrolled lines. */
10824 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10825 rotate_matrix (w->current_matrix,
10826 start_vpos,
10827 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10828 nrows_scrolled);
10829
10830 /* Disable lines that must be updated. */
10831 for (i = 0; i < it.vpos; ++i)
10832 (start_row + i)->enabled_p = 0;
10833
10834 /* Re-compute Y positions. */
10835 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10836 max_y = it.last_visible_y;
10837 for (row = start_row + nrows_scrolled;
10838 row < bottom_row;
10839 ++row)
10840 {
10841 row->y = it.current_y;
10842 row->visible_height = row->height;
10843
10844 if (row->y < min_y)
10845 row->visible_height -= min_y - row->y;
10846 if (row->y + row->height > max_y)
10847 row->visible_height -= row->y + row->height - max_y;
10848
10849 it.current_y += row->height;
10850
10851 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10852 last_reused_text_row = row;
10853 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10854 break;
10855 }
10856
10857 /* Disable lines in the current matrix which are now
10858 below the window. */
10859 for (++row; row < bottom_row; ++row)
10860 row->enabled_p = 0;
10861 }
10862
10863 /* Update window_end_pos etc.; last_reused_text_row is the last
10864 reused row from the current matrix containing text, if any.
10865 The value of last_text_row is the last displayed line
10866 containing text. */
10867 if (last_reused_text_row)
10868 {
10869 w->window_end_bytepos
10870 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10871 w->window_end_pos
10872 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10873 w->window_end_vpos
10874 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10875 w->current_matrix));
10876 }
10877 else if (last_text_row)
10878 {
10879 w->window_end_bytepos
10880 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10881 w->window_end_pos
10882 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10883 w->window_end_vpos
10884 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10885 }
10886 else
10887 {
10888 /* This window must be completely empty. */
10889 w->window_end_bytepos = 0;
10890 w->window_end_pos = w->window_end_vpos = make_number (0);
10891 }
10892 w->window_end_valid = Qnil;
10893
10894 /* Update hint: don't try scrolling again in update_window. */
10895 w->desired_matrix->no_scrolling_p = 1;
10896
10897 #if GLYPH_DEBUG
10898 debug_method_add (w, "try_window_reusing_current_matrix 1");
10899 #endif
10900 return 1;
10901 }
10902 else if (CHARPOS (new_start) > CHARPOS (start))
10903 {
10904 struct glyph_row *pt_row, *row;
10905 struct glyph_row *first_reusable_row;
10906 struct glyph_row *first_row_to_display;
10907 int dy;
10908 int yb = window_text_bottom_y (w);
10909
10910 /* Find the row starting at new_start, if there is one. Don't
10911 reuse a partially visible line at the end. */
10912 first_reusable_row = start_row;
10913 while (first_reusable_row->enabled_p
10914 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10915 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10916 < CHARPOS (new_start)))
10917 ++first_reusable_row;
10918
10919 /* Give up if there is no row to reuse. */
10920 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10921 || !first_reusable_row->enabled_p
10922 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10923 != CHARPOS (new_start)))
10924 return 0;
10925
10926 /* We can reuse fully visible rows beginning with
10927 first_reusable_row to the end of the window. Set
10928 first_row_to_display to the first row that cannot be reused.
10929 Set pt_row to the row containing point, if there is any. */
10930 pt_row = NULL;
10931 for (first_row_to_display = first_reusable_row;
10932 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
10933 ++first_row_to_display)
10934 {
10935 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10936 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10937 pt_row = first_row_to_display;
10938 }
10939
10940 /* Start displaying at the start of first_row_to_display. */
10941 xassert (first_row_to_display->y < yb);
10942 init_to_row_start (&it, w, first_row_to_display);
10943
10944 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10945 - start_vpos);
10946 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10947 - nrows_scrolled);
10948 it.current_y = (first_row_to_display->y - first_reusable_row->y
10949 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10950
10951 /* Display lines beginning with first_row_to_display in the
10952 desired matrix. Set last_text_row to the last row displayed
10953 that displays text. */
10954 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10955 if (pt_row == NULL)
10956 w->cursor.vpos = -1;
10957 last_text_row = NULL;
10958 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10959 if (display_line (&it))
10960 last_text_row = it.glyph_row - 1;
10961
10962 /* Give up If point isn't in a row displayed or reused. */
10963 if (w->cursor.vpos < 0)
10964 {
10965 clear_glyph_matrix (w->desired_matrix);
10966 return 0;
10967 }
10968
10969 /* If point is in a reused row, adjust y and vpos of the cursor
10970 position. */
10971 if (pt_row)
10972 {
10973 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10974 w->current_matrix);
10975 w->cursor.y -= first_reusable_row->y;
10976 }
10977
10978 /* Scroll the display. */
10979 run.current_y = first_reusable_row->y;
10980 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10981 run.height = it.last_visible_y - run.current_y;
10982 dy = run.current_y - run.desired_y;
10983
10984 if (run.height)
10985 {
10986 struct frame *f = XFRAME (WINDOW_FRAME (w));
10987 update_begin (f);
10988 rif->update_window_begin_hook (w);
10989 rif->clear_mouse_face (w);
10990 rif->scroll_run_hook (w, &run);
10991 rif->update_window_end_hook (w, 0, 0);
10992 update_end (f);
10993 }
10994
10995 /* Adjust Y positions of reused rows. */
10996 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10997 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10998 max_y = it.last_visible_y;
10999 for (row = first_reusable_row; row < first_row_to_display; ++row)
11000 {
11001 row->y -= dy;
11002 row->visible_height = row->height;
11003 if (row->y < min_y)
11004 row->visible_height -= min_y - row->y;
11005 if (row->y + row->height > max_y)
11006 row->visible_height -= row->y + row->height - max_y;
11007 }
11008
11009 /* Scroll the current matrix. */
11010 xassert (nrows_scrolled > 0);
11011 rotate_matrix (w->current_matrix,
11012 start_vpos,
11013 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11014 -nrows_scrolled);
11015
11016 /* Disable rows not reused. */
11017 for (row -= nrows_scrolled; row < bottom_row; ++row)
11018 row->enabled_p = 0;
11019
11020 /* Adjust window end. A null value of last_text_row means that
11021 the window end is in reused rows which in turn means that
11022 only its vpos can have changed. */
11023 if (last_text_row)
11024 {
11025 w->window_end_bytepos
11026 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11027 w->window_end_pos
11028 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11029 w->window_end_vpos
11030 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11031 }
11032 else
11033 {
11034 w->window_end_vpos
11035 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
11036 }
11037
11038 w->window_end_valid = Qnil;
11039 w->desired_matrix->no_scrolling_p = 1;
11040
11041 #if GLYPH_DEBUG
11042 debug_method_add (w, "try_window_reusing_current_matrix 2");
11043 #endif
11044 return 1;
11045 }
11046
11047 return 0;
11048 }
11049
11050
11051 \f
11052 /************************************************************************
11053 Window redisplay reusing current matrix when buffer has changed
11054 ************************************************************************/
11055
11056 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11057 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
11058 int *, int *));
11059 static struct glyph_row *
11060 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11061 struct glyph_row *));
11062
11063
11064 /* Return the last row in MATRIX displaying text. If row START is
11065 non-null, start searching with that row. IT gives the dimensions
11066 of the display. Value is null if matrix is empty; otherwise it is
11067 a pointer to the row found. */
11068
11069 static struct glyph_row *
11070 find_last_row_displaying_text (matrix, it, start)
11071 struct glyph_matrix *matrix;
11072 struct it *it;
11073 struct glyph_row *start;
11074 {
11075 struct glyph_row *row, *row_found;
11076
11077 /* Set row_found to the last row in IT->w's current matrix
11078 displaying text. The loop looks funny but think of partially
11079 visible lines. */
11080 row_found = NULL;
11081 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11082 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11083 {
11084 xassert (row->enabled_p);
11085 row_found = row;
11086 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11087 break;
11088 ++row;
11089 }
11090
11091 return row_found;
11092 }
11093
11094
11095 /* Return the last row in the current matrix of W that is not affected
11096 by changes at the start of current_buffer that occurred since W's
11097 current matrix was built. Value is null if no such row exists.
11098
11099 BEG_UNCHANGED us the number of characters unchanged at the start of
11100 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11101 first changed character in current_buffer. Characters at positions <
11102 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11103 when the current matrix was built. */
11104
11105 static struct glyph_row *
11106 find_last_unchanged_at_beg_row (w)
11107 struct window *w;
11108 {
11109 int first_changed_pos = BEG + BEG_UNCHANGED;
11110 struct glyph_row *row;
11111 struct glyph_row *row_found = NULL;
11112 int yb = window_text_bottom_y (w);
11113
11114 /* Find the last row displaying unchanged text. */
11115 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11116 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11117 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11118 {
11119 if (/* If row ends before first_changed_pos, it is unchanged,
11120 except in some case. */
11121 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11122 /* When row ends in ZV and we write at ZV it is not
11123 unchanged. */
11124 && !row->ends_at_zv_p
11125 /* When first_changed_pos is the end of a continued line,
11126 row is not unchanged because it may be no longer
11127 continued. */
11128 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11129 && row->continued_p))
11130 row_found = row;
11131
11132 /* Stop if last visible row. */
11133 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11134 break;
11135
11136 ++row;
11137 }
11138
11139 return row_found;
11140 }
11141
11142
11143 /* Find the first glyph row in the current matrix of W that is not
11144 affected by changes at the end of current_buffer since the
11145 time W's current matrix was built.
11146
11147 Return in *DELTA the number of chars by which buffer positions in
11148 unchanged text at the end of current_buffer must be adjusted.
11149
11150 Return in *DELTA_BYTES the corresponding number of bytes.
11151
11152 Value is null if no such row exists, i.e. all rows are affected by
11153 changes. */
11154
11155 static struct glyph_row *
11156 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11157 struct window *w;
11158 int *delta, *delta_bytes;
11159 {
11160 struct glyph_row *row;
11161 struct glyph_row *row_found = NULL;
11162
11163 *delta = *delta_bytes = 0;
11164
11165 /* Display must not have been paused, otherwise the current matrix
11166 is not up to date. */
11167 if (NILP (w->window_end_valid))
11168 abort ();
11169
11170 /* A value of window_end_pos >= END_UNCHANGED means that the window
11171 end is in the range of changed text. If so, there is no
11172 unchanged row at the end of W's current matrix. */
11173 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11174 return NULL;
11175
11176 /* Set row to the last row in W's current matrix displaying text. */
11177 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11178
11179 /* If matrix is entirely empty, no unchanged row exists. */
11180 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11181 {
11182 /* The value of row is the last glyph row in the matrix having a
11183 meaningful buffer position in it. The end position of row
11184 corresponds to window_end_pos. This allows us to translate
11185 buffer positions in the current matrix to current buffer
11186 positions for characters not in changed text. */
11187 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11188 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11189 int last_unchanged_pos, last_unchanged_pos_old;
11190 struct glyph_row *first_text_row
11191 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11192
11193 *delta = Z - Z_old;
11194 *delta_bytes = Z_BYTE - Z_BYTE_old;
11195
11196 /* Set last_unchanged_pos to the buffer position of the last
11197 character in the buffer that has not been changed. Z is the
11198 index + 1 of the last character in current_buffer, i.e. by
11199 subtracting END_UNCHANGED we get the index of the last
11200 unchanged character, and we have to add BEG to get its buffer
11201 position. */
11202 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11203 last_unchanged_pos_old = last_unchanged_pos - *delta;
11204
11205 /* Search backward from ROW for a row displaying a line that
11206 starts at a minimum position >= last_unchanged_pos_old. */
11207 for (; row > first_text_row; --row)
11208 {
11209 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11210 abort ();
11211
11212 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11213 row_found = row;
11214 }
11215 }
11216
11217 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11218 abort ();
11219
11220 return row_found;
11221 }
11222
11223
11224 /* Make sure that glyph rows in the current matrix of window W
11225 reference the same glyph memory as corresponding rows in the
11226 frame's frame matrix. This function is called after scrolling W's
11227 current matrix on a terminal frame in try_window_id and
11228 try_window_reusing_current_matrix. */
11229
11230 static void
11231 sync_frame_with_window_matrix_rows (w)
11232 struct window *w;
11233 {
11234 struct frame *f = XFRAME (w->frame);
11235 struct glyph_row *window_row, *window_row_end, *frame_row;
11236
11237 /* Preconditions: W must be a leaf window and full-width. Its frame
11238 must have a frame matrix. */
11239 xassert (NILP (w->hchild) && NILP (w->vchild));
11240 xassert (WINDOW_FULL_WIDTH_P (w));
11241 xassert (!FRAME_WINDOW_P (f));
11242
11243 /* If W is a full-width window, glyph pointers in W's current matrix
11244 have, by definition, to be the same as glyph pointers in the
11245 corresponding frame matrix. */
11246 window_row = w->current_matrix->rows;
11247 window_row_end = window_row + w->current_matrix->nrows;
11248 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11249 while (window_row < window_row_end)
11250 {
11251 int area;
11252
11253 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
11254 frame_row->glyphs[area] = window_row->glyphs[area];
11255
11256 /* Disable frame rows whose corresponding window rows have
11257 been disabled in try_window_id. */
11258 if (!window_row->enabled_p)
11259 frame_row->enabled_p = 0;
11260
11261 ++window_row, ++frame_row;
11262 }
11263 }
11264
11265
11266 /* Find the glyph row in window W containing CHARPOS. Consider all
11267 rows between START and END (not inclusive). END null means search
11268 all rows to the end of the display area of W. Value is the row
11269 containing CHARPOS or null. */
11270
11271 struct glyph_row *
11272 row_containing_pos (w, charpos, start, end, dy)
11273 struct window *w;
11274 int charpos;
11275 struct glyph_row *start, *end;
11276 int dy;
11277 {
11278 struct glyph_row *row = start;
11279 int last_y;
11280
11281 /* If we happen to start on a header-line, skip that. */
11282 if (row->mode_line_p)
11283 ++row;
11284
11285 if ((end && row >= end) || !row->enabled_p)
11286 return NULL;
11287
11288 last_y = window_text_bottom_y (w) - dy;
11289
11290 while ((end == NULL || row < end)
11291 && MATRIX_ROW_BOTTOM_Y (row) < last_y
11292 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11293 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11294 /* The end position of a row equals the start
11295 position of the next row. If CHARPOS is there, we
11296 would rather display it in the next line, except
11297 when this line ends in ZV. */
11298 && !row->ends_at_zv_p
11299 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))
11300 ++row;
11301
11302 /* Give up if CHARPOS not found. */
11303 if ((end && row >= end)
11304 || charpos < MATRIX_ROW_START_CHARPOS (row)
11305 || charpos > MATRIX_ROW_END_CHARPOS (row))
11306 row = NULL;
11307
11308 return row;
11309 }
11310
11311
11312 /* Try to redisplay window W by reusing its existing display. W's
11313 current matrix must be up to date when this function is called,
11314 i.e. window_end_valid must not be nil.
11315
11316 Value is
11317
11318 1 if display has been updated
11319 0 if otherwise unsuccessful
11320 -1 if redisplay with same window start is known not to succeed
11321
11322 The following steps are performed:
11323
11324 1. Find the last row in the current matrix of W that is not
11325 affected by changes at the start of current_buffer. If no such row
11326 is found, give up.
11327
11328 2. Find the first row in W's current matrix that is not affected by
11329 changes at the end of current_buffer. Maybe there is no such row.
11330
11331 3. Display lines beginning with the row + 1 found in step 1 to the
11332 row found in step 2 or, if step 2 didn't find a row, to the end of
11333 the window.
11334
11335 4. If cursor is not known to appear on the window, give up.
11336
11337 5. If display stopped at the row found in step 2, scroll the
11338 display and current matrix as needed.
11339
11340 6. Maybe display some lines at the end of W, if we must. This can
11341 happen under various circumstances, like a partially visible line
11342 becoming fully visible, or because newly displayed lines are displayed
11343 in smaller font sizes.
11344
11345 7. Update W's window end information. */
11346
11347 static int
11348 try_window_id (w)
11349 struct window *w;
11350 {
11351 struct frame *f = XFRAME (w->frame);
11352 struct glyph_matrix *current_matrix = w->current_matrix;
11353 struct glyph_matrix *desired_matrix = w->desired_matrix;
11354 struct glyph_row *last_unchanged_at_beg_row;
11355 struct glyph_row *first_unchanged_at_end_row;
11356 struct glyph_row *row;
11357 struct glyph_row *bottom_row;
11358 int bottom_vpos;
11359 struct it it;
11360 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11361 struct text_pos start_pos;
11362 struct run run;
11363 int first_unchanged_at_end_vpos = 0;
11364 struct glyph_row *last_text_row, *last_text_row_at_end;
11365 struct text_pos start;
11366 int first_changed_charpos, last_changed_charpos;
11367
11368 #if GLYPH_DEBUG
11369 if (inhibit_try_window_id)
11370 return 0;
11371 #endif
11372
11373 /* This is handy for debugging. */
11374 #if 0
11375 #define GIVE_UP(X) \
11376 do { \
11377 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11378 return 0; \
11379 } while (0)
11380 #else
11381 #define GIVE_UP(X) return 0
11382 #endif
11383
11384 SET_TEXT_POS_FROM_MARKER (start, w->start);
11385
11386 /* Don't use this for mini-windows because these can show
11387 messages and mini-buffers, and we don't handle that here. */
11388 if (MINI_WINDOW_P (w))
11389 GIVE_UP (1);
11390
11391 /* This flag is used to prevent redisplay optimizations. */
11392 if (windows_or_buffers_changed)
11393 GIVE_UP (2);
11394
11395 /* Verify that narrowing has not changed. This flag is also set to prevent
11396 redisplay optimizations. It would be nice to further
11397 reduce the number of cases where this prevents try_window_id. */
11398 if (current_buffer->clip_changed)
11399 GIVE_UP (3);
11400
11401 /* Window must either use window-based redisplay or be full width. */
11402 if (!FRAME_WINDOW_P (f)
11403 && (!line_ins_del_ok
11404 || !WINDOW_FULL_WIDTH_P (w)))
11405 GIVE_UP (4);
11406
11407 /* Give up if point is not known NOT to appear in W. */
11408 if (PT < CHARPOS (start))
11409 GIVE_UP (5);
11410
11411 /* Another way to prevent redisplay optimizations. */
11412 if (XFASTINT (w->last_modified) == 0)
11413 GIVE_UP (6);
11414
11415 /* Verify that window is not hscrolled. */
11416 if (XFASTINT (w->hscroll) != 0)
11417 GIVE_UP (7);
11418
11419 /* Verify that display wasn't paused. */
11420 if (NILP (w->window_end_valid))
11421 GIVE_UP (8);
11422
11423 /* Can't use this if highlighting a region because a cursor movement
11424 will do more than just set the cursor. */
11425 if (!NILP (Vtransient_mark_mode)
11426 && !NILP (current_buffer->mark_active))
11427 GIVE_UP (9);
11428
11429 /* Likewise if highlighting trailing whitespace. */
11430 if (!NILP (Vshow_trailing_whitespace))
11431 GIVE_UP (11);
11432
11433 /* Likewise if showing a region. */
11434 if (!NILP (w->region_showing))
11435 GIVE_UP (10);
11436
11437 /* Can use this if overlay arrow position and or string have changed. */
11438 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11439 || !EQ (last_arrow_string, Voverlay_arrow_string))
11440 GIVE_UP (12);
11441
11442
11443 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11444 only if buffer has really changed. The reason is that the gap is
11445 initially at Z for freshly visited files. The code below would
11446 set end_unchanged to 0 in that case. */
11447 if (MODIFF > SAVE_MODIFF
11448 /* This seems to happen sometimes after saving a buffer. */
11449 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11450 {
11451 if (GPT - BEG < BEG_UNCHANGED)
11452 BEG_UNCHANGED = GPT - BEG;
11453 if (Z - GPT < END_UNCHANGED)
11454 END_UNCHANGED = Z - GPT;
11455 }
11456
11457 /* The position of the first and last character that has been changed. */
11458 first_changed_charpos = BEG + BEG_UNCHANGED;
11459 last_changed_charpos = Z - END_UNCHANGED;
11460
11461 /* If window starts after a line end, and the last change is in
11462 front of that newline, then changes don't affect the display.
11463 This case happens with stealth-fontification. Note that although
11464 the display is unchanged, glyph positions in the matrix have to
11465 be adjusted, of course. */
11466 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11467 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11468 && ((last_changed_charpos < CHARPOS (start)
11469 && CHARPOS (start) == BEGV)
11470 || (last_changed_charpos < CHARPOS (start) - 1
11471 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11472 {
11473 int Z_old, delta, Z_BYTE_old, delta_bytes;
11474 struct glyph_row *r0;
11475
11476 /* Compute how many chars/bytes have been added to or removed
11477 from the buffer. */
11478 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11479 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11480 delta = Z - Z_old;
11481 delta_bytes = Z_BYTE - Z_BYTE_old;
11482
11483 /* Give up if PT is not in the window. Note that it already has
11484 been checked at the start of try_window_id that PT is not in
11485 front of the window start. */
11486 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11487 GIVE_UP (13);
11488
11489 /* If window start is unchanged, we can reuse the whole matrix
11490 as is, after adjusting glyph positions. No need to compute
11491 the window end again, since its offset from Z hasn't changed. */
11492 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11493 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11494 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11495 {
11496 /* Adjust positions in the glyph matrix. */
11497 if (delta || delta_bytes)
11498 {
11499 struct glyph_row *r1
11500 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11501 increment_matrix_positions (w->current_matrix,
11502 MATRIX_ROW_VPOS (r0, current_matrix),
11503 MATRIX_ROW_VPOS (r1, current_matrix),
11504 delta, delta_bytes);
11505 }
11506
11507 /* Set the cursor. */
11508 row = row_containing_pos (w, PT, r0, NULL, 0);
11509 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11510 return 1;
11511 }
11512 }
11513
11514 /* Handle the case that changes are all below what is displayed in
11515 the window, and that PT is in the window. This shortcut cannot
11516 be taken if ZV is visible in the window, and text has been added
11517 there that is visible in the window. */
11518 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11519 /* ZV is not visible in the window, or there are no
11520 changes at ZV, actually. */
11521 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11522 || first_changed_charpos == last_changed_charpos))
11523 {
11524 struct glyph_row *r0;
11525
11526 /* Give up if PT is not in the window. Note that it already has
11527 been checked at the start of try_window_id that PT is not in
11528 front of the window start. */
11529 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11530 GIVE_UP (14);
11531
11532 /* If window start is unchanged, we can reuse the whole matrix
11533 as is, without changing glyph positions since no text has
11534 been added/removed in front of the window end. */
11535 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11536 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11537 {
11538 /* We have to compute the window end anew since text
11539 can have been added/removed after it. */
11540 w->window_end_pos
11541 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11542 w->window_end_bytepos
11543 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11544
11545 /* Set the cursor. */
11546 row = row_containing_pos (w, PT, r0, NULL, 0);
11547 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11548 return 2;
11549 }
11550 }
11551
11552 /* Give up if window start is in the changed area.
11553
11554 The condition used to read
11555
11556 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11557
11558 but why that was tested escapes me at the moment. */
11559 if (CHARPOS (start) >= first_changed_charpos
11560 && CHARPOS (start) <= last_changed_charpos)
11561 GIVE_UP (15);
11562
11563 /* Check that window start agrees with the start of the first glyph
11564 row in its current matrix. Check this after we know the window
11565 start is not in changed text, otherwise positions would not be
11566 comparable. */
11567 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11568 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11569 GIVE_UP (16);
11570
11571 /* Give up if the window ends in strings. Overlay strings
11572 at the end are difficult to handle, so don't try. */
11573 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11574 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11575 GIVE_UP (20);
11576
11577 /* Compute the position at which we have to start displaying new
11578 lines. Some of the lines at the top of the window might be
11579 reusable because they are not displaying changed text. Find the
11580 last row in W's current matrix not affected by changes at the
11581 start of current_buffer. Value is null if changes start in the
11582 first line of window. */
11583 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11584 if (last_unchanged_at_beg_row)
11585 {
11586 /* Avoid starting to display in the moddle of a character, a TAB
11587 for instance. This is easier than to set up the iterator
11588 exactly, and it's not a frequent case, so the additional
11589 effort wouldn't really pay off. */
11590 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11591 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
11592 && last_unchanged_at_beg_row > w->current_matrix->rows)
11593 --last_unchanged_at_beg_row;
11594
11595 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11596 GIVE_UP (17);
11597
11598 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11599 GIVE_UP (18);
11600 start_pos = it.current.pos;
11601
11602 /* Start displaying new lines in the desired matrix at the same
11603 vpos we would use in the current matrix, i.e. below
11604 last_unchanged_at_beg_row. */
11605 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11606 current_matrix);
11607 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11608 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11609
11610 xassert (it.hpos == 0 && it.current_x == 0);
11611 }
11612 else
11613 {
11614 /* There are no reusable lines at the start of the window.
11615 Start displaying in the first line. */
11616 start_display (&it, w, start);
11617 start_pos = it.current.pos;
11618 }
11619
11620 /* Find the first row that is not affected by changes at the end of
11621 the buffer. Value will be null if there is no unchanged row, in
11622 which case we must redisplay to the end of the window. delta
11623 will be set to the value by which buffer positions beginning with
11624 first_unchanged_at_end_row have to be adjusted due to text
11625 changes. */
11626 first_unchanged_at_end_row
11627 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11628 IF_DEBUG (debug_delta = delta);
11629 IF_DEBUG (debug_delta_bytes = delta_bytes);
11630
11631 /* Set stop_pos to the buffer position up to which we will have to
11632 display new lines. If first_unchanged_at_end_row != NULL, this
11633 is the buffer position of the start of the line displayed in that
11634 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11635 that we don't stop at a buffer position. */
11636 stop_pos = 0;
11637 if (first_unchanged_at_end_row)
11638 {
11639 xassert (last_unchanged_at_beg_row == NULL
11640 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11641
11642 /* If this is a continuation line, move forward to the next one
11643 that isn't. Changes in lines above affect this line.
11644 Caution: this may move first_unchanged_at_end_row to a row
11645 not displaying text. */
11646 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11647 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11648 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11649 < it.last_visible_y))
11650 ++first_unchanged_at_end_row;
11651
11652 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11653 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11654 >= it.last_visible_y))
11655 first_unchanged_at_end_row = NULL;
11656 else
11657 {
11658 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11659 + delta);
11660 first_unchanged_at_end_vpos
11661 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11662 xassert (stop_pos >= Z - END_UNCHANGED);
11663 }
11664 }
11665 else if (last_unchanged_at_beg_row == NULL)
11666 GIVE_UP (19);
11667
11668
11669 #if GLYPH_DEBUG
11670
11671 /* Either there is no unchanged row at the end, or the one we have
11672 now displays text. This is a necessary condition for the window
11673 end pos calculation at the end of this function. */
11674 xassert (first_unchanged_at_end_row == NULL
11675 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11676
11677 debug_last_unchanged_at_beg_vpos
11678 = (last_unchanged_at_beg_row
11679 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11680 : -1);
11681 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11682
11683 #endif /* GLYPH_DEBUG != 0 */
11684
11685
11686 /* Display new lines. Set last_text_row to the last new line
11687 displayed which has text on it, i.e. might end up as being the
11688 line where the window_end_vpos is. */
11689 w->cursor.vpos = -1;
11690 last_text_row = NULL;
11691 overlay_arrow_seen = 0;
11692 while (it.current_y < it.last_visible_y
11693 && !fonts_changed_p
11694 && (first_unchanged_at_end_row == NULL
11695 || IT_CHARPOS (it) < stop_pos))
11696 {
11697 if (display_line (&it))
11698 last_text_row = it.glyph_row - 1;
11699 }
11700
11701 if (fonts_changed_p)
11702 return -1;
11703
11704
11705 /* Compute differences in buffer positions, y-positions etc. for
11706 lines reused at the bottom of the window. Compute what we can
11707 scroll. */
11708 if (first_unchanged_at_end_row
11709 /* No lines reused because we displayed everything up to the
11710 bottom of the window. */
11711 && it.current_y < it.last_visible_y)
11712 {
11713 dvpos = (it.vpos
11714 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11715 current_matrix));
11716 dy = it.current_y - first_unchanged_at_end_row->y;
11717 run.current_y = first_unchanged_at_end_row->y;
11718 run.desired_y = run.current_y + dy;
11719 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11720 }
11721 else
11722 {
11723 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11724 first_unchanged_at_end_row = NULL;
11725 }
11726 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11727
11728
11729 /* Find the cursor if not already found. We have to decide whether
11730 PT will appear on this window (it sometimes doesn't, but this is
11731 not a very frequent case.) This decision has to be made before
11732 the current matrix is altered. A value of cursor.vpos < 0 means
11733 that PT is either in one of the lines beginning at
11734 first_unchanged_at_end_row or below the window. Don't care for
11735 lines that might be displayed later at the window end; as
11736 mentioned, this is not a frequent case. */
11737 if (w->cursor.vpos < 0)
11738 {
11739 /* Cursor in unchanged rows at the top? */
11740 if (PT < CHARPOS (start_pos)
11741 && last_unchanged_at_beg_row)
11742 {
11743 row = row_containing_pos (w, PT,
11744 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11745 last_unchanged_at_beg_row + 1, 0);
11746 if (row)
11747 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11748 }
11749
11750 /* Start from first_unchanged_at_end_row looking for PT. */
11751 else if (first_unchanged_at_end_row)
11752 {
11753 row = row_containing_pos (w, PT - delta,
11754 first_unchanged_at_end_row, NULL, 0);
11755 if (row)
11756 set_cursor_from_row (w, row, w->current_matrix, delta,
11757 delta_bytes, dy, dvpos);
11758 }
11759
11760 /* Give up if cursor was not found. */
11761 if (w->cursor.vpos < 0)
11762 {
11763 clear_glyph_matrix (w->desired_matrix);
11764 return -1;
11765 }
11766 }
11767
11768 /* Don't let the cursor end in the scroll margins. */
11769 {
11770 int this_scroll_margin, cursor_height;
11771
11772 this_scroll_margin = max (0, scroll_margin);
11773 this_scroll_margin = min (this_scroll_margin,
11774 XFASTINT (w->height) / 4);
11775 this_scroll_margin *= CANON_Y_UNIT (it.f);
11776 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11777
11778 if ((w->cursor.y < this_scroll_margin
11779 && CHARPOS (start) > BEGV)
11780 /* Don't take scroll margin into account at the bottom because
11781 old redisplay didn't do it either. */
11782 || w->cursor.y + cursor_height > it.last_visible_y)
11783 {
11784 w->cursor.vpos = -1;
11785 clear_glyph_matrix (w->desired_matrix);
11786 return -1;
11787 }
11788 }
11789
11790 /* Scroll the display. Do it before changing the current matrix so
11791 that xterm.c doesn't get confused about where the cursor glyph is
11792 found. */
11793 if (dy && run.height)
11794 {
11795 update_begin (f);
11796
11797 if (FRAME_WINDOW_P (f))
11798 {
11799 rif->update_window_begin_hook (w);
11800 rif->clear_mouse_face (w);
11801 rif->scroll_run_hook (w, &run);
11802 rif->update_window_end_hook (w, 0, 0);
11803 }
11804 else
11805 {
11806 /* Terminal frame. In this case, dvpos gives the number of
11807 lines to scroll by; dvpos < 0 means scroll up. */
11808 int first_unchanged_at_end_vpos
11809 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11810 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11811 int end = (XFASTINT (w->top)
11812 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11813 + window_internal_height (w));
11814
11815 /* Perform the operation on the screen. */
11816 if (dvpos > 0)
11817 {
11818 /* Scroll last_unchanged_at_beg_row to the end of the
11819 window down dvpos lines. */
11820 set_terminal_window (end);
11821
11822 /* On dumb terminals delete dvpos lines at the end
11823 before inserting dvpos empty lines. */
11824 if (!scroll_region_ok)
11825 ins_del_lines (end - dvpos, -dvpos);
11826
11827 /* Insert dvpos empty lines in front of
11828 last_unchanged_at_beg_row. */
11829 ins_del_lines (from, dvpos);
11830 }
11831 else if (dvpos < 0)
11832 {
11833 /* Scroll up last_unchanged_at_beg_vpos to the end of
11834 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11835 set_terminal_window (end);
11836
11837 /* Delete dvpos lines in front of
11838 last_unchanged_at_beg_vpos. ins_del_lines will set
11839 the cursor to the given vpos and emit |dvpos| delete
11840 line sequences. */
11841 ins_del_lines (from + dvpos, dvpos);
11842
11843 /* On a dumb terminal insert dvpos empty lines at the
11844 end. */
11845 if (!scroll_region_ok)
11846 ins_del_lines (end + dvpos, -dvpos);
11847 }
11848
11849 set_terminal_window (0);
11850 }
11851
11852 update_end (f);
11853 }
11854
11855 /* Shift reused rows of the current matrix to the right position.
11856 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11857 text. */
11858 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11859 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11860 if (dvpos < 0)
11861 {
11862 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11863 bottom_vpos, dvpos);
11864 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11865 bottom_vpos, 0);
11866 }
11867 else if (dvpos > 0)
11868 {
11869 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11870 bottom_vpos, dvpos);
11871 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11872 first_unchanged_at_end_vpos + dvpos, 0);
11873 }
11874
11875 /* For frame-based redisplay, make sure that current frame and window
11876 matrix are in sync with respect to glyph memory. */
11877 if (!FRAME_WINDOW_P (f))
11878 sync_frame_with_window_matrix_rows (w);
11879
11880 /* Adjust buffer positions in reused rows. */
11881 if (delta)
11882 increment_matrix_positions (current_matrix,
11883 first_unchanged_at_end_vpos + dvpos,
11884 bottom_vpos, delta, delta_bytes);
11885
11886 /* Adjust Y positions. */
11887 if (dy)
11888 shift_glyph_matrix (w, current_matrix,
11889 first_unchanged_at_end_vpos + dvpos,
11890 bottom_vpos, dy);
11891
11892 if (first_unchanged_at_end_row)
11893 first_unchanged_at_end_row += dvpos;
11894
11895 /* If scrolling up, there may be some lines to display at the end of
11896 the window. */
11897 last_text_row_at_end = NULL;
11898 if (dy < 0)
11899 {
11900 /* Scrolling up can leave for example a partially visible line
11901 at the end of the window to be redisplayed. */
11902 /* Set last_row to the glyph row in the current matrix where the
11903 window end line is found. It has been moved up or down in
11904 the matrix by dvpos. */
11905 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11906 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11907
11908 /* If last_row is the window end line, it should display text. */
11909 xassert (last_row->displays_text_p);
11910
11911 /* If window end line was partially visible before, begin
11912 displaying at that line. Otherwise begin displaying with the
11913 line following it. */
11914 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11915 {
11916 init_to_row_start (&it, w, last_row);
11917 it.vpos = last_vpos;
11918 it.current_y = last_row->y;
11919 }
11920 else
11921 {
11922 init_to_row_end (&it, w, last_row);
11923 it.vpos = 1 + last_vpos;
11924 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11925 ++last_row;
11926 }
11927
11928 /* We may start in a continuation line. If so, we have to
11929 get the right continuation_lines_width and current_x. */
11930 it.continuation_lines_width = last_row->continuation_lines_width;
11931 it.hpos = it.current_x = 0;
11932
11933 /* Display the rest of the lines at the window end. */
11934 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11935 while (it.current_y < it.last_visible_y
11936 && !fonts_changed_p)
11937 {
11938 /* Is it always sure that the display agrees with lines in
11939 the current matrix? I don't think so, so we mark rows
11940 displayed invalid in the current matrix by setting their
11941 enabled_p flag to zero. */
11942 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11943 if (display_line (&it))
11944 last_text_row_at_end = it.glyph_row - 1;
11945 }
11946 }
11947
11948 /* Update window_end_pos and window_end_vpos. */
11949 if (first_unchanged_at_end_row
11950 && first_unchanged_at_end_row->y < it.last_visible_y
11951 && !last_text_row_at_end)
11952 {
11953 /* Window end line if one of the preserved rows from the current
11954 matrix. Set row to the last row displaying text in current
11955 matrix starting at first_unchanged_at_end_row, after
11956 scrolling. */
11957 xassert (first_unchanged_at_end_row->displays_text_p);
11958 row = find_last_row_displaying_text (w->current_matrix, &it,
11959 first_unchanged_at_end_row);
11960 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11961
11962 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11963 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11964 w->window_end_vpos
11965 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
11966 xassert (w->window_end_bytepos >= 0);
11967 IF_DEBUG (debug_method_add (w, "A"));
11968 }
11969 else if (last_text_row_at_end)
11970 {
11971 w->window_end_pos
11972 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11973 w->window_end_bytepos
11974 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11975 w->window_end_vpos
11976 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11977 xassert (w->window_end_bytepos >= 0);
11978 IF_DEBUG (debug_method_add (w, "B"));
11979 }
11980 else if (last_text_row)
11981 {
11982 /* We have displayed either to the end of the window or at the
11983 end of the window, i.e. the last row with text is to be found
11984 in the desired matrix. */
11985 w->window_end_pos
11986 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11987 w->window_end_bytepos
11988 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11989 w->window_end_vpos
11990 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11991 xassert (w->window_end_bytepos >= 0);
11992 }
11993 else if (first_unchanged_at_end_row == NULL
11994 && last_text_row == NULL
11995 && last_text_row_at_end == NULL)
11996 {
11997 /* Displayed to end of window, but no line containing text was
11998 displayed. Lines were deleted at the end of the window. */
11999 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12000 int vpos = XFASTINT (w->window_end_vpos);
12001 struct glyph_row *current_row = current_matrix->rows + vpos;
12002 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12003
12004 for (row = NULL;
12005 row == NULL && vpos >= first_vpos;
12006 --vpos, --current_row, --desired_row)
12007 {
12008 if (desired_row->enabled_p)
12009 {
12010 if (desired_row->displays_text_p)
12011 row = desired_row;
12012 }
12013 else if (current_row->displays_text_p)
12014 row = current_row;
12015 }
12016
12017 xassert (row != NULL);
12018 w->window_end_vpos = make_number (vpos + 1);
12019 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12020 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12021 xassert (w->window_end_bytepos >= 0);
12022 IF_DEBUG (debug_method_add (w, "C"));
12023 }
12024 else
12025 abort ();
12026
12027 #if 0 /* This leads to problems, for instance when the cursor is
12028 at ZV, and the cursor line displays no text. */
12029 /* Disable rows below what's displayed in the window. This makes
12030 debugging easier. */
12031 enable_glyph_matrix_rows (current_matrix,
12032 XFASTINT (w->window_end_vpos) + 1,
12033 bottom_vpos, 0);
12034 #endif
12035
12036 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12037 debug_end_vpos = XFASTINT (w->window_end_vpos));
12038
12039 /* Record that display has not been completed. */
12040 w->window_end_valid = Qnil;
12041 w->desired_matrix->no_scrolling_p = 1;
12042 return 3;
12043
12044 #undef GIVE_UP
12045 }
12046
12047
12048 \f
12049 /***********************************************************************
12050 More debugging support
12051 ***********************************************************************/
12052
12053 #if GLYPH_DEBUG
12054
12055 void dump_glyph_row P_ ((struct glyph_row *, int, int));
12056 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
12057 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
12058
12059
12060 /* Dump the contents of glyph matrix MATRIX on stderr.
12061
12062 GLYPHS 0 means don't show glyph contents.
12063 GLYPHS 1 means show glyphs in short form
12064 GLYPHS > 1 means show glyphs in long form. */
12065
12066 void
12067 dump_glyph_matrix (matrix, glyphs)
12068 struct glyph_matrix *matrix;
12069 int glyphs;
12070 {
12071 int i;
12072 for (i = 0; i < matrix->nrows; ++i)
12073 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
12074 }
12075
12076
12077 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12078 the glyph row and area where the glyph comes from. */
12079
12080 void
12081 dump_glyph (row, glyph, area)
12082 struct glyph_row *row;
12083 struct glyph *glyph;
12084 int area;
12085 {
12086 if (glyph->type == CHAR_GLYPH)
12087 {
12088 fprintf (stderr,
12089 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12090 glyph - row->glyphs[TEXT_AREA],
12091 'C',
12092 glyph->charpos,
12093 (BUFFERP (glyph->object)
12094 ? 'B'
12095 : (STRINGP (glyph->object)
12096 ? 'S'
12097 : '-')),
12098 glyph->pixel_width,
12099 glyph->u.ch,
12100 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12101 ? glyph->u.ch
12102 : '.'),
12103 glyph->face_id,
12104 glyph->left_box_line_p,
12105 glyph->right_box_line_p);
12106 }
12107 else if (glyph->type == STRETCH_GLYPH)
12108 {
12109 fprintf (stderr,
12110 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12111 glyph - row->glyphs[TEXT_AREA],
12112 'S',
12113 glyph->charpos,
12114 (BUFFERP (glyph->object)
12115 ? 'B'
12116 : (STRINGP (glyph->object)
12117 ? 'S'
12118 : '-')),
12119 glyph->pixel_width,
12120 0,
12121 '.',
12122 glyph->face_id,
12123 glyph->left_box_line_p,
12124 glyph->right_box_line_p);
12125 }
12126 else if (glyph->type == IMAGE_GLYPH)
12127 {
12128 fprintf (stderr,
12129 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12130 glyph - row->glyphs[TEXT_AREA],
12131 'I',
12132 glyph->charpos,
12133 (BUFFERP (glyph->object)
12134 ? 'B'
12135 : (STRINGP (glyph->object)
12136 ? 'S'
12137 : '-')),
12138 glyph->pixel_width,
12139 glyph->u.img_id,
12140 '.',
12141 glyph->face_id,
12142 glyph->left_box_line_p,
12143 glyph->right_box_line_p);
12144 }
12145 }
12146
12147
12148 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12149 GLYPHS 0 means don't show glyph contents.
12150 GLYPHS 1 means show glyphs in short form
12151 GLYPHS > 1 means show glyphs in long form. */
12152
12153 void
12154 dump_glyph_row (row, vpos, glyphs)
12155 struct glyph_row *row;
12156 int vpos, glyphs;
12157 {
12158 if (glyphs != 1)
12159 {
12160 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12161 fprintf (stderr, "=======================================================================\n");
12162
12163 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
12164 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12165 vpos,
12166 MATRIX_ROW_START_CHARPOS (row),
12167 MATRIX_ROW_END_CHARPOS (row),
12168 row->used[TEXT_AREA],
12169 row->contains_overlapping_glyphs_p,
12170 row->enabled_p,
12171 row->truncated_on_left_p,
12172 row->truncated_on_right_p,
12173 row->overlay_arrow_p,
12174 row->continued_p,
12175 MATRIX_ROW_CONTINUATION_LINE_P (row),
12176 row->displays_text_p,
12177 row->ends_at_zv_p,
12178 row->fill_line_p,
12179 row->ends_in_middle_of_char_p,
12180 row->starts_in_middle_of_char_p,
12181 row->mouse_face_p,
12182 row->x,
12183 row->y,
12184 row->pixel_width,
12185 row->height,
12186 row->visible_height,
12187 row->ascent,
12188 row->phys_ascent);
12189 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12190 row->end.overlay_string_index,
12191 row->continuation_lines_width);
12192 fprintf (stderr, "%9d %5d\n",
12193 CHARPOS (row->start.string_pos),
12194 CHARPOS (row->end.string_pos));
12195 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12196 row->end.dpvec_index);
12197 }
12198
12199 if (glyphs > 1)
12200 {
12201 int area;
12202
12203 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12204 {
12205 struct glyph *glyph = row->glyphs[area];
12206 struct glyph *glyph_end = glyph + row->used[area];
12207
12208 /* Glyph for a line end in text. */
12209 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12210 ++glyph_end;
12211
12212 if (glyph < glyph_end)
12213 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12214
12215 for (; glyph < glyph_end; ++glyph)
12216 dump_glyph (row, glyph, area);
12217 }
12218 }
12219 else if (glyphs == 1)
12220 {
12221 int area;
12222
12223 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12224 {
12225 char *s = (char *) alloca (row->used[area] + 1);
12226 int i;
12227
12228 for (i = 0; i < row->used[area]; ++i)
12229 {
12230 struct glyph *glyph = row->glyphs[area] + i;
12231 if (glyph->type == CHAR_GLYPH
12232 && glyph->u.ch < 0x80
12233 && glyph->u.ch >= ' ')
12234 s[i] = glyph->u.ch;
12235 else
12236 s[i] = '.';
12237 }
12238
12239 s[i] = '\0';
12240 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12241 }
12242 }
12243 }
12244
12245
12246 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12247 Sdump_glyph_matrix, 0, 1, "p",
12248 doc: /* Dump the current matrix of the selected window to stderr.
12249 Shows contents of glyph row structures. With non-nil
12250 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12251 glyphs in short form, otherwise show glyphs in long form. */)
12252 (glyphs)
12253 Lisp_Object glyphs;
12254 {
12255 struct window *w = XWINDOW (selected_window);
12256 struct buffer *buffer = XBUFFER (w->buffer);
12257
12258 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12259 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12260 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12261 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12262 fprintf (stderr, "=============================================\n");
12263 dump_glyph_matrix (w->current_matrix,
12264 NILP (glyphs) ? 0 : XINT (glyphs));
12265 return Qnil;
12266 }
12267
12268
12269 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12270 doc: /* Dump glyph row ROW to stderr.
12271 GLYPH 0 means don't dump glyphs.
12272 GLYPH 1 means dump glyphs in short form.
12273 GLYPH > 1 or omitted means dump glyphs in long form. */)
12274 (row, glyphs)
12275 Lisp_Object row, glyphs;
12276 {
12277 struct glyph_matrix *matrix;
12278 int vpos;
12279
12280 CHECK_NUMBER (row);
12281 matrix = XWINDOW (selected_window)->current_matrix;
12282 vpos = XINT (row);
12283 if (vpos >= 0 && vpos < matrix->nrows)
12284 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12285 vpos,
12286 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12287 return Qnil;
12288 }
12289
12290
12291 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12292 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12293 GLYPH 0 means don't dump glyphs.
12294 GLYPH 1 means dump glyphs in short form.
12295 GLYPH > 1 or omitted means dump glyphs in long form. */)
12296 (row, glyphs)
12297 Lisp_Object row, glyphs;
12298 {
12299 struct frame *sf = SELECTED_FRAME ();
12300 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12301 int vpos;
12302
12303 CHECK_NUMBER (row);
12304 vpos = XINT (row);
12305 if (vpos >= 0 && vpos < m->nrows)
12306 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12307 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12308 return Qnil;
12309 }
12310
12311
12312 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12313 doc: /* Toggle tracing of redisplay.
12314 With ARG, turn tracing on if and only if ARG is positive. */)
12315 (arg)
12316 Lisp_Object arg;
12317 {
12318 if (NILP (arg))
12319 trace_redisplay_p = !trace_redisplay_p;
12320 else
12321 {
12322 arg = Fprefix_numeric_value (arg);
12323 trace_redisplay_p = XINT (arg) > 0;
12324 }
12325
12326 return Qnil;
12327 }
12328
12329
12330 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12331 doc: /* Like `format', but print result to stderr. */)
12332 (nargs, args)
12333 int nargs;
12334 Lisp_Object *args;
12335 {
12336 Lisp_Object s = Fformat (nargs, args);
12337 fprintf (stderr, "%s", XSTRING (s)->data);
12338 return Qnil;
12339 }
12340
12341 #endif /* GLYPH_DEBUG */
12342
12343
12344 \f
12345 /***********************************************************************
12346 Building Desired Matrix Rows
12347 ***********************************************************************/
12348
12349 /* Return a temporary glyph row holding the glyphs of an overlay
12350 arrow. Only used for non-window-redisplay windows. */
12351
12352 static struct glyph_row *
12353 get_overlay_arrow_glyph_row (w)
12354 struct window *w;
12355 {
12356 struct frame *f = XFRAME (WINDOW_FRAME (w));
12357 struct buffer *buffer = XBUFFER (w->buffer);
12358 struct buffer *old = current_buffer;
12359 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
12360 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
12361 unsigned char *arrow_end = arrow_string + arrow_len;
12362 unsigned char *p;
12363 struct it it;
12364 int multibyte_p;
12365 int n_glyphs_before;
12366
12367 set_buffer_temp (buffer);
12368 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12369 it.glyph_row->used[TEXT_AREA] = 0;
12370 SET_TEXT_POS (it.position, 0, 0);
12371
12372 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12373 p = arrow_string;
12374 while (p < arrow_end)
12375 {
12376 Lisp_Object face, ilisp;
12377
12378 /* Get the next character. */
12379 if (multibyte_p)
12380 it.c = string_char_and_length (p, arrow_len, &it.len);
12381 else
12382 it.c = *p, it.len = 1;
12383 p += it.len;
12384
12385 /* Get its face. */
12386 ilisp = make_number (p - arrow_string);
12387 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12388 it.face_id = compute_char_face (f, it.c, face);
12389
12390 /* Compute its width, get its glyphs. */
12391 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12392 SET_TEXT_POS (it.position, -1, -1);
12393 PRODUCE_GLYPHS (&it);
12394
12395 /* If this character doesn't fit any more in the line, we have
12396 to remove some glyphs. */
12397 if (it.current_x > it.last_visible_x)
12398 {
12399 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12400 break;
12401 }
12402 }
12403
12404 set_buffer_temp (old);
12405 return it.glyph_row;
12406 }
12407
12408
12409 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12410 glyphs are only inserted for terminal frames since we can't really
12411 win with truncation glyphs when partially visible glyphs are
12412 involved. Which glyphs to insert is determined by
12413 produce_special_glyphs. */
12414
12415 static void
12416 insert_left_trunc_glyphs (it)
12417 struct it *it;
12418 {
12419 struct it truncate_it;
12420 struct glyph *from, *end, *to, *toend;
12421
12422 xassert (!FRAME_WINDOW_P (it->f));
12423
12424 /* Get the truncation glyphs. */
12425 truncate_it = *it;
12426 truncate_it.current_x = 0;
12427 truncate_it.face_id = DEFAULT_FACE_ID;
12428 truncate_it.glyph_row = &scratch_glyph_row;
12429 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12430 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12431 truncate_it.object = make_number (0);
12432 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12433
12434 /* Overwrite glyphs from IT with truncation glyphs. */
12435 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12436 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12437 to = it->glyph_row->glyphs[TEXT_AREA];
12438 toend = to + it->glyph_row->used[TEXT_AREA];
12439
12440 while (from < end)
12441 *to++ = *from++;
12442
12443 /* There may be padding glyphs left over. Overwrite them too. */
12444 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12445 {
12446 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12447 while (from < end)
12448 *to++ = *from++;
12449 }
12450
12451 if (to > toend)
12452 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12453 }
12454
12455
12456 /* Compute the pixel height and width of IT->glyph_row.
12457
12458 Most of the time, ascent and height of a display line will be equal
12459 to the max_ascent and max_height values of the display iterator
12460 structure. This is not the case if
12461
12462 1. We hit ZV without displaying anything. In this case, max_ascent
12463 and max_height will be zero.
12464
12465 2. We have some glyphs that don't contribute to the line height.
12466 (The glyph row flag contributes_to_line_height_p is for future
12467 pixmap extensions).
12468
12469 The first case is easily covered by using default values because in
12470 these cases, the line height does not really matter, except that it
12471 must not be zero. */
12472
12473 static void
12474 compute_line_metrics (it)
12475 struct it *it;
12476 {
12477 struct glyph_row *row = it->glyph_row;
12478 int area, i;
12479
12480 if (FRAME_WINDOW_P (it->f))
12481 {
12482 int i, min_y, max_y;
12483
12484 /* The line may consist of one space only, that was added to
12485 place the cursor on it. If so, the row's height hasn't been
12486 computed yet. */
12487 if (row->height == 0)
12488 {
12489 if (it->max_ascent + it->max_descent == 0)
12490 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12491 row->ascent = it->max_ascent;
12492 row->height = it->max_ascent + it->max_descent;
12493 row->phys_ascent = it->max_phys_ascent;
12494 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12495 }
12496
12497 /* Compute the width of this line. */
12498 row->pixel_width = row->x;
12499 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12500 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12501
12502 xassert (row->pixel_width >= 0);
12503 xassert (row->ascent >= 0 && row->height > 0);
12504
12505 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12506 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12507
12508 /* If first line's physical ascent is larger than its logical
12509 ascent, use the physical ascent, and make the row taller.
12510 This makes accented characters fully visible. */
12511 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12512 && row->phys_ascent > row->ascent)
12513 {
12514 row->height += row->phys_ascent - row->ascent;
12515 row->ascent = row->phys_ascent;
12516 }
12517
12518 /* Compute how much of the line is visible. */
12519 row->visible_height = row->height;
12520
12521 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12522 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12523
12524 if (row->y < min_y)
12525 row->visible_height -= min_y - row->y;
12526 if (row->y + row->height > max_y)
12527 row->visible_height -= row->y + row->height - max_y;
12528 }
12529 else
12530 {
12531 row->pixel_width = row->used[TEXT_AREA];
12532 if (row->continued_p)
12533 row->pixel_width -= it->continuation_pixel_width;
12534 else if (row->truncated_on_right_p)
12535 row->pixel_width -= it->truncation_pixel_width;
12536 row->ascent = row->phys_ascent = 0;
12537 row->height = row->phys_height = row->visible_height = 1;
12538 }
12539
12540 /* Compute a hash code for this row. */
12541 row->hash = 0;
12542 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12543 for (i = 0; i < row->used[area]; ++i)
12544 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12545 + row->glyphs[area][i].u.val
12546 + row->glyphs[area][i].face_id
12547 + row->glyphs[area][i].padding_p
12548 + (row->glyphs[area][i].type << 2));
12549
12550 it->max_ascent = it->max_descent = 0;
12551 it->max_phys_ascent = it->max_phys_descent = 0;
12552 }
12553
12554
12555 /* Append one space to the glyph row of iterator IT if doing a
12556 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12557 space have the default face, otherwise let it have the same face as
12558 IT->face_id. Value is non-zero if a space was added.
12559
12560 This function is called to make sure that there is always one glyph
12561 at the end of a glyph row that the cursor can be set on under
12562 window-systems. (If there weren't such a glyph we would not know
12563 how wide and tall a box cursor should be displayed).
12564
12565 At the same time this space let's a nicely handle clearing to the
12566 end of the line if the row ends in italic text. */
12567
12568 static int
12569 append_space (it, default_face_p)
12570 struct it *it;
12571 int default_face_p;
12572 {
12573 if (FRAME_WINDOW_P (it->f))
12574 {
12575 int n = it->glyph_row->used[TEXT_AREA];
12576
12577 if (it->glyph_row->glyphs[TEXT_AREA] + n
12578 < it->glyph_row->glyphs[1 + TEXT_AREA])
12579 {
12580 /* Save some values that must not be changed.
12581 Must save IT->c and IT->len because otherwise
12582 ITERATOR_AT_END_P wouldn't work anymore after
12583 append_space has been called. */
12584 enum display_element_type saved_what = it->what;
12585 int saved_c = it->c, saved_len = it->len;
12586 int saved_x = it->current_x;
12587 int saved_face_id = it->face_id;
12588 struct text_pos saved_pos;
12589 Lisp_Object saved_object;
12590 struct face *face;
12591
12592 saved_object = it->object;
12593 saved_pos = it->position;
12594
12595 it->what = IT_CHARACTER;
12596 bzero (&it->position, sizeof it->position);
12597 it->object = make_number (0);
12598 it->c = ' ';
12599 it->len = 1;
12600
12601 if (default_face_p)
12602 it->face_id = DEFAULT_FACE_ID;
12603 else if (it->face_before_selective_p)
12604 it->face_id = it->saved_face_id;
12605 face = FACE_FROM_ID (it->f, it->face_id);
12606 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12607
12608 PRODUCE_GLYPHS (it);
12609
12610 it->current_x = saved_x;
12611 it->object = saved_object;
12612 it->position = saved_pos;
12613 it->what = saved_what;
12614 it->face_id = saved_face_id;
12615 it->len = saved_len;
12616 it->c = saved_c;
12617 return 1;
12618 }
12619 }
12620
12621 return 0;
12622 }
12623
12624
12625 /* Extend the face of the last glyph in the text area of IT->glyph_row
12626 to the end of the display line. Called from display_line.
12627 If the glyph row is empty, add a space glyph to it so that we
12628 know the face to draw. Set the glyph row flag fill_line_p. */
12629
12630 static void
12631 extend_face_to_end_of_line (it)
12632 struct it *it;
12633 {
12634 struct face *face;
12635 struct frame *f = it->f;
12636
12637 /* If line is already filled, do nothing. */
12638 if (it->current_x >= it->last_visible_x)
12639 return;
12640
12641 /* Face extension extends the background and box of IT->face_id
12642 to the end of the line. If the background equals the background
12643 of the frame, we don't have to do anything. */
12644 if (it->face_before_selective_p)
12645 face = FACE_FROM_ID (it->f, it->saved_face_id);
12646 else
12647 face = FACE_FROM_ID (f, it->face_id);
12648
12649 if (FRAME_WINDOW_P (f)
12650 && face->box == FACE_NO_BOX
12651 && face->background == FRAME_BACKGROUND_PIXEL (f)
12652 && !face->stipple)
12653 return;
12654
12655 /* Set the glyph row flag indicating that the face of the last glyph
12656 in the text area has to be drawn to the end of the text area. */
12657 it->glyph_row->fill_line_p = 1;
12658
12659 /* If current character of IT is not ASCII, make sure we have the
12660 ASCII face. This will be automatically undone the next time
12661 get_next_display_element returns a multibyte character. Note
12662 that the character will always be single byte in unibyte text. */
12663 if (!SINGLE_BYTE_CHAR_P (it->c))
12664 {
12665 it->face_id = FACE_FOR_CHAR (f, face, 0);
12666 }
12667
12668 if (FRAME_WINDOW_P (f))
12669 {
12670 /* If the row is empty, add a space with the current face of IT,
12671 so that we know which face to draw. */
12672 if (it->glyph_row->used[TEXT_AREA] == 0)
12673 {
12674 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12675 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12676 it->glyph_row->used[TEXT_AREA] = 1;
12677 }
12678 }
12679 else
12680 {
12681 /* Save some values that must not be changed. */
12682 int saved_x = it->current_x;
12683 struct text_pos saved_pos;
12684 Lisp_Object saved_object;
12685 enum display_element_type saved_what = it->what;
12686 int saved_face_id = it->face_id;
12687
12688 saved_object = it->object;
12689 saved_pos = it->position;
12690
12691 it->what = IT_CHARACTER;
12692 bzero (&it->position, sizeof it->position);
12693 it->object = make_number (0);
12694 it->c = ' ';
12695 it->len = 1;
12696 it->face_id = face->id;
12697
12698 PRODUCE_GLYPHS (it);
12699
12700 while (it->current_x <= it->last_visible_x)
12701 PRODUCE_GLYPHS (it);
12702
12703 /* Don't count these blanks really. It would let us insert a left
12704 truncation glyph below and make us set the cursor on them, maybe. */
12705 it->current_x = saved_x;
12706 it->object = saved_object;
12707 it->position = saved_pos;
12708 it->what = saved_what;
12709 it->face_id = saved_face_id;
12710 }
12711 }
12712
12713
12714 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12715 trailing whitespace. */
12716
12717 static int
12718 trailing_whitespace_p (charpos)
12719 int charpos;
12720 {
12721 int bytepos = CHAR_TO_BYTE (charpos);
12722 int c = 0;
12723
12724 while (bytepos < ZV_BYTE
12725 && (c = FETCH_CHAR (bytepos),
12726 c == ' ' || c == '\t'))
12727 ++bytepos;
12728
12729 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12730 {
12731 if (bytepos != PT_BYTE)
12732 return 1;
12733 }
12734 return 0;
12735 }
12736
12737
12738 /* Highlight trailing whitespace, if any, in ROW. */
12739
12740 void
12741 highlight_trailing_whitespace (f, row)
12742 struct frame *f;
12743 struct glyph_row *row;
12744 {
12745 int used = row->used[TEXT_AREA];
12746
12747 if (used)
12748 {
12749 struct glyph *start = row->glyphs[TEXT_AREA];
12750 struct glyph *glyph = start + used - 1;
12751
12752 /* Skip over glyphs inserted to display the cursor at the
12753 end of a line, for extending the face of the last glyph
12754 to the end of the line on terminals, and for truncation
12755 and continuation glyphs. */
12756 while (glyph >= start
12757 && glyph->type == CHAR_GLYPH
12758 && INTEGERP (glyph->object))
12759 --glyph;
12760
12761 /* If last glyph is a space or stretch, and it's trailing
12762 whitespace, set the face of all trailing whitespace glyphs in
12763 IT->glyph_row to `trailing-whitespace'. */
12764 if (glyph >= start
12765 && BUFFERP (glyph->object)
12766 && (glyph->type == STRETCH_GLYPH
12767 || (glyph->type == CHAR_GLYPH
12768 && glyph->u.ch == ' '))
12769 && trailing_whitespace_p (glyph->charpos))
12770 {
12771 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12772
12773 while (glyph >= start
12774 && BUFFERP (glyph->object)
12775 && (glyph->type == STRETCH_GLYPH
12776 || (glyph->type == CHAR_GLYPH
12777 && glyph->u.ch == ' ')))
12778 (glyph--)->face_id = face_id;
12779 }
12780 }
12781 }
12782
12783
12784 /* Value is non-zero if glyph row ROW in window W should be
12785 used to hold the cursor. */
12786
12787 static int
12788 cursor_row_p (w, row)
12789 struct window *w;
12790 struct glyph_row *row;
12791 {
12792 int cursor_row_p = 1;
12793
12794 if (PT == MATRIX_ROW_END_CHARPOS (row))
12795 {
12796 /* If the row ends with a newline from a string, we don't want
12797 the cursor there (if the row is continued it doesn't end in a
12798 newline). */
12799 if (CHARPOS (row->end.string_pos) >= 0
12800 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12801 cursor_row_p = row->continued_p;
12802
12803 /* If the row ends at ZV, display the cursor at the end of that
12804 row instead of at the start of the row below. */
12805 else if (row->ends_at_zv_p)
12806 cursor_row_p = 1;
12807 else
12808 cursor_row_p = 0;
12809 }
12810
12811 return cursor_row_p;
12812 }
12813
12814
12815 /* Construct the glyph row IT->glyph_row in the desired matrix of
12816 IT->w from text at the current position of IT. See dispextern.h
12817 for an overview of struct it. Value is non-zero if
12818 IT->glyph_row displays text, as opposed to a line displaying ZV
12819 only. */
12820
12821 static int
12822 display_line (it)
12823 struct it *it;
12824 {
12825 struct glyph_row *row = it->glyph_row;
12826
12827 /* We always start displaying at hpos zero even if hscrolled. */
12828 xassert (it->hpos == 0 && it->current_x == 0);
12829
12830 /* We must not display in a row that's not a text row. */
12831 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12832 < it->w->desired_matrix->nrows);
12833
12834 /* Is IT->w showing the region? */
12835 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12836
12837 /* Clear the result glyph row and enable it. */
12838 prepare_desired_row (row);
12839
12840 row->y = it->current_y;
12841 row->start = it->current;
12842 row->continuation_lines_width = it->continuation_lines_width;
12843 row->displays_text_p = 1;
12844 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12845 it->starts_in_middle_of_char_p = 0;
12846
12847 /* Arrange the overlays nicely for our purposes. Usually, we call
12848 display_line on only one line at a time, in which case this
12849 can't really hurt too much, or we call it on lines which appear
12850 one after another in the buffer, in which case all calls to
12851 recenter_overlay_lists but the first will be pretty cheap. */
12852 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12853
12854 /* Move over display elements that are not visible because we are
12855 hscrolled. This may stop at an x-position < IT->first_visible_x
12856 if the first glyph is partially visible or if we hit a line end. */
12857 if (it->current_x < it->first_visible_x)
12858 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12859 MOVE_TO_POS | MOVE_TO_X);
12860
12861 /* Get the initial row height. This is either the height of the
12862 text hscrolled, if there is any, or zero. */
12863 row->ascent = it->max_ascent;
12864 row->height = it->max_ascent + it->max_descent;
12865 row->phys_ascent = it->max_phys_ascent;
12866 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12867
12868 /* Loop generating characters. The loop is left with IT on the next
12869 character to display. */
12870 while (1)
12871 {
12872 int n_glyphs_before, hpos_before, x_before;
12873 int x, i, nglyphs;
12874 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12875
12876 /* Retrieve the next thing to display. Value is zero if end of
12877 buffer reached. */
12878 if (!get_next_display_element (it))
12879 {
12880 /* Maybe add a space at the end of this line that is used to
12881 display the cursor there under X. Set the charpos of the
12882 first glyph of blank lines not corresponding to any text
12883 to -1. */
12884 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12885 || row->used[TEXT_AREA] == 0)
12886 {
12887 row->glyphs[TEXT_AREA]->charpos = -1;
12888 row->displays_text_p = 0;
12889
12890 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
12891 && (!MINI_WINDOW_P (it->w)
12892 || (minibuf_level && EQ (it->window, minibuf_window))))
12893 row->indicate_empty_line_p = 1;
12894 }
12895
12896 it->continuation_lines_width = 0;
12897 row->ends_at_zv_p = 1;
12898 break;
12899 }
12900
12901 /* Now, get the metrics of what we want to display. This also
12902 generates glyphs in `row' (which is IT->glyph_row). */
12903 n_glyphs_before = row->used[TEXT_AREA];
12904 x = it->current_x;
12905
12906 /* Remember the line height so far in case the next element doesn't
12907 fit on the line. */
12908 if (!it->truncate_lines_p)
12909 {
12910 ascent = it->max_ascent;
12911 descent = it->max_descent;
12912 phys_ascent = it->max_phys_ascent;
12913 phys_descent = it->max_phys_descent;
12914 }
12915
12916 PRODUCE_GLYPHS (it);
12917
12918 /* If this display element was in marginal areas, continue with
12919 the next one. */
12920 if (it->area != TEXT_AREA)
12921 {
12922 row->ascent = max (row->ascent, it->max_ascent);
12923 row->height = max (row->height, it->max_ascent + it->max_descent);
12924 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12925 row->phys_height = max (row->phys_height,
12926 it->max_phys_ascent + it->max_phys_descent);
12927 set_iterator_to_next (it, 1);
12928 continue;
12929 }
12930
12931 /* Does the display element fit on the line? If we truncate
12932 lines, we should draw past the right edge of the window. If
12933 we don't truncate, we want to stop so that we can display the
12934 continuation glyph before the right margin. If lines are
12935 continued, there are two possible strategies for characters
12936 resulting in more than 1 glyph (e.g. tabs): Display as many
12937 glyphs as possible in this line and leave the rest for the
12938 continuation line, or display the whole element in the next
12939 line. Original redisplay did the former, so we do it also. */
12940 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12941 hpos_before = it->hpos;
12942 x_before = x;
12943
12944 if (/* Not a newline. */
12945 nglyphs > 0
12946 /* Glyphs produced fit entirely in the line. */
12947 && it->current_x < it->last_visible_x)
12948 {
12949 it->hpos += nglyphs;
12950 row->ascent = max (row->ascent, it->max_ascent);
12951 row->height = max (row->height, it->max_ascent + it->max_descent);
12952 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12953 row->phys_height = max (row->phys_height,
12954 it->max_phys_ascent + it->max_phys_descent);
12955 if (it->current_x - it->pixel_width < it->first_visible_x)
12956 row->x = x - it->first_visible_x;
12957 }
12958 else
12959 {
12960 int new_x;
12961 struct glyph *glyph;
12962
12963 for (i = 0; i < nglyphs; ++i, x = new_x)
12964 {
12965 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12966 new_x = x + glyph->pixel_width;
12967
12968 if (/* Lines are continued. */
12969 !it->truncate_lines_p
12970 && (/* Glyph doesn't fit on the line. */
12971 new_x > it->last_visible_x
12972 /* Or it fits exactly on a window system frame. */
12973 || (new_x == it->last_visible_x
12974 && FRAME_WINDOW_P (it->f))))
12975 {
12976 /* End of a continued line. */
12977
12978 if (it->hpos == 0
12979 || (new_x == it->last_visible_x
12980 && FRAME_WINDOW_P (it->f)))
12981 {
12982 /* Current glyph is the only one on the line or
12983 fits exactly on the line. We must continue
12984 the line because we can't draw the cursor
12985 after the glyph. */
12986 row->continued_p = 1;
12987 it->current_x = new_x;
12988 it->continuation_lines_width += new_x;
12989 ++it->hpos;
12990 if (i == nglyphs - 1)
12991 set_iterator_to_next (it, 1);
12992 }
12993 else if (CHAR_GLYPH_PADDING_P (*glyph)
12994 && !FRAME_WINDOW_P (it->f))
12995 {
12996 /* A padding glyph that doesn't fit on this line.
12997 This means the whole character doesn't fit
12998 on the line. */
12999 row->used[TEXT_AREA] = n_glyphs_before;
13000
13001 /* Fill the rest of the row with continuation
13002 glyphs like in 20.x. */
13003 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13004 < row->glyphs[1 + TEXT_AREA])
13005 produce_special_glyphs (it, IT_CONTINUATION);
13006
13007 row->continued_p = 1;
13008 it->current_x = x_before;
13009 it->continuation_lines_width += x_before;
13010
13011 /* Restore the height to what it was before the
13012 element not fitting on the line. */
13013 it->max_ascent = ascent;
13014 it->max_descent = descent;
13015 it->max_phys_ascent = phys_ascent;
13016 it->max_phys_descent = phys_descent;
13017 }
13018 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13019 {
13020 /* A TAB that extends past the right edge of the
13021 window. This produces a single glyph on
13022 window system frames. We leave the glyph in
13023 this row and let it fill the row, but don't
13024 consume the TAB. */
13025 it->continuation_lines_width += it->last_visible_x;
13026 row->ends_in_middle_of_char_p = 1;
13027 row->continued_p = 1;
13028 glyph->pixel_width = it->last_visible_x - x;
13029 it->starts_in_middle_of_char_p = 1;
13030 }
13031 else
13032 {
13033 /* Something other than a TAB that draws past
13034 the right edge of the window. Restore
13035 positions to values before the element. */
13036 row->used[TEXT_AREA] = n_glyphs_before + i;
13037
13038 /* Display continuation glyphs. */
13039 if (!FRAME_WINDOW_P (it->f))
13040 produce_special_glyphs (it, IT_CONTINUATION);
13041 row->continued_p = 1;
13042
13043 it->continuation_lines_width += x;
13044
13045 if (nglyphs > 1 && i > 0)
13046 {
13047 row->ends_in_middle_of_char_p = 1;
13048 it->starts_in_middle_of_char_p = 1;
13049 }
13050
13051 /* Restore the height to what it was before the
13052 element not fitting on the line. */
13053 it->max_ascent = ascent;
13054 it->max_descent = descent;
13055 it->max_phys_ascent = phys_ascent;
13056 it->max_phys_descent = phys_descent;
13057 }
13058
13059 break;
13060 }
13061 else if (new_x > it->first_visible_x)
13062 {
13063 /* Increment number of glyphs actually displayed. */
13064 ++it->hpos;
13065
13066 if (x < it->first_visible_x)
13067 /* Glyph is partially visible, i.e. row starts at
13068 negative X position. */
13069 row->x = x - it->first_visible_x;
13070 }
13071 else
13072 {
13073 /* Glyph is completely off the left margin of the
13074 window. This should not happen because of the
13075 move_it_in_display_line at the start of
13076 this function. */
13077 abort ();
13078 }
13079 }
13080
13081 row->ascent = max (row->ascent, it->max_ascent);
13082 row->height = max (row->height, it->max_ascent + it->max_descent);
13083 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13084 row->phys_height = max (row->phys_height,
13085 it->max_phys_ascent + it->max_phys_descent);
13086
13087 /* End of this display line if row is continued. */
13088 if (row->continued_p)
13089 break;
13090 }
13091
13092 /* Is this a line end? If yes, we're also done, after making
13093 sure that a non-default face is extended up to the right
13094 margin of the window. */
13095 if (ITERATOR_AT_END_OF_LINE_P (it))
13096 {
13097 int used_before = row->used[TEXT_AREA];
13098
13099 row->ends_in_newline_from_string_p = STRINGP (it->object);
13100
13101 /* Add a space at the end of the line that is used to
13102 display the cursor there. */
13103 append_space (it, 0);
13104
13105 /* Extend the face to the end of the line. */
13106 extend_face_to_end_of_line (it);
13107
13108 /* Make sure we have the position. */
13109 if (used_before == 0)
13110 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13111
13112 /* Consume the line end. This skips over invisible lines. */
13113 set_iterator_to_next (it, 1);
13114 it->continuation_lines_width = 0;
13115 break;
13116 }
13117
13118 /* Proceed with next display element. Note that this skips
13119 over lines invisible because of selective display. */
13120 set_iterator_to_next (it, 1);
13121
13122 /* If we truncate lines, we are done when the last displayed
13123 glyphs reach past the right margin of the window. */
13124 if (it->truncate_lines_p
13125 && (FRAME_WINDOW_P (it->f)
13126 ? (it->current_x >= it->last_visible_x)
13127 : (it->current_x > it->last_visible_x)))
13128 {
13129 /* Maybe add truncation glyphs. */
13130 if (!FRAME_WINDOW_P (it->f))
13131 {
13132 int i, n;
13133
13134 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13135 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13136 break;
13137
13138 for (n = row->used[TEXT_AREA]; i < n; ++i)
13139 {
13140 row->used[TEXT_AREA] = i;
13141 produce_special_glyphs (it, IT_TRUNCATION);
13142 }
13143 }
13144
13145 row->truncated_on_right_p = 1;
13146 it->continuation_lines_width = 0;
13147 reseat_at_next_visible_line_start (it, 0);
13148 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13149 it->hpos = hpos_before;
13150 it->current_x = x_before;
13151 break;
13152 }
13153 }
13154
13155 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13156 at the left window margin. */
13157 if (it->first_visible_x
13158 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13159 {
13160 if (!FRAME_WINDOW_P (it->f))
13161 insert_left_trunc_glyphs (it);
13162 row->truncated_on_left_p = 1;
13163 }
13164
13165 /* If the start of this line is the overlay arrow-position, then
13166 mark this glyph row as the one containing the overlay arrow.
13167 This is clearly a mess with variable size fonts. It would be
13168 better to let it be displayed like cursors under X. */
13169 if (MARKERP (Voverlay_arrow_position)
13170 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13171 && (MATRIX_ROW_START_CHARPOS (row)
13172 == marker_position (Voverlay_arrow_position))
13173 && STRINGP (Voverlay_arrow_string)
13174 && ! overlay_arrow_seen)
13175 {
13176 /* Overlay arrow in window redisplay is a fringe bitmap. */
13177 if (!FRAME_WINDOW_P (it->f))
13178 {
13179 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13180 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13181 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13182 struct glyph *p = row->glyphs[TEXT_AREA];
13183 struct glyph *p2, *end;
13184
13185 /* Copy the arrow glyphs. */
13186 while (glyph < arrow_end)
13187 *p++ = *glyph++;
13188
13189 /* Throw away padding glyphs. */
13190 p2 = p;
13191 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13192 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13193 ++p2;
13194 if (p2 > p)
13195 {
13196 while (p2 < end)
13197 *p++ = *p2++;
13198 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13199 }
13200 }
13201
13202 overlay_arrow_seen = 1;
13203 row->overlay_arrow_p = 1;
13204 }
13205
13206 /* Compute pixel dimensions of this line. */
13207 compute_line_metrics (it);
13208
13209 /* Remember the position at which this line ends. */
13210 row->end = it->current;
13211
13212 /* Maybe set the cursor. */
13213 if (it->w->cursor.vpos < 0
13214 && PT >= MATRIX_ROW_START_CHARPOS (row)
13215 && PT <= MATRIX_ROW_END_CHARPOS (row)
13216 && cursor_row_p (it->w, row))
13217 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13218
13219 /* Highlight trailing whitespace. */
13220 if (!NILP (Vshow_trailing_whitespace))
13221 highlight_trailing_whitespace (it->f, it->glyph_row);
13222
13223 /* Prepare for the next line. This line starts horizontally at (X
13224 HPOS) = (0 0). Vertical positions are incremented. As a
13225 convenience for the caller, IT->glyph_row is set to the next
13226 row to be used. */
13227 it->current_x = it->hpos = 0;
13228 it->current_y += row->height;
13229 ++it->vpos;
13230 ++it->glyph_row;
13231 return row->displays_text_p;
13232 }
13233
13234
13235 \f
13236 /***********************************************************************
13237 Menu Bar
13238 ***********************************************************************/
13239
13240 /* Redisplay the menu bar in the frame for window W.
13241
13242 The menu bar of X frames that don't have X toolkit support is
13243 displayed in a special window W->frame->menu_bar_window.
13244
13245 The menu bar of terminal frames is treated specially as far as
13246 glyph matrices are concerned. Menu bar lines are not part of
13247 windows, so the update is done directly on the frame matrix rows
13248 for the menu bar. */
13249
13250 static void
13251 display_menu_bar (w)
13252 struct window *w;
13253 {
13254 struct frame *f = XFRAME (WINDOW_FRAME (w));
13255 struct it it;
13256 Lisp_Object items;
13257 int i;
13258
13259 /* Don't do all this for graphical frames. */
13260 #ifdef HAVE_NTGUI
13261 if (!NILP (Vwindow_system))
13262 return;
13263 #endif
13264 #ifdef USE_X_TOOLKIT
13265 if (FRAME_X_P (f))
13266 return;
13267 #endif
13268 #ifdef macintosh
13269 if (FRAME_MAC_P (f))
13270 return;
13271 #endif
13272
13273 #ifdef USE_X_TOOLKIT
13274 xassert (!FRAME_WINDOW_P (f));
13275 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13276 it.first_visible_x = 0;
13277 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13278 #else /* not USE_X_TOOLKIT */
13279 if (FRAME_WINDOW_P (f))
13280 {
13281 /* Menu bar lines are displayed in the desired matrix of the
13282 dummy window menu_bar_window. */
13283 struct window *menu_w;
13284 xassert (WINDOWP (f->menu_bar_window));
13285 menu_w = XWINDOW (f->menu_bar_window);
13286 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13287 MENU_FACE_ID);
13288 it.first_visible_x = 0;
13289 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13290 }
13291 else
13292 {
13293 /* This is a TTY frame, i.e. character hpos/vpos are used as
13294 pixel x/y. */
13295 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13296 MENU_FACE_ID);
13297 it.first_visible_x = 0;
13298 it.last_visible_x = FRAME_WIDTH (f);
13299 }
13300 #endif /* not USE_X_TOOLKIT */
13301
13302 if (! mode_line_inverse_video)
13303 /* Force the menu-bar to be displayed in the default face. */
13304 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13305
13306 /* Clear all rows of the menu bar. */
13307 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13308 {
13309 struct glyph_row *row = it.glyph_row + i;
13310 clear_glyph_row (row);
13311 row->enabled_p = 1;
13312 row->full_width_p = 1;
13313 }
13314
13315 /* Display all items of the menu bar. */
13316 items = FRAME_MENU_BAR_ITEMS (it.f);
13317 for (i = 0; i < XVECTOR (items)->size; i += 4)
13318 {
13319 Lisp_Object string;
13320
13321 /* Stop at nil string. */
13322 string = AREF (items, i + 1);
13323 if (NILP (string))
13324 break;
13325
13326 /* Remember where item was displayed. */
13327 AREF (items, i + 3) = make_number (it.hpos);
13328
13329 /* Display the item, pad with one space. */
13330 if (it.current_x < it.last_visible_x)
13331 display_string (NULL, string, Qnil, 0, 0, &it,
13332 XSTRING (string)->size + 1, 0, 0, -1);
13333 }
13334
13335 /* Fill out the line with spaces. */
13336 if (it.current_x < it.last_visible_x)
13337 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13338
13339 /* Compute the total height of the lines. */
13340 compute_line_metrics (&it);
13341 }
13342
13343
13344 \f
13345 /***********************************************************************
13346 Mode Line
13347 ***********************************************************************/
13348
13349 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13350 FORCE is non-zero, redisplay mode lines unconditionally.
13351 Otherwise, redisplay only mode lines that are garbaged. Value is
13352 the number of windows whose mode lines were redisplayed. */
13353
13354 static int
13355 redisplay_mode_lines (window, force)
13356 Lisp_Object window;
13357 int force;
13358 {
13359 int nwindows = 0;
13360
13361 while (!NILP (window))
13362 {
13363 struct window *w = XWINDOW (window);
13364
13365 if (WINDOWP (w->hchild))
13366 nwindows += redisplay_mode_lines (w->hchild, force);
13367 else if (WINDOWP (w->vchild))
13368 nwindows += redisplay_mode_lines (w->vchild, force);
13369 else if (force
13370 || FRAME_GARBAGED_P (XFRAME (w->frame))
13371 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13372 {
13373 struct text_pos lpoint;
13374 struct buffer *old = current_buffer;
13375
13376 /* Set the window's buffer for the mode line display. */
13377 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13378 set_buffer_internal_1 (XBUFFER (w->buffer));
13379
13380 /* Point refers normally to the selected window. For any
13381 other window, set up appropriate value. */
13382 if (!EQ (window, selected_window))
13383 {
13384 struct text_pos pt;
13385
13386 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13387 if (CHARPOS (pt) < BEGV)
13388 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13389 else if (CHARPOS (pt) > (ZV - 1))
13390 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13391 else
13392 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13393 }
13394
13395 /* Display mode lines. */
13396 clear_glyph_matrix (w->desired_matrix);
13397 if (display_mode_lines (w))
13398 {
13399 ++nwindows;
13400 w->must_be_updated_p = 1;
13401 }
13402
13403 /* Restore old settings. */
13404 set_buffer_internal_1 (old);
13405 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13406 }
13407
13408 window = w->next;
13409 }
13410
13411 return nwindows;
13412 }
13413
13414
13415 /* Display the mode and/or top line of window W. Value is the number
13416 of mode lines displayed. */
13417
13418 static int
13419 display_mode_lines (w)
13420 struct window *w;
13421 {
13422 Lisp_Object old_selected_window, old_selected_frame;
13423 int n = 0;
13424
13425 old_selected_frame = selected_frame;
13426 selected_frame = w->frame;
13427 old_selected_window = selected_window;
13428 XSETWINDOW (selected_window, w);
13429
13430 /* These will be set while the mode line specs are processed. */
13431 line_number_displayed = 0;
13432 w->column_number_displayed = Qnil;
13433
13434 if (WINDOW_WANTS_MODELINE_P (w))
13435 {
13436 struct window *old_w = XWINDOW (old_selected_window);
13437
13438 /* Select mode line face based on the real selected window. */
13439 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (old_w),
13440 current_buffer->mode_line_format);
13441 ++n;
13442 }
13443
13444 if (WINDOW_WANTS_HEADER_LINE_P (w))
13445 {
13446 display_mode_line (w, HEADER_LINE_FACE_ID,
13447 current_buffer->header_line_format);
13448 ++n;
13449 }
13450
13451 selected_frame = old_selected_frame;
13452 selected_window = old_selected_window;
13453 return n;
13454 }
13455
13456
13457 /* Display mode or top line of window W. FACE_ID specifies which line
13458 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13459 FORMAT is the mode line format to display. Value is the pixel
13460 height of the mode line displayed. */
13461
13462 static int
13463 display_mode_line (w, face_id, format)
13464 struct window *w;
13465 enum face_id face_id;
13466 Lisp_Object format;
13467 {
13468 struct it it;
13469 struct face *face;
13470
13471 init_iterator (&it, w, -1, -1, NULL, face_id);
13472 prepare_desired_row (it.glyph_row);
13473
13474 if (! mode_line_inverse_video)
13475 /* Force the mode-line to be displayed in the default face. */
13476 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13477
13478 /* Temporarily make frame's keyboard the current kboard so that
13479 kboard-local variables in the mode_line_format will get the right
13480 values. */
13481 push_frame_kboard (it.f);
13482 display_mode_element (&it, 0, 0, 0, format);
13483 pop_frame_kboard ();
13484
13485 /* Fill up with spaces. */
13486 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13487
13488 compute_line_metrics (&it);
13489 it.glyph_row->full_width_p = 1;
13490 it.glyph_row->mode_line_p = 1;
13491 it.glyph_row->continued_p = 0;
13492 it.glyph_row->truncated_on_left_p = 0;
13493 it.glyph_row->truncated_on_right_p = 0;
13494
13495 /* Make a 3D mode-line have a shadow at its right end. */
13496 face = FACE_FROM_ID (it.f, face_id);
13497 extend_face_to_end_of_line (&it);
13498 if (face->box != FACE_NO_BOX)
13499 {
13500 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13501 + it.glyph_row->used[TEXT_AREA] - 1);
13502 last->right_box_line_p = 1;
13503 }
13504
13505 return it.glyph_row->height;
13506 }
13507
13508
13509 /* Contribute ELT to the mode line for window IT->w. How it
13510 translates into text depends on its data type.
13511
13512 IT describes the display environment in which we display, as usual.
13513
13514 DEPTH is the depth in recursion. It is used to prevent
13515 infinite recursion here.
13516
13517 FIELD_WIDTH is the number of characters the display of ELT should
13518 occupy in the mode line, and PRECISION is the maximum number of
13519 characters to display from ELT's representation. See
13520 display_string for details.
13521
13522 Returns the hpos of the end of the text generated by ELT. */
13523
13524 static int
13525 display_mode_element (it, depth, field_width, precision, elt)
13526 struct it *it;
13527 int depth;
13528 int field_width, precision;
13529 Lisp_Object elt;
13530 {
13531 int n = 0, field, prec;
13532
13533 tail_recurse:
13534 if (depth > 10)
13535 goto invalid;
13536
13537 depth++;
13538
13539 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13540 {
13541 case Lisp_String:
13542 {
13543 /* A string: output it and check for %-constructs within it. */
13544 unsigned char c;
13545 unsigned char *this = XSTRING (elt)->data;
13546 unsigned char *lisp_string = this;
13547
13548 while ((precision <= 0 || n < precision)
13549 && *this
13550 && (frame_title_ptr
13551 || it->current_x < it->last_visible_x))
13552 {
13553 unsigned char *last = this;
13554
13555 /* Advance to end of string or next format specifier. */
13556 while ((c = *this++) != '\0' && c != '%')
13557 ;
13558
13559 if (this - 1 != last)
13560 {
13561 /* Output to end of string or up to '%'. Field width
13562 is length of string. Don't output more than
13563 PRECISION allows us. */
13564 --this;
13565
13566 prec = chars_in_text (last, this - last);
13567 if (precision > 0 && prec > precision - n)
13568 prec = precision - n;
13569
13570 if (frame_title_ptr)
13571 n += store_frame_title (last, 0, prec);
13572 else
13573 {
13574 int bytepos = last - lisp_string;
13575 int charpos = string_byte_to_char (elt, bytepos);
13576 n += display_string (NULL, elt, Qnil, 0, charpos,
13577 it, 0, prec, 0,
13578 STRING_MULTIBYTE (elt));
13579 }
13580 }
13581 else /* c == '%' */
13582 {
13583 unsigned char *percent_position = this;
13584
13585 /* Get the specified minimum width. Zero means
13586 don't pad. */
13587 field = 0;
13588 while ((c = *this++) >= '0' && c <= '9')
13589 field = field * 10 + c - '0';
13590
13591 /* Don't pad beyond the total padding allowed. */
13592 if (field_width - n > 0 && field > field_width - n)
13593 field = field_width - n;
13594
13595 /* Note that either PRECISION <= 0 or N < PRECISION. */
13596 prec = precision - n;
13597
13598 if (c == 'M')
13599 n += display_mode_element (it, depth, field, prec,
13600 Vglobal_mode_string);
13601 else if (c != 0)
13602 {
13603 int multibyte;
13604 unsigned char *spec
13605 = decode_mode_spec (it->w, c, field, prec, &multibyte);
13606
13607 if (frame_title_ptr)
13608 n += store_frame_title (spec, field, prec);
13609 else
13610 {
13611 int nglyphs_before, bytepos, charpos, nwritten;
13612
13613 nglyphs_before = it->glyph_row->used[TEXT_AREA];
13614 bytepos = percent_position - XSTRING (elt)->data;
13615 charpos = (STRING_MULTIBYTE (elt)
13616 ? string_byte_to_char (elt, bytepos)
13617 : bytepos);
13618 nwritten = display_string (spec, Qnil, elt,
13619 charpos, 0, it,
13620 field, prec, 0,
13621 multibyte);
13622
13623 /* Assign to the glyphs written above the
13624 string where the `%x' came from, position
13625 of the `%'. */
13626 if (nwritten > 0)
13627 {
13628 struct glyph *glyph
13629 = (it->glyph_row->glyphs[TEXT_AREA]
13630 + nglyphs_before);
13631 int i;
13632
13633 for (i = 0; i < nwritten; ++i)
13634 {
13635 glyph[i].object = elt;
13636 glyph[i].charpos = charpos;
13637 }
13638
13639 n += nwritten;
13640 }
13641 }
13642 }
13643 else /* c == 0 */
13644 break;
13645 }
13646 }
13647 }
13648 break;
13649
13650 case Lisp_Symbol:
13651 /* A symbol: process the value of the symbol recursively
13652 as if it appeared here directly. Avoid error if symbol void.
13653 Special case: if value of symbol is a string, output the string
13654 literally. */
13655 {
13656 register Lisp_Object tem;
13657 tem = Fboundp (elt);
13658 if (!NILP (tem))
13659 {
13660 tem = Fsymbol_value (elt);
13661 /* If value is a string, output that string literally:
13662 don't check for % within it. */
13663 if (STRINGP (tem))
13664 {
13665 prec = precision - n;
13666 if (frame_title_ptr)
13667 n += store_frame_title (XSTRING (tem)->data, -1, prec);
13668 else
13669 n += display_string (NULL, tem, Qnil, 0, 0, it,
13670 0, prec, 0, STRING_MULTIBYTE (tem));
13671 }
13672 else if (!EQ (tem, elt))
13673 {
13674 /* Give up right away for nil or t. */
13675 elt = tem;
13676 goto tail_recurse;
13677 }
13678 }
13679 }
13680 break;
13681
13682 case Lisp_Cons:
13683 {
13684 register Lisp_Object car, tem;
13685
13686 /* A cons cell: three distinct cases.
13687 If first element is a string or a cons, process all the elements
13688 and effectively concatenate them.
13689 If first element is a negative number, truncate displaying cdr to
13690 at most that many characters. If positive, pad (with spaces)
13691 to at least that many characters.
13692 If first element is a symbol, process the cadr or caddr recursively
13693 according to whether the symbol's value is non-nil or nil. */
13694 car = XCAR (elt);
13695 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
13696 {
13697 /* An element of the form (:eval FORM) means evaluate FORM
13698 and use the result as mode line elements. */
13699 struct gcpro gcpro1;
13700 Lisp_Object spec;
13701
13702 spec = safe_eval (XCAR (XCDR (elt)));
13703 GCPRO1 (spec);
13704 n += display_mode_element (it, depth, field_width - n,
13705 precision - n, spec);
13706 UNGCPRO;
13707 }
13708 else if (SYMBOLP (car))
13709 {
13710 tem = Fboundp (car);
13711 elt = XCDR (elt);
13712 if (!CONSP (elt))
13713 goto invalid;
13714 /* elt is now the cdr, and we know it is a cons cell.
13715 Use its car if CAR has a non-nil value. */
13716 if (!NILP (tem))
13717 {
13718 tem = Fsymbol_value (car);
13719 if (!NILP (tem))
13720 {
13721 elt = XCAR (elt);
13722 goto tail_recurse;
13723 }
13724 }
13725 /* Symbol's value is nil (or symbol is unbound)
13726 Get the cddr of the original list
13727 and if possible find the caddr and use that. */
13728 elt = XCDR (elt);
13729 if (NILP (elt))
13730 break;
13731 else if (!CONSP (elt))
13732 goto invalid;
13733 elt = XCAR (elt);
13734 goto tail_recurse;
13735 }
13736 else if (INTEGERP (car))
13737 {
13738 register int lim = XINT (car);
13739 elt = XCDR (elt);
13740 if (lim < 0)
13741 {
13742 /* Negative int means reduce maximum width. */
13743 if (precision <= 0)
13744 precision = -lim;
13745 else
13746 precision = min (precision, -lim);
13747 }
13748 else if (lim > 0)
13749 {
13750 /* Padding specified. Don't let it be more than
13751 current maximum. */
13752 if (precision > 0)
13753 lim = min (precision, lim);
13754
13755 /* If that's more padding than already wanted, queue it.
13756 But don't reduce padding already specified even if
13757 that is beyond the current truncation point. */
13758 field_width = max (lim, field_width);
13759 }
13760 goto tail_recurse;
13761 }
13762 else if (STRINGP (car) || CONSP (car))
13763 {
13764 register int limit = 50;
13765 /* Limit is to protect against circular lists. */
13766 while (CONSP (elt)
13767 && --limit > 0
13768 && (precision <= 0 || n < precision))
13769 {
13770 n += display_mode_element (it, depth, field_width - n,
13771 precision - n, XCAR (elt));
13772 elt = XCDR (elt);
13773 }
13774 }
13775 }
13776 break;
13777
13778 default:
13779 invalid:
13780 if (frame_title_ptr)
13781 n += store_frame_title ("*invalid*", 0, precision - n);
13782 else
13783 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13784 precision - n, 0, 0);
13785 return n;
13786 }
13787
13788 /* Pad to FIELD_WIDTH. */
13789 if (field_width > 0 && n < field_width)
13790 {
13791 if (frame_title_ptr)
13792 n += store_frame_title ("", field_width - n, 0);
13793 else
13794 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13795 0, 0, 0);
13796 }
13797
13798 return n;
13799 }
13800
13801
13802 /* Write a null-terminated, right justified decimal representation of
13803 the positive integer D to BUF using a minimal field width WIDTH. */
13804
13805 static void
13806 pint2str (buf, width, d)
13807 register char *buf;
13808 register int width;
13809 register int d;
13810 {
13811 register char *p = buf;
13812
13813 if (d <= 0)
13814 *p++ = '0';
13815 else
13816 {
13817 while (d > 0)
13818 {
13819 *p++ = d % 10 + '0';
13820 d /= 10;
13821 }
13822 }
13823
13824 for (width -= (int) (p - buf); width > 0; --width)
13825 *p++ = ' ';
13826 *p-- = '\0';
13827 while (p > buf)
13828 {
13829 d = *buf;
13830 *buf++ = *p;
13831 *p-- = d;
13832 }
13833 }
13834
13835 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
13836 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13837 type of CODING_SYSTEM. Return updated pointer into BUF. */
13838
13839 static unsigned char invalid_eol_type[] = "(*invalid*)";
13840
13841 static char *
13842 decode_mode_spec_coding (coding_system, buf, eol_flag)
13843 Lisp_Object coding_system;
13844 register char *buf;
13845 int eol_flag;
13846 {
13847 Lisp_Object val;
13848 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
13849 unsigned char *eol_str;
13850 int eol_str_len;
13851 /* The EOL conversion we are using. */
13852 Lisp_Object eoltype;
13853
13854 val = Fget (coding_system, Qcoding_system);
13855 eoltype = Qnil;
13856
13857 if (!VECTORP (val)) /* Not yet decided. */
13858 {
13859 if (multibyte)
13860 *buf++ = '-';
13861 if (eol_flag)
13862 eoltype = eol_mnemonic_undecided;
13863 /* Don't mention EOL conversion if it isn't decided. */
13864 }
13865 else
13866 {
13867 Lisp_Object eolvalue;
13868
13869 eolvalue = Fget (coding_system, Qeol_type);
13870
13871 if (multibyte)
13872 *buf++ = XFASTINT (AREF (val, 1));
13873
13874 if (eol_flag)
13875 {
13876 /* The EOL conversion that is normal on this system. */
13877
13878 if (NILP (eolvalue)) /* Not yet decided. */
13879 eoltype = eol_mnemonic_undecided;
13880 else if (VECTORP (eolvalue)) /* Not yet decided. */
13881 eoltype = eol_mnemonic_undecided;
13882 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
13883 eoltype = (XFASTINT (eolvalue) == 0
13884 ? eol_mnemonic_unix
13885 : (XFASTINT (eolvalue) == 1
13886 ? eol_mnemonic_dos : eol_mnemonic_mac));
13887 }
13888 }
13889
13890 if (eol_flag)
13891 {
13892 /* Mention the EOL conversion if it is not the usual one. */
13893 if (STRINGP (eoltype))
13894 {
13895 eol_str = XSTRING (eoltype)->data;
13896 eol_str_len = XSTRING (eoltype)->size;
13897 }
13898 else if (INTEGERP (eoltype)
13899 && CHAR_VALID_P (XINT (eoltype), 0))
13900 {
13901 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
13902 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
13903 }
13904 else
13905 {
13906 eol_str = invalid_eol_type;
13907 eol_str_len = sizeof (invalid_eol_type) - 1;
13908 }
13909 bcopy (eol_str, buf, eol_str_len);
13910 buf += eol_str_len;
13911 }
13912
13913 return buf;
13914 }
13915
13916 /* Return a string for the output of a mode line %-spec for window W,
13917 generated by character C. PRECISION >= 0 means don't return a
13918 string longer than that value. FIELD_WIDTH > 0 means pad the
13919 string returned with spaces to that value. Return 1 in *MULTIBYTE
13920 if the result is multibyte text. */
13921
13922 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
13923
13924 static char *
13925 decode_mode_spec (w, c, field_width, precision, multibyte)
13926 struct window *w;
13927 register int c;
13928 int field_width, precision;
13929 int *multibyte;
13930 {
13931 Lisp_Object obj;
13932 struct frame *f = XFRAME (WINDOW_FRAME (w));
13933 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
13934 struct buffer *b = XBUFFER (w->buffer);
13935
13936 obj = Qnil;
13937 *multibyte = 0;
13938
13939 switch (c)
13940 {
13941 case '*':
13942 if (!NILP (b->read_only))
13943 return "%";
13944 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13945 return "*";
13946 return "-";
13947
13948 case '+':
13949 /* This differs from %* only for a modified read-only buffer. */
13950 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13951 return "*";
13952 if (!NILP (b->read_only))
13953 return "%";
13954 return "-";
13955
13956 case '&':
13957 /* This differs from %* in ignoring read-only-ness. */
13958 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13959 return "*";
13960 return "-";
13961
13962 case '%':
13963 return "%";
13964
13965 case '[':
13966 {
13967 int i;
13968 char *p;
13969
13970 if (command_loop_level > 5)
13971 return "[[[... ";
13972 p = decode_mode_spec_buf;
13973 for (i = 0; i < command_loop_level; i++)
13974 *p++ = '[';
13975 *p = 0;
13976 return decode_mode_spec_buf;
13977 }
13978
13979 case ']':
13980 {
13981 int i;
13982 char *p;
13983
13984 if (command_loop_level > 5)
13985 return " ...]]]";
13986 p = decode_mode_spec_buf;
13987 for (i = 0; i < command_loop_level; i++)
13988 *p++ = ']';
13989 *p = 0;
13990 return decode_mode_spec_buf;
13991 }
13992
13993 case '-':
13994 {
13995 register int i;
13996
13997 /* Let lots_of_dashes be a string of infinite length. */
13998 if (field_width <= 0
13999 || field_width > sizeof (lots_of_dashes))
14000 {
14001 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
14002 decode_mode_spec_buf[i] = '-';
14003 decode_mode_spec_buf[i] = '\0';
14004 return decode_mode_spec_buf;
14005 }
14006 else
14007 return lots_of_dashes;
14008 }
14009
14010 case 'b':
14011 obj = b->name;
14012 break;
14013
14014 case 'c':
14015 {
14016 int col = current_column ();
14017 w->column_number_displayed = make_number (col);
14018 pint2str (decode_mode_spec_buf, field_width, col);
14019 return decode_mode_spec_buf;
14020 }
14021
14022 case 'F':
14023 /* %F displays the frame name. */
14024 if (!NILP (f->title))
14025 return (char *) XSTRING (f->title)->data;
14026 if (f->explicit_name || ! FRAME_WINDOW_P (f))
14027 return (char *) XSTRING (f->name)->data;
14028 return "Emacs";
14029
14030 case 'f':
14031 obj = b->filename;
14032 break;
14033
14034 case 'l':
14035 {
14036 int startpos = XMARKER (w->start)->charpos;
14037 int startpos_byte = marker_byte_position (w->start);
14038 int line, linepos, linepos_byte, topline;
14039 int nlines, junk;
14040 int height = XFASTINT (w->height);
14041
14042 /* If we decided that this buffer isn't suitable for line numbers,
14043 don't forget that too fast. */
14044 if (EQ (w->base_line_pos, w->buffer))
14045 goto no_value;
14046 /* But do forget it, if the window shows a different buffer now. */
14047 else if (BUFFERP (w->base_line_pos))
14048 w->base_line_pos = Qnil;
14049
14050 /* If the buffer is very big, don't waste time. */
14051 if (INTEGERP (Vline_number_display_limit)
14052 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
14053 {
14054 w->base_line_pos = Qnil;
14055 w->base_line_number = Qnil;
14056 goto no_value;
14057 }
14058
14059 if (!NILP (w->base_line_number)
14060 && !NILP (w->base_line_pos)
14061 && XFASTINT (w->base_line_pos) <= startpos)
14062 {
14063 line = XFASTINT (w->base_line_number);
14064 linepos = XFASTINT (w->base_line_pos);
14065 linepos_byte = buf_charpos_to_bytepos (b, linepos);
14066 }
14067 else
14068 {
14069 line = 1;
14070 linepos = BUF_BEGV (b);
14071 linepos_byte = BUF_BEGV_BYTE (b);
14072 }
14073
14074 /* Count lines from base line to window start position. */
14075 nlines = display_count_lines (linepos, linepos_byte,
14076 startpos_byte,
14077 startpos, &junk);
14078
14079 topline = nlines + line;
14080
14081 /* Determine a new base line, if the old one is too close
14082 or too far away, or if we did not have one.
14083 "Too close" means it's plausible a scroll-down would
14084 go back past it. */
14085 if (startpos == BUF_BEGV (b))
14086 {
14087 w->base_line_number = make_number (topline);
14088 w->base_line_pos = make_number (BUF_BEGV (b));
14089 }
14090 else if (nlines < height + 25 || nlines > height * 3 + 50
14091 || linepos == BUF_BEGV (b))
14092 {
14093 int limit = BUF_BEGV (b);
14094 int limit_byte = BUF_BEGV_BYTE (b);
14095 int position;
14096 int distance = (height * 2 + 30) * line_number_display_limit_width;
14097
14098 if (startpos - distance > limit)
14099 {
14100 limit = startpos - distance;
14101 limit_byte = CHAR_TO_BYTE (limit);
14102 }
14103
14104 nlines = display_count_lines (startpos, startpos_byte,
14105 limit_byte,
14106 - (height * 2 + 30),
14107 &position);
14108 /* If we couldn't find the lines we wanted within
14109 line_number_display_limit_width chars per line,
14110 give up on line numbers for this window. */
14111 if (position == limit_byte && limit == startpos - distance)
14112 {
14113 w->base_line_pos = w->buffer;
14114 w->base_line_number = Qnil;
14115 goto no_value;
14116 }
14117
14118 w->base_line_number = make_number (topline - nlines);
14119 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
14120 }
14121
14122 /* Now count lines from the start pos to point. */
14123 nlines = display_count_lines (startpos, startpos_byte,
14124 PT_BYTE, PT, &junk);
14125
14126 /* Record that we did display the line number. */
14127 line_number_displayed = 1;
14128
14129 /* Make the string to show. */
14130 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
14131 return decode_mode_spec_buf;
14132 no_value:
14133 {
14134 char* p = decode_mode_spec_buf;
14135 int pad = field_width - 2;
14136 while (pad-- > 0)
14137 *p++ = ' ';
14138 *p++ = '?';
14139 *p++ = '?';
14140 *p = '\0';
14141 return decode_mode_spec_buf;
14142 }
14143 }
14144 break;
14145
14146 case 'm':
14147 obj = b->mode_name;
14148 break;
14149
14150 case 'n':
14151 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
14152 return " Narrow";
14153 break;
14154
14155 case 'p':
14156 {
14157 int pos = marker_position (w->start);
14158 int total = BUF_ZV (b) - BUF_BEGV (b);
14159
14160 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
14161 {
14162 if (pos <= BUF_BEGV (b))
14163 return "All";
14164 else
14165 return "Bottom";
14166 }
14167 else if (pos <= BUF_BEGV (b))
14168 return "Top";
14169 else
14170 {
14171 if (total > 1000000)
14172 /* Do it differently for a large value, to avoid overflow. */
14173 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14174 else
14175 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14176 /* We can't normally display a 3-digit number,
14177 so get us a 2-digit number that is close. */
14178 if (total == 100)
14179 total = 99;
14180 sprintf (decode_mode_spec_buf, "%2d%%", total);
14181 return decode_mode_spec_buf;
14182 }
14183 }
14184
14185 /* Display percentage of size above the bottom of the screen. */
14186 case 'P':
14187 {
14188 int toppos = marker_position (w->start);
14189 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14190 int total = BUF_ZV (b) - BUF_BEGV (b);
14191
14192 if (botpos >= BUF_ZV (b))
14193 {
14194 if (toppos <= BUF_BEGV (b))
14195 return "All";
14196 else
14197 return "Bottom";
14198 }
14199 else
14200 {
14201 if (total > 1000000)
14202 /* Do it differently for a large value, to avoid overflow. */
14203 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14204 else
14205 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14206 /* We can't normally display a 3-digit number,
14207 so get us a 2-digit number that is close. */
14208 if (total == 100)
14209 total = 99;
14210 if (toppos <= BUF_BEGV (b))
14211 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14212 else
14213 sprintf (decode_mode_spec_buf, "%2d%%", total);
14214 return decode_mode_spec_buf;
14215 }
14216 }
14217
14218 case 's':
14219 /* status of process */
14220 obj = Fget_buffer_process (w->buffer);
14221 if (NILP (obj))
14222 return "no process";
14223 #ifdef subprocesses
14224 obj = Fsymbol_name (Fprocess_status (obj));
14225 #endif
14226 break;
14227
14228 case 't': /* indicate TEXT or BINARY */
14229 #ifdef MODE_LINE_BINARY_TEXT
14230 return MODE_LINE_BINARY_TEXT (b);
14231 #else
14232 return "T";
14233 #endif
14234
14235 case 'z':
14236 /* coding-system (not including end-of-line format) */
14237 case 'Z':
14238 /* coding-system (including end-of-line type) */
14239 {
14240 int eol_flag = (c == 'Z');
14241 char *p = decode_mode_spec_buf;
14242
14243 if (! FRAME_WINDOW_P (f))
14244 {
14245 /* No need to mention EOL here--the terminal never needs
14246 to do EOL conversion. */
14247 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14248 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
14249 }
14250 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14251 p, eol_flag);
14252
14253 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14254 #ifdef subprocesses
14255 obj = Fget_buffer_process (Fcurrent_buffer ());
14256 if (PROCESSP (obj))
14257 {
14258 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14259 p, eol_flag);
14260 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14261 p, eol_flag);
14262 }
14263 #endif /* subprocesses */
14264 #endif /* 0 */
14265 *p = 0;
14266 return decode_mode_spec_buf;
14267 }
14268 }
14269
14270 if (STRINGP (obj))
14271 {
14272 *multibyte = STRING_MULTIBYTE (obj);
14273 return (char *) XSTRING (obj)->data;
14274 }
14275 else
14276 return "";
14277 }
14278
14279
14280 /* Count up to COUNT lines starting from START / START_BYTE.
14281 But don't go beyond LIMIT_BYTE.
14282 Return the number of lines thus found (always nonnegative).
14283
14284 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14285
14286 static int
14287 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14288 int start, start_byte, limit_byte, count;
14289 int *byte_pos_ptr;
14290 {
14291 register unsigned char *cursor;
14292 unsigned char *base;
14293
14294 register int ceiling;
14295 register unsigned char *ceiling_addr;
14296 int orig_count = count;
14297
14298 /* If we are not in selective display mode,
14299 check only for newlines. */
14300 int selective_display = (!NILP (current_buffer->selective_display)
14301 && !INTEGERP (current_buffer->selective_display));
14302
14303 if (count > 0)
14304 {
14305 while (start_byte < limit_byte)
14306 {
14307 ceiling = BUFFER_CEILING_OF (start_byte);
14308 ceiling = min (limit_byte - 1, ceiling);
14309 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14310 base = (cursor = BYTE_POS_ADDR (start_byte));
14311 while (1)
14312 {
14313 if (selective_display)
14314 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14315 ;
14316 else
14317 while (*cursor != '\n' && ++cursor != ceiling_addr)
14318 ;
14319
14320 if (cursor != ceiling_addr)
14321 {
14322 if (--count == 0)
14323 {
14324 start_byte += cursor - base + 1;
14325 *byte_pos_ptr = start_byte;
14326 return orig_count;
14327 }
14328 else
14329 if (++cursor == ceiling_addr)
14330 break;
14331 }
14332 else
14333 break;
14334 }
14335 start_byte += cursor - base;
14336 }
14337 }
14338 else
14339 {
14340 while (start_byte > limit_byte)
14341 {
14342 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14343 ceiling = max (limit_byte, ceiling);
14344 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14345 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14346 while (1)
14347 {
14348 if (selective_display)
14349 while (--cursor != ceiling_addr
14350 && *cursor != '\n' && *cursor != 015)
14351 ;
14352 else
14353 while (--cursor != ceiling_addr && *cursor != '\n')
14354 ;
14355
14356 if (cursor != ceiling_addr)
14357 {
14358 if (++count == 0)
14359 {
14360 start_byte += cursor - base + 1;
14361 *byte_pos_ptr = start_byte;
14362 /* When scanning backwards, we should
14363 not count the newline posterior to which we stop. */
14364 return - orig_count - 1;
14365 }
14366 }
14367 else
14368 break;
14369 }
14370 /* Here we add 1 to compensate for the last decrement
14371 of CURSOR, which took it past the valid range. */
14372 start_byte += cursor - base + 1;
14373 }
14374 }
14375
14376 *byte_pos_ptr = limit_byte;
14377
14378 if (count < 0)
14379 return - orig_count + count;
14380 return orig_count - count;
14381
14382 }
14383
14384
14385 \f
14386 /***********************************************************************
14387 Displaying strings
14388 ***********************************************************************/
14389
14390 /* Display a NUL-terminated string, starting with index START.
14391
14392 If STRING is non-null, display that C string. Otherwise, the Lisp
14393 string LISP_STRING is displayed.
14394
14395 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14396 FACE_STRING. Display STRING or LISP_STRING with the face at
14397 FACE_STRING_POS in FACE_STRING:
14398
14399 Display the string in the environment given by IT, but use the
14400 standard display table, temporarily.
14401
14402 FIELD_WIDTH is the minimum number of output glyphs to produce.
14403 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14404 with spaces. If STRING has more characters, more than FIELD_WIDTH
14405 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14406
14407 PRECISION is the maximum number of characters to output from
14408 STRING. PRECISION < 0 means don't truncate the string.
14409
14410 This is roughly equivalent to printf format specifiers:
14411
14412 FIELD_WIDTH PRECISION PRINTF
14413 ----------------------------------------
14414 -1 -1 %s
14415 -1 10 %.10s
14416 10 -1 %10s
14417 20 10 %20.10s
14418
14419 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14420 display them, and < 0 means obey the current buffer's value of
14421 enable_multibyte_characters.
14422
14423 Value is the number of glyphs produced. */
14424
14425 static int
14426 display_string (string, lisp_string, face_string, face_string_pos,
14427 start, it, field_width, precision, max_x, multibyte)
14428 unsigned char *string;
14429 Lisp_Object lisp_string;
14430 Lisp_Object face_string;
14431 int face_string_pos;
14432 int start;
14433 struct it *it;
14434 int field_width, precision, max_x;
14435 int multibyte;
14436 {
14437 int hpos_at_start = it->hpos;
14438 int saved_face_id = it->face_id;
14439 struct glyph_row *row = it->glyph_row;
14440
14441 /* Initialize the iterator IT for iteration over STRING beginning
14442 with index START. */
14443 reseat_to_string (it, string, lisp_string, start,
14444 precision, field_width, multibyte);
14445
14446 /* If displaying STRING, set up the face of the iterator
14447 from LISP_STRING, if that's given. */
14448 if (STRINGP (face_string))
14449 {
14450 int endptr;
14451 struct face *face;
14452
14453 it->face_id
14454 = face_at_string_position (it->w, face_string, face_string_pos,
14455 0, it->region_beg_charpos,
14456 it->region_end_charpos,
14457 &endptr, it->base_face_id, 0);
14458 face = FACE_FROM_ID (it->f, it->face_id);
14459 it->face_box_p = face->box != FACE_NO_BOX;
14460 }
14461
14462 /* Set max_x to the maximum allowed X position. Don't let it go
14463 beyond the right edge of the window. */
14464 if (max_x <= 0)
14465 max_x = it->last_visible_x;
14466 else
14467 max_x = min (max_x, it->last_visible_x);
14468
14469 /* Skip over display elements that are not visible. because IT->w is
14470 hscrolled. */
14471 if (it->current_x < it->first_visible_x)
14472 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14473 MOVE_TO_POS | MOVE_TO_X);
14474
14475 row->ascent = it->max_ascent;
14476 row->height = it->max_ascent + it->max_descent;
14477 row->phys_ascent = it->max_phys_ascent;
14478 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14479
14480 /* This condition is for the case that we are called with current_x
14481 past last_visible_x. */
14482 while (it->current_x < max_x)
14483 {
14484 int x_before, x, n_glyphs_before, i, nglyphs;
14485
14486 /* Get the next display element. */
14487 if (!get_next_display_element (it))
14488 break;
14489
14490 /* Produce glyphs. */
14491 x_before = it->current_x;
14492 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14493 PRODUCE_GLYPHS (it);
14494
14495 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14496 i = 0;
14497 x = x_before;
14498 while (i < nglyphs)
14499 {
14500 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14501
14502 if (!it->truncate_lines_p
14503 && x + glyph->pixel_width > max_x)
14504 {
14505 /* End of continued line or max_x reached. */
14506 if (CHAR_GLYPH_PADDING_P (*glyph))
14507 {
14508 /* A wide character is unbreakable. */
14509 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14510 it->current_x = x_before;
14511 }
14512 else
14513 {
14514 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14515 it->current_x = x;
14516 }
14517 break;
14518 }
14519 else if (x + glyph->pixel_width > it->first_visible_x)
14520 {
14521 /* Glyph is at least partially visible. */
14522 ++it->hpos;
14523 if (x < it->first_visible_x)
14524 it->glyph_row->x = x - it->first_visible_x;
14525 }
14526 else
14527 {
14528 /* Glyph is off the left margin of the display area.
14529 Should not happen. */
14530 abort ();
14531 }
14532
14533 row->ascent = max (row->ascent, it->max_ascent);
14534 row->height = max (row->height, it->max_ascent + it->max_descent);
14535 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14536 row->phys_height = max (row->phys_height,
14537 it->max_phys_ascent + it->max_phys_descent);
14538 x += glyph->pixel_width;
14539 ++i;
14540 }
14541
14542 /* Stop if max_x reached. */
14543 if (i < nglyphs)
14544 break;
14545
14546 /* Stop at line ends. */
14547 if (ITERATOR_AT_END_OF_LINE_P (it))
14548 {
14549 it->continuation_lines_width = 0;
14550 break;
14551 }
14552
14553 set_iterator_to_next (it, 1);
14554
14555 /* Stop if truncating at the right edge. */
14556 if (it->truncate_lines_p
14557 && it->current_x >= it->last_visible_x)
14558 {
14559 /* Add truncation mark, but don't do it if the line is
14560 truncated at a padding space. */
14561 if (IT_CHARPOS (*it) < it->string_nchars)
14562 {
14563 if (!FRAME_WINDOW_P (it->f))
14564 {
14565 int i, n;
14566
14567 if (it->current_x > it->last_visible_x)
14568 {
14569 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14570 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14571 break;
14572 for (n = row->used[TEXT_AREA]; i < n; ++i)
14573 {
14574 row->used[TEXT_AREA] = i;
14575 produce_special_glyphs (it, IT_TRUNCATION);
14576 }
14577 }
14578 produce_special_glyphs (it, IT_TRUNCATION);
14579 }
14580 it->glyph_row->truncated_on_right_p = 1;
14581 }
14582 break;
14583 }
14584 }
14585
14586 /* Maybe insert a truncation at the left. */
14587 if (it->first_visible_x
14588 && IT_CHARPOS (*it) > 0)
14589 {
14590 if (!FRAME_WINDOW_P (it->f))
14591 insert_left_trunc_glyphs (it);
14592 it->glyph_row->truncated_on_left_p = 1;
14593 }
14594
14595 it->face_id = saved_face_id;
14596
14597 /* Value is number of columns displayed. */
14598 return it->hpos - hpos_at_start;
14599 }
14600
14601
14602 \f
14603 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
14604 appears as an element of LIST or as the car of an element of LIST.
14605 If PROPVAL is a list, compare each element against LIST in that
14606 way, and return 1/2 if any element of PROPVAL is found in LIST.
14607 Otherwise return 0. This function cannot quit.
14608 The return value is 2 if the text is invisible but with an ellipsis
14609 and 1 if it's invisible and without an ellipsis. */
14610
14611 int
14612 invisible_p (propval, list)
14613 register Lisp_Object propval;
14614 Lisp_Object list;
14615 {
14616 register Lisp_Object tail, proptail;
14617
14618 for (tail = list; CONSP (tail); tail = XCDR (tail))
14619 {
14620 register Lisp_Object tem;
14621 tem = XCAR (tail);
14622 if (EQ (propval, tem))
14623 return 1;
14624 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14625 return NILP (XCDR (tem)) ? 1 : 2;
14626 }
14627
14628 if (CONSP (propval))
14629 {
14630 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14631 {
14632 Lisp_Object propelt;
14633 propelt = XCAR (proptail);
14634 for (tail = list; CONSP (tail); tail = XCDR (tail))
14635 {
14636 register Lisp_Object tem;
14637 tem = XCAR (tail);
14638 if (EQ (propelt, tem))
14639 return 1;
14640 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14641 return NILP (XCDR (tem)) ? 1 : 2;
14642 }
14643 }
14644 }
14645
14646 return 0;
14647 }
14648
14649 \f
14650 /***********************************************************************
14651 Initialization
14652 ***********************************************************************/
14653
14654 void
14655 syms_of_xdisp ()
14656 {
14657 Vwith_echo_area_save_vector = Qnil;
14658 staticpro (&Vwith_echo_area_save_vector);
14659
14660 Vmessage_stack = Qnil;
14661 staticpro (&Vmessage_stack);
14662
14663 Qinhibit_redisplay = intern ("inhibit-redisplay");
14664 staticpro (&Qinhibit_redisplay);
14665
14666 message_dolog_marker1 = Fmake_marker ();
14667 staticpro (&message_dolog_marker1);
14668 message_dolog_marker2 = Fmake_marker ();
14669 staticpro (&message_dolog_marker2);
14670 message_dolog_marker3 = Fmake_marker ();
14671 staticpro (&message_dolog_marker3);
14672
14673 #if GLYPH_DEBUG
14674 defsubr (&Sdump_glyph_matrix);
14675 defsubr (&Sdump_glyph_row);
14676 defsubr (&Sdump_tool_bar_row);
14677 defsubr (&Strace_redisplay);
14678 defsubr (&Strace_to_stderr);
14679 #endif
14680 #ifdef HAVE_WINDOW_SYSTEM
14681 defsubr (&Stool_bar_lines_needed);
14682 #endif
14683
14684 staticpro (&Qmenu_bar_update_hook);
14685 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14686
14687 staticpro (&Qoverriding_terminal_local_map);
14688 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14689
14690 staticpro (&Qoverriding_local_map);
14691 Qoverriding_local_map = intern ("overriding-local-map");
14692
14693 staticpro (&Qwindow_scroll_functions);
14694 Qwindow_scroll_functions = intern ("window-scroll-functions");
14695
14696 staticpro (&Qredisplay_end_trigger_functions);
14697 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14698
14699 staticpro (&Qinhibit_point_motion_hooks);
14700 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14701
14702 QCdata = intern (":data");
14703 staticpro (&QCdata);
14704 Qdisplay = intern ("display");
14705 staticpro (&Qdisplay);
14706 Qspace_width = intern ("space-width");
14707 staticpro (&Qspace_width);
14708 Qraise = intern ("raise");
14709 staticpro (&Qraise);
14710 Qspace = intern ("space");
14711 staticpro (&Qspace);
14712 Qmargin = intern ("margin");
14713 staticpro (&Qmargin);
14714 Qleft_margin = intern ("left-margin");
14715 staticpro (&Qleft_margin);
14716 Qright_margin = intern ("right-margin");
14717 staticpro (&Qright_margin);
14718 Qalign_to = intern ("align-to");
14719 staticpro (&Qalign_to);
14720 QCalign_to = intern (":align-to");
14721 staticpro (&QCalign_to);
14722 Qrelative_width = intern ("relative-width");
14723 staticpro (&Qrelative_width);
14724 QCrelative_width = intern (":relative-width");
14725 staticpro (&QCrelative_width);
14726 QCrelative_height = intern (":relative-height");
14727 staticpro (&QCrelative_height);
14728 QCeval = intern (":eval");
14729 staticpro (&QCeval);
14730 Qwhen = intern ("when");
14731 staticpro (&Qwhen);
14732 QCfile = intern (":file");
14733 staticpro (&QCfile);
14734 Qfontified = intern ("fontified");
14735 staticpro (&Qfontified);
14736 Qfontification_functions = intern ("fontification-functions");
14737 staticpro (&Qfontification_functions);
14738 Qtrailing_whitespace = intern ("trailing-whitespace");
14739 staticpro (&Qtrailing_whitespace);
14740 Qimage = intern ("image");
14741 staticpro (&Qimage);
14742 Qmessage_truncate_lines = intern ("message-truncate-lines");
14743 staticpro (&Qmessage_truncate_lines);
14744 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
14745 staticpro (&Qcursor_in_non_selected_windows);
14746 Qgrow_only = intern ("grow-only");
14747 staticpro (&Qgrow_only);
14748 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14749 staticpro (&Qinhibit_menubar_update);
14750 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14751 staticpro (&Qinhibit_eval_during_redisplay);
14752 Qposition = intern ("position");
14753 staticpro (&Qposition);
14754 Qbuffer_position = intern ("buffer-position");
14755 staticpro (&Qbuffer_position);
14756 Qobject = intern ("object");
14757 staticpro (&Qobject);
14758
14759 last_arrow_position = Qnil;
14760 last_arrow_string = Qnil;
14761 staticpro (&last_arrow_position);
14762 staticpro (&last_arrow_string);
14763
14764 echo_buffer[0] = echo_buffer[1] = Qnil;
14765 staticpro (&echo_buffer[0]);
14766 staticpro (&echo_buffer[1]);
14767
14768 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14769 staticpro (&echo_area_buffer[0]);
14770 staticpro (&echo_area_buffer[1]);
14771
14772 Vmessages_buffer_name = build_string ("*Messages*");
14773 staticpro (&Vmessages_buffer_name);
14774
14775 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14776 doc: /* Non-nil means highlight trailing whitespace.
14777 The face used for trailing whitespace is `trailing-whitespace'. */);
14778 Vshow_trailing_whitespace = Qnil;
14779
14780 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14781 doc: /* Non-nil means don't actually do any redisplay.
14782 This is used for internal purposes. */);
14783 Vinhibit_redisplay = Qnil;
14784
14785 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14786 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
14787 Vglobal_mode_string = Qnil;
14788
14789 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14790 doc: /* Marker for where to display an arrow on top of the buffer text.
14791 This must be the beginning of a line in order to work.
14792 See also `overlay-arrow-string'. */);
14793 Voverlay_arrow_position = Qnil;
14794
14795 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14796 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
14797 Voverlay_arrow_string = Qnil;
14798
14799 DEFVAR_INT ("scroll-step", &scroll_step,
14800 doc: /* *The number of lines to try scrolling a window by when point moves out.
14801 If that fails to bring point back on frame, point is centered instead.
14802 If this is zero, point is always centered after it moves off frame.
14803 If you want scrolling to always be a line at a time, you should set
14804 `scroll-conservatively' to a large value rather than set this to 1. */);
14805
14806 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14807 doc: /* *Scroll up to this many lines, to bring point back on screen.
14808 A value of zero means to scroll the text to center point vertically
14809 in the window. */);
14810 scroll_conservatively = 0;
14811
14812 DEFVAR_INT ("scroll-margin", &scroll_margin,
14813 doc: /* *Number of lines of margin at the top and bottom of a window.
14814 Recenter the window whenever point gets within this many lines
14815 of the top or bottom of the window. */);
14816 scroll_margin = 0;
14817
14818 #if GLYPH_DEBUG
14819 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
14820 #endif
14821
14822 DEFVAR_BOOL ("truncate-partial-width-windows",
14823 &truncate_partial_width_windows,
14824 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
14825 truncate_partial_width_windows = 1;
14826
14827 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14828 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
14829 Any other value means to use the appropriate face, `mode-line',
14830 `header-line', or `menu' respectively.
14831
14832 This variable is deprecated; please change the above faces instead. */);
14833 mode_line_inverse_video = 1;
14834
14835 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14836 doc: /* *Maximum buffer size for which line number should be displayed.
14837 If the buffer is bigger than this, the line number does not appear
14838 in the mode line. A value of nil means no limit. */);
14839 Vline_number_display_limit = Qnil;
14840
14841 DEFVAR_INT ("line-number-display-limit-width",
14842 &line_number_display_limit_width,
14843 doc: /* *Maximum line width (in characters) for line number display.
14844 If the average length of the lines near point is bigger than this, then the
14845 line number may be omitted from the mode line. */);
14846 line_number_display_limit_width = 200;
14847
14848 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14849 doc: /* *Non-nil means highlight region even in nonselected windows. */);
14850 highlight_nonselected_windows = 0;
14851
14852 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14853 doc: /* Non-nil if more than one frame is visible on this display.
14854 Minibuffer-only frames don't count, but iconified frames do.
14855 This variable is not guaranteed to be accurate except while processing
14856 `frame-title-format' and `icon-title-format'. */);
14857
14858 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14859 doc: /* Template for displaying the title bar of visible frames.
14860 \(Assuming the window manager supports this feature.)
14861 This variable has the same structure as `mode-line-format' (which see),
14862 and is used only on frames for which no explicit name has been set
14863 \(see `modify-frame-parameters'). */);
14864 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
14865 doc: /* Template for displaying the title bar of an iconified frame.
14866 \(Assuming the window manager supports this feature.)
14867 This variable has the same structure as `mode-line-format' (which see),
14868 and is used only on frames for which no explicit name has been set
14869 \(see `modify-frame-parameters'). */);
14870 Vicon_title_format
14871 = Vframe_title_format
14872 = Fcons (intern ("multiple-frames"),
14873 Fcons (build_string ("%b"),
14874 Fcons (Fcons (empty_string,
14875 Fcons (intern ("invocation-name"),
14876 Fcons (build_string ("@"),
14877 Fcons (intern ("system-name"),
14878 Qnil)))),
14879 Qnil)));
14880
14881 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
14882 doc: /* Maximum number of lines to keep in the message log buffer.
14883 If nil, disable message logging. If t, log messages but don't truncate
14884 the buffer when it becomes large. */);
14885 Vmessage_log_max = make_number (50);
14886
14887 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
14888 doc: /* Functions called before redisplay, if window sizes have changed.
14889 The value should be a list of functions that take one argument.
14890 Just before redisplay, for each frame, if any of its windows have changed
14891 size since the last redisplay, or have been split or deleted,
14892 all the functions in the list are called, with the frame as argument. */);
14893 Vwindow_size_change_functions = Qnil;
14894
14895 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
14896 doc: /* List of Functions to call before redisplaying a window with scrolling.
14897 Each function is called with two arguments, the window
14898 and its new display-start position. Note that the value of `window-end'
14899 is not valid when these functions are called. */);
14900 Vwindow_scroll_functions = Qnil;
14901
14902 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
14903 doc: /* *Non-nil means automatically resize tool-bars.
14904 This increases a tool-bar's height if not all tool-bar items are visible.
14905 It decreases a tool-bar's height when it would display blank lines
14906 otherwise. */);
14907 auto_resize_tool_bars_p = 1;
14908
14909 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
14910 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
14911 auto_raise_tool_bar_buttons_p = 1;
14912
14913 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
14914 doc: /* *Margin around tool-bar buttons in pixels.
14915 If an integer, use that for both horizontal and vertical margins.
14916 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
14917 HORZ specifying the horizontal margin, and VERT specifying the
14918 vertical margin. */);
14919 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
14920
14921 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
14922 doc: /* *Relief thickness of tool-bar buttons. */);
14923 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
14924
14925 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
14926 doc: /* List of functions to call to fontify regions of text.
14927 Each function is called with one argument POS. Functions must
14928 fontify a region starting at POS in the current buffer, and give
14929 fontified regions the property `fontified'. */);
14930 Vfontification_functions = Qnil;
14931 Fmake_variable_buffer_local (Qfontification_functions);
14932
14933 DEFVAR_BOOL ("unibyte-display-via-language-environment",
14934 &unibyte_display_via_language_environment,
14935 doc: /* *Non-nil means display unibyte text according to language environment.
14936 Specifically this means that unibyte non-ASCII characters
14937 are displayed by converting them to the equivalent multibyte characters
14938 according to the current language environment. As a result, they are
14939 displayed according to the current fontset. */);
14940 unibyte_display_via_language_environment = 0;
14941
14942 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
14943 doc: /* *Maximum height for resizing mini-windows.
14944 If a float, it specifies a fraction of the mini-window frame's height.
14945 If an integer, it specifies a number of lines. */);
14946 Vmax_mini_window_height = make_float (0.25);
14947
14948 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
14949 doc: /* *How to resize mini-windows.
14950 A value of nil means don't automatically resize mini-windows.
14951 A value of t means resize them to fit the text displayed in them.
14952 A value of `grow-only', the default, means let mini-windows grow
14953 only, until their display becomes empty, at which point the windows
14954 go back to their normal size. */);
14955 Vresize_mini_windows = Qgrow_only;
14956
14957 DEFVAR_BOOL ("cursor-in-non-selected-windows",
14958 &cursor_in_non_selected_windows,
14959 doc: /* *Non-nil means display a hollow cursor in non-selected windows.
14960 nil means don't display a cursor there. */);
14961 cursor_in_non_selected_windows = 1;
14962
14963 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
14964 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
14965 automatic_hscrolling_p = 1;
14966
14967 DEFVAR_LISP ("image-types", &Vimage_types,
14968 doc: /* List of supported image types.
14969 Each element of the list is a symbol for a supported image type. */);
14970 Vimage_types = Qnil;
14971
14972 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
14973 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
14974 Bind this around calls to `message' to let it take effect. */);
14975 message_truncate_lines = 0;
14976
14977 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
14978 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
14979 Can be used to update submenus whose contents should vary. */);
14980 Vmenu_bar_update_hook = Qnil;
14981
14982 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
14983 doc: /* Non-nil means don't update menu bars. Internal use only. */);
14984 inhibit_menubar_update = 0;
14985
14986 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
14987 doc: /* Non-nil means don't eval Lisp during redisplay. */);
14988 inhibit_eval_during_redisplay = 0;
14989
14990 #if GLYPH_DEBUG
14991 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
14992 doc: /* Inhibit try_window_id display optimization. */);
14993 inhibit_try_window_id = 0;
14994
14995 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
14996 doc: /* Inhibit try_window_reusing display optimization. */);
14997 inhibit_try_window_reusing = 0;
14998
14999 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
15000 doc: /* Inhibit try_cursor_movement display optimization. */);
15001 inhibit_try_cursor_movement = 0;
15002 #endif /* GLYPH_DEBUG */
15003 }
15004
15005
15006 /* Initialize this module when Emacs starts. */
15007
15008 void
15009 init_xdisp ()
15010 {
15011 Lisp_Object root_window;
15012 struct window *mini_w;
15013
15014 current_header_line_height = current_mode_line_height = -1;
15015
15016 CHARPOS (this_line_start_pos) = 0;
15017
15018 mini_w = XWINDOW (minibuf_window);
15019 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
15020
15021 if (!noninteractive)
15022 {
15023 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
15024 int i;
15025
15026 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
15027 set_window_height (root_window,
15028 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
15029 0);
15030 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
15031 set_window_height (minibuf_window, 1, 0);
15032
15033 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
15034 mini_w->width = make_number (FRAME_WIDTH (f));
15035
15036 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
15037 scratch_glyph_row.glyphs[TEXT_AREA + 1]
15038 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
15039
15040 /* The default ellipsis glyphs `...'. */
15041 for (i = 0; i < 3; ++i)
15042 default_invis_vector[i] = make_number ('.');
15043 }
15044
15045 #ifdef HAVE_WINDOW_SYSTEM
15046 {
15047 /* Allocate the buffer for frame titles. */
15048 int size = 100;
15049 frame_title_buf = (char *) xmalloc (size);
15050 frame_title_buf_end = frame_title_buf + size;
15051 frame_title_ptr = NULL;
15052 }
15053 #endif /* HAVE_WINDOW_SYSTEM */
15054
15055 help_echo_showing_p = 0;
15056 }
15057
15058