(define-charset): New args :min-code and :max-code.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the 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 "character.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191
192 #ifdef HAVE_X_WINDOWS
193 #include "xterm.h"
194 #endif
195 #ifdef WINDOWSNT
196 #include "w32term.h"
197 #endif
198 #ifdef macintosh
199 #include "macterm.h"
200 #endif
201
202 #define INFINITY 10000000
203
204 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
205 extern void set_frame_menubar P_ ((struct frame *f, int, int));
206 extern int pending_menu_activation;
207 #endif
208
209 extern int interrupt_input;
210 extern int command_loop_level;
211
212 extern int minibuffer_auto_raise;
213
214 extern Lisp_Object Qface;
215
216 extern Lisp_Object Voverriding_local_map;
217 extern Lisp_Object Voverriding_local_map_menu_flag;
218 extern Lisp_Object Qmenu_item;
219
220 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
221 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
222 Lisp_Object Qredisplay_end_trigger_functions;
223 Lisp_Object Qinhibit_point_motion_hooks;
224 Lisp_Object QCeval, Qwhen, QCfile, QCdata, QCpropertize;
225 Lisp_Object Qfontified;
226 Lisp_Object Qgrow_only;
227 Lisp_Object Qinhibit_eval_during_redisplay;
228 Lisp_Object Qbuffer_position, Qposition, Qobject;
229
230 /* Functions called to fontify regions of text. */
231
232 Lisp_Object Vfontification_functions;
233 Lisp_Object Qfontification_functions;
234
235 /* Non-zero means draw tool bar buttons raised when the mouse moves
236 over them. */
237
238 int auto_raise_tool_bar_buttons_p;
239
240 /* Margin around tool bar buttons in pixels. */
241
242 Lisp_Object Vtool_bar_button_margin;
243
244 /* Thickness of shadow to draw around tool bar buttons. */
245
246 int tool_bar_button_relief;
247
248 /* Non-zero means automatically resize tool-bars so that all tool-bar
249 items are visible, and no blank lines remain. */
250
251 int auto_resize_tool_bars_p;
252
253 /* Non-nil means don't actually do any redisplay. */
254
255 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
256
257 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
258
259 int inhibit_eval_during_redisplay;
260
261 /* Names of text properties relevant for redisplay. */
262
263 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
264 extern Lisp_Object Qface, Qinvisible, Qwidth;
265
266 /* Symbols used in text property values. */
267
268 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
269 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
270 Lisp_Object Qmargin;
271 extern Lisp_Object Qheight;
272
273 /* Non-nil means highlight trailing whitespace. */
274
275 Lisp_Object Vshow_trailing_whitespace;
276
277 /* Name of the face used to highlight trailing whitespace. */
278
279 Lisp_Object Qtrailing_whitespace;
280
281 /* The symbol `image' which is the car of the lists used to represent
282 images in Lisp. */
283
284 Lisp_Object Qimage;
285
286 /* Non-zero means print newline to stdout before next mini-buffer
287 message. */
288
289 int noninteractive_need_newline;
290
291 /* Non-zero means print newline to message log before next message. */
292
293 static int message_log_need_newline;
294
295 /* Three markers that message_dolog uses.
296 It could allocate them itself, but that causes trouble
297 in handling memory-full errors. */
298 static Lisp_Object message_dolog_marker1;
299 static Lisp_Object message_dolog_marker2;
300 static Lisp_Object message_dolog_marker3;
301 \f
302 /* The buffer position of the first character appearing entirely or
303 partially on the line of the selected window which contains the
304 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
305 redisplay optimization in redisplay_internal. */
306
307 static struct text_pos this_line_start_pos;
308
309 /* Number of characters past the end of the line above, including the
310 terminating newline. */
311
312 static struct text_pos this_line_end_pos;
313
314 /* The vertical positions and the height of this line. */
315
316 static int this_line_vpos;
317 static int this_line_y;
318 static int this_line_pixel_height;
319
320 /* X position at which this display line starts. Usually zero;
321 negative if first character is partially visible. */
322
323 static int this_line_start_x;
324
325 /* Buffer that this_line_.* variables are referring to. */
326
327 static struct buffer *this_line_buffer;
328
329 /* Nonzero means truncate lines in all windows less wide than the
330 frame. */
331
332 int truncate_partial_width_windows;
333
334 /* A flag to control how to display unibyte 8-bit character. */
335
336 int unibyte_display_via_language_environment;
337
338 /* Nonzero means we have more than one non-mini-buffer-only frame.
339 Not guaranteed to be accurate except while parsing
340 frame-title-format. */
341
342 int multiple_frames;
343
344 Lisp_Object Vglobal_mode_string;
345
346 /* Marker for where to display an arrow on top of the buffer text. */
347
348 Lisp_Object Voverlay_arrow_position;
349
350 /* String to display for the arrow. Only used on terminal frames. */
351
352 Lisp_Object Voverlay_arrow_string;
353
354 /* Values of those variables at last redisplay. However, if
355 Voverlay_arrow_position is a marker, last_arrow_position is its
356 numerical position. */
357
358 static Lisp_Object last_arrow_position, last_arrow_string;
359
360 /* Like mode-line-format, but for the title bar on a visible frame. */
361
362 Lisp_Object Vframe_title_format;
363
364 /* Like mode-line-format, but for the title bar on an iconified frame. */
365
366 Lisp_Object Vicon_title_format;
367
368 /* List of functions to call when a window's size changes. These
369 functions get one arg, a frame on which one or more windows' sizes
370 have changed. */
371
372 static Lisp_Object Vwindow_size_change_functions;
373
374 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
375
376 /* Nonzero if overlay arrow has been displayed once in this window. */
377
378 static int overlay_arrow_seen;
379
380 /* Nonzero means highlight the region even in nonselected windows. */
381
382 int highlight_nonselected_windows;
383
384 /* If cursor motion alone moves point off frame, try scrolling this
385 many lines up or down if that will bring it back. */
386
387 static int scroll_step;
388
389 /* Nonzero means scroll just far enough to bring point back on the
390 screen, when appropriate. */
391
392 static int scroll_conservatively;
393
394 /* Recenter the window whenever point gets within this many lines of
395 the top or bottom of the window. This value is translated into a
396 pixel value by multiplying it with CANON_Y_UNIT, which means that
397 there is really a fixed pixel height scroll margin. */
398
399 int scroll_margin;
400
401 /* Number of windows showing the buffer of the selected window (or
402 another buffer with the same base buffer). keyboard.c refers to
403 this. */
404
405 int buffer_shared;
406
407 /* Vector containing glyphs for an ellipsis `...'. */
408
409 static Lisp_Object default_invis_vector[3];
410
411 /* Zero means display the mode-line/header-line/menu-bar in the default face
412 (this slightly odd definition is for compatibility with previous versions
413 of emacs), non-zero means display them using their respective faces.
414
415 This variable is deprecated. */
416
417 int mode_line_inverse_video;
418
419 /* Prompt to display in front of the mini-buffer contents. */
420
421 Lisp_Object minibuf_prompt;
422
423 /* Width of current mini-buffer prompt. Only set after display_line
424 of the line that contains the prompt. */
425
426 int minibuf_prompt_width;
427 int minibuf_prompt_pixel_width;
428
429 /* This is the window where the echo area message was displayed. It
430 is always a mini-buffer window, but it may not be the same window
431 currently active as a mini-buffer. */
432
433 Lisp_Object echo_area_window;
434
435 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
436 pushes the current message and the value of
437 message_enable_multibyte on the stack, the function restore_message
438 pops the stack and displays MESSAGE again. */
439
440 Lisp_Object Vmessage_stack;
441
442 /* Nonzero means multibyte characters were enabled when the echo area
443 message was specified. */
444
445 int message_enable_multibyte;
446
447 /* Nonzero if we should redraw the mode lines on the next redisplay. */
448
449 int update_mode_lines;
450
451 /* Nonzero if window sizes or contents have changed since last
452 redisplay that finished. */
453
454 int windows_or_buffers_changed;
455
456 /* Nonzero after display_mode_line if %l was used and it displayed a
457 line number. */
458
459 int line_number_displayed;
460
461 /* Maximum buffer size for which to display line numbers. */
462
463 Lisp_Object Vline_number_display_limit;
464
465 /* Line width to consider when repositioning for line number display. */
466
467 static int line_number_display_limit_width;
468
469 /* Number of lines to keep in the message log buffer. t means
470 infinite. nil means don't log at all. */
471
472 Lisp_Object Vmessage_log_max;
473
474 /* The name of the *Messages* buffer, a string. */
475
476 static Lisp_Object Vmessages_buffer_name;
477
478 /* Current, index 0, and last displayed echo area message. Either
479 buffers from echo_buffers, or nil to indicate no message. */
480
481 Lisp_Object echo_area_buffer[2];
482
483 /* The buffers referenced from echo_area_buffer. */
484
485 static Lisp_Object echo_buffer[2];
486
487 /* A vector saved used in with_area_buffer to reduce consing. */
488
489 static Lisp_Object Vwith_echo_area_save_vector;
490
491 /* Non-zero means display_echo_area should display the last echo area
492 message again. Set by redisplay_preserve_echo_area. */
493
494 static int display_last_displayed_message_p;
495
496 /* Nonzero if echo area is being used by print; zero if being used by
497 message. */
498
499 int message_buf_print;
500
501 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
502
503 Lisp_Object Qinhibit_menubar_update;
504 int inhibit_menubar_update;
505
506 /* Maximum height for resizing mini-windows. Either a float
507 specifying a fraction of the available height, or an integer
508 specifying a number of lines. */
509
510 Lisp_Object Vmax_mini_window_height;
511
512 /* Non-zero means messages should be displayed with truncated
513 lines instead of being continued. */
514
515 int message_truncate_lines;
516 Lisp_Object Qmessage_truncate_lines;
517
518 /* Set to 1 in clear_message to make redisplay_internal aware
519 of an emptied echo area. */
520
521 static int message_cleared_p;
522
523 /* Non-zero means we want a hollow cursor in windows that are not
524 selected. Zero means there's no cursor in such windows. */
525
526 int cursor_in_non_selected_windows;
527 Lisp_Object Qcursor_in_non_selected_windows;
528
529 /* A scratch glyph row with contents used for generating truncation
530 glyphs. Also used in direct_output_for_insert. */
531
532 #define MAX_SCRATCH_GLYPHS 100
533 struct glyph_row scratch_glyph_row;
534 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
535
536 /* Ascent and height of the last line processed by move_it_to. */
537
538 static int last_max_ascent, last_height;
539
540 /* Non-zero if there's a help-echo in the echo area. */
541
542 int help_echo_showing_p;
543
544 /* If >= 0, computed, exact values of mode-line and header-line height
545 to use in the macros CURRENT_MODE_LINE_HEIGHT and
546 CURRENT_HEADER_LINE_HEIGHT. */
547
548 int current_mode_line_height, current_header_line_height;
549
550 /* The maximum distance to look ahead for text properties. Values
551 that are too small let us call compute_char_face and similar
552 functions too often which is expensive. Values that are too large
553 let us call compute_char_face and alike too often because we
554 might not be interested in text properties that far away. */
555
556 #define TEXT_PROP_DISTANCE_LIMIT 100
557
558 #if GLYPH_DEBUG
559
560 /* Variables to turn off display optimizations from Lisp. */
561
562 int inhibit_try_window_id, inhibit_try_window_reusing;
563 int inhibit_try_cursor_movement;
564
565 /* Non-zero means print traces of redisplay if compiled with
566 GLYPH_DEBUG != 0. */
567
568 int trace_redisplay_p;
569
570 #endif /* GLYPH_DEBUG */
571
572 #ifdef DEBUG_TRACE_MOVE
573 /* Non-zero means trace with TRACE_MOVE to stderr. */
574 int trace_move;
575
576 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
577 #else
578 #define TRACE_MOVE(x) (void) 0
579 #endif
580
581 /* Non-zero means automatically scroll windows horizontally to make
582 point visible. */
583
584 int automatic_hscrolling_p;
585
586 /* How close to the margin can point get before the window is scrolled
587 horizontally. */
588 int automatic_hscroll_margin;
589
590 /* How much to scroll horizontally when point is inside the above margin. */
591 Lisp_Object Vautomatic_hscroll_step;
592
593 /* A list of symbols, one for each supported image type. */
594
595 Lisp_Object Vimage_types;
596
597 /* The variable `resize-mini-windows'. If nil, don't resize
598 mini-windows. If t, always resize them to fit the text they
599 display. If `grow-only', let mini-windows grow only until they
600 become empty. */
601
602 Lisp_Object Vresize_mini_windows;
603
604 /* Buffer being redisplayed -- for redisplay_window_error. */
605
606 struct buffer *displayed_buffer;
607
608 /* Value returned from text property handlers (see below). */
609
610 enum prop_handled
611 {
612 HANDLED_NORMALLY,
613 HANDLED_RECOMPUTE_PROPS,
614 HANDLED_OVERLAY_STRING_CONSUMED,
615 HANDLED_RETURN
616 };
617
618 /* A description of text properties that redisplay is interested
619 in. */
620
621 struct props
622 {
623 /* The name of the property. */
624 Lisp_Object *name;
625
626 /* A unique index for the property. */
627 enum prop_idx idx;
628
629 /* A handler function called to set up iterator IT from the property
630 at IT's current position. Value is used to steer handle_stop. */
631 enum prop_handled (*handler) P_ ((struct it *it));
632 };
633
634 static enum prop_handled handle_face_prop P_ ((struct it *));
635 static enum prop_handled handle_invisible_prop P_ ((struct it *));
636 static enum prop_handled handle_display_prop P_ ((struct it *));
637 static enum prop_handled handle_composition_prop P_ ((struct it *));
638 static enum prop_handled handle_overlay_change P_ ((struct it *));
639 static enum prop_handled handle_fontified_prop P_ ((struct it *));
640
641 /* Properties handled by iterators. */
642
643 static struct props it_props[] =
644 {
645 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
646 /* Handle `face' before `display' because some sub-properties of
647 `display' need to know the face. */
648 {&Qface, FACE_PROP_IDX, handle_face_prop},
649 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
650 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
651 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
652 {NULL, 0, NULL}
653 };
654
655 /* Value is the position described by X. If X is a marker, value is
656 the marker_position of X. Otherwise, value is X. */
657
658 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
659
660 /* Enumeration returned by some move_it_.* functions internally. */
661
662 enum move_it_result
663 {
664 /* Not used. Undefined value. */
665 MOVE_UNDEFINED,
666
667 /* Move ended at the requested buffer position or ZV. */
668 MOVE_POS_MATCH_OR_ZV,
669
670 /* Move ended at the requested X pixel position. */
671 MOVE_X_REACHED,
672
673 /* Move within a line ended at the end of a line that must be
674 continued. */
675 MOVE_LINE_CONTINUED,
676
677 /* Move within a line ended at the end of a line that would
678 be displayed truncated. */
679 MOVE_LINE_TRUNCATED,
680
681 /* Move within a line ended at a line end. */
682 MOVE_NEWLINE_OR_CR
683 };
684
685
686 \f
687 /* Function prototypes. */
688
689 static void setup_for_ellipsis P_ ((struct it *));
690 static void mark_window_display_accurate_1 P_ ((struct window *, int));
691 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
692 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
693 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
694 static int redisplay_mode_lines P_ ((Lisp_Object, int));
695 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
696
697 #if 0
698 static int invisible_text_between_p P_ ((struct it *, int, int));
699 #endif
700
701 static int next_element_from_ellipsis P_ ((struct it *));
702 static void pint2str P_ ((char *, int, int));
703 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
704 struct text_pos));
705 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
706 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
707 static void store_frame_title_char P_ ((char));
708 static int store_frame_title P_ ((unsigned char *, int, int));
709 static void x_consider_frame_title P_ ((Lisp_Object));
710 static void handle_stop P_ ((struct it *));
711 static int tool_bar_lines_needed P_ ((struct frame *));
712 static int single_display_prop_intangible_p P_ ((Lisp_Object));
713 static void ensure_echo_area_buffers P_ ((void));
714 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
715 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
716 static int with_echo_area_buffer P_ ((struct window *, int,
717 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
718 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
719 static void clear_garbaged_frames P_ ((void));
720 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
721 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
722 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
723 static int display_echo_area P_ ((struct window *));
724 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
725 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
726 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
727 static int string_char_and_length P_ ((unsigned char *, int, int *));
728 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
729 struct text_pos));
730 static int compute_window_start_on_continuation_line P_ ((struct window *));
731 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
732 static void insert_left_trunc_glyphs P_ ((struct it *));
733 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
734 static void extend_face_to_end_of_line P_ ((struct it *));
735 static int append_space P_ ((struct it *, int));
736 static int make_cursor_line_fully_visible P_ ((struct window *));
737 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
738 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
739 static int trailing_whitespace_p P_ ((int));
740 static int message_log_check_duplicate P_ ((int, int, int, int));
741 static void push_it P_ ((struct it *));
742 static void pop_it P_ ((struct it *));
743 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
744 static void redisplay_internal P_ ((int));
745 static int echo_area_display P_ ((int));
746 static void redisplay_windows P_ ((Lisp_Object));
747 static void redisplay_window P_ ((Lisp_Object, int));
748 static Lisp_Object redisplay_window_error ();
749 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
750 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
751 static void update_menu_bar P_ ((struct frame *, int));
752 static int try_window_reusing_current_matrix P_ ((struct window *));
753 static int try_window_id P_ ((struct window *));
754 static int display_line P_ ((struct it *));
755 static int display_mode_lines P_ ((struct window *));
756 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
757 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object));
758 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
759 static void display_menu_bar P_ ((struct window *));
760 static int display_count_lines P_ ((int, int, int, int, int *));
761 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
762 int, int, struct it *, int, int, int, int));
763 static void compute_line_metrics P_ ((struct it *));
764 static void run_redisplay_end_trigger_hook P_ ((struct it *));
765 static int get_overlay_strings P_ ((struct it *, int));
766 static void next_overlay_string P_ ((struct it *));
767 static void reseat P_ ((struct it *, struct text_pos, int));
768 static void reseat_1 P_ ((struct it *, struct text_pos, int));
769 static void back_to_previous_visible_line_start P_ ((struct it *));
770 static void reseat_at_previous_visible_line_start P_ ((struct it *));
771 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
772 static int next_element_from_display_vector P_ ((struct it *));
773 static int next_element_from_string P_ ((struct it *));
774 static int next_element_from_c_string P_ ((struct it *));
775 static int next_element_from_buffer P_ ((struct it *));
776 static int next_element_from_composition P_ ((struct it *));
777 static int next_element_from_image P_ ((struct it *));
778 static int next_element_from_stretch P_ ((struct it *));
779 static void load_overlay_strings P_ ((struct it *, int));
780 static int init_from_display_pos P_ ((struct it *, struct window *,
781 struct display_pos *));
782 static void reseat_to_string P_ ((struct it *, unsigned char *,
783 Lisp_Object, int, int, int, int));
784 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
785 int, int, int));
786 void move_it_vertically_backward P_ ((struct it *, int));
787 static void init_to_row_start P_ ((struct it *, struct window *,
788 struct glyph_row *));
789 static int init_to_row_end P_ ((struct it *, struct window *,
790 struct glyph_row *));
791 static void back_to_previous_line_start P_ ((struct it *));
792 static int forward_to_next_line_start P_ ((struct it *, int *));
793 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
794 Lisp_Object, int));
795 static struct text_pos string_pos P_ ((int, Lisp_Object));
796 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
797 static int number_of_chars P_ ((unsigned char *, int));
798 static void compute_stop_pos P_ ((struct it *));
799 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
800 Lisp_Object));
801 static int face_before_or_after_it_pos P_ ((struct it *, int));
802 static int next_overlay_change P_ ((int));
803 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
804 Lisp_Object, struct text_pos *,
805 int));
806 static int underlying_face_id P_ ((struct it *));
807 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
808 struct window *));
809
810 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
811 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
812
813 #ifdef HAVE_WINDOW_SYSTEM
814
815 static void update_tool_bar P_ ((struct frame *, int));
816 static void build_desired_tool_bar_string P_ ((struct frame *f));
817 static int redisplay_tool_bar P_ ((struct frame *));
818 static void display_tool_bar_line P_ ((struct it *));
819
820 #endif /* HAVE_WINDOW_SYSTEM */
821
822 \f
823 /***********************************************************************
824 Window display dimensions
825 ***********************************************************************/
826
827 /* Return the window-relative maximum y + 1 for glyph rows displaying
828 text in window W. This is the height of W minus the height of a
829 mode line, if any. */
830
831 INLINE int
832 window_text_bottom_y (w)
833 struct window *w;
834 {
835 struct frame *f = XFRAME (w->frame);
836 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
837
838 if (WINDOW_WANTS_MODELINE_P (w))
839 height -= CURRENT_MODE_LINE_HEIGHT (w);
840 return height;
841 }
842
843
844 /* Return the pixel width of display area AREA of window W. AREA < 0
845 means return the total width of W, not including fringes to
846 the left and right of the window. */
847
848 INLINE int
849 window_box_width (w, area)
850 struct window *w;
851 int area;
852 {
853 struct frame *f = XFRAME (w->frame);
854 int width = XFASTINT (w->width);
855
856 if (!w->pseudo_window_p)
857 {
858 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
859
860 if (area == TEXT_AREA)
861 {
862 if (INTEGERP (w->left_margin_width))
863 width -= XFASTINT (w->left_margin_width);
864 if (INTEGERP (w->right_margin_width))
865 width -= XFASTINT (w->right_margin_width);
866 }
867 else if (area == LEFT_MARGIN_AREA)
868 width = (INTEGERP (w->left_margin_width)
869 ? XFASTINT (w->left_margin_width) : 0);
870 else if (area == RIGHT_MARGIN_AREA)
871 width = (INTEGERP (w->right_margin_width)
872 ? XFASTINT (w->right_margin_width) : 0);
873 }
874
875 return width * CANON_X_UNIT (f);
876 }
877
878
879 /* Return the pixel height of the display area of window W, not
880 including mode lines of W, if any. */
881
882 INLINE int
883 window_box_height (w)
884 struct window *w;
885 {
886 struct frame *f = XFRAME (w->frame);
887 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
888
889 xassert (height >= 0);
890
891 /* Note: the code below that determines the mode-line/header-line
892 height is essentially the same as that contained in the macro
893 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
894 the appropriate glyph row has its `mode_line_p' flag set,
895 and if it doesn't, uses estimate_mode_line_height instead. */
896
897 if (WINDOW_WANTS_MODELINE_P (w))
898 {
899 struct glyph_row *ml_row
900 = (w->current_matrix && w->current_matrix->rows
901 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
902 : 0);
903 if (ml_row && ml_row->mode_line_p)
904 height -= ml_row->height;
905 else
906 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
907 }
908
909 if (WINDOW_WANTS_HEADER_LINE_P (w))
910 {
911 struct glyph_row *hl_row
912 = (w->current_matrix && w->current_matrix->rows
913 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
914 : 0);
915 if (hl_row && hl_row->mode_line_p)
916 height -= hl_row->height;
917 else
918 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
919 }
920
921 /* With a very small font and a mode-line that's taller than
922 default, we might end up with a negative height. */
923 return max (0, height);
924 }
925
926
927 /* Return the frame-relative coordinate of the left edge of display
928 area AREA of window W. AREA < 0 means return the left edge of the
929 whole window, to the right of the left fringe of W. */
930
931 INLINE int
932 window_box_left (w, area)
933 struct window *w;
934 int area;
935 {
936 struct frame *f = XFRAME (w->frame);
937 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
938
939 if (!w->pseudo_window_p)
940 {
941 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
942 + FRAME_LEFT_FRINGE_WIDTH (f));
943
944 if (area == TEXT_AREA)
945 x += window_box_width (w, LEFT_MARGIN_AREA);
946 else if (area == RIGHT_MARGIN_AREA)
947 x += (window_box_width (w, LEFT_MARGIN_AREA)
948 + window_box_width (w, TEXT_AREA));
949 }
950
951 return x;
952 }
953
954
955 /* Return the frame-relative coordinate of the right edge of display
956 area AREA of window W. AREA < 0 means return the left edge of the
957 whole window, to the left of the right fringe of W. */
958
959 INLINE int
960 window_box_right (w, area)
961 struct window *w;
962 int area;
963 {
964 return window_box_left (w, area) + window_box_width (w, area);
965 }
966
967
968 /* Get the bounding box of the display area AREA of window W, without
969 mode lines, in frame-relative coordinates. AREA < 0 means the
970 whole window, not including the left and right fringes of
971 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
972 coordinates of the upper-left corner of the box. Return in
973 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
974
975 INLINE void
976 window_box (w, area, box_x, box_y, box_width, box_height)
977 struct window *w;
978 int area;
979 int *box_x, *box_y, *box_width, *box_height;
980 {
981 struct frame *f = XFRAME (w->frame);
982
983 *box_width = window_box_width (w, area);
984 *box_height = window_box_height (w);
985 *box_x = window_box_left (w, area);
986 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
987 + XFASTINT (w->top) * CANON_Y_UNIT (f));
988 if (WINDOW_WANTS_HEADER_LINE_P (w))
989 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
990 }
991
992
993 /* Get the bounding box of the display area AREA of window W, without
994 mode lines. AREA < 0 means the whole window, not including the
995 left and right fringe of the window. Return in *TOP_LEFT_X
996 and TOP_LEFT_Y the frame-relative pixel coordinates of the
997 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
998 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
999 box. */
1000
1001 INLINE void
1002 window_box_edges (w, area, top_left_x, top_left_y,
1003 bottom_right_x, bottom_right_y)
1004 struct window *w;
1005 int area;
1006 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1007 {
1008 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1009 bottom_right_y);
1010 *bottom_right_x += *top_left_x;
1011 *bottom_right_y += *top_left_y;
1012 }
1013
1014
1015 \f
1016 /***********************************************************************
1017 Utilities
1018 ***********************************************************************/
1019
1020 /* Return the bottom y-position of the line the iterator IT is in.
1021 This can modify IT's settings. */
1022
1023 int
1024 line_bottom_y (it)
1025 struct it *it;
1026 {
1027 int line_height = it->max_ascent + it->max_descent;
1028 int line_top_y = it->current_y;
1029
1030 if (line_height == 0)
1031 {
1032 if (last_height)
1033 line_height = last_height;
1034 else if (IT_CHARPOS (*it) < ZV)
1035 {
1036 move_it_by_lines (it, 1, 1);
1037 line_height = (it->max_ascent || it->max_descent
1038 ? it->max_ascent + it->max_descent
1039 : last_height);
1040 }
1041 else
1042 {
1043 struct glyph_row *row = it->glyph_row;
1044
1045 /* Use the default character height. */
1046 it->glyph_row = NULL;
1047 it->what = IT_CHARACTER;
1048 it->c = ' ';
1049 it->len = 1;
1050 PRODUCE_GLYPHS (it);
1051 line_height = it->ascent + it->descent;
1052 it->glyph_row = row;
1053 }
1054 }
1055
1056 return line_top_y + line_height;
1057 }
1058
1059
1060 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1061 1 if POS is visible and the line containing POS is fully visible.
1062 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1063 and header-lines heights. */
1064
1065 int
1066 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1067 struct window *w;
1068 int charpos, *fully, exact_mode_line_heights_p;
1069 {
1070 struct it it;
1071 struct text_pos top;
1072 int visible_p;
1073 struct buffer *old_buffer = NULL;
1074
1075 if (XBUFFER (w->buffer) != current_buffer)
1076 {
1077 old_buffer = current_buffer;
1078 set_buffer_internal_1 (XBUFFER (w->buffer));
1079 }
1080
1081 *fully = visible_p = 0;
1082 SET_TEXT_POS_FROM_MARKER (top, w->start);
1083
1084 /* Compute exact mode line heights, if requested. */
1085 if (exact_mode_line_heights_p)
1086 {
1087 if (WINDOW_WANTS_MODELINE_P (w))
1088 current_mode_line_height
1089 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1090 current_buffer->mode_line_format);
1091
1092 if (WINDOW_WANTS_HEADER_LINE_P (w))
1093 current_header_line_height
1094 = display_mode_line (w, HEADER_LINE_FACE_ID,
1095 current_buffer->header_line_format);
1096 }
1097
1098 start_display (&it, w, top);
1099 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1100 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1101
1102 /* Note that we may overshoot because of invisible text. */
1103 if (IT_CHARPOS (it) >= charpos)
1104 {
1105 int top_y = it.current_y;
1106 int bottom_y = line_bottom_y (&it);
1107 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1108
1109 if (top_y < window_top_y)
1110 visible_p = bottom_y > window_top_y;
1111 else if (top_y < it.last_visible_y)
1112 {
1113 visible_p = 1;
1114 *fully = bottom_y <= it.last_visible_y;
1115 }
1116 }
1117 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1118 {
1119 move_it_by_lines (&it, 1, 0);
1120 if (charpos < IT_CHARPOS (it))
1121 {
1122 visible_p = 1;
1123 *fully = 0;
1124 }
1125 }
1126
1127 if (old_buffer)
1128 set_buffer_internal_1 (old_buffer);
1129
1130 current_header_line_height = current_mode_line_height = -1;
1131 return visible_p;
1132 }
1133
1134
1135 /* Return the next character from STR which is MAXLEN bytes long.
1136 Return in *LEN the length of the character. This is like
1137 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1138 we find one, we return a `?', but with the length of the invalid
1139 character. */
1140
1141 static INLINE int
1142 string_char_and_length (str, maxlen, len)
1143 unsigned char *str;
1144 int maxlen, *len;
1145 {
1146 int c;
1147
1148 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1149 if (!CHAR_VALID_P (c, 1))
1150 /* We may not change the length here because other places in Emacs
1151 don't use this function, i.e. they silently accept invalid
1152 characters. */
1153 c = '?';
1154
1155 return c;
1156 }
1157
1158
1159
1160 /* Given a position POS containing a valid character and byte position
1161 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1162
1163 static struct text_pos
1164 string_pos_nchars_ahead (pos, string, nchars)
1165 struct text_pos pos;
1166 Lisp_Object string;
1167 int nchars;
1168 {
1169 xassert (STRINGP (string) && nchars >= 0);
1170
1171 if (STRING_MULTIBYTE (string))
1172 {
1173 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1174 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1175 int len;
1176
1177 while (nchars--)
1178 {
1179 string_char_and_length (p, rest, &len);
1180 p += len, rest -= len;
1181 xassert (rest >= 0);
1182 CHARPOS (pos) += 1;
1183 BYTEPOS (pos) += len;
1184 }
1185 }
1186 else
1187 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1188
1189 return pos;
1190 }
1191
1192
1193 /* Value is the text position, i.e. character and byte position,
1194 for character position CHARPOS in STRING. */
1195
1196 static INLINE struct text_pos
1197 string_pos (charpos, string)
1198 int charpos;
1199 Lisp_Object string;
1200 {
1201 struct text_pos pos;
1202 xassert (STRINGP (string));
1203 xassert (charpos >= 0);
1204 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1205 return pos;
1206 }
1207
1208
1209 /* Value is a text position, i.e. character and byte position, for
1210 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1211 means recognize multibyte characters. */
1212
1213 static struct text_pos
1214 c_string_pos (charpos, s, multibyte_p)
1215 int charpos;
1216 unsigned char *s;
1217 int multibyte_p;
1218 {
1219 struct text_pos pos;
1220
1221 xassert (s != NULL);
1222 xassert (charpos >= 0);
1223
1224 if (multibyte_p)
1225 {
1226 int rest = strlen (s), len;
1227
1228 SET_TEXT_POS (pos, 0, 0);
1229 while (charpos--)
1230 {
1231 string_char_and_length (s, rest, &len);
1232 s += len, rest -= len;
1233 xassert (rest >= 0);
1234 CHARPOS (pos) += 1;
1235 BYTEPOS (pos) += len;
1236 }
1237 }
1238 else
1239 SET_TEXT_POS (pos, charpos, charpos);
1240
1241 return pos;
1242 }
1243
1244
1245 /* Value is the number of characters in C string S. MULTIBYTE_P
1246 non-zero means recognize multibyte characters. */
1247
1248 static int
1249 number_of_chars (s, multibyte_p)
1250 unsigned char *s;
1251 int multibyte_p;
1252 {
1253 int nchars;
1254
1255 if (multibyte_p)
1256 {
1257 int rest = strlen (s), len;
1258 unsigned char *p = (unsigned char *) s;
1259
1260 for (nchars = 0; rest > 0; ++nchars)
1261 {
1262 string_char_and_length (p, rest, &len);
1263 rest -= len, p += len;
1264 }
1265 }
1266 else
1267 nchars = strlen (s);
1268
1269 return nchars;
1270 }
1271
1272
1273 /* Compute byte position NEWPOS->bytepos corresponding to
1274 NEWPOS->charpos. POS is a known position in string STRING.
1275 NEWPOS->charpos must be >= POS.charpos. */
1276
1277 static void
1278 compute_string_pos (newpos, pos, string)
1279 struct text_pos *newpos, pos;
1280 Lisp_Object string;
1281 {
1282 xassert (STRINGP (string));
1283 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1284
1285 if (STRING_MULTIBYTE (string))
1286 *newpos = string_pos_nchars_ahead (pos, string,
1287 CHARPOS (*newpos) - CHARPOS (pos));
1288 else
1289 BYTEPOS (*newpos) = CHARPOS (*newpos);
1290 }
1291
1292
1293 \f
1294 /***********************************************************************
1295 Lisp form evaluation
1296 ***********************************************************************/
1297
1298 /* Error handler for safe_eval and safe_call. */
1299
1300 static Lisp_Object
1301 safe_eval_handler (arg)
1302 Lisp_Object arg;
1303 {
1304 add_to_log ("Error during redisplay: %s", arg, Qnil);
1305 return Qnil;
1306 }
1307
1308
1309 /* Evaluate SEXPR and return the result, or nil if something went
1310 wrong. Prevent redisplay during the evaluation. */
1311
1312 Lisp_Object
1313 safe_eval (sexpr)
1314 Lisp_Object sexpr;
1315 {
1316 Lisp_Object val;
1317
1318 if (inhibit_eval_during_redisplay)
1319 val = Qnil;
1320 else
1321 {
1322 int count = BINDING_STACK_SIZE ();
1323 struct gcpro gcpro1;
1324
1325 GCPRO1 (sexpr);
1326 specbind (Qinhibit_redisplay, Qt);
1327 val = internal_condition_case_1 (Feval, sexpr, Qerror,
1328 safe_eval_handler);
1329 UNGCPRO;
1330 val = unbind_to (count, val);
1331 }
1332
1333 return val;
1334 }
1335
1336
1337 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1338 Return the result, or nil if something went wrong. Prevent
1339 redisplay during the evaluation. */
1340
1341 Lisp_Object
1342 safe_call (nargs, args)
1343 int nargs;
1344 Lisp_Object *args;
1345 {
1346 Lisp_Object val;
1347
1348 if (inhibit_eval_during_redisplay)
1349 val = Qnil;
1350 else
1351 {
1352 int count = BINDING_STACK_SIZE ();
1353 struct gcpro gcpro1;
1354
1355 GCPRO1 (args[0]);
1356 gcpro1.nvars = nargs;
1357 specbind (Qinhibit_redisplay, Qt);
1358 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1359 safe_eval_handler);
1360 UNGCPRO;
1361 val = unbind_to (count, val);
1362 }
1363
1364 return val;
1365 }
1366
1367
1368 /* Call function FN with one argument ARG.
1369 Return the result, or nil if something went wrong. */
1370
1371 Lisp_Object
1372 safe_call1 (fn, arg)
1373 Lisp_Object fn, arg;
1374 {
1375 Lisp_Object args[2];
1376 args[0] = fn;
1377 args[1] = arg;
1378 return safe_call (2, args);
1379 }
1380
1381
1382 \f
1383 /***********************************************************************
1384 Debugging
1385 ***********************************************************************/
1386
1387 #if 0
1388
1389 /* Define CHECK_IT to perform sanity checks on iterators.
1390 This is for debugging. It is too slow to do unconditionally. */
1391
1392 static void
1393 check_it (it)
1394 struct it *it;
1395 {
1396 if (it->method == next_element_from_string)
1397 {
1398 xassert (STRINGP (it->string));
1399 xassert (IT_STRING_CHARPOS (*it) >= 0);
1400 }
1401 else if (it->method == next_element_from_buffer)
1402 {
1403 /* Check that character and byte positions agree. */
1404 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1405 }
1406
1407 if (it->dpvec)
1408 xassert (it->current.dpvec_index >= 0);
1409 else
1410 xassert (it->current.dpvec_index < 0);
1411 }
1412
1413 #define CHECK_IT(IT) check_it ((IT))
1414
1415 #else /* not 0 */
1416
1417 #define CHECK_IT(IT) (void) 0
1418
1419 #endif /* not 0 */
1420
1421
1422 #if GLYPH_DEBUG
1423
1424 /* Check that the window end of window W is what we expect it
1425 to be---the last row in the current matrix displaying text. */
1426
1427 static void
1428 check_window_end (w)
1429 struct window *w;
1430 {
1431 if (!MINI_WINDOW_P (w)
1432 && !NILP (w->window_end_valid))
1433 {
1434 struct glyph_row *row;
1435 xassert ((row = MATRIX_ROW (w->current_matrix,
1436 XFASTINT (w->window_end_vpos)),
1437 !row->enabled_p
1438 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1439 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1440 }
1441 }
1442
1443 #define CHECK_WINDOW_END(W) check_window_end ((W))
1444
1445 #else /* not GLYPH_DEBUG */
1446
1447 #define CHECK_WINDOW_END(W) (void) 0
1448
1449 #endif /* not GLYPH_DEBUG */
1450
1451
1452 \f
1453 /***********************************************************************
1454 Iterator initialization
1455 ***********************************************************************/
1456
1457 /* Initialize IT for displaying current_buffer in window W, starting
1458 at character position CHARPOS. CHARPOS < 0 means that no buffer
1459 position is specified which is useful when the iterator is assigned
1460 a position later. BYTEPOS is the byte position corresponding to
1461 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1462
1463 If ROW is not null, calls to produce_glyphs with IT as parameter
1464 will produce glyphs in that row.
1465
1466 BASE_FACE_ID is the id of a base face to use. It must be one of
1467 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1468 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1469 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1470
1471 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1472 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1473 will be initialized to use the corresponding mode line glyph row of
1474 the desired matrix of W. */
1475
1476 void
1477 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1478 struct it *it;
1479 struct window *w;
1480 int charpos, bytepos;
1481 struct glyph_row *row;
1482 enum face_id base_face_id;
1483 {
1484 int highlight_region_p;
1485
1486 /* Some precondition checks. */
1487 xassert (w != NULL && it != NULL);
1488 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1489 && charpos <= ZV));
1490
1491 /* If face attributes have been changed since the last redisplay,
1492 free realized faces now because they depend on face definitions
1493 that might have changed. */
1494 if (face_change_count)
1495 {
1496 face_change_count = 0;
1497 free_all_realized_faces (Qnil);
1498 }
1499
1500 /* Use one of the mode line rows of W's desired matrix if
1501 appropriate. */
1502 if (row == NULL)
1503 {
1504 if (base_face_id == MODE_LINE_FACE_ID
1505 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1506 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1507 else if (base_face_id == HEADER_LINE_FACE_ID)
1508 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1509 }
1510
1511 /* Clear IT. */
1512 bzero (it, sizeof *it);
1513 it->current.overlay_string_index = -1;
1514 it->current.dpvec_index = -1;
1515 it->base_face_id = base_face_id;
1516
1517 /* The window in which we iterate over current_buffer: */
1518 XSETWINDOW (it->window, w);
1519 it->w = w;
1520 it->f = XFRAME (w->frame);
1521
1522 /* Extra space between lines (on window systems only). */
1523 if (base_face_id == DEFAULT_FACE_ID
1524 && FRAME_WINDOW_P (it->f))
1525 {
1526 if (NATNUMP (current_buffer->extra_line_spacing))
1527 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1528 else if (it->f->extra_line_spacing > 0)
1529 it->extra_line_spacing = it->f->extra_line_spacing;
1530 }
1531
1532 /* If realized faces have been removed, e.g. because of face
1533 attribute changes of named faces, recompute them. When running
1534 in batch mode, the face cache of Vterminal_frame is null. If
1535 we happen to get called, make a dummy face cache. */
1536 if (
1537 #ifndef WINDOWSNT
1538 noninteractive &&
1539 #endif
1540 FRAME_FACE_CACHE (it->f) == NULL)
1541 init_frame_faces (it->f);
1542 if (FRAME_FACE_CACHE (it->f)->used == 0)
1543 recompute_basic_faces (it->f);
1544
1545 /* Current value of the `space-width', and 'height' properties. */
1546 it->space_width = Qnil;
1547 it->font_height = Qnil;
1548
1549 /* Are control characters displayed as `^C'? */
1550 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1551
1552 /* -1 means everything between a CR and the following line end
1553 is invisible. >0 means lines indented more than this value are
1554 invisible. */
1555 it->selective = (INTEGERP (current_buffer->selective_display)
1556 ? XFASTINT (current_buffer->selective_display)
1557 : (!NILP (current_buffer->selective_display)
1558 ? -1 : 0));
1559 it->selective_display_ellipsis_p
1560 = !NILP (current_buffer->selective_display_ellipses);
1561
1562 /* Display table to use. */
1563 it->dp = window_display_table (w);
1564
1565 /* Are multibyte characters enabled in current_buffer? */
1566 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1567
1568 /* Non-zero if we should highlight the region. */
1569 highlight_region_p
1570 = (!NILP (Vtransient_mark_mode)
1571 && !NILP (current_buffer->mark_active)
1572 && XMARKER (current_buffer->mark)->buffer != 0);
1573
1574 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1575 start and end of a visible region in window IT->w. Set both to
1576 -1 to indicate no region. */
1577 if (highlight_region_p
1578 /* Maybe highlight only in selected window. */
1579 && (/* Either show region everywhere. */
1580 highlight_nonselected_windows
1581 /* Or show region in the selected window. */
1582 || w == XWINDOW (selected_window)
1583 /* Or show the region if we are in the mini-buffer and W is
1584 the window the mini-buffer refers to. */
1585 || (MINI_WINDOW_P (XWINDOW (selected_window))
1586 && WINDOWP (Vminibuf_selected_window)
1587 && w == XWINDOW (Vminibuf_selected_window))))
1588 {
1589 int charpos = marker_position (current_buffer->mark);
1590 it->region_beg_charpos = min (PT, charpos);
1591 it->region_end_charpos = max (PT, charpos);
1592 }
1593 else
1594 it->region_beg_charpos = it->region_end_charpos = -1;
1595
1596 /* Get the position at which the redisplay_end_trigger hook should
1597 be run, if it is to be run at all. */
1598 if (MARKERP (w->redisplay_end_trigger)
1599 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1600 it->redisplay_end_trigger_charpos
1601 = marker_position (w->redisplay_end_trigger);
1602 else if (INTEGERP (w->redisplay_end_trigger))
1603 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1604
1605 /* Correct bogus values of tab_width. */
1606 it->tab_width = XINT (current_buffer->tab_width);
1607 if (it->tab_width <= 0 || it->tab_width > 1000)
1608 it->tab_width = 8;
1609
1610 /* Are lines in the display truncated? */
1611 it->truncate_lines_p
1612 = (base_face_id != DEFAULT_FACE_ID
1613 || XINT (it->w->hscroll)
1614 || (truncate_partial_width_windows
1615 && !WINDOW_FULL_WIDTH_P (it->w))
1616 || !NILP (current_buffer->truncate_lines));
1617
1618 /* Get dimensions of truncation and continuation glyphs. These are
1619 displayed as fringe bitmaps under X, so we don't need them for such
1620 frames. */
1621 if (!FRAME_WINDOW_P (it->f))
1622 {
1623 if (it->truncate_lines_p)
1624 {
1625 /* We will need the truncation glyph. */
1626 xassert (it->glyph_row == NULL);
1627 produce_special_glyphs (it, IT_TRUNCATION);
1628 it->truncation_pixel_width = it->pixel_width;
1629 }
1630 else
1631 {
1632 /* We will need the continuation glyph. */
1633 xassert (it->glyph_row == NULL);
1634 produce_special_glyphs (it, IT_CONTINUATION);
1635 it->continuation_pixel_width = it->pixel_width;
1636 }
1637
1638 /* Reset these values to zero because the produce_special_glyphs
1639 above has changed them. */
1640 it->pixel_width = it->ascent = it->descent = 0;
1641 it->phys_ascent = it->phys_descent = 0;
1642 }
1643
1644 /* Set this after getting the dimensions of truncation and
1645 continuation glyphs, so that we don't produce glyphs when calling
1646 produce_special_glyphs, above. */
1647 it->glyph_row = row;
1648 it->area = TEXT_AREA;
1649
1650 /* Get the dimensions of the display area. The display area
1651 consists of the visible window area plus a horizontally scrolled
1652 part to the left of the window. All x-values are relative to the
1653 start of this total display area. */
1654 if (base_face_id != DEFAULT_FACE_ID)
1655 {
1656 /* Mode lines, menu bar in terminal frames. */
1657 it->first_visible_x = 0;
1658 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1659 }
1660 else
1661 {
1662 it->first_visible_x
1663 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1664 it->last_visible_x = (it->first_visible_x
1665 + window_box_width (w, TEXT_AREA));
1666
1667 /* If we truncate lines, leave room for the truncator glyph(s) at
1668 the right margin. Otherwise, leave room for the continuation
1669 glyph(s). Truncation and continuation glyphs are not inserted
1670 for window-based redisplay. */
1671 if (!FRAME_WINDOW_P (it->f))
1672 {
1673 if (it->truncate_lines_p)
1674 it->last_visible_x -= it->truncation_pixel_width;
1675 else
1676 it->last_visible_x -= it->continuation_pixel_width;
1677 }
1678
1679 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1680 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1681 }
1682
1683 /* Leave room for a border glyph. */
1684 if (!FRAME_WINDOW_P (it->f)
1685 && !WINDOW_RIGHTMOST_P (it->w))
1686 it->last_visible_x -= 1;
1687
1688 it->last_visible_y = window_text_bottom_y (w);
1689
1690 /* For mode lines and alike, arrange for the first glyph having a
1691 left box line if the face specifies a box. */
1692 if (base_face_id != DEFAULT_FACE_ID)
1693 {
1694 struct face *face;
1695
1696 it->face_id = base_face_id;
1697
1698 /* If we have a boxed mode line, make the first character appear
1699 with a left box line. */
1700 face = FACE_FROM_ID (it->f, base_face_id);
1701 if (face->box != FACE_NO_BOX)
1702 it->start_of_box_run_p = 1;
1703 }
1704
1705 /* If a buffer position was specified, set the iterator there,
1706 getting overlays and face properties from that position. */
1707 if (charpos >= BUF_BEG (current_buffer))
1708 {
1709 it->end_charpos = ZV;
1710 it->face_id = -1;
1711 IT_CHARPOS (*it) = charpos;
1712
1713 /* Compute byte position if not specified. */
1714 if (bytepos < charpos)
1715 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1716 else
1717 IT_BYTEPOS (*it) = bytepos;
1718
1719 /* Compute faces etc. */
1720 reseat (it, it->current.pos, 1);
1721 }
1722
1723 CHECK_IT (it);
1724 }
1725
1726
1727 /* Initialize IT for the display of window W with window start POS. */
1728
1729 void
1730 start_display (it, w, pos)
1731 struct it *it;
1732 struct window *w;
1733 struct text_pos pos;
1734 {
1735 struct glyph_row *row;
1736 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1737
1738 row = w->desired_matrix->rows + first_vpos;
1739 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1740
1741 if (!it->truncate_lines_p)
1742 {
1743 int start_at_line_beg_p;
1744 int first_y = it->current_y;
1745
1746 /* If window start is not at a line start, skip forward to POS to
1747 get the correct continuation lines width. */
1748 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1749 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1750 if (!start_at_line_beg_p)
1751 {
1752 reseat_at_previous_visible_line_start (it);
1753 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1754
1755 /* If lines are continued, this line may end in the middle
1756 of a multi-glyph character (e.g. a control character
1757 displayed as \003, or in the middle of an overlay
1758 string). In this case move_it_to above will not have
1759 taken us to the start of the continuation line but to the
1760 end of the continued line. */
1761 if (it->current_x > 0)
1762 {
1763 if (it->current.dpvec_index >= 0
1764 || it->current.overlay_string_index >= 0)
1765 {
1766 set_iterator_to_next (it, 1);
1767 move_it_in_display_line_to (it, -1, -1, 0);
1768 }
1769
1770 it->continuation_lines_width += it->current_x;
1771 }
1772
1773 /* We're starting a new display line, not affected by the
1774 height of the continued line, so clear the appropriate
1775 fields in the iterator structure. */
1776 it->max_ascent = it->max_descent = 0;
1777 it->max_phys_ascent = it->max_phys_descent = 0;
1778
1779 it->current_y = first_y;
1780 it->vpos = 0;
1781 it->current_x = it->hpos = 0;
1782 }
1783 }
1784
1785 #if 0 /* Don't assert the following because start_display is sometimes
1786 called intentionally with a window start that is not at a
1787 line start. Please leave this code in as a comment. */
1788
1789 /* Window start should be on a line start, now. */
1790 xassert (it->continuation_lines_width
1791 || IT_CHARPOS (it) == BEGV
1792 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1793 #endif /* 0 */
1794 }
1795
1796
1797 /* Return 1 if POS is a position in ellipses displayed for invisible
1798 text. W is the window we display, for text property lookup. */
1799
1800 static int
1801 in_ellipses_for_invisible_text_p (pos, w)
1802 struct display_pos *pos;
1803 struct window *w;
1804 {
1805 Lisp_Object prop, window;
1806 int ellipses_p = 0;
1807 int charpos = CHARPOS (pos->pos);
1808
1809 /* If POS specifies a position in a display vector, this might
1810 be for an ellipsis displayed for invisible text. We won't
1811 get the iterator set up for delivering that ellipsis unless
1812 we make sure that it gets aware of the invisible text. */
1813 if (pos->dpvec_index >= 0
1814 && pos->overlay_string_index < 0
1815 && CHARPOS (pos->string_pos) < 0
1816 && charpos > BEGV
1817 && (XSETWINDOW (window, w),
1818 prop = Fget_char_property (make_number (charpos),
1819 Qinvisible, window),
1820 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1821 {
1822 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1823 window);
1824 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
1825 }
1826
1827 return ellipses_p;
1828 }
1829
1830
1831 /* Initialize IT for stepping through current_buffer in window W,
1832 starting at position POS that includes overlay string and display
1833 vector/ control character translation position information. Value
1834 is zero if there are overlay strings with newlines at POS. */
1835
1836 static int
1837 init_from_display_pos (it, w, pos)
1838 struct it *it;
1839 struct window *w;
1840 struct display_pos *pos;
1841 {
1842 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1843 int i, overlay_strings_with_newlines = 0;
1844
1845 /* If POS specifies a position in a display vector, this might
1846 be for an ellipsis displayed for invisible text. We won't
1847 get the iterator set up for delivering that ellipsis unless
1848 we make sure that it gets aware of the invisible text. */
1849 if (in_ellipses_for_invisible_text_p (pos, w))
1850 {
1851 --charpos;
1852 bytepos = 0;
1853 }
1854
1855 /* Keep in mind: the call to reseat in init_iterator skips invisible
1856 text, so we might end up at a position different from POS. This
1857 is only a problem when POS is a row start after a newline and an
1858 overlay starts there with an after-string, and the overlay has an
1859 invisible property. Since we don't skip invisible text in
1860 display_line and elsewhere immediately after consuming the
1861 newline before the row start, such a POS will not be in a string,
1862 but the call to init_iterator below will move us to the
1863 after-string. */
1864 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1865
1866 for (i = 0; i < it->n_overlay_strings; ++i)
1867 {
1868 char *s = XSTRING (it->overlay_strings[i])->data;
1869 char *e = s + STRING_BYTES (XSTRING (it->overlay_strings[i]));
1870
1871 while (s < e && *s != '\n')
1872 ++s;
1873
1874 if (s < e)
1875 {
1876 overlay_strings_with_newlines = 1;
1877 break;
1878 }
1879 }
1880
1881 /* If position is within an overlay string, set up IT to the right
1882 overlay string. */
1883 if (pos->overlay_string_index >= 0)
1884 {
1885 int relative_index;
1886
1887 /* If the first overlay string happens to have a `display'
1888 property for an image, the iterator will be set up for that
1889 image, and we have to undo that setup first before we can
1890 correct the overlay string index. */
1891 if (it->method == next_element_from_image)
1892 pop_it (it);
1893
1894 /* We already have the first chunk of overlay strings in
1895 IT->overlay_strings. Load more until the one for
1896 pos->overlay_string_index is in IT->overlay_strings. */
1897 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1898 {
1899 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1900 it->current.overlay_string_index = 0;
1901 while (n--)
1902 {
1903 load_overlay_strings (it, 0);
1904 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1905 }
1906 }
1907
1908 it->current.overlay_string_index = pos->overlay_string_index;
1909 relative_index = (it->current.overlay_string_index
1910 % OVERLAY_STRING_CHUNK_SIZE);
1911 it->string = it->overlay_strings[relative_index];
1912 xassert (STRINGP (it->string));
1913 it->current.string_pos = pos->string_pos;
1914 it->method = next_element_from_string;
1915 }
1916
1917 #if 0 /* This is bogus because POS not having an overlay string
1918 position does not mean it's after the string. Example: A
1919 line starting with a before-string and initialization of IT
1920 to the previous row's end position. */
1921 else if (it->current.overlay_string_index >= 0)
1922 {
1923 /* If POS says we're already after an overlay string ending at
1924 POS, make sure to pop the iterator because it will be in
1925 front of that overlay string. When POS is ZV, we've thereby
1926 also ``processed'' overlay strings at ZV. */
1927 while (it->sp)
1928 pop_it (it);
1929 it->current.overlay_string_index = -1;
1930 it->method = next_element_from_buffer;
1931 if (CHARPOS (pos->pos) == ZV)
1932 it->overlay_strings_at_end_processed_p = 1;
1933 }
1934 #endif /* 0 */
1935
1936 if (CHARPOS (pos->string_pos) >= 0)
1937 {
1938 /* Recorded position is not in an overlay string, but in another
1939 string. This can only be a string from a `display' property.
1940 IT should already be filled with that string. */
1941 it->current.string_pos = pos->string_pos;
1942 xassert (STRINGP (it->string));
1943 }
1944
1945 /* Restore position in display vector translations, control
1946 character translations or ellipses. */
1947 if (pos->dpvec_index >= 0)
1948 {
1949 if (it->dpvec == NULL)
1950 get_next_display_element (it);
1951 xassert (it->dpvec && it->current.dpvec_index == 0);
1952 it->current.dpvec_index = pos->dpvec_index;
1953 }
1954
1955 CHECK_IT (it);
1956 return !overlay_strings_with_newlines;
1957 }
1958
1959
1960 /* Initialize IT for stepping through current_buffer in window W
1961 starting at ROW->start. */
1962
1963 static void
1964 init_to_row_start (it, w, row)
1965 struct it *it;
1966 struct window *w;
1967 struct glyph_row *row;
1968 {
1969 init_from_display_pos (it, w, &row->start);
1970 it->continuation_lines_width = row->continuation_lines_width;
1971 CHECK_IT (it);
1972 }
1973
1974
1975 /* Initialize IT for stepping through current_buffer in window W
1976 starting in the line following ROW, i.e. starting at ROW->end.
1977 Value is zero if there are overlay strings with newlines at ROW's
1978 end position. */
1979
1980 static int
1981 init_to_row_end (it, w, row)
1982 struct it *it;
1983 struct window *w;
1984 struct glyph_row *row;
1985 {
1986 int success = 0;
1987
1988 if (init_from_display_pos (it, w, &row->end))
1989 {
1990 if (row->continued_p)
1991 it->continuation_lines_width
1992 = row->continuation_lines_width + row->pixel_width;
1993 CHECK_IT (it);
1994 success = 1;
1995 }
1996
1997 return success;
1998 }
1999
2000
2001
2002 \f
2003 /***********************************************************************
2004 Text properties
2005 ***********************************************************************/
2006
2007 /* Called when IT reaches IT->stop_charpos. Handle text property and
2008 overlay changes. Set IT->stop_charpos to the next position where
2009 to stop. */
2010
2011 static void
2012 handle_stop (it)
2013 struct it *it;
2014 {
2015 enum prop_handled handled;
2016 int handle_overlay_change_p = 1;
2017 struct props *p;
2018
2019 it->dpvec = NULL;
2020 it->current.dpvec_index = -1;
2021
2022 do
2023 {
2024 handled = HANDLED_NORMALLY;
2025
2026 /* Call text property handlers. */
2027 for (p = it_props; p->handler; ++p)
2028 {
2029 handled = p->handler (it);
2030
2031 if (handled == HANDLED_RECOMPUTE_PROPS)
2032 break;
2033 else if (handled == HANDLED_RETURN)
2034 return;
2035 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2036 handle_overlay_change_p = 0;
2037 }
2038
2039 if (handled != HANDLED_RECOMPUTE_PROPS)
2040 {
2041 /* Don't check for overlay strings below when set to deliver
2042 characters from a display vector. */
2043 if (it->method == next_element_from_display_vector)
2044 handle_overlay_change_p = 0;
2045
2046 /* Handle overlay changes. */
2047 if (handle_overlay_change_p)
2048 handled = handle_overlay_change (it);
2049
2050 /* Determine where to stop next. */
2051 if (handled == HANDLED_NORMALLY)
2052 compute_stop_pos (it);
2053 }
2054 }
2055 while (handled == HANDLED_RECOMPUTE_PROPS);
2056 }
2057
2058
2059 /* Compute IT->stop_charpos from text property and overlay change
2060 information for IT's current position. */
2061
2062 static void
2063 compute_stop_pos (it)
2064 struct it *it;
2065 {
2066 register INTERVAL iv, next_iv;
2067 Lisp_Object object, limit, position;
2068
2069 /* If nowhere else, stop at the end. */
2070 it->stop_charpos = it->end_charpos;
2071
2072 if (STRINGP (it->string))
2073 {
2074 /* Strings are usually short, so don't limit the search for
2075 properties. */
2076 object = it->string;
2077 limit = Qnil;
2078 position = make_number (IT_STRING_CHARPOS (*it));
2079 }
2080 else
2081 {
2082 int charpos;
2083
2084 /* If next overlay change is in front of the current stop pos
2085 (which is IT->end_charpos), stop there. Note: value of
2086 next_overlay_change is point-max if no overlay change
2087 follows. */
2088 charpos = next_overlay_change (IT_CHARPOS (*it));
2089 if (charpos < it->stop_charpos)
2090 it->stop_charpos = charpos;
2091
2092 /* If showing the region, we have to stop at the region
2093 start or end because the face might change there. */
2094 if (it->region_beg_charpos > 0)
2095 {
2096 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2097 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2098 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2099 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2100 }
2101
2102 /* Set up variables for computing the stop position from text
2103 property changes. */
2104 XSETBUFFER (object, current_buffer);
2105 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2106 position = make_number (IT_CHARPOS (*it));
2107
2108 }
2109
2110 /* Get the interval containing IT's position. Value is a null
2111 interval if there isn't such an interval. */
2112 iv = validate_interval_range (object, &position, &position, 0);
2113 if (!NULL_INTERVAL_P (iv))
2114 {
2115 Lisp_Object values_here[LAST_PROP_IDX];
2116 struct props *p;
2117
2118 /* Get properties here. */
2119 for (p = it_props; p->handler; ++p)
2120 values_here[p->idx] = textget (iv->plist, *p->name);
2121
2122 /* Look for an interval following iv that has different
2123 properties. */
2124 for (next_iv = next_interval (iv);
2125 (!NULL_INTERVAL_P (next_iv)
2126 && (NILP (limit)
2127 || XFASTINT (limit) > next_iv->position));
2128 next_iv = next_interval (next_iv))
2129 {
2130 for (p = it_props; p->handler; ++p)
2131 {
2132 Lisp_Object new_value;
2133
2134 new_value = textget (next_iv->plist, *p->name);
2135 if (!EQ (values_here[p->idx], new_value))
2136 break;
2137 }
2138
2139 if (p->handler)
2140 break;
2141 }
2142
2143 if (!NULL_INTERVAL_P (next_iv))
2144 {
2145 if (INTEGERP (limit)
2146 && next_iv->position >= XFASTINT (limit))
2147 /* No text property change up to limit. */
2148 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2149 else
2150 /* Text properties change in next_iv. */
2151 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2152 }
2153 }
2154
2155 xassert (STRINGP (it->string)
2156 || (it->stop_charpos >= BEGV
2157 && it->stop_charpos >= IT_CHARPOS (*it)));
2158 }
2159
2160
2161 /* Return the position of the next overlay change after POS in
2162 current_buffer. Value is point-max if no overlay change
2163 follows. This is like `next-overlay-change' but doesn't use
2164 xmalloc. */
2165
2166 static int
2167 next_overlay_change (pos)
2168 int pos;
2169 {
2170 int noverlays;
2171 int endpos;
2172 Lisp_Object *overlays;
2173 int len;
2174 int i;
2175
2176 /* Get all overlays at the given position. */
2177 len = 10;
2178 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2179 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2180 if (noverlays > len)
2181 {
2182 len = noverlays;
2183 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2184 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2185 }
2186
2187 /* If any of these overlays ends before endpos,
2188 use its ending point instead. */
2189 for (i = 0; i < noverlays; ++i)
2190 {
2191 Lisp_Object oend;
2192 int oendpos;
2193
2194 oend = OVERLAY_END (overlays[i]);
2195 oendpos = OVERLAY_POSITION (oend);
2196 endpos = min (endpos, oendpos);
2197 }
2198
2199 return endpos;
2200 }
2201
2202
2203 \f
2204 /***********************************************************************
2205 Fontification
2206 ***********************************************************************/
2207
2208 /* Handle changes in the `fontified' property of the current buffer by
2209 calling hook functions from Qfontification_functions to fontify
2210 regions of text. */
2211
2212 static enum prop_handled
2213 handle_fontified_prop (it)
2214 struct it *it;
2215 {
2216 Lisp_Object prop, pos;
2217 enum prop_handled handled = HANDLED_NORMALLY;
2218
2219 /* Get the value of the `fontified' property at IT's current buffer
2220 position. (The `fontified' property doesn't have a special
2221 meaning in strings.) If the value is nil, call functions from
2222 Qfontification_functions. */
2223 if (!STRINGP (it->string)
2224 && it->s == NULL
2225 && !NILP (Vfontification_functions)
2226 && !NILP (Vrun_hooks)
2227 && (pos = make_number (IT_CHARPOS (*it)),
2228 prop = Fget_char_property (pos, Qfontified, Qnil),
2229 NILP (prop)))
2230 {
2231 int count = BINDING_STACK_SIZE ();
2232 Lisp_Object val;
2233
2234 val = Vfontification_functions;
2235 specbind (Qfontification_functions, Qnil);
2236
2237 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2238 safe_call1 (val, pos);
2239 else
2240 {
2241 Lisp_Object globals, fn;
2242 struct gcpro gcpro1, gcpro2;
2243
2244 globals = Qnil;
2245 GCPRO2 (val, globals);
2246
2247 for (; CONSP (val); val = XCDR (val))
2248 {
2249 fn = XCAR (val);
2250
2251 if (EQ (fn, Qt))
2252 {
2253 /* A value of t indicates this hook has a local
2254 binding; it means to run the global binding too.
2255 In a global value, t should not occur. If it
2256 does, we must ignore it to avoid an endless
2257 loop. */
2258 for (globals = Fdefault_value (Qfontification_functions);
2259 CONSP (globals);
2260 globals = XCDR (globals))
2261 {
2262 fn = XCAR (globals);
2263 if (!EQ (fn, Qt))
2264 safe_call1 (fn, pos);
2265 }
2266 }
2267 else
2268 safe_call1 (fn, pos);
2269 }
2270
2271 UNGCPRO;
2272 }
2273
2274 unbind_to (count, Qnil);
2275
2276 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2277 something. This avoids an endless loop if they failed to
2278 fontify the text for which reason ever. */
2279 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2280 handled = HANDLED_RECOMPUTE_PROPS;
2281 }
2282
2283 return handled;
2284 }
2285
2286
2287 \f
2288 /***********************************************************************
2289 Faces
2290 ***********************************************************************/
2291
2292 /* Set up iterator IT from face properties at its current position.
2293 Called from handle_stop. */
2294
2295 static enum prop_handled
2296 handle_face_prop (it)
2297 struct it *it;
2298 {
2299 int new_face_id, next_stop;
2300
2301 if (!STRINGP (it->string))
2302 {
2303 new_face_id
2304 = face_at_buffer_position (it->w,
2305 IT_CHARPOS (*it),
2306 it->region_beg_charpos,
2307 it->region_end_charpos,
2308 &next_stop,
2309 (IT_CHARPOS (*it)
2310 + TEXT_PROP_DISTANCE_LIMIT),
2311 0);
2312
2313 /* Is this a start of a run of characters with box face?
2314 Caveat: this can be called for a freshly initialized
2315 iterator; face_id is -1 in this case. We know that the new
2316 face will not change until limit, i.e. if the new face has a
2317 box, all characters up to limit will have one. But, as
2318 usual, we don't know whether limit is really the end. */
2319 if (new_face_id != it->face_id)
2320 {
2321 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2322
2323 /* If new face has a box but old face has not, this is
2324 the start of a run of characters with box, i.e. it has
2325 a shadow on the left side. The value of face_id of the
2326 iterator will be -1 if this is the initial call that gets
2327 the face. In this case, we have to look in front of IT's
2328 position and see whether there is a face != new_face_id. */
2329 it->start_of_box_run_p
2330 = (new_face->box != FACE_NO_BOX
2331 && (it->face_id >= 0
2332 || IT_CHARPOS (*it) == BEG
2333 || new_face_id != face_before_it_pos (it)));
2334 it->face_box_p = new_face->box != FACE_NO_BOX;
2335 }
2336 }
2337 else
2338 {
2339 int base_face_id, bufpos;
2340
2341 if (it->current.overlay_string_index >= 0)
2342 bufpos = IT_CHARPOS (*it);
2343 else
2344 bufpos = 0;
2345
2346 /* For strings from a buffer, i.e. overlay strings or strings
2347 from a `display' property, use the face at IT's current
2348 buffer position as the base face to merge with, so that
2349 overlay strings appear in the same face as surrounding
2350 text, unless they specify their own faces. */
2351 base_face_id = underlying_face_id (it);
2352
2353 new_face_id = face_at_string_position (it->w,
2354 it->string,
2355 IT_STRING_CHARPOS (*it),
2356 bufpos,
2357 it->region_beg_charpos,
2358 it->region_end_charpos,
2359 &next_stop,
2360 base_face_id, 0);
2361
2362 #if 0 /* This shouldn't be neccessary. Let's check it. */
2363 /* If IT is used to display a mode line we would really like to
2364 use the mode line face instead of the frame's default face. */
2365 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2366 && new_face_id == DEFAULT_FACE_ID)
2367 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2368 #endif
2369
2370 /* Is this a start of a run of characters with box? Caveat:
2371 this can be called for a freshly allocated iterator; face_id
2372 is -1 is this case. We know that the new face will not
2373 change until the next check pos, i.e. if the new face has a
2374 box, all characters up to that position will have a
2375 box. But, as usual, we don't know whether that position
2376 is really the end. */
2377 if (new_face_id != it->face_id)
2378 {
2379 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2380 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2381
2382 /* If new face has a box but old face hasn't, this is the
2383 start of a run of characters with box, i.e. it has a
2384 shadow on the left side. */
2385 it->start_of_box_run_p
2386 = new_face->box && (old_face == NULL || !old_face->box);
2387 it->face_box_p = new_face->box != FACE_NO_BOX;
2388 }
2389 }
2390
2391 it->face_id = new_face_id;
2392 return HANDLED_NORMALLY;
2393 }
2394
2395
2396 /* Return the ID of the face ``underlying'' IT's current position,
2397 which is in a string. If the iterator is associated with a
2398 buffer, return the face at IT's current buffer position.
2399 Otherwise, use the iterator's base_face_id. */
2400
2401 static int
2402 underlying_face_id (it)
2403 struct it *it;
2404 {
2405 int face_id = it->base_face_id, i;
2406
2407 xassert (STRINGP (it->string));
2408
2409 for (i = it->sp - 1; i >= 0; --i)
2410 if (NILP (it->stack[i].string))
2411 face_id = it->stack[i].face_id;
2412
2413 return face_id;
2414 }
2415
2416
2417 /* Compute the face one character before or after the current position
2418 of IT. BEFORE_P non-zero means get the face in front of IT's
2419 position. Value is the id of the face. */
2420
2421 static int
2422 face_before_or_after_it_pos (it, before_p)
2423 struct it *it;
2424 int before_p;
2425 {
2426 int face_id, limit;
2427 int next_check_charpos;
2428 struct text_pos pos;
2429
2430 xassert (it->s == NULL);
2431
2432 if (STRINGP (it->string))
2433 {
2434 int bufpos, base_face_id;
2435
2436 /* No face change past the end of the string (for the case
2437 we are padding with spaces). No face change before the
2438 string start. */
2439 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2440 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2441 return it->face_id;
2442
2443 /* Set pos to the position before or after IT's current position. */
2444 if (before_p)
2445 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2446 else
2447 /* For composition, we must check the character after the
2448 composition. */
2449 pos = (it->what == IT_COMPOSITION
2450 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2451 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2452
2453 if (it->current.overlay_string_index >= 0)
2454 bufpos = IT_CHARPOS (*it);
2455 else
2456 bufpos = 0;
2457
2458 base_face_id = underlying_face_id (it);
2459
2460 /* Get the face for ASCII, or unibyte. */
2461 face_id = face_at_string_position (it->w,
2462 it->string,
2463 CHARPOS (pos),
2464 bufpos,
2465 it->region_beg_charpos,
2466 it->region_end_charpos,
2467 &next_check_charpos,
2468 base_face_id, 0);
2469
2470 /* Correct the face for charsets different from ASCII. Do it
2471 for the multibyte case only. The face returned above is
2472 suitable for unibyte text if IT->string is unibyte. */
2473 if (STRING_MULTIBYTE (it->string))
2474 {
2475 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2476 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2477 int c, len;
2478 struct face *face = FACE_FROM_ID (it->f, face_id);
2479
2480 c = string_char_and_length (p, rest, &len);
2481 face_id = FACE_FOR_CHAR (it->f, face, c);
2482 }
2483 }
2484 else
2485 {
2486 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2487 || (IT_CHARPOS (*it) <= BEGV && before_p))
2488 return it->face_id;
2489
2490 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2491 pos = it->current.pos;
2492
2493 if (before_p)
2494 DEC_TEXT_POS (pos, it->multibyte_p);
2495 else
2496 {
2497 if (it->what == IT_COMPOSITION)
2498 /* For composition, we must check the position after the
2499 composition. */
2500 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2501 else
2502 INC_TEXT_POS (pos, it->multibyte_p);
2503 }
2504
2505 /* Determine face for CHARSET_ASCII, or unibyte. */
2506 face_id = face_at_buffer_position (it->w,
2507 CHARPOS (pos),
2508 it->region_beg_charpos,
2509 it->region_end_charpos,
2510 &next_check_charpos,
2511 limit, 0);
2512
2513 /* Correct the face for charsets different from ASCII. Do it
2514 for the multibyte case only. The face returned above is
2515 suitable for unibyte text if current_buffer is unibyte. */
2516 if (it->multibyte_p)
2517 {
2518 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2519 struct face *face = FACE_FROM_ID (it->f, face_id);
2520 face_id = FACE_FOR_CHAR (it->f, face, c);
2521 }
2522 }
2523
2524 return face_id;
2525 }
2526
2527
2528 \f
2529 /***********************************************************************
2530 Invisible text
2531 ***********************************************************************/
2532
2533 /* Set up iterator IT from invisible properties at its current
2534 position. Called from handle_stop. */
2535
2536 static enum prop_handled
2537 handle_invisible_prop (it)
2538 struct it *it;
2539 {
2540 enum prop_handled handled = HANDLED_NORMALLY;
2541
2542 if (STRINGP (it->string))
2543 {
2544 extern Lisp_Object Qinvisible;
2545 Lisp_Object prop, end_charpos, limit, charpos;
2546
2547 /* Get the value of the invisible text property at the
2548 current position. Value will be nil if there is no such
2549 property. */
2550 charpos = make_number (IT_STRING_CHARPOS (*it));
2551 prop = Fget_text_property (charpos, Qinvisible, it->string);
2552
2553 if (!NILP (prop)
2554 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2555 {
2556 handled = HANDLED_RECOMPUTE_PROPS;
2557
2558 /* Get the position at which the next change of the
2559 invisible text property can be found in IT->string.
2560 Value will be nil if the property value is the same for
2561 all the rest of IT->string. */
2562 XSETINT (limit, XSTRING (it->string)->size);
2563 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2564 it->string, limit);
2565
2566 /* Text at current position is invisible. The next
2567 change in the property is at position end_charpos.
2568 Move IT's current position to that position. */
2569 if (INTEGERP (end_charpos)
2570 && XFASTINT (end_charpos) < XFASTINT (limit))
2571 {
2572 struct text_pos old;
2573 old = it->current.string_pos;
2574 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2575 compute_string_pos (&it->current.string_pos, old, it->string);
2576 }
2577 else
2578 {
2579 /* The rest of the string is invisible. If this is an
2580 overlay string, proceed with the next overlay string
2581 or whatever comes and return a character from there. */
2582 if (it->current.overlay_string_index >= 0)
2583 {
2584 next_overlay_string (it);
2585 /* Don't check for overlay strings when we just
2586 finished processing them. */
2587 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2588 }
2589 else
2590 {
2591 struct Lisp_String *s = XSTRING (it->string);
2592 IT_STRING_CHARPOS (*it) = s->size;
2593 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2594 }
2595 }
2596 }
2597 }
2598 else
2599 {
2600 int invis_p, newpos, next_stop, start_charpos;
2601 Lisp_Object pos, prop, overlay;
2602
2603 /* First of all, is there invisible text at this position? */
2604 start_charpos = IT_CHARPOS (*it);
2605 pos = make_number (IT_CHARPOS (*it));
2606 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2607 &overlay);
2608 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2609
2610 /* If we are on invisible text, skip over it. */
2611 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
2612 {
2613 /* Record whether we have to display an ellipsis for the
2614 invisible text. */
2615 int display_ellipsis_p = invis_p == 2;
2616
2617 handled = HANDLED_RECOMPUTE_PROPS;
2618
2619 /* Loop skipping over invisible text. The loop is left at
2620 ZV or with IT on the first char being visible again. */
2621 do
2622 {
2623 /* Try to skip some invisible text. Return value is the
2624 position reached which can be equal to IT's position
2625 if there is nothing invisible here. This skips both
2626 over invisible text properties and overlays with
2627 invisible property. */
2628 newpos = skip_invisible (IT_CHARPOS (*it),
2629 &next_stop, ZV, it->window);
2630
2631 /* If we skipped nothing at all we weren't at invisible
2632 text in the first place. If everything to the end of
2633 the buffer was skipped, end the loop. */
2634 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2635 invis_p = 0;
2636 else
2637 {
2638 /* We skipped some characters but not necessarily
2639 all there are. Check if we ended up on visible
2640 text. Fget_char_property returns the property of
2641 the char before the given position, i.e. if we
2642 get invis_p = 0, this means that the char at
2643 newpos is visible. */
2644 pos = make_number (newpos);
2645 prop = Fget_char_property (pos, Qinvisible, it->window);
2646 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2647 }
2648
2649 /* If we ended up on invisible text, proceed to
2650 skip starting with next_stop. */
2651 if (invis_p)
2652 IT_CHARPOS (*it) = next_stop;
2653 }
2654 while (invis_p);
2655
2656 /* The position newpos is now either ZV or on visible text. */
2657 IT_CHARPOS (*it) = newpos;
2658 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2659
2660 /* If there are before-strings at the start of invisible
2661 text, and the text is invisible because of a text
2662 property, arrange to show before-strings because 20.x did
2663 it that way. (If the text is invisible because of an
2664 overlay property instead of a text property, this is
2665 already handled in the overlay code.) */
2666 if (NILP (overlay)
2667 && get_overlay_strings (it, start_charpos))
2668 {
2669 handled = HANDLED_RECOMPUTE_PROPS;
2670 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2671 }
2672 else if (display_ellipsis_p)
2673 setup_for_ellipsis (it);
2674 }
2675 }
2676
2677 return handled;
2678 }
2679
2680
2681 /* Make iterator IT return `...' next. */
2682
2683 static void
2684 setup_for_ellipsis (it)
2685 struct it *it;
2686 {
2687 if (it->dp
2688 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2689 {
2690 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2691 it->dpvec = v->contents;
2692 it->dpend = v->contents + v->size;
2693 }
2694 else
2695 {
2696 /* Default `...'. */
2697 it->dpvec = default_invis_vector;
2698 it->dpend = default_invis_vector + 3;
2699 }
2700
2701 /* The ellipsis display does not replace the display of the
2702 character at the new position. Indicate this by setting
2703 IT->dpvec_char_len to zero. */
2704 it->dpvec_char_len = 0;
2705
2706 it->current.dpvec_index = 0;
2707 it->method = next_element_from_display_vector;
2708 }
2709
2710
2711 \f
2712 /***********************************************************************
2713 'display' property
2714 ***********************************************************************/
2715
2716 /* Set up iterator IT from `display' property at its current position.
2717 Called from handle_stop. */
2718
2719 static enum prop_handled
2720 handle_display_prop (it)
2721 struct it *it;
2722 {
2723 Lisp_Object prop, object;
2724 struct text_pos *position;
2725 int display_replaced_p = 0;
2726
2727 if (STRINGP (it->string))
2728 {
2729 object = it->string;
2730 position = &it->current.string_pos;
2731 }
2732 else
2733 {
2734 object = it->w->buffer;
2735 position = &it->current.pos;
2736 }
2737
2738 /* Reset those iterator values set from display property values. */
2739 it->font_height = Qnil;
2740 it->space_width = Qnil;
2741 it->voffset = 0;
2742
2743 /* We don't support recursive `display' properties, i.e. string
2744 values that have a string `display' property, that have a string
2745 `display' property etc. */
2746 if (!it->string_from_display_prop_p)
2747 it->area = TEXT_AREA;
2748
2749 prop = Fget_char_property (make_number (position->charpos),
2750 Qdisplay, object);
2751 if (NILP (prop))
2752 return HANDLED_NORMALLY;
2753
2754 if (CONSP (prop)
2755 /* Simple properties. */
2756 && !EQ (XCAR (prop), Qimage)
2757 && !EQ (XCAR (prop), Qspace)
2758 && !EQ (XCAR (prop), Qwhen)
2759 && !EQ (XCAR (prop), Qspace_width)
2760 && !EQ (XCAR (prop), Qheight)
2761 && !EQ (XCAR (prop), Qraise)
2762 /* Marginal area specifications. */
2763 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2764 && !NILP (XCAR (prop)))
2765 {
2766 for (; CONSP (prop); prop = XCDR (prop))
2767 {
2768 if (handle_single_display_prop (it, XCAR (prop), object,
2769 position, display_replaced_p))
2770 display_replaced_p = 1;
2771 }
2772 }
2773 else if (VECTORP (prop))
2774 {
2775 int i;
2776 for (i = 0; i < ASIZE (prop); ++i)
2777 if (handle_single_display_prop (it, AREF (prop, i), object,
2778 position, display_replaced_p))
2779 display_replaced_p = 1;
2780 }
2781 else
2782 {
2783 if (handle_single_display_prop (it, prop, object, position, 0))
2784 display_replaced_p = 1;
2785 }
2786
2787 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2788 }
2789
2790
2791 /* Value is the position of the end of the `display' property starting
2792 at START_POS in OBJECT. */
2793
2794 static struct text_pos
2795 display_prop_end (it, object, start_pos)
2796 struct it *it;
2797 Lisp_Object object;
2798 struct text_pos start_pos;
2799 {
2800 Lisp_Object end;
2801 struct text_pos end_pos;
2802
2803 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2804 Qdisplay, object, Qnil);
2805 CHARPOS (end_pos) = XFASTINT (end);
2806 if (STRINGP (object))
2807 compute_string_pos (&end_pos, start_pos, it->string);
2808 else
2809 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2810
2811 return end_pos;
2812 }
2813
2814
2815 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2816 is the object in which the `display' property was found. *POSITION
2817 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2818 means that we previously saw a display sub-property which already
2819 replaced text display with something else, for example an image;
2820 ignore such properties after the first one has been processed.
2821
2822 If PROP is a `space' or `image' sub-property, set *POSITION to the
2823 end position of the `display' property.
2824
2825 Value is non-zero if something was found which replaces the display
2826 of buffer or string text. */
2827
2828 static int
2829 handle_single_display_prop (it, prop, object, position,
2830 display_replaced_before_p)
2831 struct it *it;
2832 Lisp_Object prop;
2833 Lisp_Object object;
2834 struct text_pos *position;
2835 int display_replaced_before_p;
2836 {
2837 Lisp_Object value;
2838 int replaces_text_display_p = 0;
2839 Lisp_Object form;
2840
2841 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2842 evaluated. If the result is nil, VALUE is ignored. */
2843 form = Qt;
2844 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2845 {
2846 prop = XCDR (prop);
2847 if (!CONSP (prop))
2848 return 0;
2849 form = XCAR (prop);
2850 prop = XCDR (prop);
2851 }
2852
2853 if (!NILP (form) && !EQ (form, Qt))
2854 {
2855 int count = BINDING_STACK_SIZE ();
2856 struct gcpro gcpro1;
2857
2858 /* Bind `object' to the object having the `display' property, a
2859 buffer or string. Bind `position' to the position in the
2860 object where the property was found, and `buffer-position'
2861 to the current position in the buffer. */
2862 specbind (Qobject, object);
2863 specbind (Qposition, make_number (CHARPOS (*position)));
2864 specbind (Qbuffer_position,
2865 make_number (STRINGP (object)
2866 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2867 GCPRO1 (form);
2868 form = safe_eval (form);
2869 UNGCPRO;
2870 unbind_to (count, Qnil);
2871 }
2872
2873 if (NILP (form))
2874 return 0;
2875
2876 if (CONSP (prop)
2877 && EQ (XCAR (prop), Qheight)
2878 && CONSP (XCDR (prop)))
2879 {
2880 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2881 return 0;
2882
2883 /* `(height HEIGHT)'. */
2884 it->font_height = XCAR (XCDR (prop));
2885 if (!NILP (it->font_height))
2886 {
2887 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2888 int new_height = -1;
2889
2890 if (CONSP (it->font_height)
2891 && (EQ (XCAR (it->font_height), Qplus)
2892 || EQ (XCAR (it->font_height), Qminus))
2893 && CONSP (XCDR (it->font_height))
2894 && INTEGERP (XCAR (XCDR (it->font_height))))
2895 {
2896 /* `(+ N)' or `(- N)' where N is an integer. */
2897 int steps = XINT (XCAR (XCDR (it->font_height)));
2898 if (EQ (XCAR (it->font_height), Qplus))
2899 steps = - steps;
2900 it->face_id = smaller_face (it->f, it->face_id, steps);
2901 }
2902 else if (FUNCTIONP (it->font_height))
2903 {
2904 /* Call function with current height as argument.
2905 Value is the new height. */
2906 Lisp_Object height;
2907 height = safe_call1 (it->font_height,
2908 face->lface[LFACE_HEIGHT_INDEX]);
2909 if (NUMBERP (height))
2910 new_height = XFLOATINT (height);
2911 }
2912 else if (NUMBERP (it->font_height))
2913 {
2914 /* Value is a multiple of the canonical char height. */
2915 struct face *face;
2916
2917 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2918 new_height = (XFLOATINT (it->font_height)
2919 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2920 }
2921 else
2922 {
2923 /* Evaluate IT->font_height with `height' bound to the
2924 current specified height to get the new height. */
2925 Lisp_Object value;
2926 int count = BINDING_STACK_SIZE ();
2927
2928 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2929 value = safe_eval (it->font_height);
2930 unbind_to (count, Qnil);
2931
2932 if (NUMBERP (value))
2933 new_height = XFLOATINT (value);
2934 }
2935
2936 if (new_height > 0)
2937 it->face_id = face_with_height (it->f, it->face_id, new_height);
2938 }
2939 }
2940 else if (CONSP (prop)
2941 && EQ (XCAR (prop), Qspace_width)
2942 && CONSP (XCDR (prop)))
2943 {
2944 /* `(space_width WIDTH)'. */
2945 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2946 return 0;
2947
2948 value = XCAR (XCDR (prop));
2949 if (NUMBERP (value) && XFLOATINT (value) > 0)
2950 it->space_width = value;
2951 }
2952 else if (CONSP (prop)
2953 && EQ (XCAR (prop), Qraise)
2954 && CONSP (XCDR (prop)))
2955 {
2956 /* `(raise FACTOR)'. */
2957 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2958 return 0;
2959
2960 #ifdef HAVE_WINDOW_SYSTEM
2961 value = XCAR (XCDR (prop));
2962 if (NUMBERP (value))
2963 {
2964 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2965 it->voffset = - (XFLOATINT (value)
2966 * (FONT_HEIGHT (face->font)));
2967 }
2968 #endif /* HAVE_WINDOW_SYSTEM */
2969 }
2970 else if (!it->string_from_display_prop_p)
2971 {
2972 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2973 VALUE) or `((margin nil) VALUE)' or VALUE. */
2974 Lisp_Object location, value;
2975 struct text_pos start_pos;
2976 int valid_p;
2977
2978 /* Characters having this form of property are not displayed, so
2979 we have to find the end of the property. */
2980 start_pos = *position;
2981 *position = display_prop_end (it, object, start_pos);
2982 value = Qnil;
2983
2984 /* Let's stop at the new position and assume that all
2985 text properties change there. */
2986 it->stop_charpos = position->charpos;
2987
2988 location = Qunbound;
2989 if (CONSP (prop) && CONSP (XCAR (prop)))
2990 {
2991 Lisp_Object tem;
2992
2993 value = XCDR (prop);
2994 if (CONSP (value))
2995 value = XCAR (value);
2996
2997 tem = XCAR (prop);
2998 if (EQ (XCAR (tem), Qmargin)
2999 && (tem = XCDR (tem),
3000 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3001 (NILP (tem)
3002 || EQ (tem, Qleft_margin)
3003 || EQ (tem, Qright_margin))))
3004 location = tem;
3005 }
3006
3007 if (EQ (location, Qunbound))
3008 {
3009 location = Qnil;
3010 value = prop;
3011 }
3012
3013 #ifdef HAVE_WINDOW_SYSTEM
3014 if (FRAME_TERMCAP_P (it->f))
3015 valid_p = STRINGP (value);
3016 else
3017 valid_p = (STRINGP (value)
3018 || (CONSP (value) && EQ (XCAR (value), Qspace))
3019 || valid_image_p (value));
3020 #else /* not HAVE_WINDOW_SYSTEM */
3021 valid_p = STRINGP (value);
3022 #endif /* not HAVE_WINDOW_SYSTEM */
3023
3024 if ((EQ (location, Qleft_margin)
3025 || EQ (location, Qright_margin)
3026 || NILP (location))
3027 && valid_p
3028 && !display_replaced_before_p)
3029 {
3030 replaces_text_display_p = 1;
3031
3032 /* Save current settings of IT so that we can restore them
3033 when we are finished with the glyph property value. */
3034 push_it (it);
3035
3036 if (NILP (location))
3037 it->area = TEXT_AREA;
3038 else if (EQ (location, Qleft_margin))
3039 it->area = LEFT_MARGIN_AREA;
3040 else
3041 it->area = RIGHT_MARGIN_AREA;
3042
3043 if (STRINGP (value))
3044 {
3045 it->string = value;
3046 it->multibyte_p = STRING_MULTIBYTE (it->string);
3047 it->current.overlay_string_index = -1;
3048 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3049 it->end_charpos = it->string_nchars
3050 = XSTRING (it->string)->size;
3051 it->method = next_element_from_string;
3052 it->stop_charpos = 0;
3053 it->string_from_display_prop_p = 1;
3054 /* Say that we haven't consumed the characters with
3055 `display' property yet. The call to pop_it in
3056 set_iterator_to_next will clean this up. */
3057 *position = start_pos;
3058 }
3059 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3060 {
3061 it->method = next_element_from_stretch;
3062 it->object = value;
3063 it->current.pos = it->position = start_pos;
3064 }
3065 #ifdef HAVE_WINDOW_SYSTEM
3066 else
3067 {
3068 it->what = IT_IMAGE;
3069 it->image_id = lookup_image (it->f, value);
3070 it->position = start_pos;
3071 it->object = NILP (object) ? it->w->buffer : object;
3072 it->method = next_element_from_image;
3073
3074 /* Say that we haven't consumed the characters with
3075 `display' property yet. The call to pop_it in
3076 set_iterator_to_next will clean this up. */
3077 *position = start_pos;
3078 }
3079 #endif /* HAVE_WINDOW_SYSTEM */
3080 }
3081 else
3082 /* Invalid property or property not supported. Restore
3083 the position to what it was before. */
3084 *position = start_pos;
3085 }
3086
3087 return replaces_text_display_p;
3088 }
3089
3090
3091 /* Check if PROP is a display sub-property value whose text should be
3092 treated as intangible. */
3093
3094 static int
3095 single_display_prop_intangible_p (prop)
3096 Lisp_Object prop;
3097 {
3098 /* Skip over `when FORM'. */
3099 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3100 {
3101 prop = XCDR (prop);
3102 if (!CONSP (prop))
3103 return 0;
3104 prop = XCDR (prop);
3105 }
3106
3107 if (!CONSP (prop))
3108 return 0;
3109
3110 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3111 we don't need to treat text as intangible. */
3112 if (EQ (XCAR (prop), Qmargin))
3113 {
3114 prop = XCDR (prop);
3115 if (!CONSP (prop))
3116 return 0;
3117
3118 prop = XCDR (prop);
3119 if (!CONSP (prop)
3120 || EQ (XCAR (prop), Qleft_margin)
3121 || EQ (XCAR (prop), Qright_margin))
3122 return 0;
3123 }
3124
3125 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3126 }
3127
3128
3129 /* Check if PROP is a display property value whose text should be
3130 treated as intangible. */
3131
3132 int
3133 display_prop_intangible_p (prop)
3134 Lisp_Object prop;
3135 {
3136 if (CONSP (prop)
3137 && CONSP (XCAR (prop))
3138 && !EQ (Qmargin, XCAR (XCAR (prop))))
3139 {
3140 /* A list of sub-properties. */
3141 while (CONSP (prop))
3142 {
3143 if (single_display_prop_intangible_p (XCAR (prop)))
3144 return 1;
3145 prop = XCDR (prop);
3146 }
3147 }
3148 else if (VECTORP (prop))
3149 {
3150 /* A vector of sub-properties. */
3151 int i;
3152 for (i = 0; i < ASIZE (prop); ++i)
3153 if (single_display_prop_intangible_p (AREF (prop, i)))
3154 return 1;
3155 }
3156 else
3157 return single_display_prop_intangible_p (prop);
3158
3159 return 0;
3160 }
3161
3162
3163 /* Return 1 if PROP is a display sub-property value containing STRING. */
3164
3165 static int
3166 single_display_prop_string_p (prop, string)
3167 Lisp_Object prop, string;
3168 {
3169 if (EQ (string, prop))
3170 return 1;
3171
3172 /* Skip over `when FORM'. */
3173 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3174 {
3175 prop = XCDR (prop);
3176 if (!CONSP (prop))
3177 return 0;
3178 prop = XCDR (prop);
3179 }
3180
3181 if (CONSP (prop))
3182 /* Skip over `margin LOCATION'. */
3183 if (EQ (XCAR (prop), Qmargin))
3184 {
3185 prop = XCDR (prop);
3186 if (!CONSP (prop))
3187 return 0;
3188
3189 prop = XCDR (prop);
3190 if (!CONSP (prop))
3191 return 0;
3192 }
3193
3194 return CONSP (prop) && EQ (XCAR (prop), string);
3195 }
3196
3197
3198 /* Return 1 if STRING appears in the `display' property PROP. */
3199
3200 static int
3201 display_prop_string_p (prop, string)
3202 Lisp_Object prop, string;
3203 {
3204 if (CONSP (prop)
3205 && CONSP (XCAR (prop))
3206 && !EQ (Qmargin, XCAR (XCAR (prop))))
3207 {
3208 /* A list of sub-properties. */
3209 while (CONSP (prop))
3210 {
3211 if (single_display_prop_string_p (XCAR (prop), string))
3212 return 1;
3213 prop = XCDR (prop);
3214 }
3215 }
3216 else if (VECTORP (prop))
3217 {
3218 /* A vector of sub-properties. */
3219 int i;
3220 for (i = 0; i < ASIZE (prop); ++i)
3221 if (single_display_prop_string_p (AREF (prop, i), string))
3222 return 1;
3223 }
3224 else
3225 return single_display_prop_string_p (prop, string);
3226
3227 return 0;
3228 }
3229
3230
3231 /* Determine from which buffer position in W's buffer STRING comes
3232 from. AROUND_CHARPOS is an approximate position where it could
3233 be from. Value is the buffer position or 0 if it couldn't be
3234 determined.
3235
3236 W's buffer must be current.
3237
3238 This function is necessary because we don't record buffer positions
3239 in glyphs generated from strings (to keep struct glyph small).
3240 This function may only use code that doesn't eval because it is
3241 called asynchronously from note_mouse_highlight. */
3242
3243 int
3244 string_buffer_position (w, string, around_charpos)
3245 struct window *w;
3246 Lisp_Object string;
3247 int around_charpos;
3248 {
3249 Lisp_Object limit, prop, pos;
3250 const int MAX_DISTANCE = 1000;
3251 int found = 0;
3252
3253 pos = make_number (around_charpos);
3254 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3255 while (!found && !EQ (pos, limit))
3256 {
3257 prop = Fget_char_property (pos, Qdisplay, Qnil);
3258 if (!NILP (prop) && display_prop_string_p (prop, string))
3259 found = 1;
3260 else
3261 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3262 }
3263
3264 if (!found)
3265 {
3266 pos = make_number (around_charpos);
3267 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3268 while (!found && !EQ (pos, limit))
3269 {
3270 prop = Fget_char_property (pos, Qdisplay, Qnil);
3271 if (!NILP (prop) && display_prop_string_p (prop, string))
3272 found = 1;
3273 else
3274 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3275 limit);
3276 }
3277 }
3278
3279 return found ? XINT (pos) : 0;
3280 }
3281
3282
3283 \f
3284 /***********************************************************************
3285 `composition' property
3286 ***********************************************************************/
3287
3288 /* Set up iterator IT from `composition' property at its current
3289 position. Called from handle_stop. */
3290
3291 static enum prop_handled
3292 handle_composition_prop (it)
3293 struct it *it;
3294 {
3295 Lisp_Object prop, string;
3296 int pos, pos_byte, end;
3297 enum prop_handled handled = HANDLED_NORMALLY;
3298
3299 if (STRINGP (it->string))
3300 {
3301 pos = IT_STRING_CHARPOS (*it);
3302 pos_byte = IT_STRING_BYTEPOS (*it);
3303 string = it->string;
3304 }
3305 else
3306 {
3307 pos = IT_CHARPOS (*it);
3308 pos_byte = IT_BYTEPOS (*it);
3309 string = Qnil;
3310 }
3311
3312 /* If there's a valid composition and point is not inside of the
3313 composition (in the case that the composition is from the current
3314 buffer), draw a glyph composed from the composition components. */
3315 if (find_composition (pos, -1, &pos, &end, &prop, string)
3316 && COMPOSITION_VALID_P (pos, end, prop)
3317 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3318 {
3319 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3320
3321 if (id >= 0)
3322 {
3323 it->method = next_element_from_composition;
3324 it->cmp_id = id;
3325 it->cmp_len = COMPOSITION_LENGTH (prop);
3326 /* For a terminal, draw only the first character of the
3327 components. */
3328 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3329 it->len = (STRINGP (it->string)
3330 ? string_char_to_byte (it->string, end)
3331 : CHAR_TO_BYTE (end)) - pos_byte;
3332 it->stop_charpos = end;
3333 handled = HANDLED_RETURN;
3334 }
3335 }
3336
3337 return handled;
3338 }
3339
3340
3341 \f
3342 /***********************************************************************
3343 Overlay strings
3344 ***********************************************************************/
3345
3346 /* The following structure is used to record overlay strings for
3347 later sorting in load_overlay_strings. */
3348
3349 struct overlay_entry
3350 {
3351 Lisp_Object overlay;
3352 Lisp_Object string;
3353 int priority;
3354 int after_string_p;
3355 };
3356
3357
3358 /* Set up iterator IT from overlay strings at its current position.
3359 Called from handle_stop. */
3360
3361 static enum prop_handled
3362 handle_overlay_change (it)
3363 struct it *it;
3364 {
3365 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3366 return HANDLED_RECOMPUTE_PROPS;
3367 else
3368 return HANDLED_NORMALLY;
3369 }
3370
3371
3372 /* Set up the next overlay string for delivery by IT, if there is an
3373 overlay string to deliver. Called by set_iterator_to_next when the
3374 end of the current overlay string is reached. If there are more
3375 overlay strings to display, IT->string and
3376 IT->current.overlay_string_index are set appropriately here.
3377 Otherwise IT->string is set to nil. */
3378
3379 static void
3380 next_overlay_string (it)
3381 struct it *it;
3382 {
3383 ++it->current.overlay_string_index;
3384 if (it->current.overlay_string_index == it->n_overlay_strings)
3385 {
3386 /* No more overlay strings. Restore IT's settings to what
3387 they were before overlay strings were processed, and
3388 continue to deliver from current_buffer. */
3389 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3390
3391 pop_it (it);
3392 xassert (it->stop_charpos >= BEGV
3393 && it->stop_charpos <= it->end_charpos);
3394 it->string = Qnil;
3395 it->current.overlay_string_index = -1;
3396 SET_TEXT_POS (it->current.string_pos, -1, -1);
3397 it->n_overlay_strings = 0;
3398 it->method = next_element_from_buffer;
3399
3400 /* If we're at the end of the buffer, record that we have
3401 processed the overlay strings there already, so that
3402 next_element_from_buffer doesn't try it again. */
3403 if (IT_CHARPOS (*it) >= it->end_charpos)
3404 it->overlay_strings_at_end_processed_p = 1;
3405
3406 /* If we have to display `...' for invisible text, set
3407 the iterator up for that. */
3408 if (display_ellipsis_p)
3409 setup_for_ellipsis (it);
3410 }
3411 else
3412 {
3413 /* There are more overlay strings to process. If
3414 IT->current.overlay_string_index has advanced to a position
3415 where we must load IT->overlay_strings with more strings, do
3416 it. */
3417 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3418
3419 if (it->current.overlay_string_index && i == 0)
3420 load_overlay_strings (it, 0);
3421
3422 /* Initialize IT to deliver display elements from the overlay
3423 string. */
3424 it->string = it->overlay_strings[i];
3425 it->multibyte_p = STRING_MULTIBYTE (it->string);
3426 SET_TEXT_POS (it->current.string_pos, 0, 0);
3427 it->method = next_element_from_string;
3428 it->stop_charpos = 0;
3429 }
3430
3431 CHECK_IT (it);
3432 }
3433
3434
3435 /* Compare two overlay_entry structures E1 and E2. Used as a
3436 comparison function for qsort in load_overlay_strings. Overlay
3437 strings for the same position are sorted so that
3438
3439 1. All after-strings come in front of before-strings, except
3440 when they come from the same overlay.
3441
3442 2. Within after-strings, strings are sorted so that overlay strings
3443 from overlays with higher priorities come first.
3444
3445 2. Within before-strings, strings are sorted so that overlay
3446 strings from overlays with higher priorities come last.
3447
3448 Value is analogous to strcmp. */
3449
3450
3451 static int
3452 compare_overlay_entries (e1, e2)
3453 void *e1, *e2;
3454 {
3455 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3456 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3457 int result;
3458
3459 if (entry1->after_string_p != entry2->after_string_p)
3460 {
3461 /* Let after-strings appear in front of before-strings if
3462 they come from different overlays. */
3463 if (EQ (entry1->overlay, entry2->overlay))
3464 result = entry1->after_string_p ? 1 : -1;
3465 else
3466 result = entry1->after_string_p ? -1 : 1;
3467 }
3468 else if (entry1->after_string_p)
3469 /* After-strings sorted in order of decreasing priority. */
3470 result = entry2->priority - entry1->priority;
3471 else
3472 /* Before-strings sorted in order of increasing priority. */
3473 result = entry1->priority - entry2->priority;
3474
3475 return result;
3476 }
3477
3478
3479 /* Load the vector IT->overlay_strings with overlay strings from IT's
3480 current buffer position, or from CHARPOS if that is > 0. Set
3481 IT->n_overlays to the total number of overlay strings found.
3482
3483 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3484 a time. On entry into load_overlay_strings,
3485 IT->current.overlay_string_index gives the number of overlay
3486 strings that have already been loaded by previous calls to this
3487 function.
3488
3489 IT->add_overlay_start contains an additional overlay start
3490 position to consider for taking overlay strings from, if non-zero.
3491 This position comes into play when the overlay has an `invisible'
3492 property, and both before and after-strings. When we've skipped to
3493 the end of the overlay, because of its `invisible' property, we
3494 nevertheless want its before-string to appear.
3495 IT->add_overlay_start will contain the overlay start position
3496 in this case.
3497
3498 Overlay strings are sorted so that after-string strings come in
3499 front of before-string strings. Within before and after-strings,
3500 strings are sorted by overlay priority. See also function
3501 compare_overlay_entries. */
3502
3503 static void
3504 load_overlay_strings (it, charpos)
3505 struct it *it;
3506 int charpos;
3507 {
3508 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3509 Lisp_Object ov, overlay, window, str, invisible;
3510 int start, end;
3511 int size = 20;
3512 int n = 0, i, j, invis_p;
3513 struct overlay_entry *entries
3514 = (struct overlay_entry *) alloca (size * sizeof *entries);
3515
3516 if (charpos <= 0)
3517 charpos = IT_CHARPOS (*it);
3518
3519 /* Append the overlay string STRING of overlay OVERLAY to vector
3520 `entries' which has size `size' and currently contains `n'
3521 elements. AFTER_P non-zero means STRING is an after-string of
3522 OVERLAY. */
3523 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3524 do \
3525 { \
3526 Lisp_Object priority; \
3527 \
3528 if (n == size) \
3529 { \
3530 int new_size = 2 * size; \
3531 struct overlay_entry *old = entries; \
3532 entries = \
3533 (struct overlay_entry *) alloca (new_size \
3534 * sizeof *entries); \
3535 bcopy (old, entries, size * sizeof *entries); \
3536 size = new_size; \
3537 } \
3538 \
3539 entries[n].string = (STRING); \
3540 entries[n].overlay = (OVERLAY); \
3541 priority = Foverlay_get ((OVERLAY), Qpriority); \
3542 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3543 entries[n].after_string_p = (AFTER_P); \
3544 ++n; \
3545 } \
3546 while (0)
3547
3548 /* Process overlay before the overlay center. */
3549 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3550 {
3551 overlay = XCAR (ov);
3552 xassert (OVERLAYP (overlay));
3553 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3554 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3555
3556 if (end < charpos)
3557 break;
3558
3559 /* Skip this overlay if it doesn't start or end at IT's current
3560 position. */
3561 if (end != charpos && start != charpos)
3562 continue;
3563
3564 /* Skip this overlay if it doesn't apply to IT->w. */
3565 window = Foverlay_get (overlay, Qwindow);
3566 if (WINDOWP (window) && XWINDOW (window) != it->w)
3567 continue;
3568
3569 /* If the text ``under'' the overlay is invisible, both before-
3570 and after-strings from this overlay are visible; start and
3571 end position are indistinguishable. */
3572 invisible = Foverlay_get (overlay, Qinvisible);
3573 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3574
3575 /* If overlay has a non-empty before-string, record it. */
3576 if ((start == charpos || (end == charpos && invis_p))
3577 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3578 && XSTRING (str)->size)
3579 RECORD_OVERLAY_STRING (overlay, str, 0);
3580
3581 /* If overlay has a non-empty after-string, record it. */
3582 if ((end == charpos || (start == charpos && invis_p))
3583 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3584 && XSTRING (str)->size)
3585 RECORD_OVERLAY_STRING (overlay, str, 1);
3586 }
3587
3588 /* Process overlays after the overlay center. */
3589 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3590 {
3591 overlay = XCAR (ov);
3592 xassert (OVERLAYP (overlay));
3593 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3594 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3595
3596 if (start > charpos)
3597 break;
3598
3599 /* Skip this overlay if it doesn't start or end at IT's current
3600 position. */
3601 if (end != charpos && start != charpos)
3602 continue;
3603
3604 /* Skip this overlay if it doesn't apply to IT->w. */
3605 window = Foverlay_get (overlay, Qwindow);
3606 if (WINDOWP (window) && XWINDOW (window) != it->w)
3607 continue;
3608
3609 /* If the text ``under'' the overlay is invisible, it has a zero
3610 dimension, and both before- and after-strings apply. */
3611 invisible = Foverlay_get (overlay, Qinvisible);
3612 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3613
3614 /* If overlay has a non-empty before-string, record it. */
3615 if ((start == charpos || (end == charpos && invis_p))
3616 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3617 && XSTRING (str)->size)
3618 RECORD_OVERLAY_STRING (overlay, str, 0);
3619
3620 /* If overlay has a non-empty after-string, record it. */
3621 if ((end == charpos || (start == charpos && invis_p))
3622 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3623 && XSTRING (str)->size)
3624 RECORD_OVERLAY_STRING (overlay, str, 1);
3625 }
3626
3627 #undef RECORD_OVERLAY_STRING
3628
3629 /* Sort entries. */
3630 if (n > 1)
3631 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3632
3633 /* Record the total number of strings to process. */
3634 it->n_overlay_strings = n;
3635
3636 /* IT->current.overlay_string_index is the number of overlay strings
3637 that have already been consumed by IT. Copy some of the
3638 remaining overlay strings to IT->overlay_strings. */
3639 i = 0;
3640 j = it->current.overlay_string_index;
3641 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3642 it->overlay_strings[i++] = entries[j++].string;
3643
3644 CHECK_IT (it);
3645 }
3646
3647
3648 /* Get the first chunk of overlay strings at IT's current buffer
3649 position, or at CHARPOS if that is > 0. Value is non-zero if at
3650 least one overlay string was found. */
3651
3652 static int
3653 get_overlay_strings (it, charpos)
3654 struct it *it;
3655 int charpos;
3656 {
3657 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3658 process. This fills IT->overlay_strings with strings, and sets
3659 IT->n_overlay_strings to the total number of strings to process.
3660 IT->pos.overlay_string_index has to be set temporarily to zero
3661 because load_overlay_strings needs this; it must be set to -1
3662 when no overlay strings are found because a zero value would
3663 indicate a position in the first overlay string. */
3664 it->current.overlay_string_index = 0;
3665 load_overlay_strings (it, charpos);
3666
3667 /* If we found overlay strings, set up IT to deliver display
3668 elements from the first one. Otherwise set up IT to deliver
3669 from current_buffer. */
3670 if (it->n_overlay_strings)
3671 {
3672 /* Make sure we know settings in current_buffer, so that we can
3673 restore meaningful values when we're done with the overlay
3674 strings. */
3675 compute_stop_pos (it);
3676 xassert (it->face_id >= 0);
3677
3678 /* Save IT's settings. They are restored after all overlay
3679 strings have been processed. */
3680 xassert (it->sp == 0);
3681 push_it (it);
3682
3683 /* Set up IT to deliver display elements from the first overlay
3684 string. */
3685 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3686 it->string = it->overlay_strings[0];
3687 it->stop_charpos = 0;
3688 it->end_charpos = XSTRING (it->string)->size;
3689 it->multibyte_p = STRING_MULTIBYTE (it->string);
3690 xassert (STRINGP (it->string));
3691 it->method = next_element_from_string;
3692 }
3693 else
3694 {
3695 it->string = Qnil;
3696 it->current.overlay_string_index = -1;
3697 it->method = next_element_from_buffer;
3698 }
3699
3700 CHECK_IT (it);
3701
3702 /* Value is non-zero if we found at least one overlay string. */
3703 return STRINGP (it->string);
3704 }
3705
3706
3707 \f
3708 /***********************************************************************
3709 Saving and restoring state
3710 ***********************************************************************/
3711
3712 /* Save current settings of IT on IT->stack. Called, for example,
3713 before setting up IT for an overlay string, to be able to restore
3714 IT's settings to what they were after the overlay string has been
3715 processed. */
3716
3717 static void
3718 push_it (it)
3719 struct it *it;
3720 {
3721 struct iterator_stack_entry *p;
3722
3723 xassert (it->sp < 2);
3724 p = it->stack + it->sp;
3725
3726 p->stop_charpos = it->stop_charpos;
3727 xassert (it->face_id >= 0);
3728 p->face_id = it->face_id;
3729 p->string = it->string;
3730 p->pos = it->current;
3731 p->end_charpos = it->end_charpos;
3732 p->string_nchars = it->string_nchars;
3733 p->area = it->area;
3734 p->multibyte_p = it->multibyte_p;
3735 p->space_width = it->space_width;
3736 p->font_height = it->font_height;
3737 p->voffset = it->voffset;
3738 p->string_from_display_prop_p = it->string_from_display_prop_p;
3739 p->display_ellipsis_p = 0;
3740 ++it->sp;
3741 }
3742
3743
3744 /* Restore IT's settings from IT->stack. Called, for example, when no
3745 more overlay strings must be processed, and we return to delivering
3746 display elements from a buffer, or when the end of a string from a
3747 `display' property is reached and we return to delivering display
3748 elements from an overlay string, or from a buffer. */
3749
3750 static void
3751 pop_it (it)
3752 struct it *it;
3753 {
3754 struct iterator_stack_entry *p;
3755
3756 xassert (it->sp > 0);
3757 --it->sp;
3758 p = it->stack + it->sp;
3759 it->stop_charpos = p->stop_charpos;
3760 it->face_id = p->face_id;
3761 it->string = p->string;
3762 it->current = p->pos;
3763 it->end_charpos = p->end_charpos;
3764 it->string_nchars = p->string_nchars;
3765 it->area = p->area;
3766 it->multibyte_p = p->multibyte_p;
3767 it->space_width = p->space_width;
3768 it->font_height = p->font_height;
3769 it->voffset = p->voffset;
3770 it->string_from_display_prop_p = p->string_from_display_prop_p;
3771 }
3772
3773
3774 \f
3775 /***********************************************************************
3776 Moving over lines
3777 ***********************************************************************/
3778
3779 /* Set IT's current position to the previous line start. */
3780
3781 static void
3782 back_to_previous_line_start (it)
3783 struct it *it;
3784 {
3785 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3786 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3787 }
3788
3789
3790 /* Move IT to the next line start.
3791
3792 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3793 we skipped over part of the text (as opposed to moving the iterator
3794 continuously over the text). Otherwise, don't change the value
3795 of *SKIPPED_P.
3796
3797 Newlines may come from buffer text, overlay strings, or strings
3798 displayed via the `display' property. That's the reason we can't
3799 simply use find_next_newline_no_quit.
3800
3801 Note that this function may not skip over invisible text that is so
3802 because of text properties and immediately follows a newline. If
3803 it would, function reseat_at_next_visible_line_start, when called
3804 from set_iterator_to_next, would effectively make invisible
3805 characters following a newline part of the wrong glyph row, which
3806 leads to wrong cursor motion. */
3807
3808 static int
3809 forward_to_next_line_start (it, skipped_p)
3810 struct it *it;
3811 int *skipped_p;
3812 {
3813 int old_selective, newline_found_p, n;
3814 const int MAX_NEWLINE_DISTANCE = 500;
3815
3816 /* If already on a newline, just consume it to avoid unintended
3817 skipping over invisible text below. */
3818 if (it->what == IT_CHARACTER
3819 && it->c == '\n'
3820 && CHARPOS (it->position) == IT_CHARPOS (*it))
3821 {
3822 set_iterator_to_next (it, 0);
3823 it->c = 0;
3824 return 1;
3825 }
3826
3827 /* Don't handle selective display in the following. It's (a)
3828 unnecessary because it's done by the caller, and (b) leads to an
3829 infinite recursion because next_element_from_ellipsis indirectly
3830 calls this function. */
3831 old_selective = it->selective;
3832 it->selective = 0;
3833
3834 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3835 from buffer text. */
3836 for (n = newline_found_p = 0;
3837 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3838 n += STRINGP (it->string) ? 0 : 1)
3839 {
3840 if (!get_next_display_element (it))
3841 break;
3842 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3843 set_iterator_to_next (it, 0);
3844 }
3845
3846 /* If we didn't find a newline near enough, see if we can use a
3847 short-cut. */
3848 if (n == MAX_NEWLINE_DISTANCE)
3849 {
3850 int start = IT_CHARPOS (*it);
3851 int limit = find_next_newline_no_quit (start, 1);
3852 Lisp_Object pos;
3853
3854 xassert (!STRINGP (it->string));
3855
3856 /* If there isn't any `display' property in sight, and no
3857 overlays, we can just use the position of the newline in
3858 buffer text. */
3859 if (it->stop_charpos >= limit
3860 || ((pos = Fnext_single_property_change (make_number (start),
3861 Qdisplay,
3862 Qnil, make_number (limit)),
3863 NILP (pos))
3864 && next_overlay_change (start) == ZV))
3865 {
3866 IT_CHARPOS (*it) = limit;
3867 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3868 *skipped_p = newline_found_p = 1;
3869 }
3870 else
3871 {
3872 while (get_next_display_element (it)
3873 && !newline_found_p)
3874 {
3875 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3876 set_iterator_to_next (it, 0);
3877 }
3878 }
3879 }
3880
3881 it->selective = old_selective;
3882 return newline_found_p;
3883 }
3884
3885
3886 /* Set IT's current position to the previous visible line start. Skip
3887 invisible text that is so either due to text properties or due to
3888 selective display. Caution: this does not change IT->current_x and
3889 IT->hpos. */
3890
3891 static void
3892 back_to_previous_visible_line_start (it)
3893 struct it *it;
3894 {
3895 int visible_p = 0;
3896
3897 /* Go back one newline if not on BEGV already. */
3898 if (IT_CHARPOS (*it) > BEGV)
3899 back_to_previous_line_start (it);
3900
3901 /* Move over lines that are invisible because of selective display
3902 or text properties. */
3903 while (IT_CHARPOS (*it) > BEGV
3904 && !visible_p)
3905 {
3906 visible_p = 1;
3907
3908 /* If selective > 0, then lines indented more than that values
3909 are invisible. */
3910 if (it->selective > 0
3911 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3912 it->selective))
3913 visible_p = 0;
3914 else
3915 {
3916 Lisp_Object prop;
3917
3918 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3919 Qinvisible, it->window);
3920 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3921 visible_p = 0;
3922 }
3923
3924 /* Back one more newline if the current one is invisible. */
3925 if (!visible_p)
3926 back_to_previous_line_start (it);
3927 }
3928
3929 xassert (IT_CHARPOS (*it) >= BEGV);
3930 xassert (IT_CHARPOS (*it) == BEGV
3931 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3932 CHECK_IT (it);
3933 }
3934
3935
3936 /* Reseat iterator IT at the previous visible line start. Skip
3937 invisible text that is so either due to text properties or due to
3938 selective display. At the end, update IT's overlay information,
3939 face information etc. */
3940
3941 static void
3942 reseat_at_previous_visible_line_start (it)
3943 struct it *it;
3944 {
3945 back_to_previous_visible_line_start (it);
3946 reseat (it, it->current.pos, 1);
3947 CHECK_IT (it);
3948 }
3949
3950
3951 /* Reseat iterator IT on the next visible line start in the current
3952 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3953 preceding the line start. Skip over invisible text that is so
3954 because of selective display. Compute faces, overlays etc at the
3955 new position. Note that this function does not skip over text that
3956 is invisible because of text properties. */
3957
3958 static void
3959 reseat_at_next_visible_line_start (it, on_newline_p)
3960 struct it *it;
3961 int on_newline_p;
3962 {
3963 int newline_found_p, skipped_p = 0;
3964
3965 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3966
3967 /* Skip over lines that are invisible because they are indented
3968 more than the value of IT->selective. */
3969 if (it->selective > 0)
3970 while (IT_CHARPOS (*it) < ZV
3971 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3972 it->selective))
3973 {
3974 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3975 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3976 }
3977
3978 /* Position on the newline if that's what's requested. */
3979 if (on_newline_p && newline_found_p)
3980 {
3981 if (STRINGP (it->string))
3982 {
3983 if (IT_STRING_CHARPOS (*it) > 0)
3984 {
3985 --IT_STRING_CHARPOS (*it);
3986 --IT_STRING_BYTEPOS (*it);
3987 }
3988 }
3989 else if (IT_CHARPOS (*it) > BEGV)
3990 {
3991 --IT_CHARPOS (*it);
3992 --IT_BYTEPOS (*it);
3993 reseat (it, it->current.pos, 0);
3994 }
3995 }
3996 else if (skipped_p)
3997 reseat (it, it->current.pos, 0);
3998
3999 CHECK_IT (it);
4000 }
4001
4002
4003 \f
4004 /***********************************************************************
4005 Changing an iterator's position
4006 ***********************************************************************/
4007
4008 /* Change IT's current position to POS in current_buffer. If FORCE_P
4009 is non-zero, always check for text properties at the new position.
4010 Otherwise, text properties are only looked up if POS >=
4011 IT->check_charpos of a property. */
4012
4013 static void
4014 reseat (it, pos, force_p)
4015 struct it *it;
4016 struct text_pos pos;
4017 int force_p;
4018 {
4019 int original_pos = IT_CHARPOS (*it);
4020
4021 reseat_1 (it, pos, 0);
4022
4023 /* Determine where to check text properties. Avoid doing it
4024 where possible because text property lookup is very expensive. */
4025 if (force_p
4026 || CHARPOS (pos) > it->stop_charpos
4027 || CHARPOS (pos) < original_pos)
4028 handle_stop (it);
4029
4030 CHECK_IT (it);
4031 }
4032
4033
4034 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4035 IT->stop_pos to POS, also. */
4036
4037 static void
4038 reseat_1 (it, pos, set_stop_p)
4039 struct it *it;
4040 struct text_pos pos;
4041 int set_stop_p;
4042 {
4043 /* Don't call this function when scanning a C string. */
4044 xassert (it->s == NULL);
4045
4046 /* POS must be a reasonable value. */
4047 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4048
4049 it->current.pos = it->position = pos;
4050 XSETBUFFER (it->object, current_buffer);
4051 it->end_charpos = ZV;
4052 it->dpvec = NULL;
4053 it->current.dpvec_index = -1;
4054 it->current.overlay_string_index = -1;
4055 IT_STRING_CHARPOS (*it) = -1;
4056 IT_STRING_BYTEPOS (*it) = -1;
4057 it->string = Qnil;
4058 it->method = next_element_from_buffer;
4059 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4060 it->sp = 0;
4061 it->face_before_selective_p = 0;
4062
4063 if (set_stop_p)
4064 it->stop_charpos = CHARPOS (pos);
4065 }
4066
4067
4068 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4069 If S is non-null, it is a C string to iterate over. Otherwise,
4070 STRING gives a Lisp string to iterate over.
4071
4072 If PRECISION > 0, don't return more then PRECISION number of
4073 characters from the string.
4074
4075 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4076 characters have been returned. FIELD_WIDTH < 0 means an infinite
4077 field width.
4078
4079 MULTIBYTE = 0 means disable processing of multibyte characters,
4080 MULTIBYTE > 0 means enable it,
4081 MULTIBYTE < 0 means use IT->multibyte_p.
4082
4083 IT must be initialized via a prior call to init_iterator before
4084 calling this function. */
4085
4086 static void
4087 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4088 struct it *it;
4089 unsigned char *s;
4090 Lisp_Object string;
4091 int charpos;
4092 int precision, field_width, multibyte;
4093 {
4094 /* No region in strings. */
4095 it->region_beg_charpos = it->region_end_charpos = -1;
4096
4097 /* No text property checks performed by default, but see below. */
4098 it->stop_charpos = -1;
4099
4100 /* Set iterator position and end position. */
4101 bzero (&it->current, sizeof it->current);
4102 it->current.overlay_string_index = -1;
4103 it->current.dpvec_index = -1;
4104 xassert (charpos >= 0);
4105
4106 /* If STRING is specified, use its multibyteness, otherwise use the
4107 setting of MULTIBYTE, if specified. */
4108 if (multibyte >= 0)
4109 it->multibyte_p = multibyte > 0;
4110
4111 if (s == NULL)
4112 {
4113 xassert (STRINGP (string));
4114 it->string = string;
4115 it->s = NULL;
4116 it->end_charpos = it->string_nchars = XSTRING (string)->size;
4117 it->method = next_element_from_string;
4118 it->current.string_pos = string_pos (charpos, string);
4119 }
4120 else
4121 {
4122 it->s = s;
4123 it->string = Qnil;
4124
4125 /* Note that we use IT->current.pos, not it->current.string_pos,
4126 for displaying C strings. */
4127 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4128 if (it->multibyte_p)
4129 {
4130 it->current.pos = c_string_pos (charpos, s, 1);
4131 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4132 }
4133 else
4134 {
4135 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4136 it->end_charpos = it->string_nchars = strlen (s);
4137 }
4138
4139 it->method = next_element_from_c_string;
4140 }
4141
4142 /* PRECISION > 0 means don't return more than PRECISION characters
4143 from the string. */
4144 if (precision > 0 && it->end_charpos - charpos > precision)
4145 it->end_charpos = it->string_nchars = charpos + precision;
4146
4147 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4148 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4149 FIELD_WIDTH < 0 means infinite field width. This is useful for
4150 padding with `-' at the end of a mode line. */
4151 if (field_width < 0)
4152 field_width = INFINITY;
4153 if (field_width > it->end_charpos - charpos)
4154 it->end_charpos = charpos + field_width;
4155
4156 /* Use the standard display table for displaying strings. */
4157 if (DISP_TABLE_P (Vstandard_display_table))
4158 it->dp = XCHAR_TABLE (Vstandard_display_table);
4159
4160 it->stop_charpos = charpos;
4161 CHECK_IT (it);
4162 }
4163
4164
4165 \f
4166 /***********************************************************************
4167 Iteration
4168 ***********************************************************************/
4169
4170 /* Load IT's display element fields with information about the next
4171 display element from the current position of IT. Value is zero if
4172 end of buffer (or C string) is reached. */
4173
4174 int
4175 get_next_display_element (it)
4176 struct it *it;
4177 {
4178 /* Non-zero means that we found an display element. Zero means that
4179 we hit the end of what we iterate over. Performance note: the
4180 function pointer `method' used here turns out to be faster than
4181 using a sequence of if-statements. */
4182 int success_p = (*it->method) (it);
4183
4184 if (it->what == IT_CHARACTER)
4185 {
4186 /* Map via display table or translate control characters.
4187 IT->c, IT->len etc. have been set to the next character by
4188 the function call above. If we have a display table, and it
4189 contains an entry for IT->c, translate it. Don't do this if
4190 IT->c itself comes from a display table, otherwise we could
4191 end up in an infinite recursion. (An alternative could be to
4192 count the recursion depth of this function and signal an
4193 error when a certain maximum depth is reached.) Is it worth
4194 it? */
4195 if (success_p && it->dpvec == NULL)
4196 {
4197 Lisp_Object dv;
4198
4199 if (it->dp
4200 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4201 VECTORP (dv)))
4202 {
4203 struct Lisp_Vector *v = XVECTOR (dv);
4204
4205 /* Return the first character from the display table
4206 entry, if not empty. If empty, don't display the
4207 current character. */
4208 if (v->size)
4209 {
4210 it->dpvec_char_len = it->len;
4211 it->dpvec = v->contents;
4212 it->dpend = v->contents + v->size;
4213 it->current.dpvec_index = 0;
4214 it->method = next_element_from_display_vector;
4215 success_p = get_next_display_element (it);
4216 }
4217 else
4218 {
4219 set_iterator_to_next (it, 0);
4220 success_p = get_next_display_element (it);
4221 }
4222 }
4223
4224 /* Translate control characters into `\003' or `^C' form.
4225 Control characters coming from a display table entry are
4226 currently not translated because we use IT->dpvec to hold
4227 the translation. This could easily be changed but I
4228 don't believe that it is worth doing.
4229
4230 Non-printable multibyte characters are also translated
4231 octal form. */
4232 else if ((it->c < ' '
4233 && (it->area != TEXT_AREA
4234 || (it->c != '\n' && it->c != '\t')))
4235 || (it->multibyte_p ? CHAR_BYTE8_P (it->c) : it->c >= 127)
4236 || (it->c != '\n' && it->c != '\t'
4237 && !CHAR_PRINTABLE_P (it->c)))
4238 {
4239 /* IT->c is a control character which must be displayed
4240 either as '\003' or as `^C' where the '\\' and '^'
4241 can be defined in the display table. Fill
4242 IT->ctl_chars with glyphs for what we have to
4243 display. Then, set IT->dpvec to these glyphs. */
4244 GLYPH g;
4245
4246 if (it->c < 128 && it->ctl_arrow_p)
4247 {
4248 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4249 if (it->dp
4250 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4251 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4252 g = XINT (DISP_CTRL_GLYPH (it->dp));
4253 else
4254 g = FAST_MAKE_GLYPH ('^', 0);
4255 XSETINT (it->ctl_chars[0], g);
4256
4257 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4258 XSETINT (it->ctl_chars[1], g);
4259
4260 /* Set up IT->dpvec and return first character from it. */
4261 it->dpvec_char_len = it->len;
4262 it->dpvec = it->ctl_chars;
4263 it->dpend = it->dpvec + 2;
4264 it->current.dpvec_index = 0;
4265 it->method = next_element_from_display_vector;
4266 get_next_display_element (it);
4267 }
4268 else
4269 {
4270 unsigned char str[MAX_MULTIBYTE_LENGTH];
4271 int len;
4272 int i;
4273 GLYPH escape_glyph;
4274
4275 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4276 if (it->dp
4277 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4278 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4279 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4280 else
4281 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4282
4283 if (CHAR_BYTE8_P (it->c))
4284 {
4285 str[0] = CHAR_TO_BYTE8 (it->c);
4286 len = 1;
4287 }
4288 else if (it->c < 256)
4289 {
4290 str[0] = it->c;
4291 len = 1;
4292 }
4293 else
4294 {
4295 /* It's an invalid character, which
4296 shouldn't happen actually, but due to
4297 bugs it may happen. Let's print the char
4298 as is, there's not much meaningful we can
4299 do with it. */
4300 str[0] = it->c;
4301 str[1] = it->c >> 8;
4302 str[2] = it->c >> 16;
4303 str[3] = it->c >> 24;
4304 len = 4;
4305 }
4306
4307 for (i = 0; i < len; i++)
4308 {
4309 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4310 /* Insert three more glyphs into IT->ctl_chars for
4311 the octal display of the character. */
4312 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4313 XSETINT (it->ctl_chars[i * 4 + 1], g);
4314 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4315 XSETINT (it->ctl_chars[i * 4 + 2], g);
4316 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4317 XSETINT (it->ctl_chars[i * 4 + 3], g);
4318 }
4319
4320 /* Set up IT->dpvec and return the first character
4321 from it. */
4322 it->dpvec_char_len = it->len;
4323 it->dpvec = it->ctl_chars;
4324 it->dpend = it->dpvec + len * 4;
4325 it->current.dpvec_index = 0;
4326 it->method = next_element_from_display_vector;
4327 get_next_display_element (it);
4328 }
4329 }
4330 }
4331
4332 /* Adjust face id for a multibyte character. There are no
4333 multibyte character in unibyte text. */
4334 if (it->multibyte_p
4335 && success_p
4336 && FRAME_WINDOW_P (it->f))
4337 {
4338 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4339 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4340 }
4341 }
4342
4343 /* Is this character the last one of a run of characters with
4344 box? If yes, set IT->end_of_box_run_p to 1. */
4345 if (it->face_box_p
4346 && it->s == NULL)
4347 {
4348 int face_id;
4349 struct face *face;
4350
4351 it->end_of_box_run_p
4352 = ((face_id = face_after_it_pos (it),
4353 face_id != it->face_id)
4354 && (face = FACE_FROM_ID (it->f, face_id),
4355 face->box == FACE_NO_BOX));
4356 }
4357
4358 /* Value is 0 if end of buffer or string reached. */
4359 return success_p;
4360 }
4361
4362
4363 /* Move IT to the next display element.
4364
4365 RESEAT_P non-zero means if called on a newline in buffer text,
4366 skip to the next visible line start.
4367
4368 Functions get_next_display_element and set_iterator_to_next are
4369 separate because I find this arrangement easier to handle than a
4370 get_next_display_element function that also increments IT's
4371 position. The way it is we can first look at an iterator's current
4372 display element, decide whether it fits on a line, and if it does,
4373 increment the iterator position. The other way around we probably
4374 would either need a flag indicating whether the iterator has to be
4375 incremented the next time, or we would have to implement a
4376 decrement position function which would not be easy to write. */
4377
4378 void
4379 set_iterator_to_next (it, reseat_p)
4380 struct it *it;
4381 int reseat_p;
4382 {
4383 /* Reset flags indicating start and end of a sequence of characters
4384 with box. Reset them at the start of this function because
4385 moving the iterator to a new position might set them. */
4386 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4387
4388 if (it->method == next_element_from_buffer)
4389 {
4390 /* The current display element of IT is a character from
4391 current_buffer. Advance in the buffer, and maybe skip over
4392 invisible lines that are so because of selective display. */
4393 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4394 reseat_at_next_visible_line_start (it, 0);
4395 else
4396 {
4397 xassert (it->len != 0);
4398 IT_BYTEPOS (*it) += it->len;
4399 IT_CHARPOS (*it) += 1;
4400 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4401 }
4402 }
4403 else if (it->method == next_element_from_composition)
4404 {
4405 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4406 if (STRINGP (it->string))
4407 {
4408 IT_STRING_BYTEPOS (*it) += it->len;
4409 IT_STRING_CHARPOS (*it) += it->cmp_len;
4410 it->method = next_element_from_string;
4411 goto consider_string_end;
4412 }
4413 else
4414 {
4415 IT_BYTEPOS (*it) += it->len;
4416 IT_CHARPOS (*it) += it->cmp_len;
4417 it->method = next_element_from_buffer;
4418 }
4419 }
4420 else if (it->method == next_element_from_c_string)
4421 {
4422 /* Current display element of IT is from a C string. */
4423 IT_BYTEPOS (*it) += it->len;
4424 IT_CHARPOS (*it) += 1;
4425 }
4426 else if (it->method == next_element_from_display_vector)
4427 {
4428 /* Current display element of IT is from a display table entry.
4429 Advance in the display table definition. Reset it to null if
4430 end reached, and continue with characters from buffers/
4431 strings. */
4432 ++it->current.dpvec_index;
4433
4434 /* Restore face of the iterator to what they were before the
4435 display vector entry (these entries may contain faces). */
4436 it->face_id = it->saved_face_id;
4437
4438 if (it->dpvec + it->current.dpvec_index == it->dpend)
4439 {
4440 if (it->s)
4441 it->method = next_element_from_c_string;
4442 else if (STRINGP (it->string))
4443 it->method = next_element_from_string;
4444 else
4445 it->method = next_element_from_buffer;
4446
4447 it->dpvec = NULL;
4448 it->current.dpvec_index = -1;
4449
4450 /* Skip over characters which were displayed via IT->dpvec. */
4451 if (it->dpvec_char_len < 0)
4452 reseat_at_next_visible_line_start (it, 1);
4453 else if (it->dpvec_char_len > 0)
4454 {
4455 it->len = it->dpvec_char_len;
4456 set_iterator_to_next (it, reseat_p);
4457 }
4458 }
4459 }
4460 else if (it->method == next_element_from_string)
4461 {
4462 /* Current display element is a character from a Lisp string. */
4463 xassert (it->s == NULL && STRINGP (it->string));
4464 IT_STRING_BYTEPOS (*it) += it->len;
4465 IT_STRING_CHARPOS (*it) += 1;
4466
4467 consider_string_end:
4468
4469 if (it->current.overlay_string_index >= 0)
4470 {
4471 /* IT->string is an overlay string. Advance to the
4472 next, if there is one. */
4473 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4474 next_overlay_string (it);
4475 }
4476 else
4477 {
4478 /* IT->string is not an overlay string. If we reached
4479 its end, and there is something on IT->stack, proceed
4480 with what is on the stack. This can be either another
4481 string, this time an overlay string, or a buffer. */
4482 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4483 && it->sp > 0)
4484 {
4485 pop_it (it);
4486 if (!STRINGP (it->string))
4487 it->method = next_element_from_buffer;
4488 else
4489 goto consider_string_end;
4490 }
4491 }
4492 }
4493 else if (it->method == next_element_from_image
4494 || it->method == next_element_from_stretch)
4495 {
4496 /* The position etc with which we have to proceed are on
4497 the stack. The position may be at the end of a string,
4498 if the `display' property takes up the whole string. */
4499 pop_it (it);
4500 it->image_id = 0;
4501 if (STRINGP (it->string))
4502 {
4503 it->method = next_element_from_string;
4504 goto consider_string_end;
4505 }
4506 else
4507 it->method = next_element_from_buffer;
4508 }
4509 else
4510 /* There are no other methods defined, so this should be a bug. */
4511 abort ();
4512
4513 xassert (it->method != next_element_from_string
4514 || (STRINGP (it->string)
4515 && IT_STRING_CHARPOS (*it) >= 0));
4516 }
4517
4518
4519 /* Load IT's display element fields with information about the next
4520 display element which comes from a display table entry or from the
4521 result of translating a control character to one of the forms `^C'
4522 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4523
4524 static int
4525 next_element_from_display_vector (it)
4526 struct it *it;
4527 {
4528 /* Precondition. */
4529 xassert (it->dpvec && it->current.dpvec_index >= 0);
4530
4531 /* Remember the current face id in case glyphs specify faces.
4532 IT's face is restored in set_iterator_to_next. */
4533 it->saved_face_id = it->face_id;
4534
4535 if (INTEGERP (*it->dpvec)
4536 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4537 {
4538 int lface_id;
4539 GLYPH g;
4540
4541 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4542 it->c = FAST_GLYPH_CHAR (g);
4543 it->len = CHAR_BYTES (it->c);
4544
4545 /* The entry may contain a face id to use. Such a face id is
4546 the id of a Lisp face, not a realized face. A face id of
4547 zero means no face is specified. */
4548 lface_id = FAST_GLYPH_FACE (g);
4549 if (lface_id)
4550 {
4551 /* The function returns -1 if lface_id is invalid. */
4552 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4553 if (face_id >= 0)
4554 it->face_id = face_id;
4555 }
4556 }
4557 else
4558 /* Display table entry is invalid. Return a space. */
4559 it->c = ' ', it->len = 1;
4560
4561 /* Don't change position and object of the iterator here. They are
4562 still the values of the character that had this display table
4563 entry or was translated, and that's what we want. */
4564 it->what = IT_CHARACTER;
4565 return 1;
4566 }
4567
4568
4569 /* Load IT with the next display element from Lisp string IT->string.
4570 IT->current.string_pos is the current position within the string.
4571 If IT->current.overlay_string_index >= 0, the Lisp string is an
4572 overlay string. */
4573
4574 static int
4575 next_element_from_string (it)
4576 struct it *it;
4577 {
4578 struct text_pos position;
4579
4580 xassert (STRINGP (it->string));
4581 xassert (IT_STRING_CHARPOS (*it) >= 0);
4582 position = it->current.string_pos;
4583
4584 /* Time to check for invisible text? */
4585 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4586 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4587 {
4588 handle_stop (it);
4589
4590 /* Since a handler may have changed IT->method, we must
4591 recurse here. */
4592 return get_next_display_element (it);
4593 }
4594
4595 if (it->current.overlay_string_index >= 0)
4596 {
4597 /* Get the next character from an overlay string. In overlay
4598 strings, There is no field width or padding with spaces to
4599 do. */
4600 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4601 {
4602 it->what = IT_EOB;
4603 return 0;
4604 }
4605 else if (STRING_MULTIBYTE (it->string))
4606 {
4607 int remaining = (STRING_BYTES (XSTRING (it->string))
4608 - IT_STRING_BYTEPOS (*it));
4609 unsigned char *s = (XSTRING (it->string)->data
4610 + IT_STRING_BYTEPOS (*it));
4611 it->c = string_char_and_length (s, remaining, &it->len);
4612 }
4613 else
4614 {
4615 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4616 it->len = 1;
4617 }
4618 }
4619 else
4620 {
4621 /* Get the next character from a Lisp string that is not an
4622 overlay string. Such strings come from the mode line, for
4623 example. We may have to pad with spaces, or truncate the
4624 string. See also next_element_from_c_string. */
4625 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4626 {
4627 it->what = IT_EOB;
4628 return 0;
4629 }
4630 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4631 {
4632 /* Pad with spaces. */
4633 it->c = ' ', it->len = 1;
4634 CHARPOS (position) = BYTEPOS (position) = -1;
4635 }
4636 else if (STRING_MULTIBYTE (it->string))
4637 {
4638 int maxlen = (STRING_BYTES (XSTRING (it->string))
4639 - IT_STRING_BYTEPOS (*it));
4640 unsigned char *s = (XSTRING (it->string)->data
4641 + IT_STRING_BYTEPOS (*it));
4642 it->c = string_char_and_length (s, maxlen, &it->len);
4643 }
4644 else
4645 {
4646 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4647 it->len = 1;
4648 }
4649 }
4650
4651 /* Record what we have and where it came from. Note that we store a
4652 buffer position in IT->position although it could arguably be a
4653 string position. */
4654 it->what = IT_CHARACTER;
4655 it->object = it->string;
4656 it->position = position;
4657 return 1;
4658 }
4659
4660
4661 /* Load IT with next display element from C string IT->s.
4662 IT->string_nchars is the maximum number of characters to return
4663 from the string. IT->end_charpos may be greater than
4664 IT->string_nchars when this function is called, in which case we
4665 may have to return padding spaces. Value is zero if end of string
4666 reached, including padding spaces. */
4667
4668 static int
4669 next_element_from_c_string (it)
4670 struct it *it;
4671 {
4672 int success_p = 1;
4673
4674 xassert (it->s);
4675 it->what = IT_CHARACTER;
4676 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4677 it->object = Qnil;
4678
4679 /* IT's position can be greater IT->string_nchars in case a field
4680 width or precision has been specified when the iterator was
4681 initialized. */
4682 if (IT_CHARPOS (*it) >= it->end_charpos)
4683 {
4684 /* End of the game. */
4685 it->what = IT_EOB;
4686 success_p = 0;
4687 }
4688 else if (IT_CHARPOS (*it) >= it->string_nchars)
4689 {
4690 /* Pad with spaces. */
4691 it->c = ' ', it->len = 1;
4692 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4693 }
4694 else if (it->multibyte_p)
4695 {
4696 /* Implementation note: The calls to strlen apparently aren't a
4697 performance problem because there is no noticeable performance
4698 difference between Emacs running in unibyte or multibyte mode. */
4699 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4700 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4701 maxlen, &it->len);
4702 }
4703 else
4704 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4705
4706 return success_p;
4707 }
4708
4709
4710 /* Set up IT to return characters from an ellipsis, if appropriate.
4711 The definition of the ellipsis glyphs may come from a display table
4712 entry. This function Fills IT with the first glyph from the
4713 ellipsis if an ellipsis is to be displayed. */
4714
4715 static int
4716 next_element_from_ellipsis (it)
4717 struct it *it;
4718 {
4719 if (it->selective_display_ellipsis_p)
4720 {
4721 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4722 {
4723 /* Use the display table definition for `...'. Invalid glyphs
4724 will be handled by the method returning elements from dpvec. */
4725 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4726 it->dpvec_char_len = it->len;
4727 it->dpvec = v->contents;
4728 it->dpend = v->contents + v->size;
4729 it->current.dpvec_index = 0;
4730 it->method = next_element_from_display_vector;
4731 }
4732 else
4733 {
4734 /* Use default `...' which is stored in default_invis_vector. */
4735 it->dpvec_char_len = it->len;
4736 it->dpvec = default_invis_vector;
4737 it->dpend = default_invis_vector + 3;
4738 it->current.dpvec_index = 0;
4739 it->method = next_element_from_display_vector;
4740 }
4741 }
4742 else
4743 {
4744 /* The face at the current position may be different from the
4745 face we find after the invisible text. Remember what it
4746 was in IT->saved_face_id, and signal that it's there by
4747 setting face_before_selective_p. */
4748 it->saved_face_id = it->face_id;
4749 it->method = next_element_from_buffer;
4750 reseat_at_next_visible_line_start (it, 1);
4751 it->face_before_selective_p = 1;
4752 }
4753
4754 return get_next_display_element (it);
4755 }
4756
4757
4758 /* Deliver an image display element. The iterator IT is already
4759 filled with image information (done in handle_display_prop). Value
4760 is always 1. */
4761
4762
4763 static int
4764 next_element_from_image (it)
4765 struct it *it;
4766 {
4767 it->what = IT_IMAGE;
4768 return 1;
4769 }
4770
4771
4772 /* Fill iterator IT with next display element from a stretch glyph
4773 property. IT->object is the value of the text property. Value is
4774 always 1. */
4775
4776 static int
4777 next_element_from_stretch (it)
4778 struct it *it;
4779 {
4780 it->what = IT_STRETCH;
4781 return 1;
4782 }
4783
4784
4785 /* Load IT with the next display element from current_buffer. Value
4786 is zero if end of buffer reached. IT->stop_charpos is the next
4787 position at which to stop and check for text properties or buffer
4788 end. */
4789
4790 static int
4791 next_element_from_buffer (it)
4792 struct it *it;
4793 {
4794 int success_p = 1;
4795
4796 /* Check this assumption, otherwise, we would never enter the
4797 if-statement, below. */
4798 xassert (IT_CHARPOS (*it) >= BEGV
4799 && IT_CHARPOS (*it) <= it->stop_charpos);
4800
4801 if (IT_CHARPOS (*it) >= it->stop_charpos)
4802 {
4803 if (IT_CHARPOS (*it) >= it->end_charpos)
4804 {
4805 int overlay_strings_follow_p;
4806
4807 /* End of the game, except when overlay strings follow that
4808 haven't been returned yet. */
4809 if (it->overlay_strings_at_end_processed_p)
4810 overlay_strings_follow_p = 0;
4811 else
4812 {
4813 it->overlay_strings_at_end_processed_p = 1;
4814 overlay_strings_follow_p = get_overlay_strings (it, 0);
4815 }
4816
4817 if (overlay_strings_follow_p)
4818 success_p = get_next_display_element (it);
4819 else
4820 {
4821 it->what = IT_EOB;
4822 it->position = it->current.pos;
4823 success_p = 0;
4824 }
4825 }
4826 else
4827 {
4828 handle_stop (it);
4829 return get_next_display_element (it);
4830 }
4831 }
4832 else
4833 {
4834 /* No face changes, overlays etc. in sight, so just return a
4835 character from current_buffer. */
4836 unsigned char *p;
4837
4838 /* Maybe run the redisplay end trigger hook. Performance note:
4839 This doesn't seem to cost measurable time. */
4840 if (it->redisplay_end_trigger_charpos
4841 && it->glyph_row
4842 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4843 run_redisplay_end_trigger_hook (it);
4844
4845 /* Get the next character, maybe multibyte. */
4846 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4847 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4848 {
4849 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4850 - IT_BYTEPOS (*it));
4851 it->c = string_char_and_length (p, maxlen, &it->len);
4852 }
4853 else
4854 it->c = *p, it->len = 1;
4855
4856 /* Record what we have and where it came from. */
4857 it->what = IT_CHARACTER;;
4858 it->object = it->w->buffer;
4859 it->position = it->current.pos;
4860
4861 /* Normally we return the character found above, except when we
4862 really want to return an ellipsis for selective display. */
4863 if (it->selective)
4864 {
4865 if (it->c == '\n')
4866 {
4867 /* A value of selective > 0 means hide lines indented more
4868 than that number of columns. */
4869 if (it->selective > 0
4870 && IT_CHARPOS (*it) + 1 < ZV
4871 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4872 IT_BYTEPOS (*it) + 1,
4873 it->selective))
4874 {
4875 success_p = next_element_from_ellipsis (it);
4876 it->dpvec_char_len = -1;
4877 }
4878 }
4879 else if (it->c == '\r' && it->selective == -1)
4880 {
4881 /* A value of selective == -1 means that everything from the
4882 CR to the end of the line is invisible, with maybe an
4883 ellipsis displayed for it. */
4884 success_p = next_element_from_ellipsis (it);
4885 it->dpvec_char_len = -1;
4886 }
4887 }
4888 }
4889
4890 /* Value is zero if end of buffer reached. */
4891 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4892 return success_p;
4893 }
4894
4895
4896 /* Run the redisplay end trigger hook for IT. */
4897
4898 static void
4899 run_redisplay_end_trigger_hook (it)
4900 struct it *it;
4901 {
4902 Lisp_Object args[3];
4903
4904 /* IT->glyph_row should be non-null, i.e. we should be actually
4905 displaying something, or otherwise we should not run the hook. */
4906 xassert (it->glyph_row);
4907
4908 /* Set up hook arguments. */
4909 args[0] = Qredisplay_end_trigger_functions;
4910 args[1] = it->window;
4911 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4912 it->redisplay_end_trigger_charpos = 0;
4913
4914 /* Since we are *trying* to run these functions, don't try to run
4915 them again, even if they get an error. */
4916 it->w->redisplay_end_trigger = Qnil;
4917 Frun_hook_with_args (3, args);
4918
4919 /* Notice if it changed the face of the character we are on. */
4920 handle_face_prop (it);
4921 }
4922
4923
4924 /* Deliver a composition display element. The iterator IT is already
4925 filled with composition information (done in
4926 handle_composition_prop). Value is always 1. */
4927
4928 static int
4929 next_element_from_composition (it)
4930 struct it *it;
4931 {
4932 it->what = IT_COMPOSITION;
4933 it->position = (STRINGP (it->string)
4934 ? it->current.string_pos
4935 : it->current.pos);
4936 return 1;
4937 }
4938
4939
4940 \f
4941 /***********************************************************************
4942 Moving an iterator without producing glyphs
4943 ***********************************************************************/
4944
4945 /* Move iterator IT to a specified buffer or X position within one
4946 line on the display without producing glyphs.
4947
4948 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4949 whichever is reached first.
4950
4951 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4952
4953 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4954 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4955 TO_X includes the amount by which a window is horizontally
4956 scrolled.
4957
4958 Value is
4959
4960 MOVE_POS_MATCH_OR_ZV
4961 - when TO_POS or ZV was reached.
4962
4963 MOVE_X_REACHED
4964 -when TO_X was reached before TO_POS or ZV were reached.
4965
4966 MOVE_LINE_CONTINUED
4967 - when we reached the end of the display area and the line must
4968 be continued.
4969
4970 MOVE_LINE_TRUNCATED
4971 - when we reached the end of the display area and the line is
4972 truncated.
4973
4974 MOVE_NEWLINE_OR_CR
4975 - when we stopped at a line end, i.e. a newline or a CR and selective
4976 display is on. */
4977
4978 static enum move_it_result
4979 move_it_in_display_line_to (it, to_charpos, to_x, op)
4980 struct it *it;
4981 int to_charpos, to_x, op;
4982 {
4983 enum move_it_result result = MOVE_UNDEFINED;
4984 struct glyph_row *saved_glyph_row;
4985
4986 /* Don't produce glyphs in produce_glyphs. */
4987 saved_glyph_row = it->glyph_row;
4988 it->glyph_row = NULL;
4989
4990 while (1)
4991 {
4992 int x, i, ascent = 0, descent = 0;
4993
4994 /* Stop when ZV or TO_CHARPOS reached. */
4995 if (!get_next_display_element (it)
4996 || ((op & MOVE_TO_POS) != 0
4997 && BUFFERP (it->object)
4998 && IT_CHARPOS (*it) >= to_charpos))
4999 {
5000 result = MOVE_POS_MATCH_OR_ZV;
5001 break;
5002 }
5003
5004 /* The call to produce_glyphs will get the metrics of the
5005 display element IT is loaded with. We record in x the
5006 x-position before this display element in case it does not
5007 fit on the line. */
5008 x = it->current_x;
5009
5010 /* Remember the line height so far in case the next element doesn't
5011 fit on the line. */
5012 if (!it->truncate_lines_p)
5013 {
5014 ascent = it->max_ascent;
5015 descent = it->max_descent;
5016 }
5017
5018 PRODUCE_GLYPHS (it);
5019
5020 if (it->area != TEXT_AREA)
5021 {
5022 set_iterator_to_next (it, 1);
5023 continue;
5024 }
5025
5026 /* The number of glyphs we get back in IT->nglyphs will normally
5027 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5028 character on a terminal frame, or (iii) a line end. For the
5029 second case, IT->nglyphs - 1 padding glyphs will be present
5030 (on X frames, there is only one glyph produced for a
5031 composite character.
5032
5033 The behavior implemented below means, for continuation lines,
5034 that as many spaces of a TAB as fit on the current line are
5035 displayed there. For terminal frames, as many glyphs of a
5036 multi-glyph character are displayed in the current line, too.
5037 This is what the old redisplay code did, and we keep it that
5038 way. Under X, the whole shape of a complex character must
5039 fit on the line or it will be completely displayed in the
5040 next line.
5041
5042 Note that both for tabs and padding glyphs, all glyphs have
5043 the same width. */
5044 if (it->nglyphs)
5045 {
5046 /* More than one glyph or glyph doesn't fit on line. All
5047 glyphs have the same width. */
5048 int single_glyph_width = it->pixel_width / it->nglyphs;
5049 int new_x;
5050
5051 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5052 {
5053 new_x = x + single_glyph_width;
5054
5055 /* We want to leave anything reaching TO_X to the caller. */
5056 if ((op & MOVE_TO_X) && new_x > to_x)
5057 {
5058 it->current_x = x;
5059 result = MOVE_X_REACHED;
5060 break;
5061 }
5062 else if (/* Lines are continued. */
5063 !it->truncate_lines_p
5064 && (/* And glyph doesn't fit on the line. */
5065 new_x > it->last_visible_x
5066 /* Or it fits exactly and we're on a window
5067 system frame. */
5068 || (new_x == it->last_visible_x
5069 && FRAME_WINDOW_P (it->f))))
5070 {
5071 if (/* IT->hpos == 0 means the very first glyph
5072 doesn't fit on the line, e.g. a wide image. */
5073 it->hpos == 0
5074 || (new_x == it->last_visible_x
5075 && FRAME_WINDOW_P (it->f)))
5076 {
5077 ++it->hpos;
5078 it->current_x = new_x;
5079 if (i == it->nglyphs - 1)
5080 set_iterator_to_next (it, 1);
5081 }
5082 else
5083 {
5084 it->current_x = x;
5085 it->max_ascent = ascent;
5086 it->max_descent = descent;
5087 }
5088
5089 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5090 IT_CHARPOS (*it)));
5091 result = MOVE_LINE_CONTINUED;
5092 break;
5093 }
5094 else if (new_x > it->first_visible_x)
5095 {
5096 /* Glyph is visible. Increment number of glyphs that
5097 would be displayed. */
5098 ++it->hpos;
5099 }
5100 else
5101 {
5102 /* Glyph is completely off the left margin of the display
5103 area. Nothing to do. */
5104 }
5105 }
5106
5107 if (result != MOVE_UNDEFINED)
5108 break;
5109 }
5110 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5111 {
5112 /* Stop when TO_X specified and reached. This check is
5113 necessary here because of lines consisting of a line end,
5114 only. The line end will not produce any glyphs and we
5115 would never get MOVE_X_REACHED. */
5116 xassert (it->nglyphs == 0);
5117 result = MOVE_X_REACHED;
5118 break;
5119 }
5120
5121 /* Is this a line end? If yes, we're done. */
5122 if (ITERATOR_AT_END_OF_LINE_P (it))
5123 {
5124 result = MOVE_NEWLINE_OR_CR;
5125 break;
5126 }
5127
5128 /* The current display element has been consumed. Advance
5129 to the next. */
5130 set_iterator_to_next (it, 1);
5131
5132 /* Stop if lines are truncated and IT's current x-position is
5133 past the right edge of the window now. */
5134 if (it->truncate_lines_p
5135 && it->current_x >= it->last_visible_x)
5136 {
5137 result = MOVE_LINE_TRUNCATED;
5138 break;
5139 }
5140 }
5141
5142 /* Restore the iterator settings altered at the beginning of this
5143 function. */
5144 it->glyph_row = saved_glyph_row;
5145 return result;
5146 }
5147
5148
5149 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
5150 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
5151 the description of enum move_operation_enum.
5152
5153 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5154 screen line, this function will set IT to the next position >
5155 TO_CHARPOS. */
5156
5157 void
5158 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5159 struct it *it;
5160 int to_charpos, to_x, to_y, to_vpos;
5161 int op;
5162 {
5163 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5164 int line_height;
5165 int reached = 0;
5166
5167 for (;;)
5168 {
5169 if (op & MOVE_TO_VPOS)
5170 {
5171 /* If no TO_CHARPOS and no TO_X specified, stop at the
5172 start of the line TO_VPOS. */
5173 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5174 {
5175 if (it->vpos == to_vpos)
5176 {
5177 reached = 1;
5178 break;
5179 }
5180 else
5181 skip = move_it_in_display_line_to (it, -1, -1, 0);
5182 }
5183 else
5184 {
5185 /* TO_VPOS >= 0 means stop at TO_X in the line at
5186 TO_VPOS, or at TO_POS, whichever comes first. */
5187 if (it->vpos == to_vpos)
5188 {
5189 reached = 2;
5190 break;
5191 }
5192
5193 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5194
5195 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5196 {
5197 reached = 3;
5198 break;
5199 }
5200 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5201 {
5202 /* We have reached TO_X but not in the line we want. */
5203 skip = move_it_in_display_line_to (it, to_charpos,
5204 -1, MOVE_TO_POS);
5205 if (skip == MOVE_POS_MATCH_OR_ZV)
5206 {
5207 reached = 4;
5208 break;
5209 }
5210 }
5211 }
5212 }
5213 else if (op & MOVE_TO_Y)
5214 {
5215 struct it it_backup;
5216
5217 /* TO_Y specified means stop at TO_X in the line containing
5218 TO_Y---or at TO_CHARPOS if this is reached first. The
5219 problem is that we can't really tell whether the line
5220 contains TO_Y before we have completely scanned it, and
5221 this may skip past TO_X. What we do is to first scan to
5222 TO_X.
5223
5224 If TO_X is not specified, use a TO_X of zero. The reason
5225 is to make the outcome of this function more predictable.
5226 If we didn't use TO_X == 0, we would stop at the end of
5227 the line which is probably not what a caller would expect
5228 to happen. */
5229 skip = move_it_in_display_line_to (it, to_charpos,
5230 ((op & MOVE_TO_X)
5231 ? to_x : 0),
5232 (MOVE_TO_X
5233 | (op & MOVE_TO_POS)));
5234
5235 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5236 if (skip == MOVE_POS_MATCH_OR_ZV)
5237 {
5238 reached = 5;
5239 break;
5240 }
5241
5242 /* If TO_X was reached, we would like to know whether TO_Y
5243 is in the line. This can only be said if we know the
5244 total line height which requires us to scan the rest of
5245 the line. */
5246 if (skip == MOVE_X_REACHED)
5247 {
5248 it_backup = *it;
5249 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5250 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5251 op & MOVE_TO_POS);
5252 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5253 }
5254
5255 /* Now, decide whether TO_Y is in this line. */
5256 line_height = it->max_ascent + it->max_descent;
5257 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5258
5259 if (to_y >= it->current_y
5260 && to_y < it->current_y + line_height)
5261 {
5262 if (skip == MOVE_X_REACHED)
5263 /* If TO_Y is in this line and TO_X was reached above,
5264 we scanned too far. We have to restore IT's settings
5265 to the ones before skipping. */
5266 *it = it_backup;
5267 reached = 6;
5268 }
5269 else if (skip == MOVE_X_REACHED)
5270 {
5271 skip = skip2;
5272 if (skip == MOVE_POS_MATCH_OR_ZV)
5273 reached = 7;
5274 }
5275
5276 if (reached)
5277 break;
5278 }
5279 else
5280 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5281
5282 switch (skip)
5283 {
5284 case MOVE_POS_MATCH_OR_ZV:
5285 reached = 8;
5286 goto out;
5287
5288 case MOVE_NEWLINE_OR_CR:
5289 set_iterator_to_next (it, 1);
5290 it->continuation_lines_width = 0;
5291 break;
5292
5293 case MOVE_LINE_TRUNCATED:
5294 it->continuation_lines_width = 0;
5295 reseat_at_next_visible_line_start (it, 0);
5296 if ((op & MOVE_TO_POS) != 0
5297 && IT_CHARPOS (*it) > to_charpos)
5298 {
5299 reached = 9;
5300 goto out;
5301 }
5302 break;
5303
5304 case MOVE_LINE_CONTINUED:
5305 it->continuation_lines_width += it->current_x;
5306 break;
5307
5308 default:
5309 abort ();
5310 }
5311
5312 /* Reset/increment for the next run. */
5313 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5314 it->current_x = it->hpos = 0;
5315 it->current_y += it->max_ascent + it->max_descent;
5316 ++it->vpos;
5317 last_height = it->max_ascent + it->max_descent;
5318 last_max_ascent = it->max_ascent;
5319 it->max_ascent = it->max_descent = 0;
5320 }
5321
5322 out:
5323
5324 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5325 }
5326
5327
5328 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5329
5330 If DY > 0, move IT backward at least that many pixels. DY = 0
5331 means move IT backward to the preceding line start or BEGV. This
5332 function may move over more than DY pixels if IT->current_y - DY
5333 ends up in the middle of a line; in this case IT->current_y will be
5334 set to the top of the line moved to. */
5335
5336 void
5337 move_it_vertically_backward (it, dy)
5338 struct it *it;
5339 int dy;
5340 {
5341 int nlines, h;
5342 struct it it2, it3;
5343 int start_pos = IT_CHARPOS (*it);
5344
5345 xassert (dy >= 0);
5346
5347 /* Estimate how many newlines we must move back. */
5348 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5349
5350 /* Set the iterator's position that many lines back. */
5351 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5352 back_to_previous_visible_line_start (it);
5353
5354 /* Reseat the iterator here. When moving backward, we don't want
5355 reseat to skip forward over invisible text, set up the iterator
5356 to deliver from overlay strings at the new position etc. So,
5357 use reseat_1 here. */
5358 reseat_1 (it, it->current.pos, 1);
5359
5360 /* We are now surely at a line start. */
5361 it->current_x = it->hpos = 0;
5362
5363 /* Move forward and see what y-distance we moved. First move to the
5364 start of the next line so that we get its height. We need this
5365 height to be able to tell whether we reached the specified
5366 y-distance. */
5367 it2 = *it;
5368 it2.max_ascent = it2.max_descent = 0;
5369 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5370 MOVE_TO_POS | MOVE_TO_VPOS);
5371 xassert (IT_CHARPOS (*it) >= BEGV);
5372 it3 = it2;
5373
5374 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5375 xassert (IT_CHARPOS (*it) >= BEGV);
5376 h = it2.current_y - it->current_y;
5377 nlines = it2.vpos - it->vpos;
5378
5379 /* Correct IT's y and vpos position. */
5380 it->vpos -= nlines;
5381 it->current_y -= h;
5382
5383 if (dy == 0)
5384 {
5385 /* DY == 0 means move to the start of the screen line. The
5386 value of nlines is > 0 if continuation lines were involved. */
5387 if (nlines > 0)
5388 move_it_by_lines (it, nlines, 1);
5389 xassert (IT_CHARPOS (*it) <= start_pos);
5390 }
5391 else if (nlines)
5392 {
5393 /* The y-position we try to reach. Note that h has been
5394 subtracted in front of the if-statement. */
5395 int target_y = it->current_y + h - dy;
5396 int y0 = it3.current_y;
5397 int y1 = line_bottom_y (&it3);
5398 int line_height = y1 - y0;
5399
5400 /* If we did not reach target_y, try to move further backward if
5401 we can. If we moved too far backward, try to move forward. */
5402 if (target_y < it->current_y
5403 /* This is heuristic. In a window that's 3 lines high, with
5404 a line height of 13 pixels each, recentering with point
5405 on the bottom line will try to move -39/2 = 19 pixels
5406 backward. Try to avoid moving into the first line. */
5407 && it->current_y - target_y > line_height / 3 * 2
5408 && IT_CHARPOS (*it) > BEGV)
5409 {
5410 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5411 target_y - it->current_y));
5412 move_it_vertically (it, target_y - it->current_y);
5413 xassert (IT_CHARPOS (*it) >= BEGV);
5414 }
5415 else if (target_y >= it->current_y + line_height
5416 && IT_CHARPOS (*it) < ZV)
5417 {
5418 /* Should move forward by at least one line, maybe more. */
5419 do
5420 {
5421 move_it_by_lines (it, 1, 1);
5422 }
5423 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5424
5425 xassert (IT_CHARPOS (*it) >= BEGV);
5426 }
5427 }
5428 }
5429
5430
5431 /* Move IT by a specified amount of pixel lines DY. DY negative means
5432 move backwards. DY = 0 means move to start of screen line. At the
5433 end, IT will be on the start of a screen line. */
5434
5435 void
5436 move_it_vertically (it, dy)
5437 struct it *it;
5438 int dy;
5439 {
5440 if (dy <= 0)
5441 move_it_vertically_backward (it, -dy);
5442 else if (dy > 0)
5443 {
5444 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5445 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5446 MOVE_TO_POS | MOVE_TO_Y);
5447 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5448
5449 /* If buffer ends in ZV without a newline, move to the start of
5450 the line to satisfy the post-condition. */
5451 if (IT_CHARPOS (*it) == ZV
5452 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5453 move_it_by_lines (it, 0, 0);
5454 }
5455 }
5456
5457
5458 /* Move iterator IT past the end of the text line it is in. */
5459
5460 void
5461 move_it_past_eol (it)
5462 struct it *it;
5463 {
5464 enum move_it_result rc;
5465
5466 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5467 if (rc == MOVE_NEWLINE_OR_CR)
5468 set_iterator_to_next (it, 0);
5469 }
5470
5471
5472 #if 0 /* Currently not used. */
5473
5474 /* Return non-zero if some text between buffer positions START_CHARPOS
5475 and END_CHARPOS is invisible. IT->window is the window for text
5476 property lookup. */
5477
5478 static int
5479 invisible_text_between_p (it, start_charpos, end_charpos)
5480 struct it *it;
5481 int start_charpos, end_charpos;
5482 {
5483 Lisp_Object prop, limit;
5484 int invisible_found_p;
5485
5486 xassert (it != NULL && start_charpos <= end_charpos);
5487
5488 /* Is text at START invisible? */
5489 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5490 it->window);
5491 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5492 invisible_found_p = 1;
5493 else
5494 {
5495 limit = Fnext_single_char_property_change (make_number (start_charpos),
5496 Qinvisible, Qnil,
5497 make_number (end_charpos));
5498 invisible_found_p = XFASTINT (limit) < end_charpos;
5499 }
5500
5501 return invisible_found_p;
5502 }
5503
5504 #endif /* 0 */
5505
5506
5507 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5508 negative means move up. DVPOS == 0 means move to the start of the
5509 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5510 NEED_Y_P is zero, IT->current_y will be left unchanged.
5511
5512 Further optimization ideas: If we would know that IT->f doesn't use
5513 a face with proportional font, we could be faster for
5514 truncate-lines nil. */
5515
5516 void
5517 move_it_by_lines (it, dvpos, need_y_p)
5518 struct it *it;
5519 int dvpos, need_y_p;
5520 {
5521 struct position pos;
5522
5523 if (!FRAME_WINDOW_P (it->f))
5524 {
5525 struct text_pos textpos;
5526
5527 /* We can use vmotion on frames without proportional fonts. */
5528 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5529 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5530 reseat (it, textpos, 1);
5531 it->vpos += pos.vpos;
5532 it->current_y += pos.vpos;
5533 }
5534 else if (dvpos == 0)
5535 {
5536 /* DVPOS == 0 means move to the start of the screen line. */
5537 move_it_vertically_backward (it, 0);
5538 xassert (it->current_x == 0 && it->hpos == 0);
5539 }
5540 else if (dvpos > 0)
5541 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5542 else
5543 {
5544 struct it it2;
5545 int start_charpos, i;
5546
5547 /* Start at the beginning of the screen line containing IT's
5548 position. */
5549 move_it_vertically_backward (it, 0);
5550
5551 /* Go back -DVPOS visible lines and reseat the iterator there. */
5552 start_charpos = IT_CHARPOS (*it);
5553 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5554 back_to_previous_visible_line_start (it);
5555 reseat (it, it->current.pos, 1);
5556 it->current_x = it->hpos = 0;
5557
5558 /* Above call may have moved too far if continuation lines
5559 are involved. Scan forward and see if it did. */
5560 it2 = *it;
5561 it2.vpos = it2.current_y = 0;
5562 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5563 it->vpos -= it2.vpos;
5564 it->current_y -= it2.current_y;
5565 it->current_x = it->hpos = 0;
5566
5567 /* If we moved too far, move IT some lines forward. */
5568 if (it2.vpos > -dvpos)
5569 {
5570 int delta = it2.vpos + dvpos;
5571 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5572 }
5573 }
5574 }
5575
5576
5577 \f
5578 /***********************************************************************
5579 Messages
5580 ***********************************************************************/
5581
5582
5583 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5584 to *Messages*. */
5585
5586 void
5587 add_to_log (format, arg1, arg2)
5588 char *format;
5589 Lisp_Object arg1, arg2;
5590 {
5591 Lisp_Object args[3];
5592 Lisp_Object msg, fmt;
5593 char *buffer;
5594 int len;
5595 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5596
5597 /* Do nothing if called asynchronously. Inserting text into
5598 a buffer may call after-change-functions and alike and
5599 that would means running Lisp asynchronously. */
5600 if (handling_signal)
5601 return;
5602
5603 fmt = msg = Qnil;
5604 GCPRO4 (fmt, msg, arg1, arg2);
5605
5606 args[0] = fmt = build_string (format);
5607 args[1] = arg1;
5608 args[2] = arg2;
5609 msg = Fformat (3, args);
5610
5611 len = STRING_BYTES (XSTRING (msg)) + 1;
5612 buffer = (char *) alloca (len);
5613 bcopy (XSTRING (msg)->data, buffer, len);
5614
5615 message_dolog (buffer, len - 1, 1, 0);
5616 UNGCPRO;
5617 }
5618
5619
5620 /* Output a newline in the *Messages* buffer if "needs" one. */
5621
5622 void
5623 message_log_maybe_newline ()
5624 {
5625 if (message_log_need_newline)
5626 message_dolog ("", 0, 1, 0);
5627 }
5628
5629
5630 /* Add a string M of length NBYTES to the message log, optionally
5631 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5632 nonzero, means interpret the contents of M as multibyte. This
5633 function calls low-level routines in order to bypass text property
5634 hooks, etc. which might not be safe to run. */
5635
5636 void
5637 message_dolog (m, nbytes, nlflag, multibyte)
5638 char *m;
5639 int nbytes, nlflag, multibyte;
5640 {
5641 if (!NILP (Vmessage_log_max))
5642 {
5643 struct buffer *oldbuf;
5644 Lisp_Object oldpoint, oldbegv, oldzv;
5645 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5646 int point_at_end = 0;
5647 int zv_at_end = 0;
5648 Lisp_Object old_deactivate_mark, tem;
5649 struct gcpro gcpro1;
5650
5651 old_deactivate_mark = Vdeactivate_mark;
5652 oldbuf = current_buffer;
5653 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5654 current_buffer->undo_list = Qt;
5655
5656 oldpoint = message_dolog_marker1;
5657 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5658 oldbegv = message_dolog_marker2;
5659 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5660 oldzv = message_dolog_marker3;
5661 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5662 GCPRO1 (old_deactivate_mark);
5663
5664 if (PT == Z)
5665 point_at_end = 1;
5666 if (ZV == Z)
5667 zv_at_end = 1;
5668
5669 BEGV = BEG;
5670 BEGV_BYTE = BEG_BYTE;
5671 ZV = Z;
5672 ZV_BYTE = Z_BYTE;
5673 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5674
5675 /* Insert the string--maybe converting multibyte to single byte
5676 or vice versa, so that all the text fits the buffer. */
5677 if (multibyte
5678 && NILP (current_buffer->enable_multibyte_characters))
5679 {
5680 int i, c, char_bytes;
5681 unsigned char work[1];
5682
5683 /* Convert a multibyte string to single-byte
5684 for the *Message* buffer. */
5685 for (i = 0; i < nbytes; i += nbytes)
5686 {
5687 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5688 work[0] = (SINGLE_BYTE_CHAR_P (c)
5689 ? c
5690 : multibyte_char_to_unibyte (c, Qnil));
5691 insert_1_both (work, 1, 1, 1, 0, 0);
5692 }
5693 }
5694 else if (! multibyte
5695 && ! NILP (current_buffer->enable_multibyte_characters))
5696 {
5697 int i, c, char_bytes;
5698 unsigned char *msg = (unsigned char *) m;
5699 unsigned char str[MAX_MULTIBYTE_LENGTH];
5700 /* Convert a single-byte string to multibyte
5701 for the *Message* buffer. */
5702 for (i = 0; i < nbytes; i++)
5703 {
5704 c = unibyte_char_to_multibyte (msg[i]);
5705 char_bytes = CHAR_STRING (c, str);
5706 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5707 }
5708 }
5709 else if (nbytes)
5710 insert_1 (m, nbytes, 1, 0, 0);
5711
5712 if (nlflag)
5713 {
5714 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5715 insert_1 ("\n", 1, 1, 0, 0);
5716
5717 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5718 this_bol = PT;
5719 this_bol_byte = PT_BYTE;
5720
5721 /* See if this line duplicates the previous one.
5722 If so, combine duplicates. */
5723 if (this_bol > BEG)
5724 {
5725 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5726 prev_bol = PT;
5727 prev_bol_byte = PT_BYTE;
5728
5729 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5730 this_bol, this_bol_byte);
5731 if (dup)
5732 {
5733 del_range_both (prev_bol, prev_bol_byte,
5734 this_bol, this_bol_byte, 0);
5735 if (dup > 1)
5736 {
5737 char dupstr[40];
5738 int duplen;
5739
5740 /* If you change this format, don't forget to also
5741 change message_log_check_duplicate. */
5742 sprintf (dupstr, " [%d times]", dup);
5743 duplen = strlen (dupstr);
5744 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5745 insert_1 (dupstr, duplen, 1, 0, 1);
5746 }
5747 }
5748 }
5749
5750 /* If we have more than the desired maximum number of lines
5751 in the *Messages* buffer now, delete the oldest ones.
5752 This is safe because we don't have undo in this buffer. */
5753
5754 if (NATNUMP (Vmessage_log_max))
5755 {
5756 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5757 -XFASTINT (Vmessage_log_max) - 1, 0);
5758 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5759 }
5760 }
5761 BEGV = XMARKER (oldbegv)->charpos;
5762 BEGV_BYTE = marker_byte_position (oldbegv);
5763
5764 if (zv_at_end)
5765 {
5766 ZV = Z;
5767 ZV_BYTE = Z_BYTE;
5768 }
5769 else
5770 {
5771 ZV = XMARKER (oldzv)->charpos;
5772 ZV_BYTE = marker_byte_position (oldzv);
5773 }
5774
5775 if (point_at_end)
5776 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5777 else
5778 /* We can't do Fgoto_char (oldpoint) because it will run some
5779 Lisp code. */
5780 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5781 XMARKER (oldpoint)->bytepos);
5782
5783 UNGCPRO;
5784 unchain_marker (oldpoint);
5785 unchain_marker (oldbegv);
5786 unchain_marker (oldzv);
5787
5788 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5789 set_buffer_internal (oldbuf);
5790 if (NILP (tem))
5791 windows_or_buffers_changed = old_windows_or_buffers_changed;
5792 message_log_need_newline = !nlflag;
5793 Vdeactivate_mark = old_deactivate_mark;
5794 }
5795 }
5796
5797
5798 /* We are at the end of the buffer after just having inserted a newline.
5799 (Note: We depend on the fact we won't be crossing the gap.)
5800 Check to see if the most recent message looks a lot like the previous one.
5801 Return 0 if different, 1 if the new one should just replace it, or a
5802 value N > 1 if we should also append " [N times]". */
5803
5804 static int
5805 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5806 int prev_bol, this_bol;
5807 int prev_bol_byte, this_bol_byte;
5808 {
5809 int i;
5810 int len = Z_BYTE - 1 - this_bol_byte;
5811 int seen_dots = 0;
5812 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5813 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5814
5815 for (i = 0; i < len; i++)
5816 {
5817 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5818 seen_dots = 1;
5819 if (p1[i] != p2[i])
5820 return seen_dots;
5821 }
5822 p1 += len;
5823 if (*p1 == '\n')
5824 return 2;
5825 if (*p1++ == ' ' && *p1++ == '[')
5826 {
5827 int n = 0;
5828 while (*p1 >= '0' && *p1 <= '9')
5829 n = n * 10 + *p1++ - '0';
5830 if (strncmp (p1, " times]\n", 8) == 0)
5831 return n+1;
5832 }
5833 return 0;
5834 }
5835
5836
5837 /* Display an echo area message M with a specified length of NBYTES
5838 bytes. The string may include null characters. If M is 0, clear
5839 out any existing message, and let the mini-buffer text show
5840 through.
5841
5842 The buffer M must continue to exist until after the echo area gets
5843 cleared or some other message gets displayed there. This means do
5844 not pass text that is stored in a Lisp string; do not pass text in
5845 a buffer that was alloca'd. */
5846
5847 void
5848 message2 (m, nbytes, multibyte)
5849 char *m;
5850 int nbytes;
5851 int multibyte;
5852 {
5853 /* First flush out any partial line written with print. */
5854 message_log_maybe_newline ();
5855 if (m)
5856 message_dolog (m, nbytes, 1, multibyte);
5857 message2_nolog (m, nbytes, multibyte);
5858 }
5859
5860
5861 /* The non-logging counterpart of message2. */
5862
5863 void
5864 message2_nolog (m, nbytes, multibyte)
5865 char *m;
5866 int nbytes;
5867 {
5868 struct frame *sf = SELECTED_FRAME ();
5869 message_enable_multibyte = multibyte;
5870
5871 if (noninteractive)
5872 {
5873 if (noninteractive_need_newline)
5874 putc ('\n', stderr);
5875 noninteractive_need_newline = 0;
5876 if (m)
5877 fwrite (m, nbytes, 1, stderr);
5878 if (cursor_in_echo_area == 0)
5879 fprintf (stderr, "\n");
5880 fflush (stderr);
5881 }
5882 /* A null message buffer means that the frame hasn't really been
5883 initialized yet. Error messages get reported properly by
5884 cmd_error, so this must be just an informative message; toss it. */
5885 else if (INTERACTIVE
5886 && sf->glyphs_initialized_p
5887 && FRAME_MESSAGE_BUF (sf))
5888 {
5889 Lisp_Object mini_window;
5890 struct frame *f;
5891
5892 /* Get the frame containing the mini-buffer
5893 that the selected frame is using. */
5894 mini_window = FRAME_MINIBUF_WINDOW (sf);
5895 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5896
5897 FRAME_SAMPLE_VISIBILITY (f);
5898 if (FRAME_VISIBLE_P (sf)
5899 && ! FRAME_VISIBLE_P (f))
5900 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5901
5902 if (m)
5903 {
5904 set_message (m, Qnil, nbytes, multibyte);
5905 if (minibuffer_auto_raise)
5906 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5907 }
5908 else
5909 clear_message (1, 1);
5910
5911 do_pending_window_change (0);
5912 echo_area_display (1);
5913 do_pending_window_change (0);
5914 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5915 (*frame_up_to_date_hook) (f);
5916 }
5917 }
5918
5919
5920 /* Display an echo area message M with a specified length of NBYTES
5921 bytes. The string may include null characters. If M is not a
5922 string, clear out any existing message, and let the mini-buffer
5923 text show through. */
5924
5925 void
5926 message3 (m, nbytes, multibyte)
5927 Lisp_Object m;
5928 int nbytes;
5929 int multibyte;
5930 {
5931 struct gcpro gcpro1;
5932
5933 GCPRO1 (m);
5934
5935 /* First flush out any partial line written with print. */
5936 message_log_maybe_newline ();
5937 if (STRINGP (m))
5938 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5939 message3_nolog (m, nbytes, multibyte);
5940
5941 UNGCPRO;
5942 }
5943
5944
5945 /* The non-logging version of message3. */
5946
5947 void
5948 message3_nolog (m, nbytes, multibyte)
5949 Lisp_Object m;
5950 int nbytes, multibyte;
5951 {
5952 struct frame *sf = SELECTED_FRAME ();
5953 message_enable_multibyte = multibyte;
5954
5955 if (noninteractive)
5956 {
5957 if (noninteractive_need_newline)
5958 putc ('\n', stderr);
5959 noninteractive_need_newline = 0;
5960 if (STRINGP (m))
5961 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5962 if (cursor_in_echo_area == 0)
5963 fprintf (stderr, "\n");
5964 fflush (stderr);
5965 }
5966 /* A null message buffer means that the frame hasn't really been
5967 initialized yet. Error messages get reported properly by
5968 cmd_error, so this must be just an informative message; toss it. */
5969 else if (INTERACTIVE
5970 && sf->glyphs_initialized_p
5971 && FRAME_MESSAGE_BUF (sf))
5972 {
5973 Lisp_Object mini_window;
5974 Lisp_Object frame;
5975 struct frame *f;
5976
5977 /* Get the frame containing the mini-buffer
5978 that the selected frame is using. */
5979 mini_window = FRAME_MINIBUF_WINDOW (sf);
5980 frame = XWINDOW (mini_window)->frame;
5981 f = XFRAME (frame);
5982
5983 FRAME_SAMPLE_VISIBILITY (f);
5984 if (FRAME_VISIBLE_P (sf)
5985 && !FRAME_VISIBLE_P (f))
5986 Fmake_frame_visible (frame);
5987
5988 if (STRINGP (m) && XSTRING (m)->size)
5989 {
5990 set_message (NULL, m, nbytes, multibyte);
5991 if (minibuffer_auto_raise)
5992 Fraise_frame (frame);
5993 }
5994 else
5995 clear_message (1, 1);
5996
5997 do_pending_window_change (0);
5998 echo_area_display (1);
5999 do_pending_window_change (0);
6000 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6001 (*frame_up_to_date_hook) (f);
6002 }
6003 }
6004
6005
6006 /* Display a null-terminated echo area message M. If M is 0, clear
6007 out any existing message, and let the mini-buffer text show through.
6008
6009 The buffer M must continue to exist until after the echo area gets
6010 cleared or some other message gets displayed there. Do not pass
6011 text that is stored in a Lisp string. Do not pass text in a buffer
6012 that was alloca'd. */
6013
6014 void
6015 message1 (m)
6016 char *m;
6017 {
6018 message2 (m, (m ? strlen (m) : 0), 0);
6019 }
6020
6021
6022 /* The non-logging counterpart of message1. */
6023
6024 void
6025 message1_nolog (m)
6026 char *m;
6027 {
6028 message2_nolog (m, (m ? strlen (m) : 0), 0);
6029 }
6030
6031 /* Display a message M which contains a single %s
6032 which gets replaced with STRING. */
6033
6034 void
6035 message_with_string (m, string, log)
6036 char *m;
6037 Lisp_Object string;
6038 int log;
6039 {
6040 if (noninteractive)
6041 {
6042 if (m)
6043 {
6044 if (noninteractive_need_newline)
6045 putc ('\n', stderr);
6046 noninteractive_need_newline = 0;
6047 fprintf (stderr, m, XSTRING (string)->data);
6048 if (cursor_in_echo_area == 0)
6049 fprintf (stderr, "\n");
6050 fflush (stderr);
6051 }
6052 }
6053 else if (INTERACTIVE)
6054 {
6055 /* The frame whose minibuffer we're going to display the message on.
6056 It may be larger than the selected frame, so we need
6057 to use its buffer, not the selected frame's buffer. */
6058 Lisp_Object mini_window;
6059 struct frame *f, *sf = SELECTED_FRAME ();
6060
6061 /* Get the frame containing the minibuffer
6062 that the selected frame is using. */
6063 mini_window = FRAME_MINIBUF_WINDOW (sf);
6064 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6065
6066 /* A null message buffer means that the frame hasn't really been
6067 initialized yet. Error messages get reported properly by
6068 cmd_error, so this must be just an informative message; toss it. */
6069 if (FRAME_MESSAGE_BUF (f))
6070 {
6071 int len;
6072 char *a[1];
6073 a[0] = (char *) XSTRING (string)->data;
6074
6075 len = doprnt (FRAME_MESSAGE_BUF (f),
6076 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6077
6078 if (log)
6079 message2 (FRAME_MESSAGE_BUF (f), len,
6080 STRING_MULTIBYTE (string));
6081 else
6082 message2_nolog (FRAME_MESSAGE_BUF (f), len,
6083 STRING_MULTIBYTE (string));
6084
6085 /* Print should start at the beginning of the message
6086 buffer next time. */
6087 message_buf_print = 0;
6088 }
6089 }
6090 }
6091
6092
6093 /* Dump an informative message to the minibuf. If M is 0, clear out
6094 any existing message, and let the mini-buffer text show through. */
6095
6096 /* VARARGS 1 */
6097 void
6098 message (m, a1, a2, a3)
6099 char *m;
6100 EMACS_INT a1, a2, a3;
6101 {
6102 if (noninteractive)
6103 {
6104 if (m)
6105 {
6106 if (noninteractive_need_newline)
6107 putc ('\n', stderr);
6108 noninteractive_need_newline = 0;
6109 fprintf (stderr, m, a1, a2, a3);
6110 if (cursor_in_echo_area == 0)
6111 fprintf (stderr, "\n");
6112 fflush (stderr);
6113 }
6114 }
6115 else if (INTERACTIVE)
6116 {
6117 /* The frame whose mini-buffer we're going to display the message
6118 on. It may be larger than the selected frame, so we need to
6119 use its buffer, not the selected frame's buffer. */
6120 Lisp_Object mini_window;
6121 struct frame *f, *sf = SELECTED_FRAME ();
6122
6123 /* Get the frame containing the mini-buffer
6124 that the selected frame is using. */
6125 mini_window = FRAME_MINIBUF_WINDOW (sf);
6126 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6127
6128 /* A null message buffer means that the frame hasn't really been
6129 initialized yet. Error messages get reported properly by
6130 cmd_error, so this must be just an informative message; toss
6131 it. */
6132 if (FRAME_MESSAGE_BUF (f))
6133 {
6134 if (m)
6135 {
6136 int len;
6137 #ifdef NO_ARG_ARRAY
6138 char *a[3];
6139 a[0] = (char *) a1;
6140 a[1] = (char *) a2;
6141 a[2] = (char *) a3;
6142
6143 len = doprnt (FRAME_MESSAGE_BUF (f),
6144 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6145 #else
6146 len = doprnt (FRAME_MESSAGE_BUF (f),
6147 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6148 (char **) &a1);
6149 #endif /* NO_ARG_ARRAY */
6150
6151 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6152 }
6153 else
6154 message1 (0);
6155
6156 /* Print should start at the beginning of the message
6157 buffer next time. */
6158 message_buf_print = 0;
6159 }
6160 }
6161 }
6162
6163
6164 /* The non-logging version of message. */
6165
6166 void
6167 message_nolog (m, a1, a2, a3)
6168 char *m;
6169 EMACS_INT a1, a2, a3;
6170 {
6171 Lisp_Object old_log_max;
6172 old_log_max = Vmessage_log_max;
6173 Vmessage_log_max = Qnil;
6174 message (m, a1, a2, a3);
6175 Vmessage_log_max = old_log_max;
6176 }
6177
6178
6179 /* Display the current message in the current mini-buffer. This is
6180 only called from error handlers in process.c, and is not time
6181 critical. */
6182
6183 void
6184 update_echo_area ()
6185 {
6186 if (!NILP (echo_area_buffer[0]))
6187 {
6188 Lisp_Object string;
6189 string = Fcurrent_message ();
6190 message3 (string, XSTRING (string)->size,
6191 !NILP (current_buffer->enable_multibyte_characters));
6192 }
6193 }
6194
6195
6196 /* Make sure echo area buffers in echo_buffers[] are life. If they
6197 aren't, make new ones. */
6198
6199 static void
6200 ensure_echo_area_buffers ()
6201 {
6202 int i;
6203
6204 for (i = 0; i < 2; ++i)
6205 if (!BUFFERP (echo_buffer[i])
6206 || NILP (XBUFFER (echo_buffer[i])->name))
6207 {
6208 char name[30];
6209 Lisp_Object old_buffer;
6210 int j;
6211
6212 old_buffer = echo_buffer[i];
6213 sprintf (name, " *Echo Area %d*", i);
6214 echo_buffer[i] = Fget_buffer_create (build_string (name));
6215 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6216
6217 for (j = 0; j < 2; ++j)
6218 if (EQ (old_buffer, echo_area_buffer[j]))
6219 echo_area_buffer[j] = echo_buffer[i];
6220 }
6221 }
6222
6223
6224 /* Call FN with args A1..A4 with either the current or last displayed
6225 echo_area_buffer as current buffer.
6226
6227 WHICH zero means use the current message buffer
6228 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6229 from echo_buffer[] and clear it.
6230
6231 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6232 suitable buffer from echo_buffer[] and clear it.
6233
6234 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6235 that the current message becomes the last displayed one, make
6236 choose a suitable buffer for echo_area_buffer[0], and clear it.
6237
6238 Value is what FN returns. */
6239
6240 static int
6241 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6242 struct window *w;
6243 int which;
6244 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6245 EMACS_INT a1;
6246 Lisp_Object a2;
6247 EMACS_INT a3, a4;
6248 {
6249 Lisp_Object buffer;
6250 int this_one, the_other, clear_buffer_p, rc;
6251 int count = BINDING_STACK_SIZE ();
6252
6253 /* If buffers aren't life, make new ones. */
6254 ensure_echo_area_buffers ();
6255
6256 clear_buffer_p = 0;
6257
6258 if (which == 0)
6259 this_one = 0, the_other = 1;
6260 else if (which > 0)
6261 this_one = 1, the_other = 0;
6262 else
6263 {
6264 this_one = 0, the_other = 1;
6265 clear_buffer_p = 1;
6266
6267 /* We need a fresh one in case the current echo buffer equals
6268 the one containing the last displayed echo area message. */
6269 if (!NILP (echo_area_buffer[this_one])
6270 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6271 echo_area_buffer[this_one] = Qnil;
6272 }
6273
6274 /* Choose a suitable buffer from echo_buffer[] is we don't
6275 have one. */
6276 if (NILP (echo_area_buffer[this_one]))
6277 {
6278 echo_area_buffer[this_one]
6279 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6280 ? echo_buffer[the_other]
6281 : echo_buffer[this_one]);
6282 clear_buffer_p = 1;
6283 }
6284
6285 buffer = echo_area_buffer[this_one];
6286
6287 /* Don't get confused by reusing the buffer used for echoing
6288 for a different purpose. */
6289 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6290 cancel_echoing ();
6291
6292 record_unwind_protect (unwind_with_echo_area_buffer,
6293 with_echo_area_buffer_unwind_data (w));
6294
6295 /* Make the echo area buffer current. Note that for display
6296 purposes, it is not necessary that the displayed window's buffer
6297 == current_buffer, except for text property lookup. So, let's
6298 only set that buffer temporarily here without doing a full
6299 Fset_window_buffer. We must also change w->pointm, though,
6300 because otherwise an assertions in unshow_buffer fails, and Emacs
6301 aborts. */
6302 set_buffer_internal_1 (XBUFFER (buffer));
6303 if (w)
6304 {
6305 w->buffer = buffer;
6306 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6307 }
6308
6309 current_buffer->undo_list = Qt;
6310 current_buffer->read_only = Qnil;
6311 specbind (Qinhibit_read_only, Qt);
6312 specbind (Qinhibit_modification_hooks, Qt);
6313
6314 if (clear_buffer_p && Z > BEG)
6315 del_range (BEG, Z);
6316
6317 xassert (BEGV >= BEG);
6318 xassert (ZV <= Z && ZV >= BEGV);
6319
6320 rc = fn (a1, a2, a3, a4);
6321
6322 xassert (BEGV >= BEG);
6323 xassert (ZV <= Z && ZV >= BEGV);
6324
6325 unbind_to (count, Qnil);
6326 return rc;
6327 }
6328
6329
6330 /* Save state that should be preserved around the call to the function
6331 FN called in with_echo_area_buffer. */
6332
6333 static Lisp_Object
6334 with_echo_area_buffer_unwind_data (w)
6335 struct window *w;
6336 {
6337 int i = 0;
6338 Lisp_Object vector;
6339
6340 /* Reduce consing by keeping one vector in
6341 Vwith_echo_area_save_vector. */
6342 vector = Vwith_echo_area_save_vector;
6343 Vwith_echo_area_save_vector = Qnil;
6344
6345 if (NILP (vector))
6346 vector = Fmake_vector (make_number (7), Qnil);
6347
6348 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6349 AREF (vector, i) = Vdeactivate_mark, ++i;
6350 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6351
6352 if (w)
6353 {
6354 XSETWINDOW (AREF (vector, i), w); ++i;
6355 AREF (vector, i) = w->buffer; ++i;
6356 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6357 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6358 }
6359 else
6360 {
6361 int end = i + 4;
6362 for (; i < end; ++i)
6363 AREF (vector, i) = Qnil;
6364 }
6365
6366 xassert (i == ASIZE (vector));
6367 return vector;
6368 }
6369
6370
6371 /* Restore global state from VECTOR which was created by
6372 with_echo_area_buffer_unwind_data. */
6373
6374 static Lisp_Object
6375 unwind_with_echo_area_buffer (vector)
6376 Lisp_Object vector;
6377 {
6378 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6379 Vdeactivate_mark = AREF (vector, 1);
6380 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6381
6382 if (WINDOWP (AREF (vector, 3)))
6383 {
6384 struct window *w;
6385 Lisp_Object buffer, charpos, bytepos;
6386
6387 w = XWINDOW (AREF (vector, 3));
6388 buffer = AREF (vector, 4);
6389 charpos = AREF (vector, 5);
6390 bytepos = AREF (vector, 6);
6391
6392 w->buffer = buffer;
6393 set_marker_both (w->pointm, buffer,
6394 XFASTINT (charpos), XFASTINT (bytepos));
6395 }
6396
6397 Vwith_echo_area_save_vector = vector;
6398 return Qnil;
6399 }
6400
6401
6402 /* Set up the echo area for use by print functions. MULTIBYTE_P
6403 non-zero means we will print multibyte. */
6404
6405 void
6406 setup_echo_area_for_printing (multibyte_p)
6407 int multibyte_p;
6408 {
6409 ensure_echo_area_buffers ();
6410
6411 if (!message_buf_print)
6412 {
6413 /* A message has been output since the last time we printed.
6414 Choose a fresh echo area buffer. */
6415 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6416 echo_area_buffer[0] = echo_buffer[1];
6417 else
6418 echo_area_buffer[0] = echo_buffer[0];
6419
6420 /* Switch to that buffer and clear it. */
6421 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6422 current_buffer->truncate_lines = Qnil;
6423
6424 if (Z > BEG)
6425 {
6426 int count = BINDING_STACK_SIZE ();
6427 specbind (Qinhibit_read_only, Qt);
6428 del_range (BEG, Z);
6429 unbind_to (count, Qnil);
6430 }
6431 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6432
6433 /* Set up the buffer for the multibyteness we need. */
6434 if (multibyte_p
6435 != !NILP (current_buffer->enable_multibyte_characters))
6436 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6437
6438 /* Raise the frame containing the echo area. */
6439 if (minibuffer_auto_raise)
6440 {
6441 struct frame *sf = SELECTED_FRAME ();
6442 Lisp_Object mini_window;
6443 mini_window = FRAME_MINIBUF_WINDOW (sf);
6444 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6445 }
6446
6447 message_log_maybe_newline ();
6448 message_buf_print = 1;
6449 }
6450 else
6451 {
6452 if (NILP (echo_area_buffer[0]))
6453 {
6454 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6455 echo_area_buffer[0] = echo_buffer[1];
6456 else
6457 echo_area_buffer[0] = echo_buffer[0];
6458 }
6459
6460 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6461 {
6462 /* Someone switched buffers between print requests. */
6463 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6464 current_buffer->truncate_lines = Qnil;
6465 }
6466 }
6467 }
6468
6469
6470 /* Display an echo area message in window W. Value is non-zero if W's
6471 height is changed. If display_last_displayed_message_p is
6472 non-zero, display the message that was last displayed, otherwise
6473 display the current message. */
6474
6475 static int
6476 display_echo_area (w)
6477 struct window *w;
6478 {
6479 int i, no_message_p, window_height_changed_p, count;
6480
6481 /* Temporarily disable garbage collections while displaying the echo
6482 area. This is done because a GC can print a message itself.
6483 That message would modify the echo area buffer's contents while a
6484 redisplay of the buffer is going on, and seriously confuse
6485 redisplay. */
6486 count = inhibit_garbage_collection ();
6487
6488 /* If there is no message, we must call display_echo_area_1
6489 nevertheless because it resizes the window. But we will have to
6490 reset the echo_area_buffer in question to nil at the end because
6491 with_echo_area_buffer will sets it to an empty buffer. */
6492 i = display_last_displayed_message_p ? 1 : 0;
6493 no_message_p = NILP (echo_area_buffer[i]);
6494
6495 window_height_changed_p
6496 = with_echo_area_buffer (w, display_last_displayed_message_p,
6497 display_echo_area_1,
6498 (EMACS_INT) w, Qnil, 0, 0);
6499
6500 if (no_message_p)
6501 echo_area_buffer[i] = Qnil;
6502
6503 unbind_to (count, Qnil);
6504 return window_height_changed_p;
6505 }
6506
6507
6508 /* Helper for display_echo_area. Display the current buffer which
6509 contains the current echo area message in window W, a mini-window,
6510 a pointer to which is passed in A1. A2..A4 are currently not used.
6511 Change the height of W so that all of the message is displayed.
6512 Value is non-zero if height of W was changed. */
6513
6514 static int
6515 display_echo_area_1 (a1, a2, a3, a4)
6516 EMACS_INT a1;
6517 Lisp_Object a2;
6518 EMACS_INT a3, a4;
6519 {
6520 struct window *w = (struct window *) a1;
6521 Lisp_Object window;
6522 struct text_pos start;
6523 int window_height_changed_p = 0;
6524
6525 /* Do this before displaying, so that we have a large enough glyph
6526 matrix for the display. */
6527 window_height_changed_p = resize_mini_window (w, 0);
6528
6529 /* Display. */
6530 clear_glyph_matrix (w->desired_matrix);
6531 XSETWINDOW (window, w);
6532 SET_TEXT_POS (start, BEG, BEG_BYTE);
6533 try_window (window, start);
6534
6535 return window_height_changed_p;
6536 }
6537
6538
6539 /* Resize the echo area window to exactly the size needed for the
6540 currently displayed message, if there is one. If a mini-buffer
6541 is active, don't shrink it. */
6542
6543 void
6544 resize_echo_area_exactly ()
6545 {
6546 if (BUFFERP (echo_area_buffer[0])
6547 && WINDOWP (echo_area_window))
6548 {
6549 struct window *w = XWINDOW (echo_area_window);
6550 int resized_p;
6551 Lisp_Object resize_exactly;
6552
6553 if (minibuf_level == 0)
6554 resize_exactly = Qt;
6555 else
6556 resize_exactly = Qnil;
6557
6558 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6559 (EMACS_INT) w, resize_exactly, 0, 0);
6560 if (resized_p)
6561 {
6562 ++windows_or_buffers_changed;
6563 ++update_mode_lines;
6564 redisplay_internal (0);
6565 }
6566 }
6567 }
6568
6569
6570 /* Callback function for with_echo_area_buffer, when used from
6571 resize_echo_area_exactly. A1 contains a pointer to the window to
6572 resize, EXACTLY non-nil means resize the mini-window exactly to the
6573 size of the text displayed. A3 and A4 are not used. Value is what
6574 resize_mini_window returns. */
6575
6576 static int
6577 resize_mini_window_1 (a1, exactly, a3, a4)
6578 EMACS_INT a1;
6579 Lisp_Object exactly;
6580 EMACS_INT a3, a4;
6581 {
6582 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6583 }
6584
6585
6586 /* Resize mini-window W to fit the size of its contents. EXACT:P
6587 means size the window exactly to the size needed. Otherwise, it's
6588 only enlarged until W's buffer is empty. Value is non-zero if
6589 the window height has been changed. */
6590
6591 int
6592 resize_mini_window (w, exact_p)
6593 struct window *w;
6594 int exact_p;
6595 {
6596 struct frame *f = XFRAME (w->frame);
6597 int window_height_changed_p = 0;
6598
6599 xassert (MINI_WINDOW_P (w));
6600
6601 /* Don't resize windows while redisplaying a window; it would
6602 confuse redisplay functions when the size of the window they are
6603 displaying changes from under them. Such a resizing can happen,
6604 for instance, when which-func prints a long message while
6605 we are running fontification-functions. We're running these
6606 functions with safe_call which binds inhibit-redisplay to t. */
6607 if (!NILP (Vinhibit_redisplay))
6608 return 0;
6609
6610 /* Nil means don't try to resize. */
6611 if (NILP (Vresize_mini_windows)
6612 || (FRAME_X_P (f) && f->output_data.x == NULL))
6613 return 0;
6614
6615 if (!FRAME_MINIBUF_ONLY_P (f))
6616 {
6617 struct it it;
6618 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6619 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6620 int height, max_height;
6621 int unit = CANON_Y_UNIT (f);
6622 struct text_pos start;
6623 struct buffer *old_current_buffer = NULL;
6624
6625 if (current_buffer != XBUFFER (w->buffer))
6626 {
6627 old_current_buffer = current_buffer;
6628 set_buffer_internal (XBUFFER (w->buffer));
6629 }
6630
6631 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6632
6633 /* Compute the max. number of lines specified by the user. */
6634 if (FLOATP (Vmax_mini_window_height))
6635 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6636 else if (INTEGERP (Vmax_mini_window_height))
6637 max_height = XINT (Vmax_mini_window_height);
6638 else
6639 max_height = total_height / 4;
6640
6641 /* Correct that max. height if it's bogus. */
6642 max_height = max (1, max_height);
6643 max_height = min (total_height, max_height);
6644
6645 /* Find out the height of the text in the window. */
6646 if (it.truncate_lines_p)
6647 height = 1;
6648 else
6649 {
6650 last_height = 0;
6651 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6652 if (it.max_ascent == 0 && it.max_descent == 0)
6653 height = it.current_y + last_height;
6654 else
6655 height = it.current_y + it.max_ascent + it.max_descent;
6656 height -= it.extra_line_spacing;
6657 height = (height + unit - 1) / unit;
6658 }
6659
6660 /* Compute a suitable window start. */
6661 if (height > max_height)
6662 {
6663 height = max_height;
6664 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6665 move_it_vertically_backward (&it, (height - 1) * unit);
6666 start = it.current.pos;
6667 }
6668 else
6669 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6670 SET_MARKER_FROM_TEXT_POS (w->start, start);
6671
6672 if (EQ (Vresize_mini_windows, Qgrow_only))
6673 {
6674 /* Let it grow only, until we display an empty message, in which
6675 case the window shrinks again. */
6676 if (height > XFASTINT (w->height))
6677 {
6678 int old_height = XFASTINT (w->height);
6679 freeze_window_starts (f, 1);
6680 grow_mini_window (w, height - XFASTINT (w->height));
6681 window_height_changed_p = XFASTINT (w->height) != old_height;
6682 }
6683 else if (height < XFASTINT (w->height)
6684 && (exact_p || BEGV == ZV))
6685 {
6686 int old_height = XFASTINT (w->height);
6687 freeze_window_starts (f, 0);
6688 shrink_mini_window (w);
6689 window_height_changed_p = XFASTINT (w->height) != old_height;
6690 }
6691 }
6692 else
6693 {
6694 /* Always resize to exact size needed. */
6695 if (height > XFASTINT (w->height))
6696 {
6697 int old_height = XFASTINT (w->height);
6698 freeze_window_starts (f, 1);
6699 grow_mini_window (w, height - XFASTINT (w->height));
6700 window_height_changed_p = XFASTINT (w->height) != old_height;
6701 }
6702 else if (height < XFASTINT (w->height))
6703 {
6704 int old_height = XFASTINT (w->height);
6705 freeze_window_starts (f, 0);
6706 shrink_mini_window (w);
6707
6708 if (height)
6709 {
6710 freeze_window_starts (f, 1);
6711 grow_mini_window (w, height - XFASTINT (w->height));
6712 }
6713
6714 window_height_changed_p = XFASTINT (w->height) != old_height;
6715 }
6716 }
6717
6718 if (old_current_buffer)
6719 set_buffer_internal (old_current_buffer);
6720 }
6721
6722 return window_height_changed_p;
6723 }
6724
6725
6726 /* Value is the current message, a string, or nil if there is no
6727 current message. */
6728
6729 Lisp_Object
6730 current_message ()
6731 {
6732 Lisp_Object msg;
6733
6734 if (NILP (echo_area_buffer[0]))
6735 msg = Qnil;
6736 else
6737 {
6738 with_echo_area_buffer (0, 0, current_message_1,
6739 (EMACS_INT) &msg, Qnil, 0, 0);
6740 if (NILP (msg))
6741 echo_area_buffer[0] = Qnil;
6742 }
6743
6744 return msg;
6745 }
6746
6747
6748 static int
6749 current_message_1 (a1, a2, a3, a4)
6750 EMACS_INT a1;
6751 Lisp_Object a2;
6752 EMACS_INT a3, a4;
6753 {
6754 Lisp_Object *msg = (Lisp_Object *) a1;
6755
6756 if (Z > BEG)
6757 *msg = make_buffer_string (BEG, Z, 1);
6758 else
6759 *msg = Qnil;
6760 return 0;
6761 }
6762
6763
6764 /* Push the current message on Vmessage_stack for later restauration
6765 by restore_message. Value is non-zero if the current message isn't
6766 empty. This is a relatively infrequent operation, so it's not
6767 worth optimizing. */
6768
6769 int
6770 push_message ()
6771 {
6772 Lisp_Object msg;
6773 msg = current_message ();
6774 Vmessage_stack = Fcons (msg, Vmessage_stack);
6775 return STRINGP (msg);
6776 }
6777
6778
6779 /* Handler for record_unwind_protect calling pop_message. */
6780
6781 Lisp_Object
6782 push_message_unwind (dummy)
6783 Lisp_Object dummy;
6784 {
6785 pop_message ();
6786 return Qnil;
6787 }
6788
6789
6790 /* Restore message display from the top of Vmessage_stack. */
6791
6792 void
6793 restore_message ()
6794 {
6795 Lisp_Object msg;
6796
6797 xassert (CONSP (Vmessage_stack));
6798 msg = XCAR (Vmessage_stack);
6799 if (STRINGP (msg))
6800 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6801 else
6802 message3_nolog (msg, 0, 0);
6803 }
6804
6805
6806 /* Pop the top-most entry off Vmessage_stack. */
6807
6808 void
6809 pop_message ()
6810 {
6811 xassert (CONSP (Vmessage_stack));
6812 Vmessage_stack = XCDR (Vmessage_stack);
6813 }
6814
6815
6816 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6817 exits. If the stack is not empty, we have a missing pop_message
6818 somewhere. */
6819
6820 void
6821 check_message_stack ()
6822 {
6823 if (!NILP (Vmessage_stack))
6824 abort ();
6825 }
6826
6827
6828 /* Truncate to NCHARS what will be displayed in the echo area the next
6829 time we display it---but don't redisplay it now. */
6830
6831 void
6832 truncate_echo_area (nchars)
6833 int nchars;
6834 {
6835 if (nchars == 0)
6836 echo_area_buffer[0] = Qnil;
6837 /* A null message buffer means that the frame hasn't really been
6838 initialized yet. Error messages get reported properly by
6839 cmd_error, so this must be just an informative message; toss it. */
6840 else if (!noninteractive
6841 && INTERACTIVE
6842 && !NILP (echo_area_buffer[0]))
6843 {
6844 struct frame *sf = SELECTED_FRAME ();
6845 if (FRAME_MESSAGE_BUF (sf))
6846 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6847 }
6848 }
6849
6850
6851 /* Helper function for truncate_echo_area. Truncate the current
6852 message to at most NCHARS characters. */
6853
6854 static int
6855 truncate_message_1 (nchars, a2, a3, a4)
6856 EMACS_INT nchars;
6857 Lisp_Object a2;
6858 EMACS_INT a3, a4;
6859 {
6860 if (BEG + nchars < Z)
6861 del_range (BEG + nchars, Z);
6862 if (Z == BEG)
6863 echo_area_buffer[0] = Qnil;
6864 return 0;
6865 }
6866
6867
6868 /* Set the current message to a substring of S or STRING.
6869
6870 If STRING is a Lisp string, set the message to the first NBYTES
6871 bytes from STRING. NBYTES zero means use the whole string. If
6872 STRING is multibyte, the message will be displayed multibyte.
6873
6874 If S is not null, set the message to the first LEN bytes of S. LEN
6875 zero means use the whole string. MULTIBYTE_P non-zero means S is
6876 multibyte. Display the message multibyte in that case. */
6877
6878 void
6879 set_message (s, string, nbytes, multibyte_p)
6880 char *s;
6881 Lisp_Object string;
6882 int nbytes;
6883 {
6884 message_enable_multibyte
6885 = ((s && multibyte_p)
6886 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6887
6888 with_echo_area_buffer (0, -1, set_message_1,
6889 (EMACS_INT) s, string, nbytes, multibyte_p);
6890 message_buf_print = 0;
6891 help_echo_showing_p = 0;
6892 }
6893
6894
6895 /* Helper function for set_message. Arguments have the same meaning
6896 as there, with A1 corresponding to S and A2 corresponding to STRING
6897 This function is called with the echo area buffer being
6898 current. */
6899
6900 static int
6901 set_message_1 (a1, a2, nbytes, multibyte_p)
6902 EMACS_INT a1;
6903 Lisp_Object a2;
6904 EMACS_INT nbytes, multibyte_p;
6905 {
6906 char *s = (char *) a1;
6907 Lisp_Object string = a2;
6908
6909 xassert (BEG == Z);
6910
6911 /* Change multibyteness of the echo buffer appropriately. */
6912 if (message_enable_multibyte
6913 != !NILP (current_buffer->enable_multibyte_characters))
6914 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6915
6916 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6917
6918 /* Insert new message at BEG. */
6919 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6920
6921 if (STRINGP (string))
6922 {
6923 int nchars;
6924
6925 if (nbytes == 0)
6926 nbytes = XSTRING (string)->size_byte;
6927 nchars = string_byte_to_char (string, nbytes);
6928
6929 /* This function takes care of single/multibyte conversion. We
6930 just have to ensure that the echo area buffer has the right
6931 setting of enable_multibyte_characters. */
6932 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6933 }
6934 else if (s)
6935 {
6936 if (nbytes == 0)
6937 nbytes = strlen (s);
6938
6939 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6940 {
6941 /* Convert from multi-byte to single-byte. */
6942 int i, c, n;
6943 unsigned char work[1];
6944
6945 /* Convert a multibyte string to single-byte. */
6946 for (i = 0; i < nbytes; i += n)
6947 {
6948 c = string_char_and_length (s + i, nbytes - i, &n);
6949 work[0] = (SINGLE_BYTE_CHAR_P (c)
6950 ? c
6951 : multibyte_char_to_unibyte (c, Qnil));
6952 insert_1_both (work, 1, 1, 1, 0, 0);
6953 }
6954 }
6955 else if (!multibyte_p
6956 && !NILP (current_buffer->enable_multibyte_characters))
6957 {
6958 /* Convert from single-byte to multi-byte. */
6959 int i, c, n;
6960 unsigned char *msg = (unsigned char *) s;
6961 unsigned char str[MAX_MULTIBYTE_LENGTH];
6962
6963 /* Convert a single-byte string to multibyte. */
6964 for (i = 0; i < nbytes; i++)
6965 {
6966 c = unibyte_char_to_multibyte (msg[i]);
6967 n = CHAR_STRING (c, str);
6968 insert_1_both (str, 1, n, 1, 0, 0);
6969 }
6970 }
6971 else
6972 insert_1 (s, nbytes, 1, 0, 0);
6973 }
6974
6975 return 0;
6976 }
6977
6978
6979 /* Clear messages. CURRENT_P non-zero means clear the current
6980 message. LAST_DISPLAYED_P non-zero means clear the message
6981 last displayed. */
6982
6983 void
6984 clear_message (current_p, last_displayed_p)
6985 int current_p, last_displayed_p;
6986 {
6987 if (current_p)
6988 {
6989 echo_area_buffer[0] = Qnil;
6990 message_cleared_p = 1;
6991 }
6992
6993 if (last_displayed_p)
6994 echo_area_buffer[1] = Qnil;
6995
6996 message_buf_print = 0;
6997 }
6998
6999 /* Clear garbaged frames.
7000
7001 This function is used where the old redisplay called
7002 redraw_garbaged_frames which in turn called redraw_frame which in
7003 turn called clear_frame. The call to clear_frame was a source of
7004 flickering. I believe a clear_frame is not necessary. It should
7005 suffice in the new redisplay to invalidate all current matrices,
7006 and ensure a complete redisplay of all windows. */
7007
7008 static void
7009 clear_garbaged_frames ()
7010 {
7011 if (frame_garbaged)
7012 {
7013 Lisp_Object tail, frame;
7014
7015 FOR_EACH_FRAME (tail, frame)
7016 {
7017 struct frame *f = XFRAME (frame);
7018
7019 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7020 {
7021 if (f->resized_p)
7022 Fredraw_frame (frame);
7023 clear_current_matrices (f);
7024 f->garbaged = 0;
7025 f->resized_p = 0;
7026 }
7027 }
7028
7029 frame_garbaged = 0;
7030 ++windows_or_buffers_changed;
7031 }
7032 }
7033
7034
7035 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7036 is non-zero update selected_frame. Value is non-zero if the
7037 mini-windows height has been changed. */
7038
7039 static int
7040 echo_area_display (update_frame_p)
7041 int update_frame_p;
7042 {
7043 Lisp_Object mini_window;
7044 struct window *w;
7045 struct frame *f;
7046 int window_height_changed_p = 0;
7047 struct frame *sf = SELECTED_FRAME ();
7048
7049 mini_window = FRAME_MINIBUF_WINDOW (sf);
7050 w = XWINDOW (mini_window);
7051 f = XFRAME (WINDOW_FRAME (w));
7052
7053 /* Don't display if frame is invisible or not yet initialized. */
7054 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7055 return 0;
7056
7057 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7058 #ifndef macintosh
7059 #ifdef HAVE_WINDOW_SYSTEM
7060 /* When Emacs starts, selected_frame may be a visible terminal
7061 frame, even if we run under a window system. If we let this
7062 through, a message would be displayed on the terminal. */
7063 if (EQ (selected_frame, Vterminal_frame)
7064 && !NILP (Vwindow_system))
7065 return 0;
7066 #endif /* HAVE_WINDOW_SYSTEM */
7067 #endif
7068
7069 /* Redraw garbaged frames. */
7070 if (frame_garbaged)
7071 clear_garbaged_frames ();
7072
7073 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7074 {
7075 echo_area_window = mini_window;
7076 window_height_changed_p = display_echo_area (w);
7077 w->must_be_updated_p = 1;
7078
7079 /* Update the display, unless called from redisplay_internal.
7080 Also don't update the screen during redisplay itself. The
7081 update will happen at the end of redisplay, and an update
7082 here could cause confusion. */
7083 if (update_frame_p && !redisplaying_p)
7084 {
7085 int n = 0;
7086
7087 /* If the display update has been interrupted by pending
7088 input, update mode lines in the frame. Due to the
7089 pending input, it might have been that redisplay hasn't
7090 been called, so that mode lines above the echo area are
7091 garbaged. This looks odd, so we prevent it here. */
7092 if (!display_completed)
7093 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7094
7095 if (window_height_changed_p
7096 /* Don't do this if Emacs is shutting down. Redisplay
7097 needs to run hooks. */
7098 && !NILP (Vrun_hooks))
7099 {
7100 /* Must update other windows. Likewise as in other
7101 cases, don't let this update be interrupted by
7102 pending input. */
7103 int count = BINDING_STACK_SIZE ();
7104 specbind (Qredisplay_dont_pause, Qt);
7105 windows_or_buffers_changed = 1;
7106 redisplay_internal (0);
7107 unbind_to (count, Qnil);
7108 }
7109 else if (FRAME_WINDOW_P (f) && n == 0)
7110 {
7111 /* Window configuration is the same as before.
7112 Can do with a display update of the echo area,
7113 unless we displayed some mode lines. */
7114 update_single_window (w, 1);
7115 rif->flush_display (f);
7116 }
7117 else
7118 update_frame (f, 1, 1);
7119
7120 /* If cursor is in the echo area, make sure that the next
7121 redisplay displays the minibuffer, so that the cursor will
7122 be replaced with what the minibuffer wants. */
7123 if (cursor_in_echo_area)
7124 ++windows_or_buffers_changed;
7125 }
7126 }
7127 else if (!EQ (mini_window, selected_window))
7128 windows_or_buffers_changed++;
7129
7130 /* Last displayed message is now the current message. */
7131 echo_area_buffer[1] = echo_area_buffer[0];
7132
7133 /* Prevent redisplay optimization in redisplay_internal by resetting
7134 this_line_start_pos. This is done because the mini-buffer now
7135 displays the message instead of its buffer text. */
7136 if (EQ (mini_window, selected_window))
7137 CHARPOS (this_line_start_pos) = 0;
7138
7139 return window_height_changed_p;
7140 }
7141
7142
7143 \f
7144 /***********************************************************************
7145 Frame Titles
7146 ***********************************************************************/
7147
7148
7149 #ifdef HAVE_WINDOW_SYSTEM
7150
7151 /* A buffer for constructing frame titles in it; allocated from the
7152 heap in init_xdisp and resized as needed in store_frame_title_char. */
7153
7154 static char *frame_title_buf;
7155
7156 /* The buffer's end, and a current output position in it. */
7157
7158 static char *frame_title_buf_end;
7159 static char *frame_title_ptr;
7160
7161
7162 /* Store a single character C for the frame title in frame_title_buf.
7163 Re-allocate frame_title_buf if necessary. */
7164
7165 static void
7166 store_frame_title_char (c)
7167 char c;
7168 {
7169 /* If output position has reached the end of the allocated buffer,
7170 double the buffer's size. */
7171 if (frame_title_ptr == frame_title_buf_end)
7172 {
7173 int len = frame_title_ptr - frame_title_buf;
7174 int new_size = 2 * len * sizeof *frame_title_buf;
7175 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7176 frame_title_buf_end = frame_title_buf + new_size;
7177 frame_title_ptr = frame_title_buf + len;
7178 }
7179
7180 *frame_title_ptr++ = c;
7181 }
7182
7183
7184 /* Store part of a frame title in frame_title_buf, beginning at
7185 frame_title_ptr. STR is the string to store. Do not copy
7186 characters that yield more columns than PRECISION; PRECISION <= 0
7187 means copy the whole string. Pad with spaces until FIELD_WIDTH
7188 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7189 pad. Called from display_mode_element when it is used to build a
7190 frame title. */
7191
7192 static int
7193 store_frame_title (str, field_width, precision)
7194 unsigned char *str;
7195 int field_width, precision;
7196 {
7197 int n = 0;
7198 int dummy, nbytes;
7199
7200 /* Copy at most PRECISION chars from STR. */
7201 nbytes = strlen (str);
7202 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7203 while (nbytes--)
7204 store_frame_title_char (*str++);
7205
7206 /* Fill up with spaces until FIELD_WIDTH reached. */
7207 while (field_width > 0
7208 && n < field_width)
7209 {
7210 store_frame_title_char (' ');
7211 ++n;
7212 }
7213
7214 return n;
7215 }
7216
7217
7218 /* Set the title of FRAME, if it has changed. The title format is
7219 Vicon_title_format if FRAME is iconified, otherwise it is
7220 frame_title_format. */
7221
7222 static void
7223 x_consider_frame_title (frame)
7224 Lisp_Object frame;
7225 {
7226 struct frame *f = XFRAME (frame);
7227
7228 if (FRAME_WINDOW_P (f)
7229 || FRAME_MINIBUF_ONLY_P (f)
7230 || f->explicit_name)
7231 {
7232 /* Do we have more than one visible frame on this X display? */
7233 Lisp_Object tail;
7234 Lisp_Object fmt;
7235 struct buffer *obuf;
7236 int len;
7237 struct it it;
7238
7239 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7240 {
7241 Lisp_Object other_frame = XCAR (tail);
7242 struct frame *tf = XFRAME (other_frame);
7243
7244 if (tf != f
7245 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7246 && !FRAME_MINIBUF_ONLY_P (tf)
7247 && !EQ (other_frame, tip_frame)
7248 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7249 break;
7250 }
7251
7252 /* Set global variable indicating that multiple frames exist. */
7253 multiple_frames = CONSP (tail);
7254
7255 /* Switch to the buffer of selected window of the frame. Set up
7256 frame_title_ptr so that display_mode_element will output into it;
7257 then display the title. */
7258 obuf = current_buffer;
7259 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7260 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7261 frame_title_ptr = frame_title_buf;
7262 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7263 NULL, DEFAULT_FACE_ID);
7264 display_mode_element (&it, 0, -1, -1, fmt, Qnil);
7265 len = frame_title_ptr - frame_title_buf;
7266 frame_title_ptr = NULL;
7267 set_buffer_internal_1 (obuf);
7268
7269 /* Set the title only if it's changed. This avoids consing in
7270 the common case where it hasn't. (If it turns out that we've
7271 already wasted too much time by walking through the list with
7272 display_mode_element, then we might need to optimize at a
7273 higher level than this.) */
7274 if (! STRINGP (f->name)
7275 || STRING_BYTES (XSTRING (f->name)) != len
7276 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7277 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7278 }
7279 }
7280
7281 #else /* not HAVE_WINDOW_SYSTEM */
7282
7283 #define frame_title_ptr ((char *)0)
7284 #define store_frame_title(str, mincol, maxcol) 0
7285
7286 #endif /* not HAVE_WINDOW_SYSTEM */
7287
7288
7289
7290 \f
7291 /***********************************************************************
7292 Menu Bars
7293 ***********************************************************************/
7294
7295
7296 /* Prepare for redisplay by updating menu-bar item lists when
7297 appropriate. This can call eval. */
7298
7299 void
7300 prepare_menu_bars ()
7301 {
7302 int all_windows;
7303 struct gcpro gcpro1, gcpro2;
7304 struct frame *f;
7305 Lisp_Object tooltip_frame;
7306
7307 #ifdef HAVE_WINDOW_SYSTEM
7308 tooltip_frame = tip_frame;
7309 #else
7310 tooltip_frame = Qnil;
7311 #endif
7312
7313 /* Update all frame titles based on their buffer names, etc. We do
7314 this before the menu bars so that the buffer-menu will show the
7315 up-to-date frame titles. */
7316 #ifdef HAVE_WINDOW_SYSTEM
7317 if (windows_or_buffers_changed || update_mode_lines)
7318 {
7319 Lisp_Object tail, frame;
7320
7321 FOR_EACH_FRAME (tail, frame)
7322 {
7323 f = XFRAME (frame);
7324 if (!EQ (frame, tooltip_frame)
7325 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7326 x_consider_frame_title (frame);
7327 }
7328 }
7329 #endif /* HAVE_WINDOW_SYSTEM */
7330
7331 /* Update the menu bar item lists, if appropriate. This has to be
7332 done before any actual redisplay or generation of display lines. */
7333 all_windows = (update_mode_lines
7334 || buffer_shared > 1
7335 || windows_or_buffers_changed);
7336 if (all_windows)
7337 {
7338 Lisp_Object tail, frame;
7339 int count = BINDING_STACK_SIZE ();
7340
7341 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7342
7343 FOR_EACH_FRAME (tail, frame)
7344 {
7345 f = XFRAME (frame);
7346
7347 /* Ignore tooltip frame. */
7348 if (EQ (frame, tooltip_frame))
7349 continue;
7350
7351 /* If a window on this frame changed size, report that to
7352 the user and clear the size-change flag. */
7353 if (FRAME_WINDOW_SIZES_CHANGED (f))
7354 {
7355 Lisp_Object functions;
7356
7357 /* Clear flag first in case we get an error below. */
7358 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7359 functions = Vwindow_size_change_functions;
7360 GCPRO2 (tail, functions);
7361
7362 while (CONSP (functions))
7363 {
7364 call1 (XCAR (functions), frame);
7365 functions = XCDR (functions);
7366 }
7367 UNGCPRO;
7368 }
7369
7370 GCPRO1 (tail);
7371 update_menu_bar (f, 0);
7372 #ifdef HAVE_WINDOW_SYSTEM
7373 update_tool_bar (f, 0);
7374 #endif
7375 UNGCPRO;
7376 }
7377
7378 unbind_to (count, Qnil);
7379 }
7380 else
7381 {
7382 struct frame *sf = SELECTED_FRAME ();
7383 update_menu_bar (sf, 1);
7384 #ifdef HAVE_WINDOW_SYSTEM
7385 update_tool_bar (sf, 1);
7386 #endif
7387 }
7388
7389 /* Motif needs this. See comment in xmenu.c. Turn it off when
7390 pending_menu_activation is not defined. */
7391 #ifdef USE_X_TOOLKIT
7392 pending_menu_activation = 0;
7393 #endif
7394 }
7395
7396
7397 /* Update the menu bar item list for frame F. This has to be done
7398 before we start to fill in any display lines, because it can call
7399 eval.
7400
7401 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7402
7403 static void
7404 update_menu_bar (f, save_match_data)
7405 struct frame *f;
7406 int save_match_data;
7407 {
7408 Lisp_Object window;
7409 register struct window *w;
7410
7411 /* If called recursively during a menu update, do nothing. This can
7412 happen when, for instance, an activate-menubar-hook causes a
7413 redisplay. */
7414 if (inhibit_menubar_update)
7415 return;
7416
7417 window = FRAME_SELECTED_WINDOW (f);
7418 w = XWINDOW (window);
7419
7420 if (update_mode_lines)
7421 w->update_mode_line = Qt;
7422
7423 if (FRAME_WINDOW_P (f)
7424 ?
7425 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7426 FRAME_EXTERNAL_MENU_BAR (f)
7427 #else
7428 FRAME_MENU_BAR_LINES (f) > 0
7429 #endif
7430 : FRAME_MENU_BAR_LINES (f) > 0)
7431 {
7432 /* If the user has switched buffers or windows, we need to
7433 recompute to reflect the new bindings. But we'll
7434 recompute when update_mode_lines is set too; that means
7435 that people can use force-mode-line-update to request
7436 that the menu bar be recomputed. The adverse effect on
7437 the rest of the redisplay algorithm is about the same as
7438 windows_or_buffers_changed anyway. */
7439 if (windows_or_buffers_changed
7440 || !NILP (w->update_mode_line)
7441 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7442 < BUF_MODIFF (XBUFFER (w->buffer)))
7443 != !NILP (w->last_had_star))
7444 || ((!NILP (Vtransient_mark_mode)
7445 && !NILP (XBUFFER (w->buffer)->mark_active))
7446 != !NILP (w->region_showing)))
7447 {
7448 struct buffer *prev = current_buffer;
7449 int count = BINDING_STACK_SIZE ();
7450
7451 specbind (Qinhibit_menubar_update, Qt);
7452
7453 set_buffer_internal_1 (XBUFFER (w->buffer));
7454 if (save_match_data)
7455 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7456 if (NILP (Voverriding_local_map_menu_flag))
7457 {
7458 specbind (Qoverriding_terminal_local_map, Qnil);
7459 specbind (Qoverriding_local_map, Qnil);
7460 }
7461
7462 /* Run the Lucid hook. */
7463 safe_run_hooks (Qactivate_menubar_hook);
7464
7465 /* If it has changed current-menubar from previous value,
7466 really recompute the menu-bar from the value. */
7467 if (! NILP (Vlucid_menu_bar_dirty_flag))
7468 call0 (Qrecompute_lucid_menubar);
7469
7470 safe_run_hooks (Qmenu_bar_update_hook);
7471 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7472
7473 /* Redisplay the menu bar in case we changed it. */
7474 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7475 if (FRAME_WINDOW_P (f)
7476 #if defined (macintosh)
7477 /* All frames on Mac OS share the same menubar. So only the
7478 selected frame should be allowed to set it. */
7479 && f == SELECTED_FRAME ()
7480 #endif
7481 )
7482 set_frame_menubar (f, 0, 0);
7483 else
7484 /* On a terminal screen, the menu bar is an ordinary screen
7485 line, and this makes it get updated. */
7486 w->update_mode_line = Qt;
7487 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7488 /* In the non-toolkit version, the menu bar is an ordinary screen
7489 line, and this makes it get updated. */
7490 w->update_mode_line = Qt;
7491 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7492
7493 unbind_to (count, Qnil);
7494 set_buffer_internal_1 (prev);
7495 }
7496 }
7497 }
7498
7499
7500 \f
7501 /***********************************************************************
7502 Tool-bars
7503 ***********************************************************************/
7504
7505 #ifdef HAVE_WINDOW_SYSTEM
7506
7507 /* Update the tool-bar item list for frame F. This has to be done
7508 before we start to fill in any display lines. Called from
7509 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7510 and restore it here. */
7511
7512 static void
7513 update_tool_bar (f, save_match_data)
7514 struct frame *f;
7515 int save_match_data;
7516 {
7517 if (WINDOWP (f->tool_bar_window)
7518 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7519 {
7520 Lisp_Object window;
7521 struct window *w;
7522
7523 window = FRAME_SELECTED_WINDOW (f);
7524 w = XWINDOW (window);
7525
7526 /* If the user has switched buffers or windows, we need to
7527 recompute to reflect the new bindings. But we'll
7528 recompute when update_mode_lines is set too; that means
7529 that people can use force-mode-line-update to request
7530 that the menu bar be recomputed. The adverse effect on
7531 the rest of the redisplay algorithm is about the same as
7532 windows_or_buffers_changed anyway. */
7533 if (windows_or_buffers_changed
7534 || !NILP (w->update_mode_line)
7535 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7536 < BUF_MODIFF (XBUFFER (w->buffer)))
7537 != !NILP (w->last_had_star))
7538 || ((!NILP (Vtransient_mark_mode)
7539 && !NILP (XBUFFER (w->buffer)->mark_active))
7540 != !NILP (w->region_showing)))
7541 {
7542 struct buffer *prev = current_buffer;
7543 int count = BINDING_STACK_SIZE ();
7544
7545 /* Set current_buffer to the buffer of the selected
7546 window of the frame, so that we get the right local
7547 keymaps. */
7548 set_buffer_internal_1 (XBUFFER (w->buffer));
7549
7550 /* Save match data, if we must. */
7551 if (save_match_data)
7552 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7553
7554 /* Make sure that we don't accidentally use bogus keymaps. */
7555 if (NILP (Voverriding_local_map_menu_flag))
7556 {
7557 specbind (Qoverriding_terminal_local_map, Qnil);
7558 specbind (Qoverriding_local_map, Qnil);
7559 }
7560
7561 /* Build desired tool-bar items from keymaps. */
7562 f->tool_bar_items
7563 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7564
7565 /* Redisplay the tool-bar in case we changed it. */
7566 w->update_mode_line = Qt;
7567
7568 unbind_to (count, Qnil);
7569 set_buffer_internal_1 (prev);
7570 }
7571 }
7572 }
7573
7574
7575 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7576 F's desired tool-bar contents. F->tool_bar_items must have
7577 been set up previously by calling prepare_menu_bars. */
7578
7579 static void
7580 build_desired_tool_bar_string (f)
7581 struct frame *f;
7582 {
7583 int i, size, size_needed;
7584 struct gcpro gcpro1, gcpro2, gcpro3;
7585 Lisp_Object image, plist, props;
7586
7587 image = plist = props = Qnil;
7588 GCPRO3 (image, plist, props);
7589
7590 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7591 Otherwise, make a new string. */
7592
7593 /* The size of the string we might be able to reuse. */
7594 size = (STRINGP (f->desired_tool_bar_string)
7595 ? XSTRING (f->desired_tool_bar_string)->size
7596 : 0);
7597
7598 /* We need one space in the string for each image. */
7599 size_needed = f->n_tool_bar_items;
7600
7601 /* Reuse f->desired_tool_bar_string, if possible. */
7602 if (size < size_needed || NILP (f->desired_tool_bar_string))
7603 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7604 make_number (' '));
7605 else
7606 {
7607 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7608 Fremove_text_properties (make_number (0), make_number (size),
7609 props, f->desired_tool_bar_string);
7610 }
7611
7612 /* Put a `display' property on the string for the images to display,
7613 put a `menu_item' property on tool-bar items with a value that
7614 is the index of the item in F's tool-bar item vector. */
7615 for (i = 0; i < f->n_tool_bar_items; ++i)
7616 {
7617 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7618
7619 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7620 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7621 int hmargin, vmargin, relief, idx, end;
7622 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7623
7624 /* If image is a vector, choose the image according to the
7625 button state. */
7626 image = PROP (TOOL_BAR_ITEM_IMAGES);
7627 if (VECTORP (image))
7628 {
7629 if (enabled_p)
7630 idx = (selected_p
7631 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7632 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7633 else
7634 idx = (selected_p
7635 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7636 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7637
7638 xassert (ASIZE (image) >= idx);
7639 image = AREF (image, idx);
7640 }
7641 else
7642 idx = -1;
7643
7644 /* Ignore invalid image specifications. */
7645 if (!valid_image_p (image))
7646 continue;
7647
7648 /* Display the tool-bar button pressed, or depressed. */
7649 plist = Fcopy_sequence (XCDR (image));
7650
7651 /* Compute margin and relief to draw. */
7652 relief = (tool_bar_button_relief >= 0
7653 ? tool_bar_button_relief
7654 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7655 hmargin = vmargin = relief;
7656
7657 if (INTEGERP (Vtool_bar_button_margin)
7658 && XINT (Vtool_bar_button_margin) > 0)
7659 {
7660 hmargin += XFASTINT (Vtool_bar_button_margin);
7661 vmargin += XFASTINT (Vtool_bar_button_margin);
7662 }
7663 else if (CONSP (Vtool_bar_button_margin))
7664 {
7665 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7666 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7667 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7668
7669 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7670 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7671 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7672 }
7673
7674 if (auto_raise_tool_bar_buttons_p)
7675 {
7676 /* Add a `:relief' property to the image spec if the item is
7677 selected. */
7678 if (selected_p)
7679 {
7680 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7681 hmargin -= relief;
7682 vmargin -= relief;
7683 }
7684 }
7685 else
7686 {
7687 /* If image is selected, display it pressed, i.e. with a
7688 negative relief. If it's not selected, display it with a
7689 raised relief. */
7690 plist = Fplist_put (plist, QCrelief,
7691 (selected_p
7692 ? make_number (-relief)
7693 : make_number (relief)));
7694 hmargin -= relief;
7695 vmargin -= relief;
7696 }
7697
7698 /* Put a margin around the image. */
7699 if (hmargin || vmargin)
7700 {
7701 if (hmargin == vmargin)
7702 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7703 else
7704 plist = Fplist_put (plist, QCmargin,
7705 Fcons (make_number (hmargin),
7706 make_number (vmargin)));
7707 }
7708
7709 /* If button is not enabled, and we don't have special images
7710 for the disabled state, make the image appear disabled by
7711 applying an appropriate algorithm to it. */
7712 if (!enabled_p && idx < 0)
7713 plist = Fplist_put (plist, QCconversion, Qdisabled);
7714
7715 /* Put a `display' text property on the string for the image to
7716 display. Put a `menu-item' property on the string that gives
7717 the start of this item's properties in the tool-bar items
7718 vector. */
7719 image = Fcons (Qimage, plist);
7720 props = list4 (Qdisplay, image,
7721 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7722
7723 /* Let the last image hide all remaining spaces in the tool bar
7724 string. The string can be longer than needed when we reuse a
7725 previous string. */
7726 if (i + 1 == f->n_tool_bar_items)
7727 end = XSTRING (f->desired_tool_bar_string)->size;
7728 else
7729 end = i + 1;
7730 Fadd_text_properties (make_number (i), make_number (end),
7731 props, f->desired_tool_bar_string);
7732 #undef PROP
7733 }
7734
7735 UNGCPRO;
7736 }
7737
7738
7739 /* Display one line of the tool-bar of frame IT->f. */
7740
7741 static void
7742 display_tool_bar_line (it)
7743 struct it *it;
7744 {
7745 struct glyph_row *row = it->glyph_row;
7746 int max_x = it->last_visible_x;
7747 struct glyph *last;
7748
7749 prepare_desired_row (row);
7750 row->y = it->current_y;
7751
7752 /* Note that this isn't made use of if the face hasn't a box,
7753 so there's no need to check the face here. */
7754 it->start_of_box_run_p = 1;
7755
7756 while (it->current_x < max_x)
7757 {
7758 int x_before, x, n_glyphs_before, i, nglyphs;
7759
7760 /* Get the next display element. */
7761 if (!get_next_display_element (it))
7762 break;
7763
7764 /* Produce glyphs. */
7765 x_before = it->current_x;
7766 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7767 PRODUCE_GLYPHS (it);
7768
7769 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7770 i = 0;
7771 x = x_before;
7772 while (i < nglyphs)
7773 {
7774 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7775
7776 if (x + glyph->pixel_width > max_x)
7777 {
7778 /* Glyph doesn't fit on line. */
7779 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7780 it->current_x = x;
7781 goto out;
7782 }
7783
7784 ++it->hpos;
7785 x += glyph->pixel_width;
7786 ++i;
7787 }
7788
7789 /* Stop at line ends. */
7790 if (ITERATOR_AT_END_OF_LINE_P (it))
7791 break;
7792
7793 set_iterator_to_next (it, 1);
7794 }
7795
7796 out:;
7797
7798 row->displays_text_p = row->used[TEXT_AREA] != 0;
7799 extend_face_to_end_of_line (it);
7800 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7801 last->right_box_line_p = 1;
7802 if (last == row->glyphs[TEXT_AREA])
7803 last->left_box_line_p = 1;
7804 compute_line_metrics (it);
7805
7806 /* If line is empty, make it occupy the rest of the tool-bar. */
7807 if (!row->displays_text_p)
7808 {
7809 row->height = row->phys_height = it->last_visible_y - row->y;
7810 row->ascent = row->phys_ascent = 0;
7811 }
7812
7813 row->full_width_p = 1;
7814 row->continued_p = 0;
7815 row->truncated_on_left_p = 0;
7816 row->truncated_on_right_p = 0;
7817
7818 it->current_x = it->hpos = 0;
7819 it->current_y += row->height;
7820 ++it->vpos;
7821 ++it->glyph_row;
7822 }
7823
7824
7825 /* Value is the number of screen lines needed to make all tool-bar
7826 items of frame F visible. */
7827
7828 static int
7829 tool_bar_lines_needed (f)
7830 struct frame *f;
7831 {
7832 struct window *w = XWINDOW (f->tool_bar_window);
7833 struct it it;
7834
7835 /* Initialize an iterator for iteration over
7836 F->desired_tool_bar_string in the tool-bar window of frame F. */
7837 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7838 it.first_visible_x = 0;
7839 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7840 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7841
7842 while (!ITERATOR_AT_END_P (&it))
7843 {
7844 it.glyph_row = w->desired_matrix->rows;
7845 clear_glyph_row (it.glyph_row);
7846 display_tool_bar_line (&it);
7847 }
7848
7849 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7850 }
7851
7852
7853 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7854 0, 1, 0,
7855 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7856 (frame)
7857 Lisp_Object frame;
7858 {
7859 struct frame *f;
7860 struct window *w;
7861 int nlines = 0;
7862
7863 if (NILP (frame))
7864 frame = selected_frame;
7865 else
7866 CHECK_FRAME (frame);
7867 f = XFRAME (frame);
7868
7869 if (WINDOWP (f->tool_bar_window)
7870 || (w = XWINDOW (f->tool_bar_window),
7871 XFASTINT (w->height) > 0))
7872 {
7873 update_tool_bar (f, 1);
7874 if (f->n_tool_bar_items)
7875 {
7876 build_desired_tool_bar_string (f);
7877 nlines = tool_bar_lines_needed (f);
7878 }
7879 }
7880
7881 return make_number (nlines);
7882 }
7883
7884
7885 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7886 height should be changed. */
7887
7888 static int
7889 redisplay_tool_bar (f)
7890 struct frame *f;
7891 {
7892 struct window *w;
7893 struct it it;
7894 struct glyph_row *row;
7895 int change_height_p = 0;
7896
7897 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7898 do anything. This means you must start with tool-bar-lines
7899 non-zero to get the auto-sizing effect. Or in other words, you
7900 can turn off tool-bars by specifying tool-bar-lines zero. */
7901 if (!WINDOWP (f->tool_bar_window)
7902 || (w = XWINDOW (f->tool_bar_window),
7903 XFASTINT (w->height) == 0))
7904 return 0;
7905
7906 /* Set up an iterator for the tool-bar window. */
7907 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7908 it.first_visible_x = 0;
7909 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7910 row = it.glyph_row;
7911
7912 /* Build a string that represents the contents of the tool-bar. */
7913 build_desired_tool_bar_string (f);
7914 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7915
7916 /* Display as many lines as needed to display all tool-bar items. */
7917 while (it.current_y < it.last_visible_y)
7918 display_tool_bar_line (&it);
7919
7920 /* It doesn't make much sense to try scrolling in the tool-bar
7921 window, so don't do it. */
7922 w->desired_matrix->no_scrolling_p = 1;
7923 w->must_be_updated_p = 1;
7924
7925 if (auto_resize_tool_bars_p)
7926 {
7927 int nlines;
7928
7929 /* If we couldn't display everything, change the tool-bar's
7930 height. */
7931 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7932 change_height_p = 1;
7933
7934 /* If there are blank lines at the end, except for a partially
7935 visible blank line at the end that is smaller than
7936 CANON_Y_UNIT, change the tool-bar's height. */
7937 row = it.glyph_row - 1;
7938 if (!row->displays_text_p
7939 && row->height >= CANON_Y_UNIT (f))
7940 change_height_p = 1;
7941
7942 /* If row displays tool-bar items, but is partially visible,
7943 change the tool-bar's height. */
7944 if (row->displays_text_p
7945 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7946 change_height_p = 1;
7947
7948 /* Resize windows as needed by changing the `tool-bar-lines'
7949 frame parameter. */
7950 if (change_height_p
7951 && (nlines = tool_bar_lines_needed (f),
7952 nlines != XFASTINT (w->height)))
7953 {
7954 extern Lisp_Object Qtool_bar_lines;
7955 Lisp_Object frame;
7956 int old_height = XFASTINT (w->height);
7957
7958 XSETFRAME (frame, f);
7959 clear_glyph_matrix (w->desired_matrix);
7960 Fmodify_frame_parameters (frame,
7961 Fcons (Fcons (Qtool_bar_lines,
7962 make_number (nlines)),
7963 Qnil));
7964 if (XFASTINT (w->height) != old_height)
7965 fonts_changed_p = 1;
7966 }
7967 }
7968
7969 return change_height_p;
7970 }
7971
7972
7973 /* Get information about the tool-bar item which is displayed in GLYPH
7974 on frame F. Return in *PROP_IDX the index where tool-bar item
7975 properties start in F->tool_bar_items. Value is zero if
7976 GLYPH doesn't display a tool-bar item. */
7977
7978 int
7979 tool_bar_item_info (f, glyph, prop_idx)
7980 struct frame *f;
7981 struct glyph *glyph;
7982 int *prop_idx;
7983 {
7984 Lisp_Object prop;
7985 int success_p;
7986 int charpos;
7987
7988 /* This function can be called asynchronously, which means we must
7989 exclude any possibility that Fget_text_property signals an
7990 error. */
7991 charpos = min (XSTRING (f->current_tool_bar_string)->size, glyph->charpos);
7992 charpos = max (0, charpos);
7993
7994 /* Get the text property `menu-item' at pos. The value of that
7995 property is the start index of this item's properties in
7996 F->tool_bar_items. */
7997 prop = Fget_text_property (make_number (charpos),
7998 Qmenu_item, f->current_tool_bar_string);
7999 if (INTEGERP (prop))
8000 {
8001 *prop_idx = XINT (prop);
8002 success_p = 1;
8003 }
8004 else
8005 success_p = 0;
8006
8007 return success_p;
8008 }
8009
8010 #endif /* HAVE_WINDOW_SYSTEM */
8011
8012
8013 \f
8014 /************************************************************************
8015 Horizontal scrolling
8016 ************************************************************************/
8017
8018 static int hscroll_window_tree P_ ((Lisp_Object));
8019 static int hscroll_windows P_ ((Lisp_Object));
8020
8021 /* For all leaf windows in the window tree rooted at WINDOW, set their
8022 hscroll value so that PT is (i) visible in the window, and (ii) so
8023 that it is not within a certain margin at the window's left and
8024 right border. Value is non-zero if any window's hscroll has been
8025 changed. */
8026
8027 static int
8028 hscroll_window_tree (window)
8029 Lisp_Object window;
8030 {
8031 int hscrolled_p = 0;
8032 int hscroll_relative_p = FLOATP (Vautomatic_hscroll_step);
8033 int hscroll_step_abs = 0;
8034 double hscroll_step_rel = 0;
8035
8036 if (hscroll_relative_p)
8037 {
8038 hscroll_step_rel = XFLOAT_DATA (Vautomatic_hscroll_step);
8039 if (hscroll_step_rel < 0)
8040 {
8041 hscroll_relative_p = 0;
8042 hscroll_step_abs = 0;
8043 }
8044 }
8045 else if (INTEGERP (Vautomatic_hscroll_step))
8046 {
8047 hscroll_step_abs = XINT (Vautomatic_hscroll_step);
8048 if (hscroll_step_abs < 0)
8049 hscroll_step_abs = 0;
8050 }
8051 else
8052 hscroll_step_abs = 0;
8053
8054 while (WINDOWP (window))
8055 {
8056 struct window *w = XWINDOW (window);
8057
8058 if (WINDOWP (w->hchild))
8059 hscrolled_p |= hscroll_window_tree (w->hchild);
8060 else if (WINDOWP (w->vchild))
8061 hscrolled_p |= hscroll_window_tree (w->vchild);
8062 else if (w->cursor.vpos >= 0)
8063 {
8064 int hscroll_margin, text_area_x, text_area_y;
8065 int text_area_width, text_area_height;
8066 struct glyph_row *current_cursor_row
8067 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8068 struct glyph_row *desired_cursor_row
8069 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8070 struct glyph_row *cursor_row
8071 = (desired_cursor_row->enabled_p
8072 ? desired_cursor_row
8073 : current_cursor_row);
8074
8075 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8076 &text_area_width, &text_area_height);
8077
8078 /* Scroll when cursor is inside this scroll margin. */
8079 hscroll_margin
8080 = automatic_hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
8081
8082 if ((XFASTINT (w->hscroll)
8083 && w->cursor.x <= hscroll_margin)
8084 || (cursor_row->enabled_p
8085 && cursor_row->truncated_on_right_p
8086 && (w->cursor.x >= text_area_width - hscroll_margin)))
8087 {
8088 struct it it;
8089 int hscroll;
8090 struct buffer *saved_current_buffer;
8091 int pt;
8092 int wanted_x;
8093
8094 /* Find point in a display of infinite width. */
8095 saved_current_buffer = current_buffer;
8096 current_buffer = XBUFFER (w->buffer);
8097
8098 if (w == XWINDOW (selected_window))
8099 pt = BUF_PT (current_buffer);
8100 else
8101 {
8102 pt = marker_position (w->pointm);
8103 pt = max (BEGV, pt);
8104 pt = min (ZV, pt);
8105 }
8106
8107 /* Move iterator to pt starting at cursor_row->start in
8108 a line with infinite width. */
8109 init_to_row_start (&it, w, cursor_row);
8110 it.last_visible_x = INFINITY;
8111 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8112 current_buffer = saved_current_buffer;
8113
8114 /* Position cursor in window. */
8115 if (!hscroll_relative_p && hscroll_step_abs == 0)
8116 hscroll = max (0, it.current_x - text_area_width / 2)
8117 / CANON_X_UNIT (it.f);
8118 else if (w->cursor.x >= text_area_width - hscroll_margin)
8119 {
8120 if (hscroll_relative_p)
8121 wanted_x = text_area_width * (1 - hscroll_step_rel)
8122 - hscroll_margin;
8123 else
8124 wanted_x = text_area_width
8125 - hscroll_step_abs * CANON_X_UNIT (it.f)
8126 - hscroll_margin;
8127 hscroll
8128 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8129 }
8130 else
8131 {
8132 if (hscroll_relative_p)
8133 wanted_x = text_area_width * hscroll_step_rel
8134 + hscroll_margin;
8135 else
8136 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
8137 + hscroll_margin;
8138 hscroll
8139 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8140 }
8141 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8142
8143 /* Don't call Fset_window_hscroll if value hasn't
8144 changed because it will prevent redisplay
8145 optimizations. */
8146 if (XFASTINT (w->hscroll) != hscroll)
8147 {
8148 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8149 w->hscroll = make_number (hscroll);
8150 hscrolled_p = 1;
8151 }
8152 }
8153 }
8154
8155 window = w->next;
8156 }
8157
8158 /* Value is non-zero if hscroll of any leaf window has been changed. */
8159 return hscrolled_p;
8160 }
8161
8162
8163 /* Set hscroll so that cursor is visible and not inside horizontal
8164 scroll margins for all windows in the tree rooted at WINDOW. See
8165 also hscroll_window_tree above. Value is non-zero if any window's
8166 hscroll has been changed. If it has, desired matrices on the frame
8167 of WINDOW are cleared. */
8168
8169 static int
8170 hscroll_windows (window)
8171 Lisp_Object window;
8172 {
8173 int hscrolled_p;
8174
8175 if (automatic_hscrolling_p)
8176 {
8177 hscrolled_p = hscroll_window_tree (window);
8178 if (hscrolled_p)
8179 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8180 }
8181 else
8182 hscrolled_p = 0;
8183 return hscrolled_p;
8184 }
8185
8186
8187 \f
8188 /************************************************************************
8189 Redisplay
8190 ************************************************************************/
8191
8192 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8193 to a non-zero value. This is sometimes handy to have in a debugger
8194 session. */
8195
8196 #if GLYPH_DEBUG
8197
8198 /* First and last unchanged row for try_window_id. */
8199
8200 int debug_first_unchanged_at_end_vpos;
8201 int debug_last_unchanged_at_beg_vpos;
8202
8203 /* Delta vpos and y. */
8204
8205 int debug_dvpos, debug_dy;
8206
8207 /* Delta in characters and bytes for try_window_id. */
8208
8209 int debug_delta, debug_delta_bytes;
8210
8211 /* Values of window_end_pos and window_end_vpos at the end of
8212 try_window_id. */
8213
8214 int debug_end_pos, debug_end_vpos;
8215
8216 /* Append a string to W->desired_matrix->method. FMT is a printf
8217 format string. A1...A9 are a supplement for a variable-length
8218 argument list. If trace_redisplay_p is non-zero also printf the
8219 resulting string to stderr. */
8220
8221 static void
8222 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8223 struct window *w;
8224 char *fmt;
8225 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8226 {
8227 char buffer[512];
8228 char *method = w->desired_matrix->method;
8229 int len = strlen (method);
8230 int size = sizeof w->desired_matrix->method;
8231 int remaining = size - len - 1;
8232
8233 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8234 if (len && remaining)
8235 {
8236 method[len] = '|';
8237 --remaining, ++len;
8238 }
8239
8240 strncpy (method + len, buffer, remaining);
8241
8242 if (trace_redisplay_p)
8243 fprintf (stderr, "%p (%s): %s\n",
8244 w,
8245 ((BUFFERP (w->buffer)
8246 && STRINGP (XBUFFER (w->buffer)->name))
8247 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
8248 : "no buffer"),
8249 buffer);
8250 }
8251
8252 #endif /* GLYPH_DEBUG */
8253
8254
8255 /* This counter is used to clear the face cache every once in a while
8256 in redisplay_internal. It is incremented for each redisplay.
8257 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8258 cleared. */
8259
8260 #define CLEAR_FACE_CACHE_COUNT 500
8261 static int clear_face_cache_count;
8262
8263 /* Record the previous terminal frame we displayed. */
8264
8265 static struct frame *previous_terminal_frame;
8266
8267 /* Non-zero while redisplay_internal is in progress. */
8268
8269 int redisplaying_p;
8270
8271
8272 /* Value is non-zero if all changes in window W, which displays
8273 current_buffer, are in the text between START and END. START is a
8274 buffer position, END is given as a distance from Z. Used in
8275 redisplay_internal for display optimization. */
8276
8277 static INLINE int
8278 text_outside_line_unchanged_p (w, start, end)
8279 struct window *w;
8280 int start, end;
8281 {
8282 int unchanged_p = 1;
8283
8284 /* If text or overlays have changed, see where. */
8285 if (XFASTINT (w->last_modified) < MODIFF
8286 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8287 {
8288 /* Gap in the line? */
8289 if (GPT < start || Z - GPT < end)
8290 unchanged_p = 0;
8291
8292 /* Changes start in front of the line, or end after it? */
8293 if (unchanged_p
8294 && (BEG_UNCHANGED < start - 1
8295 || END_UNCHANGED < end))
8296 unchanged_p = 0;
8297
8298 /* If selective display, can't optimize if changes start at the
8299 beginning of the line. */
8300 if (unchanged_p
8301 && INTEGERP (current_buffer->selective_display)
8302 && XINT (current_buffer->selective_display) > 0
8303 && (BEG_UNCHANGED < start || GPT <= start))
8304 unchanged_p = 0;
8305
8306 /* If there are overlays at the start or end of the line, these
8307 may have overlay strings with newlines in them. A change at
8308 START, for instance, may actually concern the display of such
8309 overlay strings as well, and they are displayed on different
8310 lines. So, quickly rule out this case. (For the future, it
8311 might be desirable to implement something more telling than
8312 just BEG/END_UNCHANGED.) */
8313 if (unchanged_p)
8314 {
8315 if (BEG + BEG_UNCHANGED == start
8316 && overlay_touches_p (start))
8317 unchanged_p = 0;
8318 if (END_UNCHANGED == end
8319 && overlay_touches_p (Z - end))
8320 unchanged_p = 0;
8321 }
8322 }
8323
8324 return unchanged_p;
8325 }
8326
8327
8328 /* Do a frame update, taking possible shortcuts into account. This is
8329 the main external entry point for redisplay.
8330
8331 If the last redisplay displayed an echo area message and that message
8332 is no longer requested, we clear the echo area or bring back the
8333 mini-buffer if that is in use. */
8334
8335 void
8336 redisplay ()
8337 {
8338 redisplay_internal (0);
8339 }
8340
8341
8342 /* Return 1 if point moved out of or into a composition. Otherwise
8343 return 0. PREV_BUF and PREV_PT are the last point buffer and
8344 position. BUF and PT are the current point buffer and position. */
8345
8346 int
8347 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8348 struct buffer *prev_buf, *buf;
8349 int prev_pt, pt;
8350 {
8351 int start, end;
8352 Lisp_Object prop;
8353 Lisp_Object buffer;
8354
8355 XSETBUFFER (buffer, buf);
8356 /* Check a composition at the last point if point moved within the
8357 same buffer. */
8358 if (prev_buf == buf)
8359 {
8360 if (prev_pt == pt)
8361 /* Point didn't move. */
8362 return 0;
8363
8364 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8365 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8366 && COMPOSITION_VALID_P (start, end, prop)
8367 && start < prev_pt && end > prev_pt)
8368 /* The last point was within the composition. Return 1 iff
8369 point moved out of the composition. */
8370 return (pt <= start || pt >= end);
8371 }
8372
8373 /* Check a composition at the current point. */
8374 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8375 && find_composition (pt, -1, &start, &end, &prop, buffer)
8376 && COMPOSITION_VALID_P (start, end, prop)
8377 && start < pt && end > pt);
8378 }
8379
8380
8381 /* Reconsider the setting of B->clip_changed which is displayed
8382 in window W. */
8383
8384 static INLINE void
8385 reconsider_clip_changes (w, b)
8386 struct window *w;
8387 struct buffer *b;
8388 {
8389 if (b->prevent_redisplay_optimizations_p)
8390 b->clip_changed = 1;
8391 else if (b->clip_changed
8392 && !NILP (w->window_end_valid)
8393 && w->current_matrix->buffer == b
8394 && w->current_matrix->zv == BUF_ZV (b)
8395 && w->current_matrix->begv == BUF_BEGV (b))
8396 b->clip_changed = 0;
8397
8398 /* If display wasn't paused, and W is not a tool bar window, see if
8399 point has been moved into or out of a composition. In that case,
8400 we set b->clip_changed to 1 to force updating the screen. If
8401 b->clip_changed has already been set to 1, we can skip this
8402 check. */
8403 if (!b->clip_changed
8404 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8405 {
8406 int pt;
8407
8408 if (w == XWINDOW (selected_window))
8409 pt = BUF_PT (current_buffer);
8410 else
8411 pt = marker_position (w->pointm);
8412
8413 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8414 || pt != XINT (w->last_point))
8415 && check_point_in_composition (w->current_matrix->buffer,
8416 XINT (w->last_point),
8417 XBUFFER (w->buffer), pt))
8418 b->clip_changed = 1;
8419 }
8420 }
8421
8422
8423 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8424 response to any user action; therefore, we should preserve the echo
8425 area. (Actually, our caller does that job.) Perhaps in the future
8426 avoid recentering windows if it is not necessary; currently that
8427 causes some problems. */
8428
8429 static void
8430 redisplay_internal (preserve_echo_area)
8431 int preserve_echo_area;
8432 {
8433 struct window *w = XWINDOW (selected_window);
8434 struct frame *f = XFRAME (w->frame);
8435 int pause;
8436 int must_finish = 0;
8437 struct text_pos tlbufpos, tlendpos;
8438 int number_of_visible_frames;
8439 int count;
8440 struct frame *sf = SELECTED_FRAME ();
8441
8442 /* Non-zero means redisplay has to consider all windows on all
8443 frames. Zero means, only selected_window is considered. */
8444 int consider_all_windows_p;
8445
8446 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8447
8448 /* No redisplay if running in batch mode or frame is not yet fully
8449 initialized, or redisplay is explicitly turned off by setting
8450 Vinhibit_redisplay. */
8451 if (noninteractive
8452 || !NILP (Vinhibit_redisplay)
8453 || !f->glyphs_initialized_p)
8454 return;
8455
8456 /* The flag redisplay_performed_directly_p is set by
8457 direct_output_for_insert when it already did the whole screen
8458 update necessary. */
8459 if (redisplay_performed_directly_p)
8460 {
8461 redisplay_performed_directly_p = 0;
8462 if (!hscroll_windows (selected_window))
8463 return;
8464 }
8465
8466 #ifdef USE_X_TOOLKIT
8467 if (popup_activated ())
8468 return;
8469 #endif
8470
8471 /* I don't think this happens but let's be paranoid. */
8472 if (redisplaying_p)
8473 return;
8474
8475 /* Record a function that resets redisplaying_p to its old value
8476 when we leave this function. */
8477 count = BINDING_STACK_SIZE ();
8478 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8479 ++redisplaying_p;
8480
8481 retry:
8482 pause = 0;
8483 reconsider_clip_changes (w, current_buffer);
8484
8485 /* If new fonts have been loaded that make a glyph matrix adjustment
8486 necessary, do it. */
8487 if (fonts_changed_p)
8488 {
8489 adjust_glyphs (NULL);
8490 ++windows_or_buffers_changed;
8491 fonts_changed_p = 0;
8492 }
8493
8494 /* If face_change_count is non-zero, init_iterator will free all
8495 realized faces, which includes the faces referenced from current
8496 matrices. So, we can't reuse current matrices in this case. */
8497 if (face_change_count)
8498 ++windows_or_buffers_changed;
8499
8500 if (! FRAME_WINDOW_P (sf)
8501 && previous_terminal_frame != sf)
8502 {
8503 /* Since frames on an ASCII terminal share the same display
8504 area, displaying a different frame means redisplay the whole
8505 thing. */
8506 windows_or_buffers_changed++;
8507 SET_FRAME_GARBAGED (sf);
8508 XSETFRAME (Vterminal_frame, sf);
8509 }
8510 previous_terminal_frame = sf;
8511
8512 /* Set the visible flags for all frames. Do this before checking
8513 for resized or garbaged frames; they want to know if their frames
8514 are visible. See the comment in frame.h for
8515 FRAME_SAMPLE_VISIBILITY. */
8516 {
8517 Lisp_Object tail, frame;
8518
8519 number_of_visible_frames = 0;
8520
8521 FOR_EACH_FRAME (tail, frame)
8522 {
8523 struct frame *f = XFRAME (frame);
8524
8525 FRAME_SAMPLE_VISIBILITY (f);
8526 if (FRAME_VISIBLE_P (f))
8527 ++number_of_visible_frames;
8528 clear_desired_matrices (f);
8529 }
8530 }
8531
8532 /* Notice any pending interrupt request to change frame size. */
8533 do_pending_window_change (1);
8534
8535 /* Clear frames marked as garbaged. */
8536 if (frame_garbaged)
8537 clear_garbaged_frames ();
8538
8539 /* Build menubar and tool-bar items. */
8540 prepare_menu_bars ();
8541
8542 if (windows_or_buffers_changed)
8543 update_mode_lines++;
8544
8545 /* Detect case that we need to write or remove a star in the mode line. */
8546 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8547 {
8548 w->update_mode_line = Qt;
8549 if (buffer_shared > 1)
8550 update_mode_lines++;
8551 }
8552
8553 /* If %c is in the mode line, update it if needed. */
8554 if (!NILP (w->column_number_displayed)
8555 /* This alternative quickly identifies a common case
8556 where no change is needed. */
8557 && !(PT == XFASTINT (w->last_point)
8558 && XFASTINT (w->last_modified) >= MODIFF
8559 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8560 && XFASTINT (w->column_number_displayed) != current_column ())
8561 w->update_mode_line = Qt;
8562
8563 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8564
8565 /* The variable buffer_shared is set in redisplay_window and
8566 indicates that we redisplay a buffer in different windows. See
8567 there. */
8568 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8569
8570 /* If specs for an arrow have changed, do thorough redisplay
8571 to ensure we remove any arrow that should no longer exist. */
8572 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8573 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8574 consider_all_windows_p = windows_or_buffers_changed = 1;
8575
8576 /* Normally the message* functions will have already displayed and
8577 updated the echo area, but the frame may have been trashed, or
8578 the update may have been preempted, so display the echo area
8579 again here. Checking message_cleared_p captures the case that
8580 the echo area should be cleared. */
8581 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8582 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8583 || (message_cleared_p
8584 && minibuf_level == 0
8585 /* If the mini-window is currently selected, this means the
8586 echo-area doesn't show through. */
8587 && !MINI_WINDOW_P (XWINDOW (selected_window))))
8588 {
8589 int window_height_changed_p = echo_area_display (0);
8590 must_finish = 1;
8591
8592 /* If we don't display the current message, don't clear the
8593 message_cleared_p flag, because, if we did, we wouldn't clear
8594 the echo area in the next redisplay which doesn't preserve
8595 the echo area. */
8596 if (!display_last_displayed_message_p)
8597 message_cleared_p = 0;
8598
8599 if (fonts_changed_p)
8600 goto retry;
8601 else if (window_height_changed_p)
8602 {
8603 consider_all_windows_p = 1;
8604 ++update_mode_lines;
8605 ++windows_or_buffers_changed;
8606
8607 /* If window configuration was changed, frames may have been
8608 marked garbaged. Clear them or we will experience
8609 surprises wrt scrolling. */
8610 if (frame_garbaged)
8611 clear_garbaged_frames ();
8612 }
8613 }
8614 else if (EQ (selected_window, minibuf_window)
8615 && (current_buffer->clip_changed
8616 || XFASTINT (w->last_modified) < MODIFF
8617 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8618 && resize_mini_window (w, 0))
8619 {
8620 /* Resized active mini-window to fit the size of what it is
8621 showing if its contents might have changed. */
8622 must_finish = 1;
8623 consider_all_windows_p = 1;
8624 ++windows_or_buffers_changed;
8625 ++update_mode_lines;
8626
8627 /* If window configuration was changed, frames may have been
8628 marked garbaged. Clear them or we will experience
8629 surprises wrt scrolling. */
8630 if (frame_garbaged)
8631 clear_garbaged_frames ();
8632 }
8633
8634
8635 /* If showing the region, and mark has changed, we must redisplay
8636 the whole window. The assignment to this_line_start_pos prevents
8637 the optimization directly below this if-statement. */
8638 if (((!NILP (Vtransient_mark_mode)
8639 && !NILP (XBUFFER (w->buffer)->mark_active))
8640 != !NILP (w->region_showing))
8641 || (!NILP (w->region_showing)
8642 && !EQ (w->region_showing,
8643 Fmarker_position (XBUFFER (w->buffer)->mark))))
8644 CHARPOS (this_line_start_pos) = 0;
8645
8646 /* Optimize the case that only the line containing the cursor in the
8647 selected window has changed. Variables starting with this_ are
8648 set in display_line and record information about the line
8649 containing the cursor. */
8650 tlbufpos = this_line_start_pos;
8651 tlendpos = this_line_end_pos;
8652 if (!consider_all_windows_p
8653 && CHARPOS (tlbufpos) > 0
8654 && NILP (w->update_mode_line)
8655 && !current_buffer->clip_changed
8656 && FRAME_VISIBLE_P (XFRAME (w->frame))
8657 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8658 /* Make sure recorded data applies to current buffer, etc. */
8659 && this_line_buffer == current_buffer
8660 && current_buffer == XBUFFER (w->buffer)
8661 && NILP (w->force_start)
8662 /* Point must be on the line that we have info recorded about. */
8663 && PT >= CHARPOS (tlbufpos)
8664 && PT <= Z - CHARPOS (tlendpos)
8665 /* All text outside that line, including its final newline,
8666 must be unchanged */
8667 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8668 CHARPOS (tlendpos)))
8669 {
8670 if (CHARPOS (tlbufpos) > BEGV
8671 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8672 && (CHARPOS (tlbufpos) == ZV
8673 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8674 /* Former continuation line has disappeared by becoming empty */
8675 goto cancel;
8676 else if (XFASTINT (w->last_modified) < MODIFF
8677 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8678 || MINI_WINDOW_P (w))
8679 {
8680 /* We have to handle the case of continuation around a
8681 wide-column character (See the comment in indent.c around
8682 line 885).
8683
8684 For instance, in the following case:
8685
8686 -------- Insert --------
8687 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8688 J_I_ ==> J_I_ `^^' are cursors.
8689 ^^ ^^
8690 -------- --------
8691
8692 As we have to redraw the line above, we should goto cancel. */
8693
8694 struct it it;
8695 int line_height_before = this_line_pixel_height;
8696
8697 /* Note that start_display will handle the case that the
8698 line starting at tlbufpos is a continuation lines. */
8699 start_display (&it, w, tlbufpos);
8700
8701 /* Implementation note: It this still necessary? */
8702 if (it.current_x != this_line_start_x)
8703 goto cancel;
8704
8705 TRACE ((stderr, "trying display optimization 1\n"));
8706 w->cursor.vpos = -1;
8707 overlay_arrow_seen = 0;
8708 it.vpos = this_line_vpos;
8709 it.current_y = this_line_y;
8710 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8711 display_line (&it);
8712
8713 /* If line contains point, is not continued,
8714 and ends at same distance from eob as before, we win */
8715 if (w->cursor.vpos >= 0
8716 /* Line is not continued, otherwise this_line_start_pos
8717 would have been set to 0 in display_line. */
8718 && CHARPOS (this_line_start_pos)
8719 /* Line ends as before. */
8720 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8721 /* Line has same height as before. Otherwise other lines
8722 would have to be shifted up or down. */
8723 && this_line_pixel_height == line_height_before)
8724 {
8725 /* If this is not the window's last line, we must adjust
8726 the charstarts of the lines below. */
8727 if (it.current_y < it.last_visible_y)
8728 {
8729 struct glyph_row *row
8730 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8731 int delta, delta_bytes;
8732
8733 if (Z - CHARPOS (tlendpos) == ZV)
8734 {
8735 /* This line ends at end of (accessible part of)
8736 buffer. There is no newline to count. */
8737 delta = (Z
8738 - CHARPOS (tlendpos)
8739 - MATRIX_ROW_START_CHARPOS (row));
8740 delta_bytes = (Z_BYTE
8741 - BYTEPOS (tlendpos)
8742 - MATRIX_ROW_START_BYTEPOS (row));
8743 }
8744 else
8745 {
8746 /* This line ends in a newline. Must take
8747 account of the newline and the rest of the
8748 text that follows. */
8749 delta = (Z
8750 - CHARPOS (tlendpos)
8751 - MATRIX_ROW_START_CHARPOS (row));
8752 delta_bytes = (Z_BYTE
8753 - BYTEPOS (tlendpos)
8754 - MATRIX_ROW_START_BYTEPOS (row));
8755 }
8756
8757 increment_matrix_positions (w->current_matrix,
8758 this_line_vpos + 1,
8759 w->current_matrix->nrows,
8760 delta, delta_bytes);
8761 }
8762
8763 /* If this row displays text now but previously didn't,
8764 or vice versa, w->window_end_vpos may have to be
8765 adjusted. */
8766 if ((it.glyph_row - 1)->displays_text_p)
8767 {
8768 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8769 XSETINT (w->window_end_vpos, this_line_vpos);
8770 }
8771 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8772 && this_line_vpos > 0)
8773 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8774 w->window_end_valid = Qnil;
8775
8776 /* Update hint: No need to try to scroll in update_window. */
8777 w->desired_matrix->no_scrolling_p = 1;
8778
8779 #if GLYPH_DEBUG
8780 *w->desired_matrix->method = 0;
8781 debug_method_add (w, "optimization 1");
8782 #endif
8783 goto update;
8784 }
8785 else
8786 goto cancel;
8787 }
8788 else if (/* Cursor position hasn't changed. */
8789 PT == XFASTINT (w->last_point)
8790 /* Make sure the cursor was last displayed
8791 in this window. Otherwise we have to reposition it. */
8792 && 0 <= w->cursor.vpos
8793 && XINT (w->height) > w->cursor.vpos)
8794 {
8795 if (!must_finish)
8796 {
8797 do_pending_window_change (1);
8798
8799 /* We used to always goto end_of_redisplay here, but this
8800 isn't enough if we have a blinking cursor. */
8801 if (w->cursor_off_p == w->last_cursor_off_p)
8802 goto end_of_redisplay;
8803 }
8804 goto update;
8805 }
8806 /* If highlighting the region, or if the cursor is in the echo area,
8807 then we can't just move the cursor. */
8808 else if (! (!NILP (Vtransient_mark_mode)
8809 && !NILP (current_buffer->mark_active))
8810 && (EQ (selected_window, current_buffer->last_selected_window)
8811 || highlight_nonselected_windows)
8812 && NILP (w->region_showing)
8813 && NILP (Vshow_trailing_whitespace)
8814 && !cursor_in_echo_area)
8815 {
8816 struct it it;
8817 struct glyph_row *row;
8818
8819 /* Skip from tlbufpos to PT and see where it is. Note that
8820 PT may be in invisible text. If so, we will end at the
8821 next visible position. */
8822 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8823 NULL, DEFAULT_FACE_ID);
8824 it.current_x = this_line_start_x;
8825 it.current_y = this_line_y;
8826 it.vpos = this_line_vpos;
8827
8828 /* The call to move_it_to stops in front of PT, but
8829 moves over before-strings. */
8830 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8831
8832 if (it.vpos == this_line_vpos
8833 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8834 row->enabled_p))
8835 {
8836 xassert (this_line_vpos == it.vpos);
8837 xassert (this_line_y == it.current_y);
8838 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8839 #if GLYPH_DEBUG
8840 *w->desired_matrix->method = 0;
8841 debug_method_add (w, "optimization 3");
8842 #endif
8843 goto update;
8844 }
8845 else
8846 goto cancel;
8847 }
8848
8849 cancel:
8850 /* Text changed drastically or point moved off of line. */
8851 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8852 }
8853
8854 CHARPOS (this_line_start_pos) = 0;
8855 consider_all_windows_p |= buffer_shared > 1;
8856 ++clear_face_cache_count;
8857
8858
8859 /* Build desired matrices, and update the display. If
8860 consider_all_windows_p is non-zero, do it for all windows on all
8861 frames. Otherwise do it for selected_window, only. */
8862
8863 if (consider_all_windows_p)
8864 {
8865 Lisp_Object tail, frame;
8866 int i, n = 0, size = 50;
8867 struct frame **updated
8868 = (struct frame **) alloca (size * sizeof *updated);
8869
8870 /* Clear the face cache eventually. */
8871 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8872 {
8873 clear_face_cache (0);
8874 clear_face_cache_count = 0;
8875 }
8876
8877 /* Recompute # windows showing selected buffer. This will be
8878 incremented each time such a window is displayed. */
8879 buffer_shared = 0;
8880
8881 FOR_EACH_FRAME (tail, frame)
8882 {
8883 struct frame *f = XFRAME (frame);
8884
8885 if (FRAME_WINDOW_P (f) || f == sf)
8886 {
8887 if (clear_face_cache_count % 50 == 0
8888 && FRAME_WINDOW_P (f))
8889 clear_image_cache (f, 0);
8890
8891 /* Mark all the scroll bars to be removed; we'll redeem
8892 the ones we want when we redisplay their windows. */
8893 if (condemn_scroll_bars_hook)
8894 condemn_scroll_bars_hook (f);
8895
8896 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8897 redisplay_windows (FRAME_ROOT_WINDOW (f));
8898
8899 /* Any scroll bars which redisplay_windows should have
8900 nuked should now go away. */
8901 if (judge_scroll_bars_hook)
8902 judge_scroll_bars_hook (f);
8903
8904 /* If fonts changed, display again. */
8905 if (fonts_changed_p)
8906 goto retry;
8907
8908 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8909 {
8910 /* See if we have to hscroll. */
8911 if (hscroll_windows (f->root_window))
8912 goto retry;
8913
8914 /* Prevent various kinds of signals during display
8915 update. stdio is not robust about handling
8916 signals, which can cause an apparent I/O
8917 error. */
8918 if (interrupt_input)
8919 unrequest_sigio ();
8920 stop_polling ();
8921
8922 /* Update the display. */
8923 set_window_update_flags (XWINDOW (f->root_window), 1);
8924 pause |= update_frame (f, 0, 0);
8925 if (pause)
8926 break;
8927
8928 if (n == size)
8929 {
8930 int nbytes = size * sizeof *updated;
8931 struct frame **p = (struct frame **) alloca (2 * nbytes);
8932 bcopy (updated, p, nbytes);
8933 size *= 2;
8934 }
8935
8936 updated[n++] = f;
8937 }
8938 }
8939 }
8940
8941 /* Do the mark_window_display_accurate after all windows have
8942 been redisplayed because this call resets flags in buffers
8943 which are needed for proper redisplay. */
8944 for (i = 0; i < n; ++i)
8945 {
8946 struct frame *f = updated[i];
8947 mark_window_display_accurate (f->root_window, 1);
8948 if (frame_up_to_date_hook)
8949 frame_up_to_date_hook (f);
8950 }
8951 }
8952 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8953 {
8954 Lisp_Object mini_window;
8955 struct frame *mini_frame;
8956
8957 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
8958 internal_condition_case_1 (redisplay_window_1, selected_window, Qerror,
8959 redisplay_window_error);
8960
8961 /* Compare desired and current matrices, perform output. */
8962 update:
8963
8964 /* If fonts changed, display again. */
8965 if (fonts_changed_p)
8966 goto retry;
8967
8968 /* Prevent various kinds of signals during display update.
8969 stdio is not robust about handling signals,
8970 which can cause an apparent I/O error. */
8971 if (interrupt_input)
8972 unrequest_sigio ();
8973 stop_polling ();
8974
8975 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8976 {
8977 if (hscroll_windows (selected_window))
8978 goto retry;
8979
8980 XWINDOW (selected_window)->must_be_updated_p = 1;
8981 pause = update_frame (sf, 0, 0);
8982 }
8983
8984 /* We may have called echo_area_display at the top of this
8985 function. If the echo area is on another frame, that may
8986 have put text on a frame other than the selected one, so the
8987 above call to update_frame would not have caught it. Catch
8988 it here. */
8989 mini_window = FRAME_MINIBUF_WINDOW (sf);
8990 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8991
8992 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8993 {
8994 XWINDOW (mini_window)->must_be_updated_p = 1;
8995 pause |= update_frame (mini_frame, 0, 0);
8996 if (!pause && hscroll_windows (mini_window))
8997 goto retry;
8998 }
8999 }
9000
9001 /* If display was paused because of pending input, make sure we do a
9002 thorough update the next time. */
9003 if (pause)
9004 {
9005 /* Prevent the optimization at the beginning of
9006 redisplay_internal that tries a single-line update of the
9007 line containing the cursor in the selected window. */
9008 CHARPOS (this_line_start_pos) = 0;
9009
9010 /* Let the overlay arrow be updated the next time. */
9011 if (!NILP (last_arrow_position))
9012 {
9013 last_arrow_position = Qt;
9014 last_arrow_string = Qt;
9015 }
9016
9017 /* If we pause after scrolling, some rows in the current
9018 matrices of some windows are not valid. */
9019 if (!WINDOW_FULL_WIDTH_P (w)
9020 && !FRAME_WINDOW_P (XFRAME (w->frame)))
9021 update_mode_lines = 1;
9022 }
9023 else
9024 {
9025 if (!consider_all_windows_p)
9026 {
9027 /* This has already been done above if
9028 consider_all_windows_p is set. */
9029 mark_window_display_accurate_1 (w, 1);
9030
9031 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9032 last_arrow_string = Voverlay_arrow_string;
9033
9034 if (frame_up_to_date_hook != 0)
9035 frame_up_to_date_hook (sf);
9036 }
9037
9038 update_mode_lines = 0;
9039 windows_or_buffers_changed = 0;
9040 }
9041
9042 /* Start SIGIO interrupts coming again. Having them off during the
9043 code above makes it less likely one will discard output, but not
9044 impossible, since there might be stuff in the system buffer here.
9045 But it is much hairier to try to do anything about that. */
9046 if (interrupt_input)
9047 request_sigio ();
9048 start_polling ();
9049
9050 /* If a frame has become visible which was not before, redisplay
9051 again, so that we display it. Expose events for such a frame
9052 (which it gets when becoming visible) don't call the parts of
9053 redisplay constructing glyphs, so simply exposing a frame won't
9054 display anything in this case. So, we have to display these
9055 frames here explicitly. */
9056 if (!pause)
9057 {
9058 Lisp_Object tail, frame;
9059 int new_count = 0;
9060
9061 FOR_EACH_FRAME (tail, frame)
9062 {
9063 int this_is_visible = 0;
9064
9065 if (XFRAME (frame)->visible)
9066 this_is_visible = 1;
9067 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9068 if (XFRAME (frame)->visible)
9069 this_is_visible = 1;
9070
9071 if (this_is_visible)
9072 new_count++;
9073 }
9074
9075 if (new_count != number_of_visible_frames)
9076 windows_or_buffers_changed++;
9077 }
9078
9079 /* Change frame size now if a change is pending. */
9080 do_pending_window_change (1);
9081
9082 /* If we just did a pending size change, or have additional
9083 visible frames, redisplay again. */
9084 if (windows_or_buffers_changed && !pause)
9085 goto retry;
9086
9087 end_of_redisplay:;
9088
9089 unbind_to (count, Qnil);
9090 }
9091
9092
9093 /* Redisplay, but leave alone any recent echo area message unless
9094 another message has been requested in its place.
9095
9096 This is useful in situations where you need to redisplay but no
9097 user action has occurred, making it inappropriate for the message
9098 area to be cleared. See tracking_off and
9099 wait_reading_process_input for examples of these situations.
9100
9101 FROM_WHERE is an integer saying from where this function was
9102 called. This is useful for debugging. */
9103
9104 void
9105 redisplay_preserve_echo_area (from_where)
9106 int from_where;
9107 {
9108 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9109
9110 if (!NILP (echo_area_buffer[1]))
9111 {
9112 /* We have a previously displayed message, but no current
9113 message. Redisplay the previous message. */
9114 display_last_displayed_message_p = 1;
9115 redisplay_internal (1);
9116 display_last_displayed_message_p = 0;
9117 }
9118 else
9119 redisplay_internal (1);
9120 }
9121
9122
9123 /* Function registered with record_unwind_protect in
9124 redisplay_internal. Clears the flag indicating that a redisplay is
9125 in progress. */
9126
9127 static Lisp_Object
9128 unwind_redisplay (old_redisplaying_p)
9129 Lisp_Object old_redisplaying_p;
9130 {
9131 redisplaying_p = XFASTINT (old_redisplaying_p);
9132 return Qnil;
9133 }
9134
9135
9136 /* Mark the display of window W as accurate or inaccurate. If
9137 ACCURATE_P is non-zero mark display of W as accurate. If
9138 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9139 redisplay_internal is called. */
9140
9141 static void
9142 mark_window_display_accurate_1 (w, accurate_p)
9143 struct window *w;
9144 int accurate_p;
9145 {
9146 if (BUFFERP (w->buffer))
9147 {
9148 struct buffer *b = XBUFFER (w->buffer);
9149
9150 w->last_modified
9151 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9152 w->last_overlay_modified
9153 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9154 w->last_had_star
9155 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9156
9157 if (accurate_p)
9158 {
9159 b->clip_changed = 0;
9160 b->prevent_redisplay_optimizations_p = 0;
9161
9162 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9163 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9164 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9165 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9166
9167 w->current_matrix->buffer = b;
9168 w->current_matrix->begv = BUF_BEGV (b);
9169 w->current_matrix->zv = BUF_ZV (b);
9170
9171 w->last_cursor = w->cursor;
9172 w->last_cursor_off_p = w->cursor_off_p;
9173
9174 if (w == XWINDOW (selected_window))
9175 w->last_point = make_number (BUF_PT (b));
9176 else
9177 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9178 }
9179 }
9180
9181 if (accurate_p)
9182 {
9183 w->window_end_valid = w->buffer;
9184 #if 0 /* This is incorrect with variable-height lines. */
9185 xassert (XINT (w->window_end_vpos)
9186 < (XINT (w->height)
9187 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9188 #endif
9189 w->update_mode_line = Qnil;
9190 }
9191 }
9192
9193
9194 /* Mark the display of windows in the window tree rooted at WINDOW as
9195 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9196 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9197 be redisplayed the next time redisplay_internal is called. */
9198
9199 void
9200 mark_window_display_accurate (window, accurate_p)
9201 Lisp_Object window;
9202 int accurate_p;
9203 {
9204 struct window *w;
9205
9206 for (; !NILP (window); window = w->next)
9207 {
9208 w = XWINDOW (window);
9209 mark_window_display_accurate_1 (w, accurate_p);
9210
9211 if (!NILP (w->vchild))
9212 mark_window_display_accurate (w->vchild, accurate_p);
9213 if (!NILP (w->hchild))
9214 mark_window_display_accurate (w->hchild, accurate_p);
9215 }
9216
9217 if (accurate_p)
9218 {
9219 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9220 last_arrow_string = Voverlay_arrow_string;
9221 }
9222 else
9223 {
9224 /* Force a thorough redisplay the next time by setting
9225 last_arrow_position and last_arrow_string to t, which is
9226 unequal to any useful value of Voverlay_arrow_... */
9227 last_arrow_position = Qt;
9228 last_arrow_string = Qt;
9229 }
9230 }
9231
9232
9233 /* Return value in display table DP (Lisp_Char_Table *) for character
9234 C. Since a display table doesn't have any parent, we don't have to
9235 follow parent. Do not call this function directly but use the
9236 macro DISP_CHAR_VECTOR. */
9237
9238 Lisp_Object
9239 disp_char_vector (dp, c)
9240 struct Lisp_Char_Table *dp;
9241 int c;
9242 {
9243 Lisp_Object val;
9244
9245 if (ASCII_CHAR_P (c))
9246 {
9247 val = dp->ascii;
9248 if (SUB_CHAR_TABLE_P (val))
9249 val = XSUB_CHAR_TABLE (val)->contents[c];
9250 }
9251 else
9252 {
9253 Lisp_Object table;
9254
9255 XSETCHAR_TABLE (table, dp);
9256 val = char_table_ref (table, c);
9257 }
9258 if (NILP (val))
9259 val = dp->defalt;
9260 return val;
9261 }
9262
9263
9264 \f
9265 /***********************************************************************
9266 Window Redisplay
9267 ***********************************************************************/
9268
9269 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9270
9271 static void
9272 redisplay_windows (window)
9273 Lisp_Object window;
9274 {
9275 while (!NILP (window))
9276 {
9277 struct window *w = XWINDOW (window);
9278
9279 if (!NILP (w->hchild))
9280 redisplay_windows (w->hchild);
9281 else if (!NILP (w->vchild))
9282 redisplay_windows (w->vchild);
9283 else
9284 {
9285 displayed_buffer = XBUFFER (w->buffer);
9286 internal_condition_case_1 (redisplay_window_0, window, Qerror,
9287 redisplay_window_error);
9288 }
9289
9290 window = w->next;
9291 }
9292 }
9293
9294 static Lisp_Object
9295 redisplay_window_error ()
9296 {
9297 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9298 return Qnil;
9299 }
9300
9301 static Lisp_Object
9302 redisplay_window_0 (window)
9303 Lisp_Object window;
9304 {
9305 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9306 redisplay_window (window, 0);
9307 return Qnil;
9308 }
9309
9310 static Lisp_Object
9311 redisplay_window_1 (window)
9312 Lisp_Object window;
9313 {
9314 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9315 redisplay_window (window, 1);
9316 return Qnil;
9317 }
9318 \f
9319 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9320 DELTA is the number of bytes by which positions recorded in ROW
9321 differ from current buffer positions. */
9322
9323 void
9324 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9325 struct window *w;
9326 struct glyph_row *row;
9327 struct glyph_matrix *matrix;
9328 int delta, delta_bytes, dy, dvpos;
9329 {
9330 struct glyph *glyph = row->glyphs[TEXT_AREA];
9331 struct glyph *end = glyph + row->used[TEXT_AREA];
9332 int x = row->x;
9333 int pt_old = PT - delta;
9334
9335 /* Skip over glyphs not having an object at the start of the row.
9336 These are special glyphs like truncation marks on terminal
9337 frames. */
9338 if (row->displays_text_p)
9339 while (glyph < end
9340 && INTEGERP (glyph->object)
9341 && glyph->charpos < 0)
9342 {
9343 x += glyph->pixel_width;
9344 ++glyph;
9345 }
9346
9347 while (glyph < end
9348 && !INTEGERP (glyph->object)
9349 && (!BUFFERP (glyph->object)
9350 || glyph->charpos < pt_old))
9351 {
9352 x += glyph->pixel_width;
9353 ++glyph;
9354 }
9355
9356 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9357 w->cursor.x = x;
9358 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9359 w->cursor.y = row->y + dy;
9360
9361 if (w == XWINDOW (selected_window))
9362 {
9363 if (!row->continued_p
9364 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9365 && row->x == 0)
9366 {
9367 this_line_buffer = XBUFFER (w->buffer);
9368
9369 CHARPOS (this_line_start_pos)
9370 = MATRIX_ROW_START_CHARPOS (row) + delta;
9371 BYTEPOS (this_line_start_pos)
9372 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9373
9374 CHARPOS (this_line_end_pos)
9375 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9376 BYTEPOS (this_line_end_pos)
9377 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9378
9379 this_line_y = w->cursor.y;
9380 this_line_pixel_height = row->height;
9381 this_line_vpos = w->cursor.vpos;
9382 this_line_start_x = row->x;
9383 }
9384 else
9385 CHARPOS (this_line_start_pos) = 0;
9386 }
9387 }
9388
9389
9390 /* Run window scroll functions, if any, for WINDOW with new window
9391 start STARTP. Sets the window start of WINDOW to that position.
9392
9393 We assume that the window's buffer is really current. */
9394
9395 static INLINE struct text_pos
9396 run_window_scroll_functions (window, startp)
9397 Lisp_Object window;
9398 struct text_pos startp;
9399 {
9400 struct window *w = XWINDOW (window);
9401 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9402
9403 if (current_buffer != XBUFFER (w->buffer))
9404 abort ();
9405
9406 if (!NILP (Vwindow_scroll_functions))
9407 {
9408 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9409 make_number (CHARPOS (startp)));
9410 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9411 /* In case the hook functions switch buffers. */
9412 if (current_buffer != XBUFFER (w->buffer))
9413 set_buffer_internal_1 (XBUFFER (w->buffer));
9414 }
9415
9416 return startp;
9417 }
9418
9419
9420 /* Modify the desired matrix of window W and W->vscroll so that the
9421 line containing the cursor is fully visible. If this requires
9422 larger matrices than are allocated, set fonts_changed_p and return
9423 0. */
9424
9425 static int
9426 make_cursor_line_fully_visible (w)
9427 struct window *w;
9428 {
9429 struct glyph_matrix *matrix;
9430 struct glyph_row *row;
9431 int window_height;
9432
9433 /* It's not always possible to find the cursor, e.g, when a window
9434 is full of overlay strings. Don't do anything in that case. */
9435 if (w->cursor.vpos < 0)
9436 return 1;
9437
9438 matrix = w->desired_matrix;
9439 row = MATRIX_ROW (matrix, w->cursor.vpos);
9440
9441 /* If the cursor row is not partially visible, there's nothing
9442 to do. */
9443 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9444 return 1;
9445
9446 /* If the row the cursor is in is taller than the window's height,
9447 it's not clear what to do, so do nothing. */
9448 window_height = window_box_height (w);
9449 if (row->height >= window_height)
9450 return 1;
9451
9452 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9453 {
9454 int dy = row->height - row->visible_height;
9455 w->vscroll = 0;
9456 w->cursor.y += dy;
9457 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9458 }
9459 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9460 {
9461 int dy = - (row->height - row->visible_height);
9462 w->vscroll = dy;
9463 w->cursor.y += dy;
9464 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9465 }
9466
9467 /* When we change the cursor y-position of the selected window,
9468 change this_line_y as well so that the display optimization for
9469 the cursor line of the selected window in redisplay_internal uses
9470 the correct y-position. */
9471 if (w == XWINDOW (selected_window))
9472 this_line_y = w->cursor.y;
9473
9474 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9475 redisplay with larger matrices. */
9476 if (matrix->nrows < required_matrix_height (w))
9477 {
9478 fonts_changed_p = 1;
9479 return 0;
9480 }
9481
9482 return 1;
9483 }
9484
9485
9486 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9487 non-zero means only WINDOW is redisplayed in redisplay_internal.
9488 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9489 in redisplay_window to bring a partially visible line into view in
9490 the case that only the cursor has moved.
9491
9492 Value is
9493
9494 1 if scrolling succeeded
9495
9496 0 if scrolling didn't find point.
9497
9498 -1 if new fonts have been loaded so that we must interrupt
9499 redisplay, adjust glyph matrices, and try again. */
9500
9501 enum
9502 {
9503 SCROLLING_SUCCESS,
9504 SCROLLING_FAILED,
9505 SCROLLING_NEED_LARGER_MATRICES
9506 };
9507
9508 static int
9509 try_scrolling (window, just_this_one_p, scroll_conservatively,
9510 scroll_step, temp_scroll_step)
9511 Lisp_Object window;
9512 int just_this_one_p;
9513 int scroll_conservatively, scroll_step;
9514 int temp_scroll_step;
9515 {
9516 struct window *w = XWINDOW (window);
9517 struct frame *f = XFRAME (w->frame);
9518 struct text_pos scroll_margin_pos;
9519 struct text_pos pos;
9520 struct text_pos startp;
9521 struct it it;
9522 Lisp_Object window_end;
9523 int this_scroll_margin;
9524 int dy = 0;
9525 int scroll_max;
9526 int rc;
9527 int amount_to_scroll = 0;
9528 Lisp_Object aggressive;
9529 int height;
9530
9531 #if GLYPH_DEBUG
9532 debug_method_add (w, "try_scrolling");
9533 #endif
9534
9535 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9536
9537 /* Compute scroll margin height in pixels. We scroll when point is
9538 within this distance from the top or bottom of the window. */
9539 if (scroll_margin > 0)
9540 {
9541 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9542 this_scroll_margin *= CANON_Y_UNIT (f);
9543 }
9544 else
9545 this_scroll_margin = 0;
9546
9547 /* Compute how much we should try to scroll maximally to bring point
9548 into view. */
9549 if (scroll_step || scroll_conservatively || temp_scroll_step)
9550 scroll_max = max (scroll_step,
9551 max (scroll_conservatively, temp_scroll_step));
9552 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9553 || NUMBERP (current_buffer->scroll_up_aggressively))
9554 /* We're trying to scroll because of aggressive scrolling
9555 but no scroll_step is set. Choose an arbitrary one. Maybe
9556 there should be a variable for this. */
9557 scroll_max = 10;
9558 else
9559 scroll_max = 0;
9560 scroll_max *= CANON_Y_UNIT (f);
9561
9562 /* Decide whether we have to scroll down. Start at the window end
9563 and move this_scroll_margin up to find the position of the scroll
9564 margin. */
9565 window_end = Fwindow_end (window, Qt);
9566 CHARPOS (scroll_margin_pos) = XINT (window_end);
9567 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9568 if (this_scroll_margin)
9569 {
9570 start_display (&it, w, scroll_margin_pos);
9571 move_it_vertically (&it, - this_scroll_margin);
9572 scroll_margin_pos = it.current.pos;
9573 }
9574
9575 if (PT >= CHARPOS (scroll_margin_pos))
9576 {
9577 int y0;
9578
9579 /* Point is in the scroll margin at the bottom of the window, or
9580 below. Compute a new window start that makes point visible. */
9581
9582 /* Compute the distance from the scroll margin to PT.
9583 Give up if the distance is greater than scroll_max. */
9584 start_display (&it, w, scroll_margin_pos);
9585 y0 = it.current_y;
9586 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9587 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9588
9589 /* To make point visible, we have to move the window start
9590 down so that the line the cursor is in is visible, which
9591 means we have to add in the height of the cursor line. */
9592 dy = line_bottom_y (&it) - y0;
9593
9594 if (dy > scroll_max)
9595 return SCROLLING_FAILED;
9596
9597 /* Move the window start down. If scrolling conservatively,
9598 move it just enough down to make point visible. If
9599 scroll_step is set, move it down by scroll_step. */
9600 start_display (&it, w, startp);
9601
9602 if (scroll_conservatively)
9603 amount_to_scroll
9604 = max (max (dy, CANON_Y_UNIT (f)),
9605 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9606 else if (scroll_step || temp_scroll_step)
9607 amount_to_scroll = scroll_max;
9608 else
9609 {
9610 aggressive = current_buffer->scroll_up_aggressively;
9611 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9612 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9613 if (NUMBERP (aggressive))
9614 amount_to_scroll = XFLOATINT (aggressive) * height;
9615 }
9616
9617 if (amount_to_scroll <= 0)
9618 return SCROLLING_FAILED;
9619
9620 move_it_vertically (&it, amount_to_scroll);
9621 startp = it.current.pos;
9622 }
9623 else
9624 {
9625 /* See if point is inside the scroll margin at the top of the
9626 window. */
9627 scroll_margin_pos = startp;
9628 if (this_scroll_margin)
9629 {
9630 start_display (&it, w, startp);
9631 move_it_vertically (&it, this_scroll_margin);
9632 scroll_margin_pos = it.current.pos;
9633 }
9634
9635 if (PT < CHARPOS (scroll_margin_pos))
9636 {
9637 /* Point is in the scroll margin at the top of the window or
9638 above what is displayed in the window. */
9639 int y0;
9640
9641 /* Compute the vertical distance from PT to the scroll
9642 margin position. Give up if distance is greater than
9643 scroll_max. */
9644 SET_TEXT_POS (pos, PT, PT_BYTE);
9645 start_display (&it, w, pos);
9646 y0 = it.current_y;
9647 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9648 it.last_visible_y, -1,
9649 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9650 dy = it.current_y - y0;
9651 if (dy > scroll_max)
9652 return SCROLLING_FAILED;
9653
9654 /* Compute new window start. */
9655 start_display (&it, w, startp);
9656
9657 if (scroll_conservatively)
9658 amount_to_scroll =
9659 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9660 else if (scroll_step || temp_scroll_step)
9661 amount_to_scroll = scroll_max;
9662 else
9663 {
9664 aggressive = current_buffer->scroll_down_aggressively;
9665 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9666 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9667 if (NUMBERP (aggressive))
9668 amount_to_scroll = XFLOATINT (aggressive) * height;
9669 }
9670
9671 if (amount_to_scroll <= 0)
9672 return SCROLLING_FAILED;
9673
9674 move_it_vertically (&it, - amount_to_scroll);
9675 startp = it.current.pos;
9676 }
9677 }
9678
9679 /* Run window scroll functions. */
9680 startp = run_window_scroll_functions (window, startp);
9681
9682 /* Display the window. Give up if new fonts are loaded, or if point
9683 doesn't appear. */
9684 if (!try_window (window, startp))
9685 rc = SCROLLING_NEED_LARGER_MATRICES;
9686 else if (w->cursor.vpos < 0)
9687 {
9688 clear_glyph_matrix (w->desired_matrix);
9689 rc = SCROLLING_FAILED;
9690 }
9691 else
9692 {
9693 /* Maybe forget recorded base line for line number display. */
9694 if (!just_this_one_p
9695 || current_buffer->clip_changed
9696 || BEG_UNCHANGED < CHARPOS (startp))
9697 w->base_line_number = Qnil;
9698
9699 /* If cursor ends up on a partially visible line, shift display
9700 lines up or down. If that fails because we need larger
9701 matrices, give up. */
9702 if (!make_cursor_line_fully_visible (w))
9703 rc = SCROLLING_NEED_LARGER_MATRICES;
9704 else
9705 rc = SCROLLING_SUCCESS;
9706 }
9707
9708 return rc;
9709 }
9710
9711
9712 /* Compute a suitable window start for window W if display of W starts
9713 on a continuation line. Value is non-zero if a new window start
9714 was computed.
9715
9716 The new window start will be computed, based on W's width, starting
9717 from the start of the continued line. It is the start of the
9718 screen line with the minimum distance from the old start W->start. */
9719
9720 static int
9721 compute_window_start_on_continuation_line (w)
9722 struct window *w;
9723 {
9724 struct text_pos pos, start_pos;
9725 int window_start_changed_p = 0;
9726
9727 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9728
9729 /* If window start is on a continuation line... Window start may be
9730 < BEGV in case there's invisible text at the start of the
9731 buffer (M-x rmail, for example). */
9732 if (CHARPOS (start_pos) > BEGV
9733 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9734 {
9735 struct it it;
9736 struct glyph_row *row;
9737
9738 /* Handle the case that the window start is out of range. */
9739 if (CHARPOS (start_pos) < BEGV)
9740 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9741 else if (CHARPOS (start_pos) > ZV)
9742 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9743
9744 /* Find the start of the continued line. This should be fast
9745 because scan_buffer is fast (newline cache). */
9746 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9747 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9748 row, DEFAULT_FACE_ID);
9749 reseat_at_previous_visible_line_start (&it);
9750
9751 /* If the line start is "too far" away from the window start,
9752 say it takes too much time to compute a new window start. */
9753 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9754 < XFASTINT (w->height) * XFASTINT (w->width))
9755 {
9756 int min_distance, distance;
9757
9758 /* Move forward by display lines to find the new window
9759 start. If window width was enlarged, the new start can
9760 be expected to be > the old start. If window width was
9761 decreased, the new window start will be < the old start.
9762 So, we're looking for the display line start with the
9763 minimum distance from the old window start. */
9764 pos = it.current.pos;
9765 min_distance = INFINITY;
9766 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9767 distance < min_distance)
9768 {
9769 min_distance = distance;
9770 pos = it.current.pos;
9771 move_it_by_lines (&it, 1, 0);
9772 }
9773
9774 /* Set the window start there. */
9775 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9776 window_start_changed_p = 1;
9777 }
9778 }
9779
9780 return window_start_changed_p;
9781 }
9782
9783
9784 /* Try cursor movement in case text has not changes in window WINDOW,
9785 with window start STARTP. Value is
9786
9787 CURSOR_MOVEMENT_SUCCESS if successful
9788
9789 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9790
9791 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9792 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9793 we want to scroll as if scroll-step were set to 1. See the code.
9794
9795 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9796 which case we have to abort this redisplay, and adjust matrices
9797 first. */
9798
9799 enum
9800 {
9801 CURSOR_MOVEMENT_SUCCESS,
9802 CURSOR_MOVEMENT_CANNOT_BE_USED,
9803 CURSOR_MOVEMENT_MUST_SCROLL,
9804 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9805 };
9806
9807 static int
9808 try_cursor_movement (window, startp, scroll_step)
9809 Lisp_Object window;
9810 struct text_pos startp;
9811 int *scroll_step;
9812 {
9813 struct window *w = XWINDOW (window);
9814 struct frame *f = XFRAME (w->frame);
9815 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
9816
9817 #if GLYPH_DEBUG
9818 if (inhibit_try_cursor_movement)
9819 return rc;
9820 #endif
9821
9822 /* Handle case where text has not changed, only point, and it has
9823 not moved off the frame. */
9824 if (/* Point may be in this window. */
9825 PT >= CHARPOS (startp)
9826 /* Selective display hasn't changed. */
9827 && !current_buffer->clip_changed
9828 /* Function force-mode-line-update is used to force a thorough
9829 redisplay. It sets either windows_or_buffers_changed or
9830 update_mode_lines. So don't take a shortcut here for these
9831 cases. */
9832 && !update_mode_lines
9833 && !windows_or_buffers_changed
9834 /* Can't use this case if highlighting a region. When a
9835 region exists, cursor movement has to do more than just
9836 set the cursor. */
9837 && !(!NILP (Vtransient_mark_mode)
9838 && !NILP (current_buffer->mark_active))
9839 && NILP (w->region_showing)
9840 && NILP (Vshow_trailing_whitespace)
9841 /* Right after splitting windows, last_point may be nil. */
9842 && INTEGERP (w->last_point)
9843 /* This code is not used for mini-buffer for the sake of the case
9844 of redisplaying to replace an echo area message; since in
9845 that case the mini-buffer contents per se are usually
9846 unchanged. This code is of no real use in the mini-buffer
9847 since the handling of this_line_start_pos, etc., in redisplay
9848 handles the same cases. */
9849 && !EQ (window, minibuf_window)
9850 /* When splitting windows or for new windows, it happens that
9851 redisplay is called with a nil window_end_vpos or one being
9852 larger than the window. This should really be fixed in
9853 window.c. I don't have this on my list, now, so we do
9854 approximately the same as the old redisplay code. --gerd. */
9855 && INTEGERP (w->window_end_vpos)
9856 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9857 && (FRAME_WINDOW_P (f)
9858 || !MARKERP (Voverlay_arrow_position)
9859 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9860 {
9861 int this_scroll_margin;
9862 struct glyph_row *row = NULL;
9863
9864 #if GLYPH_DEBUG
9865 debug_method_add (w, "cursor movement");
9866 #endif
9867
9868 /* Scroll if point within this distance from the top or bottom
9869 of the window. This is a pixel value. */
9870 this_scroll_margin = max (0, scroll_margin);
9871 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9872 this_scroll_margin *= CANON_Y_UNIT (f);
9873
9874 /* Start with the row the cursor was displayed during the last
9875 not paused redisplay. Give up if that row is not valid. */
9876 if (w->last_cursor.vpos < 0
9877 || w->last_cursor.vpos >= w->current_matrix->nrows)
9878 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9879 else
9880 {
9881 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9882 if (row->mode_line_p)
9883 ++row;
9884 if (!row->enabled_p)
9885 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9886 }
9887
9888 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
9889 {
9890 int scroll_p = 0;
9891 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9892
9893 if (PT > XFASTINT (w->last_point))
9894 {
9895 /* Point has moved forward. */
9896 while (MATRIX_ROW_END_CHARPOS (row) < PT
9897 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9898 {
9899 xassert (row->enabled_p);
9900 ++row;
9901 }
9902
9903 /* The end position of a row equals the start position
9904 of the next row. If PT is there, we would rather
9905 display it in the next line. */
9906 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9907 && MATRIX_ROW_END_CHARPOS (row) == PT
9908 && !cursor_row_p (w, row))
9909 ++row;
9910
9911 /* If within the scroll margin, scroll. Note that
9912 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9913 the next line would be drawn, and that
9914 this_scroll_margin can be zero. */
9915 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9916 || PT > MATRIX_ROW_END_CHARPOS (row)
9917 /* Line is completely visible last line in window
9918 and PT is to be set in the next line. */
9919 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9920 && PT == MATRIX_ROW_END_CHARPOS (row)
9921 && !row->ends_at_zv_p
9922 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9923 scroll_p = 1;
9924 }
9925 else if (PT < XFASTINT (w->last_point))
9926 {
9927 /* Cursor has to be moved backward. Note that PT >=
9928 CHARPOS (startp) because of the outer
9929 if-statement. */
9930 while (!row->mode_line_p
9931 && (MATRIX_ROW_START_CHARPOS (row) > PT
9932 || (MATRIX_ROW_START_CHARPOS (row) == PT
9933 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9934 && (row->y > this_scroll_margin
9935 || CHARPOS (startp) == BEGV))
9936 {
9937 xassert (row->enabled_p);
9938 --row;
9939 }
9940
9941 /* Consider the following case: Window starts at BEGV,
9942 there is invisible, intangible text at BEGV, so that
9943 display starts at some point START > BEGV. It can
9944 happen that we are called with PT somewhere between
9945 BEGV and START. Try to handle that case. */
9946 if (row < w->current_matrix->rows
9947 || row->mode_line_p)
9948 {
9949 row = w->current_matrix->rows;
9950 if (row->mode_line_p)
9951 ++row;
9952 }
9953
9954 /* Due to newlines in overlay strings, we may have to
9955 skip forward over overlay strings. */
9956 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9957 && MATRIX_ROW_END_CHARPOS (row) == PT
9958 && !cursor_row_p (w, row))
9959 ++row;
9960
9961 /* If within the scroll margin, scroll. */
9962 if (row->y < this_scroll_margin
9963 && CHARPOS (startp) != BEGV)
9964 scroll_p = 1;
9965 }
9966
9967 if (PT < MATRIX_ROW_START_CHARPOS (row)
9968 || PT > MATRIX_ROW_END_CHARPOS (row))
9969 {
9970 /* if PT is not in the glyph row, give up. */
9971 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9972 }
9973 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9974 {
9975 if (PT == MATRIX_ROW_END_CHARPOS (row)
9976 && !row->ends_at_zv_p
9977 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
9978 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9979 else if (row->height > window_box_height (w))
9980 {
9981 /* If we end up in a partially visible line, let's
9982 make it fully visible, except when it's taller
9983 than the window, in which case we can't do much
9984 about it. */
9985 *scroll_step = 1;
9986 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9987 }
9988 else
9989 {
9990 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9991 try_window (window, startp);
9992 if (!make_cursor_line_fully_visible (w))
9993 rc = CURSOR_MOVEMENT_NEED_LARGER_MATRICES;
9994 else
9995 rc = CURSOR_MOVEMENT_SUCCESS;
9996 }
9997 }
9998 else if (scroll_p)
9999 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10000 else
10001 {
10002 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10003 rc = CURSOR_MOVEMENT_SUCCESS;
10004 }
10005 }
10006 }
10007
10008 return rc;
10009 }
10010
10011
10012 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
10013 selected_window is redisplayed. */
10014
10015 static void
10016 redisplay_window (window, just_this_one_p)
10017 Lisp_Object window;
10018 int just_this_one_p;
10019 {
10020 struct window *w = XWINDOW (window);
10021 struct frame *f = XFRAME (w->frame);
10022 struct buffer *buffer = XBUFFER (w->buffer);
10023 struct buffer *old = current_buffer;
10024 struct text_pos lpoint, opoint, startp;
10025 int update_mode_line;
10026 int tem;
10027 struct it it;
10028 /* Record it now because it's overwritten. */
10029 int current_matrix_up_to_date_p = 0;
10030 int temp_scroll_step = 0;
10031 int count = BINDING_STACK_SIZE ();
10032 int rc;
10033
10034 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10035 opoint = lpoint;
10036
10037 /* W must be a leaf window here. */
10038 xassert (!NILP (w->buffer));
10039 #if GLYPH_DEBUG
10040 *w->desired_matrix->method = 0;
10041 #endif
10042
10043 specbind (Qinhibit_point_motion_hooks, Qt);
10044
10045 reconsider_clip_changes (w, buffer);
10046
10047 /* Has the mode line to be updated? */
10048 update_mode_line = (!NILP (w->update_mode_line)
10049 || update_mode_lines
10050 || buffer->clip_changed);
10051
10052 if (MINI_WINDOW_P (w))
10053 {
10054 if (w == XWINDOW (echo_area_window)
10055 && !NILP (echo_area_buffer[0]))
10056 {
10057 if (update_mode_line)
10058 /* We may have to update a tty frame's menu bar or a
10059 tool-bar. Example `M-x C-h C-h C-g'. */
10060 goto finish_menu_bars;
10061 else
10062 /* We've already displayed the echo area glyphs in this window. */
10063 goto finish_scroll_bars;
10064 }
10065 else if (w != XWINDOW (minibuf_window))
10066 {
10067 /* W is a mini-buffer window, but it's not the currently
10068 active one, so clear it. */
10069 int yb = window_text_bottom_y (w);
10070 struct glyph_row *row;
10071 int y;
10072
10073 for (y = 0, row = w->desired_matrix->rows;
10074 y < yb;
10075 y += row->height, ++row)
10076 blank_row (w, row, y);
10077 goto finish_scroll_bars;
10078 }
10079
10080 clear_glyph_matrix (w->desired_matrix);
10081 }
10082
10083 /* Otherwise set up data on this window; select its buffer and point
10084 value. */
10085 /* Really select the buffer, for the sake of buffer-local
10086 variables. */
10087 set_buffer_internal_1 (XBUFFER (w->buffer));
10088 SET_TEXT_POS (opoint, PT, PT_BYTE);
10089
10090 current_matrix_up_to_date_p
10091 = (!NILP (w->window_end_valid)
10092 && !current_buffer->clip_changed
10093 && XFASTINT (w->last_modified) >= MODIFF
10094 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10095
10096 /* When windows_or_buffers_changed is non-zero, we can't rely on
10097 the window end being valid, so set it to nil there. */
10098 if (windows_or_buffers_changed)
10099 {
10100 /* If window starts on a continuation line, maybe adjust the
10101 window start in case the window's width changed. */
10102 if (XMARKER (w->start)->buffer == current_buffer)
10103 compute_window_start_on_continuation_line (w);
10104
10105 w->window_end_valid = Qnil;
10106 }
10107
10108 /* Some sanity checks. */
10109 CHECK_WINDOW_END (w);
10110 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
10111 abort ();
10112 if (BYTEPOS (opoint) < CHARPOS (opoint))
10113 abort ();
10114
10115 /* If %c is in mode line, update it if needed. */
10116 if (!NILP (w->column_number_displayed)
10117 /* This alternative quickly identifies a common case
10118 where no change is needed. */
10119 && !(PT == XFASTINT (w->last_point)
10120 && XFASTINT (w->last_modified) >= MODIFF
10121 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10122 && XFASTINT (w->column_number_displayed) != current_column ())
10123 update_mode_line = 1;
10124
10125 /* Count number of windows showing the selected buffer. An indirect
10126 buffer counts as its base buffer. */
10127 if (!just_this_one_p)
10128 {
10129 struct buffer *current_base, *window_base;
10130 current_base = current_buffer;
10131 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10132 if (current_base->base_buffer)
10133 current_base = current_base->base_buffer;
10134 if (window_base->base_buffer)
10135 window_base = window_base->base_buffer;
10136 if (current_base == window_base)
10137 buffer_shared++;
10138 }
10139
10140 /* Point refers normally to the selected window. For any other
10141 window, set up appropriate value. */
10142 if (!EQ (window, selected_window))
10143 {
10144 int new_pt = XMARKER (w->pointm)->charpos;
10145 int new_pt_byte = marker_byte_position (w->pointm);
10146 if (new_pt < BEGV)
10147 {
10148 new_pt = BEGV;
10149 new_pt_byte = BEGV_BYTE;
10150 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10151 }
10152 else if (new_pt > (ZV - 1))
10153 {
10154 new_pt = ZV;
10155 new_pt_byte = ZV_BYTE;
10156 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10157 }
10158
10159 /* We don't use SET_PT so that the point-motion hooks don't run. */
10160 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10161 }
10162
10163 /* If any of the character widths specified in the display table
10164 have changed, invalidate the width run cache. It's true that
10165 this may be a bit late to catch such changes, but the rest of
10166 redisplay goes (non-fatally) haywire when the display table is
10167 changed, so why should we worry about doing any better? */
10168 if (current_buffer->width_run_cache)
10169 {
10170 struct Lisp_Char_Table *disptab = buffer_display_table ();
10171
10172 if (! disptab_matches_widthtab (disptab,
10173 XVECTOR (current_buffer->width_table)))
10174 {
10175 invalidate_region_cache (current_buffer,
10176 current_buffer->width_run_cache,
10177 BEG, Z);
10178 recompute_width_table (current_buffer, disptab);
10179 }
10180 }
10181
10182 /* If window-start is screwed up, choose a new one. */
10183 if (XMARKER (w->start)->buffer != current_buffer)
10184 goto recenter;
10185
10186 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10187
10188 /* If someone specified a new starting point but did not insist,
10189 check whether it can be used. */
10190 if (!NILP (w->optional_new_start)
10191 && CHARPOS (startp) >= BEGV
10192 && CHARPOS (startp) <= ZV)
10193 {
10194 w->optional_new_start = Qnil;
10195 start_display (&it, w, startp);
10196 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10197 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10198 if (IT_CHARPOS (it) == PT)
10199 w->force_start = Qt;
10200 }
10201
10202 /* Handle case where place to start displaying has been specified,
10203 unless the specified location is outside the accessible range. */
10204 if (!NILP (w->force_start)
10205 || w->frozen_window_start_p)
10206 {
10207 w->force_start = Qnil;
10208 w->vscroll = 0;
10209 w->window_end_valid = Qnil;
10210
10211 /* Forget any recorded base line for line number display. */
10212 if (!current_matrix_up_to_date_p
10213 || current_buffer->clip_changed)
10214 w->base_line_number = Qnil;
10215
10216 /* Redisplay the mode line. Select the buffer properly for that.
10217 Also, run the hook window-scroll-functions
10218 because we have scrolled. */
10219 /* Note, we do this after clearing force_start because
10220 if there's an error, it is better to forget about force_start
10221 than to get into an infinite loop calling the hook functions
10222 and having them get more errors. */
10223 if (!update_mode_line
10224 || ! NILP (Vwindow_scroll_functions))
10225 {
10226 update_mode_line = 1;
10227 w->update_mode_line = Qt;
10228 startp = run_window_scroll_functions (window, startp);
10229 }
10230
10231 w->last_modified = make_number (0);
10232 w->last_overlay_modified = make_number (0);
10233 if (CHARPOS (startp) < BEGV)
10234 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10235 else if (CHARPOS (startp) > ZV)
10236 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10237
10238 /* Redisplay, then check if cursor has been set during the
10239 redisplay. Give up if new fonts were loaded. */
10240 if (!try_window (window, startp))
10241 {
10242 w->force_start = Qt;
10243 clear_glyph_matrix (w->desired_matrix);
10244 goto finish_scroll_bars;
10245 }
10246
10247 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10248 {
10249 /* If point does not appear, try to move point so it does
10250 appear. The desired matrix has been built above, so we
10251 can use it here. */
10252 int window_height;
10253 struct glyph_row *row;
10254
10255 window_height = window_box_height (w) / 2;
10256 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10257 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
10258 ++row;
10259
10260 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10261 MATRIX_ROW_START_BYTEPOS (row));
10262
10263 if (w != XWINDOW (selected_window))
10264 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10265 else if (current_buffer == old)
10266 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10267
10268 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10269
10270 /* If we are highlighting the region, then we just changed
10271 the region, so redisplay to show it. */
10272 if (!NILP (Vtransient_mark_mode)
10273 && !NILP (current_buffer->mark_active))
10274 {
10275 clear_glyph_matrix (w->desired_matrix);
10276 if (!try_window (window, startp))
10277 goto need_larger_matrices;
10278 }
10279 }
10280
10281 if (!make_cursor_line_fully_visible (w))
10282 goto need_larger_matrices;
10283 #if GLYPH_DEBUG
10284 debug_method_add (w, "forced window start");
10285 #endif
10286 goto done;
10287 }
10288
10289 /* Handle case where text has not changed, only point, and it has
10290 not moved off the frame. */
10291 if (current_matrix_up_to_date_p
10292 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10293 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10294 {
10295 switch (rc)
10296 {
10297 case CURSOR_MOVEMENT_SUCCESS:
10298 goto done;
10299
10300 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10301 goto need_larger_matrices;
10302
10303 case CURSOR_MOVEMENT_MUST_SCROLL:
10304 goto try_to_scroll;
10305
10306 default:
10307 abort ();
10308 }
10309 }
10310 /* If current starting point was originally the beginning of a line
10311 but no longer is, find a new starting point. */
10312 else if (!NILP (w->start_at_line_beg)
10313 && !(CHARPOS (startp) <= BEGV
10314 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10315 {
10316 #if GLYPH_DEBUG
10317 debug_method_add (w, "recenter 1");
10318 #endif
10319 goto recenter;
10320 }
10321
10322 /* Try scrolling with try_window_id. Value is > 0 if update has
10323 been done, it is -1 if we know that the same window start will
10324 not work. It is 0 if unsuccessful for some other reason. */
10325 else if ((tem = try_window_id (w)) != 0)
10326 {
10327 #if GLYPH_DEBUG
10328 debug_method_add (w, "try_window_id %d", tem);
10329 #endif
10330
10331 if (fonts_changed_p)
10332 goto need_larger_matrices;
10333 if (tem > 0)
10334 goto done;
10335
10336 /* Otherwise try_window_id has returned -1 which means that we
10337 don't want the alternative below this comment to execute. */
10338 }
10339 else if (CHARPOS (startp) >= BEGV
10340 && CHARPOS (startp) <= ZV
10341 && PT >= CHARPOS (startp)
10342 && (CHARPOS (startp) < ZV
10343 /* Avoid starting at end of buffer. */
10344 || CHARPOS (startp) == BEGV
10345 || (XFASTINT (w->last_modified) >= MODIFF
10346 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10347 {
10348 #if GLYPH_DEBUG
10349 debug_method_add (w, "same window start");
10350 #endif
10351
10352 /* Try to redisplay starting at same place as before.
10353 If point has not moved off frame, accept the results. */
10354 if (!current_matrix_up_to_date_p
10355 /* Don't use try_window_reusing_current_matrix in this case
10356 because a window scroll function can have changed the
10357 buffer. */
10358 || !NILP (Vwindow_scroll_functions)
10359 || MINI_WINDOW_P (w)
10360 || !try_window_reusing_current_matrix (w))
10361 {
10362 IF_DEBUG (debug_method_add (w, "1"));
10363 try_window (window, startp);
10364 }
10365
10366 if (fonts_changed_p)
10367 goto need_larger_matrices;
10368
10369 if (w->cursor.vpos >= 0)
10370 {
10371 if (!just_this_one_p
10372 || current_buffer->clip_changed
10373 || BEG_UNCHANGED < CHARPOS (startp))
10374 /* Forget any recorded base line for line number display. */
10375 w->base_line_number = Qnil;
10376
10377 if (!make_cursor_line_fully_visible (w))
10378 goto need_larger_matrices;
10379 goto done;
10380 }
10381 else
10382 clear_glyph_matrix (w->desired_matrix);
10383 }
10384
10385 try_to_scroll:
10386
10387 w->last_modified = make_number (0);
10388 w->last_overlay_modified = make_number (0);
10389
10390 /* Redisplay the mode line. Select the buffer properly for that. */
10391 if (!update_mode_line)
10392 {
10393 update_mode_line = 1;
10394 w->update_mode_line = Qt;
10395 }
10396
10397 /* Try to scroll by specified few lines. */
10398 if ((scroll_conservatively
10399 || scroll_step
10400 || temp_scroll_step
10401 || NUMBERP (current_buffer->scroll_up_aggressively)
10402 || NUMBERP (current_buffer->scroll_down_aggressively))
10403 && !current_buffer->clip_changed
10404 && CHARPOS (startp) >= BEGV
10405 && CHARPOS (startp) <= ZV)
10406 {
10407 /* The function returns -1 if new fonts were loaded, 1 if
10408 successful, 0 if not successful. */
10409 int rc = try_scrolling (window, just_this_one_p,
10410 scroll_conservatively,
10411 scroll_step,
10412 temp_scroll_step);
10413 switch (rc)
10414 {
10415 case SCROLLING_SUCCESS:
10416 goto done;
10417
10418 case SCROLLING_NEED_LARGER_MATRICES:
10419 goto need_larger_matrices;
10420
10421 case SCROLLING_FAILED:
10422 break;
10423
10424 default:
10425 abort ();
10426 }
10427 }
10428
10429 /* Finally, just choose place to start which centers point */
10430
10431 recenter:
10432
10433 #if GLYPH_DEBUG
10434 debug_method_add (w, "recenter");
10435 #endif
10436
10437 /* w->vscroll = 0; */
10438
10439 /* Forget any previously recorded base line for line number display. */
10440 if (!current_matrix_up_to_date_p
10441 || current_buffer->clip_changed)
10442 w->base_line_number = Qnil;
10443
10444 /* Move backward half the height of the window. */
10445 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10446 it.current_y = it.last_visible_y;
10447 move_it_vertically_backward (&it, window_box_height (w) / 2);
10448 xassert (IT_CHARPOS (it) >= BEGV);
10449
10450 /* The function move_it_vertically_backward may move over more
10451 than the specified y-distance. If it->w is small, e.g. a
10452 mini-buffer window, we may end up in front of the window's
10453 display area. Start displaying at the start of the line
10454 containing PT in this case. */
10455 if (it.current_y <= 0)
10456 {
10457 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10458 move_it_vertically (&it, 0);
10459 xassert (IT_CHARPOS (it) <= PT);
10460 it.current_y = 0;
10461 }
10462
10463 it.current_x = it.hpos = 0;
10464
10465 /* Set startp here explicitly in case that helps avoid an infinite loop
10466 in case the window-scroll-functions functions get errors. */
10467 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10468
10469 /* Run scroll hooks. */
10470 startp = run_window_scroll_functions (window, it.current.pos);
10471
10472 /* Redisplay the window. */
10473 if (!current_matrix_up_to_date_p
10474 || windows_or_buffers_changed
10475 /* Don't use try_window_reusing_current_matrix in this case
10476 because it can have changed the buffer. */
10477 || !NILP (Vwindow_scroll_functions)
10478 || !just_this_one_p
10479 || MINI_WINDOW_P (w)
10480 || !try_window_reusing_current_matrix (w))
10481 try_window (window, startp);
10482
10483 /* If new fonts have been loaded (due to fontsets), give up. We
10484 have to start a new redisplay since we need to re-adjust glyph
10485 matrices. */
10486 if (fonts_changed_p)
10487 goto need_larger_matrices;
10488
10489 /* If cursor did not appear assume that the middle of the window is
10490 in the first line of the window. Do it again with the next line.
10491 (Imagine a window of height 100, displaying two lines of height
10492 60. Moving back 50 from it->last_visible_y will end in the first
10493 line.) */
10494 if (w->cursor.vpos < 0)
10495 {
10496 if (!NILP (w->window_end_valid)
10497 && PT >= Z - XFASTINT (w->window_end_pos))
10498 {
10499 clear_glyph_matrix (w->desired_matrix);
10500 move_it_by_lines (&it, 1, 0);
10501 try_window (window, it.current.pos);
10502 }
10503 else if (PT < IT_CHARPOS (it))
10504 {
10505 clear_glyph_matrix (w->desired_matrix);
10506 move_it_by_lines (&it, -1, 0);
10507 try_window (window, it.current.pos);
10508 }
10509 else
10510 {
10511 /* Not much we can do about it. */
10512 }
10513 }
10514
10515 /* Consider the following case: Window starts at BEGV, there is
10516 invisible, intangible text at BEGV, so that display starts at
10517 some point START > BEGV. It can happen that we are called with
10518 PT somewhere between BEGV and START. Try to handle that case. */
10519 if (w->cursor.vpos < 0)
10520 {
10521 struct glyph_row *row = w->current_matrix->rows;
10522 if (row->mode_line_p)
10523 ++row;
10524 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10525 }
10526
10527 if (!make_cursor_line_fully_visible (w))
10528 goto need_larger_matrices;
10529
10530 done:
10531
10532 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10533 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10534 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10535 ? Qt : Qnil);
10536
10537 /* Display the mode line, if we must. */
10538 if ((update_mode_line
10539 /* If window not full width, must redo its mode line
10540 if (a) the window to its side is being redone and
10541 (b) we do a frame-based redisplay. This is a consequence
10542 of how inverted lines are drawn in frame-based redisplay. */
10543 || (!just_this_one_p
10544 && !FRAME_WINDOW_P (f)
10545 && !WINDOW_FULL_WIDTH_P (w))
10546 /* Line number to display. */
10547 || INTEGERP (w->base_line_pos)
10548 /* Column number is displayed and different from the one displayed. */
10549 || (!NILP (w->column_number_displayed)
10550 && XFASTINT (w->column_number_displayed) != current_column ()))
10551 /* This means that the window has a mode line. */
10552 && (WINDOW_WANTS_MODELINE_P (w)
10553 || WINDOW_WANTS_HEADER_LINE_P (w)))
10554 {
10555 display_mode_lines (w);
10556
10557 /* If mode line height has changed, arrange for a thorough
10558 immediate redisplay using the correct mode line height. */
10559 if (WINDOW_WANTS_MODELINE_P (w)
10560 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10561 {
10562 fonts_changed_p = 1;
10563 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10564 = DESIRED_MODE_LINE_HEIGHT (w);
10565 }
10566
10567 /* If top line height has changed, arrange for a thorough
10568 immediate redisplay using the correct mode line height. */
10569 if (WINDOW_WANTS_HEADER_LINE_P (w)
10570 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10571 {
10572 fonts_changed_p = 1;
10573 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10574 = DESIRED_HEADER_LINE_HEIGHT (w);
10575 }
10576
10577 if (fonts_changed_p)
10578 goto need_larger_matrices;
10579 }
10580
10581 if (!line_number_displayed
10582 && !BUFFERP (w->base_line_pos))
10583 {
10584 w->base_line_pos = Qnil;
10585 w->base_line_number = Qnil;
10586 }
10587
10588 finish_menu_bars:
10589
10590 /* When we reach a frame's selected window, redo the frame's menu bar. */
10591 if (update_mode_line
10592 && EQ (FRAME_SELECTED_WINDOW (f), window))
10593 {
10594 int redisplay_menu_p = 0;
10595
10596 if (FRAME_WINDOW_P (f))
10597 {
10598 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10599 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10600 #else
10601 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10602 #endif
10603 }
10604 else
10605 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10606
10607 if (redisplay_menu_p)
10608 display_menu_bar (w);
10609
10610 #ifdef HAVE_WINDOW_SYSTEM
10611 if (WINDOWP (f->tool_bar_window)
10612 && (FRAME_TOOL_BAR_LINES (f) > 0
10613 || auto_resize_tool_bars_p))
10614 redisplay_tool_bar (f);
10615 #endif
10616 }
10617
10618 need_larger_matrices:
10619 ;
10620 finish_scroll_bars:
10621
10622 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10623 {
10624 int start, end, whole;
10625
10626 /* Calculate the start and end positions for the current window.
10627 At some point, it would be nice to choose between scrollbars
10628 which reflect the whole buffer size, with special markers
10629 indicating narrowing, and scrollbars which reflect only the
10630 visible region.
10631
10632 Note that mini-buffers sometimes aren't displaying any text. */
10633 if (!MINI_WINDOW_P (w)
10634 || (w == XWINDOW (minibuf_window)
10635 && NILP (echo_area_buffer[0])))
10636 {
10637 whole = ZV - BEGV;
10638 start = marker_position (w->start) - BEGV;
10639 /* I don't think this is guaranteed to be right. For the
10640 moment, we'll pretend it is. */
10641 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10642
10643 if (end < start)
10644 end = start;
10645 if (whole < (end - start))
10646 whole = end - start;
10647 }
10648 else
10649 start = end = whole = 0;
10650
10651 /* Indicate what this scroll bar ought to be displaying now. */
10652 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10653
10654 /* Note that we actually used the scroll bar attached to this
10655 window, so it shouldn't be deleted at the end of redisplay. */
10656 redeem_scroll_bar_hook (w);
10657 }
10658
10659 /* Restore current_buffer and value of point in it. */
10660 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10661 set_buffer_internal_1 (old);
10662 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10663
10664 unbind_to (count, Qnil);
10665 }
10666
10667
10668 /* Build the complete desired matrix of WINDOW with a window start
10669 buffer position POS. Value is non-zero if successful. It is zero
10670 if fonts were loaded during redisplay which makes re-adjusting
10671 glyph matrices necessary. */
10672
10673 int
10674 try_window (window, pos)
10675 Lisp_Object window;
10676 struct text_pos pos;
10677 {
10678 struct window *w = XWINDOW (window);
10679 struct it it;
10680 struct glyph_row *last_text_row = NULL;
10681
10682 /* Make POS the new window start. */
10683 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10684
10685 /* Mark cursor position as unknown. No overlay arrow seen. */
10686 w->cursor.vpos = -1;
10687 overlay_arrow_seen = 0;
10688
10689 /* Initialize iterator and info to start at POS. */
10690 start_display (&it, w, pos);
10691
10692 /* Display all lines of W. */
10693 while (it.current_y < it.last_visible_y)
10694 {
10695 if (display_line (&it))
10696 last_text_row = it.glyph_row - 1;
10697 if (fonts_changed_p)
10698 return 0;
10699 }
10700
10701 /* If bottom moved off end of frame, change mode line percentage. */
10702 if (XFASTINT (w->window_end_pos) <= 0
10703 && Z != IT_CHARPOS (it))
10704 w->update_mode_line = Qt;
10705
10706 /* Set window_end_pos to the offset of the last character displayed
10707 on the window from the end of current_buffer. Set
10708 window_end_vpos to its row number. */
10709 if (last_text_row)
10710 {
10711 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10712 w->window_end_bytepos
10713 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10714 w->window_end_pos
10715 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10716 w->window_end_vpos
10717 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10718 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10719 ->displays_text_p);
10720 }
10721 else
10722 {
10723 w->window_end_bytepos = 0;
10724 w->window_end_pos = w->window_end_vpos = make_number (0);
10725 }
10726
10727 /* But that is not valid info until redisplay finishes. */
10728 w->window_end_valid = Qnil;
10729 return 1;
10730 }
10731
10732
10733 \f
10734 /************************************************************************
10735 Window redisplay reusing current matrix when buffer has not changed
10736 ************************************************************************/
10737
10738 /* Try redisplay of window W showing an unchanged buffer with a
10739 different window start than the last time it was displayed by
10740 reusing its current matrix. Value is non-zero if successful.
10741 W->start is the new window start. */
10742
10743 static int
10744 try_window_reusing_current_matrix (w)
10745 struct window *w;
10746 {
10747 struct frame *f = XFRAME (w->frame);
10748 struct glyph_row *row, *bottom_row;
10749 struct it it;
10750 struct run run;
10751 struct text_pos start, new_start;
10752 int nrows_scrolled, i;
10753 struct glyph_row *last_text_row;
10754 struct glyph_row *last_reused_text_row;
10755 struct glyph_row *start_row;
10756 int start_vpos, min_y, max_y;
10757
10758 #if GLYPH_DEBUG
10759 if (inhibit_try_window_reusing)
10760 return 0;
10761 #endif
10762
10763 if (/* This function doesn't handle terminal frames. */
10764 !FRAME_WINDOW_P (f)
10765 /* Don't try to reuse the display if windows have been split
10766 or such. */
10767 || windows_or_buffers_changed)
10768 return 0;
10769
10770 /* Can't do this if region may have changed. */
10771 if ((!NILP (Vtransient_mark_mode)
10772 && !NILP (current_buffer->mark_active))
10773 || !NILP (w->region_showing)
10774 || !NILP (Vshow_trailing_whitespace))
10775 return 0;
10776
10777 /* If top-line visibility has changed, give up. */
10778 if (WINDOW_WANTS_HEADER_LINE_P (w)
10779 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10780 return 0;
10781
10782 /* Give up if old or new display is scrolled vertically. We could
10783 make this function handle this, but right now it doesn't. */
10784 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10785 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10786 return 0;
10787
10788 /* The variable new_start now holds the new window start. The old
10789 start `start' can be determined from the current matrix. */
10790 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10791 start = start_row->start.pos;
10792 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10793
10794 /* Clear the desired matrix for the display below. */
10795 clear_glyph_matrix (w->desired_matrix);
10796
10797 if (CHARPOS (new_start) <= CHARPOS (start))
10798 {
10799 int first_row_y;
10800
10801 /* Don't use this method if the display starts with an ellipsis
10802 displayed for invisible text. It's not easy to handle that case
10803 below, and it's certainly not worth the effort since this is
10804 not a frequent case. */
10805 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10806 return 0;
10807
10808 IF_DEBUG (debug_method_add (w, "twu1"));
10809
10810 /* Display up to a row that can be reused. The variable
10811 last_text_row is set to the last row displayed that displays
10812 text. Note that it.vpos == 0 if or if not there is a
10813 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10814 start_display (&it, w, new_start);
10815 first_row_y = it.current_y;
10816 w->cursor.vpos = -1;
10817 last_text_row = last_reused_text_row = NULL;
10818
10819 while (it.current_y < it.last_visible_y
10820 && IT_CHARPOS (it) < CHARPOS (start)
10821 && !fonts_changed_p)
10822 if (display_line (&it))
10823 last_text_row = it.glyph_row - 1;
10824
10825 /* A value of current_y < last_visible_y means that we stopped
10826 at the previous window start, which in turn means that we
10827 have at least one reusable row. */
10828 if (it.current_y < it.last_visible_y)
10829 {
10830 /* IT.vpos always starts from 0; it counts text lines. */
10831 nrows_scrolled = it.vpos;
10832
10833 /* Find PT if not already found in the lines displayed. */
10834 if (w->cursor.vpos < 0)
10835 {
10836 int dy = it.current_y - first_row_y;
10837
10838 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10839 row = row_containing_pos (w, PT, row, NULL, dy);
10840 if (row)
10841 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10842 dy, nrows_scrolled);
10843 else
10844 {
10845 clear_glyph_matrix (w->desired_matrix);
10846 return 0;
10847 }
10848 }
10849
10850 /* Scroll the display. Do it before the current matrix is
10851 changed. The problem here is that update has not yet
10852 run, i.e. part of the current matrix is not up to date.
10853 scroll_run_hook will clear the cursor, and use the
10854 current matrix to get the height of the row the cursor is
10855 in. */
10856 run.current_y = first_row_y;
10857 run.desired_y = it.current_y;
10858 run.height = it.last_visible_y - it.current_y;
10859
10860 if (run.height > 0 && run.current_y != run.desired_y)
10861 {
10862 update_begin (f);
10863 rif->update_window_begin_hook (w);
10864 rif->clear_mouse_face (w);
10865 rif->scroll_run_hook (w, &run);
10866 rif->update_window_end_hook (w, 0, 0);
10867 update_end (f);
10868 }
10869
10870 /* Shift current matrix down by nrows_scrolled lines. */
10871 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10872 rotate_matrix (w->current_matrix,
10873 start_vpos,
10874 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10875 nrows_scrolled);
10876
10877 /* Disable lines that must be updated. */
10878 for (i = 0; i < it.vpos; ++i)
10879 (start_row + i)->enabled_p = 0;
10880
10881 /* Re-compute Y positions. */
10882 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10883 max_y = it.last_visible_y;
10884 for (row = start_row + nrows_scrolled;
10885 row < bottom_row;
10886 ++row)
10887 {
10888 row->y = it.current_y;
10889 row->visible_height = row->height;
10890
10891 if (row->y < min_y)
10892 row->visible_height -= min_y - row->y;
10893 if (row->y + row->height > max_y)
10894 row->visible_height -= row->y + row->height - max_y;
10895
10896 it.current_y += row->height;
10897
10898 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10899 last_reused_text_row = row;
10900 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10901 break;
10902 }
10903
10904 /* Disable lines in the current matrix which are now
10905 below the window. */
10906 for (++row; row < bottom_row; ++row)
10907 row->enabled_p = 0;
10908 }
10909
10910 /* Update window_end_pos etc.; last_reused_text_row is the last
10911 reused row from the current matrix containing text, if any.
10912 The value of last_text_row is the last displayed line
10913 containing text. */
10914 if (last_reused_text_row)
10915 {
10916 w->window_end_bytepos
10917 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10918 w->window_end_pos
10919 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10920 w->window_end_vpos
10921 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10922 w->current_matrix));
10923 }
10924 else if (last_text_row)
10925 {
10926 w->window_end_bytepos
10927 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10928 w->window_end_pos
10929 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10930 w->window_end_vpos
10931 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10932 }
10933 else
10934 {
10935 /* This window must be completely empty. */
10936 w->window_end_bytepos = 0;
10937 w->window_end_pos = w->window_end_vpos = make_number (0);
10938 }
10939 w->window_end_valid = Qnil;
10940
10941 /* Update hint: don't try scrolling again in update_window. */
10942 w->desired_matrix->no_scrolling_p = 1;
10943
10944 #if GLYPH_DEBUG
10945 debug_method_add (w, "try_window_reusing_current_matrix 1");
10946 #endif
10947 return 1;
10948 }
10949 else if (CHARPOS (new_start) > CHARPOS (start))
10950 {
10951 struct glyph_row *pt_row, *row;
10952 struct glyph_row *first_reusable_row;
10953 struct glyph_row *first_row_to_display;
10954 int dy;
10955 int yb = window_text_bottom_y (w);
10956
10957 /* Find the row starting at new_start, if there is one. Don't
10958 reuse a partially visible line at the end. */
10959 first_reusable_row = start_row;
10960 while (first_reusable_row->enabled_p
10961 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10962 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10963 < CHARPOS (new_start)))
10964 ++first_reusable_row;
10965
10966 /* Give up if there is no row to reuse. */
10967 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10968 || !first_reusable_row->enabled_p
10969 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10970 != CHARPOS (new_start)))
10971 return 0;
10972
10973 /* We can reuse fully visible rows beginning with
10974 first_reusable_row to the end of the window. Set
10975 first_row_to_display to the first row that cannot be reused.
10976 Set pt_row to the row containing point, if there is any. */
10977 pt_row = NULL;
10978 for (first_row_to_display = first_reusable_row;
10979 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
10980 ++first_row_to_display)
10981 {
10982 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10983 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10984 pt_row = first_row_to_display;
10985 }
10986
10987 /* Start displaying at the start of first_row_to_display. */
10988 xassert (first_row_to_display->y < yb);
10989 init_to_row_start (&it, w, first_row_to_display);
10990
10991 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10992 - start_vpos);
10993 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10994 - nrows_scrolled);
10995 it.current_y = (first_row_to_display->y - first_reusable_row->y
10996 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10997
10998 /* Display lines beginning with first_row_to_display in the
10999 desired matrix. Set last_text_row to the last row displayed
11000 that displays text. */
11001 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
11002 if (pt_row == NULL)
11003 w->cursor.vpos = -1;
11004 last_text_row = NULL;
11005 while (it.current_y < it.last_visible_y && !fonts_changed_p)
11006 if (display_line (&it))
11007 last_text_row = it.glyph_row - 1;
11008
11009 /* Give up If point isn't in a row displayed or reused. */
11010 if (w->cursor.vpos < 0)
11011 {
11012 clear_glyph_matrix (w->desired_matrix);
11013 return 0;
11014 }
11015
11016 /* If point is in a reused row, adjust y and vpos of the cursor
11017 position. */
11018 if (pt_row)
11019 {
11020 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
11021 w->current_matrix);
11022 w->cursor.y -= first_reusable_row->y;
11023 }
11024
11025 /* Scroll the display. */
11026 run.current_y = first_reusable_row->y;
11027 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11028 run.height = it.last_visible_y - run.current_y;
11029 dy = run.current_y - run.desired_y;
11030
11031 if (run.height)
11032 {
11033 struct frame *f = XFRAME (WINDOW_FRAME (w));
11034 update_begin (f);
11035 rif->update_window_begin_hook (w);
11036 rif->clear_mouse_face (w);
11037 rif->scroll_run_hook (w, &run);
11038 rif->update_window_end_hook (w, 0, 0);
11039 update_end (f);
11040 }
11041
11042 /* Adjust Y positions of reused rows. */
11043 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11044 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11045 max_y = it.last_visible_y;
11046 for (row = first_reusable_row; row < first_row_to_display; ++row)
11047 {
11048 row->y -= dy;
11049 row->visible_height = row->height;
11050 if (row->y < min_y)
11051 row->visible_height -= min_y - row->y;
11052 if (row->y + row->height > max_y)
11053 row->visible_height -= row->y + row->height - max_y;
11054 }
11055
11056 /* Scroll the current matrix. */
11057 xassert (nrows_scrolled > 0);
11058 rotate_matrix (w->current_matrix,
11059 start_vpos,
11060 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11061 -nrows_scrolled);
11062
11063 /* Disable rows not reused. */
11064 for (row -= nrows_scrolled; row < bottom_row; ++row)
11065 row->enabled_p = 0;
11066
11067 /* Adjust window end. A null value of last_text_row means that
11068 the window end is in reused rows which in turn means that
11069 only its vpos can have changed. */
11070 if (last_text_row)
11071 {
11072 w->window_end_bytepos
11073 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11074 w->window_end_pos
11075 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11076 w->window_end_vpos
11077 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11078 }
11079 else
11080 {
11081 w->window_end_vpos
11082 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
11083 }
11084
11085 w->window_end_valid = Qnil;
11086 w->desired_matrix->no_scrolling_p = 1;
11087
11088 #if GLYPH_DEBUG
11089 debug_method_add (w, "try_window_reusing_current_matrix 2");
11090 #endif
11091 return 1;
11092 }
11093
11094 return 0;
11095 }
11096
11097
11098 \f
11099 /************************************************************************
11100 Window redisplay reusing current matrix when buffer has changed
11101 ************************************************************************/
11102
11103 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11104 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
11105 int *, int *));
11106 static struct glyph_row *
11107 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11108 struct glyph_row *));
11109
11110
11111 /* Return the last row in MATRIX displaying text. If row START is
11112 non-null, start searching with that row. IT gives the dimensions
11113 of the display. Value is null if matrix is empty; otherwise it is
11114 a pointer to the row found. */
11115
11116 static struct glyph_row *
11117 find_last_row_displaying_text (matrix, it, start)
11118 struct glyph_matrix *matrix;
11119 struct it *it;
11120 struct glyph_row *start;
11121 {
11122 struct glyph_row *row, *row_found;
11123
11124 /* Set row_found to the last row in IT->w's current matrix
11125 displaying text. The loop looks funny but think of partially
11126 visible lines. */
11127 row_found = NULL;
11128 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11129 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11130 {
11131 xassert (row->enabled_p);
11132 row_found = row;
11133 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11134 break;
11135 ++row;
11136 }
11137
11138 return row_found;
11139 }
11140
11141
11142 /* Return the last row in the current matrix of W that is not affected
11143 by changes at the start of current_buffer that occurred since W's
11144 current matrix was built. Value is null if no such row exists.
11145
11146 BEG_UNCHANGED us the number of characters unchanged at the start of
11147 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11148 first changed character in current_buffer. Characters at positions <
11149 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11150 when the current matrix was built. */
11151
11152 static struct glyph_row *
11153 find_last_unchanged_at_beg_row (w)
11154 struct window *w;
11155 {
11156 int first_changed_pos = BEG + BEG_UNCHANGED;
11157 struct glyph_row *row;
11158 struct glyph_row *row_found = NULL;
11159 int yb = window_text_bottom_y (w);
11160
11161 /* Find the last row displaying unchanged text. */
11162 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11163 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11164 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11165 {
11166 if (/* If row ends before first_changed_pos, it is unchanged,
11167 except in some case. */
11168 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11169 /* When row ends in ZV and we write at ZV it is not
11170 unchanged. */
11171 && !row->ends_at_zv_p
11172 /* When first_changed_pos is the end of a continued line,
11173 row is not unchanged because it may be no longer
11174 continued. */
11175 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11176 && row->continued_p))
11177 row_found = row;
11178
11179 /* Stop if last visible row. */
11180 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11181 break;
11182
11183 ++row;
11184 }
11185
11186 return row_found;
11187 }
11188
11189
11190 /* Find the first glyph row in the current matrix of W that is not
11191 affected by changes at the end of current_buffer since the
11192 time W's current matrix was built.
11193
11194 Return in *DELTA the number of chars by which buffer positions in
11195 unchanged text at the end of current_buffer must be adjusted.
11196
11197 Return in *DELTA_BYTES the corresponding number of bytes.
11198
11199 Value is null if no such row exists, i.e. all rows are affected by
11200 changes. */
11201
11202 static struct glyph_row *
11203 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11204 struct window *w;
11205 int *delta, *delta_bytes;
11206 {
11207 struct glyph_row *row;
11208 struct glyph_row *row_found = NULL;
11209
11210 *delta = *delta_bytes = 0;
11211
11212 /* Display must not have been paused, otherwise the current matrix
11213 is not up to date. */
11214 if (NILP (w->window_end_valid))
11215 abort ();
11216
11217 /* A value of window_end_pos >= END_UNCHANGED means that the window
11218 end is in the range of changed text. If so, there is no
11219 unchanged row at the end of W's current matrix. */
11220 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11221 return NULL;
11222
11223 /* Set row to the last row in W's current matrix displaying text. */
11224 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11225
11226 /* If matrix is entirely empty, no unchanged row exists. */
11227 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11228 {
11229 /* The value of row is the last glyph row in the matrix having a
11230 meaningful buffer position in it. The end position of row
11231 corresponds to window_end_pos. This allows us to translate
11232 buffer positions in the current matrix to current buffer
11233 positions for characters not in changed text. */
11234 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11235 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11236 int last_unchanged_pos, last_unchanged_pos_old;
11237 struct glyph_row *first_text_row
11238 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11239
11240 *delta = Z - Z_old;
11241 *delta_bytes = Z_BYTE - Z_BYTE_old;
11242
11243 /* Set last_unchanged_pos to the buffer position of the last
11244 character in the buffer that has not been changed. Z is the
11245 index + 1 of the last character in current_buffer, i.e. by
11246 subtracting END_UNCHANGED we get the index of the last
11247 unchanged character, and we have to add BEG to get its buffer
11248 position. */
11249 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11250 last_unchanged_pos_old = last_unchanged_pos - *delta;
11251
11252 /* Search backward from ROW for a row displaying a line that
11253 starts at a minimum position >= last_unchanged_pos_old. */
11254 for (; row > first_text_row; --row)
11255 {
11256 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11257 abort ();
11258
11259 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11260 row_found = row;
11261 }
11262 }
11263
11264 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11265 abort ();
11266
11267 return row_found;
11268 }
11269
11270
11271 /* Make sure that glyph rows in the current matrix of window W
11272 reference the same glyph memory as corresponding rows in the
11273 frame's frame matrix. This function is called after scrolling W's
11274 current matrix on a terminal frame in try_window_id and
11275 try_window_reusing_current_matrix. */
11276
11277 static void
11278 sync_frame_with_window_matrix_rows (w)
11279 struct window *w;
11280 {
11281 struct frame *f = XFRAME (w->frame);
11282 struct glyph_row *window_row, *window_row_end, *frame_row;
11283
11284 /* Preconditions: W must be a leaf window and full-width. Its frame
11285 must have a frame matrix. */
11286 xassert (NILP (w->hchild) && NILP (w->vchild));
11287 xassert (WINDOW_FULL_WIDTH_P (w));
11288 xassert (!FRAME_WINDOW_P (f));
11289
11290 /* If W is a full-width window, glyph pointers in W's current matrix
11291 have, by definition, to be the same as glyph pointers in the
11292 corresponding frame matrix. */
11293 window_row = w->current_matrix->rows;
11294 window_row_end = window_row + w->current_matrix->nrows;
11295 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11296 while (window_row < window_row_end)
11297 {
11298 int area;
11299
11300 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
11301 frame_row->glyphs[area] = window_row->glyphs[area];
11302
11303 /* Disable frame rows whose corresponding window rows have
11304 been disabled in try_window_id. */
11305 if (!window_row->enabled_p)
11306 frame_row->enabled_p = 0;
11307
11308 ++window_row, ++frame_row;
11309 }
11310 }
11311
11312
11313 /* Find the glyph row in window W containing CHARPOS. Consider all
11314 rows between START and END (not inclusive). END null means search
11315 all rows to the end of the display area of W. Value is the row
11316 containing CHARPOS or null. */
11317
11318 struct glyph_row *
11319 row_containing_pos (w, charpos, start, end, dy)
11320 struct window *w;
11321 int charpos;
11322 struct glyph_row *start, *end;
11323 int dy;
11324 {
11325 struct glyph_row *row = start;
11326 int last_y;
11327
11328 /* If we happen to start on a header-line, skip that. */
11329 if (row->mode_line_p)
11330 ++row;
11331
11332 if ((end && row >= end) || !row->enabled_p)
11333 return NULL;
11334
11335 last_y = window_text_bottom_y (w) - dy;
11336
11337 while ((end == NULL || row < end)
11338 && MATRIX_ROW_BOTTOM_Y (row) < last_y
11339 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11340 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11341 /* The end position of a row equals the start
11342 position of the next row. If CHARPOS is there, we
11343 would rather display it in the next line, except
11344 when this line ends in ZV. */
11345 && !row->ends_at_zv_p
11346 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))
11347 ++row;
11348
11349 /* Give up if CHARPOS not found. */
11350 if ((end && row >= end)
11351 || charpos < MATRIX_ROW_START_CHARPOS (row)
11352 || charpos > MATRIX_ROW_END_CHARPOS (row))
11353 row = NULL;
11354
11355 return row;
11356 }
11357
11358
11359 /* Try to redisplay window W by reusing its existing display. W's
11360 current matrix must be up to date when this function is called,
11361 i.e. window_end_valid must not be nil.
11362
11363 Value is
11364
11365 1 if display has been updated
11366 0 if otherwise unsuccessful
11367 -1 if redisplay with same window start is known not to succeed
11368
11369 The following steps are performed:
11370
11371 1. Find the last row in the current matrix of W that is not
11372 affected by changes at the start of current_buffer. If no such row
11373 is found, give up.
11374
11375 2. Find the first row in W's current matrix that is not affected by
11376 changes at the end of current_buffer. Maybe there is no such row.
11377
11378 3. Display lines beginning with the row + 1 found in step 1 to the
11379 row found in step 2 or, if step 2 didn't find a row, to the end of
11380 the window.
11381
11382 4. If cursor is not known to appear on the window, give up.
11383
11384 5. If display stopped at the row found in step 2, scroll the
11385 display and current matrix as needed.
11386
11387 6. Maybe display some lines at the end of W, if we must. This can
11388 happen under various circumstances, like a partially visible line
11389 becoming fully visible, or because newly displayed lines are displayed
11390 in smaller font sizes.
11391
11392 7. Update W's window end information. */
11393
11394 static int
11395 try_window_id (w)
11396 struct window *w;
11397 {
11398 struct frame *f = XFRAME (w->frame);
11399 struct glyph_matrix *current_matrix = w->current_matrix;
11400 struct glyph_matrix *desired_matrix = w->desired_matrix;
11401 struct glyph_row *last_unchanged_at_beg_row;
11402 struct glyph_row *first_unchanged_at_end_row;
11403 struct glyph_row *row;
11404 struct glyph_row *bottom_row;
11405 int bottom_vpos;
11406 struct it it;
11407 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11408 struct text_pos start_pos;
11409 struct run run;
11410 int first_unchanged_at_end_vpos = 0;
11411 struct glyph_row *last_text_row, *last_text_row_at_end;
11412 struct text_pos start;
11413 int first_changed_charpos, last_changed_charpos;
11414
11415 #if GLYPH_DEBUG
11416 if (inhibit_try_window_id)
11417 return 0;
11418 #endif
11419
11420 /* This is handy for debugging. */
11421 #if 0
11422 #define GIVE_UP(X) \
11423 do { \
11424 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11425 return 0; \
11426 } while (0)
11427 #else
11428 #define GIVE_UP(X) return 0
11429 #endif
11430
11431 SET_TEXT_POS_FROM_MARKER (start, w->start);
11432
11433 /* Don't use this for mini-windows because these can show
11434 messages and mini-buffers, and we don't handle that here. */
11435 if (MINI_WINDOW_P (w))
11436 GIVE_UP (1);
11437
11438 /* This flag is used to prevent redisplay optimizations. */
11439 if (windows_or_buffers_changed)
11440 GIVE_UP (2);
11441
11442 /* Verify that narrowing has not changed. This flag is also set to prevent
11443 redisplay optimizations. It would be nice to further
11444 reduce the number of cases where this prevents try_window_id. */
11445 if (current_buffer->clip_changed)
11446 GIVE_UP (3);
11447
11448 /* Window must either use window-based redisplay or be full width. */
11449 if (!FRAME_WINDOW_P (f)
11450 && (!line_ins_del_ok
11451 || !WINDOW_FULL_WIDTH_P (w)))
11452 GIVE_UP (4);
11453
11454 /* Give up if point is not known NOT to appear in W. */
11455 if (PT < CHARPOS (start))
11456 GIVE_UP (5);
11457
11458 /* Another way to prevent redisplay optimizations. */
11459 if (XFASTINT (w->last_modified) == 0)
11460 GIVE_UP (6);
11461
11462 /* Verify that window is not hscrolled. */
11463 if (XFASTINT (w->hscroll) != 0)
11464 GIVE_UP (7);
11465
11466 /* Verify that display wasn't paused. */
11467 if (NILP (w->window_end_valid))
11468 GIVE_UP (8);
11469
11470 /* Can't use this if highlighting a region because a cursor movement
11471 will do more than just set the cursor. */
11472 if (!NILP (Vtransient_mark_mode)
11473 && !NILP (current_buffer->mark_active))
11474 GIVE_UP (9);
11475
11476 /* Likewise if highlighting trailing whitespace. */
11477 if (!NILP (Vshow_trailing_whitespace))
11478 GIVE_UP (11);
11479
11480 /* Likewise if showing a region. */
11481 if (!NILP (w->region_showing))
11482 GIVE_UP (10);
11483
11484 /* Can use this if overlay arrow position and or string have changed. */
11485 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11486 || !EQ (last_arrow_string, Voverlay_arrow_string))
11487 GIVE_UP (12);
11488
11489
11490 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11491 only if buffer has really changed. The reason is that the gap is
11492 initially at Z for freshly visited files. The code below would
11493 set end_unchanged to 0 in that case. */
11494 if (MODIFF > SAVE_MODIFF
11495 /* This seems to happen sometimes after saving a buffer. */
11496 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11497 {
11498 if (GPT - BEG < BEG_UNCHANGED)
11499 BEG_UNCHANGED = GPT - BEG;
11500 if (Z - GPT < END_UNCHANGED)
11501 END_UNCHANGED = Z - GPT;
11502 }
11503
11504 /* The position of the first and last character that has been changed. */
11505 first_changed_charpos = BEG + BEG_UNCHANGED;
11506 last_changed_charpos = Z - END_UNCHANGED;
11507
11508 /* If window starts after a line end, and the last change is in
11509 front of that newline, then changes don't affect the display.
11510 This case happens with stealth-fontification. Note that although
11511 the display is unchanged, glyph positions in the matrix have to
11512 be adjusted, of course. */
11513 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11514 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11515 && ((last_changed_charpos < CHARPOS (start)
11516 && CHARPOS (start) == BEGV)
11517 || (last_changed_charpos < CHARPOS (start) - 1
11518 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11519 {
11520 int Z_old, delta, Z_BYTE_old, delta_bytes;
11521 struct glyph_row *r0;
11522
11523 /* Compute how many chars/bytes have been added to or removed
11524 from the buffer. */
11525 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11526 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11527 delta = Z - Z_old;
11528 delta_bytes = Z_BYTE - Z_BYTE_old;
11529
11530 /* Give up if PT is not in the window. Note that it already has
11531 been checked at the start of try_window_id that PT is not in
11532 front of the window start. */
11533 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11534 GIVE_UP (13);
11535
11536 /* If window start is unchanged, we can reuse the whole matrix
11537 as is, after adjusting glyph positions. No need to compute
11538 the window end again, since its offset from Z hasn't changed. */
11539 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11540 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11541 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11542 {
11543 /* Adjust positions in the glyph matrix. */
11544 if (delta || delta_bytes)
11545 {
11546 struct glyph_row *r1
11547 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11548 increment_matrix_positions (w->current_matrix,
11549 MATRIX_ROW_VPOS (r0, current_matrix),
11550 MATRIX_ROW_VPOS (r1, current_matrix),
11551 delta, delta_bytes);
11552 }
11553
11554 /* Set the cursor. */
11555 row = row_containing_pos (w, PT, r0, NULL, 0);
11556 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11557 return 1;
11558 }
11559 }
11560
11561 /* Handle the case that changes are all below what is displayed in
11562 the window, and that PT is in the window. This shortcut cannot
11563 be taken if ZV is visible in the window, and text has been added
11564 there that is visible in the window. */
11565 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11566 /* ZV is not visible in the window, or there are no
11567 changes at ZV, actually. */
11568 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11569 || first_changed_charpos == last_changed_charpos))
11570 {
11571 struct glyph_row *r0;
11572
11573 /* Give up if PT is not in the window. Note that it already has
11574 been checked at the start of try_window_id that PT is not in
11575 front of the window start. */
11576 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11577 GIVE_UP (14);
11578
11579 /* If window start is unchanged, we can reuse the whole matrix
11580 as is, without changing glyph positions since no text has
11581 been added/removed in front of the window end. */
11582 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11583 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11584 {
11585 /* We have to compute the window end anew since text
11586 can have been added/removed after it. */
11587 w->window_end_pos
11588 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11589 w->window_end_bytepos
11590 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11591
11592 /* Set the cursor. */
11593 row = row_containing_pos (w, PT, r0, NULL, 0);
11594 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11595 return 2;
11596 }
11597 }
11598
11599 /* Give up if window start is in the changed area.
11600
11601 The condition used to read
11602
11603 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11604
11605 but why that was tested escapes me at the moment. */
11606 if (CHARPOS (start) >= first_changed_charpos
11607 && CHARPOS (start) <= last_changed_charpos)
11608 GIVE_UP (15);
11609
11610 /* Check that window start agrees with the start of the first glyph
11611 row in its current matrix. Check this after we know the window
11612 start is not in changed text, otherwise positions would not be
11613 comparable. */
11614 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11615 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11616 GIVE_UP (16);
11617
11618 /* Give up if the window ends in strings. Overlay strings
11619 at the end are difficult to handle, so don't try. */
11620 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11621 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11622 GIVE_UP (20);
11623
11624 /* Compute the position at which we have to start displaying new
11625 lines. Some of the lines at the top of the window might be
11626 reusable because they are not displaying changed text. Find the
11627 last row in W's current matrix not affected by changes at the
11628 start of current_buffer. Value is null if changes start in the
11629 first line of window. */
11630 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11631 if (last_unchanged_at_beg_row)
11632 {
11633 /* Avoid starting to display in the moddle of a character, a TAB
11634 for instance. This is easier than to set up the iterator
11635 exactly, and it's not a frequent case, so the additional
11636 effort wouldn't really pay off. */
11637 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11638 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
11639 && last_unchanged_at_beg_row > w->current_matrix->rows)
11640 --last_unchanged_at_beg_row;
11641
11642 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11643 GIVE_UP (17);
11644
11645 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11646 GIVE_UP (18);
11647 start_pos = it.current.pos;
11648
11649 /* Start displaying new lines in the desired matrix at the same
11650 vpos we would use in the current matrix, i.e. below
11651 last_unchanged_at_beg_row. */
11652 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11653 current_matrix);
11654 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11655 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11656
11657 xassert (it.hpos == 0 && it.current_x == 0);
11658 }
11659 else
11660 {
11661 /* There are no reusable lines at the start of the window.
11662 Start displaying in the first line. */
11663 start_display (&it, w, start);
11664 start_pos = it.current.pos;
11665 }
11666
11667 /* Find the first row that is not affected by changes at the end of
11668 the buffer. Value will be null if there is no unchanged row, in
11669 which case we must redisplay to the end of the window. delta
11670 will be set to the value by which buffer positions beginning with
11671 first_unchanged_at_end_row have to be adjusted due to text
11672 changes. */
11673 first_unchanged_at_end_row
11674 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11675 IF_DEBUG (debug_delta = delta);
11676 IF_DEBUG (debug_delta_bytes = delta_bytes);
11677
11678 /* Set stop_pos to the buffer position up to which we will have to
11679 display new lines. If first_unchanged_at_end_row != NULL, this
11680 is the buffer position of the start of the line displayed in that
11681 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11682 that we don't stop at a buffer position. */
11683 stop_pos = 0;
11684 if (first_unchanged_at_end_row)
11685 {
11686 xassert (last_unchanged_at_beg_row == NULL
11687 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11688
11689 /* If this is a continuation line, move forward to the next one
11690 that isn't. Changes in lines above affect this line.
11691 Caution: this may move first_unchanged_at_end_row to a row
11692 not displaying text. */
11693 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11694 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11695 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11696 < it.last_visible_y))
11697 ++first_unchanged_at_end_row;
11698
11699 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11700 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11701 >= it.last_visible_y))
11702 first_unchanged_at_end_row = NULL;
11703 else
11704 {
11705 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11706 + delta);
11707 first_unchanged_at_end_vpos
11708 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11709 xassert (stop_pos >= Z - END_UNCHANGED);
11710 }
11711 }
11712 else if (last_unchanged_at_beg_row == NULL)
11713 GIVE_UP (19);
11714
11715
11716 #if GLYPH_DEBUG
11717
11718 /* Either there is no unchanged row at the end, or the one we have
11719 now displays text. This is a necessary condition for the window
11720 end pos calculation at the end of this function. */
11721 xassert (first_unchanged_at_end_row == NULL
11722 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11723
11724 debug_last_unchanged_at_beg_vpos
11725 = (last_unchanged_at_beg_row
11726 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11727 : -1);
11728 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11729
11730 #endif /* GLYPH_DEBUG != 0 */
11731
11732
11733 /* Display new lines. Set last_text_row to the last new line
11734 displayed which has text on it, i.e. might end up as being the
11735 line where the window_end_vpos is. */
11736 w->cursor.vpos = -1;
11737 last_text_row = NULL;
11738 overlay_arrow_seen = 0;
11739 while (it.current_y < it.last_visible_y
11740 && !fonts_changed_p
11741 && (first_unchanged_at_end_row == NULL
11742 || IT_CHARPOS (it) < stop_pos))
11743 {
11744 if (display_line (&it))
11745 last_text_row = it.glyph_row - 1;
11746 }
11747
11748 if (fonts_changed_p)
11749 return -1;
11750
11751
11752 /* Compute differences in buffer positions, y-positions etc. for
11753 lines reused at the bottom of the window. Compute what we can
11754 scroll. */
11755 if (first_unchanged_at_end_row
11756 /* No lines reused because we displayed everything up to the
11757 bottom of the window. */
11758 && it.current_y < it.last_visible_y)
11759 {
11760 dvpos = (it.vpos
11761 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11762 current_matrix));
11763 dy = it.current_y - first_unchanged_at_end_row->y;
11764 run.current_y = first_unchanged_at_end_row->y;
11765 run.desired_y = run.current_y + dy;
11766 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11767 }
11768 else
11769 {
11770 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11771 first_unchanged_at_end_row = NULL;
11772 }
11773 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11774
11775
11776 /* Find the cursor if not already found. We have to decide whether
11777 PT will appear on this window (it sometimes doesn't, but this is
11778 not a very frequent case.) This decision has to be made before
11779 the current matrix is altered. A value of cursor.vpos < 0 means
11780 that PT is either in one of the lines beginning at
11781 first_unchanged_at_end_row or below the window. Don't care for
11782 lines that might be displayed later at the window end; as
11783 mentioned, this is not a frequent case. */
11784 if (w->cursor.vpos < 0)
11785 {
11786 /* Cursor in unchanged rows at the top? */
11787 if (PT < CHARPOS (start_pos)
11788 && last_unchanged_at_beg_row)
11789 {
11790 row = row_containing_pos (w, PT,
11791 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11792 last_unchanged_at_beg_row + 1, 0);
11793 if (row)
11794 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11795 }
11796
11797 /* Start from first_unchanged_at_end_row looking for PT. */
11798 else if (first_unchanged_at_end_row)
11799 {
11800 row = row_containing_pos (w, PT - delta,
11801 first_unchanged_at_end_row, NULL, 0);
11802 if (row)
11803 set_cursor_from_row (w, row, w->current_matrix, delta,
11804 delta_bytes, dy, dvpos);
11805 }
11806
11807 /* Give up if cursor was not found. */
11808 if (w->cursor.vpos < 0)
11809 {
11810 clear_glyph_matrix (w->desired_matrix);
11811 return -1;
11812 }
11813 }
11814
11815 /* Don't let the cursor end in the scroll margins. */
11816 {
11817 int this_scroll_margin, cursor_height;
11818
11819 this_scroll_margin = max (0, scroll_margin);
11820 this_scroll_margin = min (this_scroll_margin,
11821 XFASTINT (w->height) / 4);
11822 this_scroll_margin *= CANON_Y_UNIT (it.f);
11823 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11824
11825 if ((w->cursor.y < this_scroll_margin
11826 && CHARPOS (start) > BEGV)
11827 /* Don't take scroll margin into account at the bottom because
11828 old redisplay didn't do it either. */
11829 || w->cursor.y + cursor_height > it.last_visible_y)
11830 {
11831 w->cursor.vpos = -1;
11832 clear_glyph_matrix (w->desired_matrix);
11833 return -1;
11834 }
11835 }
11836
11837 /* Scroll the display. Do it before changing the current matrix so
11838 that xterm.c doesn't get confused about where the cursor glyph is
11839 found. */
11840 if (dy && run.height)
11841 {
11842 update_begin (f);
11843
11844 if (FRAME_WINDOW_P (f))
11845 {
11846 rif->update_window_begin_hook (w);
11847 rif->clear_mouse_face (w);
11848 rif->scroll_run_hook (w, &run);
11849 rif->update_window_end_hook (w, 0, 0);
11850 }
11851 else
11852 {
11853 /* Terminal frame. In this case, dvpos gives the number of
11854 lines to scroll by; dvpos < 0 means scroll up. */
11855 int first_unchanged_at_end_vpos
11856 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11857 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11858 int end = (XFASTINT (w->top)
11859 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11860 + window_internal_height (w));
11861
11862 /* Perform the operation on the screen. */
11863 if (dvpos > 0)
11864 {
11865 /* Scroll last_unchanged_at_beg_row to the end of the
11866 window down dvpos lines. */
11867 set_terminal_window (end);
11868
11869 /* On dumb terminals delete dvpos lines at the end
11870 before inserting dvpos empty lines. */
11871 if (!scroll_region_ok)
11872 ins_del_lines (end - dvpos, -dvpos);
11873
11874 /* Insert dvpos empty lines in front of
11875 last_unchanged_at_beg_row. */
11876 ins_del_lines (from, dvpos);
11877 }
11878 else if (dvpos < 0)
11879 {
11880 /* Scroll up last_unchanged_at_beg_vpos to the end of
11881 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11882 set_terminal_window (end);
11883
11884 /* Delete dvpos lines in front of
11885 last_unchanged_at_beg_vpos. ins_del_lines will set
11886 the cursor to the given vpos and emit |dvpos| delete
11887 line sequences. */
11888 ins_del_lines (from + dvpos, dvpos);
11889
11890 /* On a dumb terminal insert dvpos empty lines at the
11891 end. */
11892 if (!scroll_region_ok)
11893 ins_del_lines (end + dvpos, -dvpos);
11894 }
11895
11896 set_terminal_window (0);
11897 }
11898
11899 update_end (f);
11900 }
11901
11902 /* Shift reused rows of the current matrix to the right position.
11903 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11904 text. */
11905 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11906 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11907 if (dvpos < 0)
11908 {
11909 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11910 bottom_vpos, dvpos);
11911 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11912 bottom_vpos, 0);
11913 }
11914 else if (dvpos > 0)
11915 {
11916 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11917 bottom_vpos, dvpos);
11918 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11919 first_unchanged_at_end_vpos + dvpos, 0);
11920 }
11921
11922 /* For frame-based redisplay, make sure that current frame and window
11923 matrix are in sync with respect to glyph memory. */
11924 if (!FRAME_WINDOW_P (f))
11925 sync_frame_with_window_matrix_rows (w);
11926
11927 /* Adjust buffer positions in reused rows. */
11928 if (delta)
11929 increment_matrix_positions (current_matrix,
11930 first_unchanged_at_end_vpos + dvpos,
11931 bottom_vpos, delta, delta_bytes);
11932
11933 /* Adjust Y positions. */
11934 if (dy)
11935 shift_glyph_matrix (w, current_matrix,
11936 first_unchanged_at_end_vpos + dvpos,
11937 bottom_vpos, dy);
11938
11939 if (first_unchanged_at_end_row)
11940 first_unchanged_at_end_row += dvpos;
11941
11942 /* If scrolling up, there may be some lines to display at the end of
11943 the window. */
11944 last_text_row_at_end = NULL;
11945 if (dy < 0)
11946 {
11947 /* Scrolling up can leave for example a partially visible line
11948 at the end of the window to be redisplayed. */
11949 /* Set last_row to the glyph row in the current matrix where the
11950 window end line is found. It has been moved up or down in
11951 the matrix by dvpos. */
11952 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11953 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11954
11955 /* If last_row is the window end line, it should display text. */
11956 xassert (last_row->displays_text_p);
11957
11958 /* If window end line was partially visible before, begin
11959 displaying at that line. Otherwise begin displaying with the
11960 line following it. */
11961 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11962 {
11963 init_to_row_start (&it, w, last_row);
11964 it.vpos = last_vpos;
11965 it.current_y = last_row->y;
11966 }
11967 else
11968 {
11969 init_to_row_end (&it, w, last_row);
11970 it.vpos = 1 + last_vpos;
11971 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11972 ++last_row;
11973 }
11974
11975 /* We may start in a continuation line. If so, we have to
11976 get the right continuation_lines_width and current_x. */
11977 it.continuation_lines_width = last_row->continuation_lines_width;
11978 it.hpos = it.current_x = 0;
11979
11980 /* Display the rest of the lines at the window end. */
11981 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11982 while (it.current_y < it.last_visible_y
11983 && !fonts_changed_p)
11984 {
11985 /* Is it always sure that the display agrees with lines in
11986 the current matrix? I don't think so, so we mark rows
11987 displayed invalid in the current matrix by setting their
11988 enabled_p flag to zero. */
11989 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11990 if (display_line (&it))
11991 last_text_row_at_end = it.glyph_row - 1;
11992 }
11993 }
11994
11995 /* Update window_end_pos and window_end_vpos. */
11996 if (first_unchanged_at_end_row
11997 && first_unchanged_at_end_row->y < it.last_visible_y
11998 && !last_text_row_at_end)
11999 {
12000 /* Window end line if one of the preserved rows from the current
12001 matrix. Set row to the last row displaying text in current
12002 matrix starting at first_unchanged_at_end_row, after
12003 scrolling. */
12004 xassert (first_unchanged_at_end_row->displays_text_p);
12005 row = find_last_row_displaying_text (w->current_matrix, &it,
12006 first_unchanged_at_end_row);
12007 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
12008
12009 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12010 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12011 w->window_end_vpos
12012 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
12013 xassert (w->window_end_bytepos >= 0);
12014 IF_DEBUG (debug_method_add (w, "A"));
12015 }
12016 else if (last_text_row_at_end)
12017 {
12018 w->window_end_pos
12019 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
12020 w->window_end_bytepos
12021 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
12022 w->window_end_vpos
12023 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
12024 xassert (w->window_end_bytepos >= 0);
12025 IF_DEBUG (debug_method_add (w, "B"));
12026 }
12027 else if (last_text_row)
12028 {
12029 /* We have displayed either to the end of the window or at the
12030 end of the window, i.e. the last row with text is to be found
12031 in the desired matrix. */
12032 w->window_end_pos
12033 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12034 w->window_end_bytepos
12035 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12036 w->window_end_vpos
12037 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
12038 xassert (w->window_end_bytepos >= 0);
12039 }
12040 else if (first_unchanged_at_end_row == NULL
12041 && last_text_row == NULL
12042 && last_text_row_at_end == NULL)
12043 {
12044 /* Displayed to end of window, but no line containing text was
12045 displayed. Lines were deleted at the end of the window. */
12046 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12047 int vpos = XFASTINT (w->window_end_vpos);
12048 struct glyph_row *current_row = current_matrix->rows + vpos;
12049 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12050
12051 for (row = NULL;
12052 row == NULL && vpos >= first_vpos;
12053 --vpos, --current_row, --desired_row)
12054 {
12055 if (desired_row->enabled_p)
12056 {
12057 if (desired_row->displays_text_p)
12058 row = desired_row;
12059 }
12060 else if (current_row->displays_text_p)
12061 row = current_row;
12062 }
12063
12064 xassert (row != NULL);
12065 w->window_end_vpos = make_number (vpos + 1);
12066 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12067 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12068 xassert (w->window_end_bytepos >= 0);
12069 IF_DEBUG (debug_method_add (w, "C"));
12070 }
12071 else
12072 abort ();
12073
12074 #if 0 /* This leads to problems, for instance when the cursor is
12075 at ZV, and the cursor line displays no text. */
12076 /* Disable rows below what's displayed in the window. This makes
12077 debugging easier. */
12078 enable_glyph_matrix_rows (current_matrix,
12079 XFASTINT (w->window_end_vpos) + 1,
12080 bottom_vpos, 0);
12081 #endif
12082
12083 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12084 debug_end_vpos = XFASTINT (w->window_end_vpos));
12085
12086 /* Record that display has not been completed. */
12087 w->window_end_valid = Qnil;
12088 w->desired_matrix->no_scrolling_p = 1;
12089 return 3;
12090
12091 #undef GIVE_UP
12092 }
12093
12094
12095 \f
12096 /***********************************************************************
12097 More debugging support
12098 ***********************************************************************/
12099
12100 #if GLYPH_DEBUG
12101
12102 void dump_glyph_row P_ ((struct glyph_row *, int, int));
12103 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
12104 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
12105
12106
12107 /* Dump the contents of glyph matrix MATRIX on stderr.
12108
12109 GLYPHS 0 means don't show glyph contents.
12110 GLYPHS 1 means show glyphs in short form
12111 GLYPHS > 1 means show glyphs in long form. */
12112
12113 void
12114 dump_glyph_matrix (matrix, glyphs)
12115 struct glyph_matrix *matrix;
12116 int glyphs;
12117 {
12118 int i;
12119 for (i = 0; i < matrix->nrows; ++i)
12120 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
12121 }
12122
12123
12124 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12125 the glyph row and area where the glyph comes from. */
12126
12127 void
12128 dump_glyph (row, glyph, area)
12129 struct glyph_row *row;
12130 struct glyph *glyph;
12131 int area;
12132 {
12133 if (glyph->type == CHAR_GLYPH)
12134 {
12135 fprintf (stderr,
12136 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12137 glyph - row->glyphs[TEXT_AREA],
12138 'C',
12139 glyph->charpos,
12140 (BUFFERP (glyph->object)
12141 ? 'B'
12142 : (STRINGP (glyph->object)
12143 ? 'S'
12144 : '-')),
12145 glyph->pixel_width,
12146 glyph->u.ch,
12147 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12148 ? glyph->u.ch
12149 : '.'),
12150 glyph->face_id,
12151 glyph->left_box_line_p,
12152 glyph->right_box_line_p);
12153 }
12154 else if (glyph->type == STRETCH_GLYPH)
12155 {
12156 fprintf (stderr,
12157 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12158 glyph - row->glyphs[TEXT_AREA],
12159 'S',
12160 glyph->charpos,
12161 (BUFFERP (glyph->object)
12162 ? 'B'
12163 : (STRINGP (glyph->object)
12164 ? 'S'
12165 : '-')),
12166 glyph->pixel_width,
12167 0,
12168 '.',
12169 glyph->face_id,
12170 glyph->left_box_line_p,
12171 glyph->right_box_line_p);
12172 }
12173 else if (glyph->type == IMAGE_GLYPH)
12174 {
12175 fprintf (stderr,
12176 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12177 glyph - row->glyphs[TEXT_AREA],
12178 'I',
12179 glyph->charpos,
12180 (BUFFERP (glyph->object)
12181 ? 'B'
12182 : (STRINGP (glyph->object)
12183 ? 'S'
12184 : '-')),
12185 glyph->pixel_width,
12186 glyph->u.img_id,
12187 '.',
12188 glyph->face_id,
12189 glyph->left_box_line_p,
12190 glyph->right_box_line_p);
12191 }
12192 }
12193
12194
12195 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12196 GLYPHS 0 means don't show glyph contents.
12197 GLYPHS 1 means show glyphs in short form
12198 GLYPHS > 1 means show glyphs in long form. */
12199
12200 void
12201 dump_glyph_row (row, vpos, glyphs)
12202 struct glyph_row *row;
12203 int vpos, glyphs;
12204 {
12205 if (glyphs != 1)
12206 {
12207 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12208 fprintf (stderr, "=======================================================================\n");
12209
12210 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
12211 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12212 vpos,
12213 MATRIX_ROW_START_CHARPOS (row),
12214 MATRIX_ROW_END_CHARPOS (row),
12215 row->used[TEXT_AREA],
12216 row->contains_overlapping_glyphs_p,
12217 row->enabled_p,
12218 row->truncated_on_left_p,
12219 row->truncated_on_right_p,
12220 row->overlay_arrow_p,
12221 row->continued_p,
12222 MATRIX_ROW_CONTINUATION_LINE_P (row),
12223 row->displays_text_p,
12224 row->ends_at_zv_p,
12225 row->fill_line_p,
12226 row->ends_in_middle_of_char_p,
12227 row->starts_in_middle_of_char_p,
12228 row->mouse_face_p,
12229 row->x,
12230 row->y,
12231 row->pixel_width,
12232 row->height,
12233 row->visible_height,
12234 row->ascent,
12235 row->phys_ascent);
12236 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12237 row->end.overlay_string_index,
12238 row->continuation_lines_width);
12239 fprintf (stderr, "%9d %5d\n",
12240 CHARPOS (row->start.string_pos),
12241 CHARPOS (row->end.string_pos));
12242 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12243 row->end.dpvec_index);
12244 }
12245
12246 if (glyphs > 1)
12247 {
12248 int area;
12249
12250 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12251 {
12252 struct glyph *glyph = row->glyphs[area];
12253 struct glyph *glyph_end = glyph + row->used[area];
12254
12255 /* Glyph for a line end in text. */
12256 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12257 ++glyph_end;
12258
12259 if (glyph < glyph_end)
12260 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12261
12262 for (; glyph < glyph_end; ++glyph)
12263 dump_glyph (row, glyph, area);
12264 }
12265 }
12266 else if (glyphs == 1)
12267 {
12268 int area;
12269
12270 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12271 {
12272 char *s = (char *) alloca (row->used[area] + 1);
12273 int i;
12274
12275 for (i = 0; i < row->used[area]; ++i)
12276 {
12277 struct glyph *glyph = row->glyphs[area] + i;
12278 if (glyph->type == CHAR_GLYPH
12279 && glyph->u.ch < 0x80
12280 && glyph->u.ch >= ' ')
12281 s[i] = glyph->u.ch;
12282 else
12283 s[i] = '.';
12284 }
12285
12286 s[i] = '\0';
12287 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12288 }
12289 }
12290 }
12291
12292
12293 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12294 Sdump_glyph_matrix, 0, 1, "p",
12295 doc: /* Dump the current matrix of the selected window to stderr.
12296 Shows contents of glyph row structures. With non-nil
12297 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12298 glyphs in short form, otherwise show glyphs in long form. */)
12299 (glyphs)
12300 Lisp_Object glyphs;
12301 {
12302 struct window *w = XWINDOW (selected_window);
12303 struct buffer *buffer = XBUFFER (w->buffer);
12304
12305 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12306 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12307 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12308 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12309 fprintf (stderr, "=============================================\n");
12310 dump_glyph_matrix (w->current_matrix,
12311 NILP (glyphs) ? 0 : XINT (glyphs));
12312 return Qnil;
12313 }
12314
12315
12316 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12317 doc: /* Dump glyph row ROW to stderr.
12318 GLYPH 0 means don't dump glyphs.
12319 GLYPH 1 means dump glyphs in short form.
12320 GLYPH > 1 or omitted means dump glyphs in long form. */)
12321 (row, glyphs)
12322 Lisp_Object row, glyphs;
12323 {
12324 struct glyph_matrix *matrix;
12325 int vpos;
12326
12327 CHECK_NUMBER (row);
12328 matrix = XWINDOW (selected_window)->current_matrix;
12329 vpos = XINT (row);
12330 if (vpos >= 0 && vpos < matrix->nrows)
12331 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12332 vpos,
12333 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12334 return Qnil;
12335 }
12336
12337
12338 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12339 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12340 GLYPH 0 means don't dump glyphs.
12341 GLYPH 1 means dump glyphs in short form.
12342 GLYPH > 1 or omitted means dump glyphs in long form. */)
12343 (row, glyphs)
12344 Lisp_Object row, glyphs;
12345 {
12346 struct frame *sf = SELECTED_FRAME ();
12347 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12348 int vpos;
12349
12350 CHECK_NUMBER (row);
12351 vpos = XINT (row);
12352 if (vpos >= 0 && vpos < m->nrows)
12353 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12354 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12355 return Qnil;
12356 }
12357
12358
12359 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12360 doc: /* Toggle tracing of redisplay.
12361 With ARG, turn tracing on if and only if ARG is positive. */)
12362 (arg)
12363 Lisp_Object arg;
12364 {
12365 if (NILP (arg))
12366 trace_redisplay_p = !trace_redisplay_p;
12367 else
12368 {
12369 arg = Fprefix_numeric_value (arg);
12370 trace_redisplay_p = XINT (arg) > 0;
12371 }
12372
12373 return Qnil;
12374 }
12375
12376
12377 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12378 doc: /* Like `format', but print result to stderr. */)
12379 (nargs, args)
12380 int nargs;
12381 Lisp_Object *args;
12382 {
12383 Lisp_Object s = Fformat (nargs, args);
12384 fprintf (stderr, "%s", XSTRING (s)->data);
12385 return Qnil;
12386 }
12387
12388 #endif /* GLYPH_DEBUG */
12389
12390
12391 \f
12392 /***********************************************************************
12393 Building Desired Matrix Rows
12394 ***********************************************************************/
12395
12396 /* Return a temporary glyph row holding the glyphs of an overlay
12397 arrow. Only used for non-window-redisplay windows. */
12398
12399 static struct glyph_row *
12400 get_overlay_arrow_glyph_row (w)
12401 struct window *w;
12402 {
12403 struct frame *f = XFRAME (WINDOW_FRAME (w));
12404 struct buffer *buffer = XBUFFER (w->buffer);
12405 struct buffer *old = current_buffer;
12406 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
12407 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
12408 unsigned char *arrow_end = arrow_string + arrow_len;
12409 unsigned char *p;
12410 struct it it;
12411 int multibyte_p;
12412 int n_glyphs_before;
12413
12414 set_buffer_temp (buffer);
12415 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12416 it.glyph_row->used[TEXT_AREA] = 0;
12417 SET_TEXT_POS (it.position, 0, 0);
12418
12419 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12420 p = arrow_string;
12421 while (p < arrow_end)
12422 {
12423 Lisp_Object face, ilisp;
12424
12425 /* Get the next character. */
12426 if (multibyte_p)
12427 it.c = string_char_and_length (p, arrow_len, &it.len);
12428 else
12429 it.c = *p, it.len = 1;
12430 p += it.len;
12431
12432 /* Get its face. */
12433 ilisp = make_number (p - arrow_string);
12434 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12435 it.face_id = compute_char_face (f, it.c, face);
12436
12437 /* Compute its width, get its glyphs. */
12438 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12439 SET_TEXT_POS (it.position, -1, -1);
12440 PRODUCE_GLYPHS (&it);
12441
12442 /* If this character doesn't fit any more in the line, we have
12443 to remove some glyphs. */
12444 if (it.current_x > it.last_visible_x)
12445 {
12446 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12447 break;
12448 }
12449 }
12450
12451 set_buffer_temp (old);
12452 return it.glyph_row;
12453 }
12454
12455
12456 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12457 glyphs are only inserted for terminal frames since we can't really
12458 win with truncation glyphs when partially visible glyphs are
12459 involved. Which glyphs to insert is determined by
12460 produce_special_glyphs. */
12461
12462 static void
12463 insert_left_trunc_glyphs (it)
12464 struct it *it;
12465 {
12466 struct it truncate_it;
12467 struct glyph *from, *end, *to, *toend;
12468
12469 xassert (!FRAME_WINDOW_P (it->f));
12470
12471 /* Get the truncation glyphs. */
12472 truncate_it = *it;
12473 truncate_it.current_x = 0;
12474 truncate_it.face_id = DEFAULT_FACE_ID;
12475 truncate_it.glyph_row = &scratch_glyph_row;
12476 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12477 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12478 truncate_it.object = make_number (0);
12479 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12480
12481 /* Overwrite glyphs from IT with truncation glyphs. */
12482 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12483 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12484 to = it->glyph_row->glyphs[TEXT_AREA];
12485 toend = to + it->glyph_row->used[TEXT_AREA];
12486
12487 while (from < end)
12488 *to++ = *from++;
12489
12490 /* There may be padding glyphs left over. Overwrite them too. */
12491 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12492 {
12493 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12494 while (from < end)
12495 *to++ = *from++;
12496 }
12497
12498 if (to > toend)
12499 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12500 }
12501
12502
12503 /* Compute the pixel height and width of IT->glyph_row.
12504
12505 Most of the time, ascent and height of a display line will be equal
12506 to the max_ascent and max_height values of the display iterator
12507 structure. This is not the case if
12508
12509 1. We hit ZV without displaying anything. In this case, max_ascent
12510 and max_height will be zero.
12511
12512 2. We have some glyphs that don't contribute to the line height.
12513 (The glyph row flag contributes_to_line_height_p is for future
12514 pixmap extensions).
12515
12516 The first case is easily covered by using default values because in
12517 these cases, the line height does not really matter, except that it
12518 must not be zero. */
12519
12520 static void
12521 compute_line_metrics (it)
12522 struct it *it;
12523 {
12524 struct glyph_row *row = it->glyph_row;
12525 int area, i;
12526
12527 if (FRAME_WINDOW_P (it->f))
12528 {
12529 int i, min_y, max_y;
12530
12531 /* The line may consist of one space only, that was added to
12532 place the cursor on it. If so, the row's height hasn't been
12533 computed yet. */
12534 if (row->height == 0)
12535 {
12536 if (it->max_ascent + it->max_descent == 0)
12537 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12538 row->ascent = it->max_ascent;
12539 row->height = it->max_ascent + it->max_descent;
12540 row->phys_ascent = it->max_phys_ascent;
12541 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12542 }
12543
12544 /* Compute the width of this line. */
12545 row->pixel_width = row->x;
12546 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12547 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12548
12549 xassert (row->pixel_width >= 0);
12550 xassert (row->ascent >= 0 && row->height > 0);
12551
12552 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12553 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12554
12555 /* If first line's physical ascent is larger than its logical
12556 ascent, use the physical ascent, and make the row taller.
12557 This makes accented characters fully visible. */
12558 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12559 && row->phys_ascent > row->ascent)
12560 {
12561 row->height += row->phys_ascent - row->ascent;
12562 row->ascent = row->phys_ascent;
12563 }
12564
12565 /* Compute how much of the line is visible. */
12566 row->visible_height = row->height;
12567
12568 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12569 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12570
12571 if (row->y < min_y)
12572 row->visible_height -= min_y - row->y;
12573 if (row->y + row->height > max_y)
12574 row->visible_height -= row->y + row->height - max_y;
12575 }
12576 else
12577 {
12578 row->pixel_width = row->used[TEXT_AREA];
12579 if (row->continued_p)
12580 row->pixel_width -= it->continuation_pixel_width;
12581 else if (row->truncated_on_right_p)
12582 row->pixel_width -= it->truncation_pixel_width;
12583 row->ascent = row->phys_ascent = 0;
12584 row->height = row->phys_height = row->visible_height = 1;
12585 }
12586
12587 /* Compute a hash code for this row. */
12588 row->hash = 0;
12589 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12590 for (i = 0; i < row->used[area]; ++i)
12591 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12592 + row->glyphs[area][i].u.val
12593 + row->glyphs[area][i].face_id
12594 + row->glyphs[area][i].padding_p
12595 + (row->glyphs[area][i].type << 2));
12596
12597 it->max_ascent = it->max_descent = 0;
12598 it->max_phys_ascent = it->max_phys_descent = 0;
12599 }
12600
12601
12602 /* Append one space to the glyph row of iterator IT if doing a
12603 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12604 space have the default face, otherwise let it have the same face as
12605 IT->face_id. Value is non-zero if a space was added.
12606
12607 This function is called to make sure that there is always one glyph
12608 at the end of a glyph row that the cursor can be set on under
12609 window-systems. (If there weren't such a glyph we would not know
12610 how wide and tall a box cursor should be displayed).
12611
12612 At the same time this space let's a nicely handle clearing to the
12613 end of the line if the row ends in italic text. */
12614
12615 static int
12616 append_space (it, default_face_p)
12617 struct it *it;
12618 int default_face_p;
12619 {
12620 if (FRAME_WINDOW_P (it->f))
12621 {
12622 int n = it->glyph_row->used[TEXT_AREA];
12623
12624 if (it->glyph_row->glyphs[TEXT_AREA] + n
12625 < it->glyph_row->glyphs[1 + TEXT_AREA])
12626 {
12627 /* Save some values that must not be changed.
12628 Must save IT->c and IT->len because otherwise
12629 ITERATOR_AT_END_P wouldn't work anymore after
12630 append_space has been called. */
12631 enum display_element_type saved_what = it->what;
12632 int saved_c = it->c, saved_len = it->len;
12633 int saved_x = it->current_x;
12634 int saved_face_id = it->face_id;
12635 struct text_pos saved_pos;
12636 Lisp_Object saved_object;
12637 struct face *face;
12638
12639 saved_object = it->object;
12640 saved_pos = it->position;
12641
12642 it->what = IT_CHARACTER;
12643 bzero (&it->position, sizeof it->position);
12644 it->object = make_number (0);
12645 it->c = ' ';
12646 it->len = 1;
12647
12648 if (default_face_p)
12649 it->face_id = DEFAULT_FACE_ID;
12650 else if (it->face_before_selective_p)
12651 it->face_id = it->saved_face_id;
12652 face = FACE_FROM_ID (it->f, it->face_id);
12653 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12654
12655 PRODUCE_GLYPHS (it);
12656
12657 it->current_x = saved_x;
12658 it->object = saved_object;
12659 it->position = saved_pos;
12660 it->what = saved_what;
12661 it->face_id = saved_face_id;
12662 it->len = saved_len;
12663 it->c = saved_c;
12664 return 1;
12665 }
12666 }
12667
12668 return 0;
12669 }
12670
12671
12672 /* Extend the face of the last glyph in the text area of IT->glyph_row
12673 to the end of the display line. Called from display_line.
12674 If the glyph row is empty, add a space glyph to it so that we
12675 know the face to draw. Set the glyph row flag fill_line_p. */
12676
12677 static void
12678 extend_face_to_end_of_line (it)
12679 struct it *it;
12680 {
12681 struct face *face;
12682 struct frame *f = it->f;
12683
12684 /* If line is already filled, do nothing. */
12685 if (it->current_x >= it->last_visible_x)
12686 return;
12687
12688 /* Face extension extends the background and box of IT->face_id
12689 to the end of the line. If the background equals the background
12690 of the frame, we don't have to do anything. */
12691 if (it->face_before_selective_p)
12692 face = FACE_FROM_ID (it->f, it->saved_face_id);
12693 else
12694 face = FACE_FROM_ID (f, it->face_id);
12695
12696 if (FRAME_WINDOW_P (f)
12697 && face->box == FACE_NO_BOX
12698 && face->background == FRAME_BACKGROUND_PIXEL (f)
12699 && !face->stipple)
12700 return;
12701
12702 /* Set the glyph row flag indicating that the face of the last glyph
12703 in the text area has to be drawn to the end of the text area. */
12704 it->glyph_row->fill_line_p = 1;
12705
12706 /* If current character of IT is not ASCII, make sure we have the
12707 ASCII face. This will be automatically undone the next time
12708 get_next_display_element returns a multibyte character. Note
12709 that the character will always be single byte in unibyte text. */
12710 if (!SINGLE_BYTE_CHAR_P (it->c))
12711 {
12712 it->face_id = FACE_FOR_CHAR (f, face, 0);
12713 }
12714
12715 if (FRAME_WINDOW_P (f))
12716 {
12717 /* If the row is empty, add a space with the current face of IT,
12718 so that we know which face to draw. */
12719 if (it->glyph_row->used[TEXT_AREA] == 0)
12720 {
12721 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12722 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12723 it->glyph_row->used[TEXT_AREA] = 1;
12724 }
12725 }
12726 else
12727 {
12728 /* Save some values that must not be changed. */
12729 int saved_x = it->current_x;
12730 struct text_pos saved_pos;
12731 Lisp_Object saved_object;
12732 enum display_element_type saved_what = it->what;
12733 int saved_face_id = it->face_id;
12734
12735 saved_object = it->object;
12736 saved_pos = it->position;
12737
12738 it->what = IT_CHARACTER;
12739 bzero (&it->position, sizeof it->position);
12740 it->object = make_number (0);
12741 it->c = ' ';
12742 it->len = 1;
12743 it->face_id = face->id;
12744
12745 PRODUCE_GLYPHS (it);
12746
12747 while (it->current_x <= it->last_visible_x)
12748 PRODUCE_GLYPHS (it);
12749
12750 /* Don't count these blanks really. It would let us insert a left
12751 truncation glyph below and make us set the cursor on them, maybe. */
12752 it->current_x = saved_x;
12753 it->object = saved_object;
12754 it->position = saved_pos;
12755 it->what = saved_what;
12756 it->face_id = saved_face_id;
12757 }
12758 }
12759
12760
12761 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12762 trailing whitespace. */
12763
12764 static int
12765 trailing_whitespace_p (charpos)
12766 int charpos;
12767 {
12768 int bytepos = CHAR_TO_BYTE (charpos);
12769 int c = 0;
12770
12771 while (bytepos < ZV_BYTE
12772 && (c = FETCH_CHAR (bytepos),
12773 c == ' ' || c == '\t'))
12774 ++bytepos;
12775
12776 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12777 {
12778 if (bytepos != PT_BYTE)
12779 return 1;
12780 }
12781 return 0;
12782 }
12783
12784
12785 /* Highlight trailing whitespace, if any, in ROW. */
12786
12787 void
12788 highlight_trailing_whitespace (f, row)
12789 struct frame *f;
12790 struct glyph_row *row;
12791 {
12792 int used = row->used[TEXT_AREA];
12793
12794 if (used)
12795 {
12796 struct glyph *start = row->glyphs[TEXT_AREA];
12797 struct glyph *glyph = start + used - 1;
12798
12799 /* Skip over glyphs inserted to display the cursor at the
12800 end of a line, for extending the face of the last glyph
12801 to the end of the line on terminals, and for truncation
12802 and continuation glyphs. */
12803 while (glyph >= start
12804 && glyph->type == CHAR_GLYPH
12805 && INTEGERP (glyph->object))
12806 --glyph;
12807
12808 /* If last glyph is a space or stretch, and it's trailing
12809 whitespace, set the face of all trailing whitespace glyphs in
12810 IT->glyph_row to `trailing-whitespace'. */
12811 if (glyph >= start
12812 && BUFFERP (glyph->object)
12813 && (glyph->type == STRETCH_GLYPH
12814 || (glyph->type == CHAR_GLYPH
12815 && glyph->u.ch == ' '))
12816 && trailing_whitespace_p (glyph->charpos))
12817 {
12818 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12819
12820 while (glyph >= start
12821 && BUFFERP (glyph->object)
12822 && (glyph->type == STRETCH_GLYPH
12823 || (glyph->type == CHAR_GLYPH
12824 && glyph->u.ch == ' ')))
12825 (glyph--)->face_id = face_id;
12826 }
12827 }
12828 }
12829
12830
12831 /* Value is non-zero if glyph row ROW in window W should be
12832 used to hold the cursor. */
12833
12834 static int
12835 cursor_row_p (w, row)
12836 struct window *w;
12837 struct glyph_row *row;
12838 {
12839 int cursor_row_p = 1;
12840
12841 if (PT == MATRIX_ROW_END_CHARPOS (row))
12842 {
12843 /* If the row ends with a newline from a string, we don't want
12844 the cursor there (if the row is continued it doesn't end in a
12845 newline). */
12846 if (CHARPOS (row->end.string_pos) >= 0
12847 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12848 cursor_row_p = row->continued_p;
12849
12850 /* If the row ends at ZV, display the cursor at the end of that
12851 row instead of at the start of the row below. */
12852 else if (row->ends_at_zv_p)
12853 cursor_row_p = 1;
12854 else
12855 cursor_row_p = 0;
12856 }
12857
12858 return cursor_row_p;
12859 }
12860
12861
12862 /* Construct the glyph row IT->glyph_row in the desired matrix of
12863 IT->w from text at the current position of IT. See dispextern.h
12864 for an overview of struct it. Value is non-zero if
12865 IT->glyph_row displays text, as opposed to a line displaying ZV
12866 only. */
12867
12868 static int
12869 display_line (it)
12870 struct it *it;
12871 {
12872 struct glyph_row *row = it->glyph_row;
12873
12874 /* We always start displaying at hpos zero even if hscrolled. */
12875 xassert (it->hpos == 0 && it->current_x == 0);
12876
12877 /* We must not display in a row that's not a text row. */
12878 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12879 < it->w->desired_matrix->nrows);
12880
12881 /* Is IT->w showing the region? */
12882 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12883
12884 /* Clear the result glyph row and enable it. */
12885 prepare_desired_row (row);
12886
12887 row->y = it->current_y;
12888 row->start = it->current;
12889 row->continuation_lines_width = it->continuation_lines_width;
12890 row->displays_text_p = 1;
12891 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12892 it->starts_in_middle_of_char_p = 0;
12893
12894 /* Arrange the overlays nicely for our purposes. Usually, we call
12895 display_line on only one line at a time, in which case this
12896 can't really hurt too much, or we call it on lines which appear
12897 one after another in the buffer, in which case all calls to
12898 recenter_overlay_lists but the first will be pretty cheap. */
12899 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12900
12901 /* Move over display elements that are not visible because we are
12902 hscrolled. This may stop at an x-position < IT->first_visible_x
12903 if the first glyph is partially visible or if we hit a line end. */
12904 if (it->current_x < it->first_visible_x)
12905 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12906 MOVE_TO_POS | MOVE_TO_X);
12907
12908 /* Get the initial row height. This is either the height of the
12909 text hscrolled, if there is any, or zero. */
12910 row->ascent = it->max_ascent;
12911 row->height = it->max_ascent + it->max_descent;
12912 row->phys_ascent = it->max_phys_ascent;
12913 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12914
12915 /* Loop generating characters. The loop is left with IT on the next
12916 character to display. */
12917 while (1)
12918 {
12919 int n_glyphs_before, hpos_before, x_before;
12920 int x, i, nglyphs;
12921 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12922
12923 /* Retrieve the next thing to display. Value is zero if end of
12924 buffer reached. */
12925 if (!get_next_display_element (it))
12926 {
12927 /* Maybe add a space at the end of this line that is used to
12928 display the cursor there under X. Set the charpos of the
12929 first glyph of blank lines not corresponding to any text
12930 to -1. */
12931 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12932 || row->used[TEXT_AREA] == 0)
12933 {
12934 row->glyphs[TEXT_AREA]->charpos = -1;
12935 row->displays_text_p = 0;
12936
12937 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
12938 && (!MINI_WINDOW_P (it->w)
12939 || (minibuf_level && EQ (it->window, minibuf_window))))
12940 row->indicate_empty_line_p = 1;
12941 }
12942
12943 it->continuation_lines_width = 0;
12944 row->ends_at_zv_p = 1;
12945 break;
12946 }
12947
12948 /* Now, get the metrics of what we want to display. This also
12949 generates glyphs in `row' (which is IT->glyph_row). */
12950 n_glyphs_before = row->used[TEXT_AREA];
12951 x = it->current_x;
12952
12953 /* Remember the line height so far in case the next element doesn't
12954 fit on the line. */
12955 if (!it->truncate_lines_p)
12956 {
12957 ascent = it->max_ascent;
12958 descent = it->max_descent;
12959 phys_ascent = it->max_phys_ascent;
12960 phys_descent = it->max_phys_descent;
12961 }
12962
12963 PRODUCE_GLYPHS (it);
12964
12965 /* If this display element was in marginal areas, continue with
12966 the next one. */
12967 if (it->area != TEXT_AREA)
12968 {
12969 row->ascent = max (row->ascent, it->max_ascent);
12970 row->height = max (row->height, it->max_ascent + it->max_descent);
12971 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12972 row->phys_height = max (row->phys_height,
12973 it->max_phys_ascent + it->max_phys_descent);
12974 set_iterator_to_next (it, 1);
12975 continue;
12976 }
12977
12978 /* Does the display element fit on the line? If we truncate
12979 lines, we should draw past the right edge of the window. If
12980 we don't truncate, we want to stop so that we can display the
12981 continuation glyph before the right margin. If lines are
12982 continued, there are two possible strategies for characters
12983 resulting in more than 1 glyph (e.g. tabs): Display as many
12984 glyphs as possible in this line and leave the rest for the
12985 continuation line, or display the whole element in the next
12986 line. Original redisplay did the former, so we do it also. */
12987 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12988 hpos_before = it->hpos;
12989 x_before = x;
12990
12991 if (/* Not a newline. */
12992 nglyphs > 0
12993 /* Glyphs produced fit entirely in the line. */
12994 && it->current_x < it->last_visible_x)
12995 {
12996 it->hpos += nglyphs;
12997 row->ascent = max (row->ascent, it->max_ascent);
12998 row->height = max (row->height, it->max_ascent + it->max_descent);
12999 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13000 row->phys_height = max (row->phys_height,
13001 it->max_phys_ascent + it->max_phys_descent);
13002 if (it->current_x - it->pixel_width < it->first_visible_x)
13003 row->x = x - it->first_visible_x;
13004 }
13005 else
13006 {
13007 int new_x;
13008 struct glyph *glyph;
13009
13010 for (i = 0; i < nglyphs; ++i, x = new_x)
13011 {
13012 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13013 new_x = x + glyph->pixel_width;
13014
13015 if (/* Lines are continued. */
13016 !it->truncate_lines_p
13017 && (/* Glyph doesn't fit on the line. */
13018 new_x > it->last_visible_x
13019 /* Or it fits exactly on a window system frame. */
13020 || (new_x == it->last_visible_x
13021 && FRAME_WINDOW_P (it->f))))
13022 {
13023 /* End of a continued line. */
13024
13025 if (it->hpos == 0
13026 || (new_x == it->last_visible_x
13027 && FRAME_WINDOW_P (it->f)))
13028 {
13029 /* Current glyph is the only one on the line or
13030 fits exactly on the line. We must continue
13031 the line because we can't draw the cursor
13032 after the glyph. */
13033 row->continued_p = 1;
13034 it->current_x = new_x;
13035 it->continuation_lines_width += new_x;
13036 ++it->hpos;
13037 if (i == nglyphs - 1)
13038 set_iterator_to_next (it, 1);
13039 }
13040 else if (CHAR_GLYPH_PADDING_P (*glyph)
13041 && !FRAME_WINDOW_P (it->f))
13042 {
13043 /* A padding glyph that doesn't fit on this line.
13044 This means the whole character doesn't fit
13045 on the line. */
13046 row->used[TEXT_AREA] = n_glyphs_before;
13047
13048 /* Fill the rest of the row with continuation
13049 glyphs like in 20.x. */
13050 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13051 < row->glyphs[1 + TEXT_AREA])
13052 produce_special_glyphs (it, IT_CONTINUATION);
13053
13054 row->continued_p = 1;
13055 it->current_x = x_before;
13056 it->continuation_lines_width += x_before;
13057
13058 /* Restore the height to what it was before the
13059 element not fitting on the line. */
13060 it->max_ascent = ascent;
13061 it->max_descent = descent;
13062 it->max_phys_ascent = phys_ascent;
13063 it->max_phys_descent = phys_descent;
13064 }
13065 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13066 {
13067 /* A TAB that extends past the right edge of the
13068 window. This produces a single glyph on
13069 window system frames. We leave the glyph in
13070 this row and let it fill the row, but don't
13071 consume the TAB. */
13072 it->continuation_lines_width += it->last_visible_x;
13073 row->ends_in_middle_of_char_p = 1;
13074 row->continued_p = 1;
13075 glyph->pixel_width = it->last_visible_x - x;
13076 it->starts_in_middle_of_char_p = 1;
13077 }
13078 else
13079 {
13080 /* Something other than a TAB that draws past
13081 the right edge of the window. Restore
13082 positions to values before the element. */
13083 row->used[TEXT_AREA] = n_glyphs_before + i;
13084
13085 /* Display continuation glyphs. */
13086 if (!FRAME_WINDOW_P (it->f))
13087 produce_special_glyphs (it, IT_CONTINUATION);
13088 row->continued_p = 1;
13089
13090 it->continuation_lines_width += x;
13091
13092 if (nglyphs > 1 && i > 0)
13093 {
13094 row->ends_in_middle_of_char_p = 1;
13095 it->starts_in_middle_of_char_p = 1;
13096 }
13097
13098 /* Restore the height to what it was before the
13099 element not fitting on the line. */
13100 it->max_ascent = ascent;
13101 it->max_descent = descent;
13102 it->max_phys_ascent = phys_ascent;
13103 it->max_phys_descent = phys_descent;
13104 }
13105
13106 break;
13107 }
13108 else if (new_x > it->first_visible_x)
13109 {
13110 /* Increment number of glyphs actually displayed. */
13111 ++it->hpos;
13112
13113 if (x < it->first_visible_x)
13114 /* Glyph is partially visible, i.e. row starts at
13115 negative X position. */
13116 row->x = x - it->first_visible_x;
13117 }
13118 else
13119 {
13120 /* Glyph is completely off the left margin of the
13121 window. This should not happen because of the
13122 move_it_in_display_line at the start of
13123 this function. */
13124 abort ();
13125 }
13126 }
13127
13128 row->ascent = max (row->ascent, it->max_ascent);
13129 row->height = max (row->height, it->max_ascent + it->max_descent);
13130 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13131 row->phys_height = max (row->phys_height,
13132 it->max_phys_ascent + it->max_phys_descent);
13133
13134 /* End of this display line if row is continued. */
13135 if (row->continued_p)
13136 break;
13137 }
13138
13139 /* Is this a line end? If yes, we're also done, after making
13140 sure that a non-default face is extended up to the right
13141 margin of the window. */
13142 if (ITERATOR_AT_END_OF_LINE_P (it))
13143 {
13144 int used_before = row->used[TEXT_AREA];
13145
13146 row->ends_in_newline_from_string_p = STRINGP (it->object);
13147
13148 /* Add a space at the end of the line that is used to
13149 display the cursor there. */
13150 append_space (it, 0);
13151
13152 /* Extend the face to the end of the line. */
13153 extend_face_to_end_of_line (it);
13154
13155 /* Make sure we have the position. */
13156 if (used_before == 0)
13157 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13158
13159 /* Consume the line end. This skips over invisible lines. */
13160 set_iterator_to_next (it, 1);
13161 it->continuation_lines_width = 0;
13162 break;
13163 }
13164
13165 /* Proceed with next display element. Note that this skips
13166 over lines invisible because of selective display. */
13167 set_iterator_to_next (it, 1);
13168
13169 /* If we truncate lines, we are done when the last displayed
13170 glyphs reach past the right margin of the window. */
13171 if (it->truncate_lines_p
13172 && (FRAME_WINDOW_P (it->f)
13173 ? (it->current_x >= it->last_visible_x)
13174 : (it->current_x > it->last_visible_x)))
13175 {
13176 /* Maybe add truncation glyphs. */
13177 if (!FRAME_WINDOW_P (it->f))
13178 {
13179 int i, n;
13180
13181 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13182 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13183 break;
13184
13185 for (n = row->used[TEXT_AREA]; i < n; ++i)
13186 {
13187 row->used[TEXT_AREA] = i;
13188 produce_special_glyphs (it, IT_TRUNCATION);
13189 }
13190 }
13191
13192 row->truncated_on_right_p = 1;
13193 it->continuation_lines_width = 0;
13194 reseat_at_next_visible_line_start (it, 0);
13195 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13196 it->hpos = hpos_before;
13197 it->current_x = x_before;
13198 break;
13199 }
13200 }
13201
13202 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13203 at the left window margin. */
13204 if (it->first_visible_x
13205 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13206 {
13207 if (!FRAME_WINDOW_P (it->f))
13208 insert_left_trunc_glyphs (it);
13209 row->truncated_on_left_p = 1;
13210 }
13211
13212 /* If the start of this line is the overlay arrow-position, then
13213 mark this glyph row as the one containing the overlay arrow.
13214 This is clearly a mess with variable size fonts. It would be
13215 better to let it be displayed like cursors under X. */
13216 if (MARKERP (Voverlay_arrow_position)
13217 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13218 && (MATRIX_ROW_START_CHARPOS (row)
13219 == marker_position (Voverlay_arrow_position))
13220 && STRINGP (Voverlay_arrow_string)
13221 && ! overlay_arrow_seen)
13222 {
13223 /* Overlay arrow in window redisplay is a fringe bitmap. */
13224 if (!FRAME_WINDOW_P (it->f))
13225 {
13226 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13227 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13228 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13229 struct glyph *p = row->glyphs[TEXT_AREA];
13230 struct glyph *p2, *end;
13231
13232 /* Copy the arrow glyphs. */
13233 while (glyph < arrow_end)
13234 *p++ = *glyph++;
13235
13236 /* Throw away padding glyphs. */
13237 p2 = p;
13238 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13239 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13240 ++p2;
13241 if (p2 > p)
13242 {
13243 while (p2 < end)
13244 *p++ = *p2++;
13245 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13246 }
13247 }
13248
13249 overlay_arrow_seen = 1;
13250 row->overlay_arrow_p = 1;
13251 }
13252
13253 /* Compute pixel dimensions of this line. */
13254 compute_line_metrics (it);
13255
13256 /* Remember the position at which this line ends. */
13257 row->end = it->current;
13258
13259 /* Maybe set the cursor. */
13260 if (it->w->cursor.vpos < 0
13261 && PT >= MATRIX_ROW_START_CHARPOS (row)
13262 && PT <= MATRIX_ROW_END_CHARPOS (row)
13263 && cursor_row_p (it->w, row))
13264 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13265
13266 /* Highlight trailing whitespace. */
13267 if (!NILP (Vshow_trailing_whitespace))
13268 highlight_trailing_whitespace (it->f, it->glyph_row);
13269
13270 /* Prepare for the next line. This line starts horizontally at (X
13271 HPOS) = (0 0). Vertical positions are incremented. As a
13272 convenience for the caller, IT->glyph_row is set to the next
13273 row to be used. */
13274 it->current_x = it->hpos = 0;
13275 it->current_y += row->height;
13276 ++it->vpos;
13277 ++it->glyph_row;
13278 return row->displays_text_p;
13279 }
13280
13281
13282 \f
13283 /***********************************************************************
13284 Menu Bar
13285 ***********************************************************************/
13286
13287 /* Redisplay the menu bar in the frame for window W.
13288
13289 The menu bar of X frames that don't have X toolkit support is
13290 displayed in a special window W->frame->menu_bar_window.
13291
13292 The menu bar of terminal frames is treated specially as far as
13293 glyph matrices are concerned. Menu bar lines are not part of
13294 windows, so the update is done directly on the frame matrix rows
13295 for the menu bar. */
13296
13297 static void
13298 display_menu_bar (w)
13299 struct window *w;
13300 {
13301 struct frame *f = XFRAME (WINDOW_FRAME (w));
13302 struct it it;
13303 Lisp_Object items;
13304 int i;
13305
13306 /* Don't do all this for graphical frames. */
13307 #ifdef HAVE_NTGUI
13308 if (!NILP (Vwindow_system))
13309 return;
13310 #endif
13311 #ifdef USE_X_TOOLKIT
13312 if (FRAME_X_P (f))
13313 return;
13314 #endif
13315 #ifdef macintosh
13316 if (FRAME_MAC_P (f))
13317 return;
13318 #endif
13319
13320 #ifdef USE_X_TOOLKIT
13321 xassert (!FRAME_WINDOW_P (f));
13322 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13323 it.first_visible_x = 0;
13324 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13325 #else /* not USE_X_TOOLKIT */
13326 if (FRAME_WINDOW_P (f))
13327 {
13328 /* Menu bar lines are displayed in the desired matrix of the
13329 dummy window menu_bar_window. */
13330 struct window *menu_w;
13331 xassert (WINDOWP (f->menu_bar_window));
13332 menu_w = XWINDOW (f->menu_bar_window);
13333 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13334 MENU_FACE_ID);
13335 it.first_visible_x = 0;
13336 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13337 }
13338 else
13339 {
13340 /* This is a TTY frame, i.e. character hpos/vpos are used as
13341 pixel x/y. */
13342 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13343 MENU_FACE_ID);
13344 it.first_visible_x = 0;
13345 it.last_visible_x = FRAME_WIDTH (f);
13346 }
13347 #endif /* not USE_X_TOOLKIT */
13348
13349 if (! mode_line_inverse_video)
13350 /* Force the menu-bar to be displayed in the default face. */
13351 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13352
13353 /* Clear all rows of the menu bar. */
13354 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13355 {
13356 struct glyph_row *row = it.glyph_row + i;
13357 clear_glyph_row (row);
13358 row->enabled_p = 1;
13359 row->full_width_p = 1;
13360 }
13361
13362 /* Display all items of the menu bar. */
13363 items = FRAME_MENU_BAR_ITEMS (it.f);
13364 for (i = 0; i < XVECTOR (items)->size; i += 4)
13365 {
13366 Lisp_Object string;
13367
13368 /* Stop at nil string. */
13369 string = AREF (items, i + 1);
13370 if (NILP (string))
13371 break;
13372
13373 /* Remember where item was displayed. */
13374 AREF (items, i + 3) = make_number (it.hpos);
13375
13376 /* Display the item, pad with one space. */
13377 if (it.current_x < it.last_visible_x)
13378 display_string (NULL, string, Qnil, 0, 0, &it,
13379 XSTRING (string)->size + 1, 0, 0, -1);
13380 }
13381
13382 /* Fill out the line with spaces. */
13383 if (it.current_x < it.last_visible_x)
13384 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13385
13386 /* Compute the total height of the lines. */
13387 compute_line_metrics (&it);
13388 }
13389
13390
13391 \f
13392 /***********************************************************************
13393 Mode Line
13394 ***********************************************************************/
13395
13396 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13397 FORCE is non-zero, redisplay mode lines unconditionally.
13398 Otherwise, redisplay only mode lines that are garbaged. Value is
13399 the number of windows whose mode lines were redisplayed. */
13400
13401 static int
13402 redisplay_mode_lines (window, force)
13403 Lisp_Object window;
13404 int force;
13405 {
13406 int nwindows = 0;
13407
13408 while (!NILP (window))
13409 {
13410 struct window *w = XWINDOW (window);
13411
13412 if (WINDOWP (w->hchild))
13413 nwindows += redisplay_mode_lines (w->hchild, force);
13414 else if (WINDOWP (w->vchild))
13415 nwindows += redisplay_mode_lines (w->vchild, force);
13416 else if (force
13417 || FRAME_GARBAGED_P (XFRAME (w->frame))
13418 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13419 {
13420 struct text_pos lpoint;
13421 struct buffer *old = current_buffer;
13422
13423 /* Set the window's buffer for the mode line display. */
13424 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13425 set_buffer_internal_1 (XBUFFER (w->buffer));
13426
13427 /* Point refers normally to the selected window. For any
13428 other window, set up appropriate value. */
13429 if (!EQ (window, selected_window))
13430 {
13431 struct text_pos pt;
13432
13433 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13434 if (CHARPOS (pt) < BEGV)
13435 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13436 else if (CHARPOS (pt) > (ZV - 1))
13437 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13438 else
13439 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13440 }
13441
13442 /* Display mode lines. */
13443 clear_glyph_matrix (w->desired_matrix);
13444 if (display_mode_lines (w))
13445 {
13446 ++nwindows;
13447 w->must_be_updated_p = 1;
13448 }
13449
13450 /* Restore old settings. */
13451 set_buffer_internal_1 (old);
13452 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13453 }
13454
13455 window = w->next;
13456 }
13457
13458 return nwindows;
13459 }
13460
13461
13462 /* Display the mode and/or top line of window W. Value is the number
13463 of mode lines displayed. */
13464
13465 static int
13466 display_mode_lines (w)
13467 struct window *w;
13468 {
13469 Lisp_Object old_selected_window, old_selected_frame;
13470 int n = 0;
13471
13472 old_selected_frame = selected_frame;
13473 selected_frame = w->frame;
13474 old_selected_window = selected_window;
13475 XSETWINDOW (selected_window, w);
13476
13477 /* These will be set while the mode line specs are processed. */
13478 line_number_displayed = 0;
13479 w->column_number_displayed = Qnil;
13480
13481 if (WINDOW_WANTS_MODELINE_P (w))
13482 {
13483 struct window *sel_w = XWINDOW (old_selected_window);
13484
13485 /* Select mode line face based on the real selected window. */
13486 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
13487 current_buffer->mode_line_format);
13488 ++n;
13489 }
13490
13491 if (WINDOW_WANTS_HEADER_LINE_P (w))
13492 {
13493 display_mode_line (w, HEADER_LINE_FACE_ID,
13494 current_buffer->header_line_format);
13495 ++n;
13496 }
13497
13498 selected_frame = old_selected_frame;
13499 selected_window = old_selected_window;
13500 return n;
13501 }
13502
13503
13504 /* Display mode or top line of window W. FACE_ID specifies which line
13505 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13506 FORMAT is the mode line format to display. Value is the pixel
13507 height of the mode line displayed. */
13508
13509 static int
13510 display_mode_line (w, face_id, format)
13511 struct window *w;
13512 enum face_id face_id;
13513 Lisp_Object format;
13514 {
13515 struct it it;
13516 struct face *face;
13517
13518 init_iterator (&it, w, -1, -1, NULL, face_id);
13519 prepare_desired_row (it.glyph_row);
13520
13521 if (! mode_line_inverse_video)
13522 /* Force the mode-line to be displayed in the default face. */
13523 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13524
13525 /* Temporarily make frame's keyboard the current kboard so that
13526 kboard-local variables in the mode_line_format will get the right
13527 values. */
13528 push_frame_kboard (it.f);
13529 display_mode_element (&it, 0, 0, 0, format, Qnil);
13530 pop_frame_kboard ();
13531
13532 /* Fill up with spaces. */
13533 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13534
13535 compute_line_metrics (&it);
13536 it.glyph_row->full_width_p = 1;
13537 it.glyph_row->mode_line_p = 1;
13538 it.glyph_row->continued_p = 0;
13539 it.glyph_row->truncated_on_left_p = 0;
13540 it.glyph_row->truncated_on_right_p = 0;
13541
13542 /* Make a 3D mode-line have a shadow at its right end. */
13543 face = FACE_FROM_ID (it.f, face_id);
13544 extend_face_to_end_of_line (&it);
13545 if (face->box != FACE_NO_BOX)
13546 {
13547 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13548 + it.glyph_row->used[TEXT_AREA] - 1);
13549 last->right_box_line_p = 1;
13550 }
13551
13552 return it.glyph_row->height;
13553 }
13554
13555 /* Alist that caches the results of :propertize.
13556 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
13557 Lisp_Object mode_line_proptrans_alist;
13558
13559 /* Contribute ELT to the mode line for window IT->w. How it
13560 translates into text depends on its data type.
13561
13562 IT describes the display environment in which we display, as usual.
13563
13564 DEPTH is the depth in recursion. It is used to prevent
13565 infinite recursion here.
13566
13567 FIELD_WIDTH is the number of characters the display of ELT should
13568 occupy in the mode line, and PRECISION is the maximum number of
13569 characters to display from ELT's representation. See
13570 display_string for details.
13571
13572 Returns the hpos of the end of the text generated by ELT. */
13573
13574 static int
13575 display_mode_element (it, depth, field_width, precision, elt, props)
13576 struct it *it;
13577 int depth;
13578 int field_width, precision;
13579 Lisp_Object elt, props;
13580 {
13581 int n = 0, field, prec;
13582 int literal = 0;
13583
13584 tail_recurse:
13585 if (depth > 10)
13586 goto invalid;
13587
13588 depth++;
13589
13590 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13591 {
13592 case Lisp_String:
13593 {
13594 /* A string: output it and check for %-constructs within it. */
13595 unsigned char c;
13596 unsigned char *this = XSTRING (elt)->data;
13597 unsigned char *lisp_string = this;
13598
13599 if (!NILP (props))
13600 {
13601 Lisp_Object oprops, aelt;
13602 oprops = Ftext_properties_at (make_number (0), elt);
13603 if (NILP (Fequal (props, oprops)))
13604 {
13605 aelt = Fassoc (elt, mode_line_proptrans_alist);
13606 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
13607 elt = XCAR (aelt);
13608 else
13609 {
13610 elt = Fcopy_sequence (elt);
13611 Fset_text_properties (0, Flength (elt), props, elt);
13612 mode_line_proptrans_alist
13613 = Fcons (Fcons (elt, props),
13614 mode_line_proptrans_alist);
13615 }
13616 }
13617 this = XSTRING (elt)->data;
13618 lisp_string = this;
13619 }
13620
13621 if (literal)
13622 {
13623 prec = precision - n;
13624 if (frame_title_ptr)
13625 n += store_frame_title (XSTRING (elt)->data, -1, prec);
13626 else
13627 n += display_string (NULL, elt, Qnil, 0, 0, it,
13628 0, prec, 0, STRING_MULTIBYTE (elt));
13629
13630 break;
13631 }
13632
13633 while ((precision <= 0 || n < precision)
13634 && *this
13635 && (frame_title_ptr
13636 || it->current_x < it->last_visible_x))
13637 {
13638 unsigned char *last = this;
13639
13640 /* Advance to end of string or next format specifier. */
13641 while ((c = *this++) != '\0' && c != '%')
13642 ;
13643
13644 if (this - 1 != last)
13645 {
13646 /* Output to end of string or up to '%'. Field width
13647 is length of string. Don't output more than
13648 PRECISION allows us. */
13649 --this;
13650
13651 prec = chars_in_text (last, this - last);
13652 if (precision > 0 && prec > precision - n)
13653 prec = precision - n;
13654
13655 if (frame_title_ptr)
13656 n += store_frame_title (last, 0, prec);
13657 else
13658 {
13659 int bytepos = last - lisp_string;
13660 int charpos = string_byte_to_char (elt, bytepos);
13661 n += display_string (NULL, elt, Qnil, 0, charpos,
13662 it, 0, prec, 0,
13663 STRING_MULTIBYTE (elt));
13664 }
13665 }
13666 else /* c == '%' */
13667 {
13668 unsigned char *percent_position = this;
13669
13670 /* Get the specified minimum width. Zero means
13671 don't pad. */
13672 field = 0;
13673 while ((c = *this++) >= '0' && c <= '9')
13674 field = field * 10 + c - '0';
13675
13676 /* Don't pad beyond the total padding allowed. */
13677 if (field_width - n > 0 && field > field_width - n)
13678 field = field_width - n;
13679
13680 /* Note that either PRECISION <= 0 or N < PRECISION. */
13681 prec = precision - n;
13682
13683 if (c == 'M')
13684 n += display_mode_element (it, depth, field, prec,
13685 Vglobal_mode_string, props);
13686 else if (c != 0)
13687 {
13688 int multibyte;
13689 unsigned char *spec
13690 = decode_mode_spec (it->w, c, field, prec, &multibyte);
13691
13692 if (frame_title_ptr)
13693 n += store_frame_title (spec, field, prec);
13694 else
13695 {
13696 int nglyphs_before, bytepos, charpos, nwritten;
13697
13698 nglyphs_before = it->glyph_row->used[TEXT_AREA];
13699 bytepos = percent_position - lisp_string;
13700 charpos = (STRING_MULTIBYTE (elt)
13701 ? string_byte_to_char (elt, bytepos)
13702 : bytepos);
13703 nwritten = display_string (spec, Qnil, elt,
13704 charpos, 0, it,
13705 field, prec, 0,
13706 multibyte);
13707
13708 /* Assign to the glyphs written above the
13709 string where the `%x' came from, position
13710 of the `%'. */
13711 if (nwritten > 0)
13712 {
13713 struct glyph *glyph
13714 = (it->glyph_row->glyphs[TEXT_AREA]
13715 + nglyphs_before);
13716 int i;
13717
13718 for (i = 0; i < nwritten; ++i)
13719 {
13720 glyph[i].object = elt;
13721 glyph[i].charpos = charpos;
13722 }
13723
13724 n += nwritten;
13725 }
13726 }
13727 }
13728 else /* c == 0 */
13729 break;
13730 }
13731 }
13732 }
13733 break;
13734
13735 case Lisp_Symbol:
13736 /* A symbol: process the value of the symbol recursively
13737 as if it appeared here directly. Avoid error if symbol void.
13738 Special case: if value of symbol is a string, output the string
13739 literally. */
13740 {
13741 register Lisp_Object tem;
13742 tem = Fboundp (elt);
13743 if (!NILP (tem))
13744 {
13745 tem = Fsymbol_value (elt);
13746 /* If value is a string, output that string literally:
13747 don't check for % within it. */
13748 if (STRINGP (tem))
13749 literal = 1;
13750
13751 if (!EQ (tem, elt))
13752 {
13753 /* Give up right away for nil or t. */
13754 elt = tem;
13755 goto tail_recurse;
13756 }
13757 }
13758 }
13759 break;
13760
13761 case Lisp_Cons:
13762 {
13763 register Lisp_Object car, tem;
13764
13765 /* A cons cell: five distinct cases.
13766 If first element is :eval or :propertize, do something special.
13767 If first element is a string or a cons, process all the elements
13768 and effectively concatenate them.
13769 If first element is a negative number, truncate displaying cdr to
13770 at most that many characters. If positive, pad (with spaces)
13771 to at least that many characters.
13772 If first element is a symbol, process the cadr or caddr recursively
13773 according to whether the symbol's value is non-nil or nil. */
13774 car = XCAR (elt);
13775 if (EQ (car, QCeval))
13776 {
13777 /* An element of the form (:eval FORM) means evaluate FORM
13778 and use the result as mode line elements. */
13779
13780 if (CONSP (XCDR (elt)))
13781 {
13782 Lisp_Object spec;
13783 spec = safe_eval (XCAR (XCDR (elt)));
13784 n += display_mode_element (it, depth, field_width - n,
13785 precision - n, spec, props);
13786 }
13787 }
13788 else if (EQ (car, QCpropertize))
13789 {
13790 if (CONSP (XCDR (elt)))
13791 {
13792 /* An element of the form (:propertize ELT PROPS...)
13793 means display ELT but applying properties PROPS. */
13794 n += display_mode_element (it, depth, field_width - n,
13795 precision - n, XCAR (XCDR (elt)),
13796 XCDR (XCDR (elt)));
13797 }
13798 }
13799 else if (SYMBOLP (car))
13800 {
13801 tem = Fboundp (car);
13802 elt = XCDR (elt);
13803 if (!CONSP (elt))
13804 goto invalid;
13805 /* elt is now the cdr, and we know it is a cons cell.
13806 Use its car if CAR has a non-nil value. */
13807 if (!NILP (tem))
13808 {
13809 tem = Fsymbol_value (car);
13810 if (!NILP (tem))
13811 {
13812 elt = XCAR (elt);
13813 goto tail_recurse;
13814 }
13815 }
13816 /* Symbol's value is nil (or symbol is unbound)
13817 Get the cddr of the original list
13818 and if possible find the caddr and use that. */
13819 elt = XCDR (elt);
13820 if (NILP (elt))
13821 break;
13822 else if (!CONSP (elt))
13823 goto invalid;
13824 elt = XCAR (elt);
13825 goto tail_recurse;
13826 }
13827 else if (INTEGERP (car))
13828 {
13829 register int lim = XINT (car);
13830 elt = XCDR (elt);
13831 if (lim < 0)
13832 {
13833 /* Negative int means reduce maximum width. */
13834 if (precision <= 0)
13835 precision = -lim;
13836 else
13837 precision = min (precision, -lim);
13838 }
13839 else if (lim > 0)
13840 {
13841 /* Padding specified. Don't let it be more than
13842 current maximum. */
13843 if (precision > 0)
13844 lim = min (precision, lim);
13845
13846 /* If that's more padding than already wanted, queue it.
13847 But don't reduce padding already specified even if
13848 that is beyond the current truncation point. */
13849 field_width = max (lim, field_width);
13850 }
13851 goto tail_recurse;
13852 }
13853 else if (STRINGP (car) || CONSP (car))
13854 {
13855 register int limit = 50;
13856 /* Limit is to protect against circular lists. */
13857 while (CONSP (elt)
13858 && --limit > 0
13859 && (precision <= 0 || n < precision))
13860 {
13861 n += display_mode_element (it, depth, field_width - n,
13862 precision - n, XCAR (elt), props);
13863 elt = XCDR (elt);
13864 }
13865 }
13866 }
13867 break;
13868
13869 default:
13870 invalid:
13871 if (frame_title_ptr)
13872 n += store_frame_title ("*invalid*", 0, precision - n);
13873 else
13874 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13875 precision - n, 0, 0);
13876 return n;
13877 }
13878
13879 /* Pad to FIELD_WIDTH. */
13880 if (field_width > 0 && n < field_width)
13881 {
13882 if (frame_title_ptr)
13883 n += store_frame_title ("", field_width - n, 0);
13884 else
13885 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13886 0, 0, 0);
13887 }
13888
13889 return n;
13890 }
13891
13892
13893 /* Write a null-terminated, right justified decimal representation of
13894 the positive integer D to BUF using a minimal field width WIDTH. */
13895
13896 static void
13897 pint2str (buf, width, d)
13898 register char *buf;
13899 register int width;
13900 register int d;
13901 {
13902 register char *p = buf;
13903
13904 if (d <= 0)
13905 *p++ = '0';
13906 else
13907 {
13908 while (d > 0)
13909 {
13910 *p++ = d % 10 + '0';
13911 d /= 10;
13912 }
13913 }
13914
13915 for (width -= (int) (p - buf); width > 0; --width)
13916 *p++ = ' ';
13917 *p-- = '\0';
13918 while (p > buf)
13919 {
13920 d = *buf;
13921 *buf++ = *p;
13922 *p-- = d;
13923 }
13924 }
13925
13926 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
13927 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13928 type of CODING_SYSTEM. Return updated pointer into BUF. */
13929
13930 static unsigned char invalid_eol_type[] = "(*invalid*)";
13931
13932 static char *
13933 decode_mode_spec_coding (coding_system, buf, eol_flag)
13934 Lisp_Object coding_system;
13935 register char *buf;
13936 int eol_flag;
13937 {
13938 Lisp_Object val;
13939 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
13940 unsigned char *eol_str;
13941 int eol_str_len;
13942 /* The EOL conversion we are using. */
13943 Lisp_Object eoltype;
13944
13945 val = CODING_SYSTEM_SPEC (coding_system);
13946 eoltype = Qnil;
13947
13948 if (!VECTORP (val)) /* Not yet decided. */
13949 {
13950 if (multibyte)
13951 *buf++ = '-';
13952 if (eol_flag)
13953 eoltype = eol_mnemonic_undecided;
13954 /* Don't mention EOL conversion if it isn't decided. */
13955 }
13956 else
13957 {
13958 Lisp_Object attrs;
13959 Lisp_Object eolvalue;
13960
13961 attrs = AREF (val, 0);
13962 eolvalue = AREF (val, 2);
13963
13964 if (multibyte)
13965 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
13966
13967 if (eol_flag)
13968 {
13969 /* The EOL conversion that is normal on this system. */
13970
13971 if (NILP (eolvalue)) /* Not yet decided. */
13972 eoltype = eol_mnemonic_undecided;
13973 else if (VECTORP (eolvalue)) /* Not yet decided. */
13974 eoltype = eol_mnemonic_undecided;
13975 else /* eolvalue is Qunix, Qdos, or Qmac. */
13976 eoltype = (EQ (eolvalue, Qunix)
13977 ? eol_mnemonic_unix
13978 : (EQ (eolvalue, Qdos) == 1
13979 ? eol_mnemonic_dos : eol_mnemonic_mac));
13980 }
13981 }
13982
13983 if (eol_flag)
13984 {
13985 /* Mention the EOL conversion if it is not the usual one. */
13986 if (STRINGP (eoltype))
13987 {
13988 eol_str = XSTRING (eoltype)->data;
13989 eol_str_len = XSTRING (eoltype)->size;
13990 }
13991 else if (INTEGERP (eoltype)
13992 && CHAR_VALID_P (XINT (eoltype), 0))
13993 {
13994 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
13995 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
13996 }
13997 else
13998 {
13999 eol_str = invalid_eol_type;
14000 eol_str_len = sizeof (invalid_eol_type) - 1;
14001 }
14002 bcopy (eol_str, buf, eol_str_len);
14003 buf += eol_str_len;
14004 }
14005
14006 return buf;
14007 }
14008
14009 /* Return a string for the output of a mode line %-spec for window W,
14010 generated by character C. PRECISION >= 0 means don't return a
14011 string longer than that value. FIELD_WIDTH > 0 means pad the
14012 string returned with spaces to that value. Return 1 in *MULTIBYTE
14013 if the result is multibyte text. */
14014
14015 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
14016
14017 static char *
14018 decode_mode_spec (w, c, field_width, precision, multibyte)
14019 struct window *w;
14020 register int c;
14021 int field_width, precision;
14022 int *multibyte;
14023 {
14024 Lisp_Object obj;
14025 struct frame *f = XFRAME (WINDOW_FRAME (w));
14026 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
14027 struct buffer *b = XBUFFER (w->buffer);
14028
14029 obj = Qnil;
14030 *multibyte = 0;
14031
14032 switch (c)
14033 {
14034 case '*':
14035 if (!NILP (b->read_only))
14036 return "%";
14037 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14038 return "*";
14039 return "-";
14040
14041 case '+':
14042 /* This differs from %* only for a modified read-only buffer. */
14043 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14044 return "*";
14045 if (!NILP (b->read_only))
14046 return "%";
14047 return "-";
14048
14049 case '&':
14050 /* This differs from %* in ignoring read-only-ness. */
14051 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14052 return "*";
14053 return "-";
14054
14055 case '%':
14056 return "%";
14057
14058 case '[':
14059 {
14060 int i;
14061 char *p;
14062
14063 if (command_loop_level > 5)
14064 return "[[[... ";
14065 p = decode_mode_spec_buf;
14066 for (i = 0; i < command_loop_level; i++)
14067 *p++ = '[';
14068 *p = 0;
14069 return decode_mode_spec_buf;
14070 }
14071
14072 case ']':
14073 {
14074 int i;
14075 char *p;
14076
14077 if (command_loop_level > 5)
14078 return " ...]]]";
14079 p = decode_mode_spec_buf;
14080 for (i = 0; i < command_loop_level; i++)
14081 *p++ = ']';
14082 *p = 0;
14083 return decode_mode_spec_buf;
14084 }
14085
14086 case '-':
14087 {
14088 register int i;
14089
14090 /* Let lots_of_dashes be a string of infinite length. */
14091 if (field_width <= 0
14092 || field_width > sizeof (lots_of_dashes))
14093 {
14094 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
14095 decode_mode_spec_buf[i] = '-';
14096 decode_mode_spec_buf[i] = '\0';
14097 return decode_mode_spec_buf;
14098 }
14099 else
14100 return lots_of_dashes;
14101 }
14102
14103 case 'b':
14104 obj = b->name;
14105 break;
14106
14107 case 'c':
14108 {
14109 int col = current_column ();
14110 w->column_number_displayed = make_number (col);
14111 pint2str (decode_mode_spec_buf, field_width, col);
14112 return decode_mode_spec_buf;
14113 }
14114
14115 case 'F':
14116 /* %F displays the frame name. */
14117 if (!NILP (f->title))
14118 return (char *) XSTRING (f->title)->data;
14119 if (f->explicit_name || ! FRAME_WINDOW_P (f))
14120 return (char *) XSTRING (f->name)->data;
14121 return "Emacs";
14122
14123 case 'f':
14124 obj = b->filename;
14125 break;
14126
14127 case 'l':
14128 {
14129 int startpos = XMARKER (w->start)->charpos;
14130 int startpos_byte = marker_byte_position (w->start);
14131 int line, linepos, linepos_byte, topline;
14132 int nlines, junk;
14133 int height = XFASTINT (w->height);
14134
14135 /* If we decided that this buffer isn't suitable for line numbers,
14136 don't forget that too fast. */
14137 if (EQ (w->base_line_pos, w->buffer))
14138 goto no_value;
14139 /* But do forget it, if the window shows a different buffer now. */
14140 else if (BUFFERP (w->base_line_pos))
14141 w->base_line_pos = Qnil;
14142
14143 /* If the buffer is very big, don't waste time. */
14144 if (INTEGERP (Vline_number_display_limit)
14145 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
14146 {
14147 w->base_line_pos = Qnil;
14148 w->base_line_number = Qnil;
14149 goto no_value;
14150 }
14151
14152 if (!NILP (w->base_line_number)
14153 && !NILP (w->base_line_pos)
14154 && XFASTINT (w->base_line_pos) <= startpos)
14155 {
14156 line = XFASTINT (w->base_line_number);
14157 linepos = XFASTINT (w->base_line_pos);
14158 linepos_byte = buf_charpos_to_bytepos (b, linepos);
14159 }
14160 else
14161 {
14162 line = 1;
14163 linepos = BUF_BEGV (b);
14164 linepos_byte = BUF_BEGV_BYTE (b);
14165 }
14166
14167 /* Count lines from base line to window start position. */
14168 nlines = display_count_lines (linepos, linepos_byte,
14169 startpos_byte,
14170 startpos, &junk);
14171
14172 topline = nlines + line;
14173
14174 /* Determine a new base line, if the old one is too close
14175 or too far away, or if we did not have one.
14176 "Too close" means it's plausible a scroll-down would
14177 go back past it. */
14178 if (startpos == BUF_BEGV (b))
14179 {
14180 w->base_line_number = make_number (topline);
14181 w->base_line_pos = make_number (BUF_BEGV (b));
14182 }
14183 else if (nlines < height + 25 || nlines > height * 3 + 50
14184 || linepos == BUF_BEGV (b))
14185 {
14186 int limit = BUF_BEGV (b);
14187 int limit_byte = BUF_BEGV_BYTE (b);
14188 int position;
14189 int distance = (height * 2 + 30) * line_number_display_limit_width;
14190
14191 if (startpos - distance > limit)
14192 {
14193 limit = startpos - distance;
14194 limit_byte = CHAR_TO_BYTE (limit);
14195 }
14196
14197 nlines = display_count_lines (startpos, startpos_byte,
14198 limit_byte,
14199 - (height * 2 + 30),
14200 &position);
14201 /* If we couldn't find the lines we wanted within
14202 line_number_display_limit_width chars per line,
14203 give up on line numbers for this window. */
14204 if (position == limit_byte && limit == startpos - distance)
14205 {
14206 w->base_line_pos = w->buffer;
14207 w->base_line_number = Qnil;
14208 goto no_value;
14209 }
14210
14211 w->base_line_number = make_number (topline - nlines);
14212 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
14213 }
14214
14215 /* Now count lines from the start pos to point. */
14216 nlines = display_count_lines (startpos, startpos_byte,
14217 PT_BYTE, PT, &junk);
14218
14219 /* Record that we did display the line number. */
14220 line_number_displayed = 1;
14221
14222 /* Make the string to show. */
14223 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
14224 return decode_mode_spec_buf;
14225 no_value:
14226 {
14227 char* p = decode_mode_spec_buf;
14228 int pad = field_width - 2;
14229 while (pad-- > 0)
14230 *p++ = ' ';
14231 *p++ = '?';
14232 *p++ = '?';
14233 *p = '\0';
14234 return decode_mode_spec_buf;
14235 }
14236 }
14237 break;
14238
14239 case 'm':
14240 obj = b->mode_name;
14241 break;
14242
14243 case 'n':
14244 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
14245 return " Narrow";
14246 break;
14247
14248 case 'p':
14249 {
14250 int pos = marker_position (w->start);
14251 int total = BUF_ZV (b) - BUF_BEGV (b);
14252
14253 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
14254 {
14255 if (pos <= BUF_BEGV (b))
14256 return "All";
14257 else
14258 return "Bottom";
14259 }
14260 else if (pos <= BUF_BEGV (b))
14261 return "Top";
14262 else
14263 {
14264 if (total > 1000000)
14265 /* Do it differently for a large value, to avoid overflow. */
14266 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14267 else
14268 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14269 /* We can't normally display a 3-digit number,
14270 so get us a 2-digit number that is close. */
14271 if (total == 100)
14272 total = 99;
14273 sprintf (decode_mode_spec_buf, "%2d%%", total);
14274 return decode_mode_spec_buf;
14275 }
14276 }
14277
14278 /* Display percentage of size above the bottom of the screen. */
14279 case 'P':
14280 {
14281 int toppos = marker_position (w->start);
14282 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14283 int total = BUF_ZV (b) - BUF_BEGV (b);
14284
14285 if (botpos >= BUF_ZV (b))
14286 {
14287 if (toppos <= BUF_BEGV (b))
14288 return "All";
14289 else
14290 return "Bottom";
14291 }
14292 else
14293 {
14294 if (total > 1000000)
14295 /* Do it differently for a large value, to avoid overflow. */
14296 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14297 else
14298 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14299 /* We can't normally display a 3-digit number,
14300 so get us a 2-digit number that is close. */
14301 if (total == 100)
14302 total = 99;
14303 if (toppos <= BUF_BEGV (b))
14304 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14305 else
14306 sprintf (decode_mode_spec_buf, "%2d%%", total);
14307 return decode_mode_spec_buf;
14308 }
14309 }
14310
14311 case 's':
14312 /* status of process */
14313 obj = Fget_buffer_process (w->buffer);
14314 if (NILP (obj))
14315 return "no process";
14316 #ifdef subprocesses
14317 obj = Fsymbol_name (Fprocess_status (obj));
14318 #endif
14319 break;
14320
14321 case 't': /* indicate TEXT or BINARY */
14322 #ifdef MODE_LINE_BINARY_TEXT
14323 return MODE_LINE_BINARY_TEXT (b);
14324 #else
14325 return "T";
14326 #endif
14327
14328 case 'z':
14329 /* coding-system (not including end-of-line format) */
14330 case 'Z':
14331 /* coding-system (including end-of-line type) */
14332 {
14333 int eol_flag = (c == 'Z');
14334 char *p = decode_mode_spec_buf;
14335
14336 if (! FRAME_WINDOW_P (f))
14337 {
14338 /* No need to mention EOL here--the terminal never needs
14339 to do EOL conversion. */
14340 p = decode_mode_spec_coding (CODING_ID_NAME (keyboard_coding.id),
14341 p, 0);
14342 p = decode_mode_spec_coding (CODING_ID_NAME (terminal_coding.id),
14343 p, 0);
14344 }
14345 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14346 p, eol_flag);
14347
14348 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14349 #ifdef subprocesses
14350 obj = Fget_buffer_process (Fcurrent_buffer ());
14351 if (PROCESSP (obj))
14352 {
14353 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14354 p, eol_flag);
14355 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14356 p, eol_flag);
14357 }
14358 #endif /* subprocesses */
14359 #endif /* 0 */
14360 *p = 0;
14361 return decode_mode_spec_buf;
14362 }
14363 }
14364
14365 if (STRINGP (obj))
14366 {
14367 *multibyte = STRING_MULTIBYTE (obj);
14368 return (char *) XSTRING (obj)->data;
14369 }
14370 else
14371 return "";
14372 }
14373
14374
14375 /* Count up to COUNT lines starting from START / START_BYTE.
14376 But don't go beyond LIMIT_BYTE.
14377 Return the number of lines thus found (always nonnegative).
14378
14379 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14380
14381 static int
14382 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14383 int start, start_byte, limit_byte, count;
14384 int *byte_pos_ptr;
14385 {
14386 register unsigned char *cursor;
14387 unsigned char *base;
14388
14389 register int ceiling;
14390 register unsigned char *ceiling_addr;
14391 int orig_count = count;
14392
14393 /* If we are not in selective display mode,
14394 check only for newlines. */
14395 int selective_display = (!NILP (current_buffer->selective_display)
14396 && !INTEGERP (current_buffer->selective_display));
14397
14398 if (count > 0)
14399 {
14400 while (start_byte < limit_byte)
14401 {
14402 ceiling = BUFFER_CEILING_OF (start_byte);
14403 ceiling = min (limit_byte - 1, ceiling);
14404 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14405 base = (cursor = BYTE_POS_ADDR (start_byte));
14406 while (1)
14407 {
14408 if (selective_display)
14409 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14410 ;
14411 else
14412 while (*cursor != '\n' && ++cursor != ceiling_addr)
14413 ;
14414
14415 if (cursor != ceiling_addr)
14416 {
14417 if (--count == 0)
14418 {
14419 start_byte += cursor - base + 1;
14420 *byte_pos_ptr = start_byte;
14421 return orig_count;
14422 }
14423 else
14424 if (++cursor == ceiling_addr)
14425 break;
14426 }
14427 else
14428 break;
14429 }
14430 start_byte += cursor - base;
14431 }
14432 }
14433 else
14434 {
14435 while (start_byte > limit_byte)
14436 {
14437 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14438 ceiling = max (limit_byte, ceiling);
14439 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14440 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14441 while (1)
14442 {
14443 if (selective_display)
14444 while (--cursor != ceiling_addr
14445 && *cursor != '\n' && *cursor != 015)
14446 ;
14447 else
14448 while (--cursor != ceiling_addr && *cursor != '\n')
14449 ;
14450
14451 if (cursor != ceiling_addr)
14452 {
14453 if (++count == 0)
14454 {
14455 start_byte += cursor - base + 1;
14456 *byte_pos_ptr = start_byte;
14457 /* When scanning backwards, we should
14458 not count the newline posterior to which we stop. */
14459 return - orig_count - 1;
14460 }
14461 }
14462 else
14463 break;
14464 }
14465 /* Here we add 1 to compensate for the last decrement
14466 of CURSOR, which took it past the valid range. */
14467 start_byte += cursor - base + 1;
14468 }
14469 }
14470
14471 *byte_pos_ptr = limit_byte;
14472
14473 if (count < 0)
14474 return - orig_count + count;
14475 return orig_count - count;
14476
14477 }
14478
14479
14480 \f
14481 /***********************************************************************
14482 Displaying strings
14483 ***********************************************************************/
14484
14485 /* Display a NUL-terminated string, starting with index START.
14486
14487 If STRING is non-null, display that C string. Otherwise, the Lisp
14488 string LISP_STRING is displayed.
14489
14490 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14491 FACE_STRING. Display STRING or LISP_STRING with the face at
14492 FACE_STRING_POS in FACE_STRING:
14493
14494 Display the string in the environment given by IT, but use the
14495 standard display table, temporarily.
14496
14497 FIELD_WIDTH is the minimum number of output glyphs to produce.
14498 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14499 with spaces. If STRING has more characters, more than FIELD_WIDTH
14500 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14501
14502 PRECISION is the maximum number of characters to output from
14503 STRING. PRECISION < 0 means don't truncate the string.
14504
14505 This is roughly equivalent to printf format specifiers:
14506
14507 FIELD_WIDTH PRECISION PRINTF
14508 ----------------------------------------
14509 -1 -1 %s
14510 -1 10 %.10s
14511 10 -1 %10s
14512 20 10 %20.10s
14513
14514 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14515 display them, and < 0 means obey the current buffer's value of
14516 enable_multibyte_characters.
14517
14518 Value is the number of glyphs produced. */
14519
14520 static int
14521 display_string (string, lisp_string, face_string, face_string_pos,
14522 start, it, field_width, precision, max_x, multibyte)
14523 unsigned char *string;
14524 Lisp_Object lisp_string;
14525 Lisp_Object face_string;
14526 int face_string_pos;
14527 int start;
14528 struct it *it;
14529 int field_width, precision, max_x;
14530 int multibyte;
14531 {
14532 int hpos_at_start = it->hpos;
14533 int saved_face_id = it->face_id;
14534 struct glyph_row *row = it->glyph_row;
14535
14536 /* Initialize the iterator IT for iteration over STRING beginning
14537 with index START. */
14538 reseat_to_string (it, string, lisp_string, start,
14539 precision, field_width, multibyte);
14540
14541 /* If displaying STRING, set up the face of the iterator
14542 from LISP_STRING, if that's given. */
14543 if (STRINGP (face_string))
14544 {
14545 int endptr;
14546 struct face *face;
14547
14548 it->face_id
14549 = face_at_string_position (it->w, face_string, face_string_pos,
14550 0, it->region_beg_charpos,
14551 it->region_end_charpos,
14552 &endptr, it->base_face_id, 0);
14553 face = FACE_FROM_ID (it->f, it->face_id);
14554 it->face_box_p = face->box != FACE_NO_BOX;
14555 }
14556
14557 /* Set max_x to the maximum allowed X position. Don't let it go
14558 beyond the right edge of the window. */
14559 if (max_x <= 0)
14560 max_x = it->last_visible_x;
14561 else
14562 max_x = min (max_x, it->last_visible_x);
14563
14564 /* Skip over display elements that are not visible. because IT->w is
14565 hscrolled. */
14566 if (it->current_x < it->first_visible_x)
14567 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14568 MOVE_TO_POS | MOVE_TO_X);
14569
14570 row->ascent = it->max_ascent;
14571 row->height = it->max_ascent + it->max_descent;
14572 row->phys_ascent = it->max_phys_ascent;
14573 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14574
14575 /* This condition is for the case that we are called with current_x
14576 past last_visible_x. */
14577 while (it->current_x < max_x)
14578 {
14579 int x_before, x, n_glyphs_before, i, nglyphs;
14580
14581 /* Get the next display element. */
14582 if (!get_next_display_element (it))
14583 break;
14584
14585 /* Produce glyphs. */
14586 x_before = it->current_x;
14587 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14588 PRODUCE_GLYPHS (it);
14589
14590 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14591 i = 0;
14592 x = x_before;
14593 while (i < nglyphs)
14594 {
14595 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14596
14597 if (!it->truncate_lines_p
14598 && x + glyph->pixel_width > max_x)
14599 {
14600 /* End of continued line or max_x reached. */
14601 if (CHAR_GLYPH_PADDING_P (*glyph))
14602 {
14603 /* A wide character is unbreakable. */
14604 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14605 it->current_x = x_before;
14606 }
14607 else
14608 {
14609 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14610 it->current_x = x;
14611 }
14612 break;
14613 }
14614 else if (x + glyph->pixel_width > it->first_visible_x)
14615 {
14616 /* Glyph is at least partially visible. */
14617 ++it->hpos;
14618 if (x < it->first_visible_x)
14619 it->glyph_row->x = x - it->first_visible_x;
14620 }
14621 else
14622 {
14623 /* Glyph is off the left margin of the display area.
14624 Should not happen. */
14625 abort ();
14626 }
14627
14628 row->ascent = max (row->ascent, it->max_ascent);
14629 row->height = max (row->height, it->max_ascent + it->max_descent);
14630 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14631 row->phys_height = max (row->phys_height,
14632 it->max_phys_ascent + it->max_phys_descent);
14633 x += glyph->pixel_width;
14634 ++i;
14635 }
14636
14637 /* Stop if max_x reached. */
14638 if (i < nglyphs)
14639 break;
14640
14641 /* Stop at line ends. */
14642 if (ITERATOR_AT_END_OF_LINE_P (it))
14643 {
14644 it->continuation_lines_width = 0;
14645 break;
14646 }
14647
14648 set_iterator_to_next (it, 1);
14649
14650 /* Stop if truncating at the right edge. */
14651 if (it->truncate_lines_p
14652 && it->current_x >= it->last_visible_x)
14653 {
14654 /* Add truncation mark, but don't do it if the line is
14655 truncated at a padding space. */
14656 if (IT_CHARPOS (*it) < it->string_nchars)
14657 {
14658 if (!FRAME_WINDOW_P (it->f))
14659 {
14660 int i, n;
14661
14662 if (it->current_x > it->last_visible_x)
14663 {
14664 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14665 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14666 break;
14667 for (n = row->used[TEXT_AREA]; i < n; ++i)
14668 {
14669 row->used[TEXT_AREA] = i;
14670 produce_special_glyphs (it, IT_TRUNCATION);
14671 }
14672 }
14673 produce_special_glyphs (it, IT_TRUNCATION);
14674 }
14675 it->glyph_row->truncated_on_right_p = 1;
14676 }
14677 break;
14678 }
14679 }
14680
14681 /* Maybe insert a truncation at the left. */
14682 if (it->first_visible_x
14683 && IT_CHARPOS (*it) > 0)
14684 {
14685 if (!FRAME_WINDOW_P (it->f))
14686 insert_left_trunc_glyphs (it);
14687 it->glyph_row->truncated_on_left_p = 1;
14688 }
14689
14690 it->face_id = saved_face_id;
14691
14692 /* Value is number of columns displayed. */
14693 return it->hpos - hpos_at_start;
14694 }
14695
14696
14697 \f
14698 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
14699 appears as an element of LIST or as the car of an element of LIST.
14700 If PROPVAL is a list, compare each element against LIST in that
14701 way, and return 1/2 if any element of PROPVAL is found in LIST.
14702 Otherwise return 0. This function cannot quit.
14703 The return value is 2 if the text is invisible but with an ellipsis
14704 and 1 if it's invisible and without an ellipsis. */
14705
14706 int
14707 invisible_p (propval, list)
14708 register Lisp_Object propval;
14709 Lisp_Object list;
14710 {
14711 register Lisp_Object tail, proptail;
14712
14713 for (tail = list; CONSP (tail); tail = XCDR (tail))
14714 {
14715 register Lisp_Object tem;
14716 tem = XCAR (tail);
14717 if (EQ (propval, tem))
14718 return 1;
14719 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14720 return NILP (XCDR (tem)) ? 1 : 2;
14721 }
14722
14723 if (CONSP (propval))
14724 {
14725 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14726 {
14727 Lisp_Object propelt;
14728 propelt = XCAR (proptail);
14729 for (tail = list; CONSP (tail); tail = XCDR (tail))
14730 {
14731 register Lisp_Object tem;
14732 tem = XCAR (tail);
14733 if (EQ (propelt, tem))
14734 return 1;
14735 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14736 return NILP (XCDR (tem)) ? 1 : 2;
14737 }
14738 }
14739 }
14740
14741 return 0;
14742 }
14743
14744 \f
14745 /***********************************************************************
14746 Initialization
14747 ***********************************************************************/
14748
14749 void
14750 syms_of_xdisp ()
14751 {
14752 Vwith_echo_area_save_vector = Qnil;
14753 staticpro (&Vwith_echo_area_save_vector);
14754
14755 Vmessage_stack = Qnil;
14756 staticpro (&Vmessage_stack);
14757
14758 Qinhibit_redisplay = intern ("inhibit-redisplay");
14759 staticpro (&Qinhibit_redisplay);
14760
14761 message_dolog_marker1 = Fmake_marker ();
14762 staticpro (&message_dolog_marker1);
14763 message_dolog_marker2 = Fmake_marker ();
14764 staticpro (&message_dolog_marker2);
14765 message_dolog_marker3 = Fmake_marker ();
14766 staticpro (&message_dolog_marker3);
14767
14768 #if GLYPH_DEBUG
14769 defsubr (&Sdump_glyph_matrix);
14770 defsubr (&Sdump_glyph_row);
14771 defsubr (&Sdump_tool_bar_row);
14772 defsubr (&Strace_redisplay);
14773 defsubr (&Strace_to_stderr);
14774 #endif
14775 #ifdef HAVE_WINDOW_SYSTEM
14776 defsubr (&Stool_bar_lines_needed);
14777 #endif
14778
14779 staticpro (&Qmenu_bar_update_hook);
14780 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14781
14782 staticpro (&Qoverriding_terminal_local_map);
14783 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14784
14785 staticpro (&Qoverriding_local_map);
14786 Qoverriding_local_map = intern ("overriding-local-map");
14787
14788 staticpro (&Qwindow_scroll_functions);
14789 Qwindow_scroll_functions = intern ("window-scroll-functions");
14790
14791 staticpro (&Qredisplay_end_trigger_functions);
14792 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14793
14794 staticpro (&Qinhibit_point_motion_hooks);
14795 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14796
14797 QCdata = intern (":data");
14798 staticpro (&QCdata);
14799 Qdisplay = intern ("display");
14800 staticpro (&Qdisplay);
14801 Qspace_width = intern ("space-width");
14802 staticpro (&Qspace_width);
14803 Qraise = intern ("raise");
14804 staticpro (&Qraise);
14805 Qspace = intern ("space");
14806 staticpro (&Qspace);
14807 Qmargin = intern ("margin");
14808 staticpro (&Qmargin);
14809 Qleft_margin = intern ("left-margin");
14810 staticpro (&Qleft_margin);
14811 Qright_margin = intern ("right-margin");
14812 staticpro (&Qright_margin);
14813 Qalign_to = intern ("align-to");
14814 staticpro (&Qalign_to);
14815 QCalign_to = intern (":align-to");
14816 staticpro (&QCalign_to);
14817 Qrelative_width = intern ("relative-width");
14818 staticpro (&Qrelative_width);
14819 QCrelative_width = intern (":relative-width");
14820 staticpro (&QCrelative_width);
14821 QCrelative_height = intern (":relative-height");
14822 staticpro (&QCrelative_height);
14823 QCeval = intern (":eval");
14824 staticpro (&QCeval);
14825 QCpropertize = intern (":propertize");
14826 staticpro (&QCpropertize);
14827 Qwhen = intern ("when");
14828 staticpro (&Qwhen);
14829 QCfile = intern (":file");
14830 staticpro (&QCfile);
14831 Qfontified = intern ("fontified");
14832 staticpro (&Qfontified);
14833 Qfontification_functions = intern ("fontification-functions");
14834 staticpro (&Qfontification_functions);
14835 Qtrailing_whitespace = intern ("trailing-whitespace");
14836 staticpro (&Qtrailing_whitespace);
14837 Qimage = intern ("image");
14838 staticpro (&Qimage);
14839 Qmessage_truncate_lines = intern ("message-truncate-lines");
14840 staticpro (&Qmessage_truncate_lines);
14841 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
14842 staticpro (&Qcursor_in_non_selected_windows);
14843 Qgrow_only = intern ("grow-only");
14844 staticpro (&Qgrow_only);
14845 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14846 staticpro (&Qinhibit_menubar_update);
14847 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14848 staticpro (&Qinhibit_eval_during_redisplay);
14849 Qposition = intern ("position");
14850 staticpro (&Qposition);
14851 Qbuffer_position = intern ("buffer-position");
14852 staticpro (&Qbuffer_position);
14853 Qobject = intern ("object");
14854 staticpro (&Qobject);
14855
14856 last_arrow_position = Qnil;
14857 last_arrow_string = Qnil;
14858 staticpro (&last_arrow_position);
14859 staticpro (&last_arrow_string);
14860
14861 echo_buffer[0] = echo_buffer[1] = Qnil;
14862 staticpro (&echo_buffer[0]);
14863 staticpro (&echo_buffer[1]);
14864
14865 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14866 staticpro (&echo_area_buffer[0]);
14867 staticpro (&echo_area_buffer[1]);
14868
14869 Vmessages_buffer_name = build_string ("*Messages*");
14870 staticpro (&Vmessages_buffer_name);
14871
14872 mode_line_proptrans_alist = Qnil;
14873 staticpro (&mode_line_proptrans_alist);
14874
14875 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14876 doc: /* Non-nil means highlight trailing whitespace.
14877 The face used for trailing whitespace is `trailing-whitespace'. */);
14878 Vshow_trailing_whitespace = Qnil;
14879
14880 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14881 doc: /* Non-nil means don't actually do any redisplay.
14882 This is used for internal purposes. */);
14883 Vinhibit_redisplay = Qnil;
14884
14885 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14886 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
14887 Vglobal_mode_string = Qnil;
14888
14889 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14890 doc: /* Marker for where to display an arrow on top of the buffer text.
14891 This must be the beginning of a line in order to work.
14892 See also `overlay-arrow-string'. */);
14893 Voverlay_arrow_position = Qnil;
14894
14895 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14896 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
14897 Voverlay_arrow_string = Qnil;
14898
14899 DEFVAR_INT ("scroll-step", &scroll_step,
14900 doc: /* *The number of lines to try scrolling a window by when point moves out.
14901 If that fails to bring point back on frame, point is centered instead.
14902 If this is zero, point is always centered after it moves off frame.
14903 If you want scrolling to always be a line at a time, you should set
14904 `scroll-conservatively' to a large value rather than set this to 1. */);
14905
14906 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14907 doc: /* *Scroll up to this many lines, to bring point back on screen.
14908 A value of zero means to scroll the text to center point vertically
14909 in the window. */);
14910 scroll_conservatively = 0;
14911
14912 DEFVAR_INT ("scroll-margin", &scroll_margin,
14913 doc: /* *Number of lines of margin at the top and bottom of a window.
14914 Recenter the window whenever point gets within this many lines
14915 of the top or bottom of the window. */);
14916 scroll_margin = 0;
14917
14918 #if GLYPH_DEBUG
14919 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
14920 #endif
14921
14922 DEFVAR_BOOL ("truncate-partial-width-windows",
14923 &truncate_partial_width_windows,
14924 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
14925 truncate_partial_width_windows = 1;
14926
14927 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14928 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
14929 Any other value means to use the appropriate face, `mode-line',
14930 `header-line', or `menu' respectively.
14931
14932 This variable is deprecated; please change the above faces instead. */);
14933 mode_line_inverse_video = 1;
14934
14935 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14936 doc: /* *Maximum buffer size for which line number should be displayed.
14937 If the buffer is bigger than this, the line number does not appear
14938 in the mode line. A value of nil means no limit. */);
14939 Vline_number_display_limit = Qnil;
14940
14941 DEFVAR_INT ("line-number-display-limit-width",
14942 &line_number_display_limit_width,
14943 doc: /* *Maximum line width (in characters) for line number display.
14944 If the average length of the lines near point is bigger than this, then the
14945 line number may be omitted from the mode line. */);
14946 line_number_display_limit_width = 200;
14947
14948 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14949 doc: /* *Non-nil means highlight region even in nonselected windows. */);
14950 highlight_nonselected_windows = 0;
14951
14952 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14953 doc: /* Non-nil if more than one frame is visible on this display.
14954 Minibuffer-only frames don't count, but iconified frames do.
14955 This variable is not guaranteed to be accurate except while processing
14956 `frame-title-format' and `icon-title-format'. */);
14957
14958 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14959 doc: /* Template for displaying the title bar of visible frames.
14960 \(Assuming the window manager supports this feature.)
14961 This variable has the same structure as `mode-line-format' (which see),
14962 and is used only on frames for which no explicit name has been set
14963 \(see `modify-frame-parameters'). */);
14964 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
14965 doc: /* Template for displaying the title bar of an iconified frame.
14966 \(Assuming the window manager supports this feature.)
14967 This variable has the same structure as `mode-line-format' (which see),
14968 and is used only on frames for which no explicit name has been set
14969 \(see `modify-frame-parameters'). */);
14970 Vicon_title_format
14971 = Vframe_title_format
14972 = Fcons (intern ("multiple-frames"),
14973 Fcons (build_string ("%b"),
14974 Fcons (Fcons (empty_string,
14975 Fcons (intern ("invocation-name"),
14976 Fcons (build_string ("@"),
14977 Fcons (intern ("system-name"),
14978 Qnil)))),
14979 Qnil)));
14980
14981 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
14982 doc: /* Maximum number of lines to keep in the message log buffer.
14983 If nil, disable message logging. If t, log messages but don't truncate
14984 the buffer when it becomes large. */);
14985 Vmessage_log_max = make_number (50);
14986
14987 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
14988 doc: /* Functions called before redisplay, if window sizes have changed.
14989 The value should be a list of functions that take one argument.
14990 Just before redisplay, for each frame, if any of its windows have changed
14991 size since the last redisplay, or have been split or deleted,
14992 all the functions in the list are called, with the frame as argument. */);
14993 Vwindow_size_change_functions = Qnil;
14994
14995 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
14996 doc: /* List of Functions to call before redisplaying a window with scrolling.
14997 Each function is called with two arguments, the window
14998 and its new display-start position. Note that the value of `window-end'
14999 is not valid when these functions are called. */);
15000 Vwindow_scroll_functions = Qnil;
15001
15002 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
15003 doc: /* *Non-nil means automatically resize tool-bars.
15004 This increases a tool-bar's height if not all tool-bar items are visible.
15005 It decreases a tool-bar's height when it would display blank lines
15006 otherwise. */);
15007 auto_resize_tool_bars_p = 1;
15008
15009 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
15010 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
15011 auto_raise_tool_bar_buttons_p = 1;
15012
15013 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
15014 doc: /* *Margin around tool-bar buttons in pixels.
15015 If an integer, use that for both horizontal and vertical margins.
15016 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
15017 HORZ specifying the horizontal margin, and VERT specifying the
15018 vertical margin. */);
15019 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
15020
15021 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
15022 doc: /* *Relief thickness of tool-bar buttons. */);
15023 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
15024
15025 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
15026 doc: /* List of functions to call to fontify regions of text.
15027 Each function is called with one argument POS. Functions must
15028 fontify a region starting at POS in the current buffer, and give
15029 fontified regions the property `fontified'. */);
15030 Vfontification_functions = Qnil;
15031 Fmake_variable_buffer_local (Qfontification_functions);
15032
15033 DEFVAR_BOOL ("unibyte-display-via-language-environment",
15034 &unibyte_display_via_language_environment,
15035 doc: /* *Non-nil means display unibyte text according to language environment.
15036 Specifically this means that unibyte non-ASCII characters
15037 are displayed by converting them to the equivalent multibyte characters
15038 according to the current language environment. As a result, they are
15039 displayed according to the current fontset. */);
15040 unibyte_display_via_language_environment = 0;
15041
15042 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
15043 doc: /* *Maximum height for resizing mini-windows.
15044 If a float, it specifies a fraction of the mini-window frame's height.
15045 If an integer, it specifies a number of lines. */);
15046 Vmax_mini_window_height = make_float (0.25);
15047
15048 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
15049 doc: /* *How to resize mini-windows.
15050 A value of nil means don't automatically resize mini-windows.
15051 A value of t means resize them to fit the text displayed in them.
15052 A value of `grow-only', the default, means let mini-windows grow
15053 only, until their display becomes empty, at which point the windows
15054 go back to their normal size. */);
15055 Vresize_mini_windows = Qgrow_only;
15056
15057 DEFVAR_BOOL ("cursor-in-non-selected-windows",
15058 &cursor_in_non_selected_windows,
15059 doc: /* *Non-nil means display a hollow cursor in non-selected windows.
15060 nil means don't display a cursor there. */);
15061 cursor_in_non_selected_windows = 1;
15062
15063 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
15064 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
15065 automatic_hscrolling_p = 1;
15066
15067 DEFVAR_INT ("automatic-hscroll-margin", &automatic_hscroll_margin,
15068 doc: /* *How many columns away from the window edge point is allowed to get
15069 before automatic hscrolling will horizontally scroll the window. */);
15070 automatic_hscroll_margin = 5;
15071
15072 DEFVAR_LISP ("automatic-hscroll-step", &Vautomatic_hscroll_step,
15073 doc: /* *How many columns to scroll the window when point gets too close to the edge.
15074 When point is less than `automatic-hscroll-margin' columns from the window
15075 edge, automatic hscrolling will scroll the window by the amount of columns
15076 determined by this variable. If its value is a positive integer, scroll that
15077 many columns. If it's a positive floating-point number, it specifies the
15078 fraction of the window's width to scroll. If it's nil or zero, point will be
15079 centered horizontally after the scroll. Any other value, including negative
15080 numbers, are treated as if the value were zero.
15081
15082 Automatic hscrolling always moves point outside the scroll margin, so if
15083 point was more than scroll step columns inside the margin, the window will
15084 scroll more than the value given by the scroll step.
15085
15086 Note that the lower bound for automatic hscrolling specified by `scroll-left'
15087 and `scroll-right' overrides this variable's effect. */);
15088 Vautomatic_hscroll_step = make_number (0);
15089
15090 DEFVAR_LISP ("image-types", &Vimage_types,
15091 doc: /* List of supported image types.
15092 Each element of the list is a symbol for a supported image type. */);
15093 Vimage_types = Qnil;
15094
15095 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
15096 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
15097 Bind this around calls to `message' to let it take effect. */);
15098 message_truncate_lines = 0;
15099
15100 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
15101 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
15102 Can be used to update submenus whose contents should vary. */);
15103 Vmenu_bar_update_hook = Qnil;
15104
15105 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
15106 doc: /* Non-nil means don't update menu bars. Internal use only. */);
15107 inhibit_menubar_update = 0;
15108
15109 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
15110 doc: /* Non-nil means don't eval Lisp during redisplay. */);
15111 inhibit_eval_during_redisplay = 0;
15112
15113 #if GLYPH_DEBUG
15114 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
15115 doc: /* Inhibit try_window_id display optimization. */);
15116 inhibit_try_window_id = 0;
15117
15118 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
15119 doc: /* Inhibit try_window_reusing display optimization. */);
15120 inhibit_try_window_reusing = 0;
15121
15122 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
15123 doc: /* Inhibit try_cursor_movement display optimization. */);
15124 inhibit_try_cursor_movement = 0;
15125 #endif /* GLYPH_DEBUG */
15126 }
15127
15128
15129 /* Initialize this module when Emacs starts. */
15130
15131 void
15132 init_xdisp ()
15133 {
15134 Lisp_Object root_window;
15135 struct window *mini_w;
15136
15137 current_header_line_height = current_mode_line_height = -1;
15138
15139 CHARPOS (this_line_start_pos) = 0;
15140
15141 mini_w = XWINDOW (minibuf_window);
15142 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
15143
15144 if (!noninteractive)
15145 {
15146 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
15147 int i;
15148
15149 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
15150 set_window_height (root_window,
15151 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
15152 0);
15153 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
15154 set_window_height (minibuf_window, 1, 0);
15155
15156 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
15157 mini_w->width = make_number (FRAME_WIDTH (f));
15158
15159 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
15160 scratch_glyph_row.glyphs[TEXT_AREA + 1]
15161 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
15162
15163 /* The default ellipsis glyphs `...'. */
15164 for (i = 0; i < 3; ++i)
15165 default_invis_vector[i] = make_number ('.');
15166 }
15167
15168 #ifdef HAVE_WINDOW_SYSTEM
15169 {
15170 /* Allocate the buffer for frame titles. */
15171 int size = 100;
15172 frame_title_buf = (char *) xmalloc (size);
15173 frame_title_buf_end = frame_title_buf + size;
15174 frame_title_ptr = NULL;
15175 }
15176 #endif /* HAVE_WINDOW_SYSTEM */
15177
15178 help_echo_showing_p = 0;
15179 }
15180
15181