]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(try_scrolling): Exchange uses of scroll_down_aggressively
[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, MODE_LINE_FACE_ID);
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, MODE_LINE_FACE_ID,
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 or
1460 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1461 displaying the tool-bar.
1462
1463 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1464 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1465 corresponding mode line glyph row of the desired matrix of W. */
1466
1467 void
1468 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1469 struct it *it;
1470 struct window *w;
1471 int charpos, bytepos;
1472 struct glyph_row *row;
1473 enum face_id base_face_id;
1474 {
1475 int highlight_region_p;
1476
1477 /* Some precondition checks. */
1478 xassert (w != NULL && it != NULL);
1479 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1480 && charpos <= ZV));
1481
1482 /* If face attributes have been changed since the last redisplay,
1483 free realized faces now because they depend on face definitions
1484 that might have changed. */
1485 if (face_change_count)
1486 {
1487 face_change_count = 0;
1488 free_all_realized_faces (Qnil);
1489 }
1490
1491 /* Use one of the mode line rows of W's desired matrix if
1492 appropriate. */
1493 if (row == NULL)
1494 {
1495 if (base_face_id == MODE_LINE_FACE_ID)
1496 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1497 else if (base_face_id == HEADER_LINE_FACE_ID)
1498 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1499 }
1500
1501 /* Clear IT. */
1502 bzero (it, sizeof *it);
1503 it->current.overlay_string_index = -1;
1504 it->current.dpvec_index = -1;
1505 it->base_face_id = base_face_id;
1506
1507 /* The window in which we iterate over current_buffer: */
1508 XSETWINDOW (it->window, w);
1509 it->w = w;
1510 it->f = XFRAME (w->frame);
1511
1512 /* Extra space between lines (on window systems only). */
1513 if (base_face_id == DEFAULT_FACE_ID
1514 && FRAME_WINDOW_P (it->f))
1515 {
1516 if (NATNUMP (current_buffer->extra_line_spacing))
1517 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1518 else if (it->f->extra_line_spacing > 0)
1519 it->extra_line_spacing = it->f->extra_line_spacing;
1520 }
1521
1522 /* If realized faces have been removed, e.g. because of face
1523 attribute changes of named faces, recompute them. When running
1524 in batch mode, the face cache of Vterminal_frame is null. If
1525 we happen to get called, make a dummy face cache. */
1526 if (
1527 #ifndef WINDOWSNT
1528 noninteractive &&
1529 #endif
1530 FRAME_FACE_CACHE (it->f) == NULL)
1531 init_frame_faces (it->f);
1532 if (FRAME_FACE_CACHE (it->f)->used == 0)
1533 recompute_basic_faces (it->f);
1534
1535 /* Current value of the `space-width', and 'height' properties. */
1536 it->space_width = Qnil;
1537 it->font_height = Qnil;
1538
1539 /* Are control characters displayed as `^C'? */
1540 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1541
1542 /* -1 means everything between a CR and the following line end
1543 is invisible. >0 means lines indented more than this value are
1544 invisible. */
1545 it->selective = (INTEGERP (current_buffer->selective_display)
1546 ? XFASTINT (current_buffer->selective_display)
1547 : (!NILP (current_buffer->selective_display)
1548 ? -1 : 0));
1549 it->selective_display_ellipsis_p
1550 = !NILP (current_buffer->selective_display_ellipses);
1551
1552 /* Display table to use. */
1553 it->dp = window_display_table (w);
1554
1555 /* Are multibyte characters enabled in current_buffer? */
1556 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1557
1558 /* Non-zero if we should highlight the region. */
1559 highlight_region_p
1560 = (!NILP (Vtransient_mark_mode)
1561 && !NILP (current_buffer->mark_active)
1562 && XMARKER (current_buffer->mark)->buffer != 0);
1563
1564 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1565 start and end of a visible region in window IT->w. Set both to
1566 -1 to indicate no region. */
1567 if (highlight_region_p
1568 /* Maybe highlight only in selected window. */
1569 && (/* Either show region everywhere. */
1570 highlight_nonselected_windows
1571 /* Or show region in the selected window. */
1572 || w == XWINDOW (selected_window)
1573 /* Or show the region if we are in the mini-buffer and W is
1574 the window the mini-buffer refers to. */
1575 || (MINI_WINDOW_P (XWINDOW (selected_window))
1576 && WINDOWP (Vminibuf_scroll_window)
1577 && w == XWINDOW (Vminibuf_scroll_window))))
1578 {
1579 int charpos = marker_position (current_buffer->mark);
1580 it->region_beg_charpos = min (PT, charpos);
1581 it->region_end_charpos = max (PT, charpos);
1582 }
1583 else
1584 it->region_beg_charpos = it->region_end_charpos = -1;
1585
1586 /* Get the position at which the redisplay_end_trigger hook should
1587 be run, if it is to be run at all. */
1588 if (MARKERP (w->redisplay_end_trigger)
1589 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1590 it->redisplay_end_trigger_charpos
1591 = marker_position (w->redisplay_end_trigger);
1592 else if (INTEGERP (w->redisplay_end_trigger))
1593 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1594
1595 /* Correct bogus values of tab_width. */
1596 it->tab_width = XINT (current_buffer->tab_width);
1597 if (it->tab_width <= 0 || it->tab_width > 1000)
1598 it->tab_width = 8;
1599
1600 /* Are lines in the display truncated? */
1601 it->truncate_lines_p
1602 = (base_face_id != DEFAULT_FACE_ID
1603 || XINT (it->w->hscroll)
1604 || (truncate_partial_width_windows
1605 && !WINDOW_FULL_WIDTH_P (it->w))
1606 || !NILP (current_buffer->truncate_lines));
1607
1608 /* Get dimensions of truncation and continuation glyphs. These are
1609 displayed as fringe bitmaps under X, so we don't need them for such
1610 frames. */
1611 if (!FRAME_WINDOW_P (it->f))
1612 {
1613 if (it->truncate_lines_p)
1614 {
1615 /* We will need the truncation glyph. */
1616 xassert (it->glyph_row == NULL);
1617 produce_special_glyphs (it, IT_TRUNCATION);
1618 it->truncation_pixel_width = it->pixel_width;
1619 }
1620 else
1621 {
1622 /* We will need the continuation glyph. */
1623 xassert (it->glyph_row == NULL);
1624 produce_special_glyphs (it, IT_CONTINUATION);
1625 it->continuation_pixel_width = it->pixel_width;
1626 }
1627
1628 /* Reset these values to zero because the produce_special_glyphs
1629 above has changed them. */
1630 it->pixel_width = it->ascent = it->descent = 0;
1631 it->phys_ascent = it->phys_descent = 0;
1632 }
1633
1634 /* Set this after getting the dimensions of truncation and
1635 continuation glyphs, so that we don't produce glyphs when calling
1636 produce_special_glyphs, above. */
1637 it->glyph_row = row;
1638 it->area = TEXT_AREA;
1639
1640 /* Get the dimensions of the display area. The display area
1641 consists of the visible window area plus a horizontally scrolled
1642 part to the left of the window. All x-values are relative to the
1643 start of this total display area. */
1644 if (base_face_id != DEFAULT_FACE_ID)
1645 {
1646 /* Mode lines, menu bar in terminal frames. */
1647 it->first_visible_x = 0;
1648 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1649 }
1650 else
1651 {
1652 it->first_visible_x
1653 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1654 it->last_visible_x = (it->first_visible_x
1655 + window_box_width (w, TEXT_AREA));
1656
1657 /* If we truncate lines, leave room for the truncator glyph(s) at
1658 the right margin. Otherwise, leave room for the continuation
1659 glyph(s). Truncation and continuation glyphs are not inserted
1660 for window-based redisplay. */
1661 if (!FRAME_WINDOW_P (it->f))
1662 {
1663 if (it->truncate_lines_p)
1664 it->last_visible_x -= it->truncation_pixel_width;
1665 else
1666 it->last_visible_x -= it->continuation_pixel_width;
1667 }
1668
1669 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1670 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1671 }
1672
1673 /* Leave room for a border glyph. */
1674 if (!FRAME_WINDOW_P (it->f)
1675 && !WINDOW_RIGHTMOST_P (it->w))
1676 it->last_visible_x -= 1;
1677
1678 it->last_visible_y = window_text_bottom_y (w);
1679
1680 /* For mode lines and alike, arrange for the first glyph having a
1681 left box line if the face specifies a box. */
1682 if (base_face_id != DEFAULT_FACE_ID)
1683 {
1684 struct face *face;
1685
1686 it->face_id = base_face_id;
1687
1688 /* If we have a boxed mode line, make the first character appear
1689 with a left box line. */
1690 face = FACE_FROM_ID (it->f, base_face_id);
1691 if (face->box != FACE_NO_BOX)
1692 it->start_of_box_run_p = 1;
1693 }
1694
1695 /* If a buffer position was specified, set the iterator there,
1696 getting overlays and face properties from that position. */
1697 if (charpos >= BUF_BEG (current_buffer))
1698 {
1699 it->end_charpos = ZV;
1700 it->face_id = -1;
1701 IT_CHARPOS (*it) = charpos;
1702
1703 /* Compute byte position if not specified. */
1704 if (bytepos < charpos)
1705 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1706 else
1707 IT_BYTEPOS (*it) = bytepos;
1708
1709 /* Compute faces etc. */
1710 reseat (it, it->current.pos, 1);
1711 }
1712
1713 CHECK_IT (it);
1714 }
1715
1716
1717 /* Initialize IT for the display of window W with window start POS. */
1718
1719 void
1720 start_display (it, w, pos)
1721 struct it *it;
1722 struct window *w;
1723 struct text_pos pos;
1724 {
1725 struct glyph_row *row;
1726 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1727
1728 row = w->desired_matrix->rows + first_vpos;
1729 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1730
1731 if (!it->truncate_lines_p)
1732 {
1733 int start_at_line_beg_p;
1734 int first_y = it->current_y;
1735
1736 /* If window start is not at a line start, skip forward to POS to
1737 get the correct continuation lines width. */
1738 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1739 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1740 if (!start_at_line_beg_p)
1741 {
1742 reseat_at_previous_visible_line_start (it);
1743 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1744
1745 /* If lines are continued, this line may end in the middle
1746 of a multi-glyph character (e.g. a control character
1747 displayed as \003, or in the middle of an overlay
1748 string). In this case move_it_to above will not have
1749 taken us to the start of the continuation line but to the
1750 end of the continued line. */
1751 if (it->current_x > 0)
1752 {
1753 if (it->current.dpvec_index >= 0
1754 || it->current.overlay_string_index >= 0)
1755 {
1756 set_iterator_to_next (it, 1);
1757 move_it_in_display_line_to (it, -1, -1, 0);
1758 }
1759
1760 it->continuation_lines_width += it->current_x;
1761 }
1762
1763 /* We're starting a new display line, not affected by the
1764 height of the continued line, so clear the appropriate
1765 fields in the iterator structure. */
1766 it->max_ascent = it->max_descent = 0;
1767 it->max_phys_ascent = it->max_phys_descent = 0;
1768
1769 it->current_y = first_y;
1770 it->vpos = 0;
1771 it->current_x = it->hpos = 0;
1772 }
1773 }
1774
1775 #if 0 /* Don't assert the following because start_display is sometimes
1776 called intentionally with a window start that is not at a
1777 line start. Please leave this code in as a comment. */
1778
1779 /* Window start should be on a line start, now. */
1780 xassert (it->continuation_lines_width
1781 || IT_CHARPOS (it) == BEGV
1782 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1783 #endif /* 0 */
1784 }
1785
1786
1787 /* Return 1 if POS is a position in ellipses displayed for invisible
1788 text. W is the window we display, for text property lookup. */
1789
1790 static int
1791 in_ellipses_for_invisible_text_p (pos, w)
1792 struct display_pos *pos;
1793 struct window *w;
1794 {
1795 Lisp_Object prop, window;
1796 int ellipses_p = 0;
1797 int charpos = CHARPOS (pos->pos);
1798
1799 /* If POS specifies a position in a display vector, this might
1800 be for an ellipsis displayed for invisible text. We won't
1801 get the iterator set up for delivering that ellipsis unless
1802 we make sure that it gets aware of the invisible text. */
1803 if (pos->dpvec_index >= 0
1804 && pos->overlay_string_index < 0
1805 && CHARPOS (pos->string_pos) < 0
1806 && charpos > BEGV
1807 && (XSETWINDOW (window, w),
1808 prop = Fget_char_property (make_number (charpos),
1809 Qinvisible, window),
1810 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1811 {
1812 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1813 window);
1814 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
1815 }
1816
1817 return ellipses_p;
1818 }
1819
1820
1821 /* Initialize IT for stepping through current_buffer in window W,
1822 starting at position POS that includes overlay string and display
1823 vector/ control character translation position information. Value
1824 is zero if there are overlay strings with newlines at POS. */
1825
1826 static int
1827 init_from_display_pos (it, w, pos)
1828 struct it *it;
1829 struct window *w;
1830 struct display_pos *pos;
1831 {
1832 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1833 int i, overlay_strings_with_newlines = 0;
1834
1835 /* If POS specifies a position in a display vector, this might
1836 be for an ellipsis displayed for invisible text. We won't
1837 get the iterator set up for delivering that ellipsis unless
1838 we make sure that it gets aware of the invisible text. */
1839 if (in_ellipses_for_invisible_text_p (pos, w))
1840 {
1841 --charpos;
1842 bytepos = 0;
1843 }
1844
1845 /* Keep in mind: the call to reseat in init_iterator skips invisible
1846 text, so we might end up at a position different from POS. This
1847 is only a problem when POS is a row start after a newline and an
1848 overlay starts there with an after-string, and the overlay has an
1849 invisible property. Since we don't skip invisible text in
1850 display_line and elsewhere immediately after consuming the
1851 newline before the row start, such a POS will not be in a string,
1852 but the call to init_iterator below will move us to the
1853 after-string. */
1854 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1855
1856 for (i = 0; i < it->n_overlay_strings; ++i)
1857 {
1858 char *s = XSTRING (it->overlay_strings[i])->data;
1859 char *e = s + STRING_BYTES (XSTRING (it->overlay_strings[i]));
1860
1861 while (s < e && *s != '\n')
1862 ++s;
1863
1864 if (s < e)
1865 {
1866 overlay_strings_with_newlines = 1;
1867 break;
1868 }
1869 }
1870
1871 /* If position is within an overlay string, set up IT to the right
1872 overlay string. */
1873 if (pos->overlay_string_index >= 0)
1874 {
1875 int relative_index;
1876
1877 /* If the first overlay string happens to have a `display'
1878 property for an image, the iterator will be set up for that
1879 image, and we have to undo that setup first before we can
1880 correct the overlay string index. */
1881 if (it->method == next_element_from_image)
1882 pop_it (it);
1883
1884 /* We already have the first chunk of overlay strings in
1885 IT->overlay_strings. Load more until the one for
1886 pos->overlay_string_index is in IT->overlay_strings. */
1887 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1888 {
1889 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1890 it->current.overlay_string_index = 0;
1891 while (n--)
1892 {
1893 load_overlay_strings (it, 0);
1894 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1895 }
1896 }
1897
1898 it->current.overlay_string_index = pos->overlay_string_index;
1899 relative_index = (it->current.overlay_string_index
1900 % OVERLAY_STRING_CHUNK_SIZE);
1901 it->string = it->overlay_strings[relative_index];
1902 xassert (STRINGP (it->string));
1903 it->current.string_pos = pos->string_pos;
1904 it->method = next_element_from_string;
1905 }
1906
1907 #if 0 /* This is bogus because POS not having an overlay string
1908 position does not mean it's after the string. Example: A
1909 line starting with a before-string and initialization of IT
1910 to the previous row's end position. */
1911 else if (it->current.overlay_string_index >= 0)
1912 {
1913 /* If POS says we're already after an overlay string ending at
1914 POS, make sure to pop the iterator because it will be in
1915 front of that overlay string. When POS is ZV, we've thereby
1916 also ``processed'' overlay strings at ZV. */
1917 while (it->sp)
1918 pop_it (it);
1919 it->current.overlay_string_index = -1;
1920 it->method = next_element_from_buffer;
1921 if (CHARPOS (pos->pos) == ZV)
1922 it->overlay_strings_at_end_processed_p = 1;
1923 }
1924 #endif /* 0 */
1925
1926 if (CHARPOS (pos->string_pos) >= 0)
1927 {
1928 /* Recorded position is not in an overlay string, but in another
1929 string. This can only be a string from a `display' property.
1930 IT should already be filled with that string. */
1931 it->current.string_pos = pos->string_pos;
1932 xassert (STRINGP (it->string));
1933 }
1934
1935 /* Restore position in display vector translations, control
1936 character translations or ellipses. */
1937 if (pos->dpvec_index >= 0)
1938 {
1939 if (it->dpvec == NULL)
1940 get_next_display_element (it);
1941 xassert (it->dpvec && it->current.dpvec_index == 0);
1942 it->current.dpvec_index = pos->dpvec_index;
1943 }
1944
1945 CHECK_IT (it);
1946 return !overlay_strings_with_newlines;
1947 }
1948
1949
1950 /* Initialize IT for stepping through current_buffer in window W
1951 starting at ROW->start. */
1952
1953 static void
1954 init_to_row_start (it, w, row)
1955 struct it *it;
1956 struct window *w;
1957 struct glyph_row *row;
1958 {
1959 init_from_display_pos (it, w, &row->start);
1960 it->continuation_lines_width = row->continuation_lines_width;
1961 CHECK_IT (it);
1962 }
1963
1964
1965 /* Initialize IT for stepping through current_buffer in window W
1966 starting in the line following ROW, i.e. starting at ROW->end.
1967 Value is zero if there are overlay strings with newlines at ROW's
1968 end position. */
1969
1970 static int
1971 init_to_row_end (it, w, row)
1972 struct it *it;
1973 struct window *w;
1974 struct glyph_row *row;
1975 {
1976 int success = 0;
1977
1978 if (init_from_display_pos (it, w, &row->end))
1979 {
1980 if (row->continued_p)
1981 it->continuation_lines_width
1982 = row->continuation_lines_width + row->pixel_width;
1983 CHECK_IT (it);
1984 success = 1;
1985 }
1986
1987 return success;
1988 }
1989
1990
1991
1992 \f
1993 /***********************************************************************
1994 Text properties
1995 ***********************************************************************/
1996
1997 /* Called when IT reaches IT->stop_charpos. Handle text property and
1998 overlay changes. Set IT->stop_charpos to the next position where
1999 to stop. */
2000
2001 static void
2002 handle_stop (it)
2003 struct it *it;
2004 {
2005 enum prop_handled handled;
2006 int handle_overlay_change_p = 1;
2007 struct props *p;
2008
2009 it->dpvec = NULL;
2010 it->current.dpvec_index = -1;
2011
2012 do
2013 {
2014 handled = HANDLED_NORMALLY;
2015
2016 /* Call text property handlers. */
2017 for (p = it_props; p->handler; ++p)
2018 {
2019 handled = p->handler (it);
2020
2021 if (handled == HANDLED_RECOMPUTE_PROPS)
2022 break;
2023 else if (handled == HANDLED_RETURN)
2024 return;
2025 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2026 handle_overlay_change_p = 0;
2027 }
2028
2029 if (handled != HANDLED_RECOMPUTE_PROPS)
2030 {
2031 /* Don't check for overlay strings below when set to deliver
2032 characters from a display vector. */
2033 if (it->method == next_element_from_display_vector)
2034 handle_overlay_change_p = 0;
2035
2036 /* Handle overlay changes. */
2037 if (handle_overlay_change_p)
2038 handled = handle_overlay_change (it);
2039
2040 /* Determine where to stop next. */
2041 if (handled == HANDLED_NORMALLY)
2042 compute_stop_pos (it);
2043 }
2044 }
2045 while (handled == HANDLED_RECOMPUTE_PROPS);
2046 }
2047
2048
2049 /* Compute IT->stop_charpos from text property and overlay change
2050 information for IT's current position. */
2051
2052 static void
2053 compute_stop_pos (it)
2054 struct it *it;
2055 {
2056 register INTERVAL iv, next_iv;
2057 Lisp_Object object, limit, position;
2058
2059 /* If nowhere else, stop at the end. */
2060 it->stop_charpos = it->end_charpos;
2061
2062 if (STRINGP (it->string))
2063 {
2064 /* Strings are usually short, so don't limit the search for
2065 properties. */
2066 object = it->string;
2067 limit = Qnil;
2068 position = make_number (IT_STRING_CHARPOS (*it));
2069 }
2070 else
2071 {
2072 int charpos;
2073
2074 /* If next overlay change is in front of the current stop pos
2075 (which is IT->end_charpos), stop there. Note: value of
2076 next_overlay_change is point-max if no overlay change
2077 follows. */
2078 charpos = next_overlay_change (IT_CHARPOS (*it));
2079 if (charpos < it->stop_charpos)
2080 it->stop_charpos = charpos;
2081
2082 /* If showing the region, we have to stop at the region
2083 start or end because the face might change there. */
2084 if (it->region_beg_charpos > 0)
2085 {
2086 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2087 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2088 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2089 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2090 }
2091
2092 /* Set up variables for computing the stop position from text
2093 property changes. */
2094 XSETBUFFER (object, current_buffer);
2095 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2096 position = make_number (IT_CHARPOS (*it));
2097
2098 }
2099
2100 /* Get the interval containing IT's position. Value is a null
2101 interval if there isn't such an interval. */
2102 iv = validate_interval_range (object, &position, &position, 0);
2103 if (!NULL_INTERVAL_P (iv))
2104 {
2105 Lisp_Object values_here[LAST_PROP_IDX];
2106 struct props *p;
2107
2108 /* Get properties here. */
2109 for (p = it_props; p->handler; ++p)
2110 values_here[p->idx] = textget (iv->plist, *p->name);
2111
2112 /* Look for an interval following iv that has different
2113 properties. */
2114 for (next_iv = next_interval (iv);
2115 (!NULL_INTERVAL_P (next_iv)
2116 && (NILP (limit)
2117 || XFASTINT (limit) > next_iv->position));
2118 next_iv = next_interval (next_iv))
2119 {
2120 for (p = it_props; p->handler; ++p)
2121 {
2122 Lisp_Object new_value;
2123
2124 new_value = textget (next_iv->plist, *p->name);
2125 if (!EQ (values_here[p->idx], new_value))
2126 break;
2127 }
2128
2129 if (p->handler)
2130 break;
2131 }
2132
2133 if (!NULL_INTERVAL_P (next_iv))
2134 {
2135 if (INTEGERP (limit)
2136 && next_iv->position >= XFASTINT (limit))
2137 /* No text property change up to limit. */
2138 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2139 else
2140 /* Text properties change in next_iv. */
2141 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2142 }
2143 }
2144
2145 xassert (STRINGP (it->string)
2146 || (it->stop_charpos >= BEGV
2147 && it->stop_charpos >= IT_CHARPOS (*it)));
2148 }
2149
2150
2151 /* Return the position of the next overlay change after POS in
2152 current_buffer. Value is point-max if no overlay change
2153 follows. This is like `next-overlay-change' but doesn't use
2154 xmalloc. */
2155
2156 static int
2157 next_overlay_change (pos)
2158 int pos;
2159 {
2160 int noverlays;
2161 int endpos;
2162 Lisp_Object *overlays;
2163 int len;
2164 int i;
2165
2166 /* Get all overlays at the given position. */
2167 len = 10;
2168 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2169 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2170 if (noverlays > len)
2171 {
2172 len = noverlays;
2173 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2174 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2175 }
2176
2177 /* If any of these overlays ends before endpos,
2178 use its ending point instead. */
2179 for (i = 0; i < noverlays; ++i)
2180 {
2181 Lisp_Object oend;
2182 int oendpos;
2183
2184 oend = OVERLAY_END (overlays[i]);
2185 oendpos = OVERLAY_POSITION (oend);
2186 endpos = min (endpos, oendpos);
2187 }
2188
2189 return endpos;
2190 }
2191
2192
2193 \f
2194 /***********************************************************************
2195 Fontification
2196 ***********************************************************************/
2197
2198 /* Handle changes in the `fontified' property of the current buffer by
2199 calling hook functions from Qfontification_functions to fontify
2200 regions of text. */
2201
2202 static enum prop_handled
2203 handle_fontified_prop (it)
2204 struct it *it;
2205 {
2206 Lisp_Object prop, pos;
2207 enum prop_handled handled = HANDLED_NORMALLY;
2208
2209 /* Get the value of the `fontified' property at IT's current buffer
2210 position. (The `fontified' property doesn't have a special
2211 meaning in strings.) If the value is nil, call functions from
2212 Qfontification_functions. */
2213 if (!STRINGP (it->string)
2214 && it->s == NULL
2215 && !NILP (Vfontification_functions)
2216 && !NILP (Vrun_hooks)
2217 && (pos = make_number (IT_CHARPOS (*it)),
2218 prop = Fget_char_property (pos, Qfontified, Qnil),
2219 NILP (prop)))
2220 {
2221 int count = BINDING_STACK_SIZE ();
2222 Lisp_Object val;
2223
2224 val = Vfontification_functions;
2225 specbind (Qfontification_functions, Qnil);
2226
2227 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2228 safe_call1 (val, pos);
2229 else
2230 {
2231 Lisp_Object globals, fn;
2232 struct gcpro gcpro1, gcpro2;
2233
2234 globals = Qnil;
2235 GCPRO2 (val, globals);
2236
2237 for (; CONSP (val); val = XCDR (val))
2238 {
2239 fn = XCAR (val);
2240
2241 if (EQ (fn, Qt))
2242 {
2243 /* A value of t indicates this hook has a local
2244 binding; it means to run the global binding too.
2245 In a global value, t should not occur. If it
2246 does, we must ignore it to avoid an endless
2247 loop. */
2248 for (globals = Fdefault_value (Qfontification_functions);
2249 CONSP (globals);
2250 globals = XCDR (globals))
2251 {
2252 fn = XCAR (globals);
2253 if (!EQ (fn, Qt))
2254 safe_call1 (fn, pos);
2255 }
2256 }
2257 else
2258 safe_call1 (fn, pos);
2259 }
2260
2261 UNGCPRO;
2262 }
2263
2264 unbind_to (count, Qnil);
2265
2266 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2267 something. This avoids an endless loop if they failed to
2268 fontify the text for which reason ever. */
2269 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2270 handled = HANDLED_RECOMPUTE_PROPS;
2271 }
2272
2273 return handled;
2274 }
2275
2276
2277 \f
2278 /***********************************************************************
2279 Faces
2280 ***********************************************************************/
2281
2282 /* Set up iterator IT from face properties at its current position.
2283 Called from handle_stop. */
2284
2285 static enum prop_handled
2286 handle_face_prop (it)
2287 struct it *it;
2288 {
2289 int new_face_id, next_stop;
2290
2291 if (!STRINGP (it->string))
2292 {
2293 new_face_id
2294 = face_at_buffer_position (it->w,
2295 IT_CHARPOS (*it),
2296 it->region_beg_charpos,
2297 it->region_end_charpos,
2298 &next_stop,
2299 (IT_CHARPOS (*it)
2300 + TEXT_PROP_DISTANCE_LIMIT),
2301 0);
2302
2303 /* Is this a start of a run of characters with box face?
2304 Caveat: this can be called for a freshly initialized
2305 iterator; face_id is -1 in this case. We know that the new
2306 face will not change until limit, i.e. if the new face has a
2307 box, all characters up to limit will have one. But, as
2308 usual, we don't know whether limit is really the end. */
2309 if (new_face_id != it->face_id)
2310 {
2311 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2312
2313 /* If new face has a box but old face has not, this is
2314 the start of a run of characters with box, i.e. it has
2315 a shadow on the left side. The value of face_id of the
2316 iterator will be -1 if this is the initial call that gets
2317 the face. In this case, we have to look in front of IT's
2318 position and see whether there is a face != new_face_id. */
2319 it->start_of_box_run_p
2320 = (new_face->box != FACE_NO_BOX
2321 && (it->face_id >= 0
2322 || IT_CHARPOS (*it) == BEG
2323 || new_face_id != face_before_it_pos (it)));
2324 it->face_box_p = new_face->box != FACE_NO_BOX;
2325 }
2326 }
2327 else
2328 {
2329 int base_face_id, bufpos;
2330
2331 if (it->current.overlay_string_index >= 0)
2332 bufpos = IT_CHARPOS (*it);
2333 else
2334 bufpos = 0;
2335
2336 /* For strings from a buffer, i.e. overlay strings or strings
2337 from a `display' property, use the face at IT's current
2338 buffer position as the base face to merge with, so that
2339 overlay strings appear in the same face as surrounding
2340 text, unless they specify their own faces. */
2341 base_face_id = underlying_face_id (it);
2342
2343 new_face_id = face_at_string_position (it->w,
2344 it->string,
2345 IT_STRING_CHARPOS (*it),
2346 bufpos,
2347 it->region_beg_charpos,
2348 it->region_end_charpos,
2349 &next_stop,
2350 base_face_id, 0);
2351
2352 #if 0 /* This shouldn't be neccessary. Let's check it. */
2353 /* If IT is used to display a mode line we would really like to
2354 use the mode line face instead of the frame's default face. */
2355 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2356 && new_face_id == DEFAULT_FACE_ID)
2357 new_face_id = MODE_LINE_FACE_ID;
2358 #endif
2359
2360 /* Is this a start of a run of characters with box? Caveat:
2361 this can be called for a freshly allocated iterator; face_id
2362 is -1 is this case. We know that the new face will not
2363 change until the next check pos, i.e. if the new face has a
2364 box, all characters up to that position will have a
2365 box. But, as usual, we don't know whether that position
2366 is really the end. */
2367 if (new_face_id != it->face_id)
2368 {
2369 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2370 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2371
2372 /* If new face has a box but old face hasn't, this is the
2373 start of a run of characters with box, i.e. it has a
2374 shadow on the left side. */
2375 it->start_of_box_run_p
2376 = new_face->box && (old_face == NULL || !old_face->box);
2377 it->face_box_p = new_face->box != FACE_NO_BOX;
2378 }
2379 }
2380
2381 it->face_id = new_face_id;
2382 return HANDLED_NORMALLY;
2383 }
2384
2385
2386 /* Return the ID of the face ``underlying'' IT's current position,
2387 which is in a string. If the iterator is associated with a
2388 buffer, return the face at IT's current buffer position.
2389 Otherwise, use the iterator's base_face_id. */
2390
2391 static int
2392 underlying_face_id (it)
2393 struct it *it;
2394 {
2395 int face_id = it->base_face_id, i;
2396
2397 xassert (STRINGP (it->string));
2398
2399 for (i = it->sp - 1; i >= 0; --i)
2400 if (NILP (it->stack[i].string))
2401 face_id = it->stack[i].face_id;
2402
2403 return face_id;
2404 }
2405
2406
2407 /* Compute the face one character before or after the current position
2408 of IT. BEFORE_P non-zero means get the face in front of IT's
2409 position. Value is the id of the face. */
2410
2411 static int
2412 face_before_or_after_it_pos (it, before_p)
2413 struct it *it;
2414 int before_p;
2415 {
2416 int face_id, limit;
2417 int next_check_charpos;
2418 struct text_pos pos;
2419
2420 xassert (it->s == NULL);
2421
2422 if (STRINGP (it->string))
2423 {
2424 int bufpos, base_face_id;
2425
2426 /* No face change past the end of the string (for the case
2427 we are padding with spaces). No face change before the
2428 string start. */
2429 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2430 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2431 return it->face_id;
2432
2433 /* Set pos to the position before or after IT's current position. */
2434 if (before_p)
2435 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2436 else
2437 /* For composition, we must check the character after the
2438 composition. */
2439 pos = (it->what == IT_COMPOSITION
2440 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2441 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2442
2443 if (it->current.overlay_string_index >= 0)
2444 bufpos = IT_CHARPOS (*it);
2445 else
2446 bufpos = 0;
2447
2448 base_face_id = underlying_face_id (it);
2449
2450 /* Get the face for ASCII, or unibyte. */
2451 face_id = face_at_string_position (it->w,
2452 it->string,
2453 CHARPOS (pos),
2454 bufpos,
2455 it->region_beg_charpos,
2456 it->region_end_charpos,
2457 &next_check_charpos,
2458 base_face_id, 0);
2459
2460 /* Correct the face for charsets different from ASCII. Do it
2461 for the multibyte case only. The face returned above is
2462 suitable for unibyte text if IT->string is unibyte. */
2463 if (STRING_MULTIBYTE (it->string))
2464 {
2465 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2466 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2467 int c, len;
2468 struct face *face = FACE_FROM_ID (it->f, face_id);
2469
2470 c = string_char_and_length (p, rest, &len);
2471 face_id = FACE_FOR_CHAR (it->f, face, c);
2472 }
2473 }
2474 else
2475 {
2476 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2477 || (IT_CHARPOS (*it) <= BEGV && before_p))
2478 return it->face_id;
2479
2480 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2481 pos = it->current.pos;
2482
2483 if (before_p)
2484 DEC_TEXT_POS (pos, it->multibyte_p);
2485 else
2486 {
2487 if (it->what == IT_COMPOSITION)
2488 /* For composition, we must check the position after the
2489 composition. */
2490 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2491 else
2492 INC_TEXT_POS (pos, it->multibyte_p);
2493 }
2494
2495 /* Determine face for CHARSET_ASCII, or unibyte. */
2496 face_id = face_at_buffer_position (it->w,
2497 CHARPOS (pos),
2498 it->region_beg_charpos,
2499 it->region_end_charpos,
2500 &next_check_charpos,
2501 limit, 0);
2502
2503 /* Correct the face for charsets different from ASCII. Do it
2504 for the multibyte case only. The face returned above is
2505 suitable for unibyte text if current_buffer is unibyte. */
2506 if (it->multibyte_p)
2507 {
2508 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2509 struct face *face = FACE_FROM_ID (it->f, face_id);
2510 face_id = FACE_FOR_CHAR (it->f, face, c);
2511 }
2512 }
2513
2514 return face_id;
2515 }
2516
2517
2518 \f
2519 /***********************************************************************
2520 Invisible text
2521 ***********************************************************************/
2522
2523 /* Set up iterator IT from invisible properties at its current
2524 position. Called from handle_stop. */
2525
2526 static enum prop_handled
2527 handle_invisible_prop (it)
2528 struct it *it;
2529 {
2530 enum prop_handled handled = HANDLED_NORMALLY;
2531
2532 if (STRINGP (it->string))
2533 {
2534 extern Lisp_Object Qinvisible;
2535 Lisp_Object prop, end_charpos, limit, charpos;
2536
2537 /* Get the value of the invisible text property at the
2538 current position. Value will be nil if there is no such
2539 property. */
2540 charpos = make_number (IT_STRING_CHARPOS (*it));
2541 prop = Fget_text_property (charpos, Qinvisible, it->string);
2542
2543 if (!NILP (prop)
2544 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2545 {
2546 handled = HANDLED_RECOMPUTE_PROPS;
2547
2548 /* Get the position at which the next change of the
2549 invisible text property can be found in IT->string.
2550 Value will be nil if the property value is the same for
2551 all the rest of IT->string. */
2552 XSETINT (limit, XSTRING (it->string)->size);
2553 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2554 it->string, limit);
2555
2556 /* Text at current position is invisible. The next
2557 change in the property is at position end_charpos.
2558 Move IT's current position to that position. */
2559 if (INTEGERP (end_charpos)
2560 && XFASTINT (end_charpos) < XFASTINT (limit))
2561 {
2562 struct text_pos old;
2563 old = it->current.string_pos;
2564 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2565 compute_string_pos (&it->current.string_pos, old, it->string);
2566 }
2567 else
2568 {
2569 /* The rest of the string is invisible. If this is an
2570 overlay string, proceed with the next overlay string
2571 or whatever comes and return a character from there. */
2572 if (it->current.overlay_string_index >= 0)
2573 {
2574 next_overlay_string (it);
2575 /* Don't check for overlay strings when we just
2576 finished processing them. */
2577 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2578 }
2579 else
2580 {
2581 struct Lisp_String *s = XSTRING (it->string);
2582 IT_STRING_CHARPOS (*it) = s->size;
2583 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2584 }
2585 }
2586 }
2587 }
2588 else
2589 {
2590 int invis_p, newpos, next_stop, start_charpos;
2591 Lisp_Object pos, prop, overlay;
2592
2593 /* First of all, is there invisible text at this position? */
2594 start_charpos = IT_CHARPOS (*it);
2595 pos = make_number (IT_CHARPOS (*it));
2596 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2597 &overlay);
2598 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2599
2600 /* If we are on invisible text, skip over it. */
2601 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
2602 {
2603 /* Record whether we have to display an ellipsis for the
2604 invisible text. */
2605 int display_ellipsis_p = invis_p == 2;
2606
2607 handled = HANDLED_RECOMPUTE_PROPS;
2608
2609 /* Loop skipping over invisible text. The loop is left at
2610 ZV or with IT on the first char being visible again. */
2611 do
2612 {
2613 /* Try to skip some invisible text. Return value is the
2614 position reached which can be equal to IT's position
2615 if there is nothing invisible here. This skips both
2616 over invisible text properties and overlays with
2617 invisible property. */
2618 newpos = skip_invisible (IT_CHARPOS (*it),
2619 &next_stop, ZV, it->window);
2620
2621 /* If we skipped nothing at all we weren't at invisible
2622 text in the first place. If everything to the end of
2623 the buffer was skipped, end the loop. */
2624 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2625 invis_p = 0;
2626 else
2627 {
2628 /* We skipped some characters but not necessarily
2629 all there are. Check if we ended up on visible
2630 text. Fget_char_property returns the property of
2631 the char before the given position, i.e. if we
2632 get invis_p = 0, this means that the char at
2633 newpos is visible. */
2634 pos = make_number (newpos);
2635 prop = Fget_char_property (pos, Qinvisible, it->window);
2636 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2637 }
2638
2639 /* If we ended up on invisible text, proceed to
2640 skip starting with next_stop. */
2641 if (invis_p)
2642 IT_CHARPOS (*it) = next_stop;
2643 }
2644 while (invis_p);
2645
2646 /* The position newpos is now either ZV or on visible text. */
2647 IT_CHARPOS (*it) = newpos;
2648 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2649
2650 /* If there are before-strings at the start of invisible
2651 text, and the text is invisible because of a text
2652 property, arrange to show before-strings because 20.x did
2653 it that way. (If the text is invisible because of an
2654 overlay property instead of a text property, this is
2655 already handled in the overlay code.) */
2656 if (NILP (overlay)
2657 && get_overlay_strings (it, start_charpos))
2658 {
2659 handled = HANDLED_RECOMPUTE_PROPS;
2660 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2661 }
2662 else if (display_ellipsis_p)
2663 setup_for_ellipsis (it);
2664 }
2665 }
2666
2667 return handled;
2668 }
2669
2670
2671 /* Make iterator IT return `...' next. */
2672
2673 static void
2674 setup_for_ellipsis (it)
2675 struct it *it;
2676 {
2677 if (it->dp
2678 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2679 {
2680 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2681 it->dpvec = v->contents;
2682 it->dpend = v->contents + v->size;
2683 }
2684 else
2685 {
2686 /* Default `...'. */
2687 it->dpvec = default_invis_vector;
2688 it->dpend = default_invis_vector + 3;
2689 }
2690
2691 /* The ellipsis display does not replace the display of the
2692 character at the new position. Indicate this by setting
2693 IT->dpvec_char_len to zero. */
2694 it->dpvec_char_len = 0;
2695
2696 it->current.dpvec_index = 0;
2697 it->method = next_element_from_display_vector;
2698 }
2699
2700
2701 \f
2702 /***********************************************************************
2703 'display' property
2704 ***********************************************************************/
2705
2706 /* Set up iterator IT from `display' property at its current position.
2707 Called from handle_stop. */
2708
2709 static enum prop_handled
2710 handle_display_prop (it)
2711 struct it *it;
2712 {
2713 Lisp_Object prop, object;
2714 struct text_pos *position;
2715 int display_replaced_p = 0;
2716
2717 if (STRINGP (it->string))
2718 {
2719 object = it->string;
2720 position = &it->current.string_pos;
2721 }
2722 else
2723 {
2724 object = it->w->buffer;
2725 position = &it->current.pos;
2726 }
2727
2728 /* Reset those iterator values set from display property values. */
2729 it->font_height = Qnil;
2730 it->space_width = Qnil;
2731 it->voffset = 0;
2732
2733 /* We don't support recursive `display' properties, i.e. string
2734 values that have a string `display' property, that have a string
2735 `display' property etc. */
2736 if (!it->string_from_display_prop_p)
2737 it->area = TEXT_AREA;
2738
2739 prop = Fget_char_property (make_number (position->charpos),
2740 Qdisplay, object);
2741 if (NILP (prop))
2742 return HANDLED_NORMALLY;
2743
2744 if (CONSP (prop)
2745 /* Simple properties. */
2746 && !EQ (XCAR (prop), Qimage)
2747 && !EQ (XCAR (prop), Qspace)
2748 && !EQ (XCAR (prop), Qwhen)
2749 && !EQ (XCAR (prop), Qspace_width)
2750 && !EQ (XCAR (prop), Qheight)
2751 && !EQ (XCAR (prop), Qraise)
2752 /* Marginal area specifications. */
2753 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2754 && !NILP (XCAR (prop)))
2755 {
2756 for (; CONSP (prop); prop = XCDR (prop))
2757 {
2758 if (handle_single_display_prop (it, XCAR (prop), object,
2759 position, display_replaced_p))
2760 display_replaced_p = 1;
2761 }
2762 }
2763 else if (VECTORP (prop))
2764 {
2765 int i;
2766 for (i = 0; i < ASIZE (prop); ++i)
2767 if (handle_single_display_prop (it, AREF (prop, i), object,
2768 position, display_replaced_p))
2769 display_replaced_p = 1;
2770 }
2771 else
2772 {
2773 if (handle_single_display_prop (it, prop, object, position, 0))
2774 display_replaced_p = 1;
2775 }
2776
2777 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2778 }
2779
2780
2781 /* Value is the position of the end of the `display' property starting
2782 at START_POS in OBJECT. */
2783
2784 static struct text_pos
2785 display_prop_end (it, object, start_pos)
2786 struct it *it;
2787 Lisp_Object object;
2788 struct text_pos start_pos;
2789 {
2790 Lisp_Object end;
2791 struct text_pos end_pos;
2792
2793 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2794 Qdisplay, object, Qnil);
2795 CHARPOS (end_pos) = XFASTINT (end);
2796 if (STRINGP (object))
2797 compute_string_pos (&end_pos, start_pos, it->string);
2798 else
2799 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2800
2801 return end_pos;
2802 }
2803
2804
2805 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2806 is the object in which the `display' property was found. *POSITION
2807 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2808 means that we previously saw a display sub-property which already
2809 replaced text display with something else, for example an image;
2810 ignore such properties after the first one has been processed.
2811
2812 If PROP is a `space' or `image' sub-property, set *POSITION to the
2813 end position of the `display' property.
2814
2815 Value is non-zero if something was found which replaces the display
2816 of buffer or string text. */
2817
2818 static int
2819 handle_single_display_prop (it, prop, object, position,
2820 display_replaced_before_p)
2821 struct it *it;
2822 Lisp_Object prop;
2823 Lisp_Object object;
2824 struct text_pos *position;
2825 int display_replaced_before_p;
2826 {
2827 Lisp_Object value;
2828 int replaces_text_display_p = 0;
2829 Lisp_Object form;
2830
2831 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2832 evaluated. If the result is nil, VALUE is ignored. */
2833 form = Qt;
2834 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2835 {
2836 prop = XCDR (prop);
2837 if (!CONSP (prop))
2838 return 0;
2839 form = XCAR (prop);
2840 prop = XCDR (prop);
2841 }
2842
2843 if (!NILP (form) && !EQ (form, Qt))
2844 {
2845 int count = BINDING_STACK_SIZE ();
2846 struct gcpro gcpro1;
2847
2848 /* Bind `object' to the object having the `display' property, a
2849 buffer or string. Bind `position' to the position in the
2850 object where the property was found, and `buffer-position'
2851 to the current position in the buffer. */
2852 specbind (Qobject, object);
2853 specbind (Qposition, make_number (CHARPOS (*position)));
2854 specbind (Qbuffer_position,
2855 make_number (STRINGP (object)
2856 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2857 GCPRO1 (form);
2858 form = safe_eval (form);
2859 UNGCPRO;
2860 unbind_to (count, Qnil);
2861 }
2862
2863 if (NILP (form))
2864 return 0;
2865
2866 if (CONSP (prop)
2867 && EQ (XCAR (prop), Qheight)
2868 && CONSP (XCDR (prop)))
2869 {
2870 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2871 return 0;
2872
2873 /* `(height HEIGHT)'. */
2874 it->font_height = XCAR (XCDR (prop));
2875 if (!NILP (it->font_height))
2876 {
2877 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2878 int new_height = -1;
2879
2880 if (CONSP (it->font_height)
2881 && (EQ (XCAR (it->font_height), Qplus)
2882 || EQ (XCAR (it->font_height), Qminus))
2883 && CONSP (XCDR (it->font_height))
2884 && INTEGERP (XCAR (XCDR (it->font_height))))
2885 {
2886 /* `(+ N)' or `(- N)' where N is an integer. */
2887 int steps = XINT (XCAR (XCDR (it->font_height)));
2888 if (EQ (XCAR (it->font_height), Qplus))
2889 steps = - steps;
2890 it->face_id = smaller_face (it->f, it->face_id, steps);
2891 }
2892 else if (FUNCTIONP (it->font_height))
2893 {
2894 /* Call function with current height as argument.
2895 Value is the new height. */
2896 Lisp_Object height;
2897 height = safe_call1 (it->font_height,
2898 face->lface[LFACE_HEIGHT_INDEX]);
2899 if (NUMBERP (height))
2900 new_height = XFLOATINT (height);
2901 }
2902 else if (NUMBERP (it->font_height))
2903 {
2904 /* Value is a multiple of the canonical char height. */
2905 struct face *face;
2906
2907 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2908 new_height = (XFLOATINT (it->font_height)
2909 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2910 }
2911 else
2912 {
2913 /* Evaluate IT->font_height with `height' bound to the
2914 current specified height to get the new height. */
2915 Lisp_Object value;
2916 int count = BINDING_STACK_SIZE ();
2917
2918 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2919 value = safe_eval (it->font_height);
2920 unbind_to (count, Qnil);
2921
2922 if (NUMBERP (value))
2923 new_height = XFLOATINT (value);
2924 }
2925
2926 if (new_height > 0)
2927 it->face_id = face_with_height (it->f, it->face_id, new_height);
2928 }
2929 }
2930 else if (CONSP (prop)
2931 && EQ (XCAR (prop), Qspace_width)
2932 && CONSP (XCDR (prop)))
2933 {
2934 /* `(space_width WIDTH)'. */
2935 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2936 return 0;
2937
2938 value = XCAR (XCDR (prop));
2939 if (NUMBERP (value) && XFLOATINT (value) > 0)
2940 it->space_width = value;
2941 }
2942 else if (CONSP (prop)
2943 && EQ (XCAR (prop), Qraise)
2944 && CONSP (XCDR (prop)))
2945 {
2946 /* `(raise FACTOR)'. */
2947 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2948 return 0;
2949
2950 #ifdef HAVE_WINDOW_SYSTEM
2951 value = XCAR (XCDR (prop));
2952 if (NUMBERP (value))
2953 {
2954 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2955 it->voffset = - (XFLOATINT (value)
2956 * (FONT_HEIGHT (face->font)));
2957 }
2958 #endif /* HAVE_WINDOW_SYSTEM */
2959 }
2960 else if (!it->string_from_display_prop_p)
2961 {
2962 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2963 VALUE) or `((margin nil) VALUE)' or VALUE. */
2964 Lisp_Object location, value;
2965 struct text_pos start_pos;
2966 int valid_p;
2967
2968 /* Characters having this form of property are not displayed, so
2969 we have to find the end of the property. */
2970 start_pos = *position;
2971 *position = display_prop_end (it, object, start_pos);
2972 value = Qnil;
2973
2974 /* Let's stop at the new position and assume that all
2975 text properties change there. */
2976 it->stop_charpos = position->charpos;
2977
2978 location = Qunbound;
2979 if (CONSP (prop) && CONSP (XCAR (prop)))
2980 {
2981 Lisp_Object tem;
2982
2983 value = XCDR (prop);
2984 if (CONSP (value))
2985 value = XCAR (value);
2986
2987 tem = XCAR (prop);
2988 if (EQ (XCAR (tem), Qmargin)
2989 && (tem = XCDR (tem),
2990 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2991 (NILP (tem)
2992 || EQ (tem, Qleft_margin)
2993 || EQ (tem, Qright_margin))))
2994 location = tem;
2995 }
2996
2997 if (EQ (location, Qunbound))
2998 {
2999 location = Qnil;
3000 value = prop;
3001 }
3002
3003 #ifdef HAVE_WINDOW_SYSTEM
3004 if (FRAME_TERMCAP_P (it->f))
3005 valid_p = STRINGP (value);
3006 else
3007 valid_p = (STRINGP (value)
3008 || (CONSP (value) && EQ (XCAR (value), Qspace))
3009 || valid_image_p (value));
3010 #else /* not HAVE_WINDOW_SYSTEM */
3011 valid_p = STRINGP (value);
3012 #endif /* not HAVE_WINDOW_SYSTEM */
3013
3014 if ((EQ (location, Qleft_margin)
3015 || EQ (location, Qright_margin)
3016 || NILP (location))
3017 && valid_p
3018 && !display_replaced_before_p)
3019 {
3020 replaces_text_display_p = 1;
3021
3022 /* Save current settings of IT so that we can restore them
3023 when we are finished with the glyph property value. */
3024 push_it (it);
3025
3026 if (NILP (location))
3027 it->area = TEXT_AREA;
3028 else if (EQ (location, Qleft_margin))
3029 it->area = LEFT_MARGIN_AREA;
3030 else
3031 it->area = RIGHT_MARGIN_AREA;
3032
3033 if (STRINGP (value))
3034 {
3035 it->string = value;
3036 it->multibyte_p = STRING_MULTIBYTE (it->string);
3037 it->current.overlay_string_index = -1;
3038 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3039 it->end_charpos = it->string_nchars
3040 = XSTRING (it->string)->size;
3041 it->method = next_element_from_string;
3042 it->stop_charpos = 0;
3043 it->string_from_display_prop_p = 1;
3044 /* Say that we haven't consumed the characters with
3045 `display' property yet. The call to pop_it in
3046 set_iterator_to_next will clean this up. */
3047 *position = start_pos;
3048 }
3049 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3050 {
3051 it->method = next_element_from_stretch;
3052 it->object = value;
3053 it->current.pos = it->position = start_pos;
3054 }
3055 #ifdef HAVE_WINDOW_SYSTEM
3056 else
3057 {
3058 it->what = IT_IMAGE;
3059 it->image_id = lookup_image (it->f, value);
3060 it->position = start_pos;
3061 it->object = NILP (object) ? it->w->buffer : object;
3062 it->method = next_element_from_image;
3063
3064 /* Say that we haven't consumed the characters with
3065 `display' property yet. The call to pop_it in
3066 set_iterator_to_next will clean this up. */
3067 *position = start_pos;
3068 }
3069 #endif /* HAVE_WINDOW_SYSTEM */
3070 }
3071 else
3072 /* Invalid property or property not supported. Restore
3073 the position to what it was before. */
3074 *position = start_pos;
3075 }
3076
3077 return replaces_text_display_p;
3078 }
3079
3080
3081 /* Check if PROP is a display sub-property value whose text should be
3082 treated as intangible. */
3083
3084 static int
3085 single_display_prop_intangible_p (prop)
3086 Lisp_Object prop;
3087 {
3088 /* Skip over `when FORM'. */
3089 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3090 {
3091 prop = XCDR (prop);
3092 if (!CONSP (prop))
3093 return 0;
3094 prop = XCDR (prop);
3095 }
3096
3097 if (!CONSP (prop))
3098 return 0;
3099
3100 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3101 we don't need to treat text as intangible. */
3102 if (EQ (XCAR (prop), Qmargin))
3103 {
3104 prop = XCDR (prop);
3105 if (!CONSP (prop))
3106 return 0;
3107
3108 prop = XCDR (prop);
3109 if (!CONSP (prop)
3110 || EQ (XCAR (prop), Qleft_margin)
3111 || EQ (XCAR (prop), Qright_margin))
3112 return 0;
3113 }
3114
3115 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3116 }
3117
3118
3119 /* Check if PROP is a display property value whose text should be
3120 treated as intangible. */
3121
3122 int
3123 display_prop_intangible_p (prop)
3124 Lisp_Object prop;
3125 {
3126 if (CONSP (prop)
3127 && CONSP (XCAR (prop))
3128 && !EQ (Qmargin, XCAR (XCAR (prop))))
3129 {
3130 /* A list of sub-properties. */
3131 while (CONSP (prop))
3132 {
3133 if (single_display_prop_intangible_p (XCAR (prop)))
3134 return 1;
3135 prop = XCDR (prop);
3136 }
3137 }
3138 else if (VECTORP (prop))
3139 {
3140 /* A vector of sub-properties. */
3141 int i;
3142 for (i = 0; i < ASIZE (prop); ++i)
3143 if (single_display_prop_intangible_p (AREF (prop, i)))
3144 return 1;
3145 }
3146 else
3147 return single_display_prop_intangible_p (prop);
3148
3149 return 0;
3150 }
3151
3152
3153 /* Return 1 if PROP is a display sub-property value containing STRING. */
3154
3155 static int
3156 single_display_prop_string_p (prop, string)
3157 Lisp_Object prop, string;
3158 {
3159 if (EQ (string, prop))
3160 return 1;
3161
3162 /* Skip over `when FORM'. */
3163 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3164 {
3165 prop = XCDR (prop);
3166 if (!CONSP (prop))
3167 return 0;
3168 prop = XCDR (prop);
3169 }
3170
3171 if (CONSP (prop))
3172 /* Skip over `margin LOCATION'. */
3173 if (EQ (XCAR (prop), Qmargin))
3174 {
3175 prop = XCDR (prop);
3176 if (!CONSP (prop))
3177 return 0;
3178
3179 prop = XCDR (prop);
3180 if (!CONSP (prop))
3181 return 0;
3182 }
3183
3184 return CONSP (prop) && EQ (XCAR (prop), string);
3185 }
3186
3187
3188 /* Return 1 if STRING appears in the `display' property PROP. */
3189
3190 static int
3191 display_prop_string_p (prop, string)
3192 Lisp_Object prop, string;
3193 {
3194 if (CONSP (prop)
3195 && CONSP (XCAR (prop))
3196 && !EQ (Qmargin, XCAR (XCAR (prop))))
3197 {
3198 /* A list of sub-properties. */
3199 while (CONSP (prop))
3200 {
3201 if (single_display_prop_string_p (XCAR (prop), string))
3202 return 1;
3203 prop = XCDR (prop);
3204 }
3205 }
3206 else if (VECTORP (prop))
3207 {
3208 /* A vector of sub-properties. */
3209 int i;
3210 for (i = 0; i < ASIZE (prop); ++i)
3211 if (single_display_prop_string_p (AREF (prop, i), string))
3212 return 1;
3213 }
3214 else
3215 return single_display_prop_string_p (prop, string);
3216
3217 return 0;
3218 }
3219
3220
3221 /* Determine from which buffer position in W's buffer STRING comes
3222 from. AROUND_CHARPOS is an approximate position where it could
3223 be from. Value is the buffer position or 0 if it couldn't be
3224 determined.
3225
3226 W's buffer must be current.
3227
3228 This function is necessary because we don't record buffer positions
3229 in glyphs generated from strings (to keep struct glyph small).
3230 This function may only use code that doesn't eval because it is
3231 called asynchronously from note_mouse_highlight. */
3232
3233 int
3234 string_buffer_position (w, string, around_charpos)
3235 struct window *w;
3236 Lisp_Object string;
3237 int around_charpos;
3238 {
3239 Lisp_Object limit, prop, pos;
3240 const int MAX_DISTANCE = 1000;
3241 int found = 0;
3242
3243 pos = make_number (around_charpos);
3244 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3245 while (!found && !EQ (pos, limit))
3246 {
3247 prop = Fget_char_property (pos, Qdisplay, Qnil);
3248 if (!NILP (prop) && display_prop_string_p (prop, string))
3249 found = 1;
3250 else
3251 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3252 }
3253
3254 if (!found)
3255 {
3256 pos = make_number (around_charpos);
3257 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3258 while (!found && !EQ (pos, limit))
3259 {
3260 prop = Fget_char_property (pos, Qdisplay, Qnil);
3261 if (!NILP (prop) && display_prop_string_p (prop, string))
3262 found = 1;
3263 else
3264 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3265 limit);
3266 }
3267 }
3268
3269 return found ? XINT (pos) : 0;
3270 }
3271
3272
3273 \f
3274 /***********************************************************************
3275 `composition' property
3276 ***********************************************************************/
3277
3278 /* Set up iterator IT from `composition' property at its current
3279 position. Called from handle_stop. */
3280
3281 static enum prop_handled
3282 handle_composition_prop (it)
3283 struct it *it;
3284 {
3285 Lisp_Object prop, string;
3286 int pos, pos_byte, end;
3287 enum prop_handled handled = HANDLED_NORMALLY;
3288
3289 if (STRINGP (it->string))
3290 {
3291 pos = IT_STRING_CHARPOS (*it);
3292 pos_byte = IT_STRING_BYTEPOS (*it);
3293 string = it->string;
3294 }
3295 else
3296 {
3297 pos = IT_CHARPOS (*it);
3298 pos_byte = IT_BYTEPOS (*it);
3299 string = Qnil;
3300 }
3301
3302 /* If there's a valid composition and point is not inside of the
3303 composition (in the case that the composition is from the current
3304 buffer), draw a glyph composed from the composition components. */
3305 if (find_composition (pos, -1, &pos, &end, &prop, string)
3306 && COMPOSITION_VALID_P (pos, end, prop)
3307 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3308 {
3309 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3310
3311 if (id >= 0)
3312 {
3313 it->method = next_element_from_composition;
3314 it->cmp_id = id;
3315 it->cmp_len = COMPOSITION_LENGTH (prop);
3316 /* For a terminal, draw only the first character of the
3317 components. */
3318 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3319 it->len = (STRINGP (it->string)
3320 ? string_char_to_byte (it->string, end)
3321 : CHAR_TO_BYTE (end)) - pos_byte;
3322 it->stop_charpos = end;
3323 handled = HANDLED_RETURN;
3324 }
3325 }
3326
3327 return handled;
3328 }
3329
3330
3331 \f
3332 /***********************************************************************
3333 Overlay strings
3334 ***********************************************************************/
3335
3336 /* The following structure is used to record overlay strings for
3337 later sorting in load_overlay_strings. */
3338
3339 struct overlay_entry
3340 {
3341 Lisp_Object overlay;
3342 Lisp_Object string;
3343 int priority;
3344 int after_string_p;
3345 };
3346
3347
3348 /* Set up iterator IT from overlay strings at its current position.
3349 Called from handle_stop. */
3350
3351 static enum prop_handled
3352 handle_overlay_change (it)
3353 struct it *it;
3354 {
3355 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3356 return HANDLED_RECOMPUTE_PROPS;
3357 else
3358 return HANDLED_NORMALLY;
3359 }
3360
3361
3362 /* Set up the next overlay string for delivery by IT, if there is an
3363 overlay string to deliver. Called by set_iterator_to_next when the
3364 end of the current overlay string is reached. If there are more
3365 overlay strings to display, IT->string and
3366 IT->current.overlay_string_index are set appropriately here.
3367 Otherwise IT->string is set to nil. */
3368
3369 static void
3370 next_overlay_string (it)
3371 struct it *it;
3372 {
3373 ++it->current.overlay_string_index;
3374 if (it->current.overlay_string_index == it->n_overlay_strings)
3375 {
3376 /* No more overlay strings. Restore IT's settings to what
3377 they were before overlay strings were processed, and
3378 continue to deliver from current_buffer. */
3379 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3380
3381 pop_it (it);
3382 xassert (it->stop_charpos >= BEGV
3383 && it->stop_charpos <= it->end_charpos);
3384 it->string = Qnil;
3385 it->current.overlay_string_index = -1;
3386 SET_TEXT_POS (it->current.string_pos, -1, -1);
3387 it->n_overlay_strings = 0;
3388 it->method = next_element_from_buffer;
3389
3390 /* If we're at the end of the buffer, record that we have
3391 processed the overlay strings there already, so that
3392 next_element_from_buffer doesn't try it again. */
3393 if (IT_CHARPOS (*it) >= it->end_charpos)
3394 it->overlay_strings_at_end_processed_p = 1;
3395
3396 /* If we have to display `...' for invisible text, set
3397 the iterator up for that. */
3398 if (display_ellipsis_p)
3399 setup_for_ellipsis (it);
3400 }
3401 else
3402 {
3403 /* There are more overlay strings to process. If
3404 IT->current.overlay_string_index has advanced to a position
3405 where we must load IT->overlay_strings with more strings, do
3406 it. */
3407 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3408
3409 if (it->current.overlay_string_index && i == 0)
3410 load_overlay_strings (it, 0);
3411
3412 /* Initialize IT to deliver display elements from the overlay
3413 string. */
3414 it->string = it->overlay_strings[i];
3415 it->multibyte_p = STRING_MULTIBYTE (it->string);
3416 SET_TEXT_POS (it->current.string_pos, 0, 0);
3417 it->method = next_element_from_string;
3418 it->stop_charpos = 0;
3419 }
3420
3421 CHECK_IT (it);
3422 }
3423
3424
3425 /* Compare two overlay_entry structures E1 and E2. Used as a
3426 comparison function for qsort in load_overlay_strings. Overlay
3427 strings for the same position are sorted so that
3428
3429 1. All after-strings come in front of before-strings, except
3430 when they come from the same overlay.
3431
3432 2. Within after-strings, strings are sorted so that overlay strings
3433 from overlays with higher priorities come first.
3434
3435 2. Within before-strings, strings are sorted so that overlay
3436 strings from overlays with higher priorities come last.
3437
3438 Value is analogous to strcmp. */
3439
3440
3441 static int
3442 compare_overlay_entries (e1, e2)
3443 void *e1, *e2;
3444 {
3445 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3446 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3447 int result;
3448
3449 if (entry1->after_string_p != entry2->after_string_p)
3450 {
3451 /* Let after-strings appear in front of before-strings if
3452 they come from different overlays. */
3453 if (EQ (entry1->overlay, entry2->overlay))
3454 result = entry1->after_string_p ? 1 : -1;
3455 else
3456 result = entry1->after_string_p ? -1 : 1;
3457 }
3458 else if (entry1->after_string_p)
3459 /* After-strings sorted in order of decreasing priority. */
3460 result = entry2->priority - entry1->priority;
3461 else
3462 /* Before-strings sorted in order of increasing priority. */
3463 result = entry1->priority - entry2->priority;
3464
3465 return result;
3466 }
3467
3468
3469 /* Load the vector IT->overlay_strings with overlay strings from IT's
3470 current buffer position, or from CHARPOS if that is > 0. Set
3471 IT->n_overlays to the total number of overlay strings found.
3472
3473 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3474 a time. On entry into load_overlay_strings,
3475 IT->current.overlay_string_index gives the number of overlay
3476 strings that have already been loaded by previous calls to this
3477 function.
3478
3479 IT->add_overlay_start contains an additional overlay start
3480 position to consider for taking overlay strings from, if non-zero.
3481 This position comes into play when the overlay has an `invisible'
3482 property, and both before and after-strings. When we've skipped to
3483 the end of the overlay, because of its `invisible' property, we
3484 nevertheless want its before-string to appear.
3485 IT->add_overlay_start will contain the overlay start position
3486 in this case.
3487
3488 Overlay strings are sorted so that after-string strings come in
3489 front of before-string strings. Within before and after-strings,
3490 strings are sorted by overlay priority. See also function
3491 compare_overlay_entries. */
3492
3493 static void
3494 load_overlay_strings (it, charpos)
3495 struct it *it;
3496 int charpos;
3497 {
3498 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3499 Lisp_Object ov, overlay, window, str, invisible;
3500 int start, end;
3501 int size = 20;
3502 int n = 0, i, j, invis_p;
3503 struct overlay_entry *entries
3504 = (struct overlay_entry *) alloca (size * sizeof *entries);
3505
3506 if (charpos <= 0)
3507 charpos = IT_CHARPOS (*it);
3508
3509 /* Append the overlay string STRING of overlay OVERLAY to vector
3510 `entries' which has size `size' and currently contains `n'
3511 elements. AFTER_P non-zero means STRING is an after-string of
3512 OVERLAY. */
3513 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3514 do \
3515 { \
3516 Lisp_Object priority; \
3517 \
3518 if (n == size) \
3519 { \
3520 int new_size = 2 * size; \
3521 struct overlay_entry *old = entries; \
3522 entries = \
3523 (struct overlay_entry *) alloca (new_size \
3524 * sizeof *entries); \
3525 bcopy (old, entries, size * sizeof *entries); \
3526 size = new_size; \
3527 } \
3528 \
3529 entries[n].string = (STRING); \
3530 entries[n].overlay = (OVERLAY); \
3531 priority = Foverlay_get ((OVERLAY), Qpriority); \
3532 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3533 entries[n].after_string_p = (AFTER_P); \
3534 ++n; \
3535 } \
3536 while (0)
3537
3538 /* Process overlay before the overlay center. */
3539 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3540 {
3541 overlay = XCAR (ov);
3542 xassert (OVERLAYP (overlay));
3543 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3544 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3545
3546 if (end < charpos)
3547 break;
3548
3549 /* Skip this overlay if it doesn't start or end at IT's current
3550 position. */
3551 if (end != charpos && start != charpos)
3552 continue;
3553
3554 /* Skip this overlay if it doesn't apply to IT->w. */
3555 window = Foverlay_get (overlay, Qwindow);
3556 if (WINDOWP (window) && XWINDOW (window) != it->w)
3557 continue;
3558
3559 /* If the text ``under'' the overlay is invisible, both before-
3560 and after-strings from this overlay are visible; start and
3561 end position are indistinguishable. */
3562 invisible = Foverlay_get (overlay, Qinvisible);
3563 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3564
3565 /* If overlay has a non-empty before-string, record it. */
3566 if ((start == charpos || (end == charpos && invis_p))
3567 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3568 && XSTRING (str)->size)
3569 RECORD_OVERLAY_STRING (overlay, str, 0);
3570
3571 /* If overlay has a non-empty after-string, record it. */
3572 if ((end == charpos || (start == charpos && invis_p))
3573 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3574 && XSTRING (str)->size)
3575 RECORD_OVERLAY_STRING (overlay, str, 1);
3576 }
3577
3578 /* Process overlays after the overlay center. */
3579 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3580 {
3581 overlay = XCAR (ov);
3582 xassert (OVERLAYP (overlay));
3583 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3584 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3585
3586 if (start > charpos)
3587 break;
3588
3589 /* Skip this overlay if it doesn't start or end at IT's current
3590 position. */
3591 if (end != charpos && start != charpos)
3592 continue;
3593
3594 /* Skip this overlay if it doesn't apply to IT->w. */
3595 window = Foverlay_get (overlay, Qwindow);
3596 if (WINDOWP (window) && XWINDOW (window) != it->w)
3597 continue;
3598
3599 /* If the text ``under'' the overlay is invisible, it has a zero
3600 dimension, and both before- and after-strings apply. */
3601 invisible = Foverlay_get (overlay, Qinvisible);
3602 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3603
3604 /* If overlay has a non-empty before-string, record it. */
3605 if ((start == charpos || (end == charpos && invis_p))
3606 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3607 && XSTRING (str)->size)
3608 RECORD_OVERLAY_STRING (overlay, str, 0);
3609
3610 /* If overlay has a non-empty after-string, record it. */
3611 if ((end == charpos || (start == charpos && invis_p))
3612 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3613 && XSTRING (str)->size)
3614 RECORD_OVERLAY_STRING (overlay, str, 1);
3615 }
3616
3617 #undef RECORD_OVERLAY_STRING
3618
3619 /* Sort entries. */
3620 if (n > 1)
3621 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3622
3623 /* Record the total number of strings to process. */
3624 it->n_overlay_strings = n;
3625
3626 /* IT->current.overlay_string_index is the number of overlay strings
3627 that have already been consumed by IT. Copy some of the
3628 remaining overlay strings to IT->overlay_strings. */
3629 i = 0;
3630 j = it->current.overlay_string_index;
3631 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3632 it->overlay_strings[i++] = entries[j++].string;
3633
3634 CHECK_IT (it);
3635 }
3636
3637
3638 /* Get the first chunk of overlay strings at IT's current buffer
3639 position, or at CHARPOS if that is > 0. Value is non-zero if at
3640 least one overlay string was found. */
3641
3642 static int
3643 get_overlay_strings (it, charpos)
3644 struct it *it;
3645 int charpos;
3646 {
3647 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3648 process. This fills IT->overlay_strings with strings, and sets
3649 IT->n_overlay_strings to the total number of strings to process.
3650 IT->pos.overlay_string_index has to be set temporarily to zero
3651 because load_overlay_strings needs this; it must be set to -1
3652 when no overlay strings are found because a zero value would
3653 indicate a position in the first overlay string. */
3654 it->current.overlay_string_index = 0;
3655 load_overlay_strings (it, charpos);
3656
3657 /* If we found overlay strings, set up IT to deliver display
3658 elements from the first one. Otherwise set up IT to deliver
3659 from current_buffer. */
3660 if (it->n_overlay_strings)
3661 {
3662 /* Make sure we know settings in current_buffer, so that we can
3663 restore meaningful values when we're done with the overlay
3664 strings. */
3665 compute_stop_pos (it);
3666 xassert (it->face_id >= 0);
3667
3668 /* Save IT's settings. They are restored after all overlay
3669 strings have been processed. */
3670 xassert (it->sp == 0);
3671 push_it (it);
3672
3673 /* Set up IT to deliver display elements from the first overlay
3674 string. */
3675 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3676 it->string = it->overlay_strings[0];
3677 it->stop_charpos = 0;
3678 it->end_charpos = XSTRING (it->string)->size;
3679 it->multibyte_p = STRING_MULTIBYTE (it->string);
3680 xassert (STRINGP (it->string));
3681 it->method = next_element_from_string;
3682 }
3683 else
3684 {
3685 it->string = Qnil;
3686 it->current.overlay_string_index = -1;
3687 it->method = next_element_from_buffer;
3688 }
3689
3690 CHECK_IT (it);
3691
3692 /* Value is non-zero if we found at least one overlay string. */
3693 return STRINGP (it->string);
3694 }
3695
3696
3697 \f
3698 /***********************************************************************
3699 Saving and restoring state
3700 ***********************************************************************/
3701
3702 /* Save current settings of IT on IT->stack. Called, for example,
3703 before setting up IT for an overlay string, to be able to restore
3704 IT's settings to what they were after the overlay string has been
3705 processed. */
3706
3707 static void
3708 push_it (it)
3709 struct it *it;
3710 {
3711 struct iterator_stack_entry *p;
3712
3713 xassert (it->sp < 2);
3714 p = it->stack + it->sp;
3715
3716 p->stop_charpos = it->stop_charpos;
3717 xassert (it->face_id >= 0);
3718 p->face_id = it->face_id;
3719 p->string = it->string;
3720 p->pos = it->current;
3721 p->end_charpos = it->end_charpos;
3722 p->string_nchars = it->string_nchars;
3723 p->area = it->area;
3724 p->multibyte_p = it->multibyte_p;
3725 p->space_width = it->space_width;
3726 p->font_height = it->font_height;
3727 p->voffset = it->voffset;
3728 p->string_from_display_prop_p = it->string_from_display_prop_p;
3729 p->display_ellipsis_p = 0;
3730 ++it->sp;
3731 }
3732
3733
3734 /* Restore IT's settings from IT->stack. Called, for example, when no
3735 more overlay strings must be processed, and we return to delivering
3736 display elements from a buffer, or when the end of a string from a
3737 `display' property is reached and we return to delivering display
3738 elements from an overlay string, or from a buffer. */
3739
3740 static void
3741 pop_it (it)
3742 struct it *it;
3743 {
3744 struct iterator_stack_entry *p;
3745
3746 xassert (it->sp > 0);
3747 --it->sp;
3748 p = it->stack + it->sp;
3749 it->stop_charpos = p->stop_charpos;
3750 it->face_id = p->face_id;
3751 it->string = p->string;
3752 it->current = p->pos;
3753 it->end_charpos = p->end_charpos;
3754 it->string_nchars = p->string_nchars;
3755 it->area = p->area;
3756 it->multibyte_p = p->multibyte_p;
3757 it->space_width = p->space_width;
3758 it->font_height = p->font_height;
3759 it->voffset = p->voffset;
3760 it->string_from_display_prop_p = p->string_from_display_prop_p;
3761 }
3762
3763
3764 \f
3765 /***********************************************************************
3766 Moving over lines
3767 ***********************************************************************/
3768
3769 /* Set IT's current position to the previous line start. */
3770
3771 static void
3772 back_to_previous_line_start (it)
3773 struct it *it;
3774 {
3775 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3776 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3777 }
3778
3779
3780 /* Move IT to the next line start.
3781
3782 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3783 we skipped over part of the text (as opposed to moving the iterator
3784 continuously over the text). Otherwise, don't change the value
3785 of *SKIPPED_P.
3786
3787 Newlines may come from buffer text, overlay strings, or strings
3788 displayed via the `display' property. That's the reason we can't
3789 simply use find_next_newline_no_quit.
3790
3791 Note that this function may not skip over invisible text that is so
3792 because of text properties and immediately follows a newline. If
3793 it would, function reseat_at_next_visible_line_start, when called
3794 from set_iterator_to_next, would effectively make invisible
3795 characters following a newline part of the wrong glyph row, which
3796 leads to wrong cursor motion. */
3797
3798 static int
3799 forward_to_next_line_start (it, skipped_p)
3800 struct it *it;
3801 int *skipped_p;
3802 {
3803 int old_selective, newline_found_p, n;
3804 const int MAX_NEWLINE_DISTANCE = 500;
3805
3806 /* If already on a newline, just consume it to avoid unintended
3807 skipping over invisible text below. */
3808 if (it->what == IT_CHARACTER
3809 && it->c == '\n'
3810 && CHARPOS (it->position) == IT_CHARPOS (*it))
3811 {
3812 set_iterator_to_next (it, 0);
3813 it->c = 0;
3814 return 1;
3815 }
3816
3817 /* Don't handle selective display in the following. It's (a)
3818 unnecessary because it's done by the caller, and (b) leads to an
3819 infinite recursion because next_element_from_ellipsis indirectly
3820 calls this function. */
3821 old_selective = it->selective;
3822 it->selective = 0;
3823
3824 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3825 from buffer text. */
3826 for (n = newline_found_p = 0;
3827 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3828 n += STRINGP (it->string) ? 0 : 1)
3829 {
3830 if (!get_next_display_element (it))
3831 break;
3832 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3833 set_iterator_to_next (it, 0);
3834 }
3835
3836 /* If we didn't find a newline near enough, see if we can use a
3837 short-cut. */
3838 if (n == MAX_NEWLINE_DISTANCE)
3839 {
3840 int start = IT_CHARPOS (*it);
3841 int limit = find_next_newline_no_quit (start, 1);
3842 Lisp_Object pos;
3843
3844 xassert (!STRINGP (it->string));
3845
3846 /* If there isn't any `display' property in sight, and no
3847 overlays, we can just use the position of the newline in
3848 buffer text. */
3849 if (it->stop_charpos >= limit
3850 || ((pos = Fnext_single_property_change (make_number (start),
3851 Qdisplay,
3852 Qnil, make_number (limit)),
3853 NILP (pos))
3854 && next_overlay_change (start) == ZV))
3855 {
3856 IT_CHARPOS (*it) = limit;
3857 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3858 *skipped_p = newline_found_p = 1;
3859 }
3860 else
3861 {
3862 while (get_next_display_element (it)
3863 && !newline_found_p)
3864 {
3865 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3866 set_iterator_to_next (it, 0);
3867 }
3868 }
3869 }
3870
3871 it->selective = old_selective;
3872 return newline_found_p;
3873 }
3874
3875
3876 /* Set IT's current position to the previous visible line start. Skip
3877 invisible text that is so either due to text properties or due to
3878 selective display. Caution: this does not change IT->current_x and
3879 IT->hpos. */
3880
3881 static void
3882 back_to_previous_visible_line_start (it)
3883 struct it *it;
3884 {
3885 int visible_p = 0;
3886
3887 /* Go back one newline if not on BEGV already. */
3888 if (IT_CHARPOS (*it) > BEGV)
3889 back_to_previous_line_start (it);
3890
3891 /* Move over lines that are invisible because of selective display
3892 or text properties. */
3893 while (IT_CHARPOS (*it) > BEGV
3894 && !visible_p)
3895 {
3896 visible_p = 1;
3897
3898 /* If selective > 0, then lines indented more than that values
3899 are invisible. */
3900 if (it->selective > 0
3901 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3902 it->selective))
3903 visible_p = 0;
3904 else
3905 {
3906 Lisp_Object prop;
3907
3908 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3909 Qinvisible, it->window);
3910 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3911 visible_p = 0;
3912 }
3913
3914 /* Back one more newline if the current one is invisible. */
3915 if (!visible_p)
3916 back_to_previous_line_start (it);
3917 }
3918
3919 xassert (IT_CHARPOS (*it) >= BEGV);
3920 xassert (IT_CHARPOS (*it) == BEGV
3921 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3922 CHECK_IT (it);
3923 }
3924
3925
3926 /* Reseat iterator IT at the previous visible line start. Skip
3927 invisible text that is so either due to text properties or due to
3928 selective display. At the end, update IT's overlay information,
3929 face information etc. */
3930
3931 static void
3932 reseat_at_previous_visible_line_start (it)
3933 struct it *it;
3934 {
3935 back_to_previous_visible_line_start (it);
3936 reseat (it, it->current.pos, 1);
3937 CHECK_IT (it);
3938 }
3939
3940
3941 /* Reseat iterator IT on the next visible line start in the current
3942 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3943 preceding the line start. Skip over invisible text that is so
3944 because of selective display. Compute faces, overlays etc at the
3945 new position. Note that this function does not skip over text that
3946 is invisible because of text properties. */
3947
3948 static void
3949 reseat_at_next_visible_line_start (it, on_newline_p)
3950 struct it *it;
3951 int on_newline_p;
3952 {
3953 int newline_found_p, skipped_p = 0;
3954
3955 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3956
3957 /* Skip over lines that are invisible because they are indented
3958 more than the value of IT->selective. */
3959 if (it->selective > 0)
3960 while (IT_CHARPOS (*it) < ZV
3961 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3962 it->selective))
3963 {
3964 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3965 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3966 }
3967
3968 /* Position on the newline if that's what's requested. */
3969 if (on_newline_p && newline_found_p)
3970 {
3971 if (STRINGP (it->string))
3972 {
3973 if (IT_STRING_CHARPOS (*it) > 0)
3974 {
3975 --IT_STRING_CHARPOS (*it);
3976 --IT_STRING_BYTEPOS (*it);
3977 }
3978 }
3979 else if (IT_CHARPOS (*it) > BEGV)
3980 {
3981 --IT_CHARPOS (*it);
3982 --IT_BYTEPOS (*it);
3983 reseat (it, it->current.pos, 0);
3984 }
3985 }
3986 else if (skipped_p)
3987 reseat (it, it->current.pos, 0);
3988
3989 CHECK_IT (it);
3990 }
3991
3992
3993 \f
3994 /***********************************************************************
3995 Changing an iterator's position
3996 ***********************************************************************/
3997
3998 /* Change IT's current position to POS in current_buffer. If FORCE_P
3999 is non-zero, always check for text properties at the new position.
4000 Otherwise, text properties are only looked up if POS >=
4001 IT->check_charpos of a property. */
4002
4003 static void
4004 reseat (it, pos, force_p)
4005 struct it *it;
4006 struct text_pos pos;
4007 int force_p;
4008 {
4009 int original_pos = IT_CHARPOS (*it);
4010
4011 reseat_1 (it, pos, 0);
4012
4013 /* Determine where to check text properties. Avoid doing it
4014 where possible because text property lookup is very expensive. */
4015 if (force_p
4016 || CHARPOS (pos) > it->stop_charpos
4017 || CHARPOS (pos) < original_pos)
4018 handle_stop (it);
4019
4020 CHECK_IT (it);
4021 }
4022
4023
4024 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4025 IT->stop_pos to POS, also. */
4026
4027 static void
4028 reseat_1 (it, pos, set_stop_p)
4029 struct it *it;
4030 struct text_pos pos;
4031 int set_stop_p;
4032 {
4033 /* Don't call this function when scanning a C string. */
4034 xassert (it->s == NULL);
4035
4036 /* POS must be a reasonable value. */
4037 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4038
4039 it->current.pos = it->position = pos;
4040 XSETBUFFER (it->object, current_buffer);
4041 it->end_charpos = ZV;
4042 it->dpvec = NULL;
4043 it->current.dpvec_index = -1;
4044 it->current.overlay_string_index = -1;
4045 IT_STRING_CHARPOS (*it) = -1;
4046 IT_STRING_BYTEPOS (*it) = -1;
4047 it->string = Qnil;
4048 it->method = next_element_from_buffer;
4049 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4050 it->sp = 0;
4051 it->face_before_selective_p = 0;
4052
4053 if (set_stop_p)
4054 it->stop_charpos = CHARPOS (pos);
4055 }
4056
4057
4058 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4059 If S is non-null, it is a C string to iterate over. Otherwise,
4060 STRING gives a Lisp string to iterate over.
4061
4062 If PRECISION > 0, don't return more then PRECISION number of
4063 characters from the string.
4064
4065 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4066 characters have been returned. FIELD_WIDTH < 0 means an infinite
4067 field width.
4068
4069 MULTIBYTE = 0 means disable processing of multibyte characters,
4070 MULTIBYTE > 0 means enable it,
4071 MULTIBYTE < 0 means use IT->multibyte_p.
4072
4073 IT must be initialized via a prior call to init_iterator before
4074 calling this function. */
4075
4076 static void
4077 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4078 struct it *it;
4079 unsigned char *s;
4080 Lisp_Object string;
4081 int charpos;
4082 int precision, field_width, multibyte;
4083 {
4084 /* No region in strings. */
4085 it->region_beg_charpos = it->region_end_charpos = -1;
4086
4087 /* No text property checks performed by default, but see below. */
4088 it->stop_charpos = -1;
4089
4090 /* Set iterator position and end position. */
4091 bzero (&it->current, sizeof it->current);
4092 it->current.overlay_string_index = -1;
4093 it->current.dpvec_index = -1;
4094 xassert (charpos >= 0);
4095
4096 /* If STRING is specified, use its multibyteness, otherwise use the
4097 setting of MULTIBYTE, if specified. */
4098 if (multibyte >= 0)
4099 it->multibyte_p = multibyte > 0;
4100
4101 if (s == NULL)
4102 {
4103 xassert (STRINGP (string));
4104 it->string = string;
4105 it->s = NULL;
4106 it->end_charpos = it->string_nchars = XSTRING (string)->size;
4107 it->method = next_element_from_string;
4108 it->current.string_pos = string_pos (charpos, string);
4109 }
4110 else
4111 {
4112 it->s = s;
4113 it->string = Qnil;
4114
4115 /* Note that we use IT->current.pos, not it->current.string_pos,
4116 for displaying C strings. */
4117 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4118 if (it->multibyte_p)
4119 {
4120 it->current.pos = c_string_pos (charpos, s, 1);
4121 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4122 }
4123 else
4124 {
4125 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4126 it->end_charpos = it->string_nchars = strlen (s);
4127 }
4128
4129 it->method = next_element_from_c_string;
4130 }
4131
4132 /* PRECISION > 0 means don't return more than PRECISION characters
4133 from the string. */
4134 if (precision > 0 && it->end_charpos - charpos > precision)
4135 it->end_charpos = it->string_nchars = charpos + precision;
4136
4137 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4138 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4139 FIELD_WIDTH < 0 means infinite field width. This is useful for
4140 padding with `-' at the end of a mode line. */
4141 if (field_width < 0)
4142 field_width = INFINITY;
4143 if (field_width > it->end_charpos - charpos)
4144 it->end_charpos = charpos + field_width;
4145
4146 /* Use the standard display table for displaying strings. */
4147 if (DISP_TABLE_P (Vstandard_display_table))
4148 it->dp = XCHAR_TABLE (Vstandard_display_table);
4149
4150 it->stop_charpos = charpos;
4151 CHECK_IT (it);
4152 }
4153
4154
4155 \f
4156 /***********************************************************************
4157 Iteration
4158 ***********************************************************************/
4159
4160 /* Load IT's display element fields with information about the next
4161 display element from the current position of IT. Value is zero if
4162 end of buffer (or C string) is reached. */
4163
4164 int
4165 get_next_display_element (it)
4166 struct it *it;
4167 {
4168 /* Non-zero means that we found an display element. Zero means that
4169 we hit the end of what we iterate over. Performance note: the
4170 function pointer `method' used here turns out to be faster than
4171 using a sequence of if-statements. */
4172 int success_p = (*it->method) (it);
4173
4174 if (it->what == IT_CHARACTER)
4175 {
4176 /* Map via display table or translate control characters.
4177 IT->c, IT->len etc. have been set to the next character by
4178 the function call above. If we have a display table, and it
4179 contains an entry for IT->c, translate it. Don't do this if
4180 IT->c itself comes from a display table, otherwise we could
4181 end up in an infinite recursion. (An alternative could be to
4182 count the recursion depth of this function and signal an
4183 error when a certain maximum depth is reached.) Is it worth
4184 it? */
4185 if (success_p && it->dpvec == NULL)
4186 {
4187 Lisp_Object dv;
4188
4189 if (it->dp
4190 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4191 VECTORP (dv)))
4192 {
4193 struct Lisp_Vector *v = XVECTOR (dv);
4194
4195 /* Return the first character from the display table
4196 entry, if not empty. If empty, don't display the
4197 current character. */
4198 if (v->size)
4199 {
4200 it->dpvec_char_len = it->len;
4201 it->dpvec = v->contents;
4202 it->dpend = v->contents + v->size;
4203 it->current.dpvec_index = 0;
4204 it->method = next_element_from_display_vector;
4205 success_p = get_next_display_element (it);
4206 }
4207 else
4208 {
4209 set_iterator_to_next (it, 0);
4210 success_p = get_next_display_element (it);
4211 }
4212 }
4213
4214 /* Translate control characters into `\003' or `^C' form.
4215 Control characters coming from a display table entry are
4216 currently not translated because we use IT->dpvec to hold
4217 the translation. This could easily be changed but I
4218 don't believe that it is worth doing.
4219
4220 Non-printable multibyte characters are also translated
4221 octal form. */
4222 else if ((it->c < ' '
4223 && (it->area != TEXT_AREA
4224 || (it->c != '\n' && it->c != '\t')))
4225 || (it->c >= 127
4226 && it->len == 1)
4227 || !CHAR_PRINTABLE_P (it->c))
4228 {
4229 /* IT->c is a control character which must be displayed
4230 either as '\003' or as `^C' where the '\\' and '^'
4231 can be defined in the display table. Fill
4232 IT->ctl_chars with glyphs for what we have to
4233 display. Then, set IT->dpvec to these glyphs. */
4234 GLYPH g;
4235
4236 if (it->c < 128 && it->ctl_arrow_p)
4237 {
4238 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4239 if (it->dp
4240 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4241 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4242 g = XINT (DISP_CTRL_GLYPH (it->dp));
4243 else
4244 g = FAST_MAKE_GLYPH ('^', 0);
4245 XSETINT (it->ctl_chars[0], g);
4246
4247 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4248 XSETINT (it->ctl_chars[1], g);
4249
4250 /* Set up IT->dpvec and return first character from it. */
4251 it->dpvec_char_len = it->len;
4252 it->dpvec = it->ctl_chars;
4253 it->dpend = it->dpvec + 2;
4254 it->current.dpvec_index = 0;
4255 it->method = next_element_from_display_vector;
4256 get_next_display_element (it);
4257 }
4258 else
4259 {
4260 unsigned char str[MAX_MULTIBYTE_LENGTH];
4261 int len;
4262 int i;
4263 GLYPH escape_glyph;
4264
4265 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4266 if (it->dp
4267 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4268 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4269 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4270 else
4271 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4272
4273 if (SINGLE_BYTE_CHAR_P (it->c))
4274 str[0] = it->c, len = 1;
4275 else
4276 {
4277 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4278 if (len < 0)
4279 {
4280 /* It's an invalid character, which
4281 shouldn't happen actually, but due to
4282 bugs it may happen. Let's print the char
4283 as is, there's not much meaningful we can
4284 do with it. */
4285 str[0] = it->c;
4286 str[1] = it->c >> 8;
4287 str[2] = it->c >> 16;
4288 str[3] = it->c >> 24;
4289 len = 4;
4290 }
4291 }
4292
4293 for (i = 0; i < len; i++)
4294 {
4295 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4296 /* Insert three more glyphs into IT->ctl_chars for
4297 the octal display of the character. */
4298 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4299 XSETINT (it->ctl_chars[i * 4 + 1], g);
4300 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4301 XSETINT (it->ctl_chars[i * 4 + 2], g);
4302 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4303 XSETINT (it->ctl_chars[i * 4 + 3], g);
4304 }
4305
4306 /* Set up IT->dpvec and return the first character
4307 from it. */
4308 it->dpvec_char_len = it->len;
4309 it->dpvec = it->ctl_chars;
4310 it->dpend = it->dpvec + len * 4;
4311 it->current.dpvec_index = 0;
4312 it->method = next_element_from_display_vector;
4313 get_next_display_element (it);
4314 }
4315 }
4316 }
4317
4318 /* Adjust face id for a multibyte character. There are no
4319 multibyte character in unibyte text. */
4320 if (it->multibyte_p
4321 && success_p
4322 && FRAME_WINDOW_P (it->f))
4323 {
4324 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4325 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4326 }
4327 }
4328
4329 /* Is this character the last one of a run of characters with
4330 box? If yes, set IT->end_of_box_run_p to 1. */
4331 if (it->face_box_p
4332 && it->s == NULL)
4333 {
4334 int face_id;
4335 struct face *face;
4336
4337 it->end_of_box_run_p
4338 = ((face_id = face_after_it_pos (it),
4339 face_id != it->face_id)
4340 && (face = FACE_FROM_ID (it->f, face_id),
4341 face->box == FACE_NO_BOX));
4342 }
4343
4344 /* Value is 0 if end of buffer or string reached. */
4345 return success_p;
4346 }
4347
4348
4349 /* Move IT to the next display element.
4350
4351 RESEAT_P non-zero means if called on a newline in buffer text,
4352 skip to the next visible line start.
4353
4354 Functions get_next_display_element and set_iterator_to_next are
4355 separate because I find this arrangement easier to handle than a
4356 get_next_display_element function that also increments IT's
4357 position. The way it is we can first look at an iterator's current
4358 display element, decide whether it fits on a line, and if it does,
4359 increment the iterator position. The other way around we probably
4360 would either need a flag indicating whether the iterator has to be
4361 incremented the next time, or we would have to implement a
4362 decrement position function which would not be easy to write. */
4363
4364 void
4365 set_iterator_to_next (it, reseat_p)
4366 struct it *it;
4367 int reseat_p;
4368 {
4369 /* Reset flags indicating start and end of a sequence of characters
4370 with box. Reset them at the start of this function because
4371 moving the iterator to a new position might set them. */
4372 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4373
4374 if (it->method == next_element_from_buffer)
4375 {
4376 /* The current display element of IT is a character from
4377 current_buffer. Advance in the buffer, and maybe skip over
4378 invisible lines that are so because of selective display. */
4379 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4380 reseat_at_next_visible_line_start (it, 0);
4381 else
4382 {
4383 xassert (it->len != 0);
4384 IT_BYTEPOS (*it) += it->len;
4385 IT_CHARPOS (*it) += 1;
4386 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4387 }
4388 }
4389 else if (it->method == next_element_from_composition)
4390 {
4391 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4392 if (STRINGP (it->string))
4393 {
4394 IT_STRING_BYTEPOS (*it) += it->len;
4395 IT_STRING_CHARPOS (*it) += it->cmp_len;
4396 it->method = next_element_from_string;
4397 goto consider_string_end;
4398 }
4399 else
4400 {
4401 IT_BYTEPOS (*it) += it->len;
4402 IT_CHARPOS (*it) += it->cmp_len;
4403 it->method = next_element_from_buffer;
4404 }
4405 }
4406 else if (it->method == next_element_from_c_string)
4407 {
4408 /* Current display element of IT is from a C string. */
4409 IT_BYTEPOS (*it) += it->len;
4410 IT_CHARPOS (*it) += 1;
4411 }
4412 else if (it->method == next_element_from_display_vector)
4413 {
4414 /* Current display element of IT is from a display table entry.
4415 Advance in the display table definition. Reset it to null if
4416 end reached, and continue with characters from buffers/
4417 strings. */
4418 ++it->current.dpvec_index;
4419
4420 /* Restore face of the iterator to what they were before the
4421 display vector entry (these entries may contain faces). */
4422 it->face_id = it->saved_face_id;
4423
4424 if (it->dpvec + it->current.dpvec_index == it->dpend)
4425 {
4426 if (it->s)
4427 it->method = next_element_from_c_string;
4428 else if (STRINGP (it->string))
4429 it->method = next_element_from_string;
4430 else
4431 it->method = next_element_from_buffer;
4432
4433 it->dpvec = NULL;
4434 it->current.dpvec_index = -1;
4435
4436 /* Skip over characters which were displayed via IT->dpvec. */
4437 if (it->dpvec_char_len < 0)
4438 reseat_at_next_visible_line_start (it, 1);
4439 else if (it->dpvec_char_len > 0)
4440 {
4441 it->len = it->dpvec_char_len;
4442 set_iterator_to_next (it, reseat_p);
4443 }
4444 }
4445 }
4446 else if (it->method == next_element_from_string)
4447 {
4448 /* Current display element is a character from a Lisp string. */
4449 xassert (it->s == NULL && STRINGP (it->string));
4450 IT_STRING_BYTEPOS (*it) += it->len;
4451 IT_STRING_CHARPOS (*it) += 1;
4452
4453 consider_string_end:
4454
4455 if (it->current.overlay_string_index >= 0)
4456 {
4457 /* IT->string is an overlay string. Advance to the
4458 next, if there is one. */
4459 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4460 next_overlay_string (it);
4461 }
4462 else
4463 {
4464 /* IT->string is not an overlay string. If we reached
4465 its end, and there is something on IT->stack, proceed
4466 with what is on the stack. This can be either another
4467 string, this time an overlay string, or a buffer. */
4468 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4469 && it->sp > 0)
4470 {
4471 pop_it (it);
4472 if (!STRINGP (it->string))
4473 it->method = next_element_from_buffer;
4474 else
4475 goto consider_string_end;
4476 }
4477 }
4478 }
4479 else if (it->method == next_element_from_image
4480 || it->method == next_element_from_stretch)
4481 {
4482 /* The position etc with which we have to proceed are on
4483 the stack. The position may be at the end of a string,
4484 if the `display' property takes up the whole string. */
4485 pop_it (it);
4486 it->image_id = 0;
4487 if (STRINGP (it->string))
4488 {
4489 it->method = next_element_from_string;
4490 goto consider_string_end;
4491 }
4492 else
4493 it->method = next_element_from_buffer;
4494 }
4495 else
4496 /* There are no other methods defined, so this should be a bug. */
4497 abort ();
4498
4499 xassert (it->method != next_element_from_string
4500 || (STRINGP (it->string)
4501 && IT_STRING_CHARPOS (*it) >= 0));
4502 }
4503
4504
4505 /* Load IT's display element fields with information about the next
4506 display element which comes from a display table entry or from the
4507 result of translating a control character to one of the forms `^C'
4508 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4509
4510 static int
4511 next_element_from_display_vector (it)
4512 struct it *it;
4513 {
4514 /* Precondition. */
4515 xassert (it->dpvec && it->current.dpvec_index >= 0);
4516
4517 /* Remember the current face id in case glyphs specify faces.
4518 IT's face is restored in set_iterator_to_next. */
4519 it->saved_face_id = it->face_id;
4520
4521 if (INTEGERP (*it->dpvec)
4522 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4523 {
4524 int lface_id;
4525 GLYPH g;
4526
4527 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4528 it->c = FAST_GLYPH_CHAR (g);
4529 it->len = CHAR_BYTES (it->c);
4530
4531 /* The entry may contain a face id to use. Such a face id is
4532 the id of a Lisp face, not a realized face. A face id of
4533 zero means no face is specified. */
4534 lface_id = FAST_GLYPH_FACE (g);
4535 if (lface_id)
4536 {
4537 /* The function returns -1 if lface_id is invalid. */
4538 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4539 if (face_id >= 0)
4540 it->face_id = face_id;
4541 }
4542 }
4543 else
4544 /* Display table entry is invalid. Return a space. */
4545 it->c = ' ', it->len = 1;
4546
4547 /* Don't change position and object of the iterator here. They are
4548 still the values of the character that had this display table
4549 entry or was translated, and that's what we want. */
4550 it->what = IT_CHARACTER;
4551 return 1;
4552 }
4553
4554
4555 /* Load IT with the next display element from Lisp string IT->string.
4556 IT->current.string_pos is the current position within the string.
4557 If IT->current.overlay_string_index >= 0, the Lisp string is an
4558 overlay string. */
4559
4560 static int
4561 next_element_from_string (it)
4562 struct it *it;
4563 {
4564 struct text_pos position;
4565
4566 xassert (STRINGP (it->string));
4567 xassert (IT_STRING_CHARPOS (*it) >= 0);
4568 position = it->current.string_pos;
4569
4570 /* Time to check for invisible text? */
4571 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4572 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4573 {
4574 handle_stop (it);
4575
4576 /* Since a handler may have changed IT->method, we must
4577 recurse here. */
4578 return get_next_display_element (it);
4579 }
4580
4581 if (it->current.overlay_string_index >= 0)
4582 {
4583 /* Get the next character from an overlay string. In overlay
4584 strings, There is no field width or padding with spaces to
4585 do. */
4586 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4587 {
4588 it->what = IT_EOB;
4589 return 0;
4590 }
4591 else if (STRING_MULTIBYTE (it->string))
4592 {
4593 int remaining = (STRING_BYTES (XSTRING (it->string))
4594 - IT_STRING_BYTEPOS (*it));
4595 unsigned char *s = (XSTRING (it->string)->data
4596 + IT_STRING_BYTEPOS (*it));
4597 it->c = string_char_and_length (s, remaining, &it->len);
4598 }
4599 else
4600 {
4601 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4602 it->len = 1;
4603 }
4604 }
4605 else
4606 {
4607 /* Get the next character from a Lisp string that is not an
4608 overlay string. Such strings come from the mode line, for
4609 example. We may have to pad with spaces, or truncate the
4610 string. See also next_element_from_c_string. */
4611 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4612 {
4613 it->what = IT_EOB;
4614 return 0;
4615 }
4616 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4617 {
4618 /* Pad with spaces. */
4619 it->c = ' ', it->len = 1;
4620 CHARPOS (position) = BYTEPOS (position) = -1;
4621 }
4622 else if (STRING_MULTIBYTE (it->string))
4623 {
4624 int maxlen = (STRING_BYTES (XSTRING (it->string))
4625 - IT_STRING_BYTEPOS (*it));
4626 unsigned char *s = (XSTRING (it->string)->data
4627 + IT_STRING_BYTEPOS (*it));
4628 it->c = string_char_and_length (s, maxlen, &it->len);
4629 }
4630 else
4631 {
4632 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4633 it->len = 1;
4634 }
4635 }
4636
4637 /* Record what we have and where it came from. Note that we store a
4638 buffer position in IT->position although it could arguably be a
4639 string position. */
4640 it->what = IT_CHARACTER;
4641 it->object = it->string;
4642 it->position = position;
4643 return 1;
4644 }
4645
4646
4647 /* Load IT with next display element from C string IT->s.
4648 IT->string_nchars is the maximum number of characters to return
4649 from the string. IT->end_charpos may be greater than
4650 IT->string_nchars when this function is called, in which case we
4651 may have to return padding spaces. Value is zero if end of string
4652 reached, including padding spaces. */
4653
4654 static int
4655 next_element_from_c_string (it)
4656 struct it *it;
4657 {
4658 int success_p = 1;
4659
4660 xassert (it->s);
4661 it->what = IT_CHARACTER;
4662 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4663 it->object = Qnil;
4664
4665 /* IT's position can be greater IT->string_nchars in case a field
4666 width or precision has been specified when the iterator was
4667 initialized. */
4668 if (IT_CHARPOS (*it) >= it->end_charpos)
4669 {
4670 /* End of the game. */
4671 it->what = IT_EOB;
4672 success_p = 0;
4673 }
4674 else if (IT_CHARPOS (*it) >= it->string_nchars)
4675 {
4676 /* Pad with spaces. */
4677 it->c = ' ', it->len = 1;
4678 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4679 }
4680 else if (it->multibyte_p)
4681 {
4682 /* Implementation note: The calls to strlen apparently aren't a
4683 performance problem because there is no noticeable performance
4684 difference between Emacs running in unibyte or multibyte mode. */
4685 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4686 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4687 maxlen, &it->len);
4688 }
4689 else
4690 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4691
4692 return success_p;
4693 }
4694
4695
4696 /* Set up IT to return characters from an ellipsis, if appropriate.
4697 The definition of the ellipsis glyphs may come from a display table
4698 entry. This function Fills IT with the first glyph from the
4699 ellipsis if an ellipsis is to be displayed. */
4700
4701 static int
4702 next_element_from_ellipsis (it)
4703 struct it *it;
4704 {
4705 if (it->selective_display_ellipsis_p)
4706 {
4707 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4708 {
4709 /* Use the display table definition for `...'. Invalid glyphs
4710 will be handled by the method returning elements from dpvec. */
4711 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4712 it->dpvec_char_len = it->len;
4713 it->dpvec = v->contents;
4714 it->dpend = v->contents + v->size;
4715 it->current.dpvec_index = 0;
4716 it->method = next_element_from_display_vector;
4717 }
4718 else
4719 {
4720 /* Use default `...' which is stored in default_invis_vector. */
4721 it->dpvec_char_len = it->len;
4722 it->dpvec = default_invis_vector;
4723 it->dpend = default_invis_vector + 3;
4724 it->current.dpvec_index = 0;
4725 it->method = next_element_from_display_vector;
4726 }
4727 }
4728 else
4729 {
4730 /* The face at the current position may be different from the
4731 face we find after the invisible text. Remember what it
4732 was in IT->saved_face_id, and signal that it's there by
4733 setting face_before_selective_p. */
4734 it->saved_face_id = it->face_id;
4735 it->method = next_element_from_buffer;
4736 reseat_at_next_visible_line_start (it, 1);
4737 it->face_before_selective_p = 1;
4738 }
4739
4740 return get_next_display_element (it);
4741 }
4742
4743
4744 /* Deliver an image display element. The iterator IT is already
4745 filled with image information (done in handle_display_prop). Value
4746 is always 1. */
4747
4748
4749 static int
4750 next_element_from_image (it)
4751 struct it *it;
4752 {
4753 it->what = IT_IMAGE;
4754 return 1;
4755 }
4756
4757
4758 /* Fill iterator IT with next display element from a stretch glyph
4759 property. IT->object is the value of the text property. Value is
4760 always 1. */
4761
4762 static int
4763 next_element_from_stretch (it)
4764 struct it *it;
4765 {
4766 it->what = IT_STRETCH;
4767 return 1;
4768 }
4769
4770
4771 /* Load IT with the next display element from current_buffer. Value
4772 is zero if end of buffer reached. IT->stop_charpos is the next
4773 position at which to stop and check for text properties or buffer
4774 end. */
4775
4776 static int
4777 next_element_from_buffer (it)
4778 struct it *it;
4779 {
4780 int success_p = 1;
4781
4782 /* Check this assumption, otherwise, we would never enter the
4783 if-statement, below. */
4784 xassert (IT_CHARPOS (*it) >= BEGV
4785 && IT_CHARPOS (*it) <= it->stop_charpos);
4786
4787 if (IT_CHARPOS (*it) >= it->stop_charpos)
4788 {
4789 if (IT_CHARPOS (*it) >= it->end_charpos)
4790 {
4791 int overlay_strings_follow_p;
4792
4793 /* End of the game, except when overlay strings follow that
4794 haven't been returned yet. */
4795 if (it->overlay_strings_at_end_processed_p)
4796 overlay_strings_follow_p = 0;
4797 else
4798 {
4799 it->overlay_strings_at_end_processed_p = 1;
4800 overlay_strings_follow_p = get_overlay_strings (it, 0);
4801 }
4802
4803 if (overlay_strings_follow_p)
4804 success_p = get_next_display_element (it);
4805 else
4806 {
4807 it->what = IT_EOB;
4808 it->position = it->current.pos;
4809 success_p = 0;
4810 }
4811 }
4812 else
4813 {
4814 handle_stop (it);
4815 return get_next_display_element (it);
4816 }
4817 }
4818 else
4819 {
4820 /* No face changes, overlays etc. in sight, so just return a
4821 character from current_buffer. */
4822 unsigned char *p;
4823
4824 /* Maybe run the redisplay end trigger hook. Performance note:
4825 This doesn't seem to cost measurable time. */
4826 if (it->redisplay_end_trigger_charpos
4827 && it->glyph_row
4828 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4829 run_redisplay_end_trigger_hook (it);
4830
4831 /* Get the next character, maybe multibyte. */
4832 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4833 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4834 {
4835 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4836 - IT_BYTEPOS (*it));
4837 it->c = string_char_and_length (p, maxlen, &it->len);
4838 }
4839 else
4840 it->c = *p, it->len = 1;
4841
4842 /* Record what we have and where it came from. */
4843 it->what = IT_CHARACTER;;
4844 it->object = it->w->buffer;
4845 it->position = it->current.pos;
4846
4847 /* Normally we return the character found above, except when we
4848 really want to return an ellipsis for selective display. */
4849 if (it->selective)
4850 {
4851 if (it->c == '\n')
4852 {
4853 /* A value of selective > 0 means hide lines indented more
4854 than that number of columns. */
4855 if (it->selective > 0
4856 && IT_CHARPOS (*it) + 1 < ZV
4857 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4858 IT_BYTEPOS (*it) + 1,
4859 it->selective))
4860 {
4861 success_p = next_element_from_ellipsis (it);
4862 it->dpvec_char_len = -1;
4863 }
4864 }
4865 else if (it->c == '\r' && it->selective == -1)
4866 {
4867 /* A value of selective == -1 means that everything from the
4868 CR to the end of the line is invisible, with maybe an
4869 ellipsis displayed for it. */
4870 success_p = next_element_from_ellipsis (it);
4871 it->dpvec_char_len = -1;
4872 }
4873 }
4874 }
4875
4876 /* Value is zero if end of buffer reached. */
4877 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4878 return success_p;
4879 }
4880
4881
4882 /* Run the redisplay end trigger hook for IT. */
4883
4884 static void
4885 run_redisplay_end_trigger_hook (it)
4886 struct it *it;
4887 {
4888 Lisp_Object args[3];
4889
4890 /* IT->glyph_row should be non-null, i.e. we should be actually
4891 displaying something, or otherwise we should not run the hook. */
4892 xassert (it->glyph_row);
4893
4894 /* Set up hook arguments. */
4895 args[0] = Qredisplay_end_trigger_functions;
4896 args[1] = it->window;
4897 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4898 it->redisplay_end_trigger_charpos = 0;
4899
4900 /* Since we are *trying* to run these functions, don't try to run
4901 them again, even if they get an error. */
4902 it->w->redisplay_end_trigger = Qnil;
4903 Frun_hook_with_args (3, args);
4904
4905 /* Notice if it changed the face of the character we are on. */
4906 handle_face_prop (it);
4907 }
4908
4909
4910 /* Deliver a composition display element. The iterator IT is already
4911 filled with composition information (done in
4912 handle_composition_prop). Value is always 1. */
4913
4914 static int
4915 next_element_from_composition (it)
4916 struct it *it;
4917 {
4918 it->what = IT_COMPOSITION;
4919 it->position = (STRINGP (it->string)
4920 ? it->current.string_pos
4921 : it->current.pos);
4922 return 1;
4923 }
4924
4925
4926 \f
4927 /***********************************************************************
4928 Moving an iterator without producing glyphs
4929 ***********************************************************************/
4930
4931 /* Move iterator IT to a specified buffer or X position within one
4932 line on the display without producing glyphs.
4933
4934 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4935 whichever is reached first.
4936
4937 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4938
4939 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4940 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4941 TO_X includes the amount by which a window is horizontally
4942 scrolled.
4943
4944 Value is
4945
4946 MOVE_POS_MATCH_OR_ZV
4947 - when TO_POS or ZV was reached.
4948
4949 MOVE_X_REACHED
4950 -when TO_X was reached before TO_POS or ZV were reached.
4951
4952 MOVE_LINE_CONTINUED
4953 - when we reached the end of the display area and the line must
4954 be continued.
4955
4956 MOVE_LINE_TRUNCATED
4957 - when we reached the end of the display area and the line is
4958 truncated.
4959
4960 MOVE_NEWLINE_OR_CR
4961 - when we stopped at a line end, i.e. a newline or a CR and selective
4962 display is on. */
4963
4964 static enum move_it_result
4965 move_it_in_display_line_to (it, to_charpos, to_x, op)
4966 struct it *it;
4967 int to_charpos, to_x, op;
4968 {
4969 enum move_it_result result = MOVE_UNDEFINED;
4970 struct glyph_row *saved_glyph_row;
4971
4972 /* Don't produce glyphs in produce_glyphs. */
4973 saved_glyph_row = it->glyph_row;
4974 it->glyph_row = NULL;
4975
4976 while (1)
4977 {
4978 int x, i, ascent = 0, descent = 0;
4979
4980 /* Stop when ZV or TO_CHARPOS reached. */
4981 if (!get_next_display_element (it)
4982 || ((op & MOVE_TO_POS) != 0
4983 && BUFFERP (it->object)
4984 && IT_CHARPOS (*it) >= to_charpos))
4985 {
4986 result = MOVE_POS_MATCH_OR_ZV;
4987 break;
4988 }
4989
4990 /* The call to produce_glyphs will get the metrics of the
4991 display element IT is loaded with. We record in x the
4992 x-position before this display element in case it does not
4993 fit on the line. */
4994 x = it->current_x;
4995
4996 /* Remember the line height so far in case the next element doesn't
4997 fit on the line. */
4998 if (!it->truncate_lines_p)
4999 {
5000 ascent = it->max_ascent;
5001 descent = it->max_descent;
5002 }
5003
5004 PRODUCE_GLYPHS (it);
5005
5006 if (it->area != TEXT_AREA)
5007 {
5008 set_iterator_to_next (it, 1);
5009 continue;
5010 }
5011
5012 /* The number of glyphs we get back in IT->nglyphs will normally
5013 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5014 character on a terminal frame, or (iii) a line end. For the
5015 second case, IT->nglyphs - 1 padding glyphs will be present
5016 (on X frames, there is only one glyph produced for a
5017 composite character.
5018
5019 The behavior implemented below means, for continuation lines,
5020 that as many spaces of a TAB as fit on the current line are
5021 displayed there. For terminal frames, as many glyphs of a
5022 multi-glyph character are displayed in the current line, too.
5023 This is what the old redisplay code did, and we keep it that
5024 way. Under X, the whole shape of a complex character must
5025 fit on the line or it will be completely displayed in the
5026 next line.
5027
5028 Note that both for tabs and padding glyphs, all glyphs have
5029 the same width. */
5030 if (it->nglyphs)
5031 {
5032 /* More than one glyph or glyph doesn't fit on line. All
5033 glyphs have the same width. */
5034 int single_glyph_width = it->pixel_width / it->nglyphs;
5035 int new_x;
5036
5037 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5038 {
5039 new_x = x + single_glyph_width;
5040
5041 /* We want to leave anything reaching TO_X to the caller. */
5042 if ((op & MOVE_TO_X) && new_x > to_x)
5043 {
5044 it->current_x = x;
5045 result = MOVE_X_REACHED;
5046 break;
5047 }
5048 else if (/* Lines are continued. */
5049 !it->truncate_lines_p
5050 && (/* And glyph doesn't fit on the line. */
5051 new_x > it->last_visible_x
5052 /* Or it fits exactly and we're on a window
5053 system frame. */
5054 || (new_x == it->last_visible_x
5055 && FRAME_WINDOW_P (it->f))))
5056 {
5057 if (/* IT->hpos == 0 means the very first glyph
5058 doesn't fit on the line, e.g. a wide image. */
5059 it->hpos == 0
5060 || (new_x == it->last_visible_x
5061 && FRAME_WINDOW_P (it->f)))
5062 {
5063 ++it->hpos;
5064 it->current_x = new_x;
5065 if (i == it->nglyphs - 1)
5066 set_iterator_to_next (it, 1);
5067 }
5068 else
5069 {
5070 it->current_x = x;
5071 it->max_ascent = ascent;
5072 it->max_descent = descent;
5073 }
5074
5075 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5076 IT_CHARPOS (*it)));
5077 result = MOVE_LINE_CONTINUED;
5078 break;
5079 }
5080 else if (new_x > it->first_visible_x)
5081 {
5082 /* Glyph is visible. Increment number of glyphs that
5083 would be displayed. */
5084 ++it->hpos;
5085 }
5086 else
5087 {
5088 /* Glyph is completely off the left margin of the display
5089 area. Nothing to do. */
5090 }
5091 }
5092
5093 if (result != MOVE_UNDEFINED)
5094 break;
5095 }
5096 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5097 {
5098 /* Stop when TO_X specified and reached. This check is
5099 necessary here because of lines consisting of a line end,
5100 only. The line end will not produce any glyphs and we
5101 would never get MOVE_X_REACHED. */
5102 xassert (it->nglyphs == 0);
5103 result = MOVE_X_REACHED;
5104 break;
5105 }
5106
5107 /* Is this a line end? If yes, we're done. */
5108 if (ITERATOR_AT_END_OF_LINE_P (it))
5109 {
5110 result = MOVE_NEWLINE_OR_CR;
5111 break;
5112 }
5113
5114 /* The current display element has been consumed. Advance
5115 to the next. */
5116 set_iterator_to_next (it, 1);
5117
5118 /* Stop if lines are truncated and IT's current x-position is
5119 past the right edge of the window now. */
5120 if (it->truncate_lines_p
5121 && it->current_x >= it->last_visible_x)
5122 {
5123 result = MOVE_LINE_TRUNCATED;
5124 break;
5125 }
5126 }
5127
5128 /* Restore the iterator settings altered at the beginning of this
5129 function. */
5130 it->glyph_row = saved_glyph_row;
5131 return result;
5132 }
5133
5134
5135 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
5136 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
5137 the description of enum move_operation_enum.
5138
5139 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5140 screen line, this function will set IT to the next position >
5141 TO_CHARPOS. */
5142
5143 void
5144 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5145 struct it *it;
5146 int to_charpos, to_x, to_y, to_vpos;
5147 int op;
5148 {
5149 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5150 int line_height;
5151 int reached = 0;
5152
5153 for (;;)
5154 {
5155 if (op & MOVE_TO_VPOS)
5156 {
5157 /* If no TO_CHARPOS and no TO_X specified, stop at the
5158 start of the line TO_VPOS. */
5159 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5160 {
5161 if (it->vpos == to_vpos)
5162 {
5163 reached = 1;
5164 break;
5165 }
5166 else
5167 skip = move_it_in_display_line_to (it, -1, -1, 0);
5168 }
5169 else
5170 {
5171 /* TO_VPOS >= 0 means stop at TO_X in the line at
5172 TO_VPOS, or at TO_POS, whichever comes first. */
5173 if (it->vpos == to_vpos)
5174 {
5175 reached = 2;
5176 break;
5177 }
5178
5179 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5180
5181 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5182 {
5183 reached = 3;
5184 break;
5185 }
5186 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5187 {
5188 /* We have reached TO_X but not in the line we want. */
5189 skip = move_it_in_display_line_to (it, to_charpos,
5190 -1, MOVE_TO_POS);
5191 if (skip == MOVE_POS_MATCH_OR_ZV)
5192 {
5193 reached = 4;
5194 break;
5195 }
5196 }
5197 }
5198 }
5199 else if (op & MOVE_TO_Y)
5200 {
5201 struct it it_backup;
5202
5203 /* TO_Y specified means stop at TO_X in the line containing
5204 TO_Y---or at TO_CHARPOS if this is reached first. The
5205 problem is that we can't really tell whether the line
5206 contains TO_Y before we have completely scanned it, and
5207 this may skip past TO_X. What we do is to first scan to
5208 TO_X.
5209
5210 If TO_X is not specified, use a TO_X of zero. The reason
5211 is to make the outcome of this function more predictable.
5212 If we didn't use TO_X == 0, we would stop at the end of
5213 the line which is probably not what a caller would expect
5214 to happen. */
5215 skip = move_it_in_display_line_to (it, to_charpos,
5216 ((op & MOVE_TO_X)
5217 ? to_x : 0),
5218 (MOVE_TO_X
5219 | (op & MOVE_TO_POS)));
5220
5221 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5222 if (skip == MOVE_POS_MATCH_OR_ZV)
5223 {
5224 reached = 5;
5225 break;
5226 }
5227
5228 /* If TO_X was reached, we would like to know whether TO_Y
5229 is in the line. This can only be said if we know the
5230 total line height which requires us to scan the rest of
5231 the line. */
5232 if (skip == MOVE_X_REACHED)
5233 {
5234 it_backup = *it;
5235 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5236 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5237 op & MOVE_TO_POS);
5238 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5239 }
5240
5241 /* Now, decide whether TO_Y is in this line. */
5242 line_height = it->max_ascent + it->max_descent;
5243 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5244
5245 if (to_y >= it->current_y
5246 && to_y < it->current_y + line_height)
5247 {
5248 if (skip == MOVE_X_REACHED)
5249 /* If TO_Y is in this line and TO_X was reached above,
5250 we scanned too far. We have to restore IT's settings
5251 to the ones before skipping. */
5252 *it = it_backup;
5253 reached = 6;
5254 }
5255 else if (skip == MOVE_X_REACHED)
5256 {
5257 skip = skip2;
5258 if (skip == MOVE_POS_MATCH_OR_ZV)
5259 reached = 7;
5260 }
5261
5262 if (reached)
5263 break;
5264 }
5265 else
5266 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5267
5268 switch (skip)
5269 {
5270 case MOVE_POS_MATCH_OR_ZV:
5271 reached = 8;
5272 goto out;
5273
5274 case MOVE_NEWLINE_OR_CR:
5275 set_iterator_to_next (it, 1);
5276 it->continuation_lines_width = 0;
5277 break;
5278
5279 case MOVE_LINE_TRUNCATED:
5280 it->continuation_lines_width = 0;
5281 reseat_at_next_visible_line_start (it, 0);
5282 if ((op & MOVE_TO_POS) != 0
5283 && IT_CHARPOS (*it) > to_charpos)
5284 {
5285 reached = 9;
5286 goto out;
5287 }
5288 break;
5289
5290 case MOVE_LINE_CONTINUED:
5291 it->continuation_lines_width += it->current_x;
5292 break;
5293
5294 default:
5295 abort ();
5296 }
5297
5298 /* Reset/increment for the next run. */
5299 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5300 it->current_x = it->hpos = 0;
5301 it->current_y += it->max_ascent + it->max_descent;
5302 ++it->vpos;
5303 last_height = it->max_ascent + it->max_descent;
5304 last_max_ascent = it->max_ascent;
5305 it->max_ascent = it->max_descent = 0;
5306 }
5307
5308 out:
5309
5310 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5311 }
5312
5313
5314 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5315
5316 If DY > 0, move IT backward at least that many pixels. DY = 0
5317 means move IT backward to the preceding line start or BEGV. This
5318 function may move over more than DY pixels if IT->current_y - DY
5319 ends up in the middle of a line; in this case IT->current_y will be
5320 set to the top of the line moved to. */
5321
5322 void
5323 move_it_vertically_backward (it, dy)
5324 struct it *it;
5325 int dy;
5326 {
5327 int nlines, h;
5328 struct it it2, it3;
5329 int start_pos = IT_CHARPOS (*it);
5330
5331 xassert (dy >= 0);
5332
5333 /* Estimate how many newlines we must move back. */
5334 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5335
5336 /* Set the iterator's position that many lines back. */
5337 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5338 back_to_previous_visible_line_start (it);
5339
5340 /* Reseat the iterator here. When moving backward, we don't want
5341 reseat to skip forward over invisible text, set up the iterator
5342 to deliver from overlay strings at the new position etc. So,
5343 use reseat_1 here. */
5344 reseat_1 (it, it->current.pos, 1);
5345
5346 /* We are now surely at a line start. */
5347 it->current_x = it->hpos = 0;
5348
5349 /* Move forward and see what y-distance we moved. First move to the
5350 start of the next line so that we get its height. We need this
5351 height to be able to tell whether we reached the specified
5352 y-distance. */
5353 it2 = *it;
5354 it2.max_ascent = it2.max_descent = 0;
5355 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5356 MOVE_TO_POS | MOVE_TO_VPOS);
5357 xassert (IT_CHARPOS (*it) >= BEGV);
5358 it3 = it2;
5359
5360 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5361 xassert (IT_CHARPOS (*it) >= BEGV);
5362 h = it2.current_y - it->current_y;
5363 nlines = it2.vpos - it->vpos;
5364
5365 /* Correct IT's y and vpos position. */
5366 it->vpos -= nlines;
5367 it->current_y -= h;
5368
5369 if (dy == 0)
5370 {
5371 /* DY == 0 means move to the start of the screen line. The
5372 value of nlines is > 0 if continuation lines were involved. */
5373 if (nlines > 0)
5374 move_it_by_lines (it, nlines, 1);
5375 xassert (IT_CHARPOS (*it) <= start_pos);
5376 }
5377 else if (nlines)
5378 {
5379 /* The y-position we try to reach. Note that h has been
5380 subtracted in front of the if-statement. */
5381 int target_y = it->current_y + h - dy;
5382 int y0 = it3.current_y;
5383 int y1 = line_bottom_y (&it3);
5384 int line_height = y1 - y0;
5385
5386 /* If we did not reach target_y, try to move further backward if
5387 we can. If we moved too far backward, try to move forward. */
5388 if (target_y < it->current_y
5389 /* This is heuristic. In a window that's 3 lines high, with
5390 a line height of 13 pixels each, recentering with point
5391 on the bottom line will try to move -39/2 = 19 pixels
5392 backward. Try to avoid moving into the first line. */
5393 && it->current_y - target_y > line_height / 3 * 2
5394 && IT_CHARPOS (*it) > BEGV)
5395 {
5396 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5397 target_y - it->current_y));
5398 move_it_vertically (it, target_y - it->current_y);
5399 xassert (IT_CHARPOS (*it) >= BEGV);
5400 }
5401 else if (target_y >= it->current_y + line_height
5402 && IT_CHARPOS (*it) < ZV)
5403 {
5404 /* Should move forward by at least one line, maybe more. */
5405 do
5406 {
5407 move_it_by_lines (it, 1, 1);
5408 }
5409 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5410
5411 xassert (IT_CHARPOS (*it) >= BEGV);
5412 }
5413 }
5414 }
5415
5416
5417 /* Move IT by a specified amount of pixel lines DY. DY negative means
5418 move backwards. DY = 0 means move to start of screen line. At the
5419 end, IT will be on the start of a screen line. */
5420
5421 void
5422 move_it_vertically (it, dy)
5423 struct it *it;
5424 int dy;
5425 {
5426 if (dy <= 0)
5427 move_it_vertically_backward (it, -dy);
5428 else if (dy > 0)
5429 {
5430 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5431 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5432 MOVE_TO_POS | MOVE_TO_Y);
5433 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5434
5435 /* If buffer ends in ZV without a newline, move to the start of
5436 the line to satisfy the post-condition. */
5437 if (IT_CHARPOS (*it) == ZV
5438 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5439 move_it_by_lines (it, 0, 0);
5440 }
5441 }
5442
5443
5444 /* Move iterator IT past the end of the text line it is in. */
5445
5446 void
5447 move_it_past_eol (it)
5448 struct it *it;
5449 {
5450 enum move_it_result rc;
5451
5452 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5453 if (rc == MOVE_NEWLINE_OR_CR)
5454 set_iterator_to_next (it, 0);
5455 }
5456
5457
5458 #if 0 /* Currently not used. */
5459
5460 /* Return non-zero if some text between buffer positions START_CHARPOS
5461 and END_CHARPOS is invisible. IT->window is the window for text
5462 property lookup. */
5463
5464 static int
5465 invisible_text_between_p (it, start_charpos, end_charpos)
5466 struct it *it;
5467 int start_charpos, end_charpos;
5468 {
5469 Lisp_Object prop, limit;
5470 int invisible_found_p;
5471
5472 xassert (it != NULL && start_charpos <= end_charpos);
5473
5474 /* Is text at START invisible? */
5475 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5476 it->window);
5477 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5478 invisible_found_p = 1;
5479 else
5480 {
5481 limit = Fnext_single_char_property_change (make_number (start_charpos),
5482 Qinvisible, Qnil,
5483 make_number (end_charpos));
5484 invisible_found_p = XFASTINT (limit) < end_charpos;
5485 }
5486
5487 return invisible_found_p;
5488 }
5489
5490 #endif /* 0 */
5491
5492
5493 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5494 negative means move up. DVPOS == 0 means move to the start of the
5495 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5496 NEED_Y_P is zero, IT->current_y will be left unchanged.
5497
5498 Further optimization ideas: If we would know that IT->f doesn't use
5499 a face with proportional font, we could be faster for
5500 truncate-lines nil. */
5501
5502 void
5503 move_it_by_lines (it, dvpos, need_y_p)
5504 struct it *it;
5505 int dvpos, need_y_p;
5506 {
5507 struct position pos;
5508
5509 if (!FRAME_WINDOW_P (it->f))
5510 {
5511 struct text_pos textpos;
5512
5513 /* We can use vmotion on frames without proportional fonts. */
5514 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5515 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5516 reseat (it, textpos, 1);
5517 it->vpos += pos.vpos;
5518 it->current_y += pos.vpos;
5519 }
5520 else if (dvpos == 0)
5521 {
5522 /* DVPOS == 0 means move to the start of the screen line. */
5523 move_it_vertically_backward (it, 0);
5524 xassert (it->current_x == 0 && it->hpos == 0);
5525 }
5526 else if (dvpos > 0)
5527 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5528 else
5529 {
5530 struct it it2;
5531 int start_charpos, i;
5532
5533 /* Start at the beginning of the screen line containing IT's
5534 position. */
5535 move_it_vertically_backward (it, 0);
5536
5537 /* Go back -DVPOS visible lines and reseat the iterator there. */
5538 start_charpos = IT_CHARPOS (*it);
5539 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5540 back_to_previous_visible_line_start (it);
5541 reseat (it, it->current.pos, 1);
5542 it->current_x = it->hpos = 0;
5543
5544 /* Above call may have moved too far if continuation lines
5545 are involved. Scan forward and see if it did. */
5546 it2 = *it;
5547 it2.vpos = it2.current_y = 0;
5548 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5549 it->vpos -= it2.vpos;
5550 it->current_y -= it2.current_y;
5551 it->current_x = it->hpos = 0;
5552
5553 /* If we moved too far, move IT some lines forward. */
5554 if (it2.vpos > -dvpos)
5555 {
5556 int delta = it2.vpos + dvpos;
5557 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5558 }
5559 }
5560 }
5561
5562
5563 \f
5564 /***********************************************************************
5565 Messages
5566 ***********************************************************************/
5567
5568
5569 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5570 to *Messages*. */
5571
5572 void
5573 add_to_log (format, arg1, arg2)
5574 char *format;
5575 Lisp_Object arg1, arg2;
5576 {
5577 Lisp_Object args[3];
5578 Lisp_Object msg, fmt;
5579 char *buffer;
5580 int len;
5581 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5582
5583 /* Do nothing if called asynchronously. Inserting text into
5584 a buffer may call after-change-functions and alike and
5585 that would means running Lisp asynchronously. */
5586 if (handling_signal)
5587 return;
5588
5589 fmt = msg = Qnil;
5590 GCPRO4 (fmt, msg, arg1, arg2);
5591
5592 args[0] = fmt = build_string (format);
5593 args[1] = arg1;
5594 args[2] = arg2;
5595 msg = Fformat (3, args);
5596
5597 len = STRING_BYTES (XSTRING (msg)) + 1;
5598 buffer = (char *) alloca (len);
5599 bcopy (XSTRING (msg)->data, buffer, len);
5600
5601 message_dolog (buffer, len - 1, 1, 0);
5602 UNGCPRO;
5603 }
5604
5605
5606 /* Output a newline in the *Messages* buffer if "needs" one. */
5607
5608 void
5609 message_log_maybe_newline ()
5610 {
5611 if (message_log_need_newline)
5612 message_dolog ("", 0, 1, 0);
5613 }
5614
5615
5616 /* Add a string M of length NBYTES to the message log, optionally
5617 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5618 nonzero, means interpret the contents of M as multibyte. This
5619 function calls low-level routines in order to bypass text property
5620 hooks, etc. which might not be safe to run. */
5621
5622 void
5623 message_dolog (m, nbytes, nlflag, multibyte)
5624 char *m;
5625 int nbytes, nlflag, multibyte;
5626 {
5627 if (!NILP (Vmessage_log_max))
5628 {
5629 struct buffer *oldbuf;
5630 Lisp_Object oldpoint, oldbegv, oldzv;
5631 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5632 int point_at_end = 0;
5633 int zv_at_end = 0;
5634 Lisp_Object old_deactivate_mark, tem;
5635 struct gcpro gcpro1;
5636
5637 old_deactivate_mark = Vdeactivate_mark;
5638 oldbuf = current_buffer;
5639 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5640 current_buffer->undo_list = Qt;
5641
5642 oldpoint = message_dolog_marker1;
5643 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5644 oldbegv = message_dolog_marker2;
5645 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5646 oldzv = message_dolog_marker3;
5647 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5648 GCPRO1 (old_deactivate_mark);
5649
5650 if (PT == Z)
5651 point_at_end = 1;
5652 if (ZV == Z)
5653 zv_at_end = 1;
5654
5655 BEGV = BEG;
5656 BEGV_BYTE = BEG_BYTE;
5657 ZV = Z;
5658 ZV_BYTE = Z_BYTE;
5659 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5660
5661 /* Insert the string--maybe converting multibyte to single byte
5662 or vice versa, so that all the text fits the buffer. */
5663 if (multibyte
5664 && NILP (current_buffer->enable_multibyte_characters))
5665 {
5666 int i, c, char_bytes;
5667 unsigned char work[1];
5668
5669 /* Convert a multibyte string to single-byte
5670 for the *Message* buffer. */
5671 for (i = 0; i < nbytes; i += nbytes)
5672 {
5673 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5674 work[0] = (SINGLE_BYTE_CHAR_P (c)
5675 ? c
5676 : multibyte_char_to_unibyte (c, Qnil));
5677 insert_1_both (work, 1, 1, 1, 0, 0);
5678 }
5679 }
5680 else if (! multibyte
5681 && ! NILP (current_buffer->enable_multibyte_characters))
5682 {
5683 int i, c, char_bytes;
5684 unsigned char *msg = (unsigned char *) m;
5685 unsigned char str[MAX_MULTIBYTE_LENGTH];
5686 /* Convert a single-byte string to multibyte
5687 for the *Message* buffer. */
5688 for (i = 0; i < nbytes; i++)
5689 {
5690 c = unibyte_char_to_multibyte (msg[i]);
5691 char_bytes = CHAR_STRING (c, str);
5692 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5693 }
5694 }
5695 else if (nbytes)
5696 insert_1 (m, nbytes, 1, 0, 0);
5697
5698 if (nlflag)
5699 {
5700 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5701 insert_1 ("\n", 1, 1, 0, 0);
5702
5703 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5704 this_bol = PT;
5705 this_bol_byte = PT_BYTE;
5706
5707 /* See if this line duplicates the previous one.
5708 If so, combine duplicates. */
5709 if (this_bol > BEG)
5710 {
5711 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5712 prev_bol = PT;
5713 prev_bol_byte = PT_BYTE;
5714
5715 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5716 this_bol, this_bol_byte);
5717 if (dup)
5718 {
5719 del_range_both (prev_bol, prev_bol_byte,
5720 this_bol, this_bol_byte, 0);
5721 if (dup > 1)
5722 {
5723 char dupstr[40];
5724 int duplen;
5725
5726 /* If you change this format, don't forget to also
5727 change message_log_check_duplicate. */
5728 sprintf (dupstr, " [%d times]", dup);
5729 duplen = strlen (dupstr);
5730 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5731 insert_1 (dupstr, duplen, 1, 0, 1);
5732 }
5733 }
5734 }
5735
5736 /* If we have more than the desired maximum number of lines
5737 in the *Messages* buffer now, delete the oldest ones.
5738 This is safe because we don't have undo in this buffer. */
5739
5740 if (NATNUMP (Vmessage_log_max))
5741 {
5742 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5743 -XFASTINT (Vmessage_log_max) - 1, 0);
5744 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5745 }
5746 }
5747 BEGV = XMARKER (oldbegv)->charpos;
5748 BEGV_BYTE = marker_byte_position (oldbegv);
5749
5750 if (zv_at_end)
5751 {
5752 ZV = Z;
5753 ZV_BYTE = Z_BYTE;
5754 }
5755 else
5756 {
5757 ZV = XMARKER (oldzv)->charpos;
5758 ZV_BYTE = marker_byte_position (oldzv);
5759 }
5760
5761 if (point_at_end)
5762 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5763 else
5764 /* We can't do Fgoto_char (oldpoint) because it will run some
5765 Lisp code. */
5766 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5767 XMARKER (oldpoint)->bytepos);
5768
5769 UNGCPRO;
5770 unchain_marker (oldpoint);
5771 unchain_marker (oldbegv);
5772 unchain_marker (oldzv);
5773
5774 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5775 set_buffer_internal (oldbuf);
5776 if (NILP (tem))
5777 windows_or_buffers_changed = old_windows_or_buffers_changed;
5778 message_log_need_newline = !nlflag;
5779 Vdeactivate_mark = old_deactivate_mark;
5780 }
5781 }
5782
5783
5784 /* We are at the end of the buffer after just having inserted a newline.
5785 (Note: We depend on the fact we won't be crossing the gap.)
5786 Check to see if the most recent message looks a lot like the previous one.
5787 Return 0 if different, 1 if the new one should just replace it, or a
5788 value N > 1 if we should also append " [N times]". */
5789
5790 static int
5791 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5792 int prev_bol, this_bol;
5793 int prev_bol_byte, this_bol_byte;
5794 {
5795 int i;
5796 int len = Z_BYTE - 1 - this_bol_byte;
5797 int seen_dots = 0;
5798 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5799 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5800
5801 for (i = 0; i < len; i++)
5802 {
5803 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5804 seen_dots = 1;
5805 if (p1[i] != p2[i])
5806 return seen_dots;
5807 }
5808 p1 += len;
5809 if (*p1 == '\n')
5810 return 2;
5811 if (*p1++ == ' ' && *p1++ == '[')
5812 {
5813 int n = 0;
5814 while (*p1 >= '0' && *p1 <= '9')
5815 n = n * 10 + *p1++ - '0';
5816 if (strncmp (p1, " times]\n", 8) == 0)
5817 return n+1;
5818 }
5819 return 0;
5820 }
5821
5822
5823 /* Display an echo area message M with a specified length of NBYTES
5824 bytes. The string may include null characters. If M is 0, clear
5825 out any existing message, and let the mini-buffer text show
5826 through.
5827
5828 The buffer M must continue to exist until after the echo area gets
5829 cleared or some other message gets displayed there. This means do
5830 not pass text that is stored in a Lisp string; do not pass text in
5831 a buffer that was alloca'd. */
5832
5833 void
5834 message2 (m, nbytes, multibyte)
5835 char *m;
5836 int nbytes;
5837 int multibyte;
5838 {
5839 /* First flush out any partial line written with print. */
5840 message_log_maybe_newline ();
5841 if (m)
5842 message_dolog (m, nbytes, 1, multibyte);
5843 message2_nolog (m, nbytes, multibyte);
5844 }
5845
5846
5847 /* The non-logging counterpart of message2. */
5848
5849 void
5850 message2_nolog (m, nbytes, multibyte)
5851 char *m;
5852 int nbytes;
5853 {
5854 struct frame *sf = SELECTED_FRAME ();
5855 message_enable_multibyte = multibyte;
5856
5857 if (noninteractive)
5858 {
5859 if (noninteractive_need_newline)
5860 putc ('\n', stderr);
5861 noninteractive_need_newline = 0;
5862 if (m)
5863 fwrite (m, nbytes, 1, stderr);
5864 if (cursor_in_echo_area == 0)
5865 fprintf (stderr, "\n");
5866 fflush (stderr);
5867 }
5868 /* A null message buffer means that the frame hasn't really been
5869 initialized yet. Error messages get reported properly by
5870 cmd_error, so this must be just an informative message; toss it. */
5871 else if (INTERACTIVE
5872 && sf->glyphs_initialized_p
5873 && FRAME_MESSAGE_BUF (sf))
5874 {
5875 Lisp_Object mini_window;
5876 struct frame *f;
5877
5878 /* Get the frame containing the mini-buffer
5879 that the selected frame is using. */
5880 mini_window = FRAME_MINIBUF_WINDOW (sf);
5881 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5882
5883 FRAME_SAMPLE_VISIBILITY (f);
5884 if (FRAME_VISIBLE_P (sf)
5885 && ! FRAME_VISIBLE_P (f))
5886 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5887
5888 if (m)
5889 {
5890 set_message (m, Qnil, nbytes, multibyte);
5891 if (minibuffer_auto_raise)
5892 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5893 }
5894 else
5895 clear_message (1, 1);
5896
5897 do_pending_window_change (0);
5898 echo_area_display (1);
5899 do_pending_window_change (0);
5900 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5901 (*frame_up_to_date_hook) (f);
5902 }
5903 }
5904
5905
5906 /* Display an echo area message M with a specified length of NBYTES
5907 bytes. The string may include null characters. If M is not a
5908 string, clear out any existing message, and let the mini-buffer
5909 text show through. */
5910
5911 void
5912 message3 (m, nbytes, multibyte)
5913 Lisp_Object m;
5914 int nbytes;
5915 int multibyte;
5916 {
5917 struct gcpro gcpro1;
5918
5919 GCPRO1 (m);
5920
5921 /* First flush out any partial line written with print. */
5922 message_log_maybe_newline ();
5923 if (STRINGP (m))
5924 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5925 message3_nolog (m, nbytes, multibyte);
5926
5927 UNGCPRO;
5928 }
5929
5930
5931 /* The non-logging version of message3. */
5932
5933 void
5934 message3_nolog (m, nbytes, multibyte)
5935 Lisp_Object m;
5936 int nbytes, multibyte;
5937 {
5938 struct frame *sf = SELECTED_FRAME ();
5939 message_enable_multibyte = multibyte;
5940
5941 if (noninteractive)
5942 {
5943 if (noninteractive_need_newline)
5944 putc ('\n', stderr);
5945 noninteractive_need_newline = 0;
5946 if (STRINGP (m))
5947 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5948 if (cursor_in_echo_area == 0)
5949 fprintf (stderr, "\n");
5950 fflush (stderr);
5951 }
5952 /* A null message buffer means that the frame hasn't really been
5953 initialized yet. Error messages get reported properly by
5954 cmd_error, so this must be just an informative message; toss it. */
5955 else if (INTERACTIVE
5956 && sf->glyphs_initialized_p
5957 && FRAME_MESSAGE_BUF (sf))
5958 {
5959 Lisp_Object mini_window;
5960 Lisp_Object frame;
5961 struct frame *f;
5962
5963 /* Get the frame containing the mini-buffer
5964 that the selected frame is using. */
5965 mini_window = FRAME_MINIBUF_WINDOW (sf);
5966 frame = XWINDOW (mini_window)->frame;
5967 f = XFRAME (frame);
5968
5969 FRAME_SAMPLE_VISIBILITY (f);
5970 if (FRAME_VISIBLE_P (sf)
5971 && !FRAME_VISIBLE_P (f))
5972 Fmake_frame_visible (frame);
5973
5974 if (STRINGP (m) && XSTRING (m)->size)
5975 {
5976 set_message (NULL, m, nbytes, multibyte);
5977 if (minibuffer_auto_raise)
5978 Fraise_frame (frame);
5979 }
5980 else
5981 clear_message (1, 1);
5982
5983 do_pending_window_change (0);
5984 echo_area_display (1);
5985 do_pending_window_change (0);
5986 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5987 (*frame_up_to_date_hook) (f);
5988 }
5989 }
5990
5991
5992 /* Display a null-terminated echo area message M. If M is 0, clear
5993 out any existing message, and let the mini-buffer text show through.
5994
5995 The buffer M must continue to exist until after the echo area gets
5996 cleared or some other message gets displayed there. Do not pass
5997 text that is stored in a Lisp string. Do not pass text in a buffer
5998 that was alloca'd. */
5999
6000 void
6001 message1 (m)
6002 char *m;
6003 {
6004 message2 (m, (m ? strlen (m) : 0), 0);
6005 }
6006
6007
6008 /* The non-logging counterpart of message1. */
6009
6010 void
6011 message1_nolog (m)
6012 char *m;
6013 {
6014 message2_nolog (m, (m ? strlen (m) : 0), 0);
6015 }
6016
6017 /* Display a message M which contains a single %s
6018 which gets replaced with STRING. */
6019
6020 void
6021 message_with_string (m, string, log)
6022 char *m;
6023 Lisp_Object string;
6024 int log;
6025 {
6026 if (noninteractive)
6027 {
6028 if (m)
6029 {
6030 if (noninteractive_need_newline)
6031 putc ('\n', stderr);
6032 noninteractive_need_newline = 0;
6033 fprintf (stderr, m, XSTRING (string)->data);
6034 if (cursor_in_echo_area == 0)
6035 fprintf (stderr, "\n");
6036 fflush (stderr);
6037 }
6038 }
6039 else if (INTERACTIVE)
6040 {
6041 /* The frame whose minibuffer we're going to display the message on.
6042 It may be larger than the selected frame, so we need
6043 to use its buffer, not the selected frame's buffer. */
6044 Lisp_Object mini_window;
6045 struct frame *f, *sf = SELECTED_FRAME ();
6046
6047 /* Get the frame containing the minibuffer
6048 that the selected frame is using. */
6049 mini_window = FRAME_MINIBUF_WINDOW (sf);
6050 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6051
6052 /* A null message buffer means that the frame hasn't really been
6053 initialized yet. Error messages get reported properly by
6054 cmd_error, so this must be just an informative message; toss it. */
6055 if (FRAME_MESSAGE_BUF (f))
6056 {
6057 int len;
6058 char *a[1];
6059 a[0] = (char *) XSTRING (string)->data;
6060
6061 len = doprnt (FRAME_MESSAGE_BUF (f),
6062 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6063
6064 if (log)
6065 message2 (FRAME_MESSAGE_BUF (f), len,
6066 STRING_MULTIBYTE (string));
6067 else
6068 message2_nolog (FRAME_MESSAGE_BUF (f), len,
6069 STRING_MULTIBYTE (string));
6070
6071 /* Print should start at the beginning of the message
6072 buffer next time. */
6073 message_buf_print = 0;
6074 }
6075 }
6076 }
6077
6078
6079 /* Dump an informative message to the minibuf. If M is 0, clear out
6080 any existing message, and let the mini-buffer text show through. */
6081
6082 /* VARARGS 1 */
6083 void
6084 message (m, a1, a2, a3)
6085 char *m;
6086 EMACS_INT a1, a2, a3;
6087 {
6088 if (noninteractive)
6089 {
6090 if (m)
6091 {
6092 if (noninteractive_need_newline)
6093 putc ('\n', stderr);
6094 noninteractive_need_newline = 0;
6095 fprintf (stderr, m, a1, a2, a3);
6096 if (cursor_in_echo_area == 0)
6097 fprintf (stderr, "\n");
6098 fflush (stderr);
6099 }
6100 }
6101 else if (INTERACTIVE)
6102 {
6103 /* The frame whose mini-buffer we're going to display the message
6104 on. It may be larger than the selected frame, so we need to
6105 use its buffer, not the selected frame's buffer. */
6106 Lisp_Object mini_window;
6107 struct frame *f, *sf = SELECTED_FRAME ();
6108
6109 /* Get the frame containing the mini-buffer
6110 that the selected frame is using. */
6111 mini_window = FRAME_MINIBUF_WINDOW (sf);
6112 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6113
6114 /* A null message buffer means that the frame hasn't really been
6115 initialized yet. Error messages get reported properly by
6116 cmd_error, so this must be just an informative message; toss
6117 it. */
6118 if (FRAME_MESSAGE_BUF (f))
6119 {
6120 if (m)
6121 {
6122 int len;
6123 #ifdef NO_ARG_ARRAY
6124 char *a[3];
6125 a[0] = (char *) a1;
6126 a[1] = (char *) a2;
6127 a[2] = (char *) a3;
6128
6129 len = doprnt (FRAME_MESSAGE_BUF (f),
6130 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6131 #else
6132 len = doprnt (FRAME_MESSAGE_BUF (f),
6133 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6134 (char **) &a1);
6135 #endif /* NO_ARG_ARRAY */
6136
6137 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6138 }
6139 else
6140 message1 (0);
6141
6142 /* Print should start at the beginning of the message
6143 buffer next time. */
6144 message_buf_print = 0;
6145 }
6146 }
6147 }
6148
6149
6150 /* The non-logging version of message. */
6151
6152 void
6153 message_nolog (m, a1, a2, a3)
6154 char *m;
6155 EMACS_INT a1, a2, a3;
6156 {
6157 Lisp_Object old_log_max;
6158 old_log_max = Vmessage_log_max;
6159 Vmessage_log_max = Qnil;
6160 message (m, a1, a2, a3);
6161 Vmessage_log_max = old_log_max;
6162 }
6163
6164
6165 /* Display the current message in the current mini-buffer. This is
6166 only called from error handlers in process.c, and is not time
6167 critical. */
6168
6169 void
6170 update_echo_area ()
6171 {
6172 if (!NILP (echo_area_buffer[0]))
6173 {
6174 Lisp_Object string;
6175 string = Fcurrent_message ();
6176 message3 (string, XSTRING (string)->size,
6177 !NILP (current_buffer->enable_multibyte_characters));
6178 }
6179 }
6180
6181
6182 /* Make sure echo area buffers in echo_buffers[] are life. If they
6183 aren't, make new ones. */
6184
6185 static void
6186 ensure_echo_area_buffers ()
6187 {
6188 int i;
6189
6190 for (i = 0; i < 2; ++i)
6191 if (!BUFFERP (echo_buffer[i])
6192 || NILP (XBUFFER (echo_buffer[i])->name))
6193 {
6194 char name[30];
6195 Lisp_Object old_buffer;
6196 int j;
6197
6198 old_buffer = echo_buffer[i];
6199 sprintf (name, " *Echo Area %d*", i);
6200 echo_buffer[i] = Fget_buffer_create (build_string (name));
6201 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6202
6203 for (j = 0; j < 2; ++j)
6204 if (EQ (old_buffer, echo_area_buffer[j]))
6205 echo_area_buffer[j] = echo_buffer[i];
6206 }
6207 }
6208
6209
6210 /* Call FN with args A1..A4 with either the current or last displayed
6211 echo_area_buffer as current buffer.
6212
6213 WHICH zero means use the current message buffer
6214 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6215 from echo_buffer[] and clear it.
6216
6217 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6218 suitable buffer from echo_buffer[] and clear it.
6219
6220 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6221 that the current message becomes the last displayed one, make
6222 choose a suitable buffer for echo_area_buffer[0], and clear it.
6223
6224 Value is what FN returns. */
6225
6226 static int
6227 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6228 struct window *w;
6229 int which;
6230 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6231 EMACS_INT a1;
6232 Lisp_Object a2;
6233 EMACS_INT a3, a4;
6234 {
6235 Lisp_Object buffer;
6236 int this_one, the_other, clear_buffer_p, rc;
6237 int count = BINDING_STACK_SIZE ();
6238
6239 /* If buffers aren't life, make new ones. */
6240 ensure_echo_area_buffers ();
6241
6242 clear_buffer_p = 0;
6243
6244 if (which == 0)
6245 this_one = 0, the_other = 1;
6246 else if (which > 0)
6247 this_one = 1, the_other = 0;
6248 else
6249 {
6250 this_one = 0, the_other = 1;
6251 clear_buffer_p = 1;
6252
6253 /* We need a fresh one in case the current echo buffer equals
6254 the one containing the last displayed echo area message. */
6255 if (!NILP (echo_area_buffer[this_one])
6256 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6257 echo_area_buffer[this_one] = Qnil;
6258 }
6259
6260 /* Choose a suitable buffer from echo_buffer[] is we don't
6261 have one. */
6262 if (NILP (echo_area_buffer[this_one]))
6263 {
6264 echo_area_buffer[this_one]
6265 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6266 ? echo_buffer[the_other]
6267 : echo_buffer[this_one]);
6268 clear_buffer_p = 1;
6269 }
6270
6271 buffer = echo_area_buffer[this_one];
6272
6273 /* Don't get confused by reusing the buffer used for echoing
6274 for a different purpose. */
6275 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6276 cancel_echoing ();
6277
6278 record_unwind_protect (unwind_with_echo_area_buffer,
6279 with_echo_area_buffer_unwind_data (w));
6280
6281 /* Make the echo area buffer current. Note that for display
6282 purposes, it is not necessary that the displayed window's buffer
6283 == current_buffer, except for text property lookup. So, let's
6284 only set that buffer temporarily here without doing a full
6285 Fset_window_buffer. We must also change w->pointm, though,
6286 because otherwise an assertions in unshow_buffer fails, and Emacs
6287 aborts. */
6288 set_buffer_internal_1 (XBUFFER (buffer));
6289 if (w)
6290 {
6291 w->buffer = buffer;
6292 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6293 }
6294
6295 current_buffer->undo_list = Qt;
6296 current_buffer->read_only = Qnil;
6297 specbind (Qinhibit_read_only, Qt);
6298 specbind (Qinhibit_modification_hooks, Qt);
6299
6300 if (clear_buffer_p && Z > BEG)
6301 del_range (BEG, Z);
6302
6303 xassert (BEGV >= BEG);
6304 xassert (ZV <= Z && ZV >= BEGV);
6305
6306 rc = fn (a1, a2, a3, a4);
6307
6308 xassert (BEGV >= BEG);
6309 xassert (ZV <= Z && ZV >= BEGV);
6310
6311 unbind_to (count, Qnil);
6312 return rc;
6313 }
6314
6315
6316 /* Save state that should be preserved around the call to the function
6317 FN called in with_echo_area_buffer. */
6318
6319 static Lisp_Object
6320 with_echo_area_buffer_unwind_data (w)
6321 struct window *w;
6322 {
6323 int i = 0;
6324 Lisp_Object vector;
6325
6326 /* Reduce consing by keeping one vector in
6327 Vwith_echo_area_save_vector. */
6328 vector = Vwith_echo_area_save_vector;
6329 Vwith_echo_area_save_vector = Qnil;
6330
6331 if (NILP (vector))
6332 vector = Fmake_vector (make_number (7), Qnil);
6333
6334 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6335 AREF (vector, i) = Vdeactivate_mark, ++i;
6336 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6337
6338 if (w)
6339 {
6340 XSETWINDOW (AREF (vector, i), w); ++i;
6341 AREF (vector, i) = w->buffer; ++i;
6342 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6343 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6344 }
6345 else
6346 {
6347 int end = i + 4;
6348 for (; i < end; ++i)
6349 AREF (vector, i) = Qnil;
6350 }
6351
6352 xassert (i == ASIZE (vector));
6353 return vector;
6354 }
6355
6356
6357 /* Restore global state from VECTOR which was created by
6358 with_echo_area_buffer_unwind_data. */
6359
6360 static Lisp_Object
6361 unwind_with_echo_area_buffer (vector)
6362 Lisp_Object vector;
6363 {
6364 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6365 Vdeactivate_mark = AREF (vector, 1);
6366 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6367
6368 if (WINDOWP (AREF (vector, 3)))
6369 {
6370 struct window *w;
6371 Lisp_Object buffer, charpos, bytepos;
6372
6373 w = XWINDOW (AREF (vector, 3));
6374 buffer = AREF (vector, 4);
6375 charpos = AREF (vector, 5);
6376 bytepos = AREF (vector, 6);
6377
6378 w->buffer = buffer;
6379 set_marker_both (w->pointm, buffer,
6380 XFASTINT (charpos), XFASTINT (bytepos));
6381 }
6382
6383 Vwith_echo_area_save_vector = vector;
6384 return Qnil;
6385 }
6386
6387
6388 /* Set up the echo area for use by print functions. MULTIBYTE_P
6389 non-zero means we will print multibyte. */
6390
6391 void
6392 setup_echo_area_for_printing (multibyte_p)
6393 int multibyte_p;
6394 {
6395 ensure_echo_area_buffers ();
6396
6397 if (!message_buf_print)
6398 {
6399 /* A message has been output since the last time we printed.
6400 Choose a fresh echo area buffer. */
6401 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6402 echo_area_buffer[0] = echo_buffer[1];
6403 else
6404 echo_area_buffer[0] = echo_buffer[0];
6405
6406 /* Switch to that buffer and clear it. */
6407 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6408 current_buffer->truncate_lines = Qnil;
6409
6410 if (Z > BEG)
6411 {
6412 int count = BINDING_STACK_SIZE ();
6413 specbind (Qinhibit_read_only, Qt);
6414 del_range (BEG, Z);
6415 unbind_to (count, Qnil);
6416 }
6417 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6418
6419 /* Set up the buffer for the multibyteness we need. */
6420 if (multibyte_p
6421 != !NILP (current_buffer->enable_multibyte_characters))
6422 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6423
6424 /* Raise the frame containing the echo area. */
6425 if (minibuffer_auto_raise)
6426 {
6427 struct frame *sf = SELECTED_FRAME ();
6428 Lisp_Object mini_window;
6429 mini_window = FRAME_MINIBUF_WINDOW (sf);
6430 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6431 }
6432
6433 message_log_maybe_newline ();
6434 message_buf_print = 1;
6435 }
6436 else
6437 {
6438 if (NILP (echo_area_buffer[0]))
6439 {
6440 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6441 echo_area_buffer[0] = echo_buffer[1];
6442 else
6443 echo_area_buffer[0] = echo_buffer[0];
6444 }
6445
6446 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6447 {
6448 /* Someone switched buffers between print requests. */
6449 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6450 current_buffer->truncate_lines = Qnil;
6451 }
6452 }
6453 }
6454
6455
6456 /* Display an echo area message in window W. Value is non-zero if W's
6457 height is changed. If display_last_displayed_message_p is
6458 non-zero, display the message that was last displayed, otherwise
6459 display the current message. */
6460
6461 static int
6462 display_echo_area (w)
6463 struct window *w;
6464 {
6465 int i, no_message_p, window_height_changed_p, count;
6466
6467 /* Temporarily disable garbage collections while displaying the echo
6468 area. This is done because a GC can print a message itself.
6469 That message would modify the echo area buffer's contents while a
6470 redisplay of the buffer is going on, and seriously confuse
6471 redisplay. */
6472 count = inhibit_garbage_collection ();
6473
6474 /* If there is no message, we must call display_echo_area_1
6475 nevertheless because it resizes the window. But we will have to
6476 reset the echo_area_buffer in question to nil at the end because
6477 with_echo_area_buffer will sets it to an empty buffer. */
6478 i = display_last_displayed_message_p ? 1 : 0;
6479 no_message_p = NILP (echo_area_buffer[i]);
6480
6481 window_height_changed_p
6482 = with_echo_area_buffer (w, display_last_displayed_message_p,
6483 display_echo_area_1,
6484 (EMACS_INT) w, Qnil, 0, 0);
6485
6486 if (no_message_p)
6487 echo_area_buffer[i] = Qnil;
6488
6489 unbind_to (count, Qnil);
6490 return window_height_changed_p;
6491 }
6492
6493
6494 /* Helper for display_echo_area. Display the current buffer which
6495 contains the current echo area message in window W, a mini-window,
6496 a pointer to which is passed in A1. A2..A4 are currently not used.
6497 Change the height of W so that all of the message is displayed.
6498 Value is non-zero if height of W was changed. */
6499
6500 static int
6501 display_echo_area_1 (a1, a2, a3, a4)
6502 EMACS_INT a1;
6503 Lisp_Object a2;
6504 EMACS_INT a3, a4;
6505 {
6506 struct window *w = (struct window *) a1;
6507 Lisp_Object window;
6508 struct text_pos start;
6509 int window_height_changed_p = 0;
6510
6511 /* Do this before displaying, so that we have a large enough glyph
6512 matrix for the display. */
6513 window_height_changed_p = resize_mini_window (w, 0);
6514
6515 /* Display. */
6516 clear_glyph_matrix (w->desired_matrix);
6517 XSETWINDOW (window, w);
6518 SET_TEXT_POS (start, BEG, BEG_BYTE);
6519 try_window (window, start);
6520
6521 return window_height_changed_p;
6522 }
6523
6524
6525 /* Resize the echo area window to exactly the size needed for the
6526 currently displayed message, if there is one. If a mini-buffer
6527 is active, don't shrink it. */
6528
6529 void
6530 resize_echo_area_exactly ()
6531 {
6532 if (BUFFERP (echo_area_buffer[0])
6533 && WINDOWP (echo_area_window))
6534 {
6535 struct window *w = XWINDOW (echo_area_window);
6536 int resized_p;
6537 Lisp_Object resize_exactly;
6538
6539 if (minibuf_level == 0)
6540 resize_exactly = Qt;
6541 else
6542 resize_exactly = Qnil;
6543
6544 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6545 (EMACS_INT) w, resize_exactly, 0, 0);
6546 if (resized_p)
6547 {
6548 ++windows_or_buffers_changed;
6549 ++update_mode_lines;
6550 redisplay_internal (0);
6551 }
6552 }
6553 }
6554
6555
6556 /* Callback function for with_echo_area_buffer, when used from
6557 resize_echo_area_exactly. A1 contains a pointer to the window to
6558 resize, EXACTLY non-nil means resize the mini-window exactly to the
6559 size of the text displayed. A3 and A4 are not used. Value is what
6560 resize_mini_window returns. */
6561
6562 static int
6563 resize_mini_window_1 (a1, exactly, a3, a4)
6564 EMACS_INT a1;
6565 Lisp_Object exactly;
6566 EMACS_INT a3, a4;
6567 {
6568 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6569 }
6570
6571
6572 /* Resize mini-window W to fit the size of its contents. EXACT:P
6573 means size the window exactly to the size needed. Otherwise, it's
6574 only enlarged until W's buffer is empty. Value is non-zero if
6575 the window height has been changed. */
6576
6577 int
6578 resize_mini_window (w, exact_p)
6579 struct window *w;
6580 int exact_p;
6581 {
6582 struct frame *f = XFRAME (w->frame);
6583 int window_height_changed_p = 0;
6584
6585 xassert (MINI_WINDOW_P (w));
6586
6587 /* Don't resize windows while redisplaying a window; it would
6588 confuse redisplay functions when the size of the window they are
6589 displaying changes from under them. Such a resizing can happen,
6590 for instance, when which-func prints a long message while
6591 we are running fontification-functions. We're running these
6592 functions with safe_call which binds inhibit-redisplay to t. */
6593 if (!NILP (Vinhibit_redisplay))
6594 return 0;
6595
6596 /* Nil means don't try to resize. */
6597 if (NILP (Vresize_mini_windows)
6598 || (FRAME_X_P (f) && f->output_data.x == NULL))
6599 return 0;
6600
6601 if (!FRAME_MINIBUF_ONLY_P (f))
6602 {
6603 struct it it;
6604 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6605 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6606 int height, max_height;
6607 int unit = CANON_Y_UNIT (f);
6608 struct text_pos start;
6609 struct buffer *old_current_buffer = NULL;
6610
6611 if (current_buffer != XBUFFER (w->buffer))
6612 {
6613 old_current_buffer = current_buffer;
6614 set_buffer_internal (XBUFFER (w->buffer));
6615 }
6616
6617 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6618
6619 /* Compute the max. number of lines specified by the user. */
6620 if (FLOATP (Vmax_mini_window_height))
6621 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6622 else if (INTEGERP (Vmax_mini_window_height))
6623 max_height = XINT (Vmax_mini_window_height);
6624 else
6625 max_height = total_height / 4;
6626
6627 /* Correct that max. height if it's bogus. */
6628 max_height = max (1, max_height);
6629 max_height = min (total_height, max_height);
6630
6631 /* Find out the height of the text in the window. */
6632 if (it.truncate_lines_p)
6633 height = 1;
6634 else
6635 {
6636 last_height = 0;
6637 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6638 if (it.max_ascent == 0 && it.max_descent == 0)
6639 height = it.current_y + last_height;
6640 else
6641 height = it.current_y + it.max_ascent + it.max_descent;
6642 height -= it.extra_line_spacing;
6643 height = (height + unit - 1) / unit;
6644 }
6645
6646 /* Compute a suitable window start. */
6647 if (height > max_height)
6648 {
6649 height = max_height;
6650 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6651 move_it_vertically_backward (&it, (height - 1) * unit);
6652 start = it.current.pos;
6653 }
6654 else
6655 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6656 SET_MARKER_FROM_TEXT_POS (w->start, start);
6657
6658 if (EQ (Vresize_mini_windows, Qgrow_only))
6659 {
6660 /* Let it grow only, until we display an empty message, in which
6661 case the window shrinks again. */
6662 if (height > XFASTINT (w->height))
6663 {
6664 int old_height = XFASTINT (w->height);
6665 freeze_window_starts (f, 1);
6666 grow_mini_window (w, height - XFASTINT (w->height));
6667 window_height_changed_p = XFASTINT (w->height) != old_height;
6668 }
6669 else if (height < XFASTINT (w->height)
6670 && (exact_p || BEGV == ZV))
6671 {
6672 int old_height = XFASTINT (w->height);
6673 freeze_window_starts (f, 0);
6674 shrink_mini_window (w);
6675 window_height_changed_p = XFASTINT (w->height) != old_height;
6676 }
6677 }
6678 else
6679 {
6680 /* Always resize to exact size needed. */
6681 if (height > XFASTINT (w->height))
6682 {
6683 int old_height = XFASTINT (w->height);
6684 freeze_window_starts (f, 1);
6685 grow_mini_window (w, height - XFASTINT (w->height));
6686 window_height_changed_p = XFASTINT (w->height) != old_height;
6687 }
6688 else if (height < XFASTINT (w->height))
6689 {
6690 int old_height = XFASTINT (w->height);
6691 freeze_window_starts (f, 0);
6692 shrink_mini_window (w);
6693
6694 if (height)
6695 {
6696 freeze_window_starts (f, 1);
6697 grow_mini_window (w, height - XFASTINT (w->height));
6698 }
6699
6700 window_height_changed_p = XFASTINT (w->height) != old_height;
6701 }
6702 }
6703
6704 if (old_current_buffer)
6705 set_buffer_internal (old_current_buffer);
6706 }
6707
6708 return window_height_changed_p;
6709 }
6710
6711
6712 /* Value is the current message, a string, or nil if there is no
6713 current message. */
6714
6715 Lisp_Object
6716 current_message ()
6717 {
6718 Lisp_Object msg;
6719
6720 if (NILP (echo_area_buffer[0]))
6721 msg = Qnil;
6722 else
6723 {
6724 with_echo_area_buffer (0, 0, current_message_1,
6725 (EMACS_INT) &msg, Qnil, 0, 0);
6726 if (NILP (msg))
6727 echo_area_buffer[0] = Qnil;
6728 }
6729
6730 return msg;
6731 }
6732
6733
6734 static int
6735 current_message_1 (a1, a2, a3, a4)
6736 EMACS_INT a1;
6737 Lisp_Object a2;
6738 EMACS_INT a3, a4;
6739 {
6740 Lisp_Object *msg = (Lisp_Object *) a1;
6741
6742 if (Z > BEG)
6743 *msg = make_buffer_string (BEG, Z, 1);
6744 else
6745 *msg = Qnil;
6746 return 0;
6747 }
6748
6749
6750 /* Push the current message on Vmessage_stack for later restauration
6751 by restore_message. Value is non-zero if the current message isn't
6752 empty. This is a relatively infrequent operation, so it's not
6753 worth optimizing. */
6754
6755 int
6756 push_message ()
6757 {
6758 Lisp_Object msg;
6759 msg = current_message ();
6760 Vmessage_stack = Fcons (msg, Vmessage_stack);
6761 return STRINGP (msg);
6762 }
6763
6764
6765 /* Handler for record_unwind_protect calling pop_message. */
6766
6767 Lisp_Object
6768 push_message_unwind (dummy)
6769 Lisp_Object dummy;
6770 {
6771 pop_message ();
6772 return Qnil;
6773 }
6774
6775
6776 /* Restore message display from the top of Vmessage_stack. */
6777
6778 void
6779 restore_message ()
6780 {
6781 Lisp_Object msg;
6782
6783 xassert (CONSP (Vmessage_stack));
6784 msg = XCAR (Vmessage_stack);
6785 if (STRINGP (msg))
6786 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6787 else
6788 message3_nolog (msg, 0, 0);
6789 }
6790
6791
6792 /* Pop the top-most entry off Vmessage_stack. */
6793
6794 void
6795 pop_message ()
6796 {
6797 xassert (CONSP (Vmessage_stack));
6798 Vmessage_stack = XCDR (Vmessage_stack);
6799 }
6800
6801
6802 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6803 exits. If the stack is not empty, we have a missing pop_message
6804 somewhere. */
6805
6806 void
6807 check_message_stack ()
6808 {
6809 if (!NILP (Vmessage_stack))
6810 abort ();
6811 }
6812
6813
6814 /* Truncate to NCHARS what will be displayed in the echo area the next
6815 time we display it---but don't redisplay it now. */
6816
6817 void
6818 truncate_echo_area (nchars)
6819 int nchars;
6820 {
6821 if (nchars == 0)
6822 echo_area_buffer[0] = Qnil;
6823 /* A null message buffer means that the frame hasn't really been
6824 initialized yet. Error messages get reported properly by
6825 cmd_error, so this must be just an informative message; toss it. */
6826 else if (!noninteractive
6827 && INTERACTIVE
6828 && !NILP (echo_area_buffer[0]))
6829 {
6830 struct frame *sf = SELECTED_FRAME ();
6831 if (FRAME_MESSAGE_BUF (sf))
6832 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6833 }
6834 }
6835
6836
6837 /* Helper function for truncate_echo_area. Truncate the current
6838 message to at most NCHARS characters. */
6839
6840 static int
6841 truncate_message_1 (nchars, a2, a3, a4)
6842 EMACS_INT nchars;
6843 Lisp_Object a2;
6844 EMACS_INT a3, a4;
6845 {
6846 if (BEG + nchars < Z)
6847 del_range (BEG + nchars, Z);
6848 if (Z == BEG)
6849 echo_area_buffer[0] = Qnil;
6850 return 0;
6851 }
6852
6853
6854 /* Set the current message to a substring of S or STRING.
6855
6856 If STRING is a Lisp string, set the message to the first NBYTES
6857 bytes from STRING. NBYTES zero means use the whole string. If
6858 STRING is multibyte, the message will be displayed multibyte.
6859
6860 If S is not null, set the message to the first LEN bytes of S. LEN
6861 zero means use the whole string. MULTIBYTE_P non-zero means S is
6862 multibyte. Display the message multibyte in that case. */
6863
6864 void
6865 set_message (s, string, nbytes, multibyte_p)
6866 char *s;
6867 Lisp_Object string;
6868 int nbytes;
6869 {
6870 message_enable_multibyte
6871 = ((s && multibyte_p)
6872 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6873
6874 with_echo_area_buffer (0, -1, set_message_1,
6875 (EMACS_INT) s, string, nbytes, multibyte_p);
6876 message_buf_print = 0;
6877 help_echo_showing_p = 0;
6878 }
6879
6880
6881 /* Helper function for set_message. Arguments have the same meaning
6882 as there, with A1 corresponding to S and A2 corresponding to STRING
6883 This function is called with the echo area buffer being
6884 current. */
6885
6886 static int
6887 set_message_1 (a1, a2, nbytes, multibyte_p)
6888 EMACS_INT a1;
6889 Lisp_Object a2;
6890 EMACS_INT nbytes, multibyte_p;
6891 {
6892 char *s = (char *) a1;
6893 Lisp_Object string = a2;
6894
6895 xassert (BEG == Z);
6896
6897 /* Change multibyteness of the echo buffer appropriately. */
6898 if (message_enable_multibyte
6899 != !NILP (current_buffer->enable_multibyte_characters))
6900 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6901
6902 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6903
6904 /* Insert new message at BEG. */
6905 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6906
6907 if (STRINGP (string))
6908 {
6909 int nchars;
6910
6911 if (nbytes == 0)
6912 nbytes = XSTRING (string)->size_byte;
6913 nchars = string_byte_to_char (string, nbytes);
6914
6915 /* This function takes care of single/multibyte conversion. We
6916 just have to ensure that the echo area buffer has the right
6917 setting of enable_multibyte_characters. */
6918 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6919 }
6920 else if (s)
6921 {
6922 if (nbytes == 0)
6923 nbytes = strlen (s);
6924
6925 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6926 {
6927 /* Convert from multi-byte to single-byte. */
6928 int i, c, n;
6929 unsigned char work[1];
6930
6931 /* Convert a multibyte string to single-byte. */
6932 for (i = 0; i < nbytes; i += n)
6933 {
6934 c = string_char_and_length (s + i, nbytes - i, &n);
6935 work[0] = (SINGLE_BYTE_CHAR_P (c)
6936 ? c
6937 : multibyte_char_to_unibyte (c, Qnil));
6938 insert_1_both (work, 1, 1, 1, 0, 0);
6939 }
6940 }
6941 else if (!multibyte_p
6942 && !NILP (current_buffer->enable_multibyte_characters))
6943 {
6944 /* Convert from single-byte to multi-byte. */
6945 int i, c, n;
6946 unsigned char *msg = (unsigned char *) s;
6947 unsigned char str[MAX_MULTIBYTE_LENGTH];
6948
6949 /* Convert a single-byte string to multibyte. */
6950 for (i = 0; i < nbytes; i++)
6951 {
6952 c = unibyte_char_to_multibyte (msg[i]);
6953 n = CHAR_STRING (c, str);
6954 insert_1_both (str, 1, n, 1, 0, 0);
6955 }
6956 }
6957 else
6958 insert_1 (s, nbytes, 1, 0, 0);
6959 }
6960
6961 return 0;
6962 }
6963
6964
6965 /* Clear messages. CURRENT_P non-zero means clear the current
6966 message. LAST_DISPLAYED_P non-zero means clear the message
6967 last displayed. */
6968
6969 void
6970 clear_message (current_p, last_displayed_p)
6971 int current_p, last_displayed_p;
6972 {
6973 if (current_p)
6974 {
6975 echo_area_buffer[0] = Qnil;
6976 message_cleared_p = 1;
6977 }
6978
6979 if (last_displayed_p)
6980 echo_area_buffer[1] = Qnil;
6981
6982 message_buf_print = 0;
6983 }
6984
6985 /* Clear garbaged frames.
6986
6987 This function is used where the old redisplay called
6988 redraw_garbaged_frames which in turn called redraw_frame which in
6989 turn called clear_frame. The call to clear_frame was a source of
6990 flickering. I believe a clear_frame is not necessary. It should
6991 suffice in the new redisplay to invalidate all current matrices,
6992 and ensure a complete redisplay of all windows. */
6993
6994 static void
6995 clear_garbaged_frames ()
6996 {
6997 if (frame_garbaged)
6998 {
6999 Lisp_Object tail, frame;
7000
7001 FOR_EACH_FRAME (tail, frame)
7002 {
7003 struct frame *f = XFRAME (frame);
7004
7005 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7006 {
7007 if (f->resized_p)
7008 Fredraw_frame (frame);
7009 clear_current_matrices (f);
7010 f->garbaged = 0;
7011 f->resized_p = 0;
7012 }
7013 }
7014
7015 frame_garbaged = 0;
7016 ++windows_or_buffers_changed;
7017 }
7018 }
7019
7020
7021 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7022 is non-zero update selected_frame. Value is non-zero if the
7023 mini-windows height has been changed. */
7024
7025 static int
7026 echo_area_display (update_frame_p)
7027 int update_frame_p;
7028 {
7029 Lisp_Object mini_window;
7030 struct window *w;
7031 struct frame *f;
7032 int window_height_changed_p = 0;
7033 struct frame *sf = SELECTED_FRAME ();
7034
7035 mini_window = FRAME_MINIBUF_WINDOW (sf);
7036 w = XWINDOW (mini_window);
7037 f = XFRAME (WINDOW_FRAME (w));
7038
7039 /* Don't display if frame is invisible or not yet initialized. */
7040 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7041 return 0;
7042
7043 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7044 #ifndef macintosh
7045 #ifdef HAVE_WINDOW_SYSTEM
7046 /* When Emacs starts, selected_frame may be a visible terminal
7047 frame, even if we run under a window system. If we let this
7048 through, a message would be displayed on the terminal. */
7049 if (EQ (selected_frame, Vterminal_frame)
7050 && !NILP (Vwindow_system))
7051 return 0;
7052 #endif /* HAVE_WINDOW_SYSTEM */
7053 #endif
7054
7055 /* Redraw garbaged frames. */
7056 if (frame_garbaged)
7057 clear_garbaged_frames ();
7058
7059 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7060 {
7061 echo_area_window = mini_window;
7062 window_height_changed_p = display_echo_area (w);
7063 w->must_be_updated_p = 1;
7064
7065 /* Update the display, unless called from redisplay_internal.
7066 Also don't update the screen during redisplay itself. The
7067 update will happen at the end of redisplay, and an update
7068 here could cause confusion. */
7069 if (update_frame_p && !redisplaying_p)
7070 {
7071 int n = 0;
7072
7073 /* If the display update has been interrupted by pending
7074 input, update mode lines in the frame. Due to the
7075 pending input, it might have been that redisplay hasn't
7076 been called, so that mode lines above the echo area are
7077 garbaged. This looks odd, so we prevent it here. */
7078 if (!display_completed)
7079 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7080
7081 if (window_height_changed_p
7082 /* Don't do this if Emacs is shutting down. Redisplay
7083 needs to run hooks. */
7084 && !NILP (Vrun_hooks))
7085 {
7086 /* Must update other windows. Likewise as in other
7087 cases, don't let this update be interrupted by
7088 pending input. */
7089 int count = BINDING_STACK_SIZE ();
7090 specbind (Qredisplay_dont_pause, Qt);
7091 windows_or_buffers_changed = 1;
7092 redisplay_internal (0);
7093 unbind_to (count, Qnil);
7094 }
7095 else if (FRAME_WINDOW_P (f) && n == 0)
7096 {
7097 /* Window configuration is the same as before.
7098 Can do with a display update of the echo area,
7099 unless we displayed some mode lines. */
7100 update_single_window (w, 1);
7101 rif->flush_display (f);
7102 }
7103 else
7104 update_frame (f, 1, 1);
7105
7106 /* If cursor is in the echo area, make sure that the next
7107 redisplay displays the minibuffer, so that the cursor will
7108 be replaced with what the minibuffer wants. */
7109 if (cursor_in_echo_area)
7110 ++windows_or_buffers_changed;
7111 }
7112 }
7113 else if (!EQ (mini_window, selected_window))
7114 windows_or_buffers_changed++;
7115
7116 /* Last displayed message is now the current message. */
7117 echo_area_buffer[1] = echo_area_buffer[0];
7118
7119 /* Prevent redisplay optimization in redisplay_internal by resetting
7120 this_line_start_pos. This is done because the mini-buffer now
7121 displays the message instead of its buffer text. */
7122 if (EQ (mini_window, selected_window))
7123 CHARPOS (this_line_start_pos) = 0;
7124
7125 return window_height_changed_p;
7126 }
7127
7128
7129 \f
7130 /***********************************************************************
7131 Frame Titles
7132 ***********************************************************************/
7133
7134
7135 #ifdef HAVE_WINDOW_SYSTEM
7136
7137 /* A buffer for constructing frame titles in it; allocated from the
7138 heap in init_xdisp and resized as needed in store_frame_title_char. */
7139
7140 static char *frame_title_buf;
7141
7142 /* The buffer's end, and a current output position in it. */
7143
7144 static char *frame_title_buf_end;
7145 static char *frame_title_ptr;
7146
7147
7148 /* Store a single character C for the frame title in frame_title_buf.
7149 Re-allocate frame_title_buf if necessary. */
7150
7151 static void
7152 store_frame_title_char (c)
7153 char c;
7154 {
7155 /* If output position has reached the end of the allocated buffer,
7156 double the buffer's size. */
7157 if (frame_title_ptr == frame_title_buf_end)
7158 {
7159 int len = frame_title_ptr - frame_title_buf;
7160 int new_size = 2 * len * sizeof *frame_title_buf;
7161 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7162 frame_title_buf_end = frame_title_buf + new_size;
7163 frame_title_ptr = frame_title_buf + len;
7164 }
7165
7166 *frame_title_ptr++ = c;
7167 }
7168
7169
7170 /* Store part of a frame title in frame_title_buf, beginning at
7171 frame_title_ptr. STR is the string to store. Do not copy
7172 characters that yield more columns than PRECISION; PRECISION <= 0
7173 means copy the whole string. Pad with spaces until FIELD_WIDTH
7174 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7175 pad. Called from display_mode_element when it is used to build a
7176 frame title. */
7177
7178 static int
7179 store_frame_title (str, field_width, precision)
7180 unsigned char *str;
7181 int field_width, precision;
7182 {
7183 int n = 0;
7184 int dummy, nbytes;
7185
7186 /* Copy at most PRECISION chars from STR. */
7187 nbytes = strlen (str);
7188 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7189 while (nbytes--)
7190 store_frame_title_char (*str++);
7191
7192 /* Fill up with spaces until FIELD_WIDTH reached. */
7193 while (field_width > 0
7194 && n < field_width)
7195 {
7196 store_frame_title_char (' ');
7197 ++n;
7198 }
7199
7200 return n;
7201 }
7202
7203
7204 /* Set the title of FRAME, if it has changed. The title format is
7205 Vicon_title_format if FRAME is iconified, otherwise it is
7206 frame_title_format. */
7207
7208 static void
7209 x_consider_frame_title (frame)
7210 Lisp_Object frame;
7211 {
7212 struct frame *f = XFRAME (frame);
7213
7214 if (FRAME_WINDOW_P (f)
7215 || FRAME_MINIBUF_ONLY_P (f)
7216 || f->explicit_name)
7217 {
7218 /* Do we have more than one visible frame on this X display? */
7219 Lisp_Object tail;
7220 Lisp_Object fmt;
7221 struct buffer *obuf;
7222 int len;
7223 struct it it;
7224
7225 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7226 {
7227 Lisp_Object other_frame = XCAR (tail);
7228 struct frame *tf = XFRAME (other_frame);
7229
7230 if (tf != f
7231 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7232 && !FRAME_MINIBUF_ONLY_P (tf)
7233 && !EQ (other_frame, tip_frame)
7234 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7235 break;
7236 }
7237
7238 /* Set global variable indicating that multiple frames exist. */
7239 multiple_frames = CONSP (tail);
7240
7241 /* Switch to the buffer of selected window of the frame. Set up
7242 frame_title_ptr so that display_mode_element will output into it;
7243 then display the title. */
7244 obuf = current_buffer;
7245 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7246 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7247 frame_title_ptr = frame_title_buf;
7248 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7249 NULL, DEFAULT_FACE_ID);
7250 display_mode_element (&it, 0, -1, -1, fmt);
7251 len = frame_title_ptr - frame_title_buf;
7252 frame_title_ptr = NULL;
7253 set_buffer_internal_1 (obuf);
7254
7255 /* Set the title only if it's changed. This avoids consing in
7256 the common case where it hasn't. (If it turns out that we've
7257 already wasted too much time by walking through the list with
7258 display_mode_element, then we might need to optimize at a
7259 higher level than this.) */
7260 if (! STRINGP (f->name)
7261 || STRING_BYTES (XSTRING (f->name)) != len
7262 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7263 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7264 }
7265 }
7266
7267 #else /* not HAVE_WINDOW_SYSTEM */
7268
7269 #define frame_title_ptr ((char *)0)
7270 #define store_frame_title(str, mincol, maxcol) 0
7271
7272 #endif /* not HAVE_WINDOW_SYSTEM */
7273
7274
7275
7276 \f
7277 /***********************************************************************
7278 Menu Bars
7279 ***********************************************************************/
7280
7281
7282 /* Prepare for redisplay by updating menu-bar item lists when
7283 appropriate. This can call eval. */
7284
7285 void
7286 prepare_menu_bars ()
7287 {
7288 int all_windows;
7289 struct gcpro gcpro1, gcpro2;
7290 struct frame *f;
7291 Lisp_Object tooltip_frame;
7292
7293 #ifdef HAVE_WINDOW_SYSTEM
7294 tooltip_frame = tip_frame;
7295 #else
7296 tooltip_frame = Qnil;
7297 #endif
7298
7299 /* Update all frame titles based on their buffer names, etc. We do
7300 this before the menu bars so that the buffer-menu will show the
7301 up-to-date frame titles. */
7302 #ifdef HAVE_WINDOW_SYSTEM
7303 if (windows_or_buffers_changed || update_mode_lines)
7304 {
7305 Lisp_Object tail, frame;
7306
7307 FOR_EACH_FRAME (tail, frame)
7308 {
7309 f = XFRAME (frame);
7310 if (!EQ (frame, tooltip_frame)
7311 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7312 x_consider_frame_title (frame);
7313 }
7314 }
7315 #endif /* HAVE_WINDOW_SYSTEM */
7316
7317 /* Update the menu bar item lists, if appropriate. This has to be
7318 done before any actual redisplay or generation of display lines. */
7319 all_windows = (update_mode_lines
7320 || buffer_shared > 1
7321 || windows_or_buffers_changed);
7322 if (all_windows)
7323 {
7324 Lisp_Object tail, frame;
7325 int count = BINDING_STACK_SIZE ();
7326
7327 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7328
7329 FOR_EACH_FRAME (tail, frame)
7330 {
7331 f = XFRAME (frame);
7332
7333 /* Ignore tooltip frame. */
7334 if (EQ (frame, tooltip_frame))
7335 continue;
7336
7337 /* If a window on this frame changed size, report that to
7338 the user and clear the size-change flag. */
7339 if (FRAME_WINDOW_SIZES_CHANGED (f))
7340 {
7341 Lisp_Object functions;
7342
7343 /* Clear flag first in case we get an error below. */
7344 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7345 functions = Vwindow_size_change_functions;
7346 GCPRO2 (tail, functions);
7347
7348 while (CONSP (functions))
7349 {
7350 call1 (XCAR (functions), frame);
7351 functions = XCDR (functions);
7352 }
7353 UNGCPRO;
7354 }
7355
7356 GCPRO1 (tail);
7357 update_menu_bar (f, 0);
7358 #ifdef HAVE_WINDOW_SYSTEM
7359 update_tool_bar (f, 0);
7360 #endif
7361 UNGCPRO;
7362 }
7363
7364 unbind_to (count, Qnil);
7365 }
7366 else
7367 {
7368 struct frame *sf = SELECTED_FRAME ();
7369 update_menu_bar (sf, 1);
7370 #ifdef HAVE_WINDOW_SYSTEM
7371 update_tool_bar (sf, 1);
7372 #endif
7373 }
7374
7375 /* Motif needs this. See comment in xmenu.c. Turn it off when
7376 pending_menu_activation is not defined. */
7377 #ifdef USE_X_TOOLKIT
7378 pending_menu_activation = 0;
7379 #endif
7380 }
7381
7382
7383 /* Update the menu bar item list for frame F. This has to be done
7384 before we start to fill in any display lines, because it can call
7385 eval.
7386
7387 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7388
7389 static void
7390 update_menu_bar (f, save_match_data)
7391 struct frame *f;
7392 int save_match_data;
7393 {
7394 Lisp_Object window;
7395 register struct window *w;
7396
7397 /* If called recursively during a menu update, do nothing. This can
7398 happen when, for instance, an activate-menubar-hook causes a
7399 redisplay. */
7400 if (inhibit_menubar_update)
7401 return;
7402
7403 window = FRAME_SELECTED_WINDOW (f);
7404 w = XWINDOW (window);
7405
7406 if (update_mode_lines)
7407 w->update_mode_line = Qt;
7408
7409 if (FRAME_WINDOW_P (f)
7410 ?
7411 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7412 FRAME_EXTERNAL_MENU_BAR (f)
7413 #else
7414 FRAME_MENU_BAR_LINES (f) > 0
7415 #endif
7416 : FRAME_MENU_BAR_LINES (f) > 0)
7417 {
7418 /* If the user has switched buffers or windows, we need to
7419 recompute to reflect the new bindings. But we'll
7420 recompute when update_mode_lines is set too; that means
7421 that people can use force-mode-line-update to request
7422 that the menu bar be recomputed. The adverse effect on
7423 the rest of the redisplay algorithm is about the same as
7424 windows_or_buffers_changed anyway. */
7425 if (windows_or_buffers_changed
7426 || !NILP (w->update_mode_line)
7427 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7428 < BUF_MODIFF (XBUFFER (w->buffer)))
7429 != !NILP (w->last_had_star))
7430 || ((!NILP (Vtransient_mark_mode)
7431 && !NILP (XBUFFER (w->buffer)->mark_active))
7432 != !NILP (w->region_showing)))
7433 {
7434 struct buffer *prev = current_buffer;
7435 int count = BINDING_STACK_SIZE ();
7436
7437 specbind (Qinhibit_menubar_update, Qt);
7438
7439 set_buffer_internal_1 (XBUFFER (w->buffer));
7440 if (save_match_data)
7441 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7442 if (NILP (Voverriding_local_map_menu_flag))
7443 {
7444 specbind (Qoverriding_terminal_local_map, Qnil);
7445 specbind (Qoverriding_local_map, Qnil);
7446 }
7447
7448 /* Run the Lucid hook. */
7449 safe_run_hooks (Qactivate_menubar_hook);
7450
7451 /* If it has changed current-menubar from previous value,
7452 really recompute the menu-bar from the value. */
7453 if (! NILP (Vlucid_menu_bar_dirty_flag))
7454 call0 (Qrecompute_lucid_menubar);
7455
7456 safe_run_hooks (Qmenu_bar_update_hook);
7457 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7458
7459 /* Redisplay the menu bar in case we changed it. */
7460 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7461 if (FRAME_WINDOW_P (f)
7462 #if defined (macintosh)
7463 /* All frames on Mac OS share the same menubar. So only the
7464 selected frame should be allowed to set it. */
7465 && f == SELECTED_FRAME ()
7466 #endif
7467 )
7468 set_frame_menubar (f, 0, 0);
7469 else
7470 /* On a terminal screen, the menu bar is an ordinary screen
7471 line, and this makes it get updated. */
7472 w->update_mode_line = Qt;
7473 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7474 /* In the non-toolkit version, the menu bar is an ordinary screen
7475 line, and this makes it get updated. */
7476 w->update_mode_line = Qt;
7477 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7478
7479 unbind_to (count, Qnil);
7480 set_buffer_internal_1 (prev);
7481 }
7482 }
7483 }
7484
7485
7486 \f
7487 /***********************************************************************
7488 Tool-bars
7489 ***********************************************************************/
7490
7491 #ifdef HAVE_WINDOW_SYSTEM
7492
7493 /* Update the tool-bar item list for frame F. This has to be done
7494 before we start to fill in any display lines. Called from
7495 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7496 and restore it here. */
7497
7498 static void
7499 update_tool_bar (f, save_match_data)
7500 struct frame *f;
7501 int save_match_data;
7502 {
7503 if (WINDOWP (f->tool_bar_window)
7504 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7505 {
7506 Lisp_Object window;
7507 struct window *w;
7508
7509 window = FRAME_SELECTED_WINDOW (f);
7510 w = XWINDOW (window);
7511
7512 /* If the user has switched buffers or windows, we need to
7513 recompute to reflect the new bindings. But we'll
7514 recompute when update_mode_lines is set too; that means
7515 that people can use force-mode-line-update to request
7516 that the menu bar be recomputed. The adverse effect on
7517 the rest of the redisplay algorithm is about the same as
7518 windows_or_buffers_changed anyway. */
7519 if (windows_or_buffers_changed
7520 || !NILP (w->update_mode_line)
7521 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7522 < BUF_MODIFF (XBUFFER (w->buffer)))
7523 != !NILP (w->last_had_star))
7524 || ((!NILP (Vtransient_mark_mode)
7525 && !NILP (XBUFFER (w->buffer)->mark_active))
7526 != !NILP (w->region_showing)))
7527 {
7528 struct buffer *prev = current_buffer;
7529 int count = BINDING_STACK_SIZE ();
7530
7531 /* Set current_buffer to the buffer of the selected
7532 window of the frame, so that we get the right local
7533 keymaps. */
7534 set_buffer_internal_1 (XBUFFER (w->buffer));
7535
7536 /* Save match data, if we must. */
7537 if (save_match_data)
7538 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7539
7540 /* Make sure that we don't accidentally use bogus keymaps. */
7541 if (NILP (Voverriding_local_map_menu_flag))
7542 {
7543 specbind (Qoverriding_terminal_local_map, Qnil);
7544 specbind (Qoverriding_local_map, Qnil);
7545 }
7546
7547 /* Build desired tool-bar items from keymaps. */
7548 f->tool_bar_items
7549 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7550
7551 /* Redisplay the tool-bar in case we changed it. */
7552 w->update_mode_line = Qt;
7553
7554 unbind_to (count, Qnil);
7555 set_buffer_internal_1 (prev);
7556 }
7557 }
7558 }
7559
7560
7561 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7562 F's desired tool-bar contents. F->tool_bar_items must have
7563 been set up previously by calling prepare_menu_bars. */
7564
7565 static void
7566 build_desired_tool_bar_string (f)
7567 struct frame *f;
7568 {
7569 int i, size, size_needed;
7570 struct gcpro gcpro1, gcpro2, gcpro3;
7571 Lisp_Object image, plist, props;
7572
7573 image = plist = props = Qnil;
7574 GCPRO3 (image, plist, props);
7575
7576 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7577 Otherwise, make a new string. */
7578
7579 /* The size of the string we might be able to reuse. */
7580 size = (STRINGP (f->desired_tool_bar_string)
7581 ? XSTRING (f->desired_tool_bar_string)->size
7582 : 0);
7583
7584 /* We need one space in the string for each image. */
7585 size_needed = f->n_tool_bar_items;
7586
7587 /* Reuse f->desired_tool_bar_string, if possible. */
7588 if (size < size_needed || NILP (f->desired_tool_bar_string))
7589 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7590 make_number (' '));
7591 else
7592 {
7593 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7594 Fremove_text_properties (make_number (0), make_number (size),
7595 props, f->desired_tool_bar_string);
7596 }
7597
7598 /* Put a `display' property on the string for the images to display,
7599 put a `menu_item' property on tool-bar items with a value that
7600 is the index of the item in F's tool-bar item vector. */
7601 for (i = 0; i < f->n_tool_bar_items; ++i)
7602 {
7603 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7604
7605 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7606 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7607 int hmargin, vmargin, relief, idx, end;
7608 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7609
7610 /* If image is a vector, choose the image according to the
7611 button state. */
7612 image = PROP (TOOL_BAR_ITEM_IMAGES);
7613 if (VECTORP (image))
7614 {
7615 if (enabled_p)
7616 idx = (selected_p
7617 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7618 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7619 else
7620 idx = (selected_p
7621 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7622 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7623
7624 xassert (ASIZE (image) >= idx);
7625 image = AREF (image, idx);
7626 }
7627 else
7628 idx = -1;
7629
7630 /* Ignore invalid image specifications. */
7631 if (!valid_image_p (image))
7632 continue;
7633
7634 /* Display the tool-bar button pressed, or depressed. */
7635 plist = Fcopy_sequence (XCDR (image));
7636
7637 /* Compute margin and relief to draw. */
7638 relief = (tool_bar_button_relief >= 0
7639 ? tool_bar_button_relief
7640 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7641 hmargin = vmargin = relief;
7642
7643 if (INTEGERP (Vtool_bar_button_margin)
7644 && XINT (Vtool_bar_button_margin) > 0)
7645 {
7646 hmargin += XFASTINT (Vtool_bar_button_margin);
7647 vmargin += XFASTINT (Vtool_bar_button_margin);
7648 }
7649 else if (CONSP (Vtool_bar_button_margin))
7650 {
7651 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7652 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7653 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7654
7655 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7656 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7657 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7658 }
7659
7660 if (auto_raise_tool_bar_buttons_p)
7661 {
7662 /* Add a `:relief' property to the image spec if the item is
7663 selected. */
7664 if (selected_p)
7665 {
7666 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7667 hmargin -= relief;
7668 vmargin -= relief;
7669 }
7670 }
7671 else
7672 {
7673 /* If image is selected, display it pressed, i.e. with a
7674 negative relief. If it's not selected, display it with a
7675 raised relief. */
7676 plist = Fplist_put (plist, QCrelief,
7677 (selected_p
7678 ? make_number (-relief)
7679 : make_number (relief)));
7680 hmargin -= relief;
7681 vmargin -= relief;
7682 }
7683
7684 /* Put a margin around the image. */
7685 if (hmargin || vmargin)
7686 {
7687 if (hmargin == vmargin)
7688 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7689 else
7690 plist = Fplist_put (plist, QCmargin,
7691 Fcons (make_number (hmargin),
7692 make_number (vmargin)));
7693 }
7694
7695 /* If button is not enabled, and we don't have special images
7696 for the disabled state, make the image appear disabled by
7697 applying an appropriate algorithm to it. */
7698 if (!enabled_p && idx < 0)
7699 plist = Fplist_put (plist, QCconversion, Qdisabled);
7700
7701 /* Put a `display' text property on the string for the image to
7702 display. Put a `menu-item' property on the string that gives
7703 the start of this item's properties in the tool-bar items
7704 vector. */
7705 image = Fcons (Qimage, plist);
7706 props = list4 (Qdisplay, image,
7707 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7708
7709 /* Let the last image hide all remaining spaces in the tool bar
7710 string. The string can be longer than needed when we reuse a
7711 previous string. */
7712 if (i + 1 == f->n_tool_bar_items)
7713 end = XSTRING (f->desired_tool_bar_string)->size;
7714 else
7715 end = i + 1;
7716 Fadd_text_properties (make_number (i), make_number (end),
7717 props, f->desired_tool_bar_string);
7718 #undef PROP
7719 }
7720
7721 UNGCPRO;
7722 }
7723
7724
7725 /* Display one line of the tool-bar of frame IT->f. */
7726
7727 static void
7728 display_tool_bar_line (it)
7729 struct it *it;
7730 {
7731 struct glyph_row *row = it->glyph_row;
7732 int max_x = it->last_visible_x;
7733 struct glyph *last;
7734
7735 prepare_desired_row (row);
7736 row->y = it->current_y;
7737
7738 /* Note that this isn't made use of if the face hasn't a box,
7739 so there's no need to check the face here. */
7740 it->start_of_box_run_p = 1;
7741
7742 while (it->current_x < max_x)
7743 {
7744 int x_before, x, n_glyphs_before, i, nglyphs;
7745
7746 /* Get the next display element. */
7747 if (!get_next_display_element (it))
7748 break;
7749
7750 /* Produce glyphs. */
7751 x_before = it->current_x;
7752 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7753 PRODUCE_GLYPHS (it);
7754
7755 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7756 i = 0;
7757 x = x_before;
7758 while (i < nglyphs)
7759 {
7760 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7761
7762 if (x + glyph->pixel_width > max_x)
7763 {
7764 /* Glyph doesn't fit on line. */
7765 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7766 it->current_x = x;
7767 goto out;
7768 }
7769
7770 ++it->hpos;
7771 x += glyph->pixel_width;
7772 ++i;
7773 }
7774
7775 /* Stop at line ends. */
7776 if (ITERATOR_AT_END_OF_LINE_P (it))
7777 break;
7778
7779 set_iterator_to_next (it, 1);
7780 }
7781
7782 out:;
7783
7784 row->displays_text_p = row->used[TEXT_AREA] != 0;
7785 extend_face_to_end_of_line (it);
7786 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7787 last->right_box_line_p = 1;
7788 if (last == row->glyphs[TEXT_AREA])
7789 last->left_box_line_p = 1;
7790 compute_line_metrics (it);
7791
7792 /* If line is empty, make it occupy the rest of the tool-bar. */
7793 if (!row->displays_text_p)
7794 {
7795 row->height = row->phys_height = it->last_visible_y - row->y;
7796 row->ascent = row->phys_ascent = 0;
7797 }
7798
7799 row->full_width_p = 1;
7800 row->continued_p = 0;
7801 row->truncated_on_left_p = 0;
7802 row->truncated_on_right_p = 0;
7803
7804 it->current_x = it->hpos = 0;
7805 it->current_y += row->height;
7806 ++it->vpos;
7807 ++it->glyph_row;
7808 }
7809
7810
7811 /* Value is the number of screen lines needed to make all tool-bar
7812 items of frame F visible. */
7813
7814 static int
7815 tool_bar_lines_needed (f)
7816 struct frame *f;
7817 {
7818 struct window *w = XWINDOW (f->tool_bar_window);
7819 struct it it;
7820
7821 /* Initialize an iterator for iteration over
7822 F->desired_tool_bar_string in the tool-bar window of frame F. */
7823 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7824 it.first_visible_x = 0;
7825 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7826 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7827
7828 while (!ITERATOR_AT_END_P (&it))
7829 {
7830 it.glyph_row = w->desired_matrix->rows;
7831 clear_glyph_row (it.glyph_row);
7832 display_tool_bar_line (&it);
7833 }
7834
7835 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7836 }
7837
7838
7839 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7840 0, 1, 0,
7841 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7842 (frame)
7843 Lisp_Object frame;
7844 {
7845 struct frame *f;
7846 struct window *w;
7847 int nlines = 0;
7848
7849 if (NILP (frame))
7850 frame = selected_frame;
7851 else
7852 CHECK_FRAME (frame);
7853 f = XFRAME (frame);
7854
7855 if (WINDOWP (f->tool_bar_window)
7856 || (w = XWINDOW (f->tool_bar_window),
7857 XFASTINT (w->height) > 0))
7858 {
7859 update_tool_bar (f, 1);
7860 if (f->n_tool_bar_items)
7861 {
7862 build_desired_tool_bar_string (f);
7863 nlines = tool_bar_lines_needed (f);
7864 }
7865 }
7866
7867 return make_number (nlines);
7868 }
7869
7870
7871 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7872 height should be changed. */
7873
7874 static int
7875 redisplay_tool_bar (f)
7876 struct frame *f;
7877 {
7878 struct window *w;
7879 struct it it;
7880 struct glyph_row *row;
7881 int change_height_p = 0;
7882
7883 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7884 do anything. This means you must start with tool-bar-lines
7885 non-zero to get the auto-sizing effect. Or in other words, you
7886 can turn off tool-bars by specifying tool-bar-lines zero. */
7887 if (!WINDOWP (f->tool_bar_window)
7888 || (w = XWINDOW (f->tool_bar_window),
7889 XFASTINT (w->height) == 0))
7890 return 0;
7891
7892 /* Set up an iterator for the tool-bar window. */
7893 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7894 it.first_visible_x = 0;
7895 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7896 row = it.glyph_row;
7897
7898 /* Build a string that represents the contents of the tool-bar. */
7899 build_desired_tool_bar_string (f);
7900 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7901
7902 /* Display as many lines as needed to display all tool-bar items. */
7903 while (it.current_y < it.last_visible_y)
7904 display_tool_bar_line (&it);
7905
7906 /* It doesn't make much sense to try scrolling in the tool-bar
7907 window, so don't do it. */
7908 w->desired_matrix->no_scrolling_p = 1;
7909 w->must_be_updated_p = 1;
7910
7911 if (auto_resize_tool_bars_p)
7912 {
7913 int nlines;
7914
7915 /* If we couldn't display everything, change the tool-bar's
7916 height. */
7917 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7918 change_height_p = 1;
7919
7920 /* If there are blank lines at the end, except for a partially
7921 visible blank line at the end that is smaller than
7922 CANON_Y_UNIT, change the tool-bar's height. */
7923 row = it.glyph_row - 1;
7924 if (!row->displays_text_p
7925 && row->height >= CANON_Y_UNIT (f))
7926 change_height_p = 1;
7927
7928 /* If row displays tool-bar items, but is partially visible,
7929 change the tool-bar's height. */
7930 if (row->displays_text_p
7931 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7932 change_height_p = 1;
7933
7934 /* Resize windows as needed by changing the `tool-bar-lines'
7935 frame parameter. */
7936 if (change_height_p
7937 && (nlines = tool_bar_lines_needed (f),
7938 nlines != XFASTINT (w->height)))
7939 {
7940 extern Lisp_Object Qtool_bar_lines;
7941 Lisp_Object frame;
7942 int old_height = XFASTINT (w->height);
7943
7944 XSETFRAME (frame, f);
7945 clear_glyph_matrix (w->desired_matrix);
7946 Fmodify_frame_parameters (frame,
7947 Fcons (Fcons (Qtool_bar_lines,
7948 make_number (nlines)),
7949 Qnil));
7950 if (XFASTINT (w->height) != old_height)
7951 fonts_changed_p = 1;
7952 }
7953 }
7954
7955 return change_height_p;
7956 }
7957
7958
7959 /* Get information about the tool-bar item which is displayed in GLYPH
7960 on frame F. Return in *PROP_IDX the index where tool-bar item
7961 properties start in F->tool_bar_items. Value is zero if
7962 GLYPH doesn't display a tool-bar item. */
7963
7964 int
7965 tool_bar_item_info (f, glyph, prop_idx)
7966 struct frame *f;
7967 struct glyph *glyph;
7968 int *prop_idx;
7969 {
7970 Lisp_Object prop;
7971 int success_p;
7972 int charpos;
7973
7974 /* This function can be called asynchronously, which means we must
7975 exclude any possibility that Fget_text_property signals an
7976 error. */
7977 charpos = min (XSTRING (f->current_tool_bar_string)->size, glyph->charpos);
7978 charpos = max (0, charpos);
7979
7980 /* Get the text property `menu-item' at pos. The value of that
7981 property is the start index of this item's properties in
7982 F->tool_bar_items. */
7983 prop = Fget_text_property (make_number (charpos),
7984 Qmenu_item, f->current_tool_bar_string);
7985 if (INTEGERP (prop))
7986 {
7987 *prop_idx = XINT (prop);
7988 success_p = 1;
7989 }
7990 else
7991 success_p = 0;
7992
7993 return success_p;
7994 }
7995
7996 #endif /* HAVE_WINDOW_SYSTEM */
7997
7998
7999 \f
8000 /************************************************************************
8001 Horizontal scrolling
8002 ************************************************************************/
8003
8004 static int hscroll_window_tree P_ ((Lisp_Object));
8005 static int hscroll_windows P_ ((Lisp_Object));
8006
8007 /* For all leaf windows in the window tree rooted at WINDOW, set their
8008 hscroll value so that PT is (i) visible in the window, and (ii) so
8009 that it is not within a certain margin at the window's left and
8010 right border. Value is non-zero if any window's hscroll has been
8011 changed. */
8012
8013 static int
8014 hscroll_window_tree (window)
8015 Lisp_Object window;
8016 {
8017 int hscrolled_p = 0;
8018
8019 while (WINDOWP (window))
8020 {
8021 struct window *w = XWINDOW (window);
8022
8023 if (WINDOWP (w->hchild))
8024 hscrolled_p |= hscroll_window_tree (w->hchild);
8025 else if (WINDOWP (w->vchild))
8026 hscrolled_p |= hscroll_window_tree (w->vchild);
8027 else if (w->cursor.vpos >= 0)
8028 {
8029 int hscroll_margin, text_area_x, text_area_y;
8030 int text_area_width, text_area_height;
8031 struct glyph_row *current_cursor_row
8032 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8033 struct glyph_row *desired_cursor_row
8034 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8035 struct glyph_row *cursor_row
8036 = (desired_cursor_row->enabled_p
8037 ? desired_cursor_row
8038 : current_cursor_row);
8039
8040 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8041 &text_area_width, &text_area_height);
8042
8043 /* Scroll when cursor is inside this scroll margin. */
8044 /* Shouldn't we export this `5' for customization ? -stef */
8045 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
8046
8047 if ((XFASTINT (w->hscroll)
8048 && w->cursor.x < hscroll_margin)
8049 || (cursor_row->enabled_p
8050 && cursor_row->truncated_on_right_p
8051 && (w->cursor.x > text_area_width - hscroll_margin)))
8052 {
8053 struct it it;
8054 int hscroll;
8055 struct buffer *saved_current_buffer;
8056 int pt;
8057
8058 /* Find point in a display of infinite width. */
8059 saved_current_buffer = current_buffer;
8060 current_buffer = XBUFFER (w->buffer);
8061
8062 if (w == XWINDOW (selected_window))
8063 pt = BUF_PT (current_buffer);
8064 else
8065 {
8066 pt = marker_position (w->pointm);
8067 pt = max (BEGV, pt);
8068 pt = min (ZV, pt);
8069 }
8070
8071 /* Move iterator to pt starting at cursor_row->start in
8072 a line with infinite width. */
8073 init_to_row_start (&it, w, cursor_row);
8074 it.last_visible_x = INFINITY;
8075 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8076 current_buffer = saved_current_buffer;
8077
8078 /* Center cursor in window. */
8079 hscroll = (max (0, it.current_x - text_area_width / 2)
8080 / CANON_X_UNIT (it.f));
8081 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8082
8083 /* Don't call Fset_window_hscroll if value hasn't
8084 changed because it will prevent redisplay
8085 optimizations. */
8086 if (XFASTINT (w->hscroll) != hscroll)
8087 {
8088 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8089 w->hscroll = make_number (hscroll);
8090 hscrolled_p = 1;
8091 }
8092 }
8093 }
8094
8095 window = w->next;
8096 }
8097
8098 /* Value is non-zero if hscroll of any leaf window has been changed. */
8099 return hscrolled_p;
8100 }
8101
8102
8103 /* Set hscroll so that cursor is visible and not inside horizontal
8104 scroll margins for all windows in the tree rooted at WINDOW. See
8105 also hscroll_window_tree above. Value is non-zero if any window's
8106 hscroll has been changed. If it has, desired matrices on the frame
8107 of WINDOW are cleared. */
8108
8109 static int
8110 hscroll_windows (window)
8111 Lisp_Object window;
8112 {
8113 int hscrolled_p;
8114
8115 if (automatic_hscrolling_p)
8116 {
8117 hscrolled_p = hscroll_window_tree (window);
8118 if (hscrolled_p)
8119 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8120 }
8121 else
8122 hscrolled_p = 0;
8123 return hscrolled_p;
8124 }
8125
8126
8127 \f
8128 /************************************************************************
8129 Redisplay
8130 ************************************************************************/
8131
8132 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8133 to a non-zero value. This is sometimes handy to have in a debugger
8134 session. */
8135
8136 #if GLYPH_DEBUG
8137
8138 /* First and last unchanged row for try_window_id. */
8139
8140 int debug_first_unchanged_at_end_vpos;
8141 int debug_last_unchanged_at_beg_vpos;
8142
8143 /* Delta vpos and y. */
8144
8145 int debug_dvpos, debug_dy;
8146
8147 /* Delta in characters and bytes for try_window_id. */
8148
8149 int debug_delta, debug_delta_bytes;
8150
8151 /* Values of window_end_pos and window_end_vpos at the end of
8152 try_window_id. */
8153
8154 int debug_end_pos, debug_end_vpos;
8155
8156 /* Append a string to W->desired_matrix->method. FMT is a printf
8157 format string. A1...A9 are a supplement for a variable-length
8158 argument list. If trace_redisplay_p is non-zero also printf the
8159 resulting string to stderr. */
8160
8161 static void
8162 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8163 struct window *w;
8164 char *fmt;
8165 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8166 {
8167 char buffer[512];
8168 char *method = w->desired_matrix->method;
8169 int len = strlen (method);
8170 int size = sizeof w->desired_matrix->method;
8171 int remaining = size - len - 1;
8172
8173 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8174 if (len && remaining)
8175 {
8176 method[len] = '|';
8177 --remaining, ++len;
8178 }
8179
8180 strncpy (method + len, buffer, remaining);
8181
8182 if (trace_redisplay_p)
8183 fprintf (stderr, "%p (%s): %s\n",
8184 w,
8185 ((BUFFERP (w->buffer)
8186 && STRINGP (XBUFFER (w->buffer)->name))
8187 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
8188 : "no buffer"),
8189 buffer);
8190 }
8191
8192 #endif /* GLYPH_DEBUG */
8193
8194
8195 /* This counter is used to clear the face cache every once in a while
8196 in redisplay_internal. It is incremented for each redisplay.
8197 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8198 cleared. */
8199
8200 #define CLEAR_FACE_CACHE_COUNT 500
8201 static int clear_face_cache_count;
8202
8203 /* Record the previous terminal frame we displayed. */
8204
8205 static struct frame *previous_terminal_frame;
8206
8207 /* Non-zero while redisplay_internal is in progress. */
8208
8209 int redisplaying_p;
8210
8211
8212 /* Value is non-zero if all changes in window W, which displays
8213 current_buffer, are in the text between START and END. START is a
8214 buffer position, END is given as a distance from Z. Used in
8215 redisplay_internal for display optimization. */
8216
8217 static INLINE int
8218 text_outside_line_unchanged_p (w, start, end)
8219 struct window *w;
8220 int start, end;
8221 {
8222 int unchanged_p = 1;
8223
8224 /* If text or overlays have changed, see where. */
8225 if (XFASTINT (w->last_modified) < MODIFF
8226 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8227 {
8228 /* Gap in the line? */
8229 if (GPT < start || Z - GPT < end)
8230 unchanged_p = 0;
8231
8232 /* Changes start in front of the line, or end after it? */
8233 if (unchanged_p
8234 && (BEG_UNCHANGED < start - 1
8235 || END_UNCHANGED < end))
8236 unchanged_p = 0;
8237
8238 /* If selective display, can't optimize if changes start at the
8239 beginning of the line. */
8240 if (unchanged_p
8241 && INTEGERP (current_buffer->selective_display)
8242 && XINT (current_buffer->selective_display) > 0
8243 && (BEG_UNCHANGED < start || GPT <= start))
8244 unchanged_p = 0;
8245
8246 /* If there are overlays at the start or end of the line, these
8247 may have overlay strings with newlines in them. A change at
8248 START, for instance, may actually concern the display of such
8249 overlay strings as well, and they are displayed on different
8250 lines. So, quickly rule out this case. (For the future, it
8251 might be desirable to implement something more telling than
8252 just BEG/END_UNCHANGED.) */
8253 if (unchanged_p)
8254 {
8255 if (BEG + BEG_UNCHANGED == start
8256 && overlay_touches_p (start))
8257 unchanged_p = 0;
8258 if (END_UNCHANGED == end
8259 && overlay_touches_p (Z - end))
8260 unchanged_p = 0;
8261 }
8262 }
8263
8264 return unchanged_p;
8265 }
8266
8267
8268 /* Do a frame update, taking possible shortcuts into account. This is
8269 the main external entry point for redisplay.
8270
8271 If the last redisplay displayed an echo area message and that message
8272 is no longer requested, we clear the echo area or bring back the
8273 mini-buffer if that is in use. */
8274
8275 void
8276 redisplay ()
8277 {
8278 redisplay_internal (0);
8279 }
8280
8281
8282 /* Return 1 if point moved out of or into a composition. Otherwise
8283 return 0. PREV_BUF and PREV_PT are the last point buffer and
8284 position. BUF and PT are the current point buffer and position. */
8285
8286 int
8287 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8288 struct buffer *prev_buf, *buf;
8289 int prev_pt, pt;
8290 {
8291 int start, end;
8292 Lisp_Object prop;
8293 Lisp_Object buffer;
8294
8295 XSETBUFFER (buffer, buf);
8296 /* Check a composition at the last point if point moved within the
8297 same buffer. */
8298 if (prev_buf == buf)
8299 {
8300 if (prev_pt == pt)
8301 /* Point didn't move. */
8302 return 0;
8303
8304 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8305 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8306 && COMPOSITION_VALID_P (start, end, prop)
8307 && start < prev_pt && end > prev_pt)
8308 /* The last point was within the composition. Return 1 iff
8309 point moved out of the composition. */
8310 return (pt <= start || pt >= end);
8311 }
8312
8313 /* Check a composition at the current point. */
8314 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8315 && find_composition (pt, -1, &start, &end, &prop, buffer)
8316 && COMPOSITION_VALID_P (start, end, prop)
8317 && start < pt && end > pt);
8318 }
8319
8320
8321 /* Reconsider the setting of B->clip_changed which is displayed
8322 in window W. */
8323
8324 static INLINE void
8325 reconsider_clip_changes (w, b)
8326 struct window *w;
8327 struct buffer *b;
8328 {
8329 if (b->prevent_redisplay_optimizations_p)
8330 b->clip_changed = 1;
8331 else if (b->clip_changed
8332 && !NILP (w->window_end_valid)
8333 && w->current_matrix->buffer == b
8334 && w->current_matrix->zv == BUF_ZV (b)
8335 && w->current_matrix->begv == BUF_BEGV (b))
8336 b->clip_changed = 0;
8337
8338 /* If display wasn't paused, and W is not a tool bar window, see if
8339 point has been moved into or out of a composition. In that case,
8340 we set b->clip_changed to 1 to force updating the screen. If
8341 b->clip_changed has already been set to 1, we can skip this
8342 check. */
8343 if (!b->clip_changed
8344 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8345 {
8346 int pt;
8347
8348 if (w == XWINDOW (selected_window))
8349 pt = BUF_PT (current_buffer);
8350 else
8351 pt = marker_position (w->pointm);
8352
8353 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8354 || pt != XINT (w->last_point))
8355 && check_point_in_composition (w->current_matrix->buffer,
8356 XINT (w->last_point),
8357 XBUFFER (w->buffer), pt))
8358 b->clip_changed = 1;
8359 }
8360 }
8361
8362
8363 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8364 response to any user action; therefore, we should preserve the echo
8365 area. (Actually, our caller does that job.) Perhaps in the future
8366 avoid recentering windows if it is not necessary; currently that
8367 causes some problems. */
8368
8369 static void
8370 redisplay_internal (preserve_echo_area)
8371 int preserve_echo_area;
8372 {
8373 struct window *w = XWINDOW (selected_window);
8374 struct frame *f = XFRAME (w->frame);
8375 int pause;
8376 int must_finish = 0;
8377 struct text_pos tlbufpos, tlendpos;
8378 int number_of_visible_frames;
8379 int count;
8380 struct frame *sf = SELECTED_FRAME ();
8381
8382 /* Non-zero means redisplay has to consider all windows on all
8383 frames. Zero means, only selected_window is considered. */
8384 int consider_all_windows_p;
8385
8386 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8387
8388 /* No redisplay if running in batch mode or frame is not yet fully
8389 initialized, or redisplay is explicitly turned off by setting
8390 Vinhibit_redisplay. */
8391 if (noninteractive
8392 || !NILP (Vinhibit_redisplay)
8393 || !f->glyphs_initialized_p)
8394 return;
8395
8396 /* The flag redisplay_performed_directly_p is set by
8397 direct_output_for_insert when it already did the whole screen
8398 update necessary. */
8399 if (redisplay_performed_directly_p)
8400 {
8401 redisplay_performed_directly_p = 0;
8402 if (!hscroll_windows (selected_window))
8403 return;
8404 }
8405
8406 #ifdef USE_X_TOOLKIT
8407 if (popup_activated ())
8408 return;
8409 #endif
8410
8411 /* I don't think this happens but let's be paranoid. */
8412 if (redisplaying_p)
8413 return;
8414
8415 /* Record a function that resets redisplaying_p to its old value
8416 when we leave this function. */
8417 count = BINDING_STACK_SIZE ();
8418 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8419 ++redisplaying_p;
8420
8421 retry:
8422 pause = 0;
8423 reconsider_clip_changes (w, current_buffer);
8424
8425 /* If new fonts have been loaded that make a glyph matrix adjustment
8426 necessary, do it. */
8427 if (fonts_changed_p)
8428 {
8429 adjust_glyphs (NULL);
8430 ++windows_or_buffers_changed;
8431 fonts_changed_p = 0;
8432 }
8433
8434 /* If face_change_count is non-zero, init_iterator will free all
8435 realized faces, which includes the faces referenced from current
8436 matrices. So, we can't reuse current matrices in this case. */
8437 if (face_change_count)
8438 ++windows_or_buffers_changed;
8439
8440 if (! FRAME_WINDOW_P (sf)
8441 && previous_terminal_frame != sf)
8442 {
8443 /* Since frames on an ASCII terminal share the same display
8444 area, displaying a different frame means redisplay the whole
8445 thing. */
8446 windows_or_buffers_changed++;
8447 SET_FRAME_GARBAGED (sf);
8448 XSETFRAME (Vterminal_frame, sf);
8449 }
8450 previous_terminal_frame = sf;
8451
8452 /* Set the visible flags for all frames. Do this before checking
8453 for resized or garbaged frames; they want to know if their frames
8454 are visible. See the comment in frame.h for
8455 FRAME_SAMPLE_VISIBILITY. */
8456 {
8457 Lisp_Object tail, frame;
8458
8459 number_of_visible_frames = 0;
8460
8461 FOR_EACH_FRAME (tail, frame)
8462 {
8463 struct frame *f = XFRAME (frame);
8464
8465 FRAME_SAMPLE_VISIBILITY (f);
8466 if (FRAME_VISIBLE_P (f))
8467 ++number_of_visible_frames;
8468 clear_desired_matrices (f);
8469 }
8470 }
8471
8472 /* Notice any pending interrupt request to change frame size. */
8473 do_pending_window_change (1);
8474
8475 /* Clear frames marked as garbaged. */
8476 if (frame_garbaged)
8477 clear_garbaged_frames ();
8478
8479 /* Build menubar and tool-bar items. */
8480 prepare_menu_bars ();
8481
8482 if (windows_or_buffers_changed)
8483 update_mode_lines++;
8484
8485 /* Detect case that we need to write or remove a star in the mode line. */
8486 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8487 {
8488 w->update_mode_line = Qt;
8489 if (buffer_shared > 1)
8490 update_mode_lines++;
8491 }
8492
8493 /* If %c is in the mode line, update it if needed. */
8494 if (!NILP (w->column_number_displayed)
8495 /* This alternative quickly identifies a common case
8496 where no change is needed. */
8497 && !(PT == XFASTINT (w->last_point)
8498 && XFASTINT (w->last_modified) >= MODIFF
8499 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8500 && XFASTINT (w->column_number_displayed) != current_column ())
8501 w->update_mode_line = Qt;
8502
8503 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8504
8505 /* The variable buffer_shared is set in redisplay_window and
8506 indicates that we redisplay a buffer in different windows. See
8507 there. */
8508 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8509
8510 /* If specs for an arrow have changed, do thorough redisplay
8511 to ensure we remove any arrow that should no longer exist. */
8512 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8513 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8514 consider_all_windows_p = windows_or_buffers_changed = 1;
8515
8516 /* Normally the message* functions will have already displayed and
8517 updated the echo area, but the frame may have been trashed, or
8518 the update may have been preempted, so display the echo area
8519 again here. Checking message_cleared_p captures the case that
8520 the echo area should be cleared. */
8521 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8522 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8523 || (message_cleared_p
8524 && minibuf_level == 0
8525 /* If the mini-window is currently selected, this means the
8526 echo-area doesn't show through. */
8527 && !MINI_WINDOW_P (XWINDOW (selected_window))))
8528 {
8529 int window_height_changed_p = echo_area_display (0);
8530 must_finish = 1;
8531
8532 /* If we don't display the current message, don't clear the
8533 message_cleared_p flag, because, if we did, we wouldn't clear
8534 the echo area in the next redisplay which doesn't preserve
8535 the echo area. */
8536 if (!display_last_displayed_message_p)
8537 message_cleared_p = 0;
8538
8539 if (fonts_changed_p)
8540 goto retry;
8541 else if (window_height_changed_p)
8542 {
8543 consider_all_windows_p = 1;
8544 ++update_mode_lines;
8545 ++windows_or_buffers_changed;
8546
8547 /* If window configuration was changed, frames may have been
8548 marked garbaged. Clear them or we will experience
8549 surprises wrt scrolling. */
8550 if (frame_garbaged)
8551 clear_garbaged_frames ();
8552 }
8553 }
8554 else if (EQ (selected_window, minibuf_window)
8555 && (current_buffer->clip_changed
8556 || XFASTINT (w->last_modified) < MODIFF
8557 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8558 && resize_mini_window (w, 0))
8559 {
8560 /* Resized active mini-window to fit the size of what it is
8561 showing if its contents might have changed. */
8562 must_finish = 1;
8563 consider_all_windows_p = 1;
8564 ++windows_or_buffers_changed;
8565 ++update_mode_lines;
8566
8567 /* If window configuration was changed, frames may have been
8568 marked garbaged. Clear them or we will experience
8569 surprises wrt scrolling. */
8570 if (frame_garbaged)
8571 clear_garbaged_frames ();
8572 }
8573
8574
8575 /* If showing the region, and mark has changed, we must redisplay
8576 the whole window. The assignment to this_line_start_pos prevents
8577 the optimization directly below this if-statement. */
8578 if (((!NILP (Vtransient_mark_mode)
8579 && !NILP (XBUFFER (w->buffer)->mark_active))
8580 != !NILP (w->region_showing))
8581 || (!NILP (w->region_showing)
8582 && !EQ (w->region_showing,
8583 Fmarker_position (XBUFFER (w->buffer)->mark))))
8584 CHARPOS (this_line_start_pos) = 0;
8585
8586 /* Optimize the case that only the line containing the cursor in the
8587 selected window has changed. Variables starting with this_ are
8588 set in display_line and record information about the line
8589 containing the cursor. */
8590 tlbufpos = this_line_start_pos;
8591 tlendpos = this_line_end_pos;
8592 if (!consider_all_windows_p
8593 && CHARPOS (tlbufpos) > 0
8594 && NILP (w->update_mode_line)
8595 && !current_buffer->clip_changed
8596 && FRAME_VISIBLE_P (XFRAME (w->frame))
8597 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8598 /* Make sure recorded data applies to current buffer, etc. */
8599 && this_line_buffer == current_buffer
8600 && current_buffer == XBUFFER (w->buffer)
8601 && NILP (w->force_start)
8602 /* Point must be on the line that we have info recorded about. */
8603 && PT >= CHARPOS (tlbufpos)
8604 && PT <= Z - CHARPOS (tlendpos)
8605 /* All text outside that line, including its final newline,
8606 must be unchanged */
8607 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8608 CHARPOS (tlendpos)))
8609 {
8610 if (CHARPOS (tlbufpos) > BEGV
8611 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8612 && (CHARPOS (tlbufpos) == ZV
8613 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8614 /* Former continuation line has disappeared by becoming empty */
8615 goto cancel;
8616 else if (XFASTINT (w->last_modified) < MODIFF
8617 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8618 || MINI_WINDOW_P (w))
8619 {
8620 /* We have to handle the case of continuation around a
8621 wide-column character (See the comment in indent.c around
8622 line 885).
8623
8624 For instance, in the following case:
8625
8626 -------- Insert --------
8627 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8628 J_I_ ==> J_I_ `^^' are cursors.
8629 ^^ ^^
8630 -------- --------
8631
8632 As we have to redraw the line above, we should goto cancel. */
8633
8634 struct it it;
8635 int line_height_before = this_line_pixel_height;
8636
8637 /* Note that start_display will handle the case that the
8638 line starting at tlbufpos is a continuation lines. */
8639 start_display (&it, w, tlbufpos);
8640
8641 /* Implementation note: It this still necessary? */
8642 if (it.current_x != this_line_start_x)
8643 goto cancel;
8644
8645 TRACE ((stderr, "trying display optimization 1\n"));
8646 w->cursor.vpos = -1;
8647 overlay_arrow_seen = 0;
8648 it.vpos = this_line_vpos;
8649 it.current_y = this_line_y;
8650 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8651 display_line (&it);
8652
8653 /* If line contains point, is not continued,
8654 and ends at same distance from eob as before, we win */
8655 if (w->cursor.vpos >= 0
8656 /* Line is not continued, otherwise this_line_start_pos
8657 would have been set to 0 in display_line. */
8658 && CHARPOS (this_line_start_pos)
8659 /* Line ends as before. */
8660 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8661 /* Line has same height as before. Otherwise other lines
8662 would have to be shifted up or down. */
8663 && this_line_pixel_height == line_height_before)
8664 {
8665 /* If this is not the window's last line, we must adjust
8666 the charstarts of the lines below. */
8667 if (it.current_y < it.last_visible_y)
8668 {
8669 struct glyph_row *row
8670 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8671 int delta, delta_bytes;
8672
8673 if (Z - CHARPOS (tlendpos) == ZV)
8674 {
8675 /* This line ends at end of (accessible part of)
8676 buffer. There is no newline to count. */
8677 delta = (Z
8678 - CHARPOS (tlendpos)
8679 - MATRIX_ROW_START_CHARPOS (row));
8680 delta_bytes = (Z_BYTE
8681 - BYTEPOS (tlendpos)
8682 - MATRIX_ROW_START_BYTEPOS (row));
8683 }
8684 else
8685 {
8686 /* This line ends in a newline. Must take
8687 account of the newline and the rest of the
8688 text that follows. */
8689 delta = (Z
8690 - CHARPOS (tlendpos)
8691 - MATRIX_ROW_START_CHARPOS (row));
8692 delta_bytes = (Z_BYTE
8693 - BYTEPOS (tlendpos)
8694 - MATRIX_ROW_START_BYTEPOS (row));
8695 }
8696
8697 increment_matrix_positions (w->current_matrix,
8698 this_line_vpos + 1,
8699 w->current_matrix->nrows,
8700 delta, delta_bytes);
8701 }
8702
8703 /* If this row displays text now but previously didn't,
8704 or vice versa, w->window_end_vpos may have to be
8705 adjusted. */
8706 if ((it.glyph_row - 1)->displays_text_p)
8707 {
8708 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8709 XSETINT (w->window_end_vpos, this_line_vpos);
8710 }
8711 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8712 && this_line_vpos > 0)
8713 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8714 w->window_end_valid = Qnil;
8715
8716 /* Update hint: No need to try to scroll in update_window. */
8717 w->desired_matrix->no_scrolling_p = 1;
8718
8719 #if GLYPH_DEBUG
8720 *w->desired_matrix->method = 0;
8721 debug_method_add (w, "optimization 1");
8722 #endif
8723 goto update;
8724 }
8725 else
8726 goto cancel;
8727 }
8728 else if (/* Cursor position hasn't changed. */
8729 PT == XFASTINT (w->last_point)
8730 /* Make sure the cursor was last displayed
8731 in this window. Otherwise we have to reposition it. */
8732 && 0 <= w->cursor.vpos
8733 && XINT (w->height) > w->cursor.vpos)
8734 {
8735 if (!must_finish)
8736 {
8737 do_pending_window_change (1);
8738
8739 /* We used to always goto end_of_redisplay here, but this
8740 isn't enough if we have a blinking cursor. */
8741 if (w->cursor_off_p == w->last_cursor_off_p)
8742 goto end_of_redisplay;
8743 }
8744 goto update;
8745 }
8746 /* If highlighting the region, or if the cursor is in the echo area,
8747 then we can't just move the cursor. */
8748 else if (! (!NILP (Vtransient_mark_mode)
8749 && !NILP (current_buffer->mark_active))
8750 && (EQ (selected_window, current_buffer->last_selected_window)
8751 || highlight_nonselected_windows)
8752 && NILP (w->region_showing)
8753 && NILP (Vshow_trailing_whitespace)
8754 && !cursor_in_echo_area)
8755 {
8756 struct it it;
8757 struct glyph_row *row;
8758
8759 /* Skip from tlbufpos to PT and see where it is. Note that
8760 PT may be in invisible text. If so, we will end at the
8761 next visible position. */
8762 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8763 NULL, DEFAULT_FACE_ID);
8764 it.current_x = this_line_start_x;
8765 it.current_y = this_line_y;
8766 it.vpos = this_line_vpos;
8767
8768 /* The call to move_it_to stops in front of PT, but
8769 moves over before-strings. */
8770 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8771
8772 if (it.vpos == this_line_vpos
8773 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8774 row->enabled_p))
8775 {
8776 xassert (this_line_vpos == it.vpos);
8777 xassert (this_line_y == it.current_y);
8778 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8779 #if GLYPH_DEBUG
8780 *w->desired_matrix->method = 0;
8781 debug_method_add (w, "optimization 3");
8782 #endif
8783 goto update;
8784 }
8785 else
8786 goto cancel;
8787 }
8788
8789 cancel:
8790 /* Text changed drastically or point moved off of line. */
8791 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8792 }
8793
8794 CHARPOS (this_line_start_pos) = 0;
8795 consider_all_windows_p |= buffer_shared > 1;
8796 ++clear_face_cache_count;
8797
8798
8799 /* Build desired matrices, and update the display. If
8800 consider_all_windows_p is non-zero, do it for all windows on all
8801 frames. Otherwise do it for selected_window, only. */
8802
8803 if (consider_all_windows_p)
8804 {
8805 Lisp_Object tail, frame;
8806 int i, n = 0, size = 50;
8807 struct frame **updated
8808 = (struct frame **) alloca (size * sizeof *updated);
8809
8810 /* Clear the face cache eventually. */
8811 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8812 {
8813 clear_face_cache (0);
8814 clear_face_cache_count = 0;
8815 }
8816
8817 /* Recompute # windows showing selected buffer. This will be
8818 incremented each time such a window is displayed. */
8819 buffer_shared = 0;
8820
8821 FOR_EACH_FRAME (tail, frame)
8822 {
8823 struct frame *f = XFRAME (frame);
8824
8825 if (FRAME_WINDOW_P (f) || f == sf)
8826 {
8827 if (clear_face_cache_count % 50 == 0
8828 && FRAME_WINDOW_P (f))
8829 clear_image_cache (f, 0);
8830
8831 /* Mark all the scroll bars to be removed; we'll redeem
8832 the ones we want when we redisplay their windows. */
8833 if (condemn_scroll_bars_hook)
8834 condemn_scroll_bars_hook (f);
8835
8836 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8837 redisplay_windows (FRAME_ROOT_WINDOW (f));
8838
8839 /* Any scroll bars which redisplay_windows should have
8840 nuked should now go away. */
8841 if (judge_scroll_bars_hook)
8842 judge_scroll_bars_hook (f);
8843
8844 /* If fonts changed, display again. */
8845 if (fonts_changed_p)
8846 goto retry;
8847
8848 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8849 {
8850 /* See if we have to hscroll. */
8851 if (hscroll_windows (f->root_window))
8852 goto retry;
8853
8854 /* Prevent various kinds of signals during display
8855 update. stdio is not robust about handling
8856 signals, which can cause an apparent I/O
8857 error. */
8858 if (interrupt_input)
8859 unrequest_sigio ();
8860 stop_polling ();
8861
8862 /* Update the display. */
8863 set_window_update_flags (XWINDOW (f->root_window), 1);
8864 pause |= update_frame (f, 0, 0);
8865 if (pause)
8866 break;
8867
8868 if (n == size)
8869 {
8870 int nbytes = size * sizeof *updated;
8871 struct frame **p = (struct frame **) alloca (2 * nbytes);
8872 bcopy (updated, p, nbytes);
8873 size *= 2;
8874 }
8875
8876 updated[n++] = f;
8877 }
8878 }
8879 }
8880
8881 /* Do the mark_window_display_accurate after all windows have
8882 been redisplayed because this call resets flags in buffers
8883 which are needed for proper redisplay. */
8884 for (i = 0; i < n; ++i)
8885 {
8886 struct frame *f = updated[i];
8887 mark_window_display_accurate (f->root_window, 1);
8888 if (frame_up_to_date_hook)
8889 frame_up_to_date_hook (f);
8890 }
8891 }
8892 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8893 {
8894 Lisp_Object mini_window;
8895 struct frame *mini_frame;
8896
8897 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
8898 internal_condition_case_1 (redisplay_window_1, selected_window, Qerror,
8899 redisplay_window_error);
8900
8901 /* Compare desired and current matrices, perform output. */
8902 update:
8903
8904 /* If fonts changed, display again. */
8905 if (fonts_changed_p)
8906 goto retry;
8907
8908 /* Prevent various kinds of signals during display update.
8909 stdio is not robust about handling signals,
8910 which can cause an apparent I/O error. */
8911 if (interrupt_input)
8912 unrequest_sigio ();
8913 stop_polling ();
8914
8915 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8916 {
8917 if (hscroll_windows (selected_window))
8918 goto retry;
8919
8920 XWINDOW (selected_window)->must_be_updated_p = 1;
8921 pause = update_frame (sf, 0, 0);
8922 }
8923
8924 /* We may have called echo_area_display at the top of this
8925 function. If the echo area is on another frame, that may
8926 have put text on a frame other than the selected one, so the
8927 above call to update_frame would not have caught it. Catch
8928 it here. */
8929 mini_window = FRAME_MINIBUF_WINDOW (sf);
8930 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8931
8932 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8933 {
8934 XWINDOW (mini_window)->must_be_updated_p = 1;
8935 pause |= update_frame (mini_frame, 0, 0);
8936 if (!pause && hscroll_windows (mini_window))
8937 goto retry;
8938 }
8939 }
8940
8941 /* If display was paused because of pending input, make sure we do a
8942 thorough update the next time. */
8943 if (pause)
8944 {
8945 /* Prevent the optimization at the beginning of
8946 redisplay_internal that tries a single-line update of the
8947 line containing the cursor in the selected window. */
8948 CHARPOS (this_line_start_pos) = 0;
8949
8950 /* Let the overlay arrow be updated the next time. */
8951 if (!NILP (last_arrow_position))
8952 {
8953 last_arrow_position = Qt;
8954 last_arrow_string = Qt;
8955 }
8956
8957 /* If we pause after scrolling, some rows in the current
8958 matrices of some windows are not valid. */
8959 if (!WINDOW_FULL_WIDTH_P (w)
8960 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8961 update_mode_lines = 1;
8962 }
8963 else
8964 {
8965 if (!consider_all_windows_p)
8966 {
8967 /* This has already been done above if
8968 consider_all_windows_p is set. */
8969 mark_window_display_accurate_1 (w, 1);
8970
8971 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8972 last_arrow_string = Voverlay_arrow_string;
8973
8974 if (frame_up_to_date_hook != 0)
8975 frame_up_to_date_hook (sf);
8976 }
8977
8978 update_mode_lines = 0;
8979 windows_or_buffers_changed = 0;
8980 }
8981
8982 /* Start SIGIO interrupts coming again. Having them off during the
8983 code above makes it less likely one will discard output, but not
8984 impossible, since there might be stuff in the system buffer here.
8985 But it is much hairier to try to do anything about that. */
8986 if (interrupt_input)
8987 request_sigio ();
8988 start_polling ();
8989
8990 /* If a frame has become visible which was not before, redisplay
8991 again, so that we display it. Expose events for such a frame
8992 (which it gets when becoming visible) don't call the parts of
8993 redisplay constructing glyphs, so simply exposing a frame won't
8994 display anything in this case. So, we have to display these
8995 frames here explicitly. */
8996 if (!pause)
8997 {
8998 Lisp_Object tail, frame;
8999 int new_count = 0;
9000
9001 FOR_EACH_FRAME (tail, frame)
9002 {
9003 int this_is_visible = 0;
9004
9005 if (XFRAME (frame)->visible)
9006 this_is_visible = 1;
9007 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9008 if (XFRAME (frame)->visible)
9009 this_is_visible = 1;
9010
9011 if (this_is_visible)
9012 new_count++;
9013 }
9014
9015 if (new_count != number_of_visible_frames)
9016 windows_or_buffers_changed++;
9017 }
9018
9019 /* Change frame size now if a change is pending. */
9020 do_pending_window_change (1);
9021
9022 /* If we just did a pending size change, or have additional
9023 visible frames, redisplay again. */
9024 if (windows_or_buffers_changed && !pause)
9025 goto retry;
9026
9027 end_of_redisplay:;
9028
9029 unbind_to (count, Qnil);
9030 }
9031
9032
9033 /* Redisplay, but leave alone any recent echo area message unless
9034 another message has been requested in its place.
9035
9036 This is useful in situations where you need to redisplay but no
9037 user action has occurred, making it inappropriate for the message
9038 area to be cleared. See tracking_off and
9039 wait_reading_process_input for examples of these situations.
9040
9041 FROM_WHERE is an integer saying from where this function was
9042 called. This is useful for debugging. */
9043
9044 void
9045 redisplay_preserve_echo_area (from_where)
9046 int from_where;
9047 {
9048 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9049
9050 if (!NILP (echo_area_buffer[1]))
9051 {
9052 /* We have a previously displayed message, but no current
9053 message. Redisplay the previous message. */
9054 display_last_displayed_message_p = 1;
9055 redisplay_internal (1);
9056 display_last_displayed_message_p = 0;
9057 }
9058 else
9059 redisplay_internal (1);
9060 }
9061
9062
9063 /* Function registered with record_unwind_protect in
9064 redisplay_internal. Clears the flag indicating that a redisplay is
9065 in progress. */
9066
9067 static Lisp_Object
9068 unwind_redisplay (old_redisplaying_p)
9069 Lisp_Object old_redisplaying_p;
9070 {
9071 redisplaying_p = XFASTINT (old_redisplaying_p);
9072 return Qnil;
9073 }
9074
9075
9076 /* Mark the display of window W as accurate or inaccurate. If
9077 ACCURATE_P is non-zero mark display of W as accurate. If
9078 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9079 redisplay_internal is called. */
9080
9081 static void
9082 mark_window_display_accurate_1 (w, accurate_p)
9083 struct window *w;
9084 int accurate_p;
9085 {
9086 if (BUFFERP (w->buffer))
9087 {
9088 struct buffer *b = XBUFFER (w->buffer);
9089
9090 w->last_modified
9091 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9092 w->last_overlay_modified
9093 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9094 w->last_had_star
9095 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9096
9097 if (accurate_p)
9098 {
9099 b->clip_changed = 0;
9100 b->prevent_redisplay_optimizations_p = 0;
9101
9102 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9103 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9104 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9105 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9106
9107 w->current_matrix->buffer = b;
9108 w->current_matrix->begv = BUF_BEGV (b);
9109 w->current_matrix->zv = BUF_ZV (b);
9110
9111 w->last_cursor = w->cursor;
9112 w->last_cursor_off_p = w->cursor_off_p;
9113
9114 if (w == XWINDOW (selected_window))
9115 w->last_point = make_number (BUF_PT (b));
9116 else
9117 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9118 }
9119 }
9120
9121 if (accurate_p)
9122 {
9123 w->window_end_valid = w->buffer;
9124 #if 0 /* This is incorrect with variable-height lines. */
9125 xassert (XINT (w->window_end_vpos)
9126 < (XINT (w->height)
9127 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9128 #endif
9129 w->update_mode_line = Qnil;
9130 }
9131 }
9132
9133
9134 /* Mark the display of windows in the window tree rooted at WINDOW as
9135 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9136 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9137 be redisplayed the next time redisplay_internal is called. */
9138
9139 void
9140 mark_window_display_accurate (window, accurate_p)
9141 Lisp_Object window;
9142 int accurate_p;
9143 {
9144 struct window *w;
9145
9146 for (; !NILP (window); window = w->next)
9147 {
9148 w = XWINDOW (window);
9149 mark_window_display_accurate_1 (w, accurate_p);
9150
9151 if (!NILP (w->vchild))
9152 mark_window_display_accurate (w->vchild, accurate_p);
9153 if (!NILP (w->hchild))
9154 mark_window_display_accurate (w->hchild, accurate_p);
9155 }
9156
9157 if (accurate_p)
9158 {
9159 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9160 last_arrow_string = Voverlay_arrow_string;
9161 }
9162 else
9163 {
9164 /* Force a thorough redisplay the next time by setting
9165 last_arrow_position and last_arrow_string to t, which is
9166 unequal to any useful value of Voverlay_arrow_... */
9167 last_arrow_position = Qt;
9168 last_arrow_string = Qt;
9169 }
9170 }
9171
9172
9173 /* Return value in display table DP (Lisp_Char_Table *) for character
9174 C. Since a display table doesn't have any parent, we don't have to
9175 follow parent. Do not call this function directly but use the
9176 macro DISP_CHAR_VECTOR. */
9177
9178 Lisp_Object
9179 disp_char_vector (dp, c)
9180 struct Lisp_Char_Table *dp;
9181 int c;
9182 {
9183 int code[4], i;
9184 Lisp_Object val;
9185
9186 if (SINGLE_BYTE_CHAR_P (c))
9187 return (dp->contents[c]);
9188
9189 SPLIT_CHAR (c, code[0], code[1], code[2]);
9190 if (code[1] < 32)
9191 code[1] = -1;
9192 else if (code[2] < 32)
9193 code[2] = -1;
9194
9195 /* Here, the possible range of code[0] (== charset ID) is
9196 128..max_charset. Since the top level char table contains data
9197 for multibyte characters after 256th element, we must increment
9198 code[0] by 128 to get a correct index. */
9199 code[0] += 128;
9200 code[3] = -1; /* anchor */
9201
9202 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9203 {
9204 val = dp->contents[code[i]];
9205 if (!SUB_CHAR_TABLE_P (val))
9206 return (NILP (val) ? dp->defalt : val);
9207 }
9208
9209 /* Here, val is a sub char table. We return the default value of
9210 it. */
9211 return (dp->defalt);
9212 }
9213
9214
9215 \f
9216 /***********************************************************************
9217 Window Redisplay
9218 ***********************************************************************/
9219
9220 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9221
9222 static void
9223 redisplay_windows (window)
9224 Lisp_Object window;
9225 {
9226 while (!NILP (window))
9227 {
9228 struct window *w = XWINDOW (window);
9229
9230 if (!NILP (w->hchild))
9231 redisplay_windows (w->hchild);
9232 else if (!NILP (w->vchild))
9233 redisplay_windows (w->vchild);
9234 else
9235 {
9236 displayed_buffer = XBUFFER (w->buffer);
9237 internal_condition_case_1 (redisplay_window_0, window, Qerror,
9238 redisplay_window_error);
9239 }
9240
9241 window = w->next;
9242 }
9243 }
9244
9245 static Lisp_Object
9246 redisplay_window_error ()
9247 {
9248 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9249 return Qnil;
9250 }
9251
9252 static Lisp_Object
9253 redisplay_window_0 (window)
9254 Lisp_Object window;
9255 {
9256 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9257 redisplay_window (window, 0);
9258 return Qnil;
9259 }
9260
9261 static Lisp_Object
9262 redisplay_window_1 (window)
9263 Lisp_Object window;
9264 {
9265 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9266 redisplay_window (window, 1);
9267 return Qnil;
9268 }
9269 \f
9270 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9271 DELTA is the number of bytes by which positions recorded in ROW
9272 differ from current buffer positions. */
9273
9274 void
9275 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9276 struct window *w;
9277 struct glyph_row *row;
9278 struct glyph_matrix *matrix;
9279 int delta, delta_bytes, dy, dvpos;
9280 {
9281 struct glyph *glyph = row->glyphs[TEXT_AREA];
9282 struct glyph *end = glyph + row->used[TEXT_AREA];
9283 int x = row->x;
9284 int pt_old = PT - delta;
9285
9286 /* Skip over glyphs not having an object at the start of the row.
9287 These are special glyphs like truncation marks on terminal
9288 frames. */
9289 if (row->displays_text_p)
9290 while (glyph < end
9291 && INTEGERP (glyph->object)
9292 && glyph->charpos < 0)
9293 {
9294 x += glyph->pixel_width;
9295 ++glyph;
9296 }
9297
9298 while (glyph < end
9299 && !INTEGERP (glyph->object)
9300 && (!BUFFERP (glyph->object)
9301 || glyph->charpos < pt_old))
9302 {
9303 x += glyph->pixel_width;
9304 ++glyph;
9305 }
9306
9307 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9308 w->cursor.x = x;
9309 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9310 w->cursor.y = row->y + dy;
9311
9312 if (w == XWINDOW (selected_window))
9313 {
9314 if (!row->continued_p
9315 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9316 && row->x == 0)
9317 {
9318 this_line_buffer = XBUFFER (w->buffer);
9319
9320 CHARPOS (this_line_start_pos)
9321 = MATRIX_ROW_START_CHARPOS (row) + delta;
9322 BYTEPOS (this_line_start_pos)
9323 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9324
9325 CHARPOS (this_line_end_pos)
9326 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9327 BYTEPOS (this_line_end_pos)
9328 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9329
9330 this_line_y = w->cursor.y;
9331 this_line_pixel_height = row->height;
9332 this_line_vpos = w->cursor.vpos;
9333 this_line_start_x = row->x;
9334 }
9335 else
9336 CHARPOS (this_line_start_pos) = 0;
9337 }
9338 }
9339
9340
9341 /* Run window scroll functions, if any, for WINDOW with new window
9342 start STARTP. Sets the window start of WINDOW to that position.
9343
9344 We assume that the window's buffer is really current. */
9345
9346 static INLINE struct text_pos
9347 run_window_scroll_functions (window, startp)
9348 Lisp_Object window;
9349 struct text_pos startp;
9350 {
9351 struct window *w = XWINDOW (window);
9352 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9353
9354 if (current_buffer != XBUFFER (w->buffer))
9355 abort ();
9356
9357 if (!NILP (Vwindow_scroll_functions))
9358 {
9359 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9360 make_number (CHARPOS (startp)));
9361 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9362 /* In case the hook functions switch buffers. */
9363 if (current_buffer != XBUFFER (w->buffer))
9364 set_buffer_internal_1 (XBUFFER (w->buffer));
9365 }
9366
9367 return startp;
9368 }
9369
9370
9371 /* Modify the desired matrix of window W and W->vscroll so that the
9372 line containing the cursor is fully visible. If this requires
9373 larger matrices than are allocated, set fonts_changed_p and return
9374 0. */
9375
9376 static int
9377 make_cursor_line_fully_visible (w)
9378 struct window *w;
9379 {
9380 struct glyph_matrix *matrix;
9381 struct glyph_row *row;
9382 int window_height;
9383
9384 /* It's not always possible to find the cursor, e.g, when a window
9385 is full of overlay strings. Don't do anything in that case. */
9386 if (w->cursor.vpos < 0)
9387 return 1;
9388
9389 matrix = w->desired_matrix;
9390 row = MATRIX_ROW (matrix, w->cursor.vpos);
9391
9392 /* If the cursor row is not partially visible, there's nothing
9393 to do. */
9394 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9395 return 1;
9396
9397 /* If the row the cursor is in is taller than the window's height,
9398 it's not clear what to do, so do nothing. */
9399 window_height = window_box_height (w);
9400 if (row->height >= window_height)
9401 return 1;
9402
9403 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9404 {
9405 int dy = row->height - row->visible_height;
9406 w->vscroll = 0;
9407 w->cursor.y += dy;
9408 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9409 }
9410 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9411 {
9412 int dy = - (row->height - row->visible_height);
9413 w->vscroll = dy;
9414 w->cursor.y += dy;
9415 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9416 }
9417
9418 /* When we change the cursor y-position of the selected window,
9419 change this_line_y as well so that the display optimization for
9420 the cursor line of the selected window in redisplay_internal uses
9421 the correct y-position. */
9422 if (w == XWINDOW (selected_window))
9423 this_line_y = w->cursor.y;
9424
9425 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9426 redisplay with larger matrices. */
9427 if (matrix->nrows < required_matrix_height (w))
9428 {
9429 fonts_changed_p = 1;
9430 return 0;
9431 }
9432
9433 return 1;
9434 }
9435
9436
9437 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9438 non-zero means only WINDOW is redisplayed in redisplay_internal.
9439 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9440 in redisplay_window to bring a partially visible line into view in
9441 the case that only the cursor has moved.
9442
9443 Value is
9444
9445 1 if scrolling succeeded
9446
9447 0 if scrolling didn't find point.
9448
9449 -1 if new fonts have been loaded so that we must interrupt
9450 redisplay, adjust glyph matrices, and try again. */
9451
9452 enum
9453 {
9454 SCROLLING_SUCCESS,
9455 SCROLLING_FAILED,
9456 SCROLLING_NEED_LARGER_MATRICES
9457 };
9458
9459 static int
9460 try_scrolling (window, just_this_one_p, scroll_conservatively,
9461 scroll_step, temp_scroll_step)
9462 Lisp_Object window;
9463 int just_this_one_p;
9464 int scroll_conservatively, scroll_step;
9465 int temp_scroll_step;
9466 {
9467 struct window *w = XWINDOW (window);
9468 struct frame *f = XFRAME (w->frame);
9469 struct text_pos scroll_margin_pos;
9470 struct text_pos pos;
9471 struct text_pos startp;
9472 struct it it;
9473 Lisp_Object window_end;
9474 int this_scroll_margin;
9475 int dy = 0;
9476 int scroll_max;
9477 int rc;
9478 int amount_to_scroll = 0;
9479 Lisp_Object aggressive;
9480 int height;
9481
9482 #if GLYPH_DEBUG
9483 debug_method_add (w, "try_scrolling");
9484 #endif
9485
9486 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9487
9488 /* Compute scroll margin height in pixels. We scroll when point is
9489 within this distance from the top or bottom of the window. */
9490 if (scroll_margin > 0)
9491 {
9492 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9493 this_scroll_margin *= CANON_Y_UNIT (f);
9494 }
9495 else
9496 this_scroll_margin = 0;
9497
9498 /* Compute how much we should try to scroll maximally to bring point
9499 into view. */
9500 if (scroll_step || scroll_conservatively || temp_scroll_step)
9501 scroll_max = max (scroll_step,
9502 max (scroll_conservatively, temp_scroll_step));
9503 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9504 || NUMBERP (current_buffer->scroll_up_aggressively))
9505 /* We're trying to scroll because of aggressive scrolling
9506 but no scroll_step is set. Choose an arbitrary one. Maybe
9507 there should be a variable for this. */
9508 scroll_max = 10;
9509 else
9510 scroll_max = 0;
9511 scroll_max *= CANON_Y_UNIT (f);
9512
9513 /* Decide whether we have to scroll down. Start at the window end
9514 and move this_scroll_margin up to find the position of the scroll
9515 margin. */
9516 window_end = Fwindow_end (window, Qt);
9517 CHARPOS (scroll_margin_pos) = XINT (window_end);
9518 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9519 if (this_scroll_margin)
9520 {
9521 start_display (&it, w, scroll_margin_pos);
9522 move_it_vertically (&it, - this_scroll_margin);
9523 scroll_margin_pos = it.current.pos;
9524 }
9525
9526 if (PT >= CHARPOS (scroll_margin_pos))
9527 {
9528 int y0;
9529
9530 /* Point is in the scroll margin at the bottom of the window, or
9531 below. Compute a new window start that makes point visible. */
9532
9533 /* Compute the distance from the scroll margin to PT.
9534 Give up if the distance is greater than scroll_max. */
9535 start_display (&it, w, scroll_margin_pos);
9536 y0 = it.current_y;
9537 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9538 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9539
9540 /* To make point visible, we have to move the window start
9541 down so that the line the cursor is in is visible, which
9542 means we have to add in the height of the cursor line. */
9543 dy = line_bottom_y (&it) - y0;
9544
9545 if (dy > scroll_max)
9546 return SCROLLING_FAILED;
9547
9548 /* Move the window start down. If scrolling conservatively,
9549 move it just enough down to make point visible. If
9550 scroll_step is set, move it down by scroll_step. */
9551 start_display (&it, w, startp);
9552
9553 if (scroll_conservatively)
9554 amount_to_scroll
9555 = max (max (dy, CANON_Y_UNIT (f)),
9556 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9557 else if (scroll_step || temp_scroll_step)
9558 amount_to_scroll = scroll_max;
9559 else
9560 {
9561 aggressive = current_buffer->scroll_up_aggressively;
9562 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9563 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9564 if (NUMBERP (aggressive))
9565 amount_to_scroll = XFLOATINT (aggressive) * height;
9566 }
9567
9568 if (amount_to_scroll <= 0)
9569 return SCROLLING_FAILED;
9570
9571 move_it_vertically (&it, amount_to_scroll);
9572 startp = it.current.pos;
9573 }
9574 else
9575 {
9576 /* See if point is inside the scroll margin at the top of the
9577 window. */
9578 scroll_margin_pos = startp;
9579 if (this_scroll_margin)
9580 {
9581 start_display (&it, w, startp);
9582 move_it_vertically (&it, this_scroll_margin);
9583 scroll_margin_pos = it.current.pos;
9584 }
9585
9586 if (PT < CHARPOS (scroll_margin_pos))
9587 {
9588 /* Point is in the scroll margin at the top of the window or
9589 above what is displayed in the window. */
9590 int y0;
9591
9592 /* Compute the vertical distance from PT to the scroll
9593 margin position. Give up if distance is greater than
9594 scroll_max. */
9595 SET_TEXT_POS (pos, PT, PT_BYTE);
9596 start_display (&it, w, pos);
9597 y0 = it.current_y;
9598 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9599 it.last_visible_y, -1,
9600 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9601 dy = it.current_y - y0;
9602 if (dy > scroll_max)
9603 return SCROLLING_FAILED;
9604
9605 /* Compute new window start. */
9606 start_display (&it, w, startp);
9607
9608 if (scroll_conservatively)
9609 amount_to_scroll =
9610 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9611 else if (scroll_step || temp_scroll_step)
9612 amount_to_scroll = scroll_max;
9613 else
9614 {
9615 aggressive = current_buffer->scroll_down_aggressively;
9616 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9617 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9618 if (NUMBERP (aggressive))
9619 amount_to_scroll = XFLOATINT (aggressive) * height;
9620 }
9621
9622 if (amount_to_scroll <= 0)
9623 return SCROLLING_FAILED;
9624
9625 move_it_vertically (&it, - amount_to_scroll);
9626 startp = it.current.pos;
9627 }
9628 }
9629
9630 /* Run window scroll functions. */
9631 startp = run_window_scroll_functions (window, startp);
9632
9633 /* Display the window. Give up if new fonts are loaded, or if point
9634 doesn't appear. */
9635 if (!try_window (window, startp))
9636 rc = SCROLLING_NEED_LARGER_MATRICES;
9637 else if (w->cursor.vpos < 0)
9638 {
9639 clear_glyph_matrix (w->desired_matrix);
9640 rc = SCROLLING_FAILED;
9641 }
9642 else
9643 {
9644 /* Maybe forget recorded base line for line number display. */
9645 if (!just_this_one_p
9646 || current_buffer->clip_changed
9647 || BEG_UNCHANGED < CHARPOS (startp))
9648 w->base_line_number = Qnil;
9649
9650 /* If cursor ends up on a partially visible line, shift display
9651 lines up or down. If that fails because we need larger
9652 matrices, give up. */
9653 if (!make_cursor_line_fully_visible (w))
9654 rc = SCROLLING_NEED_LARGER_MATRICES;
9655 else
9656 rc = SCROLLING_SUCCESS;
9657 }
9658
9659 return rc;
9660 }
9661
9662
9663 /* Compute a suitable window start for window W if display of W starts
9664 on a continuation line. Value is non-zero if a new window start
9665 was computed.
9666
9667 The new window start will be computed, based on W's width, starting
9668 from the start of the continued line. It is the start of the
9669 screen line with the minimum distance from the old start W->start. */
9670
9671 static int
9672 compute_window_start_on_continuation_line (w)
9673 struct window *w;
9674 {
9675 struct text_pos pos, start_pos;
9676 int window_start_changed_p = 0;
9677
9678 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9679
9680 /* If window start is on a continuation line... Window start may be
9681 < BEGV in case there's invisible text at the start of the
9682 buffer (M-x rmail, for example). */
9683 if (CHARPOS (start_pos) > BEGV
9684 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9685 {
9686 struct it it;
9687 struct glyph_row *row;
9688
9689 /* Handle the case that the window start is out of range. */
9690 if (CHARPOS (start_pos) < BEGV)
9691 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9692 else if (CHARPOS (start_pos) > ZV)
9693 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9694
9695 /* Find the start of the continued line. This should be fast
9696 because scan_buffer is fast (newline cache). */
9697 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9698 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9699 row, DEFAULT_FACE_ID);
9700 reseat_at_previous_visible_line_start (&it);
9701
9702 /* If the line start is "too far" away from the window start,
9703 say it takes too much time to compute a new window start. */
9704 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9705 < XFASTINT (w->height) * XFASTINT (w->width))
9706 {
9707 int min_distance, distance;
9708
9709 /* Move forward by display lines to find the new window
9710 start. If window width was enlarged, the new start can
9711 be expected to be > the old start. If window width was
9712 decreased, the new window start will be < the old start.
9713 So, we're looking for the display line start with the
9714 minimum distance from the old window start. */
9715 pos = it.current.pos;
9716 min_distance = INFINITY;
9717 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9718 distance < min_distance)
9719 {
9720 min_distance = distance;
9721 pos = it.current.pos;
9722 move_it_by_lines (&it, 1, 0);
9723 }
9724
9725 /* Set the window start there. */
9726 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9727 window_start_changed_p = 1;
9728 }
9729 }
9730
9731 return window_start_changed_p;
9732 }
9733
9734
9735 /* Try cursor movement in case text has not changes in window WINDOW,
9736 with window start STARTP. Value is
9737
9738 CURSOR_MOVEMENT_SUCCESS if successful
9739
9740 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9741
9742 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9743 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9744 we want to scroll as if scroll-step were set to 1. See the code.
9745
9746 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9747 which case we have to abort this redisplay, and adjust matrices
9748 first. */
9749
9750 enum
9751 {
9752 CURSOR_MOVEMENT_SUCCESS,
9753 CURSOR_MOVEMENT_CANNOT_BE_USED,
9754 CURSOR_MOVEMENT_MUST_SCROLL,
9755 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9756 };
9757
9758 static int
9759 try_cursor_movement (window, startp, scroll_step)
9760 Lisp_Object window;
9761 struct text_pos startp;
9762 int *scroll_step;
9763 {
9764 struct window *w = XWINDOW (window);
9765 struct frame *f = XFRAME (w->frame);
9766 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
9767
9768 #if GLYPH_DEBUG
9769 if (inhibit_try_cursor_movement)
9770 return rc;
9771 #endif
9772
9773 /* Handle case where text has not changed, only point, and it has
9774 not moved off the frame. */
9775 if (/* Point may be in this window. */
9776 PT >= CHARPOS (startp)
9777 /* Selective display hasn't changed. */
9778 && !current_buffer->clip_changed
9779 /* Function force-mode-line-update is used to force a thorough
9780 redisplay. It sets either windows_or_buffers_changed or
9781 update_mode_lines. So don't take a shortcut here for these
9782 cases. */
9783 && !update_mode_lines
9784 && !windows_or_buffers_changed
9785 /* Can't use this case if highlighting a region. When a
9786 region exists, cursor movement has to do more than just
9787 set the cursor. */
9788 && !(!NILP (Vtransient_mark_mode)
9789 && !NILP (current_buffer->mark_active))
9790 && NILP (w->region_showing)
9791 && NILP (Vshow_trailing_whitespace)
9792 /* Right after splitting windows, last_point may be nil. */
9793 && INTEGERP (w->last_point)
9794 /* This code is not used for mini-buffer for the sake of the case
9795 of redisplaying to replace an echo area message; since in
9796 that case the mini-buffer contents per se are usually
9797 unchanged. This code is of no real use in the mini-buffer
9798 since the handling of this_line_start_pos, etc., in redisplay
9799 handles the same cases. */
9800 && !EQ (window, minibuf_window)
9801 /* When splitting windows or for new windows, it happens that
9802 redisplay is called with a nil window_end_vpos or one being
9803 larger than the window. This should really be fixed in
9804 window.c. I don't have this on my list, now, so we do
9805 approximately the same as the old redisplay code. --gerd. */
9806 && INTEGERP (w->window_end_vpos)
9807 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9808 && (FRAME_WINDOW_P (f)
9809 || !MARKERP (Voverlay_arrow_position)
9810 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9811 {
9812 int this_scroll_margin;
9813 struct glyph_row *row = NULL;
9814
9815 #if GLYPH_DEBUG
9816 debug_method_add (w, "cursor movement");
9817 #endif
9818
9819 /* Scroll if point within this distance from the top or bottom
9820 of the window. This is a pixel value. */
9821 this_scroll_margin = max (0, scroll_margin);
9822 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9823 this_scroll_margin *= CANON_Y_UNIT (f);
9824
9825 /* Start with the row the cursor was displayed during the last
9826 not paused redisplay. Give up if that row is not valid. */
9827 if (w->last_cursor.vpos < 0
9828 || w->last_cursor.vpos >= w->current_matrix->nrows)
9829 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9830 else
9831 {
9832 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9833 if (row->mode_line_p)
9834 ++row;
9835 if (!row->enabled_p)
9836 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9837 }
9838
9839 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
9840 {
9841 int scroll_p = 0;
9842 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9843
9844 if (PT > XFASTINT (w->last_point))
9845 {
9846 /* Point has moved forward. */
9847 while (MATRIX_ROW_END_CHARPOS (row) < PT
9848 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9849 {
9850 xassert (row->enabled_p);
9851 ++row;
9852 }
9853
9854 /* The end position of a row equals the start position
9855 of the next row. If PT is there, we would rather
9856 display it in the next line. */
9857 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9858 && MATRIX_ROW_END_CHARPOS (row) == PT
9859 && !cursor_row_p (w, row))
9860 ++row;
9861
9862 /* If within the scroll margin, scroll. Note that
9863 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9864 the next line would be drawn, and that
9865 this_scroll_margin can be zero. */
9866 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9867 || PT > MATRIX_ROW_END_CHARPOS (row)
9868 /* Line is completely visible last line in window
9869 and PT is to be set in the next line. */
9870 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9871 && PT == MATRIX_ROW_END_CHARPOS (row)
9872 && !row->ends_at_zv_p
9873 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9874 scroll_p = 1;
9875 }
9876 else if (PT < XFASTINT (w->last_point))
9877 {
9878 /* Cursor has to be moved backward. Note that PT >=
9879 CHARPOS (startp) because of the outer
9880 if-statement. */
9881 while (!row->mode_line_p
9882 && (MATRIX_ROW_START_CHARPOS (row) > PT
9883 || (MATRIX_ROW_START_CHARPOS (row) == PT
9884 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9885 && (row->y > this_scroll_margin
9886 || CHARPOS (startp) == BEGV))
9887 {
9888 xassert (row->enabled_p);
9889 --row;
9890 }
9891
9892 /* Consider the following case: Window starts at BEGV,
9893 there is invisible, intangible text at BEGV, so that
9894 display starts at some point START > BEGV. It can
9895 happen that we are called with PT somewhere between
9896 BEGV and START. Try to handle that case. */
9897 if (row < w->current_matrix->rows
9898 || row->mode_line_p)
9899 {
9900 row = w->current_matrix->rows;
9901 if (row->mode_line_p)
9902 ++row;
9903 }
9904
9905 /* Due to newlines in overlay strings, we may have to
9906 skip forward over overlay strings. */
9907 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9908 && MATRIX_ROW_END_CHARPOS (row) == PT
9909 && !cursor_row_p (w, row))
9910 ++row;
9911
9912 /* If within the scroll margin, scroll. */
9913 if (row->y < this_scroll_margin
9914 && CHARPOS (startp) != BEGV)
9915 scroll_p = 1;
9916 }
9917
9918 if (PT < MATRIX_ROW_START_CHARPOS (row)
9919 || PT > MATRIX_ROW_END_CHARPOS (row))
9920 {
9921 /* if PT is not in the glyph row, give up. */
9922 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9923 }
9924 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9925 {
9926 if (PT == MATRIX_ROW_END_CHARPOS (row)
9927 && !row->ends_at_zv_p
9928 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
9929 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9930 else if (row->height > window_box_height (w))
9931 {
9932 /* If we end up in a partially visible line, let's
9933 make it fully visible, except when it's taller
9934 than the window, in which case we can't do much
9935 about it. */
9936 *scroll_step = 1;
9937 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9938 }
9939 else
9940 {
9941 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9942 try_window (window, startp);
9943 if (!make_cursor_line_fully_visible (w))
9944 rc = CURSOR_MOVEMENT_NEED_LARGER_MATRICES;
9945 else
9946 rc = CURSOR_MOVEMENT_SUCCESS;
9947 }
9948 }
9949 else if (scroll_p)
9950 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9951 else
9952 {
9953 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9954 rc = CURSOR_MOVEMENT_SUCCESS;
9955 }
9956 }
9957 }
9958
9959 return rc;
9960 }
9961
9962
9963 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9964 selected_window is redisplayed. */
9965
9966 static void
9967 redisplay_window (window, just_this_one_p)
9968 Lisp_Object window;
9969 int just_this_one_p;
9970 {
9971 struct window *w = XWINDOW (window);
9972 struct frame *f = XFRAME (w->frame);
9973 struct buffer *buffer = XBUFFER (w->buffer);
9974 struct buffer *old = current_buffer;
9975 struct text_pos lpoint, opoint, startp;
9976 int update_mode_line;
9977 int tem;
9978 struct it it;
9979 /* Record it now because it's overwritten. */
9980 int current_matrix_up_to_date_p = 0;
9981 int temp_scroll_step = 0;
9982 int count = BINDING_STACK_SIZE ();
9983 int rc;
9984
9985 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9986 opoint = lpoint;
9987
9988 /* W must be a leaf window here. */
9989 xassert (!NILP (w->buffer));
9990 #if GLYPH_DEBUG
9991 *w->desired_matrix->method = 0;
9992 #endif
9993
9994 specbind (Qinhibit_point_motion_hooks, Qt);
9995
9996 reconsider_clip_changes (w, buffer);
9997
9998 /* Has the mode line to be updated? */
9999 update_mode_line = (!NILP (w->update_mode_line)
10000 || update_mode_lines
10001 || buffer->clip_changed);
10002
10003 if (MINI_WINDOW_P (w))
10004 {
10005 if (w == XWINDOW (echo_area_window)
10006 && !NILP (echo_area_buffer[0]))
10007 {
10008 if (update_mode_line)
10009 /* We may have to update a tty frame's menu bar or a
10010 tool-bar. Example `M-x C-h C-h C-g'. */
10011 goto finish_menu_bars;
10012 else
10013 /* We've already displayed the echo area glyphs in this window. */
10014 goto finish_scroll_bars;
10015 }
10016 else if (w != XWINDOW (minibuf_window))
10017 {
10018 /* W is a mini-buffer window, but it's not the currently
10019 active one, so clear it. */
10020 int yb = window_text_bottom_y (w);
10021 struct glyph_row *row;
10022 int y;
10023
10024 for (y = 0, row = w->desired_matrix->rows;
10025 y < yb;
10026 y += row->height, ++row)
10027 blank_row (w, row, y);
10028 goto finish_scroll_bars;
10029 }
10030
10031 clear_glyph_matrix (w->desired_matrix);
10032 }
10033
10034 /* Otherwise set up data on this window; select its buffer and point
10035 value. */
10036 /* Really select the buffer, for the sake of buffer-local
10037 variables. */
10038 set_buffer_internal_1 (XBUFFER (w->buffer));
10039 SET_TEXT_POS (opoint, PT, PT_BYTE);
10040
10041 current_matrix_up_to_date_p
10042 = (!NILP (w->window_end_valid)
10043 && !current_buffer->clip_changed
10044 && XFASTINT (w->last_modified) >= MODIFF
10045 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10046
10047 /* When windows_or_buffers_changed is non-zero, we can't rely on
10048 the window end being valid, so set it to nil there. */
10049 if (windows_or_buffers_changed)
10050 {
10051 /* If window starts on a continuation line, maybe adjust the
10052 window start in case the window's width changed. */
10053 if (XMARKER (w->start)->buffer == current_buffer)
10054 compute_window_start_on_continuation_line (w);
10055
10056 w->window_end_valid = Qnil;
10057 }
10058
10059 /* Some sanity checks. */
10060 CHECK_WINDOW_END (w);
10061 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
10062 abort ();
10063 if (BYTEPOS (opoint) < CHARPOS (opoint))
10064 abort ();
10065
10066 /* If %c is in mode line, update it if needed. */
10067 if (!NILP (w->column_number_displayed)
10068 /* This alternative quickly identifies a common case
10069 where no change is needed. */
10070 && !(PT == XFASTINT (w->last_point)
10071 && XFASTINT (w->last_modified) >= MODIFF
10072 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10073 && XFASTINT (w->column_number_displayed) != current_column ())
10074 update_mode_line = 1;
10075
10076 /* Count number of windows showing the selected buffer. An indirect
10077 buffer counts as its base buffer. */
10078 if (!just_this_one_p)
10079 {
10080 struct buffer *current_base, *window_base;
10081 current_base = current_buffer;
10082 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10083 if (current_base->base_buffer)
10084 current_base = current_base->base_buffer;
10085 if (window_base->base_buffer)
10086 window_base = window_base->base_buffer;
10087 if (current_base == window_base)
10088 buffer_shared++;
10089 }
10090
10091 /* Point refers normally to the selected window. For any other
10092 window, set up appropriate value. */
10093 if (!EQ (window, selected_window))
10094 {
10095 int new_pt = XMARKER (w->pointm)->charpos;
10096 int new_pt_byte = marker_byte_position (w->pointm);
10097 if (new_pt < BEGV)
10098 {
10099 new_pt = BEGV;
10100 new_pt_byte = BEGV_BYTE;
10101 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10102 }
10103 else if (new_pt > (ZV - 1))
10104 {
10105 new_pt = ZV;
10106 new_pt_byte = ZV_BYTE;
10107 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10108 }
10109
10110 /* We don't use SET_PT so that the point-motion hooks don't run. */
10111 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10112 }
10113
10114 /* If any of the character widths specified in the display table
10115 have changed, invalidate the width run cache. It's true that
10116 this may be a bit late to catch such changes, but the rest of
10117 redisplay goes (non-fatally) haywire when the display table is
10118 changed, so why should we worry about doing any better? */
10119 if (current_buffer->width_run_cache)
10120 {
10121 struct Lisp_Char_Table *disptab = buffer_display_table ();
10122
10123 if (! disptab_matches_widthtab (disptab,
10124 XVECTOR (current_buffer->width_table)))
10125 {
10126 invalidate_region_cache (current_buffer,
10127 current_buffer->width_run_cache,
10128 BEG, Z);
10129 recompute_width_table (current_buffer, disptab);
10130 }
10131 }
10132
10133 /* If window-start is screwed up, choose a new one. */
10134 if (XMARKER (w->start)->buffer != current_buffer)
10135 goto recenter;
10136
10137 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10138
10139 /* If someone specified a new starting point but did not insist,
10140 check whether it can be used. */
10141 if (!NILP (w->optional_new_start)
10142 && CHARPOS (startp) >= BEGV
10143 && CHARPOS (startp) <= ZV)
10144 {
10145 w->optional_new_start = Qnil;
10146 start_display (&it, w, startp);
10147 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10148 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10149 if (IT_CHARPOS (it) == PT)
10150 w->force_start = Qt;
10151 }
10152
10153 /* Handle case where place to start displaying has been specified,
10154 unless the specified location is outside the accessible range. */
10155 if (!NILP (w->force_start)
10156 || w->frozen_window_start_p)
10157 {
10158 w->force_start = Qnil;
10159 w->vscroll = 0;
10160 w->window_end_valid = Qnil;
10161
10162 /* Forget any recorded base line for line number display. */
10163 if (!current_matrix_up_to_date_p
10164 || current_buffer->clip_changed)
10165 w->base_line_number = Qnil;
10166
10167 /* Redisplay the mode line. Select the buffer properly for that.
10168 Also, run the hook window-scroll-functions
10169 because we have scrolled. */
10170 /* Note, we do this after clearing force_start because
10171 if there's an error, it is better to forget about force_start
10172 than to get into an infinite loop calling the hook functions
10173 and having them get more errors. */
10174 if (!update_mode_line
10175 || ! NILP (Vwindow_scroll_functions))
10176 {
10177 update_mode_line = 1;
10178 w->update_mode_line = Qt;
10179 startp = run_window_scroll_functions (window, startp);
10180 }
10181
10182 w->last_modified = make_number (0);
10183 w->last_overlay_modified = make_number (0);
10184 if (CHARPOS (startp) < BEGV)
10185 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10186 else if (CHARPOS (startp) > ZV)
10187 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10188
10189 /* Redisplay, then check if cursor has been set during the
10190 redisplay. Give up if new fonts were loaded. */
10191 if (!try_window (window, startp))
10192 {
10193 w->force_start = Qt;
10194 clear_glyph_matrix (w->desired_matrix);
10195 goto finish_scroll_bars;
10196 }
10197
10198 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10199 {
10200 /* If point does not appear, try to move point so it does
10201 appear. The desired matrix has been built above, so we
10202 can use it here. */
10203 int window_height;
10204 struct glyph_row *row;
10205
10206 window_height = window_box_height (w) / 2;
10207 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10208 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
10209 ++row;
10210
10211 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10212 MATRIX_ROW_START_BYTEPOS (row));
10213
10214 if (w != XWINDOW (selected_window))
10215 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10216 else if (current_buffer == old)
10217 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10218
10219 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10220
10221 /* If we are highlighting the region, then we just changed
10222 the region, so redisplay to show it. */
10223 if (!NILP (Vtransient_mark_mode)
10224 && !NILP (current_buffer->mark_active))
10225 {
10226 clear_glyph_matrix (w->desired_matrix);
10227 if (!try_window (window, startp))
10228 goto need_larger_matrices;
10229 }
10230 }
10231
10232 if (!make_cursor_line_fully_visible (w))
10233 goto need_larger_matrices;
10234 #if GLYPH_DEBUG
10235 debug_method_add (w, "forced window start");
10236 #endif
10237 goto done;
10238 }
10239
10240 /* Handle case where text has not changed, only point, and it has
10241 not moved off the frame. */
10242 if (current_matrix_up_to_date_p
10243 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10244 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10245 {
10246 switch (rc)
10247 {
10248 case CURSOR_MOVEMENT_SUCCESS:
10249 goto done;
10250
10251 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10252 goto need_larger_matrices;
10253
10254 case CURSOR_MOVEMENT_MUST_SCROLL:
10255 goto try_to_scroll;
10256
10257 default:
10258 abort ();
10259 }
10260 }
10261 /* If current starting point was originally the beginning of a line
10262 but no longer is, find a new starting point. */
10263 else if (!NILP (w->start_at_line_beg)
10264 && !(CHARPOS (startp) <= BEGV
10265 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10266 {
10267 #if GLYPH_DEBUG
10268 debug_method_add (w, "recenter 1");
10269 #endif
10270 goto recenter;
10271 }
10272
10273 /* Try scrolling with try_window_id. Value is > 0 if update has
10274 been done, it is -1 if we know that the same window start will
10275 not work. It is 0 if unsuccessful for some other reason. */
10276 else if ((tem = try_window_id (w)) != 0)
10277 {
10278 #if GLYPH_DEBUG
10279 debug_method_add (w, "try_window_id %d", tem);
10280 #endif
10281
10282 if (fonts_changed_p)
10283 goto need_larger_matrices;
10284 if (tem > 0)
10285 goto done;
10286
10287 /* Otherwise try_window_id has returned -1 which means that we
10288 don't want the alternative below this comment to execute. */
10289 }
10290 else if (CHARPOS (startp) >= BEGV
10291 && CHARPOS (startp) <= ZV
10292 && PT >= CHARPOS (startp)
10293 && (CHARPOS (startp) < ZV
10294 /* Avoid starting at end of buffer. */
10295 || CHARPOS (startp) == BEGV
10296 || (XFASTINT (w->last_modified) >= MODIFF
10297 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10298 {
10299 #if GLYPH_DEBUG
10300 debug_method_add (w, "same window start");
10301 #endif
10302
10303 /* Try to redisplay starting at same place as before.
10304 If point has not moved off frame, accept the results. */
10305 if (!current_matrix_up_to_date_p
10306 /* Don't use try_window_reusing_current_matrix in this case
10307 because a window scroll function can have changed the
10308 buffer. */
10309 || !NILP (Vwindow_scroll_functions)
10310 || MINI_WINDOW_P (w)
10311 || !try_window_reusing_current_matrix (w))
10312 {
10313 IF_DEBUG (debug_method_add (w, "1"));
10314 try_window (window, startp);
10315 }
10316
10317 if (fonts_changed_p)
10318 goto need_larger_matrices;
10319
10320 if (w->cursor.vpos >= 0)
10321 {
10322 if (!just_this_one_p
10323 || current_buffer->clip_changed
10324 || BEG_UNCHANGED < CHARPOS (startp))
10325 /* Forget any recorded base line for line number display. */
10326 w->base_line_number = Qnil;
10327
10328 if (!make_cursor_line_fully_visible (w))
10329 goto need_larger_matrices;
10330 goto done;
10331 }
10332 else
10333 clear_glyph_matrix (w->desired_matrix);
10334 }
10335
10336 try_to_scroll:
10337
10338 w->last_modified = make_number (0);
10339 w->last_overlay_modified = make_number (0);
10340
10341 /* Redisplay the mode line. Select the buffer properly for that. */
10342 if (!update_mode_line)
10343 {
10344 update_mode_line = 1;
10345 w->update_mode_line = Qt;
10346 }
10347
10348 /* Try to scroll by specified few lines. */
10349 if ((scroll_conservatively
10350 || scroll_step
10351 || temp_scroll_step
10352 || NUMBERP (current_buffer->scroll_up_aggressively)
10353 || NUMBERP (current_buffer->scroll_down_aggressively))
10354 && !current_buffer->clip_changed
10355 && CHARPOS (startp) >= BEGV
10356 && CHARPOS (startp) <= ZV)
10357 {
10358 /* The function returns -1 if new fonts were loaded, 1 if
10359 successful, 0 if not successful. */
10360 int rc = try_scrolling (window, just_this_one_p,
10361 scroll_conservatively,
10362 scroll_step,
10363 temp_scroll_step);
10364 switch (rc)
10365 {
10366 case SCROLLING_SUCCESS:
10367 goto done;
10368
10369 case SCROLLING_NEED_LARGER_MATRICES:
10370 goto need_larger_matrices;
10371
10372 case SCROLLING_FAILED:
10373 break;
10374
10375 default:
10376 abort ();
10377 }
10378 }
10379
10380 /* Finally, just choose place to start which centers point */
10381
10382 recenter:
10383
10384 #if GLYPH_DEBUG
10385 debug_method_add (w, "recenter");
10386 #endif
10387
10388 /* w->vscroll = 0; */
10389
10390 /* Forget any previously recorded base line for line number display. */
10391 if (!current_matrix_up_to_date_p
10392 || current_buffer->clip_changed)
10393 w->base_line_number = Qnil;
10394
10395 /* Move backward half the height of the window. */
10396 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10397 it.current_y = it.last_visible_y;
10398 move_it_vertically_backward (&it, window_box_height (w) / 2);
10399 xassert (IT_CHARPOS (it) >= BEGV);
10400
10401 /* The function move_it_vertically_backward may move over more
10402 than the specified y-distance. If it->w is small, e.g. a
10403 mini-buffer window, we may end up in front of the window's
10404 display area. Start displaying at the start of the line
10405 containing PT in this case. */
10406 if (it.current_y <= 0)
10407 {
10408 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10409 move_it_vertically (&it, 0);
10410 xassert (IT_CHARPOS (it) <= PT);
10411 it.current_y = 0;
10412 }
10413
10414 it.current_x = it.hpos = 0;
10415
10416 /* Set startp here explicitly in case that helps avoid an infinite loop
10417 in case the window-scroll-functions functions get errors. */
10418 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10419
10420 /* Run scroll hooks. */
10421 startp = run_window_scroll_functions (window, it.current.pos);
10422
10423 /* Redisplay the window. */
10424 if (!current_matrix_up_to_date_p
10425 || windows_or_buffers_changed
10426 /* Don't use try_window_reusing_current_matrix in this case
10427 because it can have changed the buffer. */
10428 || !NILP (Vwindow_scroll_functions)
10429 || !just_this_one_p
10430 || MINI_WINDOW_P (w)
10431 || !try_window_reusing_current_matrix (w))
10432 try_window (window, startp);
10433
10434 /* If new fonts have been loaded (due to fontsets), give up. We
10435 have to start a new redisplay since we need to re-adjust glyph
10436 matrices. */
10437 if (fonts_changed_p)
10438 goto need_larger_matrices;
10439
10440 /* If cursor did not appear assume that the middle of the window is
10441 in the first line of the window. Do it again with the next line.
10442 (Imagine a window of height 100, displaying two lines of height
10443 60. Moving back 50 from it->last_visible_y will end in the first
10444 line.) */
10445 if (w->cursor.vpos < 0)
10446 {
10447 if (!NILP (w->window_end_valid)
10448 && PT >= Z - XFASTINT (w->window_end_pos))
10449 {
10450 clear_glyph_matrix (w->desired_matrix);
10451 move_it_by_lines (&it, 1, 0);
10452 try_window (window, it.current.pos);
10453 }
10454 else if (PT < IT_CHARPOS (it))
10455 {
10456 clear_glyph_matrix (w->desired_matrix);
10457 move_it_by_lines (&it, -1, 0);
10458 try_window (window, it.current.pos);
10459 }
10460 else
10461 {
10462 /* Not much we can do about it. */
10463 }
10464 }
10465
10466 /* Consider the following case: Window starts at BEGV, there is
10467 invisible, intangible text at BEGV, so that display starts at
10468 some point START > BEGV. It can happen that we are called with
10469 PT somewhere between BEGV and START. Try to handle that case. */
10470 if (w->cursor.vpos < 0)
10471 {
10472 struct glyph_row *row = w->current_matrix->rows;
10473 if (row->mode_line_p)
10474 ++row;
10475 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10476 }
10477
10478 if (!make_cursor_line_fully_visible (w))
10479 goto need_larger_matrices;
10480
10481 done:
10482
10483 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10484 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10485 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10486 ? Qt : Qnil);
10487
10488 /* Display the mode line, if we must. */
10489 if ((update_mode_line
10490 /* If window not full width, must redo its mode line
10491 if (a) the window to its side is being redone and
10492 (b) we do a frame-based redisplay. This is a consequence
10493 of how inverted lines are drawn in frame-based redisplay. */
10494 || (!just_this_one_p
10495 && !FRAME_WINDOW_P (f)
10496 && !WINDOW_FULL_WIDTH_P (w))
10497 /* Line number to display. */
10498 || INTEGERP (w->base_line_pos)
10499 /* Column number is displayed and different from the one displayed. */
10500 || (!NILP (w->column_number_displayed)
10501 && XFASTINT (w->column_number_displayed) != current_column ()))
10502 /* This means that the window has a mode line. */
10503 && (WINDOW_WANTS_MODELINE_P (w)
10504 || WINDOW_WANTS_HEADER_LINE_P (w)))
10505 {
10506 display_mode_lines (w);
10507
10508 /* If mode line height has changed, arrange for a thorough
10509 immediate redisplay using the correct mode line height. */
10510 if (WINDOW_WANTS_MODELINE_P (w)
10511 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10512 {
10513 fonts_changed_p = 1;
10514 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10515 = DESIRED_MODE_LINE_HEIGHT (w);
10516 }
10517
10518 /* If top line height has changed, arrange for a thorough
10519 immediate redisplay using the correct mode line height. */
10520 if (WINDOW_WANTS_HEADER_LINE_P (w)
10521 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10522 {
10523 fonts_changed_p = 1;
10524 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10525 = DESIRED_HEADER_LINE_HEIGHT (w);
10526 }
10527
10528 if (fonts_changed_p)
10529 goto need_larger_matrices;
10530 }
10531
10532 if (!line_number_displayed
10533 && !BUFFERP (w->base_line_pos))
10534 {
10535 w->base_line_pos = Qnil;
10536 w->base_line_number = Qnil;
10537 }
10538
10539 finish_menu_bars:
10540
10541 /* When we reach a frame's selected window, redo the frame's menu bar. */
10542 if (update_mode_line
10543 && EQ (FRAME_SELECTED_WINDOW (f), window))
10544 {
10545 int redisplay_menu_p = 0;
10546
10547 if (FRAME_WINDOW_P (f))
10548 {
10549 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10550 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10551 #else
10552 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10553 #endif
10554 }
10555 else
10556 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10557
10558 if (redisplay_menu_p)
10559 display_menu_bar (w);
10560
10561 #ifdef HAVE_WINDOW_SYSTEM
10562 if (WINDOWP (f->tool_bar_window)
10563 && (FRAME_TOOL_BAR_LINES (f) > 0
10564 || auto_resize_tool_bars_p))
10565 redisplay_tool_bar (f);
10566 #endif
10567 }
10568
10569 need_larger_matrices:
10570 ;
10571 finish_scroll_bars:
10572
10573 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10574 {
10575 int start, end, whole;
10576
10577 /* Calculate the start and end positions for the current window.
10578 At some point, it would be nice to choose between scrollbars
10579 which reflect the whole buffer size, with special markers
10580 indicating narrowing, and scrollbars which reflect only the
10581 visible region.
10582
10583 Note that mini-buffers sometimes aren't displaying any text. */
10584 if (!MINI_WINDOW_P (w)
10585 || (w == XWINDOW (minibuf_window)
10586 && NILP (echo_area_buffer[0])))
10587 {
10588 whole = ZV - BEGV;
10589 start = marker_position (w->start) - BEGV;
10590 /* I don't think this is guaranteed to be right. For the
10591 moment, we'll pretend it is. */
10592 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10593
10594 if (end < start)
10595 end = start;
10596 if (whole < (end - start))
10597 whole = end - start;
10598 }
10599 else
10600 start = end = whole = 0;
10601
10602 /* Indicate what this scroll bar ought to be displaying now. */
10603 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10604
10605 /* Note that we actually used the scroll bar attached to this
10606 window, so it shouldn't be deleted at the end of redisplay. */
10607 redeem_scroll_bar_hook (w);
10608 }
10609
10610 /* Restore current_buffer and value of point in it. */
10611 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10612 set_buffer_internal_1 (old);
10613 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10614
10615 unbind_to (count, Qnil);
10616 }
10617
10618
10619 /* Build the complete desired matrix of WINDOW with a window start
10620 buffer position POS. Value is non-zero if successful. It is zero
10621 if fonts were loaded during redisplay which makes re-adjusting
10622 glyph matrices necessary. */
10623
10624 int
10625 try_window (window, pos)
10626 Lisp_Object window;
10627 struct text_pos pos;
10628 {
10629 struct window *w = XWINDOW (window);
10630 struct it it;
10631 struct glyph_row *last_text_row = NULL;
10632
10633 /* Make POS the new window start. */
10634 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10635
10636 /* Mark cursor position as unknown. No overlay arrow seen. */
10637 w->cursor.vpos = -1;
10638 overlay_arrow_seen = 0;
10639
10640 /* Initialize iterator and info to start at POS. */
10641 start_display (&it, w, pos);
10642
10643 /* Display all lines of W. */
10644 while (it.current_y < it.last_visible_y)
10645 {
10646 if (display_line (&it))
10647 last_text_row = it.glyph_row - 1;
10648 if (fonts_changed_p)
10649 return 0;
10650 }
10651
10652 /* If bottom moved off end of frame, change mode line percentage. */
10653 if (XFASTINT (w->window_end_pos) <= 0
10654 && Z != IT_CHARPOS (it))
10655 w->update_mode_line = Qt;
10656
10657 /* Set window_end_pos to the offset of the last character displayed
10658 on the window from the end of current_buffer. Set
10659 window_end_vpos to its row number. */
10660 if (last_text_row)
10661 {
10662 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10663 w->window_end_bytepos
10664 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10665 w->window_end_pos
10666 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10667 w->window_end_vpos
10668 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10669 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10670 ->displays_text_p);
10671 }
10672 else
10673 {
10674 w->window_end_bytepos = 0;
10675 w->window_end_pos = w->window_end_vpos = make_number (0);
10676 }
10677
10678 /* But that is not valid info until redisplay finishes. */
10679 w->window_end_valid = Qnil;
10680 return 1;
10681 }
10682
10683
10684 \f
10685 /************************************************************************
10686 Window redisplay reusing current matrix when buffer has not changed
10687 ************************************************************************/
10688
10689 /* Try redisplay of window W showing an unchanged buffer with a
10690 different window start than the last time it was displayed by
10691 reusing its current matrix. Value is non-zero if successful.
10692 W->start is the new window start. */
10693
10694 static int
10695 try_window_reusing_current_matrix (w)
10696 struct window *w;
10697 {
10698 struct frame *f = XFRAME (w->frame);
10699 struct glyph_row *row, *bottom_row;
10700 struct it it;
10701 struct run run;
10702 struct text_pos start, new_start;
10703 int nrows_scrolled, i;
10704 struct glyph_row *last_text_row;
10705 struct glyph_row *last_reused_text_row;
10706 struct glyph_row *start_row;
10707 int start_vpos, min_y, max_y;
10708
10709 #if GLYPH_DEBUG
10710 if (inhibit_try_window_reusing)
10711 return 0;
10712 #endif
10713
10714 if (/* This function doesn't handle terminal frames. */
10715 !FRAME_WINDOW_P (f)
10716 /* Don't try to reuse the display if windows have been split
10717 or such. */
10718 || windows_or_buffers_changed)
10719 return 0;
10720
10721 /* Can't do this if region may have changed. */
10722 if ((!NILP (Vtransient_mark_mode)
10723 && !NILP (current_buffer->mark_active))
10724 || !NILP (w->region_showing)
10725 || !NILP (Vshow_trailing_whitespace))
10726 return 0;
10727
10728 /* If top-line visibility has changed, give up. */
10729 if (WINDOW_WANTS_HEADER_LINE_P (w)
10730 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10731 return 0;
10732
10733 /* Give up if old or new display is scrolled vertically. We could
10734 make this function handle this, but right now it doesn't. */
10735 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10736 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10737 return 0;
10738
10739 /* The variable new_start now holds the new window start. The old
10740 start `start' can be determined from the current matrix. */
10741 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10742 start = start_row->start.pos;
10743 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10744
10745 /* Clear the desired matrix for the display below. */
10746 clear_glyph_matrix (w->desired_matrix);
10747
10748 if (CHARPOS (new_start) <= CHARPOS (start))
10749 {
10750 int first_row_y;
10751
10752 /* Don't use this method if the display starts with an ellipsis
10753 displayed for invisible text. It's not easy to handle that case
10754 below, and it's certainly not worth the effort since this is
10755 not a frequent case. */
10756 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10757 return 0;
10758
10759 IF_DEBUG (debug_method_add (w, "twu1"));
10760
10761 /* Display up to a row that can be reused. The variable
10762 last_text_row is set to the last row displayed that displays
10763 text. Note that it.vpos == 0 if or if not there is a
10764 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10765 start_display (&it, w, new_start);
10766 first_row_y = it.current_y;
10767 w->cursor.vpos = -1;
10768 last_text_row = last_reused_text_row = NULL;
10769
10770 while (it.current_y < it.last_visible_y
10771 && IT_CHARPOS (it) < CHARPOS (start)
10772 && !fonts_changed_p)
10773 if (display_line (&it))
10774 last_text_row = it.glyph_row - 1;
10775
10776 /* A value of current_y < last_visible_y means that we stopped
10777 at the previous window start, which in turn means that we
10778 have at least one reusable row. */
10779 if (it.current_y < it.last_visible_y)
10780 {
10781 /* IT.vpos always starts from 0; it counts text lines. */
10782 nrows_scrolled = it.vpos;
10783
10784 /* Find PT if not already found in the lines displayed. */
10785 if (w->cursor.vpos < 0)
10786 {
10787 int dy = it.current_y - first_row_y;
10788
10789 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10790 row = row_containing_pos (w, PT, row, NULL, dy);
10791 if (row)
10792 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10793 dy, nrows_scrolled);
10794 else
10795 {
10796 clear_glyph_matrix (w->desired_matrix);
10797 return 0;
10798 }
10799 }
10800
10801 /* Scroll the display. Do it before the current matrix is
10802 changed. The problem here is that update has not yet
10803 run, i.e. part of the current matrix is not up to date.
10804 scroll_run_hook will clear the cursor, and use the
10805 current matrix to get the height of the row the cursor is
10806 in. */
10807 run.current_y = first_row_y;
10808 run.desired_y = it.current_y;
10809 run.height = it.last_visible_y - it.current_y;
10810
10811 if (run.height > 0 && run.current_y != run.desired_y)
10812 {
10813 update_begin (f);
10814 rif->update_window_begin_hook (w);
10815 rif->clear_mouse_face (w);
10816 rif->scroll_run_hook (w, &run);
10817 rif->update_window_end_hook (w, 0, 0);
10818 update_end (f);
10819 }
10820
10821 /* Shift current matrix down by nrows_scrolled lines. */
10822 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10823 rotate_matrix (w->current_matrix,
10824 start_vpos,
10825 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10826 nrows_scrolled);
10827
10828 /* Disable lines that must be updated. */
10829 for (i = 0; i < it.vpos; ++i)
10830 (start_row + i)->enabled_p = 0;
10831
10832 /* Re-compute Y positions. */
10833 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10834 max_y = it.last_visible_y;
10835 for (row = start_row + nrows_scrolled;
10836 row < bottom_row;
10837 ++row)
10838 {
10839 row->y = it.current_y;
10840 row->visible_height = row->height;
10841
10842 if (row->y < min_y)
10843 row->visible_height -= min_y - row->y;
10844 if (row->y + row->height > max_y)
10845 row->visible_height -= row->y + row->height - max_y;
10846
10847 it.current_y += row->height;
10848
10849 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10850 last_reused_text_row = row;
10851 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10852 break;
10853 }
10854
10855 /* Disable lines in the current matrix which are now
10856 below the window. */
10857 for (++row; row < bottom_row; ++row)
10858 row->enabled_p = 0;
10859 }
10860
10861 /* Update window_end_pos etc.; last_reused_text_row is the last
10862 reused row from the current matrix containing text, if any.
10863 The value of last_text_row is the last displayed line
10864 containing text. */
10865 if (last_reused_text_row)
10866 {
10867 w->window_end_bytepos
10868 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10869 w->window_end_pos
10870 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10871 w->window_end_vpos
10872 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10873 w->current_matrix));
10874 }
10875 else if (last_text_row)
10876 {
10877 w->window_end_bytepos
10878 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10879 w->window_end_pos
10880 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10881 w->window_end_vpos
10882 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10883 }
10884 else
10885 {
10886 /* This window must be completely empty. */
10887 w->window_end_bytepos = 0;
10888 w->window_end_pos = w->window_end_vpos = make_number (0);
10889 }
10890 w->window_end_valid = Qnil;
10891
10892 /* Update hint: don't try scrolling again in update_window. */
10893 w->desired_matrix->no_scrolling_p = 1;
10894
10895 #if GLYPH_DEBUG
10896 debug_method_add (w, "try_window_reusing_current_matrix 1");
10897 #endif
10898 return 1;
10899 }
10900 else if (CHARPOS (new_start) > CHARPOS (start))
10901 {
10902 struct glyph_row *pt_row, *row;
10903 struct glyph_row *first_reusable_row;
10904 struct glyph_row *first_row_to_display;
10905 int dy;
10906 int yb = window_text_bottom_y (w);
10907
10908 /* Find the row starting at new_start, if there is one. Don't
10909 reuse a partially visible line at the end. */
10910 first_reusable_row = start_row;
10911 while (first_reusable_row->enabled_p
10912 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10913 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10914 < CHARPOS (new_start)))
10915 ++first_reusable_row;
10916
10917 /* Give up if there is no row to reuse. */
10918 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10919 || !first_reusable_row->enabled_p
10920 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10921 != CHARPOS (new_start)))
10922 return 0;
10923
10924 /* We can reuse fully visible rows beginning with
10925 first_reusable_row to the end of the window. Set
10926 first_row_to_display to the first row that cannot be reused.
10927 Set pt_row to the row containing point, if there is any. */
10928 pt_row = NULL;
10929 for (first_row_to_display = first_reusable_row;
10930 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
10931 ++first_row_to_display)
10932 {
10933 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10934 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10935 pt_row = first_row_to_display;
10936 }
10937
10938 /* Start displaying at the start of first_row_to_display. */
10939 xassert (first_row_to_display->y < yb);
10940 init_to_row_start (&it, w, first_row_to_display);
10941
10942 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10943 - start_vpos);
10944 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10945 - nrows_scrolled);
10946 it.current_y = (first_row_to_display->y - first_reusable_row->y
10947 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10948
10949 /* Display lines beginning with first_row_to_display in the
10950 desired matrix. Set last_text_row to the last row displayed
10951 that displays text. */
10952 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10953 if (pt_row == NULL)
10954 w->cursor.vpos = -1;
10955 last_text_row = NULL;
10956 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10957 if (display_line (&it))
10958 last_text_row = it.glyph_row - 1;
10959
10960 /* Give up If point isn't in a row displayed or reused. */
10961 if (w->cursor.vpos < 0)
10962 {
10963 clear_glyph_matrix (w->desired_matrix);
10964 return 0;
10965 }
10966
10967 /* If point is in a reused row, adjust y and vpos of the cursor
10968 position. */
10969 if (pt_row)
10970 {
10971 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10972 w->current_matrix);
10973 w->cursor.y -= first_reusable_row->y;
10974 }
10975
10976 /* Scroll the display. */
10977 run.current_y = first_reusable_row->y;
10978 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10979 run.height = it.last_visible_y - run.current_y;
10980 dy = run.current_y - run.desired_y;
10981
10982 if (run.height)
10983 {
10984 struct frame *f = XFRAME (WINDOW_FRAME (w));
10985 update_begin (f);
10986 rif->update_window_begin_hook (w);
10987 rif->clear_mouse_face (w);
10988 rif->scroll_run_hook (w, &run);
10989 rif->update_window_end_hook (w, 0, 0);
10990 update_end (f);
10991 }
10992
10993 /* Adjust Y positions of reused rows. */
10994 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10995 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10996 max_y = it.last_visible_y;
10997 for (row = first_reusable_row; row < first_row_to_display; ++row)
10998 {
10999 row->y -= dy;
11000 row->visible_height = row->height;
11001 if (row->y < min_y)
11002 row->visible_height -= min_y - row->y;
11003 if (row->y + row->height > max_y)
11004 row->visible_height -= row->y + row->height - max_y;
11005 }
11006
11007 /* Scroll the current matrix. */
11008 xassert (nrows_scrolled > 0);
11009 rotate_matrix (w->current_matrix,
11010 start_vpos,
11011 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11012 -nrows_scrolled);
11013
11014 /* Disable rows not reused. */
11015 for (row -= nrows_scrolled; row < bottom_row; ++row)
11016 row->enabled_p = 0;
11017
11018 /* Adjust window end. A null value of last_text_row means that
11019 the window end is in reused rows which in turn means that
11020 only its vpos can have changed. */
11021 if (last_text_row)
11022 {
11023 w->window_end_bytepos
11024 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11025 w->window_end_pos
11026 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11027 w->window_end_vpos
11028 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11029 }
11030 else
11031 {
11032 w->window_end_vpos
11033 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
11034 }
11035
11036 w->window_end_valid = Qnil;
11037 w->desired_matrix->no_scrolling_p = 1;
11038
11039 #if GLYPH_DEBUG
11040 debug_method_add (w, "try_window_reusing_current_matrix 2");
11041 #endif
11042 return 1;
11043 }
11044
11045 return 0;
11046 }
11047
11048
11049 \f
11050 /************************************************************************
11051 Window redisplay reusing current matrix when buffer has changed
11052 ************************************************************************/
11053
11054 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11055 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
11056 int *, int *));
11057 static struct glyph_row *
11058 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11059 struct glyph_row *));
11060
11061
11062 /* Return the last row in MATRIX displaying text. If row START is
11063 non-null, start searching with that row. IT gives the dimensions
11064 of the display. Value is null if matrix is empty; otherwise it is
11065 a pointer to the row found. */
11066
11067 static struct glyph_row *
11068 find_last_row_displaying_text (matrix, it, start)
11069 struct glyph_matrix *matrix;
11070 struct it *it;
11071 struct glyph_row *start;
11072 {
11073 struct glyph_row *row, *row_found;
11074
11075 /* Set row_found to the last row in IT->w's current matrix
11076 displaying text. The loop looks funny but think of partially
11077 visible lines. */
11078 row_found = NULL;
11079 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11080 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11081 {
11082 xassert (row->enabled_p);
11083 row_found = row;
11084 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11085 break;
11086 ++row;
11087 }
11088
11089 return row_found;
11090 }
11091
11092
11093 /* Return the last row in the current matrix of W that is not affected
11094 by changes at the start of current_buffer that occurred since W's
11095 current matrix was built. Value is null if no such row exists.
11096
11097 BEG_UNCHANGED us the number of characters unchanged at the start of
11098 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11099 first changed character in current_buffer. Characters at positions <
11100 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11101 when the current matrix was built. */
11102
11103 static struct glyph_row *
11104 find_last_unchanged_at_beg_row (w)
11105 struct window *w;
11106 {
11107 int first_changed_pos = BEG + BEG_UNCHANGED;
11108 struct glyph_row *row;
11109 struct glyph_row *row_found = NULL;
11110 int yb = window_text_bottom_y (w);
11111
11112 /* Find the last row displaying unchanged text. */
11113 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11114 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11115 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11116 {
11117 if (/* If row ends before first_changed_pos, it is unchanged,
11118 except in some case. */
11119 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11120 /* When row ends in ZV and we write at ZV it is not
11121 unchanged. */
11122 && !row->ends_at_zv_p
11123 /* When first_changed_pos is the end of a continued line,
11124 row is not unchanged because it may be no longer
11125 continued. */
11126 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11127 && row->continued_p))
11128 row_found = row;
11129
11130 /* Stop if last visible row. */
11131 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11132 break;
11133
11134 ++row;
11135 }
11136
11137 return row_found;
11138 }
11139
11140
11141 /* Find the first glyph row in the current matrix of W that is not
11142 affected by changes at the end of current_buffer since the
11143 time W's current matrix was built.
11144
11145 Return in *DELTA the number of chars by which buffer positions in
11146 unchanged text at the end of current_buffer must be adjusted.
11147
11148 Return in *DELTA_BYTES the corresponding number of bytes.
11149
11150 Value is null if no such row exists, i.e. all rows are affected by
11151 changes. */
11152
11153 static struct glyph_row *
11154 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11155 struct window *w;
11156 int *delta, *delta_bytes;
11157 {
11158 struct glyph_row *row;
11159 struct glyph_row *row_found = NULL;
11160
11161 *delta = *delta_bytes = 0;
11162
11163 /* Display must not have been paused, otherwise the current matrix
11164 is not up to date. */
11165 if (NILP (w->window_end_valid))
11166 abort ();
11167
11168 /* A value of window_end_pos >= END_UNCHANGED means that the window
11169 end is in the range of changed text. If so, there is no
11170 unchanged row at the end of W's current matrix. */
11171 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11172 return NULL;
11173
11174 /* Set row to the last row in W's current matrix displaying text. */
11175 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11176
11177 /* If matrix is entirely empty, no unchanged row exists. */
11178 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11179 {
11180 /* The value of row is the last glyph row in the matrix having a
11181 meaningful buffer position in it. The end position of row
11182 corresponds to window_end_pos. This allows us to translate
11183 buffer positions in the current matrix to current buffer
11184 positions for characters not in changed text. */
11185 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11186 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11187 int last_unchanged_pos, last_unchanged_pos_old;
11188 struct glyph_row *first_text_row
11189 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11190
11191 *delta = Z - Z_old;
11192 *delta_bytes = Z_BYTE - Z_BYTE_old;
11193
11194 /* Set last_unchanged_pos to the buffer position of the last
11195 character in the buffer that has not been changed. Z is the
11196 index + 1 of the last character in current_buffer, i.e. by
11197 subtracting END_UNCHANGED we get the index of the last
11198 unchanged character, and we have to add BEG to get its buffer
11199 position. */
11200 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11201 last_unchanged_pos_old = last_unchanged_pos - *delta;
11202
11203 /* Search backward from ROW for a row displaying a line that
11204 starts at a minimum position >= last_unchanged_pos_old. */
11205 for (; row > first_text_row; --row)
11206 {
11207 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11208 abort ();
11209
11210 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11211 row_found = row;
11212 }
11213 }
11214
11215 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11216 abort ();
11217
11218 return row_found;
11219 }
11220
11221
11222 /* Make sure that glyph rows in the current matrix of window W
11223 reference the same glyph memory as corresponding rows in the
11224 frame's frame matrix. This function is called after scrolling W's
11225 current matrix on a terminal frame in try_window_id and
11226 try_window_reusing_current_matrix. */
11227
11228 static void
11229 sync_frame_with_window_matrix_rows (w)
11230 struct window *w;
11231 {
11232 struct frame *f = XFRAME (w->frame);
11233 struct glyph_row *window_row, *window_row_end, *frame_row;
11234
11235 /* Preconditions: W must be a leaf window and full-width. Its frame
11236 must have a frame matrix. */
11237 xassert (NILP (w->hchild) && NILP (w->vchild));
11238 xassert (WINDOW_FULL_WIDTH_P (w));
11239 xassert (!FRAME_WINDOW_P (f));
11240
11241 /* If W is a full-width window, glyph pointers in W's current matrix
11242 have, by definition, to be the same as glyph pointers in the
11243 corresponding frame matrix. */
11244 window_row = w->current_matrix->rows;
11245 window_row_end = window_row + w->current_matrix->nrows;
11246 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11247 while (window_row < window_row_end)
11248 {
11249 int area;
11250
11251 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
11252 frame_row->glyphs[area] = window_row->glyphs[area];
11253
11254 /* Disable frame rows whose corresponding window rows have
11255 been disabled in try_window_id. */
11256 if (!window_row->enabled_p)
11257 frame_row->enabled_p = 0;
11258
11259 ++window_row, ++frame_row;
11260 }
11261 }
11262
11263
11264 /* Find the glyph row in window W containing CHARPOS. Consider all
11265 rows between START and END (not inclusive). END null means search
11266 all rows to the end of the display area of W. Value is the row
11267 containing CHARPOS or null. */
11268
11269 struct glyph_row *
11270 row_containing_pos (w, charpos, start, end, dy)
11271 struct window *w;
11272 int charpos;
11273 struct glyph_row *start, *end;
11274 int dy;
11275 {
11276 struct glyph_row *row = start;
11277 int last_y;
11278
11279 /* If we happen to start on a header-line, skip that. */
11280 if (row->mode_line_p)
11281 ++row;
11282
11283 if ((end && row >= end) || !row->enabled_p)
11284 return NULL;
11285
11286 last_y = window_text_bottom_y (w) - dy;
11287
11288 while ((end == NULL || row < end)
11289 && MATRIX_ROW_BOTTOM_Y (row) < last_y
11290 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11291 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11292 /* The end position of a row equals the start
11293 position of the next row. If CHARPOS is there, we
11294 would rather display it in the next line, except
11295 when this line ends in ZV. */
11296 && !row->ends_at_zv_p
11297 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))
11298 ++row;
11299
11300 /* Give up if CHARPOS not found. */
11301 if ((end && row >= end)
11302 || charpos < MATRIX_ROW_START_CHARPOS (row)
11303 || charpos > MATRIX_ROW_END_CHARPOS (row))
11304 row = NULL;
11305
11306 return row;
11307 }
11308
11309
11310 /* Try to redisplay window W by reusing its existing display. W's
11311 current matrix must be up to date when this function is called,
11312 i.e. window_end_valid must not be nil.
11313
11314 Value is
11315
11316 1 if display has been updated
11317 0 if otherwise unsuccessful
11318 -1 if redisplay with same window start is known not to succeed
11319
11320 The following steps are performed:
11321
11322 1. Find the last row in the current matrix of W that is not
11323 affected by changes at the start of current_buffer. If no such row
11324 is found, give up.
11325
11326 2. Find the first row in W's current matrix that is not affected by
11327 changes at the end of current_buffer. Maybe there is no such row.
11328
11329 3. Display lines beginning with the row + 1 found in step 1 to the
11330 row found in step 2 or, if step 2 didn't find a row, to the end of
11331 the window.
11332
11333 4. If cursor is not known to appear on the window, give up.
11334
11335 5. If display stopped at the row found in step 2, scroll the
11336 display and current matrix as needed.
11337
11338 6. Maybe display some lines at the end of W, if we must. This can
11339 happen under various circumstances, like a partially visible line
11340 becoming fully visible, or because newly displayed lines are displayed
11341 in smaller font sizes.
11342
11343 7. Update W's window end information. */
11344
11345 static int
11346 try_window_id (w)
11347 struct window *w;
11348 {
11349 struct frame *f = XFRAME (w->frame);
11350 struct glyph_matrix *current_matrix = w->current_matrix;
11351 struct glyph_matrix *desired_matrix = w->desired_matrix;
11352 struct glyph_row *last_unchanged_at_beg_row;
11353 struct glyph_row *first_unchanged_at_end_row;
11354 struct glyph_row *row;
11355 struct glyph_row *bottom_row;
11356 int bottom_vpos;
11357 struct it it;
11358 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11359 struct text_pos start_pos;
11360 struct run run;
11361 int first_unchanged_at_end_vpos = 0;
11362 struct glyph_row *last_text_row, *last_text_row_at_end;
11363 struct text_pos start;
11364 int first_changed_charpos, last_changed_charpos;
11365
11366 #if GLYPH_DEBUG
11367 if (inhibit_try_window_id)
11368 return 0;
11369 #endif
11370
11371 /* This is handy for debugging. */
11372 #if 0
11373 #define GIVE_UP(X) \
11374 do { \
11375 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11376 return 0; \
11377 } while (0)
11378 #else
11379 #define GIVE_UP(X) return 0
11380 #endif
11381
11382 SET_TEXT_POS_FROM_MARKER (start, w->start);
11383
11384 /* Don't use this for mini-windows because these can show
11385 messages and mini-buffers, and we don't handle that here. */
11386 if (MINI_WINDOW_P (w))
11387 GIVE_UP (1);
11388
11389 /* This flag is used to prevent redisplay optimizations. */
11390 if (windows_or_buffers_changed)
11391 GIVE_UP (2);
11392
11393 /* Verify that narrowing has not changed. This flag is also set to prevent
11394 redisplay optimizations. It would be nice to further
11395 reduce the number of cases where this prevents try_window_id. */
11396 if (current_buffer->clip_changed)
11397 GIVE_UP (3);
11398
11399 /* Window must either use window-based redisplay or be full width. */
11400 if (!FRAME_WINDOW_P (f)
11401 && (!line_ins_del_ok
11402 || !WINDOW_FULL_WIDTH_P (w)))
11403 GIVE_UP (4);
11404
11405 /* Give up if point is not known NOT to appear in W. */
11406 if (PT < CHARPOS (start))
11407 GIVE_UP (5);
11408
11409 /* Another way to prevent redisplay optimizations. */
11410 if (XFASTINT (w->last_modified) == 0)
11411 GIVE_UP (6);
11412
11413 /* Verify that window is not hscrolled. */
11414 if (XFASTINT (w->hscroll) != 0)
11415 GIVE_UP (7);
11416
11417 /* Verify that display wasn't paused. */
11418 if (NILP (w->window_end_valid))
11419 GIVE_UP (8);
11420
11421 /* Can't use this if highlighting a region because a cursor movement
11422 will do more than just set the cursor. */
11423 if (!NILP (Vtransient_mark_mode)
11424 && !NILP (current_buffer->mark_active))
11425 GIVE_UP (9);
11426
11427 /* Likewise if highlighting trailing whitespace. */
11428 if (!NILP (Vshow_trailing_whitespace))
11429 GIVE_UP (11);
11430
11431 /* Likewise if showing a region. */
11432 if (!NILP (w->region_showing))
11433 GIVE_UP (10);
11434
11435 /* Can use this if overlay arrow position and or string have changed. */
11436 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11437 || !EQ (last_arrow_string, Voverlay_arrow_string))
11438 GIVE_UP (12);
11439
11440
11441 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11442 only if buffer has really changed. The reason is that the gap is
11443 initially at Z for freshly visited files. The code below would
11444 set end_unchanged to 0 in that case. */
11445 if (MODIFF > SAVE_MODIFF
11446 /* This seems to happen sometimes after saving a buffer. */
11447 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11448 {
11449 if (GPT - BEG < BEG_UNCHANGED)
11450 BEG_UNCHANGED = GPT - BEG;
11451 if (Z - GPT < END_UNCHANGED)
11452 END_UNCHANGED = Z - GPT;
11453 }
11454
11455 /* The position of the first and last character that has been changed. */
11456 first_changed_charpos = BEG + BEG_UNCHANGED;
11457 last_changed_charpos = Z - END_UNCHANGED;
11458
11459 /* If window starts after a line end, and the last change is in
11460 front of that newline, then changes don't affect the display.
11461 This case happens with stealth-fontification. Note that although
11462 the display is unchanged, glyph positions in the matrix have to
11463 be adjusted, of course. */
11464 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11465 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11466 && ((last_changed_charpos < CHARPOS (start)
11467 && CHARPOS (start) == BEGV)
11468 || (last_changed_charpos < CHARPOS (start) - 1
11469 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11470 {
11471 int Z_old, delta, Z_BYTE_old, delta_bytes;
11472 struct glyph_row *r0;
11473
11474 /* Compute how many chars/bytes have been added to or removed
11475 from the buffer. */
11476 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11477 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11478 delta = Z - Z_old;
11479 delta_bytes = Z_BYTE - Z_BYTE_old;
11480
11481 /* Give up if PT is not in the window. Note that it already has
11482 been checked at the start of try_window_id that PT is not in
11483 front of the window start. */
11484 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11485 GIVE_UP (13);
11486
11487 /* If window start is unchanged, we can reuse the whole matrix
11488 as is, after adjusting glyph positions. No need to compute
11489 the window end again, since its offset from Z hasn't changed. */
11490 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11491 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11492 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11493 {
11494 /* Adjust positions in the glyph matrix. */
11495 if (delta || delta_bytes)
11496 {
11497 struct glyph_row *r1
11498 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11499 increment_matrix_positions (w->current_matrix,
11500 MATRIX_ROW_VPOS (r0, current_matrix),
11501 MATRIX_ROW_VPOS (r1, current_matrix),
11502 delta, delta_bytes);
11503 }
11504
11505 /* Set the cursor. */
11506 row = row_containing_pos (w, PT, r0, NULL, 0);
11507 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11508 return 1;
11509 }
11510 }
11511
11512 /* Handle the case that changes are all below what is displayed in
11513 the window, and that PT is in the window. This shortcut cannot
11514 be taken if ZV is visible in the window, and text has been added
11515 there that is visible in the window. */
11516 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11517 /* ZV is not visible in the window, or there are no
11518 changes at ZV, actually. */
11519 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11520 || first_changed_charpos == last_changed_charpos))
11521 {
11522 struct glyph_row *r0;
11523
11524 /* Give up if PT is not in the window. Note that it already has
11525 been checked at the start of try_window_id that PT is not in
11526 front of the window start. */
11527 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11528 GIVE_UP (14);
11529
11530 /* If window start is unchanged, we can reuse the whole matrix
11531 as is, without changing glyph positions since no text has
11532 been added/removed in front of the window end. */
11533 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11534 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11535 {
11536 /* We have to compute the window end anew since text
11537 can have been added/removed after it. */
11538 w->window_end_pos
11539 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11540 w->window_end_bytepos
11541 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11542
11543 /* Set the cursor. */
11544 row = row_containing_pos (w, PT, r0, NULL, 0);
11545 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11546 return 2;
11547 }
11548 }
11549
11550 /* Give up if window start is in the changed area.
11551
11552 The condition used to read
11553
11554 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11555
11556 but why that was tested escapes me at the moment. */
11557 if (CHARPOS (start) >= first_changed_charpos
11558 && CHARPOS (start) <= last_changed_charpos)
11559 GIVE_UP (15);
11560
11561 /* Check that window start agrees with the start of the first glyph
11562 row in its current matrix. Check this after we know the window
11563 start is not in changed text, otherwise positions would not be
11564 comparable. */
11565 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11566 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11567 GIVE_UP (16);
11568
11569 /* Give up if the window ends in strings. Overlay strings
11570 at the end are difficult to handle, so don't try. */
11571 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11572 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11573 GIVE_UP (20);
11574
11575 /* Compute the position at which we have to start displaying new
11576 lines. Some of the lines at the top of the window might be
11577 reusable because they are not displaying changed text. Find the
11578 last row in W's current matrix not affected by changes at the
11579 start of current_buffer. Value is null if changes start in the
11580 first line of window. */
11581 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11582 if (last_unchanged_at_beg_row)
11583 {
11584 /* Avoid starting to display in the moddle of a character, a TAB
11585 for instance. This is easier than to set up the iterator
11586 exactly, and it's not a frequent case, so the additional
11587 effort wouldn't really pay off. */
11588 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11589 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
11590 && last_unchanged_at_beg_row > w->current_matrix->rows)
11591 --last_unchanged_at_beg_row;
11592
11593 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11594 GIVE_UP (17);
11595
11596 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11597 GIVE_UP (18);
11598 start_pos = it.current.pos;
11599
11600 /* Start displaying new lines in the desired matrix at the same
11601 vpos we would use in the current matrix, i.e. below
11602 last_unchanged_at_beg_row. */
11603 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11604 current_matrix);
11605 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11606 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11607
11608 xassert (it.hpos == 0 && it.current_x == 0);
11609 }
11610 else
11611 {
11612 /* There are no reusable lines at the start of the window.
11613 Start displaying in the first line. */
11614 start_display (&it, w, start);
11615 start_pos = it.current.pos;
11616 }
11617
11618 /* Find the first row that is not affected by changes at the end of
11619 the buffer. Value will be null if there is no unchanged row, in
11620 which case we must redisplay to the end of the window. delta
11621 will be set to the value by which buffer positions beginning with
11622 first_unchanged_at_end_row have to be adjusted due to text
11623 changes. */
11624 first_unchanged_at_end_row
11625 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11626 IF_DEBUG (debug_delta = delta);
11627 IF_DEBUG (debug_delta_bytes = delta_bytes);
11628
11629 /* Set stop_pos to the buffer position up to which we will have to
11630 display new lines. If first_unchanged_at_end_row != NULL, this
11631 is the buffer position of the start of the line displayed in that
11632 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11633 that we don't stop at a buffer position. */
11634 stop_pos = 0;
11635 if (first_unchanged_at_end_row)
11636 {
11637 xassert (last_unchanged_at_beg_row == NULL
11638 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11639
11640 /* If this is a continuation line, move forward to the next one
11641 that isn't. Changes in lines above affect this line.
11642 Caution: this may move first_unchanged_at_end_row to a row
11643 not displaying text. */
11644 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11645 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11646 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11647 < it.last_visible_y))
11648 ++first_unchanged_at_end_row;
11649
11650 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11651 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11652 >= it.last_visible_y))
11653 first_unchanged_at_end_row = NULL;
11654 else
11655 {
11656 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11657 + delta);
11658 first_unchanged_at_end_vpos
11659 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11660 xassert (stop_pos >= Z - END_UNCHANGED);
11661 }
11662 }
11663 else if (last_unchanged_at_beg_row == NULL)
11664 GIVE_UP (19);
11665
11666
11667 #if GLYPH_DEBUG
11668
11669 /* Either there is no unchanged row at the end, or the one we have
11670 now displays text. This is a necessary condition for the window
11671 end pos calculation at the end of this function. */
11672 xassert (first_unchanged_at_end_row == NULL
11673 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11674
11675 debug_last_unchanged_at_beg_vpos
11676 = (last_unchanged_at_beg_row
11677 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11678 : -1);
11679 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11680
11681 #endif /* GLYPH_DEBUG != 0 */
11682
11683
11684 /* Display new lines. Set last_text_row to the last new line
11685 displayed which has text on it, i.e. might end up as being the
11686 line where the window_end_vpos is. */
11687 w->cursor.vpos = -1;
11688 last_text_row = NULL;
11689 overlay_arrow_seen = 0;
11690 while (it.current_y < it.last_visible_y
11691 && !fonts_changed_p
11692 && (first_unchanged_at_end_row == NULL
11693 || IT_CHARPOS (it) < stop_pos))
11694 {
11695 if (display_line (&it))
11696 last_text_row = it.glyph_row - 1;
11697 }
11698
11699 if (fonts_changed_p)
11700 return -1;
11701
11702
11703 /* Compute differences in buffer positions, y-positions etc. for
11704 lines reused at the bottom of the window. Compute what we can
11705 scroll. */
11706 if (first_unchanged_at_end_row
11707 /* No lines reused because we displayed everything up to the
11708 bottom of the window. */
11709 && it.current_y < it.last_visible_y)
11710 {
11711 dvpos = (it.vpos
11712 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11713 current_matrix));
11714 dy = it.current_y - first_unchanged_at_end_row->y;
11715 run.current_y = first_unchanged_at_end_row->y;
11716 run.desired_y = run.current_y + dy;
11717 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11718 }
11719 else
11720 {
11721 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11722 first_unchanged_at_end_row = NULL;
11723 }
11724 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11725
11726
11727 /* Find the cursor if not already found. We have to decide whether
11728 PT will appear on this window (it sometimes doesn't, but this is
11729 not a very frequent case.) This decision has to be made before
11730 the current matrix is altered. A value of cursor.vpos < 0 means
11731 that PT is either in one of the lines beginning at
11732 first_unchanged_at_end_row or below the window. Don't care for
11733 lines that might be displayed later at the window end; as
11734 mentioned, this is not a frequent case. */
11735 if (w->cursor.vpos < 0)
11736 {
11737 /* Cursor in unchanged rows at the top? */
11738 if (PT < CHARPOS (start_pos)
11739 && last_unchanged_at_beg_row)
11740 {
11741 row = row_containing_pos (w, PT,
11742 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11743 last_unchanged_at_beg_row + 1, 0);
11744 if (row)
11745 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11746 }
11747
11748 /* Start from first_unchanged_at_end_row looking for PT. */
11749 else if (first_unchanged_at_end_row)
11750 {
11751 row = row_containing_pos (w, PT - delta,
11752 first_unchanged_at_end_row, NULL, 0);
11753 if (row)
11754 set_cursor_from_row (w, row, w->current_matrix, delta,
11755 delta_bytes, dy, dvpos);
11756 }
11757
11758 /* Give up if cursor was not found. */
11759 if (w->cursor.vpos < 0)
11760 {
11761 clear_glyph_matrix (w->desired_matrix);
11762 return -1;
11763 }
11764 }
11765
11766 /* Don't let the cursor end in the scroll margins. */
11767 {
11768 int this_scroll_margin, cursor_height;
11769
11770 this_scroll_margin = max (0, scroll_margin);
11771 this_scroll_margin = min (this_scroll_margin,
11772 XFASTINT (w->height) / 4);
11773 this_scroll_margin *= CANON_Y_UNIT (it.f);
11774 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11775
11776 if ((w->cursor.y < this_scroll_margin
11777 && CHARPOS (start) > BEGV)
11778 /* Don't take scroll margin into account at the bottom because
11779 old redisplay didn't do it either. */
11780 || w->cursor.y + cursor_height > it.last_visible_y)
11781 {
11782 w->cursor.vpos = -1;
11783 clear_glyph_matrix (w->desired_matrix);
11784 return -1;
11785 }
11786 }
11787
11788 /* Scroll the display. Do it before changing the current matrix so
11789 that xterm.c doesn't get confused about where the cursor glyph is
11790 found. */
11791 if (dy && run.height)
11792 {
11793 update_begin (f);
11794
11795 if (FRAME_WINDOW_P (f))
11796 {
11797 rif->update_window_begin_hook (w);
11798 rif->clear_mouse_face (w);
11799 rif->scroll_run_hook (w, &run);
11800 rif->update_window_end_hook (w, 0, 0);
11801 }
11802 else
11803 {
11804 /* Terminal frame. In this case, dvpos gives the number of
11805 lines to scroll by; dvpos < 0 means scroll up. */
11806 int first_unchanged_at_end_vpos
11807 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11808 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11809 int end = (XFASTINT (w->top)
11810 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11811 + window_internal_height (w));
11812
11813 /* Perform the operation on the screen. */
11814 if (dvpos > 0)
11815 {
11816 /* Scroll last_unchanged_at_beg_row to the end of the
11817 window down dvpos lines. */
11818 set_terminal_window (end);
11819
11820 /* On dumb terminals delete dvpos lines at the end
11821 before inserting dvpos empty lines. */
11822 if (!scroll_region_ok)
11823 ins_del_lines (end - dvpos, -dvpos);
11824
11825 /* Insert dvpos empty lines in front of
11826 last_unchanged_at_beg_row. */
11827 ins_del_lines (from, dvpos);
11828 }
11829 else if (dvpos < 0)
11830 {
11831 /* Scroll up last_unchanged_at_beg_vpos to the end of
11832 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11833 set_terminal_window (end);
11834
11835 /* Delete dvpos lines in front of
11836 last_unchanged_at_beg_vpos. ins_del_lines will set
11837 the cursor to the given vpos and emit |dvpos| delete
11838 line sequences. */
11839 ins_del_lines (from + dvpos, dvpos);
11840
11841 /* On a dumb terminal insert dvpos empty lines at the
11842 end. */
11843 if (!scroll_region_ok)
11844 ins_del_lines (end + dvpos, -dvpos);
11845 }
11846
11847 set_terminal_window (0);
11848 }
11849
11850 update_end (f);
11851 }
11852
11853 /* Shift reused rows of the current matrix to the right position.
11854 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11855 text. */
11856 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11857 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11858 if (dvpos < 0)
11859 {
11860 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11861 bottom_vpos, dvpos);
11862 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11863 bottom_vpos, 0);
11864 }
11865 else if (dvpos > 0)
11866 {
11867 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11868 bottom_vpos, dvpos);
11869 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11870 first_unchanged_at_end_vpos + dvpos, 0);
11871 }
11872
11873 /* For frame-based redisplay, make sure that current frame and window
11874 matrix are in sync with respect to glyph memory. */
11875 if (!FRAME_WINDOW_P (f))
11876 sync_frame_with_window_matrix_rows (w);
11877
11878 /* Adjust buffer positions in reused rows. */
11879 if (delta)
11880 increment_matrix_positions (current_matrix,
11881 first_unchanged_at_end_vpos + dvpos,
11882 bottom_vpos, delta, delta_bytes);
11883
11884 /* Adjust Y positions. */
11885 if (dy)
11886 shift_glyph_matrix (w, current_matrix,
11887 first_unchanged_at_end_vpos + dvpos,
11888 bottom_vpos, dy);
11889
11890 if (first_unchanged_at_end_row)
11891 first_unchanged_at_end_row += dvpos;
11892
11893 /* If scrolling up, there may be some lines to display at the end of
11894 the window. */
11895 last_text_row_at_end = NULL;
11896 if (dy < 0)
11897 {
11898 /* Scrolling up can leave for example a partially visible line
11899 at the end of the window to be redisplayed. */
11900 /* Set last_row to the glyph row in the current matrix where the
11901 window end line is found. It has been moved up or down in
11902 the matrix by dvpos. */
11903 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11904 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11905
11906 /* If last_row is the window end line, it should display text. */
11907 xassert (last_row->displays_text_p);
11908
11909 /* If window end line was partially visible before, begin
11910 displaying at that line. Otherwise begin displaying with the
11911 line following it. */
11912 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11913 {
11914 init_to_row_start (&it, w, last_row);
11915 it.vpos = last_vpos;
11916 it.current_y = last_row->y;
11917 }
11918 else
11919 {
11920 init_to_row_end (&it, w, last_row);
11921 it.vpos = 1 + last_vpos;
11922 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11923 ++last_row;
11924 }
11925
11926 /* We may start in a continuation line. If so, we have to
11927 get the right continuation_lines_width and current_x. */
11928 it.continuation_lines_width = last_row->continuation_lines_width;
11929 it.hpos = it.current_x = 0;
11930
11931 /* Display the rest of the lines at the window end. */
11932 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11933 while (it.current_y < it.last_visible_y
11934 && !fonts_changed_p)
11935 {
11936 /* Is it always sure that the display agrees with lines in
11937 the current matrix? I don't think so, so we mark rows
11938 displayed invalid in the current matrix by setting their
11939 enabled_p flag to zero. */
11940 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11941 if (display_line (&it))
11942 last_text_row_at_end = it.glyph_row - 1;
11943 }
11944 }
11945
11946 /* Update window_end_pos and window_end_vpos. */
11947 if (first_unchanged_at_end_row
11948 && first_unchanged_at_end_row->y < it.last_visible_y
11949 && !last_text_row_at_end)
11950 {
11951 /* Window end line if one of the preserved rows from the current
11952 matrix. Set row to the last row displaying text in current
11953 matrix starting at first_unchanged_at_end_row, after
11954 scrolling. */
11955 xassert (first_unchanged_at_end_row->displays_text_p);
11956 row = find_last_row_displaying_text (w->current_matrix, &it,
11957 first_unchanged_at_end_row);
11958 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11959
11960 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11961 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11962 w->window_end_vpos
11963 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
11964 xassert (w->window_end_bytepos >= 0);
11965 IF_DEBUG (debug_method_add (w, "A"));
11966 }
11967 else if (last_text_row_at_end)
11968 {
11969 w->window_end_pos
11970 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11971 w->window_end_bytepos
11972 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11973 w->window_end_vpos
11974 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11975 xassert (w->window_end_bytepos >= 0);
11976 IF_DEBUG (debug_method_add (w, "B"));
11977 }
11978 else if (last_text_row)
11979 {
11980 /* We have displayed either to the end of the window or at the
11981 end of the window, i.e. the last row with text is to be found
11982 in the desired matrix. */
11983 w->window_end_pos
11984 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11985 w->window_end_bytepos
11986 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11987 w->window_end_vpos
11988 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11989 xassert (w->window_end_bytepos >= 0);
11990 }
11991 else if (first_unchanged_at_end_row == NULL
11992 && last_text_row == NULL
11993 && last_text_row_at_end == NULL)
11994 {
11995 /* Displayed to end of window, but no line containing text was
11996 displayed. Lines were deleted at the end of the window. */
11997 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11998 int vpos = XFASTINT (w->window_end_vpos);
11999 struct glyph_row *current_row = current_matrix->rows + vpos;
12000 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12001
12002 for (row = NULL;
12003 row == NULL && vpos >= first_vpos;
12004 --vpos, --current_row, --desired_row)
12005 {
12006 if (desired_row->enabled_p)
12007 {
12008 if (desired_row->displays_text_p)
12009 row = desired_row;
12010 }
12011 else if (current_row->displays_text_p)
12012 row = current_row;
12013 }
12014
12015 xassert (row != NULL);
12016 w->window_end_vpos = make_number (vpos + 1);
12017 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12018 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12019 xassert (w->window_end_bytepos >= 0);
12020 IF_DEBUG (debug_method_add (w, "C"));
12021 }
12022 else
12023 abort ();
12024
12025 #if 0 /* This leads to problems, for instance when the cursor is
12026 at ZV, and the cursor line displays no text. */
12027 /* Disable rows below what's displayed in the window. This makes
12028 debugging easier. */
12029 enable_glyph_matrix_rows (current_matrix,
12030 XFASTINT (w->window_end_vpos) + 1,
12031 bottom_vpos, 0);
12032 #endif
12033
12034 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12035 debug_end_vpos = XFASTINT (w->window_end_vpos));
12036
12037 /* Record that display has not been completed. */
12038 w->window_end_valid = Qnil;
12039 w->desired_matrix->no_scrolling_p = 1;
12040 return 3;
12041
12042 #undef GIVE_UP
12043 }
12044
12045
12046 \f
12047 /***********************************************************************
12048 More debugging support
12049 ***********************************************************************/
12050
12051 #if GLYPH_DEBUG
12052
12053 void dump_glyph_row P_ ((struct glyph_row *, int, int));
12054 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
12055 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
12056
12057
12058 /* Dump the contents of glyph matrix MATRIX on stderr.
12059
12060 GLYPHS 0 means don't show glyph contents.
12061 GLYPHS 1 means show glyphs in short form
12062 GLYPHS > 1 means show glyphs in long form. */
12063
12064 void
12065 dump_glyph_matrix (matrix, glyphs)
12066 struct glyph_matrix *matrix;
12067 int glyphs;
12068 {
12069 int i;
12070 for (i = 0; i < matrix->nrows; ++i)
12071 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
12072 }
12073
12074
12075 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12076 the glyph row and area where the glyph comes from. */
12077
12078 void
12079 dump_glyph (row, glyph, area)
12080 struct glyph_row *row;
12081 struct glyph *glyph;
12082 int area;
12083 {
12084 if (glyph->type == CHAR_GLYPH)
12085 {
12086 fprintf (stderr,
12087 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12088 glyph - row->glyphs[TEXT_AREA],
12089 'C',
12090 glyph->charpos,
12091 (BUFFERP (glyph->object)
12092 ? 'B'
12093 : (STRINGP (glyph->object)
12094 ? 'S'
12095 : '-')),
12096 glyph->pixel_width,
12097 glyph->u.ch,
12098 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12099 ? glyph->u.ch
12100 : '.'),
12101 glyph->face_id,
12102 glyph->left_box_line_p,
12103 glyph->right_box_line_p);
12104 }
12105 else if (glyph->type == STRETCH_GLYPH)
12106 {
12107 fprintf (stderr,
12108 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12109 glyph - row->glyphs[TEXT_AREA],
12110 'S',
12111 glyph->charpos,
12112 (BUFFERP (glyph->object)
12113 ? 'B'
12114 : (STRINGP (glyph->object)
12115 ? 'S'
12116 : '-')),
12117 glyph->pixel_width,
12118 0,
12119 '.',
12120 glyph->face_id,
12121 glyph->left_box_line_p,
12122 glyph->right_box_line_p);
12123 }
12124 else if (glyph->type == IMAGE_GLYPH)
12125 {
12126 fprintf (stderr,
12127 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12128 glyph - row->glyphs[TEXT_AREA],
12129 'I',
12130 glyph->charpos,
12131 (BUFFERP (glyph->object)
12132 ? 'B'
12133 : (STRINGP (glyph->object)
12134 ? 'S'
12135 : '-')),
12136 glyph->pixel_width,
12137 glyph->u.img_id,
12138 '.',
12139 glyph->face_id,
12140 glyph->left_box_line_p,
12141 glyph->right_box_line_p);
12142 }
12143 }
12144
12145
12146 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12147 GLYPHS 0 means don't show glyph contents.
12148 GLYPHS 1 means show glyphs in short form
12149 GLYPHS > 1 means show glyphs in long form. */
12150
12151 void
12152 dump_glyph_row (row, vpos, glyphs)
12153 struct glyph_row *row;
12154 int vpos, glyphs;
12155 {
12156 if (glyphs != 1)
12157 {
12158 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12159 fprintf (stderr, "=======================================================================\n");
12160
12161 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
12162 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12163 vpos,
12164 MATRIX_ROW_START_CHARPOS (row),
12165 MATRIX_ROW_END_CHARPOS (row),
12166 row->used[TEXT_AREA],
12167 row->contains_overlapping_glyphs_p,
12168 row->enabled_p,
12169 row->truncated_on_left_p,
12170 row->truncated_on_right_p,
12171 row->overlay_arrow_p,
12172 row->continued_p,
12173 MATRIX_ROW_CONTINUATION_LINE_P (row),
12174 row->displays_text_p,
12175 row->ends_at_zv_p,
12176 row->fill_line_p,
12177 row->ends_in_middle_of_char_p,
12178 row->starts_in_middle_of_char_p,
12179 row->mouse_face_p,
12180 row->x,
12181 row->y,
12182 row->pixel_width,
12183 row->height,
12184 row->visible_height,
12185 row->ascent,
12186 row->phys_ascent);
12187 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12188 row->end.overlay_string_index,
12189 row->continuation_lines_width);
12190 fprintf (stderr, "%9d %5d\n",
12191 CHARPOS (row->start.string_pos),
12192 CHARPOS (row->end.string_pos));
12193 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12194 row->end.dpvec_index);
12195 }
12196
12197 if (glyphs > 1)
12198 {
12199 int area;
12200
12201 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12202 {
12203 struct glyph *glyph = row->glyphs[area];
12204 struct glyph *glyph_end = glyph + row->used[area];
12205
12206 /* Glyph for a line end in text. */
12207 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12208 ++glyph_end;
12209
12210 if (glyph < glyph_end)
12211 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12212
12213 for (; glyph < glyph_end; ++glyph)
12214 dump_glyph (row, glyph, area);
12215 }
12216 }
12217 else if (glyphs == 1)
12218 {
12219 int area;
12220
12221 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12222 {
12223 char *s = (char *) alloca (row->used[area] + 1);
12224 int i;
12225
12226 for (i = 0; i < row->used[area]; ++i)
12227 {
12228 struct glyph *glyph = row->glyphs[area] + i;
12229 if (glyph->type == CHAR_GLYPH
12230 && glyph->u.ch < 0x80
12231 && glyph->u.ch >= ' ')
12232 s[i] = glyph->u.ch;
12233 else
12234 s[i] = '.';
12235 }
12236
12237 s[i] = '\0';
12238 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12239 }
12240 }
12241 }
12242
12243
12244 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12245 Sdump_glyph_matrix, 0, 1, "p",
12246 doc: /* Dump the current matrix of the selected window to stderr.
12247 Shows contents of glyph row structures. With non-nil
12248 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12249 glyphs in short form, otherwise show glyphs in long form. */)
12250 (glyphs)
12251 Lisp_Object glyphs;
12252 {
12253 struct window *w = XWINDOW (selected_window);
12254 struct buffer *buffer = XBUFFER (w->buffer);
12255
12256 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12257 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12258 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12259 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12260 fprintf (stderr, "=============================================\n");
12261 dump_glyph_matrix (w->current_matrix,
12262 NILP (glyphs) ? 0 : XINT (glyphs));
12263 return Qnil;
12264 }
12265
12266
12267 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12268 doc: /* Dump glyph row ROW to stderr.
12269 GLYPH 0 means don't dump glyphs.
12270 GLYPH 1 means dump glyphs in short form.
12271 GLYPH > 1 or omitted means dump glyphs in long form. */)
12272 (row, glyphs)
12273 Lisp_Object row, glyphs;
12274 {
12275 struct glyph_matrix *matrix;
12276 int vpos;
12277
12278 CHECK_NUMBER (row);
12279 matrix = XWINDOW (selected_window)->current_matrix;
12280 vpos = XINT (row);
12281 if (vpos >= 0 && vpos < matrix->nrows)
12282 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12283 vpos,
12284 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12285 return Qnil;
12286 }
12287
12288
12289 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12290 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12291 GLYPH 0 means don't dump glyphs.
12292 GLYPH 1 means dump glyphs in short form.
12293 GLYPH > 1 or omitted means dump glyphs in long form. */)
12294 (row, glyphs)
12295 Lisp_Object row, glyphs;
12296 {
12297 struct frame *sf = SELECTED_FRAME ();
12298 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12299 int vpos;
12300
12301 CHECK_NUMBER (row);
12302 vpos = XINT (row);
12303 if (vpos >= 0 && vpos < m->nrows)
12304 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12305 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12306 return Qnil;
12307 }
12308
12309
12310 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12311 doc: /* Toggle tracing of redisplay.
12312 With ARG, turn tracing on if and only if ARG is positive. */)
12313 (arg)
12314 Lisp_Object arg;
12315 {
12316 if (NILP (arg))
12317 trace_redisplay_p = !trace_redisplay_p;
12318 else
12319 {
12320 arg = Fprefix_numeric_value (arg);
12321 trace_redisplay_p = XINT (arg) > 0;
12322 }
12323
12324 return Qnil;
12325 }
12326
12327
12328 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12329 doc: /* Like `format', but print result to stderr. */)
12330 (nargs, args)
12331 int nargs;
12332 Lisp_Object *args;
12333 {
12334 Lisp_Object s = Fformat (nargs, args);
12335 fprintf (stderr, "%s", XSTRING (s)->data);
12336 return Qnil;
12337 }
12338
12339 #endif /* GLYPH_DEBUG */
12340
12341
12342 \f
12343 /***********************************************************************
12344 Building Desired Matrix Rows
12345 ***********************************************************************/
12346
12347 /* Return a temporary glyph row holding the glyphs of an overlay
12348 arrow. Only used for non-window-redisplay windows. */
12349
12350 static struct glyph_row *
12351 get_overlay_arrow_glyph_row (w)
12352 struct window *w;
12353 {
12354 struct frame *f = XFRAME (WINDOW_FRAME (w));
12355 struct buffer *buffer = XBUFFER (w->buffer);
12356 struct buffer *old = current_buffer;
12357 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
12358 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
12359 unsigned char *arrow_end = arrow_string + arrow_len;
12360 unsigned char *p;
12361 struct it it;
12362 int multibyte_p;
12363 int n_glyphs_before;
12364
12365 set_buffer_temp (buffer);
12366 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12367 it.glyph_row->used[TEXT_AREA] = 0;
12368 SET_TEXT_POS (it.position, 0, 0);
12369
12370 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12371 p = arrow_string;
12372 while (p < arrow_end)
12373 {
12374 Lisp_Object face, ilisp;
12375
12376 /* Get the next character. */
12377 if (multibyte_p)
12378 it.c = string_char_and_length (p, arrow_len, &it.len);
12379 else
12380 it.c = *p, it.len = 1;
12381 p += it.len;
12382
12383 /* Get its face. */
12384 ilisp = make_number (p - arrow_string);
12385 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12386 it.face_id = compute_char_face (f, it.c, face);
12387
12388 /* Compute its width, get its glyphs. */
12389 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12390 SET_TEXT_POS (it.position, -1, -1);
12391 PRODUCE_GLYPHS (&it);
12392
12393 /* If this character doesn't fit any more in the line, we have
12394 to remove some glyphs. */
12395 if (it.current_x > it.last_visible_x)
12396 {
12397 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12398 break;
12399 }
12400 }
12401
12402 set_buffer_temp (old);
12403 return it.glyph_row;
12404 }
12405
12406
12407 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12408 glyphs are only inserted for terminal frames since we can't really
12409 win with truncation glyphs when partially visible glyphs are
12410 involved. Which glyphs to insert is determined by
12411 produce_special_glyphs. */
12412
12413 static void
12414 insert_left_trunc_glyphs (it)
12415 struct it *it;
12416 {
12417 struct it truncate_it;
12418 struct glyph *from, *end, *to, *toend;
12419
12420 xassert (!FRAME_WINDOW_P (it->f));
12421
12422 /* Get the truncation glyphs. */
12423 truncate_it = *it;
12424 truncate_it.current_x = 0;
12425 truncate_it.face_id = DEFAULT_FACE_ID;
12426 truncate_it.glyph_row = &scratch_glyph_row;
12427 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12428 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12429 truncate_it.object = make_number (0);
12430 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12431
12432 /* Overwrite glyphs from IT with truncation glyphs. */
12433 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12434 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12435 to = it->glyph_row->glyphs[TEXT_AREA];
12436 toend = to + it->glyph_row->used[TEXT_AREA];
12437
12438 while (from < end)
12439 *to++ = *from++;
12440
12441 /* There may be padding glyphs left over. Overwrite them too. */
12442 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12443 {
12444 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12445 while (from < end)
12446 *to++ = *from++;
12447 }
12448
12449 if (to > toend)
12450 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12451 }
12452
12453
12454 /* Compute the pixel height and width of IT->glyph_row.
12455
12456 Most of the time, ascent and height of a display line will be equal
12457 to the max_ascent and max_height values of the display iterator
12458 structure. This is not the case if
12459
12460 1. We hit ZV without displaying anything. In this case, max_ascent
12461 and max_height will be zero.
12462
12463 2. We have some glyphs that don't contribute to the line height.
12464 (The glyph row flag contributes_to_line_height_p is for future
12465 pixmap extensions).
12466
12467 The first case is easily covered by using default values because in
12468 these cases, the line height does not really matter, except that it
12469 must not be zero. */
12470
12471 static void
12472 compute_line_metrics (it)
12473 struct it *it;
12474 {
12475 struct glyph_row *row = it->glyph_row;
12476 int area, i;
12477
12478 if (FRAME_WINDOW_P (it->f))
12479 {
12480 int i, min_y, max_y;
12481
12482 /* The line may consist of one space only, that was added to
12483 place the cursor on it. If so, the row's height hasn't been
12484 computed yet. */
12485 if (row->height == 0)
12486 {
12487 if (it->max_ascent + it->max_descent == 0)
12488 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12489 row->ascent = it->max_ascent;
12490 row->height = it->max_ascent + it->max_descent;
12491 row->phys_ascent = it->max_phys_ascent;
12492 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12493 }
12494
12495 /* Compute the width of this line. */
12496 row->pixel_width = row->x;
12497 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12498 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12499
12500 xassert (row->pixel_width >= 0);
12501 xassert (row->ascent >= 0 && row->height > 0);
12502
12503 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12504 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12505
12506 /* If first line's physical ascent is larger than its logical
12507 ascent, use the physical ascent, and make the row taller.
12508 This makes accented characters fully visible. */
12509 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12510 && row->phys_ascent > row->ascent)
12511 {
12512 row->height += row->phys_ascent - row->ascent;
12513 row->ascent = row->phys_ascent;
12514 }
12515
12516 /* Compute how much of the line is visible. */
12517 row->visible_height = row->height;
12518
12519 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12520 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12521
12522 if (row->y < min_y)
12523 row->visible_height -= min_y - row->y;
12524 if (row->y + row->height > max_y)
12525 row->visible_height -= row->y + row->height - max_y;
12526 }
12527 else
12528 {
12529 row->pixel_width = row->used[TEXT_AREA];
12530 if (row->continued_p)
12531 row->pixel_width -= it->continuation_pixel_width;
12532 else if (row->truncated_on_right_p)
12533 row->pixel_width -= it->truncation_pixel_width;
12534 row->ascent = row->phys_ascent = 0;
12535 row->height = row->phys_height = row->visible_height = 1;
12536 }
12537
12538 /* Compute a hash code for this row. */
12539 row->hash = 0;
12540 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12541 for (i = 0; i < row->used[area]; ++i)
12542 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12543 + row->glyphs[area][i].u.val
12544 + row->glyphs[area][i].face_id
12545 + row->glyphs[area][i].padding_p
12546 + (row->glyphs[area][i].type << 2));
12547
12548 it->max_ascent = it->max_descent = 0;
12549 it->max_phys_ascent = it->max_phys_descent = 0;
12550 }
12551
12552
12553 /* Append one space to the glyph row of iterator IT if doing a
12554 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12555 space have the default face, otherwise let it have the same face as
12556 IT->face_id. Value is non-zero if a space was added.
12557
12558 This function is called to make sure that there is always one glyph
12559 at the end of a glyph row that the cursor can be set on under
12560 window-systems. (If there weren't such a glyph we would not know
12561 how wide and tall a box cursor should be displayed).
12562
12563 At the same time this space let's a nicely handle clearing to the
12564 end of the line if the row ends in italic text. */
12565
12566 static int
12567 append_space (it, default_face_p)
12568 struct it *it;
12569 int default_face_p;
12570 {
12571 if (FRAME_WINDOW_P (it->f))
12572 {
12573 int n = it->glyph_row->used[TEXT_AREA];
12574
12575 if (it->glyph_row->glyphs[TEXT_AREA] + n
12576 < it->glyph_row->glyphs[1 + TEXT_AREA])
12577 {
12578 /* Save some values that must not be changed.
12579 Must save IT->c and IT->len because otherwise
12580 ITERATOR_AT_END_P wouldn't work anymore after
12581 append_space has been called. */
12582 enum display_element_type saved_what = it->what;
12583 int saved_c = it->c, saved_len = it->len;
12584 int saved_x = it->current_x;
12585 int saved_face_id = it->face_id;
12586 struct text_pos saved_pos;
12587 Lisp_Object saved_object;
12588 struct face *face;
12589
12590 saved_object = it->object;
12591 saved_pos = it->position;
12592
12593 it->what = IT_CHARACTER;
12594 bzero (&it->position, sizeof it->position);
12595 it->object = make_number (0);
12596 it->c = ' ';
12597 it->len = 1;
12598
12599 if (default_face_p)
12600 it->face_id = DEFAULT_FACE_ID;
12601 else if (it->face_before_selective_p)
12602 it->face_id = it->saved_face_id;
12603 face = FACE_FROM_ID (it->f, it->face_id);
12604 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12605
12606 PRODUCE_GLYPHS (it);
12607
12608 it->current_x = saved_x;
12609 it->object = saved_object;
12610 it->position = saved_pos;
12611 it->what = saved_what;
12612 it->face_id = saved_face_id;
12613 it->len = saved_len;
12614 it->c = saved_c;
12615 return 1;
12616 }
12617 }
12618
12619 return 0;
12620 }
12621
12622
12623 /* Extend the face of the last glyph in the text area of IT->glyph_row
12624 to the end of the display line. Called from display_line.
12625 If the glyph row is empty, add a space glyph to it so that we
12626 know the face to draw. Set the glyph row flag fill_line_p. */
12627
12628 static void
12629 extend_face_to_end_of_line (it)
12630 struct it *it;
12631 {
12632 struct face *face;
12633 struct frame *f = it->f;
12634
12635 /* If line is already filled, do nothing. */
12636 if (it->current_x >= it->last_visible_x)
12637 return;
12638
12639 /* Face extension extends the background and box of IT->face_id
12640 to the end of the line. If the background equals the background
12641 of the frame, we don't have to do anything. */
12642 if (it->face_before_selective_p)
12643 face = FACE_FROM_ID (it->f, it->saved_face_id);
12644 else
12645 face = FACE_FROM_ID (f, it->face_id);
12646
12647 if (FRAME_WINDOW_P (f)
12648 && face->box == FACE_NO_BOX
12649 && face->background == FRAME_BACKGROUND_PIXEL (f)
12650 && !face->stipple)
12651 return;
12652
12653 /* Set the glyph row flag indicating that the face of the last glyph
12654 in the text area has to be drawn to the end of the text area. */
12655 it->glyph_row->fill_line_p = 1;
12656
12657 /* If current character of IT is not ASCII, make sure we have the
12658 ASCII face. This will be automatically undone the next time
12659 get_next_display_element returns a multibyte character. Note
12660 that the character will always be single byte in unibyte text. */
12661 if (!SINGLE_BYTE_CHAR_P (it->c))
12662 {
12663 it->face_id = FACE_FOR_CHAR (f, face, 0);
12664 }
12665
12666 if (FRAME_WINDOW_P (f))
12667 {
12668 /* If the row is empty, add a space with the current face of IT,
12669 so that we know which face to draw. */
12670 if (it->glyph_row->used[TEXT_AREA] == 0)
12671 {
12672 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12673 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12674 it->glyph_row->used[TEXT_AREA] = 1;
12675 }
12676 }
12677 else
12678 {
12679 /* Save some values that must not be changed. */
12680 int saved_x = it->current_x;
12681 struct text_pos saved_pos;
12682 Lisp_Object saved_object;
12683 enum display_element_type saved_what = it->what;
12684 int saved_face_id = it->face_id;
12685
12686 saved_object = it->object;
12687 saved_pos = it->position;
12688
12689 it->what = IT_CHARACTER;
12690 bzero (&it->position, sizeof it->position);
12691 it->object = make_number (0);
12692 it->c = ' ';
12693 it->len = 1;
12694 it->face_id = face->id;
12695
12696 PRODUCE_GLYPHS (it);
12697
12698 while (it->current_x <= it->last_visible_x)
12699 PRODUCE_GLYPHS (it);
12700
12701 /* Don't count these blanks really. It would let us insert a left
12702 truncation glyph below and make us set the cursor on them, maybe. */
12703 it->current_x = saved_x;
12704 it->object = saved_object;
12705 it->position = saved_pos;
12706 it->what = saved_what;
12707 it->face_id = saved_face_id;
12708 }
12709 }
12710
12711
12712 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12713 trailing whitespace. */
12714
12715 static int
12716 trailing_whitespace_p (charpos)
12717 int charpos;
12718 {
12719 int bytepos = CHAR_TO_BYTE (charpos);
12720 int c = 0;
12721
12722 while (bytepos < ZV_BYTE
12723 && (c = FETCH_CHAR (bytepos),
12724 c == ' ' || c == '\t'))
12725 ++bytepos;
12726
12727 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12728 {
12729 if (bytepos != PT_BYTE)
12730 return 1;
12731 }
12732 return 0;
12733 }
12734
12735
12736 /* Highlight trailing whitespace, if any, in ROW. */
12737
12738 void
12739 highlight_trailing_whitespace (f, row)
12740 struct frame *f;
12741 struct glyph_row *row;
12742 {
12743 int used = row->used[TEXT_AREA];
12744
12745 if (used)
12746 {
12747 struct glyph *start = row->glyphs[TEXT_AREA];
12748 struct glyph *glyph = start + used - 1;
12749
12750 /* Skip over glyphs inserted to display the cursor at the
12751 end of a line, for extending the face of the last glyph
12752 to the end of the line on terminals, and for truncation
12753 and continuation glyphs. */
12754 while (glyph >= start
12755 && glyph->type == CHAR_GLYPH
12756 && INTEGERP (glyph->object))
12757 --glyph;
12758
12759 /* If last glyph is a space or stretch, and it's trailing
12760 whitespace, set the face of all trailing whitespace glyphs in
12761 IT->glyph_row to `trailing-whitespace'. */
12762 if (glyph >= start
12763 && BUFFERP (glyph->object)
12764 && (glyph->type == STRETCH_GLYPH
12765 || (glyph->type == CHAR_GLYPH
12766 && glyph->u.ch == ' '))
12767 && trailing_whitespace_p (glyph->charpos))
12768 {
12769 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12770
12771 while (glyph >= start
12772 && BUFFERP (glyph->object)
12773 && (glyph->type == STRETCH_GLYPH
12774 || (glyph->type == CHAR_GLYPH
12775 && glyph->u.ch == ' ')))
12776 (glyph--)->face_id = face_id;
12777 }
12778 }
12779 }
12780
12781
12782 /* Value is non-zero if glyph row ROW in window W should be
12783 used to hold the cursor. */
12784
12785 static int
12786 cursor_row_p (w, row)
12787 struct window *w;
12788 struct glyph_row *row;
12789 {
12790 int cursor_row_p = 1;
12791
12792 if (PT == MATRIX_ROW_END_CHARPOS (row))
12793 {
12794 /* If the row ends with a newline from a string, we don't want
12795 the cursor there (if the row is continued it doesn't end in a
12796 newline). */
12797 if (CHARPOS (row->end.string_pos) >= 0
12798 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12799 cursor_row_p = row->continued_p;
12800
12801 /* If the row ends at ZV, display the cursor at the end of that
12802 row instead of at the start of the row below. */
12803 else if (row->ends_at_zv_p)
12804 cursor_row_p = 1;
12805 else
12806 cursor_row_p = 0;
12807 }
12808
12809 return cursor_row_p;
12810 }
12811
12812
12813 /* Construct the glyph row IT->glyph_row in the desired matrix of
12814 IT->w from text at the current position of IT. See dispextern.h
12815 for an overview of struct it. Value is non-zero if
12816 IT->glyph_row displays text, as opposed to a line displaying ZV
12817 only. */
12818
12819 static int
12820 display_line (it)
12821 struct it *it;
12822 {
12823 struct glyph_row *row = it->glyph_row;
12824
12825 /* We always start displaying at hpos zero even if hscrolled. */
12826 xassert (it->hpos == 0 && it->current_x == 0);
12827
12828 /* We must not display in a row that's not a text row. */
12829 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12830 < it->w->desired_matrix->nrows);
12831
12832 /* Is IT->w showing the region? */
12833 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12834
12835 /* Clear the result glyph row and enable it. */
12836 prepare_desired_row (row);
12837
12838 row->y = it->current_y;
12839 row->start = it->current;
12840 row->continuation_lines_width = it->continuation_lines_width;
12841 row->displays_text_p = 1;
12842 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12843 it->starts_in_middle_of_char_p = 0;
12844
12845 /* Arrange the overlays nicely for our purposes. Usually, we call
12846 display_line on only one line at a time, in which case this
12847 can't really hurt too much, or we call it on lines which appear
12848 one after another in the buffer, in which case all calls to
12849 recenter_overlay_lists but the first will be pretty cheap. */
12850 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12851
12852 /* Move over display elements that are not visible because we are
12853 hscrolled. This may stop at an x-position < IT->first_visible_x
12854 if the first glyph is partially visible or if we hit a line end. */
12855 if (it->current_x < it->first_visible_x)
12856 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12857 MOVE_TO_POS | MOVE_TO_X);
12858
12859 /* Get the initial row height. This is either the height of the
12860 text hscrolled, if there is any, or zero. */
12861 row->ascent = it->max_ascent;
12862 row->height = it->max_ascent + it->max_descent;
12863 row->phys_ascent = it->max_phys_ascent;
12864 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12865
12866 /* Loop generating characters. The loop is left with IT on the next
12867 character to display. */
12868 while (1)
12869 {
12870 int n_glyphs_before, hpos_before, x_before;
12871 int x, i, nglyphs;
12872 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12873
12874 /* Retrieve the next thing to display. Value is zero if end of
12875 buffer reached. */
12876 if (!get_next_display_element (it))
12877 {
12878 /* Maybe add a space at the end of this line that is used to
12879 display the cursor there under X. Set the charpos of the
12880 first glyph of blank lines not corresponding to any text
12881 to -1. */
12882 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12883 || row->used[TEXT_AREA] == 0)
12884 {
12885 row->glyphs[TEXT_AREA]->charpos = -1;
12886 row->displays_text_p = 0;
12887
12888 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
12889 && (!MINI_WINDOW_P (it->w)
12890 || (minibuf_level && EQ (it->window, minibuf_window))))
12891 row->indicate_empty_line_p = 1;
12892 }
12893
12894 it->continuation_lines_width = 0;
12895 row->ends_at_zv_p = 1;
12896 break;
12897 }
12898
12899 /* Now, get the metrics of what we want to display. This also
12900 generates glyphs in `row' (which is IT->glyph_row). */
12901 n_glyphs_before = row->used[TEXT_AREA];
12902 x = it->current_x;
12903
12904 /* Remember the line height so far in case the next element doesn't
12905 fit on the line. */
12906 if (!it->truncate_lines_p)
12907 {
12908 ascent = it->max_ascent;
12909 descent = it->max_descent;
12910 phys_ascent = it->max_phys_ascent;
12911 phys_descent = it->max_phys_descent;
12912 }
12913
12914 PRODUCE_GLYPHS (it);
12915
12916 /* If this display element was in marginal areas, continue with
12917 the next one. */
12918 if (it->area != TEXT_AREA)
12919 {
12920 row->ascent = max (row->ascent, it->max_ascent);
12921 row->height = max (row->height, it->max_ascent + it->max_descent);
12922 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12923 row->phys_height = max (row->phys_height,
12924 it->max_phys_ascent + it->max_phys_descent);
12925 set_iterator_to_next (it, 1);
12926 continue;
12927 }
12928
12929 /* Does the display element fit on the line? If we truncate
12930 lines, we should draw past the right edge of the window. If
12931 we don't truncate, we want to stop so that we can display the
12932 continuation glyph before the right margin. If lines are
12933 continued, there are two possible strategies for characters
12934 resulting in more than 1 glyph (e.g. tabs): Display as many
12935 glyphs as possible in this line and leave the rest for the
12936 continuation line, or display the whole element in the next
12937 line. Original redisplay did the former, so we do it also. */
12938 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12939 hpos_before = it->hpos;
12940 x_before = x;
12941
12942 if (/* Not a newline. */
12943 nglyphs > 0
12944 /* Glyphs produced fit entirely in the line. */
12945 && it->current_x < it->last_visible_x)
12946 {
12947 it->hpos += nglyphs;
12948 row->ascent = max (row->ascent, it->max_ascent);
12949 row->height = max (row->height, it->max_ascent + it->max_descent);
12950 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12951 row->phys_height = max (row->phys_height,
12952 it->max_phys_ascent + it->max_phys_descent);
12953 if (it->current_x - it->pixel_width < it->first_visible_x)
12954 row->x = x - it->first_visible_x;
12955 }
12956 else
12957 {
12958 int new_x;
12959 struct glyph *glyph;
12960
12961 for (i = 0; i < nglyphs; ++i, x = new_x)
12962 {
12963 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12964 new_x = x + glyph->pixel_width;
12965
12966 if (/* Lines are continued. */
12967 !it->truncate_lines_p
12968 && (/* Glyph doesn't fit on the line. */
12969 new_x > it->last_visible_x
12970 /* Or it fits exactly on a window system frame. */
12971 || (new_x == it->last_visible_x
12972 && FRAME_WINDOW_P (it->f))))
12973 {
12974 /* End of a continued line. */
12975
12976 if (it->hpos == 0
12977 || (new_x == it->last_visible_x
12978 && FRAME_WINDOW_P (it->f)))
12979 {
12980 /* Current glyph is the only one on the line or
12981 fits exactly on the line. We must continue
12982 the line because we can't draw the cursor
12983 after the glyph. */
12984 row->continued_p = 1;
12985 it->current_x = new_x;
12986 it->continuation_lines_width += new_x;
12987 ++it->hpos;
12988 if (i == nglyphs - 1)
12989 set_iterator_to_next (it, 1);
12990 }
12991 else if (CHAR_GLYPH_PADDING_P (*glyph)
12992 && !FRAME_WINDOW_P (it->f))
12993 {
12994 /* A padding glyph that doesn't fit on this line.
12995 This means the whole character doesn't fit
12996 on the line. */
12997 row->used[TEXT_AREA] = n_glyphs_before;
12998
12999 /* Fill the rest of the row with continuation
13000 glyphs like in 20.x. */
13001 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13002 < row->glyphs[1 + TEXT_AREA])
13003 produce_special_glyphs (it, IT_CONTINUATION);
13004
13005 row->continued_p = 1;
13006 it->current_x = x_before;
13007 it->continuation_lines_width += x_before;
13008
13009 /* Restore the height to what it was before the
13010 element not fitting on the line. */
13011 it->max_ascent = ascent;
13012 it->max_descent = descent;
13013 it->max_phys_ascent = phys_ascent;
13014 it->max_phys_descent = phys_descent;
13015 }
13016 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13017 {
13018 /* A TAB that extends past the right edge of the
13019 window. This produces a single glyph on
13020 window system frames. We leave the glyph in
13021 this row and let it fill the row, but don't
13022 consume the TAB. */
13023 it->continuation_lines_width += it->last_visible_x;
13024 row->ends_in_middle_of_char_p = 1;
13025 row->continued_p = 1;
13026 glyph->pixel_width = it->last_visible_x - x;
13027 it->starts_in_middle_of_char_p = 1;
13028 }
13029 else
13030 {
13031 /* Something other than a TAB that draws past
13032 the right edge of the window. Restore
13033 positions to values before the element. */
13034 row->used[TEXT_AREA] = n_glyphs_before + i;
13035
13036 /* Display continuation glyphs. */
13037 if (!FRAME_WINDOW_P (it->f))
13038 produce_special_glyphs (it, IT_CONTINUATION);
13039 row->continued_p = 1;
13040
13041 it->continuation_lines_width += x;
13042
13043 if (nglyphs > 1 && i > 0)
13044 {
13045 row->ends_in_middle_of_char_p = 1;
13046 it->starts_in_middle_of_char_p = 1;
13047 }
13048
13049 /* Restore the height to what it was before the
13050 element not fitting on the line. */
13051 it->max_ascent = ascent;
13052 it->max_descent = descent;
13053 it->max_phys_ascent = phys_ascent;
13054 it->max_phys_descent = phys_descent;
13055 }
13056
13057 break;
13058 }
13059 else if (new_x > it->first_visible_x)
13060 {
13061 /* Increment number of glyphs actually displayed. */
13062 ++it->hpos;
13063
13064 if (x < it->first_visible_x)
13065 /* Glyph is partially visible, i.e. row starts at
13066 negative X position. */
13067 row->x = x - it->first_visible_x;
13068 }
13069 else
13070 {
13071 /* Glyph is completely off the left margin of the
13072 window. This should not happen because of the
13073 move_it_in_display_line at the start of
13074 this function. */
13075 abort ();
13076 }
13077 }
13078
13079 row->ascent = max (row->ascent, it->max_ascent);
13080 row->height = max (row->height, it->max_ascent + it->max_descent);
13081 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13082 row->phys_height = max (row->phys_height,
13083 it->max_phys_ascent + it->max_phys_descent);
13084
13085 /* End of this display line if row is continued. */
13086 if (row->continued_p)
13087 break;
13088 }
13089
13090 /* Is this a line end? If yes, we're also done, after making
13091 sure that a non-default face is extended up to the right
13092 margin of the window. */
13093 if (ITERATOR_AT_END_OF_LINE_P (it))
13094 {
13095 int used_before = row->used[TEXT_AREA];
13096
13097 row->ends_in_newline_from_string_p = STRINGP (it->object);
13098
13099 /* Add a space at the end of the line that is used to
13100 display the cursor there. */
13101 append_space (it, 0);
13102
13103 /* Extend the face to the end of the line. */
13104 extend_face_to_end_of_line (it);
13105
13106 /* Make sure we have the position. */
13107 if (used_before == 0)
13108 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13109
13110 /* Consume the line end. This skips over invisible lines. */
13111 set_iterator_to_next (it, 1);
13112 it->continuation_lines_width = 0;
13113 break;
13114 }
13115
13116 /* Proceed with next display element. Note that this skips
13117 over lines invisible because of selective display. */
13118 set_iterator_to_next (it, 1);
13119
13120 /* If we truncate lines, we are done when the last displayed
13121 glyphs reach past the right margin of the window. */
13122 if (it->truncate_lines_p
13123 && (FRAME_WINDOW_P (it->f)
13124 ? (it->current_x >= it->last_visible_x)
13125 : (it->current_x > it->last_visible_x)))
13126 {
13127 /* Maybe add truncation glyphs. */
13128 if (!FRAME_WINDOW_P (it->f))
13129 {
13130 int i, n;
13131
13132 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13133 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13134 break;
13135
13136 for (n = row->used[TEXT_AREA]; i < n; ++i)
13137 {
13138 row->used[TEXT_AREA] = i;
13139 produce_special_glyphs (it, IT_TRUNCATION);
13140 }
13141 }
13142
13143 row->truncated_on_right_p = 1;
13144 it->continuation_lines_width = 0;
13145 reseat_at_next_visible_line_start (it, 0);
13146 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13147 it->hpos = hpos_before;
13148 it->current_x = x_before;
13149 break;
13150 }
13151 }
13152
13153 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13154 at the left window margin. */
13155 if (it->first_visible_x
13156 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13157 {
13158 if (!FRAME_WINDOW_P (it->f))
13159 insert_left_trunc_glyphs (it);
13160 row->truncated_on_left_p = 1;
13161 }
13162
13163 /* If the start of this line is the overlay arrow-position, then
13164 mark this glyph row as the one containing the overlay arrow.
13165 This is clearly a mess with variable size fonts. It would be
13166 better to let it be displayed like cursors under X. */
13167 if (MARKERP (Voverlay_arrow_position)
13168 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13169 && (MATRIX_ROW_START_CHARPOS (row)
13170 == marker_position (Voverlay_arrow_position))
13171 && STRINGP (Voverlay_arrow_string)
13172 && ! overlay_arrow_seen)
13173 {
13174 /* Overlay arrow in window redisplay is a fringe bitmap. */
13175 if (!FRAME_WINDOW_P (it->f))
13176 {
13177 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13178 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13179 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13180 struct glyph *p = row->glyphs[TEXT_AREA];
13181 struct glyph *p2, *end;
13182
13183 /* Copy the arrow glyphs. */
13184 while (glyph < arrow_end)
13185 *p++ = *glyph++;
13186
13187 /* Throw away padding glyphs. */
13188 p2 = p;
13189 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13190 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13191 ++p2;
13192 if (p2 > p)
13193 {
13194 while (p2 < end)
13195 *p++ = *p2++;
13196 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13197 }
13198 }
13199
13200 overlay_arrow_seen = 1;
13201 row->overlay_arrow_p = 1;
13202 }
13203
13204 /* Compute pixel dimensions of this line. */
13205 compute_line_metrics (it);
13206
13207 /* Remember the position at which this line ends. */
13208 row->end = it->current;
13209
13210 /* Maybe set the cursor. */
13211 if (it->w->cursor.vpos < 0
13212 && PT >= MATRIX_ROW_START_CHARPOS (row)
13213 && PT <= MATRIX_ROW_END_CHARPOS (row)
13214 && cursor_row_p (it->w, row))
13215 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13216
13217 /* Highlight trailing whitespace. */
13218 if (!NILP (Vshow_trailing_whitespace))
13219 highlight_trailing_whitespace (it->f, it->glyph_row);
13220
13221 /* Prepare for the next line. This line starts horizontally at (X
13222 HPOS) = (0 0). Vertical positions are incremented. As a
13223 convenience for the caller, IT->glyph_row is set to the next
13224 row to be used. */
13225 it->current_x = it->hpos = 0;
13226 it->current_y += row->height;
13227 ++it->vpos;
13228 ++it->glyph_row;
13229 return row->displays_text_p;
13230 }
13231
13232
13233 \f
13234 /***********************************************************************
13235 Menu Bar
13236 ***********************************************************************/
13237
13238 /* Redisplay the menu bar in the frame for window W.
13239
13240 The menu bar of X frames that don't have X toolkit support is
13241 displayed in a special window W->frame->menu_bar_window.
13242
13243 The menu bar of terminal frames is treated specially as far as
13244 glyph matrices are concerned. Menu bar lines are not part of
13245 windows, so the update is done directly on the frame matrix rows
13246 for the menu bar. */
13247
13248 static void
13249 display_menu_bar (w)
13250 struct window *w;
13251 {
13252 struct frame *f = XFRAME (WINDOW_FRAME (w));
13253 struct it it;
13254 Lisp_Object items;
13255 int i;
13256
13257 /* Don't do all this for graphical frames. */
13258 #ifdef HAVE_NTGUI
13259 if (!NILP (Vwindow_system))
13260 return;
13261 #endif
13262 #ifdef USE_X_TOOLKIT
13263 if (FRAME_X_P (f))
13264 return;
13265 #endif
13266 #ifdef macintosh
13267 if (FRAME_MAC_P (f))
13268 return;
13269 #endif
13270
13271 #ifdef USE_X_TOOLKIT
13272 xassert (!FRAME_WINDOW_P (f));
13273 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13274 it.first_visible_x = 0;
13275 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13276 #else /* not USE_X_TOOLKIT */
13277 if (FRAME_WINDOW_P (f))
13278 {
13279 /* Menu bar lines are displayed in the desired matrix of the
13280 dummy window menu_bar_window. */
13281 struct window *menu_w;
13282 xassert (WINDOWP (f->menu_bar_window));
13283 menu_w = XWINDOW (f->menu_bar_window);
13284 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13285 MENU_FACE_ID);
13286 it.first_visible_x = 0;
13287 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13288 }
13289 else
13290 {
13291 /* This is a TTY frame, i.e. character hpos/vpos are used as
13292 pixel x/y. */
13293 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13294 MENU_FACE_ID);
13295 it.first_visible_x = 0;
13296 it.last_visible_x = FRAME_WIDTH (f);
13297 }
13298 #endif /* not USE_X_TOOLKIT */
13299
13300 if (! mode_line_inverse_video)
13301 /* Force the menu-bar to be displayed in the default face. */
13302 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13303
13304 /* Clear all rows of the menu bar. */
13305 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13306 {
13307 struct glyph_row *row = it.glyph_row + i;
13308 clear_glyph_row (row);
13309 row->enabled_p = 1;
13310 row->full_width_p = 1;
13311 }
13312
13313 /* Display all items of the menu bar. */
13314 items = FRAME_MENU_BAR_ITEMS (it.f);
13315 for (i = 0; i < XVECTOR (items)->size; i += 4)
13316 {
13317 Lisp_Object string;
13318
13319 /* Stop at nil string. */
13320 string = AREF (items, i + 1);
13321 if (NILP (string))
13322 break;
13323
13324 /* Remember where item was displayed. */
13325 AREF (items, i + 3) = make_number (it.hpos);
13326
13327 /* Display the item, pad with one space. */
13328 if (it.current_x < it.last_visible_x)
13329 display_string (NULL, string, Qnil, 0, 0, &it,
13330 XSTRING (string)->size + 1, 0, 0, -1);
13331 }
13332
13333 /* Fill out the line with spaces. */
13334 if (it.current_x < it.last_visible_x)
13335 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13336
13337 /* Compute the total height of the lines. */
13338 compute_line_metrics (&it);
13339 }
13340
13341
13342 \f
13343 /***********************************************************************
13344 Mode Line
13345 ***********************************************************************/
13346
13347 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13348 FORCE is non-zero, redisplay mode lines unconditionally.
13349 Otherwise, redisplay only mode lines that are garbaged. Value is
13350 the number of windows whose mode lines were redisplayed. */
13351
13352 static int
13353 redisplay_mode_lines (window, force)
13354 Lisp_Object window;
13355 int force;
13356 {
13357 int nwindows = 0;
13358
13359 while (!NILP (window))
13360 {
13361 struct window *w = XWINDOW (window);
13362
13363 if (WINDOWP (w->hchild))
13364 nwindows += redisplay_mode_lines (w->hchild, force);
13365 else if (WINDOWP (w->vchild))
13366 nwindows += redisplay_mode_lines (w->vchild, force);
13367 else if (force
13368 || FRAME_GARBAGED_P (XFRAME (w->frame))
13369 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13370 {
13371 struct text_pos lpoint;
13372 struct buffer *old = current_buffer;
13373
13374 /* Set the window's buffer for the mode line display. */
13375 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13376 set_buffer_internal_1 (XBUFFER (w->buffer));
13377
13378 /* Point refers normally to the selected window. For any
13379 other window, set up appropriate value. */
13380 if (!EQ (window, selected_window))
13381 {
13382 struct text_pos pt;
13383
13384 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13385 if (CHARPOS (pt) < BEGV)
13386 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13387 else if (CHARPOS (pt) > (ZV - 1))
13388 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13389 else
13390 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13391 }
13392
13393 /* Display mode lines. */
13394 clear_glyph_matrix (w->desired_matrix);
13395 if (display_mode_lines (w))
13396 {
13397 ++nwindows;
13398 w->must_be_updated_p = 1;
13399 }
13400
13401 /* Restore old settings. */
13402 set_buffer_internal_1 (old);
13403 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13404 }
13405
13406 window = w->next;
13407 }
13408
13409 return nwindows;
13410 }
13411
13412
13413 /* Display the mode and/or top line of window W. Value is the number
13414 of mode lines displayed. */
13415
13416 static int
13417 display_mode_lines (w)
13418 struct window *w;
13419 {
13420 Lisp_Object old_selected_window, old_selected_frame;
13421 int n = 0;
13422
13423 old_selected_frame = selected_frame;
13424 selected_frame = w->frame;
13425 old_selected_window = selected_window;
13426 XSETWINDOW (selected_window, w);
13427
13428 /* These will be set while the mode line specs are processed. */
13429 line_number_displayed = 0;
13430 w->column_number_displayed = Qnil;
13431
13432 if (WINDOW_WANTS_MODELINE_P (w))
13433 {
13434 display_mode_line (w, MODE_LINE_FACE_ID,
13435 current_buffer->mode_line_format);
13436 ++n;
13437 }
13438
13439 if (WINDOW_WANTS_HEADER_LINE_P (w))
13440 {
13441 display_mode_line (w, HEADER_LINE_FACE_ID,
13442 current_buffer->header_line_format);
13443 ++n;
13444 }
13445
13446 selected_frame = old_selected_frame;
13447 selected_window = old_selected_window;
13448 return n;
13449 }
13450
13451
13452 /* Display mode or top line of window W. FACE_ID specifies which line
13453 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13454 FORMAT is the mode line format to display. Value is the pixel
13455 height of the mode line displayed. */
13456
13457 static int
13458 display_mode_line (w, face_id, format)
13459 struct window *w;
13460 enum face_id face_id;
13461 Lisp_Object format;
13462 {
13463 struct it it;
13464 struct face *face;
13465
13466 init_iterator (&it, w, -1, -1, NULL, face_id);
13467 prepare_desired_row (it.glyph_row);
13468
13469 if (! mode_line_inverse_video)
13470 /* Force the mode-line to be displayed in the default face. */
13471 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13472
13473 /* Temporarily make frame's keyboard the current kboard so that
13474 kboard-local variables in the mode_line_format will get the right
13475 values. */
13476 push_frame_kboard (it.f);
13477 display_mode_element (&it, 0, 0, 0, format);
13478 pop_frame_kboard ();
13479
13480 /* Fill up with spaces. */
13481 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13482
13483 compute_line_metrics (&it);
13484 it.glyph_row->full_width_p = 1;
13485 it.glyph_row->mode_line_p = 1;
13486 it.glyph_row->continued_p = 0;
13487 it.glyph_row->truncated_on_left_p = 0;
13488 it.glyph_row->truncated_on_right_p = 0;
13489
13490 /* Make a 3D mode-line have a shadow at its right end. */
13491 face = FACE_FROM_ID (it.f, face_id);
13492 extend_face_to_end_of_line (&it);
13493 if (face->box != FACE_NO_BOX)
13494 {
13495 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13496 + it.glyph_row->used[TEXT_AREA] - 1);
13497 last->right_box_line_p = 1;
13498 }
13499
13500 return it.glyph_row->height;
13501 }
13502
13503
13504 /* Contribute ELT to the mode line for window IT->w. How it
13505 translates into text depends on its data type.
13506
13507 IT describes the display environment in which we display, as usual.
13508
13509 DEPTH is the depth in recursion. It is used to prevent
13510 infinite recursion here.
13511
13512 FIELD_WIDTH is the number of characters the display of ELT should
13513 occupy in the mode line, and PRECISION is the maximum number of
13514 characters to display from ELT's representation. See
13515 display_string for details.
13516
13517 Returns the hpos of the end of the text generated by ELT. */
13518
13519 static int
13520 display_mode_element (it, depth, field_width, precision, elt)
13521 struct it *it;
13522 int depth;
13523 int field_width, precision;
13524 Lisp_Object elt;
13525 {
13526 int n = 0, field, prec;
13527
13528 tail_recurse:
13529 if (depth > 10)
13530 goto invalid;
13531
13532 depth++;
13533
13534 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13535 {
13536 case Lisp_String:
13537 {
13538 /* A string: output it and check for %-constructs within it. */
13539 unsigned char c;
13540 unsigned char *this = XSTRING (elt)->data;
13541 unsigned char *lisp_string = this;
13542
13543 while ((precision <= 0 || n < precision)
13544 && *this
13545 && (frame_title_ptr
13546 || it->current_x < it->last_visible_x))
13547 {
13548 unsigned char *last = this;
13549
13550 /* Advance to end of string or next format specifier. */
13551 while ((c = *this++) != '\0' && c != '%')
13552 ;
13553
13554 if (this - 1 != last)
13555 {
13556 /* Output to end of string or up to '%'. Field width
13557 is length of string. Don't output more than
13558 PRECISION allows us. */
13559 --this;
13560
13561 prec = chars_in_text (last, this - last);
13562 if (precision > 0 && prec > precision - n)
13563 prec = precision - n;
13564
13565 if (frame_title_ptr)
13566 n += store_frame_title (last, 0, prec);
13567 else
13568 {
13569 int bytepos = last - lisp_string;
13570 int charpos = string_byte_to_char (elt, bytepos);
13571 n += display_string (NULL, elt, Qnil, 0, charpos,
13572 it, 0, prec, 0,
13573 STRING_MULTIBYTE (elt));
13574 }
13575 }
13576 else /* c == '%' */
13577 {
13578 unsigned char *percent_position = this;
13579
13580 /* Get the specified minimum width. Zero means
13581 don't pad. */
13582 field = 0;
13583 while ((c = *this++) >= '0' && c <= '9')
13584 field = field * 10 + c - '0';
13585
13586 /* Don't pad beyond the total padding allowed. */
13587 if (field_width - n > 0 && field > field_width - n)
13588 field = field_width - n;
13589
13590 /* Note that either PRECISION <= 0 or N < PRECISION. */
13591 prec = precision - n;
13592
13593 if (c == 'M')
13594 n += display_mode_element (it, depth, field, prec,
13595 Vglobal_mode_string);
13596 else if (c != 0)
13597 {
13598 int multibyte;
13599 unsigned char *spec
13600 = decode_mode_spec (it->w, c, field, prec, &multibyte);
13601
13602 if (frame_title_ptr)
13603 n += store_frame_title (spec, field, prec);
13604 else
13605 {
13606 int nglyphs_before, bytepos, charpos, nwritten;
13607
13608 nglyphs_before = it->glyph_row->used[TEXT_AREA];
13609 bytepos = percent_position - XSTRING (elt)->data;
13610 charpos = (STRING_MULTIBYTE (elt)
13611 ? string_byte_to_char (elt, bytepos)
13612 : bytepos);
13613 nwritten = display_string (spec, Qnil, elt,
13614 charpos, 0, it,
13615 field, prec, 0,
13616 multibyte);
13617
13618 /* Assign to the glyphs written above the
13619 string where the `%x' came from, position
13620 of the `%'. */
13621 if (nwritten > 0)
13622 {
13623 struct glyph *glyph
13624 = (it->glyph_row->glyphs[TEXT_AREA]
13625 + nglyphs_before);
13626 int i;
13627
13628 for (i = 0; i < nwritten; ++i)
13629 {
13630 glyph[i].object = elt;
13631 glyph[i].charpos = charpos;
13632 }
13633
13634 n += nwritten;
13635 }
13636 }
13637 }
13638 else /* c == 0 */
13639 break;
13640 }
13641 }
13642 }
13643 break;
13644
13645 case Lisp_Symbol:
13646 /* A symbol: process the value of the symbol recursively
13647 as if it appeared here directly. Avoid error if symbol void.
13648 Special case: if value of symbol is a string, output the string
13649 literally. */
13650 {
13651 register Lisp_Object tem;
13652 tem = Fboundp (elt);
13653 if (!NILP (tem))
13654 {
13655 tem = Fsymbol_value (elt);
13656 /* If value is a string, output that string literally:
13657 don't check for % within it. */
13658 if (STRINGP (tem))
13659 {
13660 prec = precision - n;
13661 if (frame_title_ptr)
13662 n += store_frame_title (XSTRING (tem)->data, -1, prec);
13663 else
13664 n += display_string (NULL, tem, Qnil, 0, 0, it,
13665 0, prec, 0, STRING_MULTIBYTE (tem));
13666 }
13667 else if (!EQ (tem, elt))
13668 {
13669 /* Give up right away for nil or t. */
13670 elt = tem;
13671 goto tail_recurse;
13672 }
13673 }
13674 }
13675 break;
13676
13677 case Lisp_Cons:
13678 {
13679 register Lisp_Object car, tem;
13680
13681 /* A cons cell: three distinct cases.
13682 If first element is a string or a cons, process all the elements
13683 and effectively concatenate them.
13684 If first element is a negative number, truncate displaying cdr to
13685 at most that many characters. If positive, pad (with spaces)
13686 to at least that many characters.
13687 If first element is a symbol, process the cadr or caddr recursively
13688 according to whether the symbol's value is non-nil or nil. */
13689 car = XCAR (elt);
13690 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
13691 {
13692 /* An element of the form (:eval FORM) means evaluate FORM
13693 and use the result as mode line elements. */
13694 struct gcpro gcpro1;
13695 Lisp_Object spec;
13696
13697 spec = safe_eval (XCAR (XCDR (elt)));
13698 GCPRO1 (spec);
13699 n += display_mode_element (it, depth, field_width - n,
13700 precision - n, spec);
13701 UNGCPRO;
13702 }
13703 else if (SYMBOLP (car))
13704 {
13705 tem = Fboundp (car);
13706 elt = XCDR (elt);
13707 if (!CONSP (elt))
13708 goto invalid;
13709 /* elt is now the cdr, and we know it is a cons cell.
13710 Use its car if CAR has a non-nil value. */
13711 if (!NILP (tem))
13712 {
13713 tem = Fsymbol_value (car);
13714 if (!NILP (tem))
13715 {
13716 elt = XCAR (elt);
13717 goto tail_recurse;
13718 }
13719 }
13720 /* Symbol's value is nil (or symbol is unbound)
13721 Get the cddr of the original list
13722 and if possible find the caddr and use that. */
13723 elt = XCDR (elt);
13724 if (NILP (elt))
13725 break;
13726 else if (!CONSP (elt))
13727 goto invalid;
13728 elt = XCAR (elt);
13729 goto tail_recurse;
13730 }
13731 else if (INTEGERP (car))
13732 {
13733 register int lim = XINT (car);
13734 elt = XCDR (elt);
13735 if (lim < 0)
13736 {
13737 /* Negative int means reduce maximum width. */
13738 if (precision <= 0)
13739 precision = -lim;
13740 else
13741 precision = min (precision, -lim);
13742 }
13743 else if (lim > 0)
13744 {
13745 /* Padding specified. Don't let it be more than
13746 current maximum. */
13747 if (precision > 0)
13748 lim = min (precision, lim);
13749
13750 /* If that's more padding than already wanted, queue it.
13751 But don't reduce padding already specified even if
13752 that is beyond the current truncation point. */
13753 field_width = max (lim, field_width);
13754 }
13755 goto tail_recurse;
13756 }
13757 else if (STRINGP (car) || CONSP (car))
13758 {
13759 register int limit = 50;
13760 /* Limit is to protect against circular lists. */
13761 while (CONSP (elt)
13762 && --limit > 0
13763 && (precision <= 0 || n < precision))
13764 {
13765 n += display_mode_element (it, depth, field_width - n,
13766 precision - n, XCAR (elt));
13767 elt = XCDR (elt);
13768 }
13769 }
13770 }
13771 break;
13772
13773 default:
13774 invalid:
13775 if (frame_title_ptr)
13776 n += store_frame_title ("*invalid*", 0, precision - n);
13777 else
13778 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13779 precision - n, 0, 0);
13780 return n;
13781 }
13782
13783 /* Pad to FIELD_WIDTH. */
13784 if (field_width > 0 && n < field_width)
13785 {
13786 if (frame_title_ptr)
13787 n += store_frame_title ("", field_width - n, 0);
13788 else
13789 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13790 0, 0, 0);
13791 }
13792
13793 return n;
13794 }
13795
13796
13797 /* Write a null-terminated, right justified decimal representation of
13798 the positive integer D to BUF using a minimal field width WIDTH. */
13799
13800 static void
13801 pint2str (buf, width, d)
13802 register char *buf;
13803 register int width;
13804 register int d;
13805 {
13806 register char *p = buf;
13807
13808 if (d <= 0)
13809 *p++ = '0';
13810 else
13811 {
13812 while (d > 0)
13813 {
13814 *p++ = d % 10 + '0';
13815 d /= 10;
13816 }
13817 }
13818
13819 for (width -= (int) (p - buf); width > 0; --width)
13820 *p++ = ' ';
13821 *p-- = '\0';
13822 while (p > buf)
13823 {
13824 d = *buf;
13825 *buf++ = *p;
13826 *p-- = d;
13827 }
13828 }
13829
13830 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
13831 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13832 type of CODING_SYSTEM. Return updated pointer into BUF. */
13833
13834 static unsigned char invalid_eol_type[] = "(*invalid*)";
13835
13836 static char *
13837 decode_mode_spec_coding (coding_system, buf, eol_flag)
13838 Lisp_Object coding_system;
13839 register char *buf;
13840 int eol_flag;
13841 {
13842 Lisp_Object val;
13843 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
13844 unsigned char *eol_str;
13845 int eol_str_len;
13846 /* The EOL conversion we are using. */
13847 Lisp_Object eoltype;
13848
13849 val = Fget (coding_system, Qcoding_system);
13850 eoltype = Qnil;
13851
13852 if (!VECTORP (val)) /* Not yet decided. */
13853 {
13854 if (multibyte)
13855 *buf++ = '-';
13856 if (eol_flag)
13857 eoltype = eol_mnemonic_undecided;
13858 /* Don't mention EOL conversion if it isn't decided. */
13859 }
13860 else
13861 {
13862 Lisp_Object eolvalue;
13863
13864 eolvalue = Fget (coding_system, Qeol_type);
13865
13866 if (multibyte)
13867 *buf++ = XFASTINT (AREF (val, 1));
13868
13869 if (eol_flag)
13870 {
13871 /* The EOL conversion that is normal on this system. */
13872
13873 if (NILP (eolvalue)) /* Not yet decided. */
13874 eoltype = eol_mnemonic_undecided;
13875 else if (VECTORP (eolvalue)) /* Not yet decided. */
13876 eoltype = eol_mnemonic_undecided;
13877 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
13878 eoltype = (XFASTINT (eolvalue) == 0
13879 ? eol_mnemonic_unix
13880 : (XFASTINT (eolvalue) == 1
13881 ? eol_mnemonic_dos : eol_mnemonic_mac));
13882 }
13883 }
13884
13885 if (eol_flag)
13886 {
13887 /* Mention the EOL conversion if it is not the usual one. */
13888 if (STRINGP (eoltype))
13889 {
13890 eol_str = XSTRING (eoltype)->data;
13891 eol_str_len = XSTRING (eoltype)->size;
13892 }
13893 else if (INTEGERP (eoltype)
13894 && CHAR_VALID_P (XINT (eoltype), 0))
13895 {
13896 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
13897 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
13898 }
13899 else
13900 {
13901 eol_str = invalid_eol_type;
13902 eol_str_len = sizeof (invalid_eol_type) - 1;
13903 }
13904 bcopy (eol_str, buf, eol_str_len);
13905 buf += eol_str_len;
13906 }
13907
13908 return buf;
13909 }
13910
13911 /* Return a string for the output of a mode line %-spec for window W,
13912 generated by character C. PRECISION >= 0 means don't return a
13913 string longer than that value. FIELD_WIDTH > 0 means pad the
13914 string returned with spaces to that value. Return 1 in *MULTIBYTE
13915 if the result is multibyte text. */
13916
13917 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
13918
13919 static char *
13920 decode_mode_spec (w, c, field_width, precision, multibyte)
13921 struct window *w;
13922 register int c;
13923 int field_width, precision;
13924 int *multibyte;
13925 {
13926 Lisp_Object obj;
13927 struct frame *f = XFRAME (WINDOW_FRAME (w));
13928 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
13929 struct buffer *b = XBUFFER (w->buffer);
13930
13931 obj = Qnil;
13932 *multibyte = 0;
13933
13934 switch (c)
13935 {
13936 case '*':
13937 if (!NILP (b->read_only))
13938 return "%";
13939 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13940 return "*";
13941 return "-";
13942
13943 case '+':
13944 /* This differs from %* only for a modified read-only buffer. */
13945 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13946 return "*";
13947 if (!NILP (b->read_only))
13948 return "%";
13949 return "-";
13950
13951 case '&':
13952 /* This differs from %* in ignoring read-only-ness. */
13953 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13954 return "*";
13955 return "-";
13956
13957 case '%':
13958 return "%";
13959
13960 case '[':
13961 {
13962 int i;
13963 char *p;
13964
13965 if (command_loop_level > 5)
13966 return "[[[... ";
13967 p = decode_mode_spec_buf;
13968 for (i = 0; i < command_loop_level; i++)
13969 *p++ = '[';
13970 *p = 0;
13971 return decode_mode_spec_buf;
13972 }
13973
13974 case ']':
13975 {
13976 int i;
13977 char *p;
13978
13979 if (command_loop_level > 5)
13980 return " ...]]]";
13981 p = decode_mode_spec_buf;
13982 for (i = 0; i < command_loop_level; i++)
13983 *p++ = ']';
13984 *p = 0;
13985 return decode_mode_spec_buf;
13986 }
13987
13988 case '-':
13989 {
13990 register int i;
13991
13992 /* Let lots_of_dashes be a string of infinite length. */
13993 if (field_width <= 0
13994 || field_width > sizeof (lots_of_dashes))
13995 {
13996 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
13997 decode_mode_spec_buf[i] = '-';
13998 decode_mode_spec_buf[i] = '\0';
13999 return decode_mode_spec_buf;
14000 }
14001 else
14002 return lots_of_dashes;
14003 }
14004
14005 case 'b':
14006 obj = b->name;
14007 break;
14008
14009 case 'c':
14010 {
14011 int col = current_column ();
14012 w->column_number_displayed = make_number (col);
14013 pint2str (decode_mode_spec_buf, field_width, col);
14014 return decode_mode_spec_buf;
14015 }
14016
14017 case 'F':
14018 /* %F displays the frame name. */
14019 if (!NILP (f->title))
14020 return (char *) XSTRING (f->title)->data;
14021 if (f->explicit_name || ! FRAME_WINDOW_P (f))
14022 return (char *) XSTRING (f->name)->data;
14023 return "Emacs";
14024
14025 case 'f':
14026 obj = b->filename;
14027 break;
14028
14029 case 'l':
14030 {
14031 int startpos = XMARKER (w->start)->charpos;
14032 int startpos_byte = marker_byte_position (w->start);
14033 int line, linepos, linepos_byte, topline;
14034 int nlines, junk;
14035 int height = XFASTINT (w->height);
14036
14037 /* If we decided that this buffer isn't suitable for line numbers,
14038 don't forget that too fast. */
14039 if (EQ (w->base_line_pos, w->buffer))
14040 goto no_value;
14041 /* But do forget it, if the window shows a different buffer now. */
14042 else if (BUFFERP (w->base_line_pos))
14043 w->base_line_pos = Qnil;
14044
14045 /* If the buffer is very big, don't waste time. */
14046 if (INTEGERP (Vline_number_display_limit)
14047 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
14048 {
14049 w->base_line_pos = Qnil;
14050 w->base_line_number = Qnil;
14051 goto no_value;
14052 }
14053
14054 if (!NILP (w->base_line_number)
14055 && !NILP (w->base_line_pos)
14056 && XFASTINT (w->base_line_pos) <= startpos)
14057 {
14058 line = XFASTINT (w->base_line_number);
14059 linepos = XFASTINT (w->base_line_pos);
14060 linepos_byte = buf_charpos_to_bytepos (b, linepos);
14061 }
14062 else
14063 {
14064 line = 1;
14065 linepos = BUF_BEGV (b);
14066 linepos_byte = BUF_BEGV_BYTE (b);
14067 }
14068
14069 /* Count lines from base line to window start position. */
14070 nlines = display_count_lines (linepos, linepos_byte,
14071 startpos_byte,
14072 startpos, &junk);
14073
14074 topline = nlines + line;
14075
14076 /* Determine a new base line, if the old one is too close
14077 or too far away, or if we did not have one.
14078 "Too close" means it's plausible a scroll-down would
14079 go back past it. */
14080 if (startpos == BUF_BEGV (b))
14081 {
14082 w->base_line_number = make_number (topline);
14083 w->base_line_pos = make_number (BUF_BEGV (b));
14084 }
14085 else if (nlines < height + 25 || nlines > height * 3 + 50
14086 || linepos == BUF_BEGV (b))
14087 {
14088 int limit = BUF_BEGV (b);
14089 int limit_byte = BUF_BEGV_BYTE (b);
14090 int position;
14091 int distance = (height * 2 + 30) * line_number_display_limit_width;
14092
14093 if (startpos - distance > limit)
14094 {
14095 limit = startpos - distance;
14096 limit_byte = CHAR_TO_BYTE (limit);
14097 }
14098
14099 nlines = display_count_lines (startpos, startpos_byte,
14100 limit_byte,
14101 - (height * 2 + 30),
14102 &position);
14103 /* If we couldn't find the lines we wanted within
14104 line_number_display_limit_width chars per line,
14105 give up on line numbers for this window. */
14106 if (position == limit_byte && limit == startpos - distance)
14107 {
14108 w->base_line_pos = w->buffer;
14109 w->base_line_number = Qnil;
14110 goto no_value;
14111 }
14112
14113 w->base_line_number = make_number (topline - nlines);
14114 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
14115 }
14116
14117 /* Now count lines from the start pos to point. */
14118 nlines = display_count_lines (startpos, startpos_byte,
14119 PT_BYTE, PT, &junk);
14120
14121 /* Record that we did display the line number. */
14122 line_number_displayed = 1;
14123
14124 /* Make the string to show. */
14125 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
14126 return decode_mode_spec_buf;
14127 no_value:
14128 {
14129 char* p = decode_mode_spec_buf;
14130 int pad = field_width - 2;
14131 while (pad-- > 0)
14132 *p++ = ' ';
14133 *p++ = '?';
14134 *p++ = '?';
14135 *p = '\0';
14136 return decode_mode_spec_buf;
14137 }
14138 }
14139 break;
14140
14141 case 'm':
14142 obj = b->mode_name;
14143 break;
14144
14145 case 'n':
14146 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
14147 return " Narrow";
14148 break;
14149
14150 case 'p':
14151 {
14152 int pos = marker_position (w->start);
14153 int total = BUF_ZV (b) - BUF_BEGV (b);
14154
14155 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
14156 {
14157 if (pos <= BUF_BEGV (b))
14158 return "All";
14159 else
14160 return "Bottom";
14161 }
14162 else if (pos <= BUF_BEGV (b))
14163 return "Top";
14164 else
14165 {
14166 if (total > 1000000)
14167 /* Do it differently for a large value, to avoid overflow. */
14168 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14169 else
14170 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14171 /* We can't normally display a 3-digit number,
14172 so get us a 2-digit number that is close. */
14173 if (total == 100)
14174 total = 99;
14175 sprintf (decode_mode_spec_buf, "%2d%%", total);
14176 return decode_mode_spec_buf;
14177 }
14178 }
14179
14180 /* Display percentage of size above the bottom of the screen. */
14181 case 'P':
14182 {
14183 int toppos = marker_position (w->start);
14184 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14185 int total = BUF_ZV (b) - BUF_BEGV (b);
14186
14187 if (botpos >= BUF_ZV (b))
14188 {
14189 if (toppos <= BUF_BEGV (b))
14190 return "All";
14191 else
14192 return "Bottom";
14193 }
14194 else
14195 {
14196 if (total > 1000000)
14197 /* Do it differently for a large value, to avoid overflow. */
14198 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14199 else
14200 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14201 /* We can't normally display a 3-digit number,
14202 so get us a 2-digit number that is close. */
14203 if (total == 100)
14204 total = 99;
14205 if (toppos <= BUF_BEGV (b))
14206 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14207 else
14208 sprintf (decode_mode_spec_buf, "%2d%%", total);
14209 return decode_mode_spec_buf;
14210 }
14211 }
14212
14213 case 's':
14214 /* status of process */
14215 obj = Fget_buffer_process (w->buffer);
14216 if (NILP (obj))
14217 return "no process";
14218 #ifdef subprocesses
14219 obj = Fsymbol_name (Fprocess_status (obj));
14220 #endif
14221 break;
14222
14223 case 't': /* indicate TEXT or BINARY */
14224 #ifdef MODE_LINE_BINARY_TEXT
14225 return MODE_LINE_BINARY_TEXT (b);
14226 #else
14227 return "T";
14228 #endif
14229
14230 case 'z':
14231 /* coding-system (not including end-of-line format) */
14232 case 'Z':
14233 /* coding-system (including end-of-line type) */
14234 {
14235 int eol_flag = (c == 'Z');
14236 char *p = decode_mode_spec_buf;
14237
14238 if (! FRAME_WINDOW_P (f))
14239 {
14240 /* No need to mention EOL here--the terminal never needs
14241 to do EOL conversion. */
14242 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14243 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
14244 }
14245 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14246 p, eol_flag);
14247
14248 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14249 #ifdef subprocesses
14250 obj = Fget_buffer_process (Fcurrent_buffer ());
14251 if (PROCESSP (obj))
14252 {
14253 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14254 p, eol_flag);
14255 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14256 p, eol_flag);
14257 }
14258 #endif /* subprocesses */
14259 #endif /* 0 */
14260 *p = 0;
14261 return decode_mode_spec_buf;
14262 }
14263 }
14264
14265 if (STRINGP (obj))
14266 {
14267 *multibyte = STRING_MULTIBYTE (obj);
14268 return (char *) XSTRING (obj)->data;
14269 }
14270 else
14271 return "";
14272 }
14273
14274
14275 /* Count up to COUNT lines starting from START / START_BYTE.
14276 But don't go beyond LIMIT_BYTE.
14277 Return the number of lines thus found (always nonnegative).
14278
14279 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14280
14281 static int
14282 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14283 int start, start_byte, limit_byte, count;
14284 int *byte_pos_ptr;
14285 {
14286 register unsigned char *cursor;
14287 unsigned char *base;
14288
14289 register int ceiling;
14290 register unsigned char *ceiling_addr;
14291 int orig_count = count;
14292
14293 /* If we are not in selective display mode,
14294 check only for newlines. */
14295 int selective_display = (!NILP (current_buffer->selective_display)
14296 && !INTEGERP (current_buffer->selective_display));
14297
14298 if (count > 0)
14299 {
14300 while (start_byte < limit_byte)
14301 {
14302 ceiling = BUFFER_CEILING_OF (start_byte);
14303 ceiling = min (limit_byte - 1, ceiling);
14304 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14305 base = (cursor = BYTE_POS_ADDR (start_byte));
14306 while (1)
14307 {
14308 if (selective_display)
14309 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14310 ;
14311 else
14312 while (*cursor != '\n' && ++cursor != ceiling_addr)
14313 ;
14314
14315 if (cursor != ceiling_addr)
14316 {
14317 if (--count == 0)
14318 {
14319 start_byte += cursor - base + 1;
14320 *byte_pos_ptr = start_byte;
14321 return orig_count;
14322 }
14323 else
14324 if (++cursor == ceiling_addr)
14325 break;
14326 }
14327 else
14328 break;
14329 }
14330 start_byte += cursor - base;
14331 }
14332 }
14333 else
14334 {
14335 while (start_byte > limit_byte)
14336 {
14337 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14338 ceiling = max (limit_byte, ceiling);
14339 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14340 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14341 while (1)
14342 {
14343 if (selective_display)
14344 while (--cursor != ceiling_addr
14345 && *cursor != '\n' && *cursor != 015)
14346 ;
14347 else
14348 while (--cursor != ceiling_addr && *cursor != '\n')
14349 ;
14350
14351 if (cursor != ceiling_addr)
14352 {
14353 if (++count == 0)
14354 {
14355 start_byte += cursor - base + 1;
14356 *byte_pos_ptr = start_byte;
14357 /* When scanning backwards, we should
14358 not count the newline posterior to which we stop. */
14359 return - orig_count - 1;
14360 }
14361 }
14362 else
14363 break;
14364 }
14365 /* Here we add 1 to compensate for the last decrement
14366 of CURSOR, which took it past the valid range. */
14367 start_byte += cursor - base + 1;
14368 }
14369 }
14370
14371 *byte_pos_ptr = limit_byte;
14372
14373 if (count < 0)
14374 return - orig_count + count;
14375 return orig_count - count;
14376
14377 }
14378
14379
14380 \f
14381 /***********************************************************************
14382 Displaying strings
14383 ***********************************************************************/
14384
14385 /* Display a NUL-terminated string, starting with index START.
14386
14387 If STRING is non-null, display that C string. Otherwise, the Lisp
14388 string LISP_STRING is displayed.
14389
14390 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14391 FACE_STRING. Display STRING or LISP_STRING with the face at
14392 FACE_STRING_POS in FACE_STRING:
14393
14394 Display the string in the environment given by IT, but use the
14395 standard display table, temporarily.
14396
14397 FIELD_WIDTH is the minimum number of output glyphs to produce.
14398 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14399 with spaces. If STRING has more characters, more than FIELD_WIDTH
14400 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14401
14402 PRECISION is the maximum number of characters to output from
14403 STRING. PRECISION < 0 means don't truncate the string.
14404
14405 This is roughly equivalent to printf format specifiers:
14406
14407 FIELD_WIDTH PRECISION PRINTF
14408 ----------------------------------------
14409 -1 -1 %s
14410 -1 10 %.10s
14411 10 -1 %10s
14412 20 10 %20.10s
14413
14414 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14415 display them, and < 0 means obey the current buffer's value of
14416 enable_multibyte_characters.
14417
14418 Value is the number of glyphs produced. */
14419
14420 static int
14421 display_string (string, lisp_string, face_string, face_string_pos,
14422 start, it, field_width, precision, max_x, multibyte)
14423 unsigned char *string;
14424 Lisp_Object lisp_string;
14425 Lisp_Object face_string;
14426 int face_string_pos;
14427 int start;
14428 struct it *it;
14429 int field_width, precision, max_x;
14430 int multibyte;
14431 {
14432 int hpos_at_start = it->hpos;
14433 int saved_face_id = it->face_id;
14434 struct glyph_row *row = it->glyph_row;
14435
14436 /* Initialize the iterator IT for iteration over STRING beginning
14437 with index START. */
14438 reseat_to_string (it, string, lisp_string, start,
14439 precision, field_width, multibyte);
14440
14441 /* If displaying STRING, set up the face of the iterator
14442 from LISP_STRING, if that's given. */
14443 if (STRINGP (face_string))
14444 {
14445 int endptr;
14446 struct face *face;
14447
14448 it->face_id
14449 = face_at_string_position (it->w, face_string, face_string_pos,
14450 0, it->region_beg_charpos,
14451 it->region_end_charpos,
14452 &endptr, it->base_face_id, 0);
14453 face = FACE_FROM_ID (it->f, it->face_id);
14454 it->face_box_p = face->box != FACE_NO_BOX;
14455 }
14456
14457 /* Set max_x to the maximum allowed X position. Don't let it go
14458 beyond the right edge of the window. */
14459 if (max_x <= 0)
14460 max_x = it->last_visible_x;
14461 else
14462 max_x = min (max_x, it->last_visible_x);
14463
14464 /* Skip over display elements that are not visible. because IT->w is
14465 hscrolled. */
14466 if (it->current_x < it->first_visible_x)
14467 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14468 MOVE_TO_POS | MOVE_TO_X);
14469
14470 row->ascent = it->max_ascent;
14471 row->height = it->max_ascent + it->max_descent;
14472 row->phys_ascent = it->max_phys_ascent;
14473 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14474
14475 /* This condition is for the case that we are called with current_x
14476 past last_visible_x. */
14477 while (it->current_x < max_x)
14478 {
14479 int x_before, x, n_glyphs_before, i, nglyphs;
14480
14481 /* Get the next display element. */
14482 if (!get_next_display_element (it))
14483 break;
14484
14485 /* Produce glyphs. */
14486 x_before = it->current_x;
14487 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14488 PRODUCE_GLYPHS (it);
14489
14490 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14491 i = 0;
14492 x = x_before;
14493 while (i < nglyphs)
14494 {
14495 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14496
14497 if (!it->truncate_lines_p
14498 && x + glyph->pixel_width > max_x)
14499 {
14500 /* End of continued line or max_x reached. */
14501 if (CHAR_GLYPH_PADDING_P (*glyph))
14502 {
14503 /* A wide character is unbreakable. */
14504 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14505 it->current_x = x_before;
14506 }
14507 else
14508 {
14509 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14510 it->current_x = x;
14511 }
14512 break;
14513 }
14514 else if (x + glyph->pixel_width > it->first_visible_x)
14515 {
14516 /* Glyph is at least partially visible. */
14517 ++it->hpos;
14518 if (x < it->first_visible_x)
14519 it->glyph_row->x = x - it->first_visible_x;
14520 }
14521 else
14522 {
14523 /* Glyph is off the left margin of the display area.
14524 Should not happen. */
14525 abort ();
14526 }
14527
14528 row->ascent = max (row->ascent, it->max_ascent);
14529 row->height = max (row->height, it->max_ascent + it->max_descent);
14530 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14531 row->phys_height = max (row->phys_height,
14532 it->max_phys_ascent + it->max_phys_descent);
14533 x += glyph->pixel_width;
14534 ++i;
14535 }
14536
14537 /* Stop if max_x reached. */
14538 if (i < nglyphs)
14539 break;
14540
14541 /* Stop at line ends. */
14542 if (ITERATOR_AT_END_OF_LINE_P (it))
14543 {
14544 it->continuation_lines_width = 0;
14545 break;
14546 }
14547
14548 set_iterator_to_next (it, 1);
14549
14550 /* Stop if truncating at the right edge. */
14551 if (it->truncate_lines_p
14552 && it->current_x >= it->last_visible_x)
14553 {
14554 /* Add truncation mark, but don't do it if the line is
14555 truncated at a padding space. */
14556 if (IT_CHARPOS (*it) < it->string_nchars)
14557 {
14558 if (!FRAME_WINDOW_P (it->f))
14559 {
14560 int i, n;
14561
14562 if (it->current_x > it->last_visible_x)
14563 {
14564 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14565 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14566 break;
14567 for (n = row->used[TEXT_AREA]; i < n; ++i)
14568 {
14569 row->used[TEXT_AREA] = i;
14570 produce_special_glyphs (it, IT_TRUNCATION);
14571 }
14572 }
14573 produce_special_glyphs (it, IT_TRUNCATION);
14574 }
14575 it->glyph_row->truncated_on_right_p = 1;
14576 }
14577 break;
14578 }
14579 }
14580
14581 /* Maybe insert a truncation at the left. */
14582 if (it->first_visible_x
14583 && IT_CHARPOS (*it) > 0)
14584 {
14585 if (!FRAME_WINDOW_P (it->f))
14586 insert_left_trunc_glyphs (it);
14587 it->glyph_row->truncated_on_left_p = 1;
14588 }
14589
14590 it->face_id = saved_face_id;
14591
14592 /* Value is number of columns displayed. */
14593 return it->hpos - hpos_at_start;
14594 }
14595
14596
14597 \f
14598 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
14599 appears as an element of LIST or as the car of an element of LIST.
14600 If PROPVAL is a list, compare each element against LIST in that
14601 way, and return 1/2 if any element of PROPVAL is found in LIST.
14602 Otherwise return 0. This function cannot quit.
14603 The return value is 2 if the text is invisible but with an ellipsis
14604 and 1 if it's invisible and without an ellipsis. */
14605
14606 int
14607 invisible_p (propval, list)
14608 register Lisp_Object propval;
14609 Lisp_Object list;
14610 {
14611 register Lisp_Object tail, proptail;
14612
14613 for (tail = list; CONSP (tail); tail = XCDR (tail))
14614 {
14615 register Lisp_Object tem;
14616 tem = XCAR (tail);
14617 if (EQ (propval, tem))
14618 return 1;
14619 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14620 return NILP (XCDR (tem)) ? 1 : 2;
14621 }
14622
14623 if (CONSP (propval))
14624 {
14625 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14626 {
14627 Lisp_Object propelt;
14628 propelt = XCAR (proptail);
14629 for (tail = list; CONSP (tail); tail = XCDR (tail))
14630 {
14631 register Lisp_Object tem;
14632 tem = XCAR (tail);
14633 if (EQ (propelt, tem))
14634 return 1;
14635 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14636 return NILP (XCDR (tem)) ? 1 : 2;
14637 }
14638 }
14639 }
14640
14641 return 0;
14642 }
14643
14644 \f
14645 /***********************************************************************
14646 Initialization
14647 ***********************************************************************/
14648
14649 void
14650 syms_of_xdisp ()
14651 {
14652 Vwith_echo_area_save_vector = Qnil;
14653 staticpro (&Vwith_echo_area_save_vector);
14654
14655 Vmessage_stack = Qnil;
14656 staticpro (&Vmessage_stack);
14657
14658 Qinhibit_redisplay = intern ("inhibit-redisplay");
14659 staticpro (&Qinhibit_redisplay);
14660
14661 message_dolog_marker1 = Fmake_marker ();
14662 staticpro (&message_dolog_marker1);
14663 message_dolog_marker2 = Fmake_marker ();
14664 staticpro (&message_dolog_marker2);
14665 message_dolog_marker3 = Fmake_marker ();
14666 staticpro (&message_dolog_marker3);
14667
14668 #if GLYPH_DEBUG
14669 defsubr (&Sdump_glyph_matrix);
14670 defsubr (&Sdump_glyph_row);
14671 defsubr (&Sdump_tool_bar_row);
14672 defsubr (&Strace_redisplay);
14673 defsubr (&Strace_to_stderr);
14674 #endif
14675 #ifdef HAVE_WINDOW_SYSTEM
14676 defsubr (&Stool_bar_lines_needed);
14677 #endif
14678
14679 staticpro (&Qmenu_bar_update_hook);
14680 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14681
14682 staticpro (&Qoverriding_terminal_local_map);
14683 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14684
14685 staticpro (&Qoverriding_local_map);
14686 Qoverriding_local_map = intern ("overriding-local-map");
14687
14688 staticpro (&Qwindow_scroll_functions);
14689 Qwindow_scroll_functions = intern ("window-scroll-functions");
14690
14691 staticpro (&Qredisplay_end_trigger_functions);
14692 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14693
14694 staticpro (&Qinhibit_point_motion_hooks);
14695 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14696
14697 QCdata = intern (":data");
14698 staticpro (&QCdata);
14699 Qdisplay = intern ("display");
14700 staticpro (&Qdisplay);
14701 Qspace_width = intern ("space-width");
14702 staticpro (&Qspace_width);
14703 Qraise = intern ("raise");
14704 staticpro (&Qraise);
14705 Qspace = intern ("space");
14706 staticpro (&Qspace);
14707 Qmargin = intern ("margin");
14708 staticpro (&Qmargin);
14709 Qleft_margin = intern ("left-margin");
14710 staticpro (&Qleft_margin);
14711 Qright_margin = intern ("right-margin");
14712 staticpro (&Qright_margin);
14713 Qalign_to = intern ("align-to");
14714 staticpro (&Qalign_to);
14715 QCalign_to = intern (":align-to");
14716 staticpro (&QCalign_to);
14717 Qrelative_width = intern ("relative-width");
14718 staticpro (&Qrelative_width);
14719 QCrelative_width = intern (":relative-width");
14720 staticpro (&QCrelative_width);
14721 QCrelative_height = intern (":relative-height");
14722 staticpro (&QCrelative_height);
14723 QCeval = intern (":eval");
14724 staticpro (&QCeval);
14725 Qwhen = intern ("when");
14726 staticpro (&Qwhen);
14727 QCfile = intern (":file");
14728 staticpro (&QCfile);
14729 Qfontified = intern ("fontified");
14730 staticpro (&Qfontified);
14731 Qfontification_functions = intern ("fontification-functions");
14732 staticpro (&Qfontification_functions);
14733 Qtrailing_whitespace = intern ("trailing-whitespace");
14734 staticpro (&Qtrailing_whitespace);
14735 Qimage = intern ("image");
14736 staticpro (&Qimage);
14737 Qmessage_truncate_lines = intern ("message-truncate-lines");
14738 staticpro (&Qmessage_truncate_lines);
14739 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
14740 staticpro (&Qcursor_in_non_selected_windows);
14741 Qgrow_only = intern ("grow-only");
14742 staticpro (&Qgrow_only);
14743 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14744 staticpro (&Qinhibit_menubar_update);
14745 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14746 staticpro (&Qinhibit_eval_during_redisplay);
14747 Qposition = intern ("position");
14748 staticpro (&Qposition);
14749 Qbuffer_position = intern ("buffer-position");
14750 staticpro (&Qbuffer_position);
14751 Qobject = intern ("object");
14752 staticpro (&Qobject);
14753
14754 last_arrow_position = Qnil;
14755 last_arrow_string = Qnil;
14756 staticpro (&last_arrow_position);
14757 staticpro (&last_arrow_string);
14758
14759 echo_buffer[0] = echo_buffer[1] = Qnil;
14760 staticpro (&echo_buffer[0]);
14761 staticpro (&echo_buffer[1]);
14762
14763 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14764 staticpro (&echo_area_buffer[0]);
14765 staticpro (&echo_area_buffer[1]);
14766
14767 Vmessages_buffer_name = build_string ("*Messages*");
14768 staticpro (&Vmessages_buffer_name);
14769
14770 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14771 doc: /* Non-nil means highlight trailing whitespace.
14772 The face used for trailing whitespace is `trailing-whitespace'. */);
14773 Vshow_trailing_whitespace = Qnil;
14774
14775 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14776 doc: /* Non-nil means don't actually do any redisplay.
14777 This is used for internal purposes. */);
14778 Vinhibit_redisplay = Qnil;
14779
14780 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14781 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
14782 Vglobal_mode_string = Qnil;
14783
14784 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14785 doc: /* Marker for where to display an arrow on top of the buffer text.
14786 This must be the beginning of a line in order to work.
14787 See also `overlay-arrow-string'. */);
14788 Voverlay_arrow_position = Qnil;
14789
14790 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14791 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
14792 Voverlay_arrow_string = Qnil;
14793
14794 DEFVAR_INT ("scroll-step", &scroll_step,
14795 doc: /* *The number of lines to try scrolling a window by when point moves out.
14796 If that fails to bring point back on frame, point is centered instead.
14797 If this is zero, point is always centered after it moves off frame.
14798 If you want scrolling to always be a line at a time, you should set
14799 `scroll-conservatively' to a large value rather than set this to 1. */);
14800
14801 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14802 doc: /* *Scroll up to this many lines, to bring point back on screen.
14803 A value of zero means to scroll the text to center point vertically
14804 in the window. */);
14805 scroll_conservatively = 0;
14806
14807 DEFVAR_INT ("scroll-margin", &scroll_margin,
14808 doc: /* *Number of lines of margin at the top and bottom of a window.
14809 Recenter the window whenever point gets within this many lines
14810 of the top or bottom of the window. */);
14811 scroll_margin = 0;
14812
14813 #if GLYPH_DEBUG
14814 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
14815 #endif
14816
14817 DEFVAR_BOOL ("truncate-partial-width-windows",
14818 &truncate_partial_width_windows,
14819 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
14820 truncate_partial_width_windows = 1;
14821
14822 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14823 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
14824 Any other value means to use the appropriate face, `mode-line',
14825 `header-line', or `menu' respectively.
14826
14827 This variable is deprecated; please change the above faces instead. */);
14828 mode_line_inverse_video = 1;
14829
14830 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14831 doc: /* *Maximum buffer size for which line number should be displayed.
14832 If the buffer is bigger than this, the line number does not appear
14833 in the mode line. A value of nil means no limit. */);
14834 Vline_number_display_limit = Qnil;
14835
14836 DEFVAR_INT ("line-number-display-limit-width",
14837 &line_number_display_limit_width,
14838 doc: /* *Maximum line width (in characters) for line number display.
14839 If the average length of the lines near point is bigger than this, then the
14840 line number may be omitted from the mode line. */);
14841 line_number_display_limit_width = 200;
14842
14843 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14844 doc: /* *Non-nil means highlight region even in nonselected windows. */);
14845 highlight_nonselected_windows = 0;
14846
14847 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14848 doc: /* Non-nil if more than one frame is visible on this display.
14849 Minibuffer-only frames don't count, but iconified frames do.
14850 This variable is not guaranteed to be accurate except while processing
14851 `frame-title-format' and `icon-title-format'. */);
14852
14853 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14854 doc: /* Template for displaying the title bar of visible frames.
14855 \(Assuming the window manager supports this feature.)
14856 This variable has the same structure as `mode-line-format' (which see),
14857 and is used only on frames for which no explicit name has been set
14858 \(see `modify-frame-parameters'). */);
14859 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
14860 doc: /* Template for displaying the title bar of an iconified frame.
14861 \(Assuming the window manager supports this feature.)
14862 This variable has the same structure as `mode-line-format' (which see),
14863 and is used only on frames for which no explicit name has been set
14864 \(see `modify-frame-parameters'). */);
14865 Vicon_title_format
14866 = Vframe_title_format
14867 = Fcons (intern ("multiple-frames"),
14868 Fcons (build_string ("%b"),
14869 Fcons (Fcons (empty_string,
14870 Fcons (intern ("invocation-name"),
14871 Fcons (build_string ("@"),
14872 Fcons (intern ("system-name"),
14873 Qnil)))),
14874 Qnil)));
14875
14876 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
14877 doc: /* Maximum number of lines to keep in the message log buffer.
14878 If nil, disable message logging. If t, log messages but don't truncate
14879 the buffer when it becomes large. */);
14880 Vmessage_log_max = make_number (50);
14881
14882 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
14883 doc: /* Functions called before redisplay, if window sizes have changed.
14884 The value should be a list of functions that take one argument.
14885 Just before redisplay, for each frame, if any of its windows have changed
14886 size since the last redisplay, or have been split or deleted,
14887 all the functions in the list are called, with the frame as argument. */);
14888 Vwindow_size_change_functions = Qnil;
14889
14890 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
14891 doc: /* List of Functions to call before redisplaying a window with scrolling.
14892 Each function is called with two arguments, the window
14893 and its new display-start position. Note that the value of `window-end'
14894 is not valid when these functions are called. */);
14895 Vwindow_scroll_functions = Qnil;
14896
14897 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
14898 doc: /* *Non-nil means automatically resize tool-bars.
14899 This increases a tool-bar's height if not all tool-bar items are visible.
14900 It decreases a tool-bar's height when it would display blank lines
14901 otherwise. */);
14902 auto_resize_tool_bars_p = 1;
14903
14904 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
14905 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
14906 auto_raise_tool_bar_buttons_p = 1;
14907
14908 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
14909 doc: /* *Margin around tool-bar buttons in pixels.
14910 If an integer, use that for both horizontal and vertical margins.
14911 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
14912 HORZ specifying the horizontal margin, and VERT specifying the
14913 vertical margin. */);
14914 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
14915
14916 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
14917 doc: /* *Relief thickness of tool-bar buttons. */);
14918 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
14919
14920 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
14921 doc: /* List of functions to call to fontify regions of text.
14922 Each function is called with one argument POS. Functions must
14923 fontify a region starting at POS in the current buffer, and give
14924 fontified regions the property `fontified'. */);
14925 Vfontification_functions = Qnil;
14926 Fmake_variable_buffer_local (Qfontification_functions);
14927
14928 DEFVAR_BOOL ("unibyte-display-via-language-environment",
14929 &unibyte_display_via_language_environment,
14930 doc: /* *Non-nil means display unibyte text according to language environment.
14931 Specifically this means that unibyte non-ASCII characters
14932 are displayed by converting them to the equivalent multibyte characters
14933 according to the current language environment. As a result, they are
14934 displayed according to the current fontset. */);
14935 unibyte_display_via_language_environment = 0;
14936
14937 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
14938 doc: /* *Maximum height for resizing mini-windows.
14939 If a float, it specifies a fraction of the mini-window frame's height.
14940 If an integer, it specifies a number of lines. */);
14941 Vmax_mini_window_height = make_float (0.25);
14942
14943 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
14944 doc: /* *How to resize mini-windows.
14945 A value of nil means don't automatically resize mini-windows.
14946 A value of t means resize them to fit the text displayed in them.
14947 A value of `grow-only', the default, means let mini-windows grow
14948 only, until their display becomes empty, at which point the windows
14949 go back to their normal size. */);
14950 Vresize_mini_windows = Qgrow_only;
14951
14952 DEFVAR_BOOL ("cursor-in-non-selected-windows",
14953 &cursor_in_non_selected_windows,
14954 doc: /* *Non-nil means display a hollow cursor in non-selected windows.
14955 nil means don't display a cursor there. */);
14956 cursor_in_non_selected_windows = 1;
14957
14958 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
14959 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
14960 automatic_hscrolling_p = 1;
14961
14962 DEFVAR_LISP ("image-types", &Vimage_types,
14963 doc: /* List of supported image types.
14964 Each element of the list is a symbol for a supported image type. */);
14965 Vimage_types = Qnil;
14966
14967 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
14968 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
14969 Bind this around calls to `message' to let it take effect. */);
14970 message_truncate_lines = 0;
14971
14972 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
14973 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
14974 Can be used to update submenus whose contents should vary. */);
14975 Vmenu_bar_update_hook = Qnil;
14976
14977 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
14978 doc: /* Non-nil means don't update menu bars. Internal use only. */);
14979 inhibit_menubar_update = 0;
14980
14981 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
14982 doc: /* Non-nil means don't eval Lisp during redisplay. */);
14983 inhibit_eval_during_redisplay = 0;
14984
14985 #if GLYPH_DEBUG
14986 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
14987 doc: /* Inhibit try_window_id display optimization. */);
14988 inhibit_try_window_id = 0;
14989
14990 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
14991 doc: /* Inhibit try_window_reusing display optimization. */);
14992 inhibit_try_window_reusing = 0;
14993
14994 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
14995 doc: /* Inhibit try_cursor_movement display optimization. */);
14996 inhibit_try_cursor_movement = 0;
14997 #endif /* GLYPH_DEBUG */
14998 }
14999
15000
15001 /* Initialize this module when Emacs starts. */
15002
15003 void
15004 init_xdisp ()
15005 {
15006 Lisp_Object root_window;
15007 struct window *mini_w;
15008
15009 current_header_line_height = current_mode_line_height = -1;
15010
15011 CHARPOS (this_line_start_pos) = 0;
15012
15013 mini_w = XWINDOW (minibuf_window);
15014 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
15015
15016 if (!noninteractive)
15017 {
15018 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
15019 int i;
15020
15021 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
15022 set_window_height (root_window,
15023 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
15024 0);
15025 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
15026 set_window_height (minibuf_window, 1, 0);
15027
15028 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
15029 mini_w->width = make_number (FRAME_WIDTH (f));
15030
15031 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
15032 scratch_glyph_row.glyphs[TEXT_AREA + 1]
15033 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
15034
15035 /* The default ellipsis glyphs `...'. */
15036 for (i = 0; i < 3; ++i)
15037 default_invis_vector[i] = make_number ('.');
15038 }
15039
15040 #ifdef HAVE_WINDOW_SYSTEM
15041 {
15042 /* Allocate the buffer for frame titles. */
15043 int size = 100;
15044 frame_title_buf = (char *) xmalloc (size);
15045 frame_title_buf_end = frame_title_buf + size;
15046 frame_title_ptr = NULL;
15047 }
15048 #endif /* HAVE_WINDOW_SYSTEM */
15049
15050 help_echo_showing_p = 0;
15051 }
15052
15053