]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(gamegrid-add-score): Add info to docstring.
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99, 2000, 2001, 2002, 2003
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 do? 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 an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "macros.h"
183 #include "disptab.h"
184 #include "termhooks.h"
185 #include "intervals.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
189 #include "fontset.h"
190
191 #ifdef HAVE_X_WINDOWS
192 #include "xterm.h"
193 #endif
194 #ifdef WINDOWSNT
195 #include "w32term.h"
196 #endif
197 #ifdef MAC_OS
198 #include "macterm.h"
199 #endif
200
201 #define INFINITY 10000000
202
203 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
204 || defined (USE_GTK)
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 extern Lisp_Object Vminibuffer_list;
214
215 extern Lisp_Object Qface;
216 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
217
218 extern Lisp_Object Voverriding_local_map;
219 extern Lisp_Object Voverriding_local_map_menu_flag;
220 extern Lisp_Object Qmenu_item;
221 extern Lisp_Object Qwhen;
222
223 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
224 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
225 Lisp_Object Qredisplay_end_trigger_functions;
226 Lisp_Object Qinhibit_point_motion_hooks;
227 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
228 Lisp_Object Qfontified;
229 Lisp_Object Qgrow_only;
230 Lisp_Object Qinhibit_eval_during_redisplay;
231 Lisp_Object Qbuffer_position, Qposition, Qobject;
232
233 /* Cursor shapes */
234 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
235
236 Lisp_Object Qrisky_local_variable;
237
238 /* Holds the list (error). */
239 Lisp_Object list_of_error;
240
241 /* Functions called to fontify regions of text. */
242
243 Lisp_Object Vfontification_functions;
244 Lisp_Object Qfontification_functions;
245
246 /* Non-zero means draw tool bar buttons raised when the mouse moves
247 over them. */
248
249 int auto_raise_tool_bar_buttons_p;
250
251 /* Margin around tool bar buttons in pixels. */
252
253 Lisp_Object Vtool_bar_button_margin;
254
255 /* Thickness of shadow to draw around tool bar buttons. */
256
257 EMACS_INT tool_bar_button_relief;
258
259 /* Non-zero means automatically resize tool-bars so that all tool-bar
260 items are visible, and no blank lines remain. */
261
262 int auto_resize_tool_bars_p;
263
264 /* Non-nil means don't actually do any redisplay. */
265
266 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
267
268 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
269
270 int inhibit_eval_during_redisplay;
271
272 /* Names of text properties relevant for redisplay. */
273
274 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
275 extern Lisp_Object Qface, Qinvisible, Qwidth;
276
277 /* Symbols used in text property values. */
278
279 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
280 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
281 Lisp_Object Qmargin;
282 extern Lisp_Object Qheight;
283
284 /* Non-nil means highlight trailing whitespace. */
285
286 Lisp_Object Vshow_trailing_whitespace;
287
288 /* Name of the face used to highlight trailing whitespace. */
289
290 Lisp_Object Qtrailing_whitespace;
291
292 /* The symbol `image' which is the car of the lists used to represent
293 images in Lisp. */
294
295 Lisp_Object Qimage;
296
297 /* Non-zero means print newline to stdout before next mini-buffer
298 message. */
299
300 int noninteractive_need_newline;
301
302 /* Non-zero means print newline to message log before next message. */
303
304 static int message_log_need_newline;
305
306 /* Three markers that message_dolog uses.
307 It could allocate them itself, but that causes trouble
308 in handling memory-full errors. */
309 static Lisp_Object message_dolog_marker1;
310 static Lisp_Object message_dolog_marker2;
311 static Lisp_Object message_dolog_marker3;
312 \f
313 /* The buffer position of the first character appearing entirely or
314 partially on the line of the selected window which contains the
315 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
316 redisplay optimization in redisplay_internal. */
317
318 static struct text_pos this_line_start_pos;
319
320 /* Number of characters past the end of the line above, including the
321 terminating newline. */
322
323 static struct text_pos this_line_end_pos;
324
325 /* The vertical positions and the height of this line. */
326
327 static int this_line_vpos;
328 static int this_line_y;
329 static int this_line_pixel_height;
330
331 /* X position at which this display line starts. Usually zero;
332 negative if first character is partially visible. */
333
334 static int this_line_start_x;
335
336 /* Buffer that this_line_.* variables are referring to. */
337
338 static struct buffer *this_line_buffer;
339
340 /* Nonzero means truncate lines in all windows less wide than the
341 frame. */
342
343 int truncate_partial_width_windows;
344
345 /* A flag to control how to display unibyte 8-bit character. */
346
347 int unibyte_display_via_language_environment;
348
349 /* Nonzero means we have more than one non-mini-buffer-only frame.
350 Not guaranteed to be accurate except while parsing
351 frame-title-format. */
352
353 int multiple_frames;
354
355 Lisp_Object Vglobal_mode_string;
356
357 /* Marker for where to display an arrow on top of the buffer text. */
358
359 Lisp_Object Voverlay_arrow_position;
360
361 /* String to display for the arrow. Only used on terminal frames. */
362
363 Lisp_Object Voverlay_arrow_string;
364
365 /* Values of those variables at last redisplay. However, if
366 Voverlay_arrow_position is a marker, last_arrow_position is its
367 numerical position. */
368
369 static Lisp_Object last_arrow_position, last_arrow_string;
370
371 /* Like mode-line-format, but for the title bar on a visible frame. */
372
373 Lisp_Object Vframe_title_format;
374
375 /* Like mode-line-format, but for the title bar on an iconified frame. */
376
377 Lisp_Object Vicon_title_format;
378
379 /* List of functions to call when a window's size changes. These
380 functions get one arg, a frame on which one or more windows' sizes
381 have changed. */
382
383 static Lisp_Object Vwindow_size_change_functions;
384
385 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
386
387 /* Nonzero if overlay arrow has been displayed once in this window. */
388
389 static int overlay_arrow_seen;
390
391 /* Nonzero means highlight the region even in nonselected windows. */
392
393 int highlight_nonselected_windows;
394
395 /* If cursor motion alone moves point off frame, try scrolling this
396 many lines up or down if that will bring it back. */
397
398 static EMACS_INT scroll_step;
399
400 /* Nonzero means scroll just far enough to bring point back on the
401 screen, when appropriate. */
402
403 static EMACS_INT scroll_conservatively;
404
405 /* Recenter the window whenever point gets within this many lines of
406 the top or bottom of the window. This value is translated into a
407 pixel value by multiplying it with CANON_Y_UNIT, which means that
408 there is really a fixed pixel height scroll margin. */
409
410 EMACS_INT scroll_margin;
411
412 /* Number of windows showing the buffer of the selected window (or
413 another buffer with the same base buffer). keyboard.c refers to
414 this. */
415
416 int buffer_shared;
417
418 /* Vector containing glyphs for an ellipsis `...'. */
419
420 static Lisp_Object default_invis_vector[3];
421
422 /* Zero means display the mode-line/header-line/menu-bar in the default face
423 (this slightly odd definition is for compatibility with previous versions
424 of emacs), non-zero means display them using their respective faces.
425
426 This variable is deprecated. */
427
428 int mode_line_inverse_video;
429
430 /* Prompt to display in front of the mini-buffer contents. */
431
432 Lisp_Object minibuf_prompt;
433
434 /* Width of current mini-buffer prompt. Only set after display_line
435 of the line that contains the prompt. */
436
437 int minibuf_prompt_width;
438
439 /* This is the window where the echo area message was displayed. It
440 is always a mini-buffer window, but it may not be the same window
441 currently active as a mini-buffer. */
442
443 Lisp_Object echo_area_window;
444
445 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
446 pushes the current message and the value of
447 message_enable_multibyte on the stack, the function restore_message
448 pops the stack and displays MESSAGE again. */
449
450 Lisp_Object Vmessage_stack;
451
452 /* Nonzero means multibyte characters were enabled when the echo area
453 message was specified. */
454
455 int message_enable_multibyte;
456
457 /* Nonzero if we should redraw the mode lines on the next redisplay. */
458
459 int update_mode_lines;
460
461 /* Nonzero if window sizes or contents have changed since last
462 redisplay that finished. */
463
464 int windows_or_buffers_changed;
465
466 /* Nonzero means a frame's cursor type has been changed. */
467
468 int cursor_type_changed;
469
470 /* Nonzero after display_mode_line if %l was used and it displayed a
471 line number. */
472
473 int line_number_displayed;
474
475 /* Maximum buffer size for which to display line numbers. */
476
477 Lisp_Object Vline_number_display_limit;
478
479 /* Line width to consider when repositioning for line number display. */
480
481 static EMACS_INT line_number_display_limit_width;
482
483 /* Number of lines to keep in the message log buffer. t means
484 infinite. nil means don't log at all. */
485
486 Lisp_Object Vmessage_log_max;
487
488 /* The name of the *Messages* buffer, a string. */
489
490 static Lisp_Object Vmessages_buffer_name;
491
492 /* Current, index 0, and last displayed echo area message. Either
493 buffers from echo_buffers, or nil to indicate no message. */
494
495 Lisp_Object echo_area_buffer[2];
496
497 /* The buffers referenced from echo_area_buffer. */
498
499 static Lisp_Object echo_buffer[2];
500
501 /* A vector saved used in with_area_buffer to reduce consing. */
502
503 static Lisp_Object Vwith_echo_area_save_vector;
504
505 /* Non-zero means display_echo_area should display the last echo area
506 message again. Set by redisplay_preserve_echo_area. */
507
508 static int display_last_displayed_message_p;
509
510 /* Nonzero if echo area is being used by print; zero if being used by
511 message. */
512
513 int message_buf_print;
514
515 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
516
517 Lisp_Object Qinhibit_menubar_update;
518 int inhibit_menubar_update;
519
520 /* Maximum height for resizing mini-windows. Either a float
521 specifying a fraction of the available height, or an integer
522 specifying a number of lines. */
523
524 Lisp_Object Vmax_mini_window_height;
525
526 /* Non-zero means messages should be displayed with truncated
527 lines instead of being continued. */
528
529 int message_truncate_lines;
530 Lisp_Object Qmessage_truncate_lines;
531
532 /* Set to 1 in clear_message to make redisplay_internal aware
533 of an emptied echo area. */
534
535 static int message_cleared_p;
536
537 /* Non-zero means we want a hollow cursor in windows that are not
538 selected. Zero means there's no cursor in such windows. */
539
540 Lisp_Object Vcursor_in_non_selected_windows;
541 Lisp_Object Qcursor_in_non_selected_windows;
542
543 /* How to blink the default frame cursor off. */
544 Lisp_Object Vblink_cursor_alist;
545
546 /* A scratch glyph row with contents used for generating truncation
547 glyphs. Also used in direct_output_for_insert. */
548
549 #define MAX_SCRATCH_GLYPHS 100
550 struct glyph_row scratch_glyph_row;
551 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
552
553 /* Ascent and height of the last line processed by move_it_to. */
554
555 static int last_max_ascent, last_height;
556
557 /* Non-zero if there's a help-echo in the echo area. */
558
559 int help_echo_showing_p;
560
561 /* If >= 0, computed, exact values of mode-line and header-line height
562 to use in the macros CURRENT_MODE_LINE_HEIGHT and
563 CURRENT_HEADER_LINE_HEIGHT. */
564
565 int current_mode_line_height, current_header_line_height;
566
567 /* The maximum distance to look ahead for text properties. Values
568 that are too small let us call compute_char_face and similar
569 functions too often which is expensive. Values that are too large
570 let us call compute_char_face and alike too often because we
571 might not be interested in text properties that far away. */
572
573 #define TEXT_PROP_DISTANCE_LIMIT 100
574
575 #if GLYPH_DEBUG
576
577 /* Variables to turn off display optimizations from Lisp. */
578
579 int inhibit_try_window_id, inhibit_try_window_reusing;
580 int inhibit_try_cursor_movement;
581
582 /* Non-zero means print traces of redisplay if compiled with
583 GLYPH_DEBUG != 0. */
584
585 int trace_redisplay_p;
586
587 #endif /* GLYPH_DEBUG */
588
589 #ifdef DEBUG_TRACE_MOVE
590 /* Non-zero means trace with TRACE_MOVE to stderr. */
591 int trace_move;
592
593 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
594 #else
595 #define TRACE_MOVE(x) (void) 0
596 #endif
597
598 /* Non-zero means automatically scroll windows horizontally to make
599 point visible. */
600
601 int automatic_hscrolling_p;
602
603 /* How close to the margin can point get before the window is scrolled
604 horizontally. */
605 EMACS_INT hscroll_margin;
606
607 /* How much to scroll horizontally when point is inside the above margin. */
608 Lisp_Object Vhscroll_step;
609
610 /* A list of symbols, one for each supported image type. */
611
612 Lisp_Object Vimage_types;
613
614 /* The variable `resize-mini-windows'. If nil, don't resize
615 mini-windows. If t, always resize them to fit the text they
616 display. If `grow-only', let mini-windows grow only until they
617 become empty. */
618
619 Lisp_Object Vresize_mini_windows;
620
621 /* Buffer being redisplayed -- for redisplay_window_error. */
622
623 struct buffer *displayed_buffer;
624
625 /* Value returned from text property handlers (see below). */
626
627 enum prop_handled
628 {
629 HANDLED_NORMALLY,
630 HANDLED_RECOMPUTE_PROPS,
631 HANDLED_OVERLAY_STRING_CONSUMED,
632 HANDLED_RETURN
633 };
634
635 /* A description of text properties that redisplay is interested
636 in. */
637
638 struct props
639 {
640 /* The name of the property. */
641 Lisp_Object *name;
642
643 /* A unique index for the property. */
644 enum prop_idx idx;
645
646 /* A handler function called to set up iterator IT from the property
647 at IT's current position. Value is used to steer handle_stop. */
648 enum prop_handled (*handler) P_ ((struct it *it));
649 };
650
651 static enum prop_handled handle_face_prop P_ ((struct it *));
652 static enum prop_handled handle_invisible_prop P_ ((struct it *));
653 static enum prop_handled handle_display_prop P_ ((struct it *));
654 static enum prop_handled handle_composition_prop P_ ((struct it *));
655 static enum prop_handled handle_overlay_change P_ ((struct it *));
656 static enum prop_handled handle_fontified_prop P_ ((struct it *));
657
658 /* Properties handled by iterators. */
659
660 static struct props it_props[] =
661 {
662 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
663 /* Handle `face' before `display' because some sub-properties of
664 `display' need to know the face. */
665 {&Qface, FACE_PROP_IDX, handle_face_prop},
666 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
667 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
668 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
669 {NULL, 0, NULL}
670 };
671
672 /* Value is the position described by X. If X is a marker, value is
673 the marker_position of X. Otherwise, value is X. */
674
675 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
676
677 /* Enumeration returned by some move_it_.* functions internally. */
678
679 enum move_it_result
680 {
681 /* Not used. Undefined value. */
682 MOVE_UNDEFINED,
683
684 /* Move ended at the requested buffer position or ZV. */
685 MOVE_POS_MATCH_OR_ZV,
686
687 /* Move ended at the requested X pixel position. */
688 MOVE_X_REACHED,
689
690 /* Move within a line ended at the end of a line that must be
691 continued. */
692 MOVE_LINE_CONTINUED,
693
694 /* Move within a line ended at the end of a line that would
695 be displayed truncated. */
696 MOVE_LINE_TRUNCATED,
697
698 /* Move within a line ended at a line end. */
699 MOVE_NEWLINE_OR_CR
700 };
701
702 /* This counter is used to clear the face cache every once in a while
703 in redisplay_internal. It is incremented for each redisplay.
704 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
705 cleared. */
706
707 #define CLEAR_FACE_CACHE_COUNT 500
708 static int clear_face_cache_count;
709
710 /* Record the previous terminal frame we displayed. */
711
712 static struct frame *previous_terminal_frame;
713
714 /* Non-zero while redisplay_internal is in progress. */
715
716 int redisplaying_p;
717
718 /* Non-zero means don't free realized faces. Bound while freeing
719 realized faces is dangerous because glyph matrices might still
720 reference them. */
721
722 int inhibit_free_realized_faces;
723 Lisp_Object Qinhibit_free_realized_faces;
724
725 \f
726 /* Function prototypes. */
727
728 static void setup_for_ellipsis P_ ((struct it *));
729 static void mark_window_display_accurate_1 P_ ((struct window *, int));
730 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
731 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
732 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
733 static int redisplay_mode_lines P_ ((Lisp_Object, int));
734 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
735
736 #if 0
737 static int invisible_text_between_p P_ ((struct it *, int, int));
738 #endif
739
740 static int next_element_from_ellipsis P_ ((struct it *));
741 static void pint2str P_ ((char *, int, int));
742 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
743 struct text_pos));
744 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
745 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
746 static void store_frame_title_char P_ ((char));
747 static int store_frame_title P_ ((const unsigned char *, int, int));
748 static void x_consider_frame_title P_ ((Lisp_Object));
749 static void handle_stop P_ ((struct it *));
750 static int tool_bar_lines_needed P_ ((struct frame *));
751 static int single_display_prop_intangible_p P_ ((Lisp_Object));
752 static void ensure_echo_area_buffers P_ ((void));
753 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
754 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
755 static int with_echo_area_buffer P_ ((struct window *, int,
756 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
757 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
758 static void clear_garbaged_frames P_ ((void));
759 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
760 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
761 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
762 static int display_echo_area P_ ((struct window *));
763 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
764 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
765 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
766 static int string_char_and_length P_ ((const unsigned char *, int, int *));
767 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
768 struct text_pos));
769 static int compute_window_start_on_continuation_line P_ ((struct window *));
770 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
771 static void insert_left_trunc_glyphs P_ ((struct it *));
772 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
773 static void extend_face_to_end_of_line P_ ((struct it *));
774 static int append_space P_ ((struct it *, int));
775 static int make_cursor_line_fully_visible P_ ((struct window *));
776 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
777 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
778 static int trailing_whitespace_p P_ ((int));
779 static int message_log_check_duplicate P_ ((int, int, int, int));
780 static void push_it P_ ((struct it *));
781 static void pop_it P_ ((struct it *));
782 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
783 static void redisplay_internal P_ ((int));
784 static int echo_area_display P_ ((int));
785 static void redisplay_windows P_ ((Lisp_Object));
786 static void redisplay_window P_ ((Lisp_Object, int));
787 static Lisp_Object redisplay_window_error ();
788 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
789 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
790 static void update_menu_bar P_ ((struct frame *, int));
791 static int try_window_reusing_current_matrix P_ ((struct window *));
792 static int try_window_id P_ ((struct window *));
793 static int display_line P_ ((struct it *));
794 static int display_mode_lines P_ ((struct window *));
795 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
796 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
797 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
798 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
799 static void display_menu_bar P_ ((struct window *));
800 static int display_count_lines P_ ((int, int, int, int, int *));
801 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
802 int, int, struct it *, int, int, int, int));
803 static void compute_line_metrics P_ ((struct it *));
804 static void run_redisplay_end_trigger_hook P_ ((struct it *));
805 static int get_overlay_strings P_ ((struct it *, int));
806 static void next_overlay_string P_ ((struct it *));
807 static void reseat P_ ((struct it *, struct text_pos, int));
808 static void reseat_1 P_ ((struct it *, struct text_pos, int));
809 static void back_to_previous_visible_line_start P_ ((struct it *));
810 static void reseat_at_previous_visible_line_start P_ ((struct it *));
811 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
812 static int next_element_from_display_vector P_ ((struct it *));
813 static int next_element_from_string P_ ((struct it *));
814 static int next_element_from_c_string P_ ((struct it *));
815 static int next_element_from_buffer P_ ((struct it *));
816 static int next_element_from_composition P_ ((struct it *));
817 static int next_element_from_image P_ ((struct it *));
818 static int next_element_from_stretch P_ ((struct it *));
819 static void load_overlay_strings P_ ((struct it *, int));
820 static int init_from_display_pos P_ ((struct it *, struct window *,
821 struct display_pos *));
822 static void reseat_to_string P_ ((struct it *, unsigned char *,
823 Lisp_Object, int, int, int, int));
824 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
825 int, int, int));
826 void move_it_vertically_backward P_ ((struct it *, int));
827 static void init_to_row_start P_ ((struct it *, struct window *,
828 struct glyph_row *));
829 static int init_to_row_end P_ ((struct it *, struct window *,
830 struct glyph_row *));
831 static void back_to_previous_line_start P_ ((struct it *));
832 static int forward_to_next_line_start P_ ((struct it *, int *));
833 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
834 Lisp_Object, int));
835 static struct text_pos string_pos P_ ((int, Lisp_Object));
836 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
837 static int number_of_chars P_ ((unsigned char *, int));
838 static void compute_stop_pos P_ ((struct it *));
839 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
840 Lisp_Object));
841 static int face_before_or_after_it_pos P_ ((struct it *, int));
842 static int next_overlay_change P_ ((int));
843 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
844 Lisp_Object, struct text_pos *,
845 int));
846 static int underlying_face_id P_ ((struct it *));
847 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
848 struct window *));
849
850 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
851 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
852
853 #ifdef HAVE_WINDOW_SYSTEM
854
855 static void update_tool_bar P_ ((struct frame *, int));
856 static void build_desired_tool_bar_string P_ ((struct frame *f));
857 static int redisplay_tool_bar P_ ((struct frame *));
858 static void display_tool_bar_line P_ ((struct it *));
859
860 #endif /* HAVE_WINDOW_SYSTEM */
861
862 \f
863 /***********************************************************************
864 Window display dimensions
865 ***********************************************************************/
866
867 /* Return the bottom boundary y-position for text lines in window W.
868 This is the first y position at which a line cannot start.
869 It is relative to the top of the window.
870
871 This is the height of W minus the height of a mode line, if any. */
872
873 INLINE int
874 window_text_bottom_y (w)
875 struct window *w;
876 {
877 struct frame *f = XFRAME (w->frame);
878 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
879
880 if (WINDOW_WANTS_MODELINE_P (w))
881 height -= CURRENT_MODE_LINE_HEIGHT (w);
882 return height;
883 }
884
885
886 /* Return the pixel width of display area AREA of window W. AREA < 0
887 means return the total width of W, not including fringes to
888 the left and right of the window. */
889
890 INLINE int
891 window_box_width (w, area)
892 struct window *w;
893 int area;
894 {
895 struct frame *f = XFRAME (w->frame);
896 int width = XFASTINT (w->width);
897
898 if (!w->pseudo_window_p)
899 {
900 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
901
902 if (area == TEXT_AREA)
903 {
904 if (INTEGERP (w->left_margin_width))
905 width -= XFASTINT (w->left_margin_width);
906 if (INTEGERP (w->right_margin_width))
907 width -= XFASTINT (w->right_margin_width);
908 }
909 else if (area == LEFT_MARGIN_AREA)
910 width = (INTEGERP (w->left_margin_width)
911 ? XFASTINT (w->left_margin_width) : 0);
912 else if (area == RIGHT_MARGIN_AREA)
913 width = (INTEGERP (w->right_margin_width)
914 ? XFASTINT (w->right_margin_width) : 0);
915 }
916
917 return width * CANON_X_UNIT (f);
918 }
919
920
921 /* Return the pixel height of the display area of window W, not
922 including mode lines of W, if any. */
923
924 INLINE int
925 window_box_height (w)
926 struct window *w;
927 {
928 struct frame *f = XFRAME (w->frame);
929 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
930
931 xassert (height >= 0);
932
933 /* Note: the code below that determines the mode-line/header-line
934 height is essentially the same as that contained in the macro
935 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
936 the appropriate glyph row has its `mode_line_p' flag set,
937 and if it doesn't, uses estimate_mode_line_height instead. */
938
939 if (WINDOW_WANTS_MODELINE_P (w))
940 {
941 struct glyph_row *ml_row
942 = (w->current_matrix && w->current_matrix->rows
943 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
944 : 0);
945 if (ml_row && ml_row->mode_line_p)
946 height -= ml_row->height;
947 else
948 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
949 }
950
951 if (WINDOW_WANTS_HEADER_LINE_P (w))
952 {
953 struct glyph_row *hl_row
954 = (w->current_matrix && w->current_matrix->rows
955 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
956 : 0);
957 if (hl_row && hl_row->mode_line_p)
958 height -= hl_row->height;
959 else
960 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
961 }
962
963 /* With a very small font and a mode-line that's taller than
964 default, we might end up with a negative height. */
965 return max (0, height);
966 }
967
968
969 /* Return the frame-relative coordinate of the left edge of display
970 area AREA of window W. AREA < 0 means return the left edge of the
971 whole window, to the right of the left fringe of W. */
972
973 INLINE int
974 window_box_left (w, area)
975 struct window *w;
976 int area;
977 {
978 struct frame *f = XFRAME (w->frame);
979 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
980
981 if (!w->pseudo_window_p)
982 {
983 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
984 + FRAME_LEFT_FRINGE_WIDTH (f));
985
986 if (area == TEXT_AREA)
987 x += window_box_width (w, LEFT_MARGIN_AREA);
988 else if (area == RIGHT_MARGIN_AREA)
989 x += (window_box_width (w, LEFT_MARGIN_AREA)
990 + window_box_width (w, TEXT_AREA));
991 }
992
993 return x;
994 }
995
996
997 /* Return the frame-relative coordinate of the right edge of display
998 area AREA of window W. AREA < 0 means return the left edge of the
999 whole window, to the left of the right fringe of W. */
1000
1001 INLINE int
1002 window_box_right (w, area)
1003 struct window *w;
1004 int area;
1005 {
1006 return window_box_left (w, area) + window_box_width (w, area);
1007 }
1008
1009
1010 /* Get the bounding box of the display area AREA of window W, without
1011 mode lines, in frame-relative coordinates. AREA < 0 means the
1012 whole window, not including the left and right fringes of
1013 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1014 coordinates of the upper-left corner of the box. Return in
1015 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1016
1017 INLINE void
1018 window_box (w, area, box_x, box_y, box_width, box_height)
1019 struct window *w;
1020 int area;
1021 int *box_x, *box_y, *box_width, *box_height;
1022 {
1023 struct frame *f = XFRAME (w->frame);
1024
1025 *box_width = window_box_width (w, area);
1026 *box_height = window_box_height (w);
1027 *box_x = window_box_left (w, area);
1028 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
1029 + XFASTINT (w->top) * CANON_Y_UNIT (f));
1030 if (WINDOW_WANTS_HEADER_LINE_P (w))
1031 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1032 }
1033
1034
1035 /* Get the bounding box of the display area AREA of window W, without
1036 mode lines. AREA < 0 means the whole window, not including the
1037 left and right fringe of the window. Return in *TOP_LEFT_X
1038 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1039 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1040 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1041 box. */
1042
1043 INLINE void
1044 window_box_edges (w, area, top_left_x, top_left_y,
1045 bottom_right_x, bottom_right_y)
1046 struct window *w;
1047 int area;
1048 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1049 {
1050 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1051 bottom_right_y);
1052 *bottom_right_x += *top_left_x;
1053 *bottom_right_y += *top_left_y;
1054 }
1055
1056
1057 \f
1058 /***********************************************************************
1059 Utilities
1060 ***********************************************************************/
1061
1062 /* Return the bottom y-position of the line the iterator IT is in.
1063 This can modify IT's settings. */
1064
1065 int
1066 line_bottom_y (it)
1067 struct it *it;
1068 {
1069 int line_height = it->max_ascent + it->max_descent;
1070 int line_top_y = it->current_y;
1071
1072 if (line_height == 0)
1073 {
1074 if (last_height)
1075 line_height = last_height;
1076 else if (IT_CHARPOS (*it) < ZV)
1077 {
1078 move_it_by_lines (it, 1, 1);
1079 line_height = (it->max_ascent || it->max_descent
1080 ? it->max_ascent + it->max_descent
1081 : last_height);
1082 }
1083 else
1084 {
1085 struct glyph_row *row = it->glyph_row;
1086
1087 /* Use the default character height. */
1088 it->glyph_row = NULL;
1089 it->what = IT_CHARACTER;
1090 it->c = ' ';
1091 it->len = 1;
1092 PRODUCE_GLYPHS (it);
1093 line_height = it->ascent + it->descent;
1094 it->glyph_row = row;
1095 }
1096 }
1097
1098 return line_top_y + line_height;
1099 }
1100
1101
1102 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1103 1 if POS is visible and the line containing POS is fully visible.
1104 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1105 and header-lines heights. */
1106
1107 int
1108 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1109 struct window *w;
1110 int charpos, *fully, exact_mode_line_heights_p;
1111 {
1112 struct it it;
1113 struct text_pos top;
1114 int visible_p;
1115 struct buffer *old_buffer = NULL;
1116
1117 if (XBUFFER (w->buffer) != current_buffer)
1118 {
1119 old_buffer = current_buffer;
1120 set_buffer_internal_1 (XBUFFER (w->buffer));
1121 }
1122
1123 *fully = visible_p = 0;
1124 SET_TEXT_POS_FROM_MARKER (top, w->start);
1125
1126 /* Compute exact mode line heights, if requested. */
1127 if (exact_mode_line_heights_p)
1128 {
1129 if (WINDOW_WANTS_MODELINE_P (w))
1130 current_mode_line_height
1131 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1132 current_buffer->mode_line_format);
1133
1134 if (WINDOW_WANTS_HEADER_LINE_P (w))
1135 current_header_line_height
1136 = display_mode_line (w, HEADER_LINE_FACE_ID,
1137 current_buffer->header_line_format);
1138 }
1139
1140 start_display (&it, w, top);
1141 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1142 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1143
1144 /* Note that we may overshoot because of invisible text. */
1145 if (IT_CHARPOS (it) >= charpos)
1146 {
1147 int top_y = it.current_y;
1148 int bottom_y = line_bottom_y (&it);
1149 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1150
1151 if (top_y < window_top_y)
1152 visible_p = bottom_y > window_top_y;
1153 else if (top_y < it.last_visible_y)
1154 {
1155 visible_p = 1;
1156 *fully = bottom_y <= it.last_visible_y;
1157 }
1158 }
1159 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1160 {
1161 move_it_by_lines (&it, 1, 0);
1162 if (charpos < IT_CHARPOS (it))
1163 {
1164 visible_p = 1;
1165 *fully = 0;
1166 }
1167 }
1168
1169 if (old_buffer)
1170 set_buffer_internal_1 (old_buffer);
1171
1172 current_header_line_height = current_mode_line_height = -1;
1173 return visible_p;
1174 }
1175
1176
1177 /* Return the next character from STR which is MAXLEN bytes long.
1178 Return in *LEN the length of the character. This is like
1179 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1180 we find one, we return a `?', but with the length of the invalid
1181 character. */
1182
1183 static INLINE int
1184 string_char_and_length (str, maxlen, len)
1185 const unsigned char *str;
1186 int maxlen, *len;
1187 {
1188 int c;
1189
1190 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1191 if (!CHAR_VALID_P (c, 1))
1192 /* We may not change the length here because other places in Emacs
1193 don't use this function, i.e. they silently accept invalid
1194 characters. */
1195 c = '?';
1196
1197 return c;
1198 }
1199
1200
1201
1202 /* Given a position POS containing a valid character and byte position
1203 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1204
1205 static struct text_pos
1206 string_pos_nchars_ahead (pos, string, nchars)
1207 struct text_pos pos;
1208 Lisp_Object string;
1209 int nchars;
1210 {
1211 xassert (STRINGP (string) && nchars >= 0);
1212
1213 if (STRING_MULTIBYTE (string))
1214 {
1215 int rest = SBYTES (string) - BYTEPOS (pos);
1216 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1217 int len;
1218
1219 while (nchars--)
1220 {
1221 string_char_and_length (p, rest, &len);
1222 p += len, rest -= len;
1223 xassert (rest >= 0);
1224 CHARPOS (pos) += 1;
1225 BYTEPOS (pos) += len;
1226 }
1227 }
1228 else
1229 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1230
1231 return pos;
1232 }
1233
1234
1235 /* Value is the text position, i.e. character and byte position,
1236 for character position CHARPOS in STRING. */
1237
1238 static INLINE struct text_pos
1239 string_pos (charpos, string)
1240 int charpos;
1241 Lisp_Object string;
1242 {
1243 struct text_pos pos;
1244 xassert (STRINGP (string));
1245 xassert (charpos >= 0);
1246 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1247 return pos;
1248 }
1249
1250
1251 /* Value is a text position, i.e. character and byte position, for
1252 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1253 means recognize multibyte characters. */
1254
1255 static struct text_pos
1256 c_string_pos (charpos, s, multibyte_p)
1257 int charpos;
1258 unsigned char *s;
1259 int multibyte_p;
1260 {
1261 struct text_pos pos;
1262
1263 xassert (s != NULL);
1264 xassert (charpos >= 0);
1265
1266 if (multibyte_p)
1267 {
1268 int rest = strlen (s), len;
1269
1270 SET_TEXT_POS (pos, 0, 0);
1271 while (charpos--)
1272 {
1273 string_char_and_length (s, rest, &len);
1274 s += len, rest -= len;
1275 xassert (rest >= 0);
1276 CHARPOS (pos) += 1;
1277 BYTEPOS (pos) += len;
1278 }
1279 }
1280 else
1281 SET_TEXT_POS (pos, charpos, charpos);
1282
1283 return pos;
1284 }
1285
1286
1287 /* Value is the number of characters in C string S. MULTIBYTE_P
1288 non-zero means recognize multibyte characters. */
1289
1290 static int
1291 number_of_chars (s, multibyte_p)
1292 unsigned char *s;
1293 int multibyte_p;
1294 {
1295 int nchars;
1296
1297 if (multibyte_p)
1298 {
1299 int rest = strlen (s), len;
1300 unsigned char *p = (unsigned char *) s;
1301
1302 for (nchars = 0; rest > 0; ++nchars)
1303 {
1304 string_char_and_length (p, rest, &len);
1305 rest -= len, p += len;
1306 }
1307 }
1308 else
1309 nchars = strlen (s);
1310
1311 return nchars;
1312 }
1313
1314
1315 /* Compute byte position NEWPOS->bytepos corresponding to
1316 NEWPOS->charpos. POS is a known position in string STRING.
1317 NEWPOS->charpos must be >= POS.charpos. */
1318
1319 static void
1320 compute_string_pos (newpos, pos, string)
1321 struct text_pos *newpos, pos;
1322 Lisp_Object string;
1323 {
1324 xassert (STRINGP (string));
1325 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1326
1327 if (STRING_MULTIBYTE (string))
1328 *newpos = string_pos_nchars_ahead (pos, string,
1329 CHARPOS (*newpos) - CHARPOS (pos));
1330 else
1331 BYTEPOS (*newpos) = CHARPOS (*newpos);
1332 }
1333
1334
1335 \f
1336 /***********************************************************************
1337 Lisp form evaluation
1338 ***********************************************************************/
1339
1340 /* Error handler for safe_eval and safe_call. */
1341
1342 static Lisp_Object
1343 safe_eval_handler (arg)
1344 Lisp_Object arg;
1345 {
1346 add_to_log ("Error during redisplay: %s", arg, Qnil);
1347 return Qnil;
1348 }
1349
1350
1351 /* Evaluate SEXPR and return the result, or nil if something went
1352 wrong. Prevent redisplay during the evaluation. */
1353
1354 Lisp_Object
1355 safe_eval (sexpr)
1356 Lisp_Object sexpr;
1357 {
1358 Lisp_Object val;
1359
1360 if (inhibit_eval_during_redisplay)
1361 val = Qnil;
1362 else
1363 {
1364 int count = SPECPDL_INDEX ();
1365 struct gcpro gcpro1;
1366
1367 GCPRO1 (sexpr);
1368 specbind (Qinhibit_redisplay, Qt);
1369 /* Use Qt to ensure debugger does not run,
1370 so there is no possibility of wanting to redisplay. */
1371 val = internal_condition_case_1 (Feval, sexpr, Qt,
1372 safe_eval_handler);
1373 UNGCPRO;
1374 val = unbind_to (count, val);
1375 }
1376
1377 return val;
1378 }
1379
1380
1381 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1382 Return the result, or nil if something went wrong. Prevent
1383 redisplay during the evaluation. */
1384
1385 Lisp_Object
1386 safe_call (nargs, args)
1387 int nargs;
1388 Lisp_Object *args;
1389 {
1390 Lisp_Object val;
1391
1392 if (inhibit_eval_during_redisplay)
1393 val = Qnil;
1394 else
1395 {
1396 int count = SPECPDL_INDEX ();
1397 struct gcpro gcpro1;
1398
1399 GCPRO1 (args[0]);
1400 gcpro1.nvars = nargs;
1401 specbind (Qinhibit_redisplay, Qt);
1402 /* Use Qt to ensure debugger does not run,
1403 so there is no possibility of wanting to redisplay. */
1404 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1405 safe_eval_handler);
1406 UNGCPRO;
1407 val = unbind_to (count, val);
1408 }
1409
1410 return val;
1411 }
1412
1413
1414 /* Call function FN with one argument ARG.
1415 Return the result, or nil if something went wrong. */
1416
1417 Lisp_Object
1418 safe_call1 (fn, arg)
1419 Lisp_Object fn, arg;
1420 {
1421 Lisp_Object args[2];
1422 args[0] = fn;
1423 args[1] = arg;
1424 return safe_call (2, args);
1425 }
1426
1427
1428 \f
1429 /***********************************************************************
1430 Debugging
1431 ***********************************************************************/
1432
1433 #if 0
1434
1435 /* Define CHECK_IT to perform sanity checks on iterators.
1436 This is for debugging. It is too slow to do unconditionally. */
1437
1438 static void
1439 check_it (it)
1440 struct it *it;
1441 {
1442 if (it->method == next_element_from_string)
1443 {
1444 xassert (STRINGP (it->string));
1445 xassert (IT_STRING_CHARPOS (*it) >= 0);
1446 }
1447 else if (it->method == next_element_from_buffer)
1448 {
1449 /* Check that character and byte positions agree. */
1450 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1451 }
1452
1453 if (it->dpvec)
1454 xassert (it->current.dpvec_index >= 0);
1455 else
1456 xassert (it->current.dpvec_index < 0);
1457 }
1458
1459 #define CHECK_IT(IT) check_it ((IT))
1460
1461 #else /* not 0 */
1462
1463 #define CHECK_IT(IT) (void) 0
1464
1465 #endif /* not 0 */
1466
1467
1468 #if GLYPH_DEBUG
1469
1470 /* Check that the window end of window W is what we expect it
1471 to be---the last row in the current matrix displaying text. */
1472
1473 static void
1474 check_window_end (w)
1475 struct window *w;
1476 {
1477 if (!MINI_WINDOW_P (w)
1478 && !NILP (w->window_end_valid))
1479 {
1480 struct glyph_row *row;
1481 xassert ((row = MATRIX_ROW (w->current_matrix,
1482 XFASTINT (w->window_end_vpos)),
1483 !row->enabled_p
1484 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1485 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1486 }
1487 }
1488
1489 #define CHECK_WINDOW_END(W) check_window_end ((W))
1490
1491 #else /* not GLYPH_DEBUG */
1492
1493 #define CHECK_WINDOW_END(W) (void) 0
1494
1495 #endif /* not GLYPH_DEBUG */
1496
1497
1498 \f
1499 /***********************************************************************
1500 Iterator initialization
1501 ***********************************************************************/
1502
1503 /* Initialize IT for displaying current_buffer in window W, starting
1504 at character position CHARPOS. CHARPOS < 0 means that no buffer
1505 position is specified which is useful when the iterator is assigned
1506 a position later. BYTEPOS is the byte position corresponding to
1507 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1508
1509 If ROW is not null, calls to produce_glyphs with IT as parameter
1510 will produce glyphs in that row.
1511
1512 BASE_FACE_ID is the id of a base face to use. It must be one of
1513 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1514 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1515 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1516
1517 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1518 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1519 will be initialized to use the corresponding mode line glyph row of
1520 the desired matrix of W. */
1521
1522 void
1523 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1524 struct it *it;
1525 struct window *w;
1526 int charpos, bytepos;
1527 struct glyph_row *row;
1528 enum face_id base_face_id;
1529 {
1530 int highlight_region_p;
1531
1532 /* Some precondition checks. */
1533 xassert (w != NULL && it != NULL);
1534 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1535 && charpos <= ZV));
1536
1537 /* If face attributes have been changed since the last redisplay,
1538 free realized faces now because they depend on face definitions
1539 that might have changed. Don't free faces while there might be
1540 desired matrices pending which reference these faces. */
1541 if (face_change_count && !inhibit_free_realized_faces)
1542 {
1543 face_change_count = 0;
1544 free_all_realized_faces (Qnil);
1545 }
1546
1547 /* Use one of the mode line rows of W's desired matrix if
1548 appropriate. */
1549 if (row == NULL)
1550 {
1551 if (base_face_id == MODE_LINE_FACE_ID
1552 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1553 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1554 else if (base_face_id == HEADER_LINE_FACE_ID)
1555 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1556 }
1557
1558 /* Clear IT. */
1559 bzero (it, sizeof *it);
1560 it->current.overlay_string_index = -1;
1561 it->current.dpvec_index = -1;
1562 it->base_face_id = base_face_id;
1563
1564 /* The window in which we iterate over current_buffer: */
1565 XSETWINDOW (it->window, w);
1566 it->w = w;
1567 it->f = XFRAME (w->frame);
1568
1569 /* Extra space between lines (on window systems only). */
1570 if (base_face_id == DEFAULT_FACE_ID
1571 && FRAME_WINDOW_P (it->f))
1572 {
1573 if (NATNUMP (current_buffer->extra_line_spacing))
1574 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1575 else if (it->f->extra_line_spacing > 0)
1576 it->extra_line_spacing = it->f->extra_line_spacing;
1577 }
1578
1579 /* If realized faces have been removed, e.g. because of face
1580 attribute changes of named faces, recompute them. When running
1581 in batch mode, the face cache of Vterminal_frame is null. If
1582 we happen to get called, make a dummy face cache. */
1583 if (
1584 #ifndef WINDOWSNT
1585 noninteractive &&
1586 #endif
1587 FRAME_FACE_CACHE (it->f) == NULL)
1588 init_frame_faces (it->f);
1589 if (FRAME_FACE_CACHE (it->f)->used == 0)
1590 recompute_basic_faces (it->f);
1591
1592 /* Current value of the `space-width', and 'height' properties. */
1593 it->space_width = Qnil;
1594 it->font_height = Qnil;
1595
1596 /* Are control characters displayed as `^C'? */
1597 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1598
1599 /* -1 means everything between a CR and the following line end
1600 is invisible. >0 means lines indented more than this value are
1601 invisible. */
1602 it->selective = (INTEGERP (current_buffer->selective_display)
1603 ? XFASTINT (current_buffer->selective_display)
1604 : (!NILP (current_buffer->selective_display)
1605 ? -1 : 0));
1606 it->selective_display_ellipsis_p
1607 = !NILP (current_buffer->selective_display_ellipses);
1608
1609 /* Display table to use. */
1610 it->dp = window_display_table (w);
1611
1612 /* Are multibyte characters enabled in current_buffer? */
1613 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1614
1615 /* Non-zero if we should highlight the region. */
1616 highlight_region_p
1617 = (!NILP (Vtransient_mark_mode)
1618 && !NILP (current_buffer->mark_active)
1619 && XMARKER (current_buffer->mark)->buffer != 0);
1620
1621 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1622 start and end of a visible region in window IT->w. Set both to
1623 -1 to indicate no region. */
1624 if (highlight_region_p
1625 /* Maybe highlight only in selected window. */
1626 && (/* Either show region everywhere. */
1627 highlight_nonselected_windows
1628 /* Or show region in the selected window. */
1629 || w == XWINDOW (selected_window)
1630 /* Or show the region if we are in the mini-buffer and W is
1631 the window the mini-buffer refers to. */
1632 || (MINI_WINDOW_P (XWINDOW (selected_window))
1633 && WINDOWP (minibuf_selected_window)
1634 && w == XWINDOW (minibuf_selected_window))))
1635 {
1636 int charpos = marker_position (current_buffer->mark);
1637 it->region_beg_charpos = min (PT, charpos);
1638 it->region_end_charpos = max (PT, charpos);
1639 }
1640 else
1641 it->region_beg_charpos = it->region_end_charpos = -1;
1642
1643 /* Get the position at which the redisplay_end_trigger hook should
1644 be run, if it is to be run at all. */
1645 if (MARKERP (w->redisplay_end_trigger)
1646 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1647 it->redisplay_end_trigger_charpos
1648 = marker_position (w->redisplay_end_trigger);
1649 else if (INTEGERP (w->redisplay_end_trigger))
1650 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1651
1652 /* Correct bogus values of tab_width. */
1653 it->tab_width = XINT (current_buffer->tab_width);
1654 if (it->tab_width <= 0 || it->tab_width > 1000)
1655 it->tab_width = 8;
1656
1657 /* Are lines in the display truncated? */
1658 it->truncate_lines_p
1659 = (base_face_id != DEFAULT_FACE_ID
1660 || XINT (it->w->hscroll)
1661 || (truncate_partial_width_windows
1662 && !WINDOW_FULL_WIDTH_P (it->w))
1663 || !NILP (current_buffer->truncate_lines));
1664
1665 /* Get dimensions of truncation and continuation glyphs. These are
1666 displayed as fringe bitmaps under X, so we don't need them for such
1667 frames. */
1668 if (!FRAME_WINDOW_P (it->f))
1669 {
1670 if (it->truncate_lines_p)
1671 {
1672 /* We will need the truncation glyph. */
1673 xassert (it->glyph_row == NULL);
1674 produce_special_glyphs (it, IT_TRUNCATION);
1675 it->truncation_pixel_width = it->pixel_width;
1676 }
1677 else
1678 {
1679 /* We will need the continuation glyph. */
1680 xassert (it->glyph_row == NULL);
1681 produce_special_glyphs (it, IT_CONTINUATION);
1682 it->continuation_pixel_width = it->pixel_width;
1683 }
1684
1685 /* Reset these values to zero because the produce_special_glyphs
1686 above has changed them. */
1687 it->pixel_width = it->ascent = it->descent = 0;
1688 it->phys_ascent = it->phys_descent = 0;
1689 }
1690
1691 /* Set this after getting the dimensions of truncation and
1692 continuation glyphs, so that we don't produce glyphs when calling
1693 produce_special_glyphs, above. */
1694 it->glyph_row = row;
1695 it->area = TEXT_AREA;
1696
1697 /* Get the dimensions of the display area. The display area
1698 consists of the visible window area plus a horizontally scrolled
1699 part to the left of the window. All x-values are relative to the
1700 start of this total display area. */
1701 if (base_face_id != DEFAULT_FACE_ID)
1702 {
1703 /* Mode lines, menu bar in terminal frames. */
1704 it->first_visible_x = 0;
1705 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1706 }
1707 else
1708 {
1709 it->first_visible_x
1710 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1711 it->last_visible_x = (it->first_visible_x
1712 + window_box_width (w, TEXT_AREA));
1713
1714 /* If we truncate lines, leave room for the truncator glyph(s) at
1715 the right margin. Otherwise, leave room for the continuation
1716 glyph(s). Truncation and continuation glyphs are not inserted
1717 for window-based redisplay. */
1718 if (!FRAME_WINDOW_P (it->f))
1719 {
1720 if (it->truncate_lines_p)
1721 it->last_visible_x -= it->truncation_pixel_width;
1722 else
1723 it->last_visible_x -= it->continuation_pixel_width;
1724 }
1725
1726 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1727 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1728 }
1729
1730 /* Leave room for a border glyph. */
1731 if (!FRAME_WINDOW_P (it->f)
1732 && !WINDOW_RIGHTMOST_P (it->w))
1733 it->last_visible_x -= 1;
1734
1735 it->last_visible_y = window_text_bottom_y (w);
1736
1737 /* For mode lines and alike, arrange for the first glyph having a
1738 left box line if the face specifies a box. */
1739 if (base_face_id != DEFAULT_FACE_ID)
1740 {
1741 struct face *face;
1742
1743 it->face_id = base_face_id;
1744
1745 /* If we have a boxed mode line, make the first character appear
1746 with a left box line. */
1747 face = FACE_FROM_ID (it->f, base_face_id);
1748 if (face->box != FACE_NO_BOX)
1749 it->start_of_box_run_p = 1;
1750 }
1751
1752 /* If a buffer position was specified, set the iterator there,
1753 getting overlays and face properties from that position. */
1754 if (charpos >= BUF_BEG (current_buffer))
1755 {
1756 it->end_charpos = ZV;
1757 it->face_id = -1;
1758 IT_CHARPOS (*it) = charpos;
1759
1760 /* Compute byte position if not specified. */
1761 if (bytepos < charpos)
1762 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1763 else
1764 IT_BYTEPOS (*it) = bytepos;
1765
1766 /* Compute faces etc. */
1767 reseat (it, it->current.pos, 1);
1768 }
1769
1770 CHECK_IT (it);
1771 }
1772
1773
1774 /* Initialize IT for the display of window W with window start POS. */
1775
1776 void
1777 start_display (it, w, pos)
1778 struct it *it;
1779 struct window *w;
1780 struct text_pos pos;
1781 {
1782 struct glyph_row *row;
1783 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1784
1785 row = w->desired_matrix->rows + first_vpos;
1786 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1787
1788 if (!it->truncate_lines_p)
1789 {
1790 int start_at_line_beg_p;
1791 int first_y = it->current_y;
1792
1793 /* If window start is not at a line start, skip forward to POS to
1794 get the correct continuation lines width. */
1795 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1796 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1797 if (!start_at_line_beg_p)
1798 {
1799 int new_x;
1800
1801 reseat_at_previous_visible_line_start (it);
1802 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1803
1804 new_x = it->current_x + it->pixel_width;
1805
1806 /* If lines are continued, this line may end in the middle
1807 of a multi-glyph character (e.g. a control character
1808 displayed as \003, or in the middle of an overlay
1809 string). In this case move_it_to above will not have
1810 taken us to the start of the continuation line but to the
1811 end of the continued line. */
1812 if (it->current_x > 0
1813 && !it->truncate_lines_p /* Lines are continued. */
1814 && (/* And glyph doesn't fit on the line. */
1815 new_x > it->last_visible_x
1816 /* Or it fits exactly and we're on a window
1817 system frame. */
1818 || (new_x == it->last_visible_x
1819 && FRAME_WINDOW_P (it->f))))
1820 {
1821 if (it->current.dpvec_index >= 0
1822 || it->current.overlay_string_index >= 0)
1823 {
1824 set_iterator_to_next (it, 1);
1825 move_it_in_display_line_to (it, -1, -1, 0);
1826 }
1827
1828 it->continuation_lines_width += it->current_x;
1829 }
1830
1831 /* We're starting a new display line, not affected by the
1832 height of the continued line, so clear the appropriate
1833 fields in the iterator structure. */
1834 it->max_ascent = it->max_descent = 0;
1835 it->max_phys_ascent = it->max_phys_descent = 0;
1836
1837 it->current_y = first_y;
1838 it->vpos = 0;
1839 it->current_x = it->hpos = 0;
1840 }
1841 }
1842
1843 #if 0 /* Don't assert the following because start_display is sometimes
1844 called intentionally with a window start that is not at a
1845 line start. Please leave this code in as a comment. */
1846
1847 /* Window start should be on a line start, now. */
1848 xassert (it->continuation_lines_width
1849 || IT_CHARPOS (it) == BEGV
1850 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1851 #endif /* 0 */
1852 }
1853
1854
1855 /* Return 1 if POS is a position in ellipses displayed for invisible
1856 text. W is the window we display, for text property lookup. */
1857
1858 static int
1859 in_ellipses_for_invisible_text_p (pos, w)
1860 struct display_pos *pos;
1861 struct window *w;
1862 {
1863 Lisp_Object prop, window;
1864 int ellipses_p = 0;
1865 int charpos = CHARPOS (pos->pos);
1866
1867 /* If POS specifies a position in a display vector, this might
1868 be for an ellipsis displayed for invisible text. We won't
1869 get the iterator set up for delivering that ellipsis unless
1870 we make sure that it gets aware of the invisible text. */
1871 if (pos->dpvec_index >= 0
1872 && pos->overlay_string_index < 0
1873 && CHARPOS (pos->string_pos) < 0
1874 && charpos > BEGV
1875 && (XSETWINDOW (window, w),
1876 prop = Fget_char_property (make_number (charpos),
1877 Qinvisible, window),
1878 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1879 {
1880 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1881 window);
1882 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
1883 }
1884
1885 return ellipses_p;
1886 }
1887
1888
1889 /* Initialize IT for stepping through current_buffer in window W,
1890 starting at position POS that includes overlay string and display
1891 vector/ control character translation position information. Value
1892 is zero if there are overlay strings with newlines at POS. */
1893
1894 static int
1895 init_from_display_pos (it, w, pos)
1896 struct it *it;
1897 struct window *w;
1898 struct display_pos *pos;
1899 {
1900 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1901 int i, overlay_strings_with_newlines = 0;
1902
1903 /* If POS specifies a position in a display vector, this might
1904 be for an ellipsis displayed for invisible text. We won't
1905 get the iterator set up for delivering that ellipsis unless
1906 we make sure that it gets aware of the invisible text. */
1907 if (in_ellipses_for_invisible_text_p (pos, w))
1908 {
1909 --charpos;
1910 bytepos = 0;
1911 }
1912
1913 /* Keep in mind: the call to reseat in init_iterator skips invisible
1914 text, so we might end up at a position different from POS. This
1915 is only a problem when POS is a row start after a newline and an
1916 overlay starts there with an after-string, and the overlay has an
1917 invisible property. Since we don't skip invisible text in
1918 display_line and elsewhere immediately after consuming the
1919 newline before the row start, such a POS will not be in a string,
1920 but the call to init_iterator below will move us to the
1921 after-string. */
1922 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1923
1924 for (i = 0; i < it->n_overlay_strings; ++i)
1925 {
1926 const char *s = SDATA (it->overlay_strings[i]);
1927 const char *e = s + SBYTES (it->overlay_strings[i]);
1928
1929 while (s < e && *s != '\n')
1930 ++s;
1931
1932 if (s < e)
1933 {
1934 overlay_strings_with_newlines = 1;
1935 break;
1936 }
1937 }
1938
1939 /* If position is within an overlay string, set up IT to the right
1940 overlay string. */
1941 if (pos->overlay_string_index >= 0)
1942 {
1943 int relative_index;
1944
1945 /* If the first overlay string happens to have a `display'
1946 property for an image, the iterator will be set up for that
1947 image, and we have to undo that setup first before we can
1948 correct the overlay string index. */
1949 if (it->method == next_element_from_image)
1950 pop_it (it);
1951
1952 /* We already have the first chunk of overlay strings in
1953 IT->overlay_strings. Load more until the one for
1954 pos->overlay_string_index is in IT->overlay_strings. */
1955 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1956 {
1957 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1958 it->current.overlay_string_index = 0;
1959 while (n--)
1960 {
1961 load_overlay_strings (it, 0);
1962 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1963 }
1964 }
1965
1966 it->current.overlay_string_index = pos->overlay_string_index;
1967 relative_index = (it->current.overlay_string_index
1968 % OVERLAY_STRING_CHUNK_SIZE);
1969 it->string = it->overlay_strings[relative_index];
1970 xassert (STRINGP (it->string));
1971 it->current.string_pos = pos->string_pos;
1972 it->method = next_element_from_string;
1973 }
1974
1975 #if 0 /* This is bogus because POS not having an overlay string
1976 position does not mean it's after the string. Example: A
1977 line starting with a before-string and initialization of IT
1978 to the previous row's end position. */
1979 else if (it->current.overlay_string_index >= 0)
1980 {
1981 /* If POS says we're already after an overlay string ending at
1982 POS, make sure to pop the iterator because it will be in
1983 front of that overlay string. When POS is ZV, we've thereby
1984 also ``processed'' overlay strings at ZV. */
1985 while (it->sp)
1986 pop_it (it);
1987 it->current.overlay_string_index = -1;
1988 it->method = next_element_from_buffer;
1989 if (CHARPOS (pos->pos) == ZV)
1990 it->overlay_strings_at_end_processed_p = 1;
1991 }
1992 #endif /* 0 */
1993
1994 if (CHARPOS (pos->string_pos) >= 0)
1995 {
1996 /* Recorded position is not in an overlay string, but in another
1997 string. This can only be a string from a `display' property.
1998 IT should already be filled with that string. */
1999 it->current.string_pos = pos->string_pos;
2000 xassert (STRINGP (it->string));
2001 }
2002
2003 /* Restore position in display vector translations, control
2004 character translations or ellipses. */
2005 if (pos->dpvec_index >= 0)
2006 {
2007 if (it->dpvec == NULL)
2008 get_next_display_element (it);
2009 xassert (it->dpvec && it->current.dpvec_index == 0);
2010 it->current.dpvec_index = pos->dpvec_index;
2011 }
2012
2013 CHECK_IT (it);
2014 return !overlay_strings_with_newlines;
2015 }
2016
2017
2018 /* Initialize IT for stepping through current_buffer in window W
2019 starting at ROW->start. */
2020
2021 static void
2022 init_to_row_start (it, w, row)
2023 struct it *it;
2024 struct window *w;
2025 struct glyph_row *row;
2026 {
2027 init_from_display_pos (it, w, &row->start);
2028 it->continuation_lines_width = row->continuation_lines_width;
2029 CHECK_IT (it);
2030 }
2031
2032
2033 /* Initialize IT for stepping through current_buffer in window W
2034 starting in the line following ROW, i.e. starting at ROW->end.
2035 Value is zero if there are overlay strings with newlines at ROW's
2036 end position. */
2037
2038 static int
2039 init_to_row_end (it, w, row)
2040 struct it *it;
2041 struct window *w;
2042 struct glyph_row *row;
2043 {
2044 int success = 0;
2045
2046 if (init_from_display_pos (it, w, &row->end))
2047 {
2048 if (row->continued_p)
2049 it->continuation_lines_width
2050 = row->continuation_lines_width + row->pixel_width;
2051 CHECK_IT (it);
2052 success = 1;
2053 }
2054
2055 return success;
2056 }
2057
2058
2059
2060 \f
2061 /***********************************************************************
2062 Text properties
2063 ***********************************************************************/
2064
2065 /* Called when IT reaches IT->stop_charpos. Handle text property and
2066 overlay changes. Set IT->stop_charpos to the next position where
2067 to stop. */
2068
2069 static void
2070 handle_stop (it)
2071 struct it *it;
2072 {
2073 enum prop_handled handled;
2074 int handle_overlay_change_p = 1;
2075 struct props *p;
2076
2077 it->dpvec = NULL;
2078 it->current.dpvec_index = -1;
2079
2080 do
2081 {
2082 handled = HANDLED_NORMALLY;
2083
2084 /* Call text property handlers. */
2085 for (p = it_props; p->handler; ++p)
2086 {
2087 handled = p->handler (it);
2088
2089 if (handled == HANDLED_RECOMPUTE_PROPS)
2090 break;
2091 else if (handled == HANDLED_RETURN)
2092 return;
2093 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2094 handle_overlay_change_p = 0;
2095 }
2096
2097 if (handled != HANDLED_RECOMPUTE_PROPS)
2098 {
2099 /* Don't check for overlay strings below when set to deliver
2100 characters from a display vector. */
2101 if (it->method == next_element_from_display_vector)
2102 handle_overlay_change_p = 0;
2103
2104 /* Handle overlay changes. */
2105 if (handle_overlay_change_p)
2106 handled = handle_overlay_change (it);
2107
2108 /* Determine where to stop next. */
2109 if (handled == HANDLED_NORMALLY)
2110 compute_stop_pos (it);
2111 }
2112 }
2113 while (handled == HANDLED_RECOMPUTE_PROPS);
2114 }
2115
2116
2117 /* Compute IT->stop_charpos from text property and overlay change
2118 information for IT's current position. */
2119
2120 static void
2121 compute_stop_pos (it)
2122 struct it *it;
2123 {
2124 register INTERVAL iv, next_iv;
2125 Lisp_Object object, limit, position;
2126
2127 /* If nowhere else, stop at the end. */
2128 it->stop_charpos = it->end_charpos;
2129
2130 if (STRINGP (it->string))
2131 {
2132 /* Strings are usually short, so don't limit the search for
2133 properties. */
2134 object = it->string;
2135 limit = Qnil;
2136 position = make_number (IT_STRING_CHARPOS (*it));
2137 }
2138 else
2139 {
2140 int charpos;
2141
2142 /* If next overlay change is in front of the current stop pos
2143 (which is IT->end_charpos), stop there. Note: value of
2144 next_overlay_change is point-max if no overlay change
2145 follows. */
2146 charpos = next_overlay_change (IT_CHARPOS (*it));
2147 if (charpos < it->stop_charpos)
2148 it->stop_charpos = charpos;
2149
2150 /* If showing the region, we have to stop at the region
2151 start or end because the face might change there. */
2152 if (it->region_beg_charpos > 0)
2153 {
2154 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2155 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2156 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2157 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2158 }
2159
2160 /* Set up variables for computing the stop position from text
2161 property changes. */
2162 XSETBUFFER (object, current_buffer);
2163 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2164 position = make_number (IT_CHARPOS (*it));
2165
2166 }
2167
2168 /* Get the interval containing IT's position. Value is a null
2169 interval if there isn't such an interval. */
2170 iv = validate_interval_range (object, &position, &position, 0);
2171 if (!NULL_INTERVAL_P (iv))
2172 {
2173 Lisp_Object values_here[LAST_PROP_IDX];
2174 struct props *p;
2175
2176 /* Get properties here. */
2177 for (p = it_props; p->handler; ++p)
2178 values_here[p->idx] = textget (iv->plist, *p->name);
2179
2180 /* Look for an interval following iv that has different
2181 properties. */
2182 for (next_iv = next_interval (iv);
2183 (!NULL_INTERVAL_P (next_iv)
2184 && (NILP (limit)
2185 || XFASTINT (limit) > next_iv->position));
2186 next_iv = next_interval (next_iv))
2187 {
2188 for (p = it_props; p->handler; ++p)
2189 {
2190 Lisp_Object new_value;
2191
2192 new_value = textget (next_iv->plist, *p->name);
2193 if (!EQ (values_here[p->idx], new_value))
2194 break;
2195 }
2196
2197 if (p->handler)
2198 break;
2199 }
2200
2201 if (!NULL_INTERVAL_P (next_iv))
2202 {
2203 if (INTEGERP (limit)
2204 && next_iv->position >= XFASTINT (limit))
2205 /* No text property change up to limit. */
2206 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2207 else
2208 /* Text properties change in next_iv. */
2209 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2210 }
2211 }
2212
2213 xassert (STRINGP (it->string)
2214 || (it->stop_charpos >= BEGV
2215 && it->stop_charpos >= IT_CHARPOS (*it)));
2216 }
2217
2218
2219 /* Return the position of the next overlay change after POS in
2220 current_buffer. Value is point-max if no overlay change
2221 follows. This is like `next-overlay-change' but doesn't use
2222 xmalloc. */
2223
2224 static int
2225 next_overlay_change (pos)
2226 int pos;
2227 {
2228 int noverlays;
2229 int endpos;
2230 Lisp_Object *overlays;
2231 int len;
2232 int i;
2233
2234 /* Get all overlays at the given position. */
2235 len = 10;
2236 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2237 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2238 if (noverlays > len)
2239 {
2240 len = noverlays;
2241 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2242 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2243 }
2244
2245 /* If any of these overlays ends before endpos,
2246 use its ending point instead. */
2247 for (i = 0; i < noverlays; ++i)
2248 {
2249 Lisp_Object oend;
2250 int oendpos;
2251
2252 oend = OVERLAY_END (overlays[i]);
2253 oendpos = OVERLAY_POSITION (oend);
2254 endpos = min (endpos, oendpos);
2255 }
2256
2257 return endpos;
2258 }
2259
2260
2261 \f
2262 /***********************************************************************
2263 Fontification
2264 ***********************************************************************/
2265
2266 /* Handle changes in the `fontified' property of the current buffer by
2267 calling hook functions from Qfontification_functions to fontify
2268 regions of text. */
2269
2270 static enum prop_handled
2271 handle_fontified_prop (it)
2272 struct it *it;
2273 {
2274 Lisp_Object prop, pos;
2275 enum prop_handled handled = HANDLED_NORMALLY;
2276
2277 /* Get the value of the `fontified' property at IT's current buffer
2278 position. (The `fontified' property doesn't have a special
2279 meaning in strings.) If the value is nil, call functions from
2280 Qfontification_functions. */
2281 if (!STRINGP (it->string)
2282 && it->s == NULL
2283 && !NILP (Vfontification_functions)
2284 && !NILP (Vrun_hooks)
2285 && (pos = make_number (IT_CHARPOS (*it)),
2286 prop = Fget_char_property (pos, Qfontified, Qnil),
2287 NILP (prop)))
2288 {
2289 int count = SPECPDL_INDEX ();
2290 Lisp_Object val;
2291
2292 val = Vfontification_functions;
2293 specbind (Qfontification_functions, Qnil);
2294
2295 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2296 safe_call1 (val, pos);
2297 else
2298 {
2299 Lisp_Object globals, fn;
2300 struct gcpro gcpro1, gcpro2;
2301
2302 globals = Qnil;
2303 GCPRO2 (val, globals);
2304
2305 for (; CONSP (val); val = XCDR (val))
2306 {
2307 fn = XCAR (val);
2308
2309 if (EQ (fn, Qt))
2310 {
2311 /* A value of t indicates this hook has a local
2312 binding; it means to run the global binding too.
2313 In a global value, t should not occur. If it
2314 does, we must ignore it to avoid an endless
2315 loop. */
2316 for (globals = Fdefault_value (Qfontification_functions);
2317 CONSP (globals);
2318 globals = XCDR (globals))
2319 {
2320 fn = XCAR (globals);
2321 if (!EQ (fn, Qt))
2322 safe_call1 (fn, pos);
2323 }
2324 }
2325 else
2326 safe_call1 (fn, pos);
2327 }
2328
2329 UNGCPRO;
2330 }
2331
2332 unbind_to (count, Qnil);
2333
2334 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2335 something. This avoids an endless loop if they failed to
2336 fontify the text for which reason ever. */
2337 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2338 handled = HANDLED_RECOMPUTE_PROPS;
2339 }
2340
2341 return handled;
2342 }
2343
2344
2345 \f
2346 /***********************************************************************
2347 Faces
2348 ***********************************************************************/
2349
2350 /* Set up iterator IT from face properties at its current position.
2351 Called from handle_stop. */
2352
2353 static enum prop_handled
2354 handle_face_prop (it)
2355 struct it *it;
2356 {
2357 int new_face_id, next_stop;
2358
2359 if (!STRINGP (it->string))
2360 {
2361 new_face_id
2362 = face_at_buffer_position (it->w,
2363 IT_CHARPOS (*it),
2364 it->region_beg_charpos,
2365 it->region_end_charpos,
2366 &next_stop,
2367 (IT_CHARPOS (*it)
2368 + TEXT_PROP_DISTANCE_LIMIT),
2369 0);
2370
2371 /* Is this a start of a run of characters with box face?
2372 Caveat: this can be called for a freshly initialized
2373 iterator; face_id is -1 in this case. We know that the new
2374 face will not change until limit, i.e. if the new face has a
2375 box, all characters up to limit will have one. But, as
2376 usual, we don't know whether limit 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
2381 /* If new face has a box but old face has not, this is
2382 the start of a run of characters with box, i.e. it has
2383 a shadow on the left side. The value of face_id of the
2384 iterator will be -1 if this is the initial call that gets
2385 the face. In this case, we have to look in front of IT's
2386 position and see whether there is a face != new_face_id. */
2387 it->start_of_box_run_p
2388 = (new_face->box != FACE_NO_BOX
2389 && (it->face_id >= 0
2390 || IT_CHARPOS (*it) == BEG
2391 || new_face_id != face_before_it_pos (it)));
2392 it->face_box_p = new_face->box != FACE_NO_BOX;
2393 }
2394 }
2395 else
2396 {
2397 int base_face_id, bufpos;
2398
2399 if (it->current.overlay_string_index >= 0)
2400 bufpos = IT_CHARPOS (*it);
2401 else
2402 bufpos = 0;
2403
2404 /* For strings from a buffer, i.e. overlay strings or strings
2405 from a `display' property, use the face at IT's current
2406 buffer position as the base face to merge with, so that
2407 overlay strings appear in the same face as surrounding
2408 text, unless they specify their own faces. */
2409 base_face_id = underlying_face_id (it);
2410
2411 new_face_id = face_at_string_position (it->w,
2412 it->string,
2413 IT_STRING_CHARPOS (*it),
2414 bufpos,
2415 it->region_beg_charpos,
2416 it->region_end_charpos,
2417 &next_stop,
2418 base_face_id, 0);
2419
2420 #if 0 /* This shouldn't be neccessary. Let's check it. */
2421 /* If IT is used to display a mode line we would really like to
2422 use the mode line face instead of the frame's default face. */
2423 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2424 && new_face_id == DEFAULT_FACE_ID)
2425 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2426 #endif
2427
2428 /* Is this a start of a run of characters with box? Caveat:
2429 this can be called for a freshly allocated iterator; face_id
2430 is -1 is this case. We know that the new face will not
2431 change until the next check pos, i.e. if the new face has a
2432 box, all characters up to that position will have a
2433 box. But, as usual, we don't know whether that position
2434 is really the end. */
2435 if (new_face_id != it->face_id)
2436 {
2437 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2438 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2439
2440 /* If new face has a box but old face hasn't, this is the
2441 start of a run of characters with box, i.e. it has a
2442 shadow on the left side. */
2443 it->start_of_box_run_p
2444 = new_face->box && (old_face == NULL || !old_face->box);
2445 it->face_box_p = new_face->box != FACE_NO_BOX;
2446 }
2447 }
2448
2449 it->face_id = new_face_id;
2450 return HANDLED_NORMALLY;
2451 }
2452
2453
2454 /* Return the ID of the face ``underlying'' IT's current position,
2455 which is in a string. If the iterator is associated with a
2456 buffer, return the face at IT's current buffer position.
2457 Otherwise, use the iterator's base_face_id. */
2458
2459 static int
2460 underlying_face_id (it)
2461 struct it *it;
2462 {
2463 int face_id = it->base_face_id, i;
2464
2465 xassert (STRINGP (it->string));
2466
2467 for (i = it->sp - 1; i >= 0; --i)
2468 if (NILP (it->stack[i].string))
2469 face_id = it->stack[i].face_id;
2470
2471 return face_id;
2472 }
2473
2474
2475 /* Compute the face one character before or after the current position
2476 of IT. BEFORE_P non-zero means get the face in front of IT's
2477 position. Value is the id of the face. */
2478
2479 static int
2480 face_before_or_after_it_pos (it, before_p)
2481 struct it *it;
2482 int before_p;
2483 {
2484 int face_id, limit;
2485 int next_check_charpos;
2486 struct text_pos pos;
2487
2488 xassert (it->s == NULL);
2489
2490 if (STRINGP (it->string))
2491 {
2492 int bufpos, base_face_id;
2493
2494 /* No face change past the end of the string (for the case
2495 we are padding with spaces). No face change before the
2496 string start. */
2497 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2498 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2499 return it->face_id;
2500
2501 /* Set pos to the position before or after IT's current position. */
2502 if (before_p)
2503 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2504 else
2505 /* For composition, we must check the character after the
2506 composition. */
2507 pos = (it->what == IT_COMPOSITION
2508 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2509 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2510
2511 if (it->current.overlay_string_index >= 0)
2512 bufpos = IT_CHARPOS (*it);
2513 else
2514 bufpos = 0;
2515
2516 base_face_id = underlying_face_id (it);
2517
2518 /* Get the face for ASCII, or unibyte. */
2519 face_id = face_at_string_position (it->w,
2520 it->string,
2521 CHARPOS (pos),
2522 bufpos,
2523 it->region_beg_charpos,
2524 it->region_end_charpos,
2525 &next_check_charpos,
2526 base_face_id, 0);
2527
2528 /* Correct the face for charsets different from ASCII. Do it
2529 for the multibyte case only. The face returned above is
2530 suitable for unibyte text if IT->string is unibyte. */
2531 if (STRING_MULTIBYTE (it->string))
2532 {
2533 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2534 int rest = SBYTES (it->string) - BYTEPOS (pos);
2535 int c, len;
2536 struct face *face = FACE_FROM_ID (it->f, face_id);
2537
2538 c = string_char_and_length (p, rest, &len);
2539 face_id = FACE_FOR_CHAR (it->f, face, c);
2540 }
2541 }
2542 else
2543 {
2544 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2545 || (IT_CHARPOS (*it) <= BEGV && before_p))
2546 return it->face_id;
2547
2548 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2549 pos = it->current.pos;
2550
2551 if (before_p)
2552 DEC_TEXT_POS (pos, it->multibyte_p);
2553 else
2554 {
2555 if (it->what == IT_COMPOSITION)
2556 /* For composition, we must check the position after the
2557 composition. */
2558 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2559 else
2560 INC_TEXT_POS (pos, it->multibyte_p);
2561 }
2562
2563 /* Determine face for CHARSET_ASCII, or unibyte. */
2564 face_id = face_at_buffer_position (it->w,
2565 CHARPOS (pos),
2566 it->region_beg_charpos,
2567 it->region_end_charpos,
2568 &next_check_charpos,
2569 limit, 0);
2570
2571 /* Correct the face for charsets different from ASCII. Do it
2572 for the multibyte case only. The face returned above is
2573 suitable for unibyte text if current_buffer is unibyte. */
2574 if (it->multibyte_p)
2575 {
2576 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
2577 struct face *face = FACE_FROM_ID (it->f, face_id);
2578 face_id = FACE_FOR_CHAR (it->f, face, c);
2579 }
2580 }
2581
2582 return face_id;
2583 }
2584
2585
2586 \f
2587 /***********************************************************************
2588 Invisible text
2589 ***********************************************************************/
2590
2591 /* Set up iterator IT from invisible properties at its current
2592 position. Called from handle_stop. */
2593
2594 static enum prop_handled
2595 handle_invisible_prop (it)
2596 struct it *it;
2597 {
2598 enum prop_handled handled = HANDLED_NORMALLY;
2599
2600 if (STRINGP (it->string))
2601 {
2602 extern Lisp_Object Qinvisible;
2603 Lisp_Object prop, end_charpos, limit, charpos;
2604
2605 /* Get the value of the invisible text property at the
2606 current position. Value will be nil if there is no such
2607 property. */
2608 charpos = make_number (IT_STRING_CHARPOS (*it));
2609 prop = Fget_text_property (charpos, Qinvisible, it->string);
2610
2611 if (!NILP (prop)
2612 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2613 {
2614 handled = HANDLED_RECOMPUTE_PROPS;
2615
2616 /* Get the position at which the next change of the
2617 invisible text property can be found in IT->string.
2618 Value will be nil if the property value is the same for
2619 all the rest of IT->string. */
2620 XSETINT (limit, SCHARS (it->string));
2621 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2622 it->string, limit);
2623
2624 /* Text at current position is invisible. The next
2625 change in the property is at position end_charpos.
2626 Move IT's current position to that position. */
2627 if (INTEGERP (end_charpos)
2628 && XFASTINT (end_charpos) < XFASTINT (limit))
2629 {
2630 struct text_pos old;
2631 old = it->current.string_pos;
2632 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2633 compute_string_pos (&it->current.string_pos, old, it->string);
2634 }
2635 else
2636 {
2637 /* The rest of the string is invisible. If this is an
2638 overlay string, proceed with the next overlay string
2639 or whatever comes and return a character from there. */
2640 if (it->current.overlay_string_index >= 0)
2641 {
2642 next_overlay_string (it);
2643 /* Don't check for overlay strings when we just
2644 finished processing them. */
2645 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2646 }
2647 else
2648 {
2649 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
2650 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
2651 }
2652 }
2653 }
2654 }
2655 else
2656 {
2657 int invis_p, newpos, next_stop, start_charpos;
2658 Lisp_Object pos, prop, overlay;
2659
2660 /* First of all, is there invisible text at this position? */
2661 start_charpos = IT_CHARPOS (*it);
2662 pos = make_number (IT_CHARPOS (*it));
2663 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2664 &overlay);
2665 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2666
2667 /* If we are on invisible text, skip over it. */
2668 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
2669 {
2670 /* Record whether we have to display an ellipsis for the
2671 invisible text. */
2672 int display_ellipsis_p = invis_p == 2;
2673
2674 handled = HANDLED_RECOMPUTE_PROPS;
2675
2676 /* Loop skipping over invisible text. The loop is left at
2677 ZV or with IT on the first char being visible again. */
2678 do
2679 {
2680 /* Try to skip some invisible text. Return value is the
2681 position reached which can be equal to IT's position
2682 if there is nothing invisible here. This skips both
2683 over invisible text properties and overlays with
2684 invisible property. */
2685 newpos = skip_invisible (IT_CHARPOS (*it),
2686 &next_stop, ZV, it->window);
2687
2688 /* If we skipped nothing at all we weren't at invisible
2689 text in the first place. If everything to the end of
2690 the buffer was skipped, end the loop. */
2691 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2692 invis_p = 0;
2693 else
2694 {
2695 /* We skipped some characters but not necessarily
2696 all there are. Check if we ended up on visible
2697 text. Fget_char_property returns the property of
2698 the char before the given position, i.e. if we
2699 get invis_p = 0, this means that the char at
2700 newpos is visible. */
2701 pos = make_number (newpos);
2702 prop = Fget_char_property (pos, Qinvisible, it->window);
2703 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2704 }
2705
2706 /* If we ended up on invisible text, proceed to
2707 skip starting with next_stop. */
2708 if (invis_p)
2709 IT_CHARPOS (*it) = next_stop;
2710 }
2711 while (invis_p);
2712
2713 /* The position newpos is now either ZV or on visible text. */
2714 IT_CHARPOS (*it) = newpos;
2715 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2716
2717 /* If there are before-strings at the start of invisible
2718 text, and the text is invisible because of a text
2719 property, arrange to show before-strings because 20.x did
2720 it that way. (If the text is invisible because of an
2721 overlay property instead of a text property, this is
2722 already handled in the overlay code.) */
2723 if (NILP (overlay)
2724 && get_overlay_strings (it, start_charpos))
2725 {
2726 handled = HANDLED_RECOMPUTE_PROPS;
2727 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2728 }
2729 else if (display_ellipsis_p)
2730 setup_for_ellipsis (it);
2731 }
2732 }
2733
2734 return handled;
2735 }
2736
2737
2738 /* Make iterator IT return `...' next. */
2739
2740 static void
2741 setup_for_ellipsis (it)
2742 struct it *it;
2743 {
2744 if (it->dp
2745 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2746 {
2747 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2748 it->dpvec = v->contents;
2749 it->dpend = v->contents + v->size;
2750 }
2751 else
2752 {
2753 /* Default `...'. */
2754 it->dpvec = default_invis_vector;
2755 it->dpend = default_invis_vector + 3;
2756 }
2757
2758 /* The ellipsis display does not replace the display of the
2759 character at the new position. Indicate this by setting
2760 IT->dpvec_char_len to zero. */
2761 it->dpvec_char_len = 0;
2762
2763 it->current.dpvec_index = 0;
2764 it->method = next_element_from_display_vector;
2765 }
2766
2767
2768 \f
2769 /***********************************************************************
2770 'display' property
2771 ***********************************************************************/
2772
2773 /* Set up iterator IT from `display' property at its current position.
2774 Called from handle_stop. */
2775
2776 static enum prop_handled
2777 handle_display_prop (it)
2778 struct it *it;
2779 {
2780 Lisp_Object prop, object;
2781 struct text_pos *position;
2782 int display_replaced_p = 0;
2783
2784 if (STRINGP (it->string))
2785 {
2786 object = it->string;
2787 position = &it->current.string_pos;
2788 }
2789 else
2790 {
2791 object = it->w->buffer;
2792 position = &it->current.pos;
2793 }
2794
2795 /* Reset those iterator values set from display property values. */
2796 it->font_height = Qnil;
2797 it->space_width = Qnil;
2798 it->voffset = 0;
2799
2800 /* We don't support recursive `display' properties, i.e. string
2801 values that have a string `display' property, that have a string
2802 `display' property etc. */
2803 if (!it->string_from_display_prop_p)
2804 it->area = TEXT_AREA;
2805
2806 prop = Fget_char_property (make_number (position->charpos),
2807 Qdisplay, object);
2808 if (NILP (prop))
2809 return HANDLED_NORMALLY;
2810
2811 if (CONSP (prop)
2812 /* Simple properties. */
2813 && !EQ (XCAR (prop), Qimage)
2814 && !EQ (XCAR (prop), Qspace)
2815 && !EQ (XCAR (prop), Qwhen)
2816 && !EQ (XCAR (prop), Qspace_width)
2817 && !EQ (XCAR (prop), Qheight)
2818 && !EQ (XCAR (prop), Qraise)
2819 /* Marginal area specifications. */
2820 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2821 && !NILP (XCAR (prop)))
2822 {
2823 for (; CONSP (prop); prop = XCDR (prop))
2824 {
2825 if (handle_single_display_prop (it, XCAR (prop), object,
2826 position, display_replaced_p))
2827 display_replaced_p = 1;
2828 }
2829 }
2830 else if (VECTORP (prop))
2831 {
2832 int i;
2833 for (i = 0; i < ASIZE (prop); ++i)
2834 if (handle_single_display_prop (it, AREF (prop, i), object,
2835 position, display_replaced_p))
2836 display_replaced_p = 1;
2837 }
2838 else
2839 {
2840 if (handle_single_display_prop (it, prop, object, position, 0))
2841 display_replaced_p = 1;
2842 }
2843
2844 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2845 }
2846
2847
2848 /* Value is the position of the end of the `display' property starting
2849 at START_POS in OBJECT. */
2850
2851 static struct text_pos
2852 display_prop_end (it, object, start_pos)
2853 struct it *it;
2854 Lisp_Object object;
2855 struct text_pos start_pos;
2856 {
2857 Lisp_Object end;
2858 struct text_pos end_pos;
2859
2860 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2861 Qdisplay, object, Qnil);
2862 CHARPOS (end_pos) = XFASTINT (end);
2863 if (STRINGP (object))
2864 compute_string_pos (&end_pos, start_pos, it->string);
2865 else
2866 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2867
2868 return end_pos;
2869 }
2870
2871
2872 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2873 is the object in which the `display' property was found. *POSITION
2874 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2875 means that we previously saw a display sub-property which already
2876 replaced text display with something else, for example an image;
2877 ignore such properties after the first one has been processed.
2878
2879 If PROP is a `space' or `image' sub-property, set *POSITION to the
2880 end position of the `display' property.
2881
2882 Value is non-zero if something was found which replaces the display
2883 of buffer or string text. */
2884
2885 static int
2886 handle_single_display_prop (it, prop, object, position,
2887 display_replaced_before_p)
2888 struct it *it;
2889 Lisp_Object prop;
2890 Lisp_Object object;
2891 struct text_pos *position;
2892 int display_replaced_before_p;
2893 {
2894 Lisp_Object value;
2895 int replaces_text_display_p = 0;
2896 Lisp_Object form;
2897
2898 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2899 evaluated. If the result is nil, VALUE is ignored. */
2900 form = Qt;
2901 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2902 {
2903 prop = XCDR (prop);
2904 if (!CONSP (prop))
2905 return 0;
2906 form = XCAR (prop);
2907 prop = XCDR (prop);
2908 }
2909
2910 if (!NILP (form) && !EQ (form, Qt))
2911 {
2912 int count = SPECPDL_INDEX ();
2913 struct gcpro gcpro1;
2914
2915 /* Bind `object' to the object having the `display' property, a
2916 buffer or string. Bind `position' to the position in the
2917 object where the property was found, and `buffer-position'
2918 to the current position in the buffer. */
2919 specbind (Qobject, object);
2920 specbind (Qposition, make_number (CHARPOS (*position)));
2921 specbind (Qbuffer_position,
2922 make_number (STRINGP (object)
2923 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2924 GCPRO1 (form);
2925 form = safe_eval (form);
2926 UNGCPRO;
2927 unbind_to (count, Qnil);
2928 }
2929
2930 if (NILP (form))
2931 return 0;
2932
2933 if (CONSP (prop)
2934 && EQ (XCAR (prop), Qheight)
2935 && CONSP (XCDR (prop)))
2936 {
2937 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2938 return 0;
2939
2940 /* `(height HEIGHT)'. */
2941 it->font_height = XCAR (XCDR (prop));
2942 if (!NILP (it->font_height))
2943 {
2944 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2945 int new_height = -1;
2946
2947 if (CONSP (it->font_height)
2948 && (EQ (XCAR (it->font_height), Qplus)
2949 || EQ (XCAR (it->font_height), Qminus))
2950 && CONSP (XCDR (it->font_height))
2951 && INTEGERP (XCAR (XCDR (it->font_height))))
2952 {
2953 /* `(+ N)' or `(- N)' where N is an integer. */
2954 int steps = XINT (XCAR (XCDR (it->font_height)));
2955 if (EQ (XCAR (it->font_height), Qplus))
2956 steps = - steps;
2957 it->face_id = smaller_face (it->f, it->face_id, steps);
2958 }
2959 else if (FUNCTIONP (it->font_height))
2960 {
2961 /* Call function with current height as argument.
2962 Value is the new height. */
2963 Lisp_Object height;
2964 height = safe_call1 (it->font_height,
2965 face->lface[LFACE_HEIGHT_INDEX]);
2966 if (NUMBERP (height))
2967 new_height = XFLOATINT (height);
2968 }
2969 else if (NUMBERP (it->font_height))
2970 {
2971 /* Value is a multiple of the canonical char height. */
2972 struct face *face;
2973
2974 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2975 new_height = (XFLOATINT (it->font_height)
2976 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2977 }
2978 else
2979 {
2980 /* Evaluate IT->font_height with `height' bound to the
2981 current specified height to get the new height. */
2982 Lisp_Object value;
2983 int count = SPECPDL_INDEX ();
2984
2985 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2986 value = safe_eval (it->font_height);
2987 unbind_to (count, Qnil);
2988
2989 if (NUMBERP (value))
2990 new_height = XFLOATINT (value);
2991 }
2992
2993 if (new_height > 0)
2994 it->face_id = face_with_height (it->f, it->face_id, new_height);
2995 }
2996 }
2997 else if (CONSP (prop)
2998 && EQ (XCAR (prop), Qspace_width)
2999 && CONSP (XCDR (prop)))
3000 {
3001 /* `(space_width WIDTH)'. */
3002 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3003 return 0;
3004
3005 value = XCAR (XCDR (prop));
3006 if (NUMBERP (value) && XFLOATINT (value) > 0)
3007 it->space_width = value;
3008 }
3009 else if (CONSP (prop)
3010 && EQ (XCAR (prop), Qraise)
3011 && CONSP (XCDR (prop)))
3012 {
3013 /* `(raise FACTOR)'. */
3014 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3015 return 0;
3016
3017 #ifdef HAVE_WINDOW_SYSTEM
3018 value = XCAR (XCDR (prop));
3019 if (NUMBERP (value))
3020 {
3021 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3022 it->voffset = - (XFLOATINT (value)
3023 * (FONT_HEIGHT (face->font)));
3024 }
3025 #endif /* HAVE_WINDOW_SYSTEM */
3026 }
3027 else if (!it->string_from_display_prop_p)
3028 {
3029 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3030 VALUE) or `((margin nil) VALUE)' or VALUE. */
3031 Lisp_Object location, value;
3032 struct text_pos start_pos;
3033 int valid_p;
3034
3035 /* Characters having this form of property are not displayed, so
3036 we have to find the end of the property. */
3037 start_pos = *position;
3038 *position = display_prop_end (it, object, start_pos);
3039 value = Qnil;
3040
3041 /* Let's stop at the new position and assume that all
3042 text properties change there. */
3043 it->stop_charpos = position->charpos;
3044
3045 location = Qunbound;
3046 if (CONSP (prop) && CONSP (XCAR (prop)))
3047 {
3048 Lisp_Object tem;
3049
3050 value = XCDR (prop);
3051 if (CONSP (value))
3052 value = XCAR (value);
3053
3054 tem = XCAR (prop);
3055 if (EQ (XCAR (tem), Qmargin)
3056 && (tem = XCDR (tem),
3057 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3058 (NILP (tem)
3059 || EQ (tem, Qleft_margin)
3060 || EQ (tem, Qright_margin))))
3061 location = tem;
3062 }
3063
3064 if (EQ (location, Qunbound))
3065 {
3066 location = Qnil;
3067 value = prop;
3068 }
3069
3070 #ifdef HAVE_WINDOW_SYSTEM
3071 if (FRAME_TERMCAP_P (it->f))
3072 valid_p = STRINGP (value);
3073 else
3074 valid_p = (STRINGP (value)
3075 || (CONSP (value) && EQ (XCAR (value), Qspace))
3076 || valid_image_p (value));
3077 #else /* not HAVE_WINDOW_SYSTEM */
3078 valid_p = STRINGP (value);
3079 #endif /* not HAVE_WINDOW_SYSTEM */
3080
3081 if ((EQ (location, Qleft_margin)
3082 || EQ (location, Qright_margin)
3083 || NILP (location))
3084 && valid_p
3085 && !display_replaced_before_p)
3086 {
3087 replaces_text_display_p = 1;
3088
3089 /* Save current settings of IT so that we can restore them
3090 when we are finished with the glyph property value. */
3091 push_it (it);
3092
3093 if (NILP (location))
3094 it->area = TEXT_AREA;
3095 else if (EQ (location, Qleft_margin))
3096 it->area = LEFT_MARGIN_AREA;
3097 else
3098 it->area = RIGHT_MARGIN_AREA;
3099
3100 if (STRINGP (value))
3101 {
3102 it->string = value;
3103 it->multibyte_p = STRING_MULTIBYTE (it->string);
3104 it->current.overlay_string_index = -1;
3105 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3106 it->end_charpos = it->string_nchars = SCHARS (it->string);
3107 it->method = next_element_from_string;
3108 it->stop_charpos = 0;
3109 it->string_from_display_prop_p = 1;
3110 /* Say that we haven't consumed the characters with
3111 `display' property yet. The call to pop_it in
3112 set_iterator_to_next will clean this up. */
3113 *position = start_pos;
3114 }
3115 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3116 {
3117 it->method = next_element_from_stretch;
3118 it->object = value;
3119 it->current.pos = it->position = start_pos;
3120 }
3121 #ifdef HAVE_WINDOW_SYSTEM
3122 else
3123 {
3124 it->what = IT_IMAGE;
3125 it->image_id = lookup_image (it->f, value);
3126 it->position = start_pos;
3127 it->object = NILP (object) ? it->w->buffer : object;
3128 it->method = next_element_from_image;
3129
3130 /* Say that we haven't consumed the characters with
3131 `display' property yet. The call to pop_it in
3132 set_iterator_to_next will clean this up. */
3133 *position = start_pos;
3134 }
3135 #endif /* HAVE_WINDOW_SYSTEM */
3136 }
3137 else
3138 /* Invalid property or property not supported. Restore
3139 the position to what it was before. */
3140 *position = start_pos;
3141 }
3142
3143 return replaces_text_display_p;
3144 }
3145
3146
3147 /* Check if PROP is a display sub-property value whose text should be
3148 treated as intangible. */
3149
3150 static int
3151 single_display_prop_intangible_p (prop)
3152 Lisp_Object prop;
3153 {
3154 /* Skip over `when FORM'. */
3155 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3156 {
3157 prop = XCDR (prop);
3158 if (!CONSP (prop))
3159 return 0;
3160 prop = XCDR (prop);
3161 }
3162
3163 if (STRINGP (prop))
3164 return 1;
3165
3166 if (!CONSP (prop))
3167 return 0;
3168
3169 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3170 we don't need to treat text as intangible. */
3171 if (EQ (XCAR (prop), Qmargin))
3172 {
3173 prop = XCDR (prop);
3174 if (!CONSP (prop))
3175 return 0;
3176
3177 prop = XCDR (prop);
3178 if (!CONSP (prop)
3179 || EQ (XCAR (prop), Qleft_margin)
3180 || EQ (XCAR (prop), Qright_margin))
3181 return 0;
3182 }
3183
3184 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3185 }
3186
3187
3188 /* Check if PROP is a display property value whose text should be
3189 treated as intangible. */
3190
3191 int
3192 display_prop_intangible_p (prop)
3193 Lisp_Object prop;
3194 {
3195 if (CONSP (prop)
3196 && CONSP (XCAR (prop))
3197 && !EQ (Qmargin, XCAR (XCAR (prop))))
3198 {
3199 /* A list of sub-properties. */
3200 while (CONSP (prop))
3201 {
3202 if (single_display_prop_intangible_p (XCAR (prop)))
3203 return 1;
3204 prop = XCDR (prop);
3205 }
3206 }
3207 else if (VECTORP (prop))
3208 {
3209 /* A vector of sub-properties. */
3210 int i;
3211 for (i = 0; i < ASIZE (prop); ++i)
3212 if (single_display_prop_intangible_p (AREF (prop, i)))
3213 return 1;
3214 }
3215 else
3216 return single_display_prop_intangible_p (prop);
3217
3218 return 0;
3219 }
3220
3221
3222 /* Return 1 if PROP is a display sub-property value containing STRING. */
3223
3224 static int
3225 single_display_prop_string_p (prop, string)
3226 Lisp_Object prop, string;
3227 {
3228 if (EQ (string, prop))
3229 return 1;
3230
3231 /* Skip over `when FORM'. */
3232 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3233 {
3234 prop = XCDR (prop);
3235 if (!CONSP (prop))
3236 return 0;
3237 prop = XCDR (prop);
3238 }
3239
3240 if (CONSP (prop))
3241 /* Skip over `margin LOCATION'. */
3242 if (EQ (XCAR (prop), Qmargin))
3243 {
3244 prop = XCDR (prop);
3245 if (!CONSP (prop))
3246 return 0;
3247
3248 prop = XCDR (prop);
3249 if (!CONSP (prop))
3250 return 0;
3251 }
3252
3253 return CONSP (prop) && EQ (XCAR (prop), string);
3254 }
3255
3256
3257 /* Return 1 if STRING appears in the `display' property PROP. */
3258
3259 static int
3260 display_prop_string_p (prop, string)
3261 Lisp_Object prop, string;
3262 {
3263 if (CONSP (prop)
3264 && CONSP (XCAR (prop))
3265 && !EQ (Qmargin, XCAR (XCAR (prop))))
3266 {
3267 /* A list of sub-properties. */
3268 while (CONSP (prop))
3269 {
3270 if (single_display_prop_string_p (XCAR (prop), string))
3271 return 1;
3272 prop = XCDR (prop);
3273 }
3274 }
3275 else if (VECTORP (prop))
3276 {
3277 /* A vector of sub-properties. */
3278 int i;
3279 for (i = 0; i < ASIZE (prop); ++i)
3280 if (single_display_prop_string_p (AREF (prop, i), string))
3281 return 1;
3282 }
3283 else
3284 return single_display_prop_string_p (prop, string);
3285
3286 return 0;
3287 }
3288
3289
3290 /* Determine from which buffer position in W's buffer STRING comes
3291 from. AROUND_CHARPOS is an approximate position where it could
3292 be from. Value is the buffer position or 0 if it couldn't be
3293 determined.
3294
3295 W's buffer must be current.
3296
3297 This function is necessary because we don't record buffer positions
3298 in glyphs generated from strings (to keep struct glyph small).
3299 This function may only use code that doesn't eval because it is
3300 called asynchronously from note_mouse_highlight. */
3301
3302 int
3303 string_buffer_position (w, string, around_charpos)
3304 struct window *w;
3305 Lisp_Object string;
3306 int around_charpos;
3307 {
3308 Lisp_Object limit, prop, pos;
3309 const int MAX_DISTANCE = 1000;
3310 int found = 0;
3311
3312 pos = make_number (around_charpos);
3313 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3314 while (!found && !EQ (pos, limit))
3315 {
3316 prop = Fget_char_property (pos, Qdisplay, Qnil);
3317 if (!NILP (prop) && display_prop_string_p (prop, string))
3318 found = 1;
3319 else
3320 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3321 }
3322
3323 if (!found)
3324 {
3325 pos = make_number (around_charpos);
3326 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3327 while (!found && !EQ (pos, limit))
3328 {
3329 prop = Fget_char_property (pos, Qdisplay, Qnil);
3330 if (!NILP (prop) && display_prop_string_p (prop, string))
3331 found = 1;
3332 else
3333 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3334 limit);
3335 }
3336 }
3337
3338 return found ? XINT (pos) : 0;
3339 }
3340
3341
3342 \f
3343 /***********************************************************************
3344 `composition' property
3345 ***********************************************************************/
3346
3347 /* Set up iterator IT from `composition' property at its current
3348 position. Called from handle_stop. */
3349
3350 static enum prop_handled
3351 handle_composition_prop (it)
3352 struct it *it;
3353 {
3354 Lisp_Object prop, string;
3355 int pos, pos_byte, end;
3356 enum prop_handled handled = HANDLED_NORMALLY;
3357
3358 if (STRINGP (it->string))
3359 {
3360 pos = IT_STRING_CHARPOS (*it);
3361 pos_byte = IT_STRING_BYTEPOS (*it);
3362 string = it->string;
3363 }
3364 else
3365 {
3366 pos = IT_CHARPOS (*it);
3367 pos_byte = IT_BYTEPOS (*it);
3368 string = Qnil;
3369 }
3370
3371 /* If there's a valid composition and point is not inside of the
3372 composition (in the case that the composition is from the current
3373 buffer), draw a glyph composed from the composition components. */
3374 if (find_composition (pos, -1, &pos, &end, &prop, string)
3375 && COMPOSITION_VALID_P (pos, end, prop)
3376 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3377 {
3378 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3379
3380 if (id >= 0)
3381 {
3382 it->method = next_element_from_composition;
3383 it->cmp_id = id;
3384 it->cmp_len = COMPOSITION_LENGTH (prop);
3385 /* For a terminal, draw only the first character of the
3386 components. */
3387 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3388 it->len = (STRINGP (it->string)
3389 ? string_char_to_byte (it->string, end)
3390 : CHAR_TO_BYTE (end)) - pos_byte;
3391 it->stop_charpos = end;
3392 handled = HANDLED_RETURN;
3393 }
3394 }
3395
3396 return handled;
3397 }
3398
3399
3400 \f
3401 /***********************************************************************
3402 Overlay strings
3403 ***********************************************************************/
3404
3405 /* The following structure is used to record overlay strings for
3406 later sorting in load_overlay_strings. */
3407
3408 struct overlay_entry
3409 {
3410 Lisp_Object overlay;
3411 Lisp_Object string;
3412 int priority;
3413 int after_string_p;
3414 };
3415
3416
3417 /* Set up iterator IT from overlay strings at its current position.
3418 Called from handle_stop. */
3419
3420 static enum prop_handled
3421 handle_overlay_change (it)
3422 struct it *it;
3423 {
3424 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3425 return HANDLED_RECOMPUTE_PROPS;
3426 else
3427 return HANDLED_NORMALLY;
3428 }
3429
3430
3431 /* Set up the next overlay string for delivery by IT, if there is an
3432 overlay string to deliver. Called by set_iterator_to_next when the
3433 end of the current overlay string is reached. If there are more
3434 overlay strings to display, IT->string and
3435 IT->current.overlay_string_index are set appropriately here.
3436 Otherwise IT->string is set to nil. */
3437
3438 static void
3439 next_overlay_string (it)
3440 struct it *it;
3441 {
3442 ++it->current.overlay_string_index;
3443 if (it->current.overlay_string_index == it->n_overlay_strings)
3444 {
3445 /* No more overlay strings. Restore IT's settings to what
3446 they were before overlay strings were processed, and
3447 continue to deliver from current_buffer. */
3448 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3449
3450 pop_it (it);
3451 xassert (it->stop_charpos >= BEGV
3452 && it->stop_charpos <= it->end_charpos);
3453 it->string = Qnil;
3454 it->current.overlay_string_index = -1;
3455 SET_TEXT_POS (it->current.string_pos, -1, -1);
3456 it->n_overlay_strings = 0;
3457 it->method = next_element_from_buffer;
3458
3459 /* If we're at the end of the buffer, record that we have
3460 processed the overlay strings there already, so that
3461 next_element_from_buffer doesn't try it again. */
3462 if (IT_CHARPOS (*it) >= it->end_charpos)
3463 it->overlay_strings_at_end_processed_p = 1;
3464
3465 /* If we have to display `...' for invisible text, set
3466 the iterator up for that. */
3467 if (display_ellipsis_p)
3468 setup_for_ellipsis (it);
3469 }
3470 else
3471 {
3472 /* There are more overlay strings to process. If
3473 IT->current.overlay_string_index has advanced to a position
3474 where we must load IT->overlay_strings with more strings, do
3475 it. */
3476 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3477
3478 if (it->current.overlay_string_index && i == 0)
3479 load_overlay_strings (it, 0);
3480
3481 /* Initialize IT to deliver display elements from the overlay
3482 string. */
3483 it->string = it->overlay_strings[i];
3484 it->multibyte_p = STRING_MULTIBYTE (it->string);
3485 SET_TEXT_POS (it->current.string_pos, 0, 0);
3486 it->method = next_element_from_string;
3487 it->stop_charpos = 0;
3488 }
3489
3490 CHECK_IT (it);
3491 }
3492
3493
3494 /* Compare two overlay_entry structures E1 and E2. Used as a
3495 comparison function for qsort in load_overlay_strings. Overlay
3496 strings for the same position are sorted so that
3497
3498 1. All after-strings come in front of before-strings, except
3499 when they come from the same overlay.
3500
3501 2. Within after-strings, strings are sorted so that overlay strings
3502 from overlays with higher priorities come first.
3503
3504 2. Within before-strings, strings are sorted so that overlay
3505 strings from overlays with higher priorities come last.
3506
3507 Value is analogous to strcmp. */
3508
3509
3510 static int
3511 compare_overlay_entries (e1, e2)
3512 void *e1, *e2;
3513 {
3514 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3515 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3516 int result;
3517
3518 if (entry1->after_string_p != entry2->after_string_p)
3519 {
3520 /* Let after-strings appear in front of before-strings if
3521 they come from different overlays. */
3522 if (EQ (entry1->overlay, entry2->overlay))
3523 result = entry1->after_string_p ? 1 : -1;
3524 else
3525 result = entry1->after_string_p ? -1 : 1;
3526 }
3527 else if (entry1->after_string_p)
3528 /* After-strings sorted in order of decreasing priority. */
3529 result = entry2->priority - entry1->priority;
3530 else
3531 /* Before-strings sorted in order of increasing priority. */
3532 result = entry1->priority - entry2->priority;
3533
3534 return result;
3535 }
3536
3537
3538 /* Load the vector IT->overlay_strings with overlay strings from IT's
3539 current buffer position, or from CHARPOS if that is > 0. Set
3540 IT->n_overlays to the total number of overlay strings found.
3541
3542 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3543 a time. On entry into load_overlay_strings,
3544 IT->current.overlay_string_index gives the number of overlay
3545 strings that have already been loaded by previous calls to this
3546 function.
3547
3548 IT->add_overlay_start contains an additional overlay start
3549 position to consider for taking overlay strings from, if non-zero.
3550 This position comes into play when the overlay has an `invisible'
3551 property, and both before and after-strings. When we've skipped to
3552 the end of the overlay, because of its `invisible' property, we
3553 nevertheless want its before-string to appear.
3554 IT->add_overlay_start will contain the overlay start position
3555 in this case.
3556
3557 Overlay strings are sorted so that after-string strings come in
3558 front of before-string strings. Within before and after-strings,
3559 strings are sorted by overlay priority. See also function
3560 compare_overlay_entries. */
3561
3562 static void
3563 load_overlay_strings (it, charpos)
3564 struct it *it;
3565 int charpos;
3566 {
3567 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3568 Lisp_Object ov, overlay, window, str, invisible;
3569 int start, end;
3570 int size = 20;
3571 int n = 0, i, j, invis_p;
3572 struct overlay_entry *entries
3573 = (struct overlay_entry *) alloca (size * sizeof *entries);
3574
3575 if (charpos <= 0)
3576 charpos = IT_CHARPOS (*it);
3577
3578 /* Append the overlay string STRING of overlay OVERLAY to vector
3579 `entries' which has size `size' and currently contains `n'
3580 elements. AFTER_P non-zero means STRING is an after-string of
3581 OVERLAY. */
3582 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3583 do \
3584 { \
3585 Lisp_Object priority; \
3586 \
3587 if (n == size) \
3588 { \
3589 int new_size = 2 * size; \
3590 struct overlay_entry *old = entries; \
3591 entries = \
3592 (struct overlay_entry *) alloca (new_size \
3593 * sizeof *entries); \
3594 bcopy (old, entries, size * sizeof *entries); \
3595 size = new_size; \
3596 } \
3597 \
3598 entries[n].string = (STRING); \
3599 entries[n].overlay = (OVERLAY); \
3600 priority = Foverlay_get ((OVERLAY), Qpriority); \
3601 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3602 entries[n].after_string_p = (AFTER_P); \
3603 ++n; \
3604 } \
3605 while (0)
3606
3607 /* Process overlay before the overlay center. */
3608 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3609 {
3610 overlay = XCAR (ov);
3611 xassert (OVERLAYP (overlay));
3612 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3613 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3614
3615 if (end < charpos)
3616 break;
3617
3618 /* Skip this overlay if it doesn't start or end at IT's current
3619 position. */
3620 if (end != charpos && start != charpos)
3621 continue;
3622
3623 /* Skip this overlay if it doesn't apply to IT->w. */
3624 window = Foverlay_get (overlay, Qwindow);
3625 if (WINDOWP (window) && XWINDOW (window) != it->w)
3626 continue;
3627
3628 /* If the text ``under'' the overlay is invisible, both before-
3629 and after-strings from this overlay are visible; start and
3630 end position are indistinguishable. */
3631 invisible = Foverlay_get (overlay, Qinvisible);
3632 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3633
3634 /* If overlay has a non-empty before-string, record it. */
3635 if ((start == charpos || (end == charpos && invis_p))
3636 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3637 && SCHARS (str))
3638 RECORD_OVERLAY_STRING (overlay, str, 0);
3639
3640 /* If overlay has a non-empty after-string, record it. */
3641 if ((end == charpos || (start == charpos && invis_p))
3642 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3643 && SCHARS (str))
3644 RECORD_OVERLAY_STRING (overlay, str, 1);
3645 }
3646
3647 /* Process overlays after the overlay center. */
3648 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3649 {
3650 overlay = XCAR (ov);
3651 xassert (OVERLAYP (overlay));
3652 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3653 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3654
3655 if (start > charpos)
3656 break;
3657
3658 /* Skip this overlay if it doesn't start or end at IT's current
3659 position. */
3660 if (end != charpos && start != charpos)
3661 continue;
3662
3663 /* Skip this overlay if it doesn't apply to IT->w. */
3664 window = Foverlay_get (overlay, Qwindow);
3665 if (WINDOWP (window) && XWINDOW (window) != it->w)
3666 continue;
3667
3668 /* If the text ``under'' the overlay is invisible, it has a zero
3669 dimension, and both before- and after-strings apply. */
3670 invisible = Foverlay_get (overlay, Qinvisible);
3671 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3672
3673 /* If overlay has a non-empty before-string, record it. */
3674 if ((start == charpos || (end == charpos && invis_p))
3675 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3676 && SCHARS (str))
3677 RECORD_OVERLAY_STRING (overlay, str, 0);
3678
3679 /* If overlay has a non-empty after-string, record it. */
3680 if ((end == charpos || (start == charpos && invis_p))
3681 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3682 && SCHARS (str))
3683 RECORD_OVERLAY_STRING (overlay, str, 1);
3684 }
3685
3686 #undef RECORD_OVERLAY_STRING
3687
3688 /* Sort entries. */
3689 if (n > 1)
3690 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3691
3692 /* Record the total number of strings to process. */
3693 it->n_overlay_strings = n;
3694
3695 /* IT->current.overlay_string_index is the number of overlay strings
3696 that have already been consumed by IT. Copy some of the
3697 remaining overlay strings to IT->overlay_strings. */
3698 i = 0;
3699 j = it->current.overlay_string_index;
3700 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3701 it->overlay_strings[i++] = entries[j++].string;
3702
3703 CHECK_IT (it);
3704 }
3705
3706
3707 /* Get the first chunk of overlay strings at IT's current buffer
3708 position, or at CHARPOS if that is > 0. Value is non-zero if at
3709 least one overlay string was found. */
3710
3711 static int
3712 get_overlay_strings (it, charpos)
3713 struct it *it;
3714 int charpos;
3715 {
3716 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3717 process. This fills IT->overlay_strings with strings, and sets
3718 IT->n_overlay_strings to the total number of strings to process.
3719 IT->pos.overlay_string_index has to be set temporarily to zero
3720 because load_overlay_strings needs this; it must be set to -1
3721 when no overlay strings are found because a zero value would
3722 indicate a position in the first overlay string. */
3723 it->current.overlay_string_index = 0;
3724 load_overlay_strings (it, charpos);
3725
3726 /* If we found overlay strings, set up IT to deliver display
3727 elements from the first one. Otherwise set up IT to deliver
3728 from current_buffer. */
3729 if (it->n_overlay_strings)
3730 {
3731 /* Make sure we know settings in current_buffer, so that we can
3732 restore meaningful values when we're done with the overlay
3733 strings. */
3734 compute_stop_pos (it);
3735 xassert (it->face_id >= 0);
3736
3737 /* Save IT's settings. They are restored after all overlay
3738 strings have been processed. */
3739 xassert (it->sp == 0);
3740 push_it (it);
3741
3742 /* Set up IT to deliver display elements from the first overlay
3743 string. */
3744 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3745 it->string = it->overlay_strings[0];
3746 it->stop_charpos = 0;
3747 xassert (STRINGP (it->string));
3748 it->end_charpos = SCHARS (it->string);
3749 it->multibyte_p = STRING_MULTIBYTE (it->string);
3750 it->method = next_element_from_string;
3751 }
3752 else
3753 {
3754 it->string = Qnil;
3755 it->current.overlay_string_index = -1;
3756 it->method = next_element_from_buffer;
3757 }
3758
3759 CHECK_IT (it);
3760
3761 /* Value is non-zero if we found at least one overlay string. */
3762 return STRINGP (it->string);
3763 }
3764
3765
3766 \f
3767 /***********************************************************************
3768 Saving and restoring state
3769 ***********************************************************************/
3770
3771 /* Save current settings of IT on IT->stack. Called, for example,
3772 before setting up IT for an overlay string, to be able to restore
3773 IT's settings to what they were after the overlay string has been
3774 processed. */
3775
3776 static void
3777 push_it (it)
3778 struct it *it;
3779 {
3780 struct iterator_stack_entry *p;
3781
3782 xassert (it->sp < 2);
3783 p = it->stack + it->sp;
3784
3785 p->stop_charpos = it->stop_charpos;
3786 xassert (it->face_id >= 0);
3787 p->face_id = it->face_id;
3788 p->string = it->string;
3789 p->pos = it->current;
3790 p->end_charpos = it->end_charpos;
3791 p->string_nchars = it->string_nchars;
3792 p->area = it->area;
3793 p->multibyte_p = it->multibyte_p;
3794 p->space_width = it->space_width;
3795 p->font_height = it->font_height;
3796 p->voffset = it->voffset;
3797 p->string_from_display_prop_p = it->string_from_display_prop_p;
3798 p->display_ellipsis_p = 0;
3799 ++it->sp;
3800 }
3801
3802
3803 /* Restore IT's settings from IT->stack. Called, for example, when no
3804 more overlay strings must be processed, and we return to delivering
3805 display elements from a buffer, or when the end of a string from a
3806 `display' property is reached and we return to delivering display
3807 elements from an overlay string, or from a buffer. */
3808
3809 static void
3810 pop_it (it)
3811 struct it *it;
3812 {
3813 struct iterator_stack_entry *p;
3814
3815 xassert (it->sp > 0);
3816 --it->sp;
3817 p = it->stack + it->sp;
3818 it->stop_charpos = p->stop_charpos;
3819 it->face_id = p->face_id;
3820 it->string = p->string;
3821 it->current = p->pos;
3822 it->end_charpos = p->end_charpos;
3823 it->string_nchars = p->string_nchars;
3824 it->area = p->area;
3825 it->multibyte_p = p->multibyte_p;
3826 it->space_width = p->space_width;
3827 it->font_height = p->font_height;
3828 it->voffset = p->voffset;
3829 it->string_from_display_prop_p = p->string_from_display_prop_p;
3830 }
3831
3832
3833 \f
3834 /***********************************************************************
3835 Moving over lines
3836 ***********************************************************************/
3837
3838 /* Set IT's current position to the previous line start. */
3839
3840 static void
3841 back_to_previous_line_start (it)
3842 struct it *it;
3843 {
3844 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3845 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3846 }
3847
3848
3849 /* Move IT to the next line start.
3850
3851 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3852 we skipped over part of the text (as opposed to moving the iterator
3853 continuously over the text). Otherwise, don't change the value
3854 of *SKIPPED_P.
3855
3856 Newlines may come from buffer text, overlay strings, or strings
3857 displayed via the `display' property. That's the reason we can't
3858 simply use find_next_newline_no_quit.
3859
3860 Note that this function may not skip over invisible text that is so
3861 because of text properties and immediately follows a newline. If
3862 it would, function reseat_at_next_visible_line_start, when called
3863 from set_iterator_to_next, would effectively make invisible
3864 characters following a newline part of the wrong glyph row, which
3865 leads to wrong cursor motion. */
3866
3867 static int
3868 forward_to_next_line_start (it, skipped_p)
3869 struct it *it;
3870 int *skipped_p;
3871 {
3872 int old_selective, newline_found_p, n;
3873 const int MAX_NEWLINE_DISTANCE = 500;
3874
3875 /* If already on a newline, just consume it to avoid unintended
3876 skipping over invisible text below. */
3877 if (it->what == IT_CHARACTER
3878 && it->c == '\n'
3879 && CHARPOS (it->position) == IT_CHARPOS (*it))
3880 {
3881 set_iterator_to_next (it, 0);
3882 it->c = 0;
3883 return 1;
3884 }
3885
3886 /* Don't handle selective display in the following. It's (a)
3887 unnecessary because it's done by the caller, and (b) leads to an
3888 infinite recursion because next_element_from_ellipsis indirectly
3889 calls this function. */
3890 old_selective = it->selective;
3891 it->selective = 0;
3892
3893 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3894 from buffer text. */
3895 for (n = newline_found_p = 0;
3896 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3897 n += STRINGP (it->string) ? 0 : 1)
3898 {
3899 if (!get_next_display_element (it))
3900 return 0;
3901 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3902 set_iterator_to_next (it, 0);
3903 }
3904
3905 /* If we didn't find a newline near enough, see if we can use a
3906 short-cut. */
3907 if (!newline_found_p)
3908 {
3909 int start = IT_CHARPOS (*it);
3910 int limit = find_next_newline_no_quit (start, 1);
3911 Lisp_Object pos;
3912
3913 xassert (!STRINGP (it->string));
3914
3915 /* If there isn't any `display' property in sight, and no
3916 overlays, we can just use the position of the newline in
3917 buffer text. */
3918 if (it->stop_charpos >= limit
3919 || ((pos = Fnext_single_property_change (make_number (start),
3920 Qdisplay,
3921 Qnil, make_number (limit)),
3922 NILP (pos))
3923 && next_overlay_change (start) == ZV))
3924 {
3925 IT_CHARPOS (*it) = limit;
3926 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3927 *skipped_p = newline_found_p = 1;
3928 }
3929 else
3930 {
3931 while (get_next_display_element (it)
3932 && !newline_found_p)
3933 {
3934 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3935 set_iterator_to_next (it, 0);
3936 }
3937 }
3938 }
3939
3940 it->selective = old_selective;
3941 return newline_found_p;
3942 }
3943
3944
3945 /* Set IT's current position to the previous visible line start. Skip
3946 invisible text that is so either due to text properties or due to
3947 selective display. Caution: this does not change IT->current_x and
3948 IT->hpos. */
3949
3950 static void
3951 back_to_previous_visible_line_start (it)
3952 struct it *it;
3953 {
3954 int visible_p = 0;
3955
3956 /* Go back one newline if not on BEGV already. */
3957 if (IT_CHARPOS (*it) > BEGV)
3958 back_to_previous_line_start (it);
3959
3960 /* Move over lines that are invisible because of selective display
3961 or text properties. */
3962 while (IT_CHARPOS (*it) > BEGV
3963 && !visible_p)
3964 {
3965 visible_p = 1;
3966
3967 /* If selective > 0, then lines indented more than that values
3968 are invisible. */
3969 if (it->selective > 0
3970 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3971 (double) it->selective)) /* iftc */
3972 visible_p = 0;
3973 else
3974 {
3975 Lisp_Object prop;
3976
3977 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3978 Qinvisible, it->window);
3979 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3980 visible_p = 0;
3981 }
3982
3983 /* Back one more newline if the current one is invisible. */
3984 if (!visible_p)
3985 back_to_previous_line_start (it);
3986 }
3987
3988 xassert (IT_CHARPOS (*it) >= BEGV);
3989 xassert (IT_CHARPOS (*it) == BEGV
3990 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3991 CHECK_IT (it);
3992 }
3993
3994
3995 /* Reseat iterator IT at the previous visible line start. Skip
3996 invisible text that is so either due to text properties or due to
3997 selective display. At the end, update IT's overlay information,
3998 face information etc. */
3999
4000 static void
4001 reseat_at_previous_visible_line_start (it)
4002 struct it *it;
4003 {
4004 back_to_previous_visible_line_start (it);
4005 reseat (it, it->current.pos, 1);
4006 CHECK_IT (it);
4007 }
4008
4009
4010 /* Reseat iterator IT on the next visible line start in the current
4011 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4012 preceding the line start. Skip over invisible text that is so
4013 because of selective display. Compute faces, overlays etc at the
4014 new position. Note that this function does not skip over text that
4015 is invisible because of text properties. */
4016
4017 static void
4018 reseat_at_next_visible_line_start (it, on_newline_p)
4019 struct it *it;
4020 int on_newline_p;
4021 {
4022 int newline_found_p, skipped_p = 0;
4023
4024 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4025
4026 /* Skip over lines that are invisible because they are indented
4027 more than the value of IT->selective. */
4028 if (it->selective > 0)
4029 while (IT_CHARPOS (*it) < ZV
4030 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4031 (double) it->selective)) /* iftc */
4032 {
4033 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4034 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4035 }
4036
4037 /* Position on the newline if that's what's requested. */
4038 if (on_newline_p && newline_found_p)
4039 {
4040 if (STRINGP (it->string))
4041 {
4042 if (IT_STRING_CHARPOS (*it) > 0)
4043 {
4044 --IT_STRING_CHARPOS (*it);
4045 --IT_STRING_BYTEPOS (*it);
4046 }
4047 }
4048 else if (IT_CHARPOS (*it) > BEGV)
4049 {
4050 --IT_CHARPOS (*it);
4051 --IT_BYTEPOS (*it);
4052 reseat (it, it->current.pos, 0);
4053 }
4054 }
4055 else if (skipped_p)
4056 reseat (it, it->current.pos, 0);
4057
4058 CHECK_IT (it);
4059 }
4060
4061
4062 \f
4063 /***********************************************************************
4064 Changing an iterator's position
4065 ***********************************************************************/
4066
4067 /* Change IT's current position to POS in current_buffer. If FORCE_P
4068 is non-zero, always check for text properties at the new position.
4069 Otherwise, text properties are only looked up if POS >=
4070 IT->check_charpos of a property. */
4071
4072 static void
4073 reseat (it, pos, force_p)
4074 struct it *it;
4075 struct text_pos pos;
4076 int force_p;
4077 {
4078 int original_pos = IT_CHARPOS (*it);
4079
4080 reseat_1 (it, pos, 0);
4081
4082 /* Determine where to check text properties. Avoid doing it
4083 where possible because text property lookup is very expensive. */
4084 if (force_p
4085 || CHARPOS (pos) > it->stop_charpos
4086 || CHARPOS (pos) < original_pos)
4087 handle_stop (it);
4088
4089 CHECK_IT (it);
4090 }
4091
4092
4093 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4094 IT->stop_pos to POS, also. */
4095
4096 static void
4097 reseat_1 (it, pos, set_stop_p)
4098 struct it *it;
4099 struct text_pos pos;
4100 int set_stop_p;
4101 {
4102 /* Don't call this function when scanning a C string. */
4103 xassert (it->s == NULL);
4104
4105 /* POS must be a reasonable value. */
4106 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4107
4108 it->current.pos = it->position = pos;
4109 XSETBUFFER (it->object, current_buffer);
4110 it->end_charpos = ZV;
4111 it->dpvec = NULL;
4112 it->current.dpvec_index = -1;
4113 it->current.overlay_string_index = -1;
4114 IT_STRING_CHARPOS (*it) = -1;
4115 IT_STRING_BYTEPOS (*it) = -1;
4116 it->string = Qnil;
4117 it->method = next_element_from_buffer;
4118 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4119 it->sp = 0;
4120 it->face_before_selective_p = 0;
4121
4122 if (set_stop_p)
4123 it->stop_charpos = CHARPOS (pos);
4124 }
4125
4126
4127 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4128 If S is non-null, it is a C string to iterate over. Otherwise,
4129 STRING gives a Lisp string to iterate over.
4130
4131 If PRECISION > 0, don't return more then PRECISION number of
4132 characters from the string.
4133
4134 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4135 characters have been returned. FIELD_WIDTH < 0 means an infinite
4136 field width.
4137
4138 MULTIBYTE = 0 means disable processing of multibyte characters,
4139 MULTIBYTE > 0 means enable it,
4140 MULTIBYTE < 0 means use IT->multibyte_p.
4141
4142 IT must be initialized via a prior call to init_iterator before
4143 calling this function. */
4144
4145 static void
4146 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4147 struct it *it;
4148 unsigned char *s;
4149 Lisp_Object string;
4150 int charpos;
4151 int precision, field_width, multibyte;
4152 {
4153 /* No region in strings. */
4154 it->region_beg_charpos = it->region_end_charpos = -1;
4155
4156 /* No text property checks performed by default, but see below. */
4157 it->stop_charpos = -1;
4158
4159 /* Set iterator position and end position. */
4160 bzero (&it->current, sizeof it->current);
4161 it->current.overlay_string_index = -1;
4162 it->current.dpvec_index = -1;
4163 xassert (charpos >= 0);
4164
4165 /* If STRING is specified, use its multibyteness, otherwise use the
4166 setting of MULTIBYTE, if specified. */
4167 if (multibyte >= 0)
4168 it->multibyte_p = multibyte > 0;
4169
4170 if (s == NULL)
4171 {
4172 xassert (STRINGP (string));
4173 it->string = string;
4174 it->s = NULL;
4175 it->end_charpos = it->string_nchars = SCHARS (string);
4176 it->method = next_element_from_string;
4177 it->current.string_pos = string_pos (charpos, string);
4178 }
4179 else
4180 {
4181 it->s = s;
4182 it->string = Qnil;
4183
4184 /* Note that we use IT->current.pos, not it->current.string_pos,
4185 for displaying C strings. */
4186 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4187 if (it->multibyte_p)
4188 {
4189 it->current.pos = c_string_pos (charpos, s, 1);
4190 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4191 }
4192 else
4193 {
4194 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4195 it->end_charpos = it->string_nchars = strlen (s);
4196 }
4197
4198 it->method = next_element_from_c_string;
4199 }
4200
4201 /* PRECISION > 0 means don't return more than PRECISION characters
4202 from the string. */
4203 if (precision > 0 && it->end_charpos - charpos > precision)
4204 it->end_charpos = it->string_nchars = charpos + precision;
4205
4206 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4207 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4208 FIELD_WIDTH < 0 means infinite field width. This is useful for
4209 padding with `-' at the end of a mode line. */
4210 if (field_width < 0)
4211 field_width = INFINITY;
4212 if (field_width > it->end_charpos - charpos)
4213 it->end_charpos = charpos + field_width;
4214
4215 /* Use the standard display table for displaying strings. */
4216 if (DISP_TABLE_P (Vstandard_display_table))
4217 it->dp = XCHAR_TABLE (Vstandard_display_table);
4218
4219 it->stop_charpos = charpos;
4220 CHECK_IT (it);
4221 }
4222
4223
4224 \f
4225 /***********************************************************************
4226 Iteration
4227 ***********************************************************************/
4228
4229 /* Load IT's display element fields with information about the next
4230 display element from the current position of IT. Value is zero if
4231 end of buffer (or C string) is reached. */
4232
4233 int
4234 get_next_display_element (it)
4235 struct it *it;
4236 {
4237 /* Non-zero means that we found a display element. Zero means that
4238 we hit the end of what we iterate over. Performance note: the
4239 function pointer `method' used here turns out to be faster than
4240 using a sequence of if-statements. */
4241 int success_p = (*it->method) (it);
4242
4243 if (it->what == IT_CHARACTER)
4244 {
4245 /* Map via display table or translate control characters.
4246 IT->c, IT->len etc. have been set to the next character by
4247 the function call above. If we have a display table, and it
4248 contains an entry for IT->c, translate it. Don't do this if
4249 IT->c itself comes from a display table, otherwise we could
4250 end up in an infinite recursion. (An alternative could be to
4251 count the recursion depth of this function and signal an
4252 error when a certain maximum depth is reached.) Is it worth
4253 it? */
4254 if (success_p && it->dpvec == NULL)
4255 {
4256 Lisp_Object dv;
4257
4258 if (it->dp
4259 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4260 VECTORP (dv)))
4261 {
4262 struct Lisp_Vector *v = XVECTOR (dv);
4263
4264 /* Return the first character from the display table
4265 entry, if not empty. If empty, don't display the
4266 current character. */
4267 if (v->size)
4268 {
4269 it->dpvec_char_len = it->len;
4270 it->dpvec = v->contents;
4271 it->dpend = v->contents + v->size;
4272 it->current.dpvec_index = 0;
4273 it->method = next_element_from_display_vector;
4274 success_p = get_next_display_element (it);
4275 }
4276 else
4277 {
4278 set_iterator_to_next (it, 0);
4279 success_p = get_next_display_element (it);
4280 }
4281 }
4282
4283 /* Translate control characters into `\003' or `^C' form.
4284 Control characters coming from a display table entry are
4285 currently not translated because we use IT->dpvec to hold
4286 the translation. This could easily be changed but I
4287 don't believe that it is worth doing.
4288
4289 If it->multibyte_p is nonzero, eight-bit characters and
4290 non-printable multibyte characters are also translated to
4291 octal form.
4292
4293 If it->multibyte_p is zero, eight-bit characters that
4294 don't have corresponding multibyte char code are also
4295 translated to octal form. */
4296 else if ((it->c < ' '
4297 && (it->area != TEXT_AREA
4298 || (it->c != '\n' && it->c != '\t')))
4299 || (it->multibyte_p
4300 ? ((it->c >= 127
4301 && it->len == 1)
4302 || !CHAR_PRINTABLE_P (it->c))
4303 : (it->c >= 127
4304 && it->c == unibyte_char_to_multibyte (it->c))))
4305 {
4306 /* IT->c is a control character which must be displayed
4307 either as '\003' or as `^C' where the '\\' and '^'
4308 can be defined in the display table. Fill
4309 IT->ctl_chars with glyphs for what we have to
4310 display. Then, set IT->dpvec to these glyphs. */
4311 GLYPH g;
4312
4313 if (it->c < 128 && it->ctl_arrow_p)
4314 {
4315 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4316 if (it->dp
4317 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4318 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4319 g = XINT (DISP_CTRL_GLYPH (it->dp));
4320 else
4321 g = FAST_MAKE_GLYPH ('^', 0);
4322 XSETINT (it->ctl_chars[0], g);
4323
4324 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4325 XSETINT (it->ctl_chars[1], g);
4326
4327 /* Set up IT->dpvec and return first character from it. */
4328 it->dpvec_char_len = it->len;
4329 it->dpvec = it->ctl_chars;
4330 it->dpend = it->dpvec + 2;
4331 it->current.dpvec_index = 0;
4332 it->method = next_element_from_display_vector;
4333 get_next_display_element (it);
4334 }
4335 else
4336 {
4337 unsigned char str[MAX_MULTIBYTE_LENGTH];
4338 int len;
4339 int i;
4340 GLYPH escape_glyph;
4341
4342 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4343 if (it->dp
4344 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4345 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4346 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4347 else
4348 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4349
4350 if (SINGLE_BYTE_CHAR_P (it->c))
4351 str[0] = it->c, len = 1;
4352 else
4353 {
4354 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4355 if (len < 0)
4356 {
4357 /* It's an invalid character, which
4358 shouldn't happen actually, but due to
4359 bugs it may happen. Let's print the char
4360 as is, there's not much meaningful we can
4361 do with it. */
4362 str[0] = it->c;
4363 str[1] = it->c >> 8;
4364 str[2] = it->c >> 16;
4365 str[3] = it->c >> 24;
4366 len = 4;
4367 }
4368 }
4369
4370 for (i = 0; i < len; i++)
4371 {
4372 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4373 /* Insert three more glyphs into IT->ctl_chars for
4374 the octal display of the character. */
4375 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4376 XSETINT (it->ctl_chars[i * 4 + 1], g);
4377 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4378 XSETINT (it->ctl_chars[i * 4 + 2], g);
4379 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4380 XSETINT (it->ctl_chars[i * 4 + 3], g);
4381 }
4382
4383 /* Set up IT->dpvec and return the first character
4384 from it. */
4385 it->dpvec_char_len = it->len;
4386 it->dpvec = it->ctl_chars;
4387 it->dpend = it->dpvec + len * 4;
4388 it->current.dpvec_index = 0;
4389 it->method = next_element_from_display_vector;
4390 get_next_display_element (it);
4391 }
4392 }
4393 }
4394
4395 /* Adjust face id for a multibyte character. There are no
4396 multibyte character in unibyte text. */
4397 if (it->multibyte_p
4398 && success_p
4399 && FRAME_WINDOW_P (it->f))
4400 {
4401 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4402 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4403 }
4404 }
4405
4406 /* Is this character the last one of a run of characters with
4407 box? If yes, set IT->end_of_box_run_p to 1. */
4408 if (it->face_box_p
4409 && it->s == NULL)
4410 {
4411 int face_id;
4412 struct face *face;
4413
4414 it->end_of_box_run_p
4415 = ((face_id = face_after_it_pos (it),
4416 face_id != it->face_id)
4417 && (face = FACE_FROM_ID (it->f, face_id),
4418 face->box == FACE_NO_BOX));
4419 }
4420
4421 /* Value is 0 if end of buffer or string reached. */
4422 return success_p;
4423 }
4424
4425
4426 /* Move IT to the next display element.
4427
4428 RESEAT_P non-zero means if called on a newline in buffer text,
4429 skip to the next visible line start.
4430
4431 Functions get_next_display_element and set_iterator_to_next are
4432 separate because I find this arrangement easier to handle than a
4433 get_next_display_element function that also increments IT's
4434 position. The way it is we can first look at an iterator's current
4435 display element, decide whether it fits on a line, and if it does,
4436 increment the iterator position. The other way around we probably
4437 would either need a flag indicating whether the iterator has to be
4438 incremented the next time, or we would have to implement a
4439 decrement position function which would not be easy to write. */
4440
4441 void
4442 set_iterator_to_next (it, reseat_p)
4443 struct it *it;
4444 int reseat_p;
4445 {
4446 /* Reset flags indicating start and end of a sequence of characters
4447 with box. Reset them at the start of this function because
4448 moving the iterator to a new position might set them. */
4449 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4450
4451 if (it->method == next_element_from_buffer)
4452 {
4453 /* The current display element of IT is a character from
4454 current_buffer. Advance in the buffer, and maybe skip over
4455 invisible lines that are so because of selective display. */
4456 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4457 reseat_at_next_visible_line_start (it, 0);
4458 else
4459 {
4460 xassert (it->len != 0);
4461 IT_BYTEPOS (*it) += it->len;
4462 IT_CHARPOS (*it) += 1;
4463 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4464 }
4465 }
4466 else if (it->method == next_element_from_composition)
4467 {
4468 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4469 if (STRINGP (it->string))
4470 {
4471 IT_STRING_BYTEPOS (*it) += it->len;
4472 IT_STRING_CHARPOS (*it) += it->cmp_len;
4473 it->method = next_element_from_string;
4474 goto consider_string_end;
4475 }
4476 else
4477 {
4478 IT_BYTEPOS (*it) += it->len;
4479 IT_CHARPOS (*it) += it->cmp_len;
4480 it->method = next_element_from_buffer;
4481 }
4482 }
4483 else if (it->method == next_element_from_c_string)
4484 {
4485 /* Current display element of IT is from a C string. */
4486 IT_BYTEPOS (*it) += it->len;
4487 IT_CHARPOS (*it) += 1;
4488 }
4489 else if (it->method == next_element_from_display_vector)
4490 {
4491 /* Current display element of IT is from a display table entry.
4492 Advance in the display table definition. Reset it to null if
4493 end reached, and continue with characters from buffers/
4494 strings. */
4495 ++it->current.dpvec_index;
4496
4497 /* Restore face of the iterator to what they were before the
4498 display vector entry (these entries may contain faces). */
4499 it->face_id = it->saved_face_id;
4500
4501 if (it->dpvec + it->current.dpvec_index == it->dpend)
4502 {
4503 if (it->s)
4504 it->method = next_element_from_c_string;
4505 else if (STRINGP (it->string))
4506 it->method = next_element_from_string;
4507 else
4508 it->method = next_element_from_buffer;
4509
4510 it->dpvec = NULL;
4511 it->current.dpvec_index = -1;
4512
4513 /* Skip over characters which were displayed via IT->dpvec. */
4514 if (it->dpvec_char_len < 0)
4515 reseat_at_next_visible_line_start (it, 1);
4516 else if (it->dpvec_char_len > 0)
4517 {
4518 it->len = it->dpvec_char_len;
4519 set_iterator_to_next (it, reseat_p);
4520 }
4521 }
4522 }
4523 else if (it->method == next_element_from_string)
4524 {
4525 /* Current display element is a character from a Lisp string. */
4526 xassert (it->s == NULL && STRINGP (it->string));
4527 IT_STRING_BYTEPOS (*it) += it->len;
4528 IT_STRING_CHARPOS (*it) += 1;
4529
4530 consider_string_end:
4531
4532 if (it->current.overlay_string_index >= 0)
4533 {
4534 /* IT->string is an overlay string. Advance to the
4535 next, if there is one. */
4536 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4537 next_overlay_string (it);
4538 }
4539 else
4540 {
4541 /* IT->string is not an overlay string. If we reached
4542 its end, and there is something on IT->stack, proceed
4543 with what is on the stack. This can be either another
4544 string, this time an overlay string, or a buffer. */
4545 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
4546 && it->sp > 0)
4547 {
4548 pop_it (it);
4549 if (!STRINGP (it->string))
4550 it->method = next_element_from_buffer;
4551 else
4552 goto consider_string_end;
4553 }
4554 }
4555 }
4556 else if (it->method == next_element_from_image
4557 || it->method == next_element_from_stretch)
4558 {
4559 /* The position etc with which we have to proceed are on
4560 the stack. The position may be at the end of a string,
4561 if the `display' property takes up the whole string. */
4562 pop_it (it);
4563 it->image_id = 0;
4564 if (STRINGP (it->string))
4565 {
4566 it->method = next_element_from_string;
4567 goto consider_string_end;
4568 }
4569 else
4570 it->method = next_element_from_buffer;
4571 }
4572 else
4573 /* There are no other methods defined, so this should be a bug. */
4574 abort ();
4575
4576 xassert (it->method != next_element_from_string
4577 || (STRINGP (it->string)
4578 && IT_STRING_CHARPOS (*it) >= 0));
4579 }
4580
4581
4582 /* Load IT's display element fields with information about the next
4583 display element which comes from a display table entry or from the
4584 result of translating a control character to one of the forms `^C'
4585 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4586
4587 static int
4588 next_element_from_display_vector (it)
4589 struct it *it;
4590 {
4591 /* Precondition. */
4592 xassert (it->dpvec && it->current.dpvec_index >= 0);
4593
4594 /* Remember the current face id in case glyphs specify faces.
4595 IT's face is restored in set_iterator_to_next. */
4596 it->saved_face_id = it->face_id;
4597
4598 if (INTEGERP (*it->dpvec)
4599 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4600 {
4601 int lface_id;
4602 GLYPH g;
4603
4604 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4605 it->c = FAST_GLYPH_CHAR (g);
4606 it->len = CHAR_BYTES (it->c);
4607
4608 /* The entry may contain a face id to use. Such a face id is
4609 the id of a Lisp face, not a realized face. A face id of
4610 zero means no face is specified. */
4611 lface_id = FAST_GLYPH_FACE (g);
4612 if (lface_id)
4613 {
4614 /* The function returns -1 if lface_id is invalid. */
4615 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4616 if (face_id >= 0)
4617 it->face_id = face_id;
4618 }
4619 }
4620 else
4621 /* Display table entry is invalid. Return a space. */
4622 it->c = ' ', it->len = 1;
4623
4624 /* Don't change position and object of the iterator here. They are
4625 still the values of the character that had this display table
4626 entry or was translated, and that's what we want. */
4627 it->what = IT_CHARACTER;
4628 return 1;
4629 }
4630
4631
4632 /* Load IT with the next display element from Lisp string IT->string.
4633 IT->current.string_pos is the current position within the string.
4634 If IT->current.overlay_string_index >= 0, the Lisp string is an
4635 overlay string. */
4636
4637 static int
4638 next_element_from_string (it)
4639 struct it *it;
4640 {
4641 struct text_pos position;
4642
4643 xassert (STRINGP (it->string));
4644 xassert (IT_STRING_CHARPOS (*it) >= 0);
4645 position = it->current.string_pos;
4646
4647 /* Time to check for invisible text? */
4648 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4649 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4650 {
4651 handle_stop (it);
4652
4653 /* Since a handler may have changed IT->method, we must
4654 recurse here. */
4655 return get_next_display_element (it);
4656 }
4657
4658 if (it->current.overlay_string_index >= 0)
4659 {
4660 /* Get the next character from an overlay string. In overlay
4661 strings, There is no field width or padding with spaces to
4662 do. */
4663 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4664 {
4665 it->what = IT_EOB;
4666 return 0;
4667 }
4668 else if (STRING_MULTIBYTE (it->string))
4669 {
4670 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4671 const unsigned char *s = (SDATA (it->string)
4672 + IT_STRING_BYTEPOS (*it));
4673 it->c = string_char_and_length (s, remaining, &it->len);
4674 }
4675 else
4676 {
4677 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4678 it->len = 1;
4679 }
4680 }
4681 else
4682 {
4683 /* Get the next character from a Lisp string that is not an
4684 overlay string. Such strings come from the mode line, for
4685 example. We may have to pad with spaces, or truncate the
4686 string. See also next_element_from_c_string. */
4687 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4688 {
4689 it->what = IT_EOB;
4690 return 0;
4691 }
4692 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4693 {
4694 /* Pad with spaces. */
4695 it->c = ' ', it->len = 1;
4696 CHARPOS (position) = BYTEPOS (position) = -1;
4697 }
4698 else if (STRING_MULTIBYTE (it->string))
4699 {
4700 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4701 const unsigned char *s = (SDATA (it->string)
4702 + IT_STRING_BYTEPOS (*it));
4703 it->c = string_char_and_length (s, maxlen, &it->len);
4704 }
4705 else
4706 {
4707 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4708 it->len = 1;
4709 }
4710 }
4711
4712 /* Record what we have and where it came from. Note that we store a
4713 buffer position in IT->position although it could arguably be a
4714 string position. */
4715 it->what = IT_CHARACTER;
4716 it->object = it->string;
4717 it->position = position;
4718 return 1;
4719 }
4720
4721
4722 /* Load IT with next display element from C string IT->s.
4723 IT->string_nchars is the maximum number of characters to return
4724 from the string. IT->end_charpos may be greater than
4725 IT->string_nchars when this function is called, in which case we
4726 may have to return padding spaces. Value is zero if end of string
4727 reached, including padding spaces. */
4728
4729 static int
4730 next_element_from_c_string (it)
4731 struct it *it;
4732 {
4733 int success_p = 1;
4734
4735 xassert (it->s);
4736 it->what = IT_CHARACTER;
4737 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4738 it->object = Qnil;
4739
4740 /* IT's position can be greater IT->string_nchars in case a field
4741 width or precision has been specified when the iterator was
4742 initialized. */
4743 if (IT_CHARPOS (*it) >= it->end_charpos)
4744 {
4745 /* End of the game. */
4746 it->what = IT_EOB;
4747 success_p = 0;
4748 }
4749 else if (IT_CHARPOS (*it) >= it->string_nchars)
4750 {
4751 /* Pad with spaces. */
4752 it->c = ' ', it->len = 1;
4753 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4754 }
4755 else if (it->multibyte_p)
4756 {
4757 /* Implementation note: The calls to strlen apparently aren't a
4758 performance problem because there is no noticeable performance
4759 difference between Emacs running in unibyte or multibyte mode. */
4760 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4761 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4762 maxlen, &it->len);
4763 }
4764 else
4765 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4766
4767 return success_p;
4768 }
4769
4770
4771 /* Set up IT to return characters from an ellipsis, if appropriate.
4772 The definition of the ellipsis glyphs may come from a display table
4773 entry. This function Fills IT with the first glyph from the
4774 ellipsis if an ellipsis is to be displayed. */
4775
4776 static int
4777 next_element_from_ellipsis (it)
4778 struct it *it;
4779 {
4780 if (it->selective_display_ellipsis_p)
4781 {
4782 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4783 {
4784 /* Use the display table definition for `...'. Invalid glyphs
4785 will be handled by the method returning elements from dpvec. */
4786 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4787 it->dpvec_char_len = it->len;
4788 it->dpvec = v->contents;
4789 it->dpend = v->contents + v->size;
4790 it->current.dpvec_index = 0;
4791 it->method = next_element_from_display_vector;
4792 }
4793 else
4794 {
4795 /* Use default `...' which is stored in default_invis_vector. */
4796 it->dpvec_char_len = it->len;
4797 it->dpvec = default_invis_vector;
4798 it->dpend = default_invis_vector + 3;
4799 it->current.dpvec_index = 0;
4800 it->method = next_element_from_display_vector;
4801 }
4802 }
4803 else
4804 {
4805 /* The face at the current position may be different from the
4806 face we find after the invisible text. Remember what it
4807 was in IT->saved_face_id, and signal that it's there by
4808 setting face_before_selective_p. */
4809 it->saved_face_id = it->face_id;
4810 it->method = next_element_from_buffer;
4811 reseat_at_next_visible_line_start (it, 1);
4812 it->face_before_selective_p = 1;
4813 }
4814
4815 return get_next_display_element (it);
4816 }
4817
4818
4819 /* Deliver an image display element. The iterator IT is already
4820 filled with image information (done in handle_display_prop). Value
4821 is always 1. */
4822
4823
4824 static int
4825 next_element_from_image (it)
4826 struct it *it;
4827 {
4828 it->what = IT_IMAGE;
4829 return 1;
4830 }
4831
4832
4833 /* Fill iterator IT with next display element from a stretch glyph
4834 property. IT->object is the value of the text property. Value is
4835 always 1. */
4836
4837 static int
4838 next_element_from_stretch (it)
4839 struct it *it;
4840 {
4841 it->what = IT_STRETCH;
4842 return 1;
4843 }
4844
4845
4846 /* Load IT with the next display element from current_buffer. Value
4847 is zero if end of buffer reached. IT->stop_charpos is the next
4848 position at which to stop and check for text properties or buffer
4849 end. */
4850
4851 static int
4852 next_element_from_buffer (it)
4853 struct it *it;
4854 {
4855 int success_p = 1;
4856
4857 /* Check this assumption, otherwise, we would never enter the
4858 if-statement, below. */
4859 xassert (IT_CHARPOS (*it) >= BEGV
4860 && IT_CHARPOS (*it) <= it->stop_charpos);
4861
4862 if (IT_CHARPOS (*it) >= it->stop_charpos)
4863 {
4864 if (IT_CHARPOS (*it) >= it->end_charpos)
4865 {
4866 int overlay_strings_follow_p;
4867
4868 /* End of the game, except when overlay strings follow that
4869 haven't been returned yet. */
4870 if (it->overlay_strings_at_end_processed_p)
4871 overlay_strings_follow_p = 0;
4872 else
4873 {
4874 it->overlay_strings_at_end_processed_p = 1;
4875 overlay_strings_follow_p = get_overlay_strings (it, 0);
4876 }
4877
4878 if (overlay_strings_follow_p)
4879 success_p = get_next_display_element (it);
4880 else
4881 {
4882 it->what = IT_EOB;
4883 it->position = it->current.pos;
4884 success_p = 0;
4885 }
4886 }
4887 else
4888 {
4889 handle_stop (it);
4890 return get_next_display_element (it);
4891 }
4892 }
4893 else
4894 {
4895 /* No face changes, overlays etc. in sight, so just return a
4896 character from current_buffer. */
4897 unsigned char *p;
4898
4899 /* Maybe run the redisplay end trigger hook. Performance note:
4900 This doesn't seem to cost measurable time. */
4901 if (it->redisplay_end_trigger_charpos
4902 && it->glyph_row
4903 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4904 run_redisplay_end_trigger_hook (it);
4905
4906 /* Get the next character, maybe multibyte. */
4907 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4908 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4909 {
4910 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4911 - IT_BYTEPOS (*it));
4912 it->c = string_char_and_length (p, maxlen, &it->len);
4913 }
4914 else
4915 it->c = *p, it->len = 1;
4916
4917 /* Record what we have and where it came from. */
4918 it->what = IT_CHARACTER;;
4919 it->object = it->w->buffer;
4920 it->position = it->current.pos;
4921
4922 /* Normally we return the character found above, except when we
4923 really want to return an ellipsis for selective display. */
4924 if (it->selective)
4925 {
4926 if (it->c == '\n')
4927 {
4928 /* A value of selective > 0 means hide lines indented more
4929 than that number of columns. */
4930 if (it->selective > 0
4931 && IT_CHARPOS (*it) + 1 < ZV
4932 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4933 IT_BYTEPOS (*it) + 1,
4934 (double) it->selective)) /* iftc */
4935 {
4936 success_p = next_element_from_ellipsis (it);
4937 it->dpvec_char_len = -1;
4938 }
4939 }
4940 else if (it->c == '\r' && it->selective == -1)
4941 {
4942 /* A value of selective == -1 means that everything from the
4943 CR to the end of the line is invisible, with maybe an
4944 ellipsis displayed for it. */
4945 success_p = next_element_from_ellipsis (it);
4946 it->dpvec_char_len = -1;
4947 }
4948 }
4949 }
4950
4951 /* Value is zero if end of buffer reached. */
4952 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4953 return success_p;
4954 }
4955
4956
4957 /* Run the redisplay end trigger hook for IT. */
4958
4959 static void
4960 run_redisplay_end_trigger_hook (it)
4961 struct it *it;
4962 {
4963 Lisp_Object args[3];
4964
4965 /* IT->glyph_row should be non-null, i.e. we should be actually
4966 displaying something, or otherwise we should not run the hook. */
4967 xassert (it->glyph_row);
4968
4969 /* Set up hook arguments. */
4970 args[0] = Qredisplay_end_trigger_functions;
4971 args[1] = it->window;
4972 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4973 it->redisplay_end_trigger_charpos = 0;
4974
4975 /* Since we are *trying* to run these functions, don't try to run
4976 them again, even if they get an error. */
4977 it->w->redisplay_end_trigger = Qnil;
4978 Frun_hook_with_args (3, args);
4979
4980 /* Notice if it changed the face of the character we are on. */
4981 handle_face_prop (it);
4982 }
4983
4984
4985 /* Deliver a composition display element. The iterator IT is already
4986 filled with composition information (done in
4987 handle_composition_prop). Value is always 1. */
4988
4989 static int
4990 next_element_from_composition (it)
4991 struct it *it;
4992 {
4993 it->what = IT_COMPOSITION;
4994 it->position = (STRINGP (it->string)
4995 ? it->current.string_pos
4996 : it->current.pos);
4997 return 1;
4998 }
4999
5000
5001 \f
5002 /***********************************************************************
5003 Moving an iterator without producing glyphs
5004 ***********************************************************************/
5005
5006 /* Move iterator IT to a specified buffer or X position within one
5007 line on the display without producing glyphs.
5008
5009 OP should be a bit mask including some or all of these bits:
5010 MOVE_TO_X: Stop on reaching x-position TO_X.
5011 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5012 Regardless of OP's value, stop in reaching the end of the display line.
5013
5014 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5015 This means, in particular, that TO_X includes window's horizontal
5016 scroll amount.
5017
5018 The return value has several possible values that
5019 say what condition caused the scan to stop:
5020
5021 MOVE_POS_MATCH_OR_ZV
5022 - when TO_POS or ZV was reached.
5023
5024 MOVE_X_REACHED
5025 -when TO_X was reached before TO_POS or ZV were reached.
5026
5027 MOVE_LINE_CONTINUED
5028 - when we reached the end of the display area and the line must
5029 be continued.
5030
5031 MOVE_LINE_TRUNCATED
5032 - when we reached the end of the display area and the line is
5033 truncated.
5034
5035 MOVE_NEWLINE_OR_CR
5036 - when we stopped at a line end, i.e. a newline or a CR and selective
5037 display is on. */
5038
5039 static enum move_it_result
5040 move_it_in_display_line_to (it, to_charpos, to_x, op)
5041 struct it *it;
5042 int to_charpos, to_x, op;
5043 {
5044 enum move_it_result result = MOVE_UNDEFINED;
5045 struct glyph_row *saved_glyph_row;
5046
5047 /* Don't produce glyphs in produce_glyphs. */
5048 saved_glyph_row = it->glyph_row;
5049 it->glyph_row = NULL;
5050
5051 while (1)
5052 {
5053 int x, i, ascent = 0, descent = 0;
5054
5055 /* Stop when ZV or TO_CHARPOS reached. */
5056 if (!get_next_display_element (it)
5057 || ((op & MOVE_TO_POS) != 0
5058 && BUFFERP (it->object)
5059 && IT_CHARPOS (*it) >= to_charpos))
5060 {
5061 result = MOVE_POS_MATCH_OR_ZV;
5062 break;
5063 }
5064
5065 /* The call to produce_glyphs will get the metrics of the
5066 display element IT is loaded with. We record in x the
5067 x-position before this display element in case it does not
5068 fit on the line. */
5069 x = it->current_x;
5070
5071 /* Remember the line height so far in case the next element doesn't
5072 fit on the line. */
5073 if (!it->truncate_lines_p)
5074 {
5075 ascent = it->max_ascent;
5076 descent = it->max_descent;
5077 }
5078
5079 PRODUCE_GLYPHS (it);
5080
5081 if (it->area != TEXT_AREA)
5082 {
5083 set_iterator_to_next (it, 1);
5084 continue;
5085 }
5086
5087 /* The number of glyphs we get back in IT->nglyphs will normally
5088 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5089 character on a terminal frame, or (iii) a line end. For the
5090 second case, IT->nglyphs - 1 padding glyphs will be present
5091 (on X frames, there is only one glyph produced for a
5092 composite character.
5093
5094 The behavior implemented below means, for continuation lines,
5095 that as many spaces of a TAB as fit on the current line are
5096 displayed there. For terminal frames, as many glyphs of a
5097 multi-glyph character are displayed in the current line, too.
5098 This is what the old redisplay code did, and we keep it that
5099 way. Under X, the whole shape of a complex character must
5100 fit on the line or it will be completely displayed in the
5101 next line.
5102
5103 Note that both for tabs and padding glyphs, all glyphs have
5104 the same width. */
5105 if (it->nglyphs)
5106 {
5107 /* More than one glyph or glyph doesn't fit on line. All
5108 glyphs have the same width. */
5109 int single_glyph_width = it->pixel_width / it->nglyphs;
5110 int new_x;
5111
5112 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5113 {
5114 new_x = x + single_glyph_width;
5115
5116 /* We want to leave anything reaching TO_X to the caller. */
5117 if ((op & MOVE_TO_X) && new_x > to_x)
5118 {
5119 it->current_x = x;
5120 result = MOVE_X_REACHED;
5121 break;
5122 }
5123 else if (/* Lines are continued. */
5124 !it->truncate_lines_p
5125 && (/* And glyph doesn't fit on the line. */
5126 new_x > it->last_visible_x
5127 /* Or it fits exactly and we're on a window
5128 system frame. */
5129 || (new_x == it->last_visible_x
5130 && FRAME_WINDOW_P (it->f))))
5131 {
5132 if (/* IT->hpos == 0 means the very first glyph
5133 doesn't fit on the line, e.g. a wide image. */
5134 it->hpos == 0
5135 || (new_x == it->last_visible_x
5136 && FRAME_WINDOW_P (it->f)))
5137 {
5138 ++it->hpos;
5139 it->current_x = new_x;
5140 if (i == it->nglyphs - 1)
5141 set_iterator_to_next (it, 1);
5142 }
5143 else
5144 {
5145 it->current_x = x;
5146 it->max_ascent = ascent;
5147 it->max_descent = descent;
5148 }
5149
5150 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5151 IT_CHARPOS (*it)));
5152 result = MOVE_LINE_CONTINUED;
5153 break;
5154 }
5155 else if (new_x > it->first_visible_x)
5156 {
5157 /* Glyph is visible. Increment number of glyphs that
5158 would be displayed. */
5159 ++it->hpos;
5160 }
5161 else
5162 {
5163 /* Glyph is completely off the left margin of the display
5164 area. Nothing to do. */
5165 }
5166 }
5167
5168 if (result != MOVE_UNDEFINED)
5169 break;
5170 }
5171 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5172 {
5173 /* Stop when TO_X specified and reached. This check is
5174 necessary here because of lines consisting of a line end,
5175 only. The line end will not produce any glyphs and we
5176 would never get MOVE_X_REACHED. */
5177 xassert (it->nglyphs == 0);
5178 result = MOVE_X_REACHED;
5179 break;
5180 }
5181
5182 /* Is this a line end? If yes, we're done. */
5183 if (ITERATOR_AT_END_OF_LINE_P (it))
5184 {
5185 result = MOVE_NEWLINE_OR_CR;
5186 break;
5187 }
5188
5189 /* The current display element has been consumed. Advance
5190 to the next. */
5191 set_iterator_to_next (it, 1);
5192
5193 /* Stop if lines are truncated and IT's current x-position is
5194 past the right edge of the window now. */
5195 if (it->truncate_lines_p
5196 && it->current_x >= it->last_visible_x)
5197 {
5198 result = MOVE_LINE_TRUNCATED;
5199 break;
5200 }
5201 }
5202
5203 /* Restore the iterator settings altered at the beginning of this
5204 function. */
5205 it->glyph_row = saved_glyph_row;
5206 return result;
5207 }
5208
5209
5210 /* Move IT forward until it satisfies one or more of the criteria in
5211 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5212
5213 OP is a bit-mask that specifies where to stop, and in particular,
5214 which of those four position arguments makes a difference. See the
5215 description of enum move_operation_enum.
5216
5217 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5218 screen line, this function will set IT to the next position >
5219 TO_CHARPOS. */
5220
5221 void
5222 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5223 struct it *it;
5224 int to_charpos, to_x, to_y, to_vpos;
5225 int op;
5226 {
5227 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5228 int line_height;
5229 int reached = 0;
5230
5231 for (;;)
5232 {
5233 if (op & MOVE_TO_VPOS)
5234 {
5235 /* If no TO_CHARPOS and no TO_X specified, stop at the
5236 start of the line TO_VPOS. */
5237 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5238 {
5239 if (it->vpos == to_vpos)
5240 {
5241 reached = 1;
5242 break;
5243 }
5244 else
5245 skip = move_it_in_display_line_to (it, -1, -1, 0);
5246 }
5247 else
5248 {
5249 /* TO_VPOS >= 0 means stop at TO_X in the line at
5250 TO_VPOS, or at TO_POS, whichever comes first. */
5251 if (it->vpos == to_vpos)
5252 {
5253 reached = 2;
5254 break;
5255 }
5256
5257 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5258
5259 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5260 {
5261 reached = 3;
5262 break;
5263 }
5264 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5265 {
5266 /* We have reached TO_X but not in the line we want. */
5267 skip = move_it_in_display_line_to (it, to_charpos,
5268 -1, MOVE_TO_POS);
5269 if (skip == MOVE_POS_MATCH_OR_ZV)
5270 {
5271 reached = 4;
5272 break;
5273 }
5274 }
5275 }
5276 }
5277 else if (op & MOVE_TO_Y)
5278 {
5279 struct it it_backup;
5280
5281 /* TO_Y specified means stop at TO_X in the line containing
5282 TO_Y---or at TO_CHARPOS if this is reached first. The
5283 problem is that we can't really tell whether the line
5284 contains TO_Y before we have completely scanned it, and
5285 this may skip past TO_X. What we do is to first scan to
5286 TO_X.
5287
5288 If TO_X is not specified, use a TO_X of zero. The reason
5289 is to make the outcome of this function more predictable.
5290 If we didn't use TO_X == 0, we would stop at the end of
5291 the line which is probably not what a caller would expect
5292 to happen. */
5293 skip = move_it_in_display_line_to (it, to_charpos,
5294 ((op & MOVE_TO_X)
5295 ? to_x : 0),
5296 (MOVE_TO_X
5297 | (op & MOVE_TO_POS)));
5298
5299 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5300 if (skip == MOVE_POS_MATCH_OR_ZV)
5301 {
5302 reached = 5;
5303 break;
5304 }
5305
5306 /* If TO_X was reached, we would like to know whether TO_Y
5307 is in the line. This can only be said if we know the
5308 total line height which requires us to scan the rest of
5309 the line. */
5310 if (skip == MOVE_X_REACHED)
5311 {
5312 it_backup = *it;
5313 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5314 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5315 op & MOVE_TO_POS);
5316 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5317 }
5318
5319 /* Now, decide whether TO_Y is in this line. */
5320 line_height = it->max_ascent + it->max_descent;
5321 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5322
5323 if (to_y >= it->current_y
5324 && to_y < it->current_y + line_height)
5325 {
5326 if (skip == MOVE_X_REACHED)
5327 /* If TO_Y is in this line and TO_X was reached above,
5328 we scanned too far. We have to restore IT's settings
5329 to the ones before skipping. */
5330 *it = it_backup;
5331 reached = 6;
5332 }
5333 else if (skip == MOVE_X_REACHED)
5334 {
5335 skip = skip2;
5336 if (skip == MOVE_POS_MATCH_OR_ZV)
5337 reached = 7;
5338 }
5339
5340 if (reached)
5341 break;
5342 }
5343 else
5344 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5345
5346 switch (skip)
5347 {
5348 case MOVE_POS_MATCH_OR_ZV:
5349 reached = 8;
5350 goto out;
5351
5352 case MOVE_NEWLINE_OR_CR:
5353 set_iterator_to_next (it, 1);
5354 it->continuation_lines_width = 0;
5355 break;
5356
5357 case MOVE_LINE_TRUNCATED:
5358 it->continuation_lines_width = 0;
5359 reseat_at_next_visible_line_start (it, 0);
5360 if ((op & MOVE_TO_POS) != 0
5361 && IT_CHARPOS (*it) > to_charpos)
5362 {
5363 reached = 9;
5364 goto out;
5365 }
5366 break;
5367
5368 case MOVE_LINE_CONTINUED:
5369 it->continuation_lines_width += it->current_x;
5370 break;
5371
5372 default:
5373 abort ();
5374 }
5375
5376 /* Reset/increment for the next run. */
5377 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5378 it->current_x = it->hpos = 0;
5379 it->current_y += it->max_ascent + it->max_descent;
5380 ++it->vpos;
5381 last_height = it->max_ascent + it->max_descent;
5382 last_max_ascent = it->max_ascent;
5383 it->max_ascent = it->max_descent = 0;
5384 }
5385
5386 out:
5387
5388 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5389 }
5390
5391
5392 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5393
5394 If DY > 0, move IT backward at least that many pixels. DY = 0
5395 means move IT backward to the preceding line start or BEGV. This
5396 function may move over more than DY pixels if IT->current_y - DY
5397 ends up in the middle of a line; in this case IT->current_y will be
5398 set to the top of the line moved to. */
5399
5400 void
5401 move_it_vertically_backward (it, dy)
5402 struct it *it;
5403 int dy;
5404 {
5405 int nlines, h;
5406 struct it it2, it3;
5407 int start_pos = IT_CHARPOS (*it);
5408
5409 xassert (dy >= 0);
5410
5411 /* Estimate how many newlines we must move back. */
5412 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5413
5414 /* Set the iterator's position that many lines back. */
5415 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5416 back_to_previous_visible_line_start (it);
5417
5418 /* Reseat the iterator here. When moving backward, we don't want
5419 reseat to skip forward over invisible text, set up the iterator
5420 to deliver from overlay strings at the new position etc. So,
5421 use reseat_1 here. */
5422 reseat_1 (it, it->current.pos, 1);
5423
5424 /* We are now surely at a line start. */
5425 it->current_x = it->hpos = 0;
5426 it->continuation_lines_width = 0;
5427
5428 /* Move forward and see what y-distance we moved. First move to the
5429 start of the next line so that we get its height. We need this
5430 height to be able to tell whether we reached the specified
5431 y-distance. */
5432 it2 = *it;
5433 it2.max_ascent = it2.max_descent = 0;
5434 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5435 MOVE_TO_POS | MOVE_TO_VPOS);
5436 xassert (IT_CHARPOS (*it) >= BEGV);
5437 it3 = it2;
5438
5439 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5440 xassert (IT_CHARPOS (*it) >= BEGV);
5441 h = it2.current_y - it->current_y;
5442 nlines = it2.vpos - it->vpos;
5443
5444 /* Correct IT's y and vpos position. */
5445 it->vpos -= nlines;
5446 it->current_y -= h;
5447
5448 if (dy == 0)
5449 {
5450 /* DY == 0 means move to the start of the screen line. The
5451 value of nlines is > 0 if continuation lines were involved. */
5452 if (nlines > 0)
5453 move_it_by_lines (it, nlines, 1);
5454 xassert (IT_CHARPOS (*it) <= start_pos);
5455 }
5456 else if (nlines)
5457 {
5458 /* The y-position we try to reach. Note that h has been
5459 subtracted in front of the if-statement. */
5460 int target_y = it->current_y + h - dy;
5461 int y0 = it3.current_y;
5462 int y1 = line_bottom_y (&it3);
5463 int line_height = y1 - y0;
5464
5465 /* If we did not reach target_y, try to move further backward if
5466 we can. If we moved too far backward, try to move forward. */
5467 if (target_y < it->current_y
5468 /* This is heuristic. In a window that's 3 lines high, with
5469 a line height of 13 pixels each, recentering with point
5470 on the bottom line will try to move -39/2 = 19 pixels
5471 backward. Try to avoid moving into the first line. */
5472 && it->current_y - target_y > line_height / 3 * 2
5473 && IT_CHARPOS (*it) > BEGV)
5474 {
5475 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5476 target_y - it->current_y));
5477 move_it_vertically (it, target_y - it->current_y);
5478 xassert (IT_CHARPOS (*it) >= BEGV);
5479 }
5480 else if (target_y >= it->current_y + line_height
5481 && IT_CHARPOS (*it) < ZV)
5482 {
5483 /* Should move forward by at least one line, maybe more.
5484
5485 Note: Calling move_it_by_lines can be expensive on
5486 terminal frames, where compute_motion is used (via
5487 vmotion) to do the job, when there are very long lines
5488 and truncate-lines is nil. That's the reason for
5489 treating terminal frames specially here. */
5490
5491 if (!FRAME_WINDOW_P (it->f))
5492 move_it_vertically (it, target_y - (it->current_y + line_height));
5493 else
5494 {
5495 do
5496 {
5497 move_it_by_lines (it, 1, 1);
5498 }
5499 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5500 }
5501
5502 xassert (IT_CHARPOS (*it) >= BEGV);
5503 }
5504 }
5505 }
5506
5507
5508 /* Move IT by a specified amount of pixel lines DY. DY negative means
5509 move backwards. DY = 0 means move to start of screen line. At the
5510 end, IT will be on the start of a screen line. */
5511
5512 void
5513 move_it_vertically (it, dy)
5514 struct it *it;
5515 int dy;
5516 {
5517 if (dy <= 0)
5518 move_it_vertically_backward (it, -dy);
5519 else if (dy > 0)
5520 {
5521 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5522 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5523 MOVE_TO_POS | MOVE_TO_Y);
5524 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5525
5526 /* If buffer ends in ZV without a newline, move to the start of
5527 the line to satisfy the post-condition. */
5528 if (IT_CHARPOS (*it) == ZV
5529 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5530 move_it_by_lines (it, 0, 0);
5531 }
5532 }
5533
5534
5535 /* Move iterator IT past the end of the text line it is in. */
5536
5537 void
5538 move_it_past_eol (it)
5539 struct it *it;
5540 {
5541 enum move_it_result rc;
5542
5543 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5544 if (rc == MOVE_NEWLINE_OR_CR)
5545 set_iterator_to_next (it, 0);
5546 }
5547
5548
5549 #if 0 /* Currently not used. */
5550
5551 /* Return non-zero if some text between buffer positions START_CHARPOS
5552 and END_CHARPOS is invisible. IT->window is the window for text
5553 property lookup. */
5554
5555 static int
5556 invisible_text_between_p (it, start_charpos, end_charpos)
5557 struct it *it;
5558 int start_charpos, end_charpos;
5559 {
5560 Lisp_Object prop, limit;
5561 int invisible_found_p;
5562
5563 xassert (it != NULL && start_charpos <= end_charpos);
5564
5565 /* Is text at START invisible? */
5566 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5567 it->window);
5568 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5569 invisible_found_p = 1;
5570 else
5571 {
5572 limit = Fnext_single_char_property_change (make_number (start_charpos),
5573 Qinvisible, Qnil,
5574 make_number (end_charpos));
5575 invisible_found_p = XFASTINT (limit) < end_charpos;
5576 }
5577
5578 return invisible_found_p;
5579 }
5580
5581 #endif /* 0 */
5582
5583
5584 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5585 negative means move up. DVPOS == 0 means move to the start of the
5586 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5587 NEED_Y_P is zero, IT->current_y will be left unchanged.
5588
5589 Further optimization ideas: If we would know that IT->f doesn't use
5590 a face with proportional font, we could be faster for
5591 truncate-lines nil. */
5592
5593 void
5594 move_it_by_lines (it, dvpos, need_y_p)
5595 struct it *it;
5596 int dvpos, need_y_p;
5597 {
5598 struct position pos;
5599
5600 if (!FRAME_WINDOW_P (it->f))
5601 {
5602 struct text_pos textpos;
5603
5604 /* We can use vmotion on frames without proportional fonts. */
5605 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5606 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5607 reseat (it, textpos, 1);
5608 it->vpos += pos.vpos;
5609 it->current_y += pos.vpos;
5610 }
5611 else if (dvpos == 0)
5612 {
5613 /* DVPOS == 0 means move to the start of the screen line. */
5614 move_it_vertically_backward (it, 0);
5615 xassert (it->current_x == 0 && it->hpos == 0);
5616 }
5617 else if (dvpos > 0)
5618 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5619 else
5620 {
5621 struct it it2;
5622 int start_charpos, i;
5623
5624 /* Start at the beginning of the screen line containing IT's
5625 position. */
5626 move_it_vertically_backward (it, 0);
5627
5628 /* Go back -DVPOS visible lines and reseat the iterator there. */
5629 start_charpos = IT_CHARPOS (*it);
5630 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5631 back_to_previous_visible_line_start (it);
5632 reseat (it, it->current.pos, 1);
5633 it->current_x = it->hpos = 0;
5634
5635 /* Above call may have moved too far if continuation lines
5636 are involved. Scan forward and see if it did. */
5637 it2 = *it;
5638 it2.vpos = it2.current_y = 0;
5639 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5640 it->vpos -= it2.vpos;
5641 it->current_y -= it2.current_y;
5642 it->current_x = it->hpos = 0;
5643
5644 /* If we moved too far, move IT some lines forward. */
5645 if (it2.vpos > -dvpos)
5646 {
5647 int delta = it2.vpos + dvpos;
5648 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5649 }
5650 }
5651 }
5652
5653 /* Return 1 if IT points into the middle of a display vector. */
5654
5655 int
5656 in_display_vector_p (it)
5657 struct it *it;
5658 {
5659 return (it->method == next_element_from_display_vector
5660 && it->current.dpvec_index > 0
5661 && it->dpvec + it->current.dpvec_index != it->dpend);
5662 }
5663
5664 \f
5665 /***********************************************************************
5666 Messages
5667 ***********************************************************************/
5668
5669
5670 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5671 to *Messages*. */
5672
5673 void
5674 add_to_log (format, arg1, arg2)
5675 char *format;
5676 Lisp_Object arg1, arg2;
5677 {
5678 Lisp_Object args[3];
5679 Lisp_Object msg, fmt;
5680 char *buffer;
5681 int len;
5682 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5683
5684 /* Do nothing if called asynchronously. Inserting text into
5685 a buffer may call after-change-functions and alike and
5686 that would means running Lisp asynchronously. */
5687 if (handling_signal)
5688 return;
5689
5690 fmt = msg = Qnil;
5691 GCPRO4 (fmt, msg, arg1, arg2);
5692
5693 args[0] = fmt = build_string (format);
5694 args[1] = arg1;
5695 args[2] = arg2;
5696 msg = Fformat (3, args);
5697
5698 len = SBYTES (msg) + 1;
5699 buffer = (char *) alloca (len);
5700 bcopy (SDATA (msg), buffer, len);
5701
5702 message_dolog (buffer, len - 1, 1, 0);
5703 UNGCPRO;
5704 }
5705
5706
5707 /* Output a newline in the *Messages* buffer if "needs" one. */
5708
5709 void
5710 message_log_maybe_newline ()
5711 {
5712 if (message_log_need_newline)
5713 message_dolog ("", 0, 1, 0);
5714 }
5715
5716
5717 /* Add a string M of length NBYTES to the message log, optionally
5718 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5719 nonzero, means interpret the contents of M as multibyte. This
5720 function calls low-level routines in order to bypass text property
5721 hooks, etc. which might not be safe to run. */
5722
5723 void
5724 message_dolog (m, nbytes, nlflag, multibyte)
5725 const char *m;
5726 int nbytes, nlflag, multibyte;
5727 {
5728 if (!NILP (Vmemory_full))
5729 return;
5730
5731 if (!NILP (Vmessage_log_max))
5732 {
5733 struct buffer *oldbuf;
5734 Lisp_Object oldpoint, oldbegv, oldzv;
5735 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5736 int point_at_end = 0;
5737 int zv_at_end = 0;
5738 Lisp_Object old_deactivate_mark, tem;
5739 struct gcpro gcpro1;
5740
5741 old_deactivate_mark = Vdeactivate_mark;
5742 oldbuf = current_buffer;
5743 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5744 current_buffer->undo_list = Qt;
5745
5746 oldpoint = message_dolog_marker1;
5747 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5748 oldbegv = message_dolog_marker2;
5749 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5750 oldzv = message_dolog_marker3;
5751 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5752 GCPRO1 (old_deactivate_mark);
5753
5754 if (PT == Z)
5755 point_at_end = 1;
5756 if (ZV == Z)
5757 zv_at_end = 1;
5758
5759 BEGV = BEG;
5760 BEGV_BYTE = BEG_BYTE;
5761 ZV = Z;
5762 ZV_BYTE = Z_BYTE;
5763 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5764
5765 /* Insert the string--maybe converting multibyte to single byte
5766 or vice versa, so that all the text fits the buffer. */
5767 if (multibyte
5768 && NILP (current_buffer->enable_multibyte_characters))
5769 {
5770 int i, c, char_bytes;
5771 unsigned char work[1];
5772
5773 /* Convert a multibyte string to single-byte
5774 for the *Message* buffer. */
5775 for (i = 0; i < nbytes; i += char_bytes)
5776 {
5777 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5778 work[0] = (SINGLE_BYTE_CHAR_P (c)
5779 ? c
5780 : multibyte_char_to_unibyte (c, Qnil));
5781 insert_1_both (work, 1, 1, 1, 0, 0);
5782 }
5783 }
5784 else if (! multibyte
5785 && ! NILP (current_buffer->enable_multibyte_characters))
5786 {
5787 int i, c, char_bytes;
5788 unsigned char *msg = (unsigned char *) m;
5789 unsigned char str[MAX_MULTIBYTE_LENGTH];
5790 /* Convert a single-byte string to multibyte
5791 for the *Message* buffer. */
5792 for (i = 0; i < nbytes; i++)
5793 {
5794 c = unibyte_char_to_multibyte (msg[i]);
5795 char_bytes = CHAR_STRING (c, str);
5796 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5797 }
5798 }
5799 else if (nbytes)
5800 insert_1 (m, nbytes, 1, 0, 0);
5801
5802 if (nlflag)
5803 {
5804 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5805 insert_1 ("\n", 1, 1, 0, 0);
5806
5807 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5808 this_bol = PT;
5809 this_bol_byte = PT_BYTE;
5810
5811 /* See if this line duplicates the previous one.
5812 If so, combine duplicates. */
5813 if (this_bol > BEG)
5814 {
5815 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5816 prev_bol = PT;
5817 prev_bol_byte = PT_BYTE;
5818
5819 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5820 this_bol, this_bol_byte);
5821 if (dup)
5822 {
5823 del_range_both (prev_bol, prev_bol_byte,
5824 this_bol, this_bol_byte, 0);
5825 if (dup > 1)
5826 {
5827 char dupstr[40];
5828 int duplen;
5829
5830 /* If you change this format, don't forget to also
5831 change message_log_check_duplicate. */
5832 sprintf (dupstr, " [%d times]", dup);
5833 duplen = strlen (dupstr);
5834 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5835 insert_1 (dupstr, duplen, 1, 0, 1);
5836 }
5837 }
5838 }
5839
5840 /* If we have more than the desired maximum number of lines
5841 in the *Messages* buffer now, delete the oldest ones.
5842 This is safe because we don't have undo in this buffer. */
5843
5844 if (NATNUMP (Vmessage_log_max))
5845 {
5846 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5847 -XFASTINT (Vmessage_log_max) - 1, 0);
5848 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5849 }
5850 }
5851 BEGV = XMARKER (oldbegv)->charpos;
5852 BEGV_BYTE = marker_byte_position (oldbegv);
5853
5854 if (zv_at_end)
5855 {
5856 ZV = Z;
5857 ZV_BYTE = Z_BYTE;
5858 }
5859 else
5860 {
5861 ZV = XMARKER (oldzv)->charpos;
5862 ZV_BYTE = marker_byte_position (oldzv);
5863 }
5864
5865 if (point_at_end)
5866 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5867 else
5868 /* We can't do Fgoto_char (oldpoint) because it will run some
5869 Lisp code. */
5870 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5871 XMARKER (oldpoint)->bytepos);
5872
5873 UNGCPRO;
5874 unchain_marker (oldpoint);
5875 unchain_marker (oldbegv);
5876 unchain_marker (oldzv);
5877
5878 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5879 set_buffer_internal (oldbuf);
5880 if (NILP (tem))
5881 windows_or_buffers_changed = old_windows_or_buffers_changed;
5882 message_log_need_newline = !nlflag;
5883 Vdeactivate_mark = old_deactivate_mark;
5884 }
5885 }
5886
5887
5888 /* We are at the end of the buffer after just having inserted a newline.
5889 (Note: We depend on the fact we won't be crossing the gap.)
5890 Check to see if the most recent message looks a lot like the previous one.
5891 Return 0 if different, 1 if the new one should just replace it, or a
5892 value N > 1 if we should also append " [N times]". */
5893
5894 static int
5895 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5896 int prev_bol, this_bol;
5897 int prev_bol_byte, this_bol_byte;
5898 {
5899 int i;
5900 int len = Z_BYTE - 1 - this_bol_byte;
5901 int seen_dots = 0;
5902 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5903 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5904
5905 for (i = 0; i < len; i++)
5906 {
5907 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5908 seen_dots = 1;
5909 if (p1[i] != p2[i])
5910 return seen_dots;
5911 }
5912 p1 += len;
5913 if (*p1 == '\n')
5914 return 2;
5915 if (*p1++ == ' ' && *p1++ == '[')
5916 {
5917 int n = 0;
5918 while (*p1 >= '0' && *p1 <= '9')
5919 n = n * 10 + *p1++ - '0';
5920 if (strncmp (p1, " times]\n", 8) == 0)
5921 return n+1;
5922 }
5923 return 0;
5924 }
5925
5926
5927 /* Display an echo area message M with a specified length of NBYTES
5928 bytes. The string may include null characters. If M is 0, clear
5929 out any existing message, and let the mini-buffer text show
5930 through.
5931
5932 The buffer M must continue to exist until after the echo area gets
5933 cleared or some other message gets displayed there. This means do
5934 not pass text that is stored in a Lisp string; do not pass text in
5935 a buffer that was alloca'd. */
5936
5937 void
5938 message2 (m, nbytes, multibyte)
5939 const char *m;
5940 int nbytes;
5941 int multibyte;
5942 {
5943 /* First flush out any partial line written with print. */
5944 message_log_maybe_newline ();
5945 if (m)
5946 message_dolog (m, nbytes, 1, multibyte);
5947 message2_nolog (m, nbytes, multibyte);
5948 }
5949
5950
5951 /* The non-logging counterpart of message2. */
5952
5953 void
5954 message2_nolog (m, nbytes, multibyte)
5955 const char *m;
5956 int nbytes, multibyte;
5957 {
5958 struct frame *sf = SELECTED_FRAME ();
5959 message_enable_multibyte = multibyte;
5960
5961 if (noninteractive)
5962 {
5963 if (noninteractive_need_newline)
5964 putc ('\n', stderr);
5965 noninteractive_need_newline = 0;
5966 if (m)
5967 fwrite (m, nbytes, 1, stderr);
5968 if (cursor_in_echo_area == 0)
5969 fprintf (stderr, "\n");
5970 fflush (stderr);
5971 }
5972 /* A null message buffer means that the frame hasn't really been
5973 initialized yet. Error messages get reported properly by
5974 cmd_error, so this must be just an informative message; toss it. */
5975 else if (INTERACTIVE
5976 && sf->glyphs_initialized_p
5977 && FRAME_MESSAGE_BUF (sf))
5978 {
5979 Lisp_Object mini_window;
5980 struct frame *f;
5981
5982 /* Get the frame containing the mini-buffer
5983 that the selected frame is using. */
5984 mini_window = FRAME_MINIBUF_WINDOW (sf);
5985 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5986
5987 FRAME_SAMPLE_VISIBILITY (f);
5988 if (FRAME_VISIBLE_P (sf)
5989 && ! FRAME_VISIBLE_P (f))
5990 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5991
5992 if (m)
5993 {
5994 set_message (m, Qnil, nbytes, multibyte);
5995 if (minibuffer_auto_raise)
5996 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5997 }
5998 else
5999 clear_message (1, 1);
6000
6001 do_pending_window_change (0);
6002 echo_area_display (1);
6003 do_pending_window_change (0);
6004 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6005 (*frame_up_to_date_hook) (f);
6006 }
6007 }
6008
6009
6010 /* Display an echo area message M with a specified length of NBYTES
6011 bytes. The string may include null characters. If M is not a
6012 string, clear out any existing message, and let the mini-buffer
6013 text show through. */
6014
6015 void
6016 message3 (m, nbytes, multibyte)
6017 Lisp_Object m;
6018 int nbytes;
6019 int multibyte;
6020 {
6021 struct gcpro gcpro1;
6022
6023 GCPRO1 (m);
6024
6025 /* First flush out any partial line written with print. */
6026 message_log_maybe_newline ();
6027 if (STRINGP (m))
6028 message_dolog (SDATA (m), nbytes, 1, multibyte);
6029 message3_nolog (m, nbytes, multibyte);
6030
6031 UNGCPRO;
6032 }
6033
6034
6035 /* The non-logging version of message3. */
6036
6037 void
6038 message3_nolog (m, nbytes, multibyte)
6039 Lisp_Object m;
6040 int nbytes, multibyte;
6041 {
6042 struct frame *sf = SELECTED_FRAME ();
6043 message_enable_multibyte = multibyte;
6044
6045 if (noninteractive)
6046 {
6047 if (noninteractive_need_newline)
6048 putc ('\n', stderr);
6049 noninteractive_need_newline = 0;
6050 if (STRINGP (m))
6051 fwrite (SDATA (m), nbytes, 1, stderr);
6052 if (cursor_in_echo_area == 0)
6053 fprintf (stderr, "\n");
6054 fflush (stderr);
6055 }
6056 /* A null message buffer means that the frame hasn't really been
6057 initialized yet. Error messages get reported properly by
6058 cmd_error, so this must be just an informative message; toss it. */
6059 else if (INTERACTIVE
6060 && sf->glyphs_initialized_p
6061 && FRAME_MESSAGE_BUF (sf))
6062 {
6063 Lisp_Object mini_window;
6064 Lisp_Object frame;
6065 struct frame *f;
6066
6067 /* Get the frame containing the mini-buffer
6068 that the selected frame is using. */
6069 mini_window = FRAME_MINIBUF_WINDOW (sf);
6070 frame = XWINDOW (mini_window)->frame;
6071 f = XFRAME (frame);
6072
6073 FRAME_SAMPLE_VISIBILITY (f);
6074 if (FRAME_VISIBLE_P (sf)
6075 && !FRAME_VISIBLE_P (f))
6076 Fmake_frame_visible (frame);
6077
6078 if (STRINGP (m) && SCHARS (m) > 0)
6079 {
6080 set_message (NULL, m, nbytes, multibyte);
6081 if (minibuffer_auto_raise)
6082 Fraise_frame (frame);
6083 }
6084 else
6085 clear_message (1, 1);
6086
6087 do_pending_window_change (0);
6088 echo_area_display (1);
6089 do_pending_window_change (0);
6090 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6091 (*frame_up_to_date_hook) (f);
6092 }
6093 }
6094
6095
6096 /* Display a null-terminated echo area message M. If M is 0, clear
6097 out any existing message, and let the mini-buffer text show through.
6098
6099 The buffer M must continue to exist until after the echo area gets
6100 cleared or some other message gets displayed there. Do not pass
6101 text that is stored in a Lisp string. Do not pass text in a buffer
6102 that was alloca'd. */
6103
6104 void
6105 message1 (m)
6106 char *m;
6107 {
6108 message2 (m, (m ? strlen (m) : 0), 0);
6109 }
6110
6111
6112 /* The non-logging counterpart of message1. */
6113
6114 void
6115 message1_nolog (m)
6116 char *m;
6117 {
6118 message2_nolog (m, (m ? strlen (m) : 0), 0);
6119 }
6120
6121 /* Display a message M which contains a single %s
6122 which gets replaced with STRING. */
6123
6124 void
6125 message_with_string (m, string, log)
6126 char *m;
6127 Lisp_Object string;
6128 int log;
6129 {
6130 CHECK_STRING (string);
6131
6132 if (noninteractive)
6133 {
6134 if (m)
6135 {
6136 if (noninteractive_need_newline)
6137 putc ('\n', stderr);
6138 noninteractive_need_newline = 0;
6139 fprintf (stderr, m, SDATA (string));
6140 if (cursor_in_echo_area == 0)
6141 fprintf (stderr, "\n");
6142 fflush (stderr);
6143 }
6144 }
6145 else if (INTERACTIVE)
6146 {
6147 /* The frame whose minibuffer we're going to display the message on.
6148 It may be larger than the selected frame, so we need
6149 to use its buffer, not the selected frame's buffer. */
6150 Lisp_Object mini_window;
6151 struct frame *f, *sf = SELECTED_FRAME ();
6152
6153 /* Get the frame containing the minibuffer
6154 that the selected frame is using. */
6155 mini_window = FRAME_MINIBUF_WINDOW (sf);
6156 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6157
6158 /* A null message buffer means that the frame hasn't really been
6159 initialized yet. Error messages get reported properly by
6160 cmd_error, so this must be just an informative message; toss it. */
6161 if (FRAME_MESSAGE_BUF (f))
6162 {
6163 Lisp_Object args[2], message;
6164 struct gcpro gcpro1, gcpro2;
6165
6166 args[0] = build_string (m);
6167 args[1] = message = string;
6168 GCPRO2 (args[0], message);
6169 gcpro1.nvars = 2;
6170
6171 message = Fformat (2, args);
6172
6173 if (log)
6174 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6175 else
6176 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6177
6178 UNGCPRO;
6179
6180 /* Print should start at the beginning of the message
6181 buffer next time. */
6182 message_buf_print = 0;
6183 }
6184 }
6185 }
6186
6187
6188 /* Dump an informative message to the minibuf. If M is 0, clear out
6189 any existing message, and let the mini-buffer text show through. */
6190
6191 /* VARARGS 1 */
6192 void
6193 message (m, a1, a2, a3)
6194 char *m;
6195 EMACS_INT a1, a2, a3;
6196 {
6197 if (noninteractive)
6198 {
6199 if (m)
6200 {
6201 if (noninteractive_need_newline)
6202 putc ('\n', stderr);
6203 noninteractive_need_newline = 0;
6204 fprintf (stderr, m, a1, a2, a3);
6205 if (cursor_in_echo_area == 0)
6206 fprintf (stderr, "\n");
6207 fflush (stderr);
6208 }
6209 }
6210 else if (INTERACTIVE)
6211 {
6212 /* The frame whose mini-buffer we're going to display the message
6213 on. It may be larger than the selected frame, so we need to
6214 use its buffer, not the selected frame's buffer. */
6215 Lisp_Object mini_window;
6216 struct frame *f, *sf = SELECTED_FRAME ();
6217
6218 /* Get the frame containing the mini-buffer
6219 that the selected frame is using. */
6220 mini_window = FRAME_MINIBUF_WINDOW (sf);
6221 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6222
6223 /* A null message buffer means that the frame hasn't really been
6224 initialized yet. Error messages get reported properly by
6225 cmd_error, so this must be just an informative message; toss
6226 it. */
6227 if (FRAME_MESSAGE_BUF (f))
6228 {
6229 if (m)
6230 {
6231 int len;
6232 #ifdef NO_ARG_ARRAY
6233 char *a[3];
6234 a[0] = (char *) a1;
6235 a[1] = (char *) a2;
6236 a[2] = (char *) a3;
6237
6238 len = doprnt (FRAME_MESSAGE_BUF (f),
6239 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6240 #else
6241 len = doprnt (FRAME_MESSAGE_BUF (f),
6242 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6243 (char **) &a1);
6244 #endif /* NO_ARG_ARRAY */
6245
6246 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6247 }
6248 else
6249 message1 (0);
6250
6251 /* Print should start at the beginning of the message
6252 buffer next time. */
6253 message_buf_print = 0;
6254 }
6255 }
6256 }
6257
6258
6259 /* The non-logging version of message. */
6260
6261 void
6262 message_nolog (m, a1, a2, a3)
6263 char *m;
6264 EMACS_INT a1, a2, a3;
6265 {
6266 Lisp_Object old_log_max;
6267 old_log_max = Vmessage_log_max;
6268 Vmessage_log_max = Qnil;
6269 message (m, a1, a2, a3);
6270 Vmessage_log_max = old_log_max;
6271 }
6272
6273
6274 /* Display the current message in the current mini-buffer. This is
6275 only called from error handlers in process.c, and is not time
6276 critical. */
6277
6278 void
6279 update_echo_area ()
6280 {
6281 if (!NILP (echo_area_buffer[0]))
6282 {
6283 Lisp_Object string;
6284 string = Fcurrent_message ();
6285 message3 (string, SBYTES (string),
6286 !NILP (current_buffer->enable_multibyte_characters));
6287 }
6288 }
6289
6290
6291 /* Make sure echo area buffers in `echo_buffers' are live.
6292 If they aren't, make new ones. */
6293
6294 static void
6295 ensure_echo_area_buffers ()
6296 {
6297 int i;
6298
6299 for (i = 0; i < 2; ++i)
6300 if (!BUFFERP (echo_buffer[i])
6301 || NILP (XBUFFER (echo_buffer[i])->name))
6302 {
6303 char name[30];
6304 Lisp_Object old_buffer;
6305 int j;
6306
6307 old_buffer = echo_buffer[i];
6308 sprintf (name, " *Echo Area %d*", i);
6309 echo_buffer[i] = Fget_buffer_create (build_string (name));
6310 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6311
6312 for (j = 0; j < 2; ++j)
6313 if (EQ (old_buffer, echo_area_buffer[j]))
6314 echo_area_buffer[j] = echo_buffer[i];
6315 }
6316 }
6317
6318
6319 /* Call FN with args A1..A4 with either the current or last displayed
6320 echo_area_buffer as current buffer.
6321
6322 WHICH zero means use the current message buffer
6323 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6324 from echo_buffer[] and clear it.
6325
6326 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6327 suitable buffer from echo_buffer[] and clear it.
6328
6329 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6330 that the current message becomes the last displayed one, make
6331 choose a suitable buffer for echo_area_buffer[0], and clear it.
6332
6333 Value is what FN returns. */
6334
6335 static int
6336 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6337 struct window *w;
6338 int which;
6339 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6340 EMACS_INT a1;
6341 Lisp_Object a2;
6342 EMACS_INT a3, a4;
6343 {
6344 Lisp_Object buffer;
6345 int this_one, the_other, clear_buffer_p, rc;
6346 int count = SPECPDL_INDEX ();
6347
6348 /* If buffers aren't live, make new ones. */
6349 ensure_echo_area_buffers ();
6350
6351 clear_buffer_p = 0;
6352
6353 if (which == 0)
6354 this_one = 0, the_other = 1;
6355 else if (which > 0)
6356 this_one = 1, the_other = 0;
6357 else
6358 {
6359 this_one = 0, the_other = 1;
6360 clear_buffer_p = 1;
6361
6362 /* We need a fresh one in case the current echo buffer equals
6363 the one containing the last displayed echo area message. */
6364 if (!NILP (echo_area_buffer[this_one])
6365 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6366 echo_area_buffer[this_one] = Qnil;
6367 }
6368
6369 /* Choose a suitable buffer from echo_buffer[] is we don't
6370 have one. */
6371 if (NILP (echo_area_buffer[this_one]))
6372 {
6373 echo_area_buffer[this_one]
6374 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6375 ? echo_buffer[the_other]
6376 : echo_buffer[this_one]);
6377 clear_buffer_p = 1;
6378 }
6379
6380 buffer = echo_area_buffer[this_one];
6381
6382 /* Don't get confused by reusing the buffer used for echoing
6383 for a different purpose. */
6384 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6385 cancel_echoing ();
6386
6387 record_unwind_protect (unwind_with_echo_area_buffer,
6388 with_echo_area_buffer_unwind_data (w));
6389
6390 /* Make the echo area buffer current. Note that for display
6391 purposes, it is not necessary that the displayed window's buffer
6392 == current_buffer, except for text property lookup. So, let's
6393 only set that buffer temporarily here without doing a full
6394 Fset_window_buffer. We must also change w->pointm, though,
6395 because otherwise an assertions in unshow_buffer fails, and Emacs
6396 aborts. */
6397 set_buffer_internal_1 (XBUFFER (buffer));
6398 if (w)
6399 {
6400 w->buffer = buffer;
6401 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6402 }
6403
6404 current_buffer->undo_list = Qt;
6405 current_buffer->read_only = Qnil;
6406 specbind (Qinhibit_read_only, Qt);
6407 specbind (Qinhibit_modification_hooks, Qt);
6408
6409 if (clear_buffer_p && Z > BEG)
6410 del_range (BEG, Z);
6411
6412 xassert (BEGV >= BEG);
6413 xassert (ZV <= Z && ZV >= BEGV);
6414
6415 rc = fn (a1, a2, a3, a4);
6416
6417 xassert (BEGV >= BEG);
6418 xassert (ZV <= Z && ZV >= BEGV);
6419
6420 unbind_to (count, Qnil);
6421 return rc;
6422 }
6423
6424
6425 /* Save state that should be preserved around the call to the function
6426 FN called in with_echo_area_buffer. */
6427
6428 static Lisp_Object
6429 with_echo_area_buffer_unwind_data (w)
6430 struct window *w;
6431 {
6432 int i = 0;
6433 Lisp_Object vector;
6434
6435 /* Reduce consing by keeping one vector in
6436 Vwith_echo_area_save_vector. */
6437 vector = Vwith_echo_area_save_vector;
6438 Vwith_echo_area_save_vector = Qnil;
6439
6440 if (NILP (vector))
6441 vector = Fmake_vector (make_number (7), Qnil);
6442
6443 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6444 AREF (vector, i) = Vdeactivate_mark, ++i;
6445 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6446
6447 if (w)
6448 {
6449 XSETWINDOW (AREF (vector, i), w); ++i;
6450 AREF (vector, i) = w->buffer; ++i;
6451 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6452 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6453 }
6454 else
6455 {
6456 int end = i + 4;
6457 for (; i < end; ++i)
6458 AREF (vector, i) = Qnil;
6459 }
6460
6461 xassert (i == ASIZE (vector));
6462 return vector;
6463 }
6464
6465
6466 /* Restore global state from VECTOR which was created by
6467 with_echo_area_buffer_unwind_data. */
6468
6469 static Lisp_Object
6470 unwind_with_echo_area_buffer (vector)
6471 Lisp_Object vector;
6472 {
6473 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6474 Vdeactivate_mark = AREF (vector, 1);
6475 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6476
6477 if (WINDOWP (AREF (vector, 3)))
6478 {
6479 struct window *w;
6480 Lisp_Object buffer, charpos, bytepos;
6481
6482 w = XWINDOW (AREF (vector, 3));
6483 buffer = AREF (vector, 4);
6484 charpos = AREF (vector, 5);
6485 bytepos = AREF (vector, 6);
6486
6487 w->buffer = buffer;
6488 set_marker_both (w->pointm, buffer,
6489 XFASTINT (charpos), XFASTINT (bytepos));
6490 }
6491
6492 Vwith_echo_area_save_vector = vector;
6493 return Qnil;
6494 }
6495
6496
6497 /* Set up the echo area for use by print functions. MULTIBYTE_P
6498 non-zero means we will print multibyte. */
6499
6500 void
6501 setup_echo_area_for_printing (multibyte_p)
6502 int multibyte_p;
6503 {
6504 /* If we can't find an echo area any more, exit. */
6505 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
6506 Fkill_emacs (Qnil);
6507
6508 ensure_echo_area_buffers ();
6509
6510 if (!message_buf_print)
6511 {
6512 /* A message has been output since the last time we printed.
6513 Choose a fresh echo area buffer. */
6514 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6515 echo_area_buffer[0] = echo_buffer[1];
6516 else
6517 echo_area_buffer[0] = echo_buffer[0];
6518
6519 /* Switch to that buffer and clear it. */
6520 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6521 current_buffer->truncate_lines = Qnil;
6522
6523 if (Z > BEG)
6524 {
6525 int count = SPECPDL_INDEX ();
6526 specbind (Qinhibit_read_only, Qt);
6527 /* Note that undo recording is always disabled. */
6528 del_range (BEG, Z);
6529 unbind_to (count, Qnil);
6530 }
6531 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6532
6533 /* Set up the buffer for the multibyteness we need. */
6534 if (multibyte_p
6535 != !NILP (current_buffer->enable_multibyte_characters))
6536 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6537
6538 /* Raise the frame containing the echo area. */
6539 if (minibuffer_auto_raise)
6540 {
6541 struct frame *sf = SELECTED_FRAME ();
6542 Lisp_Object mini_window;
6543 mini_window = FRAME_MINIBUF_WINDOW (sf);
6544 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6545 }
6546
6547 message_log_maybe_newline ();
6548 message_buf_print = 1;
6549 }
6550 else
6551 {
6552 if (NILP (echo_area_buffer[0]))
6553 {
6554 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6555 echo_area_buffer[0] = echo_buffer[1];
6556 else
6557 echo_area_buffer[0] = echo_buffer[0];
6558 }
6559
6560 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6561 {
6562 /* Someone switched buffers between print requests. */
6563 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6564 current_buffer->truncate_lines = Qnil;
6565 }
6566 }
6567 }
6568
6569
6570 /* Display an echo area message in window W. Value is non-zero if W's
6571 height is changed. If display_last_displayed_message_p is
6572 non-zero, display the message that was last displayed, otherwise
6573 display the current message. */
6574
6575 static int
6576 display_echo_area (w)
6577 struct window *w;
6578 {
6579 int i, no_message_p, window_height_changed_p, count;
6580
6581 /* Temporarily disable garbage collections while displaying the echo
6582 area. This is done because a GC can print a message itself.
6583 That message would modify the echo area buffer's contents while a
6584 redisplay of the buffer is going on, and seriously confuse
6585 redisplay. */
6586 count = inhibit_garbage_collection ();
6587
6588 /* If there is no message, we must call display_echo_area_1
6589 nevertheless because it resizes the window. But we will have to
6590 reset the echo_area_buffer in question to nil at the end because
6591 with_echo_area_buffer will sets it to an empty buffer. */
6592 i = display_last_displayed_message_p ? 1 : 0;
6593 no_message_p = NILP (echo_area_buffer[i]);
6594
6595 window_height_changed_p
6596 = with_echo_area_buffer (w, display_last_displayed_message_p,
6597 display_echo_area_1,
6598 (EMACS_INT) w, Qnil, 0, 0);
6599
6600 if (no_message_p)
6601 echo_area_buffer[i] = Qnil;
6602
6603 unbind_to (count, Qnil);
6604 return window_height_changed_p;
6605 }
6606
6607
6608 /* Helper for display_echo_area. Display the current buffer which
6609 contains the current echo area message in window W, a mini-window,
6610 a pointer to which is passed in A1. A2..A4 are currently not used.
6611 Change the height of W so that all of the message is displayed.
6612 Value is non-zero if height of W was changed. */
6613
6614 static int
6615 display_echo_area_1 (a1, a2, a3, a4)
6616 EMACS_INT a1;
6617 Lisp_Object a2;
6618 EMACS_INT a3, a4;
6619 {
6620 struct window *w = (struct window *) a1;
6621 Lisp_Object window;
6622 struct text_pos start;
6623 int window_height_changed_p = 0;
6624
6625 /* Do this before displaying, so that we have a large enough glyph
6626 matrix for the display. */
6627 window_height_changed_p = resize_mini_window (w, 0);
6628
6629 /* Display. */
6630 clear_glyph_matrix (w->desired_matrix);
6631 XSETWINDOW (window, w);
6632 SET_TEXT_POS (start, BEG, BEG_BYTE);
6633 try_window (window, start);
6634
6635 return window_height_changed_p;
6636 }
6637
6638
6639 /* Resize the echo area window to exactly the size needed for the
6640 currently displayed message, if there is one. If a mini-buffer
6641 is active, don't shrink it. */
6642
6643 void
6644 resize_echo_area_exactly ()
6645 {
6646 if (BUFFERP (echo_area_buffer[0])
6647 && WINDOWP (echo_area_window))
6648 {
6649 struct window *w = XWINDOW (echo_area_window);
6650 int resized_p;
6651 Lisp_Object resize_exactly;
6652
6653 if (minibuf_level == 0)
6654 resize_exactly = Qt;
6655 else
6656 resize_exactly = Qnil;
6657
6658 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6659 (EMACS_INT) w, resize_exactly, 0, 0);
6660 if (resized_p)
6661 {
6662 ++windows_or_buffers_changed;
6663 ++update_mode_lines;
6664 redisplay_internal (0);
6665 }
6666 }
6667 }
6668
6669
6670 /* Callback function for with_echo_area_buffer, when used from
6671 resize_echo_area_exactly. A1 contains a pointer to the window to
6672 resize, EXACTLY non-nil means resize the mini-window exactly to the
6673 size of the text displayed. A3 and A4 are not used. Value is what
6674 resize_mini_window returns. */
6675
6676 static int
6677 resize_mini_window_1 (a1, exactly, a3, a4)
6678 EMACS_INT a1;
6679 Lisp_Object exactly;
6680 EMACS_INT a3, a4;
6681 {
6682 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6683 }
6684
6685
6686 /* Resize mini-window W to fit the size of its contents. EXACT:P
6687 means size the window exactly to the size needed. Otherwise, it's
6688 only enlarged until W's buffer is empty. Value is non-zero if
6689 the window height has been changed. */
6690
6691 int
6692 resize_mini_window (w, exact_p)
6693 struct window *w;
6694 int exact_p;
6695 {
6696 struct frame *f = XFRAME (w->frame);
6697 int window_height_changed_p = 0;
6698
6699 xassert (MINI_WINDOW_P (w));
6700
6701 /* Don't resize windows while redisplaying a window; it would
6702 confuse redisplay functions when the size of the window they are
6703 displaying changes from under them. Such a resizing can happen,
6704 for instance, when which-func prints a long message while
6705 we are running fontification-functions. We're running these
6706 functions with safe_call which binds inhibit-redisplay to t. */
6707 if (!NILP (Vinhibit_redisplay))
6708 return 0;
6709
6710 /* Nil means don't try to resize. */
6711 if (NILP (Vresize_mini_windows)
6712 || (FRAME_X_P (f) && f->output_data.x == NULL))
6713 return 0;
6714
6715 if (!FRAME_MINIBUF_ONLY_P (f))
6716 {
6717 struct it it;
6718 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6719 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6720 int height, max_height;
6721 int unit = CANON_Y_UNIT (f);
6722 struct text_pos start;
6723 struct buffer *old_current_buffer = NULL;
6724
6725 if (current_buffer != XBUFFER (w->buffer))
6726 {
6727 old_current_buffer = current_buffer;
6728 set_buffer_internal (XBUFFER (w->buffer));
6729 }
6730
6731 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6732
6733 /* Compute the max. number of lines specified by the user. */
6734 if (FLOATP (Vmax_mini_window_height))
6735 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6736 else if (INTEGERP (Vmax_mini_window_height))
6737 max_height = XINT (Vmax_mini_window_height);
6738 else
6739 max_height = total_height / 4;
6740
6741 /* Correct that max. height if it's bogus. */
6742 max_height = max (1, max_height);
6743 max_height = min (total_height, max_height);
6744
6745 /* Find out the height of the text in the window. */
6746 if (it.truncate_lines_p)
6747 height = 1;
6748 else
6749 {
6750 last_height = 0;
6751 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6752 if (it.max_ascent == 0 && it.max_descent == 0)
6753 height = it.current_y + last_height;
6754 else
6755 height = it.current_y + it.max_ascent + it.max_descent;
6756 height -= it.extra_line_spacing;
6757 height = (height + unit - 1) / unit;
6758 }
6759
6760 /* Compute a suitable window start. */
6761 if (height > max_height)
6762 {
6763 height = max_height;
6764 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6765 move_it_vertically_backward (&it, (height - 1) * unit);
6766 start = it.current.pos;
6767 }
6768 else
6769 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6770 SET_MARKER_FROM_TEXT_POS (w->start, start);
6771
6772 if (EQ (Vresize_mini_windows, Qgrow_only))
6773 {
6774 /* Let it grow only, until we display an empty message, in which
6775 case the window shrinks again. */
6776 if (height > XFASTINT (w->height))
6777 {
6778 int old_height = XFASTINT (w->height);
6779 freeze_window_starts (f, 1);
6780 grow_mini_window (w, height - XFASTINT (w->height));
6781 window_height_changed_p = XFASTINT (w->height) != old_height;
6782 }
6783 else if (height < XFASTINT (w->height)
6784 && (exact_p || BEGV == ZV))
6785 {
6786 int old_height = XFASTINT (w->height);
6787 freeze_window_starts (f, 0);
6788 shrink_mini_window (w);
6789 window_height_changed_p = XFASTINT (w->height) != old_height;
6790 }
6791 }
6792 else
6793 {
6794 /* Always resize to exact size needed. */
6795 if (height > XFASTINT (w->height))
6796 {
6797 int old_height = XFASTINT (w->height);
6798 freeze_window_starts (f, 1);
6799 grow_mini_window (w, height - XFASTINT (w->height));
6800 window_height_changed_p = XFASTINT (w->height) != old_height;
6801 }
6802 else if (height < XFASTINT (w->height))
6803 {
6804 int old_height = XFASTINT (w->height);
6805 freeze_window_starts (f, 0);
6806 shrink_mini_window (w);
6807
6808 if (height)
6809 {
6810 freeze_window_starts (f, 1);
6811 grow_mini_window (w, height - XFASTINT (w->height));
6812 }
6813
6814 window_height_changed_p = XFASTINT (w->height) != old_height;
6815 }
6816 }
6817
6818 if (old_current_buffer)
6819 set_buffer_internal (old_current_buffer);
6820 }
6821
6822 return window_height_changed_p;
6823 }
6824
6825
6826 /* Value is the current message, a string, or nil if there is no
6827 current message. */
6828
6829 Lisp_Object
6830 current_message ()
6831 {
6832 Lisp_Object msg;
6833
6834 if (NILP (echo_area_buffer[0]))
6835 msg = Qnil;
6836 else
6837 {
6838 with_echo_area_buffer (0, 0, current_message_1,
6839 (EMACS_INT) &msg, Qnil, 0, 0);
6840 if (NILP (msg))
6841 echo_area_buffer[0] = Qnil;
6842 }
6843
6844 return msg;
6845 }
6846
6847
6848 static int
6849 current_message_1 (a1, a2, a3, a4)
6850 EMACS_INT a1;
6851 Lisp_Object a2;
6852 EMACS_INT a3, a4;
6853 {
6854 Lisp_Object *msg = (Lisp_Object *) a1;
6855
6856 if (Z > BEG)
6857 *msg = make_buffer_string (BEG, Z, 1);
6858 else
6859 *msg = Qnil;
6860 return 0;
6861 }
6862
6863
6864 /* Push the current message on Vmessage_stack for later restauration
6865 by restore_message. Value is non-zero if the current message isn't
6866 empty. This is a relatively infrequent operation, so it's not
6867 worth optimizing. */
6868
6869 int
6870 push_message ()
6871 {
6872 Lisp_Object msg;
6873 msg = current_message ();
6874 Vmessage_stack = Fcons (msg, Vmessage_stack);
6875 return STRINGP (msg);
6876 }
6877
6878
6879 /* Restore message display from the top of Vmessage_stack. */
6880
6881 void
6882 restore_message ()
6883 {
6884 Lisp_Object msg;
6885
6886 xassert (CONSP (Vmessage_stack));
6887 msg = XCAR (Vmessage_stack);
6888 if (STRINGP (msg))
6889 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
6890 else
6891 message3_nolog (msg, 0, 0);
6892 }
6893
6894
6895 /* Handler for record_unwind_protect calling pop_message. */
6896
6897 Lisp_Object
6898 pop_message_unwind (dummy)
6899 Lisp_Object dummy;
6900 {
6901 pop_message ();
6902 return Qnil;
6903 }
6904
6905 /* Pop the top-most entry off Vmessage_stack. */
6906
6907 void
6908 pop_message ()
6909 {
6910 xassert (CONSP (Vmessage_stack));
6911 Vmessage_stack = XCDR (Vmessage_stack);
6912 }
6913
6914
6915 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6916 exits. If the stack is not empty, we have a missing pop_message
6917 somewhere. */
6918
6919 void
6920 check_message_stack ()
6921 {
6922 if (!NILP (Vmessage_stack))
6923 abort ();
6924 }
6925
6926
6927 /* Truncate to NCHARS what will be displayed in the echo area the next
6928 time we display it---but don't redisplay it now. */
6929
6930 void
6931 truncate_echo_area (nchars)
6932 int nchars;
6933 {
6934 if (nchars == 0)
6935 echo_area_buffer[0] = Qnil;
6936 /* A null message buffer means that the frame hasn't really been
6937 initialized yet. Error messages get reported properly by
6938 cmd_error, so this must be just an informative message; toss it. */
6939 else if (!noninteractive
6940 && INTERACTIVE
6941 && !NILP (echo_area_buffer[0]))
6942 {
6943 struct frame *sf = SELECTED_FRAME ();
6944 if (FRAME_MESSAGE_BUF (sf))
6945 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6946 }
6947 }
6948
6949
6950 /* Helper function for truncate_echo_area. Truncate the current
6951 message to at most NCHARS characters. */
6952
6953 static int
6954 truncate_message_1 (nchars, a2, a3, a4)
6955 EMACS_INT nchars;
6956 Lisp_Object a2;
6957 EMACS_INT a3, a4;
6958 {
6959 if (BEG + nchars < Z)
6960 del_range (BEG + nchars, Z);
6961 if (Z == BEG)
6962 echo_area_buffer[0] = Qnil;
6963 return 0;
6964 }
6965
6966
6967 /* Set the current message to a substring of S or STRING.
6968
6969 If STRING is a Lisp string, set the message to the first NBYTES
6970 bytes from STRING. NBYTES zero means use the whole string. If
6971 STRING is multibyte, the message will be displayed multibyte.
6972
6973 If S is not null, set the message to the first LEN bytes of S. LEN
6974 zero means use the whole string. MULTIBYTE_P non-zero means S is
6975 multibyte. Display the message multibyte in that case. */
6976
6977 void
6978 set_message (s, string, nbytes, multibyte_p)
6979 const char *s;
6980 Lisp_Object string;
6981 int nbytes, multibyte_p;
6982 {
6983 message_enable_multibyte
6984 = ((s && multibyte_p)
6985 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6986
6987 with_echo_area_buffer (0, -1, set_message_1,
6988 (EMACS_INT) s, string, nbytes, multibyte_p);
6989 message_buf_print = 0;
6990 help_echo_showing_p = 0;
6991 }
6992
6993
6994 /* Helper function for set_message. Arguments have the same meaning
6995 as there, with A1 corresponding to S and A2 corresponding to STRING
6996 This function is called with the echo area buffer being
6997 current. */
6998
6999 static int
7000 set_message_1 (a1, a2, nbytes, multibyte_p)
7001 EMACS_INT a1;
7002 Lisp_Object a2;
7003 EMACS_INT nbytes, multibyte_p;
7004 {
7005 const char *s = (const char *) a1;
7006 Lisp_Object string = a2;
7007
7008 xassert (BEG == Z);
7009
7010 /* Change multibyteness of the echo buffer appropriately. */
7011 if (message_enable_multibyte
7012 != !NILP (current_buffer->enable_multibyte_characters))
7013 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7014
7015 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7016
7017 /* Insert new message at BEG. */
7018 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7019
7020 if (STRINGP (string))
7021 {
7022 int nchars;
7023
7024 if (nbytes == 0)
7025 nbytes = SBYTES (string);
7026 nchars = string_byte_to_char (string, nbytes);
7027
7028 /* This function takes care of single/multibyte conversion. We
7029 just have to ensure that the echo area buffer has the right
7030 setting of enable_multibyte_characters. */
7031 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7032 }
7033 else if (s)
7034 {
7035 if (nbytes == 0)
7036 nbytes = strlen (s);
7037
7038 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7039 {
7040 /* Convert from multi-byte to single-byte. */
7041 int i, c, n;
7042 unsigned char work[1];
7043
7044 /* Convert a multibyte string to single-byte. */
7045 for (i = 0; i < nbytes; i += n)
7046 {
7047 c = string_char_and_length (s + i, nbytes - i, &n);
7048 work[0] = (SINGLE_BYTE_CHAR_P (c)
7049 ? c
7050 : multibyte_char_to_unibyte (c, Qnil));
7051 insert_1_both (work, 1, 1, 1, 0, 0);
7052 }
7053 }
7054 else if (!multibyte_p
7055 && !NILP (current_buffer->enable_multibyte_characters))
7056 {
7057 /* Convert from single-byte to multi-byte. */
7058 int i, c, n;
7059 const unsigned char *msg = (const unsigned char *) s;
7060 unsigned char str[MAX_MULTIBYTE_LENGTH];
7061
7062 /* Convert a single-byte string to multibyte. */
7063 for (i = 0; i < nbytes; i++)
7064 {
7065 c = unibyte_char_to_multibyte (msg[i]);
7066 n = CHAR_STRING (c, str);
7067 insert_1_both (str, 1, n, 1, 0, 0);
7068 }
7069 }
7070 else
7071 insert_1 (s, nbytes, 1, 0, 0);
7072 }
7073
7074 return 0;
7075 }
7076
7077
7078 /* Clear messages. CURRENT_P non-zero means clear the current
7079 message. LAST_DISPLAYED_P non-zero means clear the message
7080 last displayed. */
7081
7082 void
7083 clear_message (current_p, last_displayed_p)
7084 int current_p, last_displayed_p;
7085 {
7086 if (current_p)
7087 {
7088 echo_area_buffer[0] = Qnil;
7089 message_cleared_p = 1;
7090 }
7091
7092 if (last_displayed_p)
7093 echo_area_buffer[1] = Qnil;
7094
7095 message_buf_print = 0;
7096 }
7097
7098 /* Clear garbaged frames.
7099
7100 This function is used where the old redisplay called
7101 redraw_garbaged_frames which in turn called redraw_frame which in
7102 turn called clear_frame. The call to clear_frame was a source of
7103 flickering. I believe a clear_frame is not necessary. It should
7104 suffice in the new redisplay to invalidate all current matrices,
7105 and ensure a complete redisplay of all windows. */
7106
7107 static void
7108 clear_garbaged_frames ()
7109 {
7110 if (frame_garbaged)
7111 {
7112 Lisp_Object tail, frame;
7113 int changed_count = 0;
7114
7115 FOR_EACH_FRAME (tail, frame)
7116 {
7117 struct frame *f = XFRAME (frame);
7118
7119 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7120 {
7121 if (f->resized_p)
7122 Fredraw_frame (frame);
7123 clear_current_matrices (f);
7124 changed_count++;
7125 f->garbaged = 0;
7126 f->resized_p = 0;
7127 }
7128 }
7129
7130 frame_garbaged = 0;
7131 if (changed_count)
7132 ++windows_or_buffers_changed;
7133 }
7134 }
7135
7136
7137 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7138 is non-zero update selected_frame. Value is non-zero if the
7139 mini-windows height has been changed. */
7140
7141 static int
7142 echo_area_display (update_frame_p)
7143 int update_frame_p;
7144 {
7145 Lisp_Object mini_window;
7146 struct window *w;
7147 struct frame *f;
7148 int window_height_changed_p = 0;
7149 struct frame *sf = SELECTED_FRAME ();
7150
7151 mini_window = FRAME_MINIBUF_WINDOW (sf);
7152 w = XWINDOW (mini_window);
7153 f = XFRAME (WINDOW_FRAME (w));
7154
7155 /* Don't display if frame is invisible or not yet initialized. */
7156 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7157 return 0;
7158
7159 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7160 #ifndef MAC_OS8
7161 #ifdef HAVE_WINDOW_SYSTEM
7162 /* When Emacs starts, selected_frame may be a visible terminal
7163 frame, even if we run under a window system. If we let this
7164 through, a message would be displayed on the terminal. */
7165 if (EQ (selected_frame, Vterminal_frame)
7166 && !NILP (Vwindow_system))
7167 return 0;
7168 #endif /* HAVE_WINDOW_SYSTEM */
7169 #endif
7170
7171 /* Redraw garbaged frames. */
7172 if (frame_garbaged)
7173 clear_garbaged_frames ();
7174
7175 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7176 {
7177 echo_area_window = mini_window;
7178 window_height_changed_p = display_echo_area (w);
7179 w->must_be_updated_p = 1;
7180
7181 /* Update the display, unless called from redisplay_internal.
7182 Also don't update the screen during redisplay itself. The
7183 update will happen at the end of redisplay, and an update
7184 here could cause confusion. */
7185 if (update_frame_p && !redisplaying_p)
7186 {
7187 int n = 0;
7188
7189 /* If the display update has been interrupted by pending
7190 input, update mode lines in the frame. Due to the
7191 pending input, it might have been that redisplay hasn't
7192 been called, so that mode lines above the echo area are
7193 garbaged. This looks odd, so we prevent it here. */
7194 if (!display_completed)
7195 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7196
7197 if (window_height_changed_p
7198 /* Don't do this if Emacs is shutting down. Redisplay
7199 needs to run hooks. */
7200 && !NILP (Vrun_hooks))
7201 {
7202 /* Must update other windows. Likewise as in other
7203 cases, don't let this update be interrupted by
7204 pending input. */
7205 int count = SPECPDL_INDEX ();
7206 specbind (Qredisplay_dont_pause, Qt);
7207 windows_or_buffers_changed = 1;
7208 redisplay_internal (0);
7209 unbind_to (count, Qnil);
7210 }
7211 else if (FRAME_WINDOW_P (f) && n == 0)
7212 {
7213 /* Window configuration is the same as before.
7214 Can do with a display update of the echo area,
7215 unless we displayed some mode lines. */
7216 update_single_window (w, 1);
7217 rif->flush_display (f);
7218 }
7219 else
7220 update_frame (f, 1, 1);
7221
7222 /* If cursor is in the echo area, make sure that the next
7223 redisplay displays the minibuffer, so that the cursor will
7224 be replaced with what the minibuffer wants. */
7225 if (cursor_in_echo_area)
7226 ++windows_or_buffers_changed;
7227 }
7228 }
7229 else if (!EQ (mini_window, selected_window))
7230 windows_or_buffers_changed++;
7231
7232 /* Last displayed message is now the current message. */
7233 echo_area_buffer[1] = echo_area_buffer[0];
7234
7235 /* Prevent redisplay optimization in redisplay_internal by resetting
7236 this_line_start_pos. This is done because the mini-buffer now
7237 displays the message instead of its buffer text. */
7238 if (EQ (mini_window, selected_window))
7239 CHARPOS (this_line_start_pos) = 0;
7240
7241 return window_height_changed_p;
7242 }
7243
7244
7245 \f
7246 /***********************************************************************
7247 Frame Titles
7248 ***********************************************************************/
7249
7250
7251 /* The frame title buffering code is also used by Fformat_mode_line.
7252 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7253
7254 /* A buffer for constructing frame titles in it; allocated from the
7255 heap in init_xdisp and resized as needed in store_frame_title_char. */
7256
7257 static char *frame_title_buf;
7258
7259 /* The buffer's end, and a current output position in it. */
7260
7261 static char *frame_title_buf_end;
7262 static char *frame_title_ptr;
7263
7264
7265 /* Store a single character C for the frame title in frame_title_buf.
7266 Re-allocate frame_title_buf if necessary. */
7267
7268 static void
7269 #ifdef PROTOTYPES
7270 store_frame_title_char (char c)
7271 #else
7272 store_frame_title_char (c)
7273 char c;
7274 #endif
7275 {
7276 /* If output position has reached the end of the allocated buffer,
7277 double the buffer's size. */
7278 if (frame_title_ptr == frame_title_buf_end)
7279 {
7280 int len = frame_title_ptr - frame_title_buf;
7281 int new_size = 2 * len * sizeof *frame_title_buf;
7282 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7283 frame_title_buf_end = frame_title_buf + new_size;
7284 frame_title_ptr = frame_title_buf + len;
7285 }
7286
7287 *frame_title_ptr++ = c;
7288 }
7289
7290
7291 /* Store part of a frame title in frame_title_buf, beginning at
7292 frame_title_ptr. STR is the string to store. Do not copy
7293 characters that yield more columns than PRECISION; PRECISION <= 0
7294 means copy the whole string. Pad with spaces until FIELD_WIDTH
7295 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7296 pad. Called from display_mode_element when it is used to build a
7297 frame title. */
7298
7299 static int
7300 store_frame_title (str, field_width, precision)
7301 const unsigned char *str;
7302 int field_width, precision;
7303 {
7304 int n = 0;
7305 int dummy, nbytes;
7306
7307 /* Copy at most PRECISION chars from STR. */
7308 nbytes = strlen (str);
7309 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7310 while (nbytes--)
7311 store_frame_title_char (*str++);
7312
7313 /* Fill up with spaces until FIELD_WIDTH reached. */
7314 while (field_width > 0
7315 && n < field_width)
7316 {
7317 store_frame_title_char (' ');
7318 ++n;
7319 }
7320
7321 return n;
7322 }
7323
7324 #ifdef HAVE_WINDOW_SYSTEM
7325
7326 /* Set the title of FRAME, if it has changed. The title format is
7327 Vicon_title_format if FRAME is iconified, otherwise it is
7328 frame_title_format. */
7329
7330 static void
7331 x_consider_frame_title (frame)
7332 Lisp_Object frame;
7333 {
7334 struct frame *f = XFRAME (frame);
7335
7336 if (FRAME_WINDOW_P (f)
7337 || FRAME_MINIBUF_ONLY_P (f)
7338 || f->explicit_name)
7339 {
7340 /* Do we have more than one visible frame on this X display? */
7341 Lisp_Object tail;
7342 Lisp_Object fmt;
7343 struct buffer *obuf;
7344 int len;
7345 struct it it;
7346
7347 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7348 {
7349 Lisp_Object other_frame = XCAR (tail);
7350 struct frame *tf = XFRAME (other_frame);
7351
7352 if (tf != f
7353 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7354 && !FRAME_MINIBUF_ONLY_P (tf)
7355 && !EQ (other_frame, tip_frame)
7356 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7357 break;
7358 }
7359
7360 /* Set global variable indicating that multiple frames exist. */
7361 multiple_frames = CONSP (tail);
7362
7363 /* Switch to the buffer of selected window of the frame. Set up
7364 frame_title_ptr so that display_mode_element will output into it;
7365 then display the title. */
7366 obuf = current_buffer;
7367 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7368 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7369 frame_title_ptr = frame_title_buf;
7370 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7371 NULL, DEFAULT_FACE_ID);
7372 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7373 len = frame_title_ptr - frame_title_buf;
7374 frame_title_ptr = NULL;
7375 set_buffer_internal_1 (obuf);
7376
7377 /* Set the title only if it's changed. This avoids consing in
7378 the common case where it hasn't. (If it turns out that we've
7379 already wasted too much time by walking through the list with
7380 display_mode_element, then we might need to optimize at a
7381 higher level than this.) */
7382 if (! STRINGP (f->name)
7383 || SBYTES (f->name) != len
7384 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7385 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7386 }
7387 }
7388
7389 #endif /* not HAVE_WINDOW_SYSTEM */
7390
7391
7392
7393 \f
7394 /***********************************************************************
7395 Menu Bars
7396 ***********************************************************************/
7397
7398
7399 /* Prepare for redisplay by updating menu-bar item lists when
7400 appropriate. This can call eval. */
7401
7402 void
7403 prepare_menu_bars ()
7404 {
7405 int all_windows;
7406 struct gcpro gcpro1, gcpro2;
7407 struct frame *f;
7408 Lisp_Object tooltip_frame;
7409
7410 #ifdef HAVE_WINDOW_SYSTEM
7411 tooltip_frame = tip_frame;
7412 #else
7413 tooltip_frame = Qnil;
7414 #endif
7415
7416 /* Update all frame titles based on their buffer names, etc. We do
7417 this before the menu bars so that the buffer-menu will show the
7418 up-to-date frame titles. */
7419 #ifdef HAVE_WINDOW_SYSTEM
7420 if (windows_or_buffers_changed || update_mode_lines)
7421 {
7422 Lisp_Object tail, frame;
7423
7424 FOR_EACH_FRAME (tail, frame)
7425 {
7426 f = XFRAME (frame);
7427 if (!EQ (frame, tooltip_frame)
7428 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7429 x_consider_frame_title (frame);
7430 }
7431 }
7432 #endif /* HAVE_WINDOW_SYSTEM */
7433
7434 /* Update the menu bar item lists, if appropriate. This has to be
7435 done before any actual redisplay or generation of display lines. */
7436 all_windows = (update_mode_lines
7437 || buffer_shared > 1
7438 || windows_or_buffers_changed);
7439 if (all_windows)
7440 {
7441 Lisp_Object tail, frame;
7442 int count = SPECPDL_INDEX ();
7443
7444 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7445
7446 FOR_EACH_FRAME (tail, frame)
7447 {
7448 f = XFRAME (frame);
7449
7450 /* Ignore tooltip frame. */
7451 if (EQ (frame, tooltip_frame))
7452 continue;
7453
7454 /* If a window on this frame changed size, report that to
7455 the user and clear the size-change flag. */
7456 if (FRAME_WINDOW_SIZES_CHANGED (f))
7457 {
7458 Lisp_Object functions;
7459
7460 /* Clear flag first in case we get an error below. */
7461 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7462 functions = Vwindow_size_change_functions;
7463 GCPRO2 (tail, functions);
7464
7465 while (CONSP (functions))
7466 {
7467 call1 (XCAR (functions), frame);
7468 functions = XCDR (functions);
7469 }
7470 UNGCPRO;
7471 }
7472
7473 GCPRO1 (tail);
7474 update_menu_bar (f, 0);
7475 #ifdef HAVE_WINDOW_SYSTEM
7476 update_tool_bar (f, 0);
7477 #endif
7478 UNGCPRO;
7479 }
7480
7481 unbind_to (count, Qnil);
7482 }
7483 else
7484 {
7485 struct frame *sf = SELECTED_FRAME ();
7486 update_menu_bar (sf, 1);
7487 #ifdef HAVE_WINDOW_SYSTEM
7488 update_tool_bar (sf, 1);
7489 #endif
7490 }
7491
7492 /* Motif needs this. See comment in xmenu.c. Turn it off when
7493 pending_menu_activation is not defined. */
7494 #ifdef USE_X_TOOLKIT
7495 pending_menu_activation = 0;
7496 #endif
7497 }
7498
7499
7500 /* Update the menu bar item list for frame F. This has to be done
7501 before we start to fill in any display lines, because it can call
7502 eval.
7503
7504 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7505
7506 static void
7507 update_menu_bar (f, save_match_data)
7508 struct frame *f;
7509 int save_match_data;
7510 {
7511 Lisp_Object window;
7512 register struct window *w;
7513
7514 /* If called recursively during a menu update, do nothing. This can
7515 happen when, for instance, an activate-menubar-hook causes a
7516 redisplay. */
7517 if (inhibit_menubar_update)
7518 return;
7519
7520 window = FRAME_SELECTED_WINDOW (f);
7521 w = XWINDOW (window);
7522
7523 #if 0 /* The if statement below this if statement used to include the
7524 condition !NILP (w->update_mode_line), rather than using
7525 update_mode_lines directly, and this if statement may have
7526 been added to make that condition work. Now the if
7527 statement below matches its comment, this isn't needed. */
7528 if (update_mode_lines)
7529 w->update_mode_line = Qt;
7530 #endif
7531
7532 if (FRAME_WINDOW_P (f)
7533 ?
7534 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
7535 || defined (USE_GTK)
7536 FRAME_EXTERNAL_MENU_BAR (f)
7537 #else
7538 FRAME_MENU_BAR_LINES (f) > 0
7539 #endif
7540 : FRAME_MENU_BAR_LINES (f) > 0)
7541 {
7542 /* If the user has switched buffers or windows, we need to
7543 recompute to reflect the new bindings. But we'll
7544 recompute when update_mode_lines is set too; that means
7545 that people can use force-mode-line-update to request
7546 that the menu bar be recomputed. The adverse effect on
7547 the rest of the redisplay algorithm is about the same as
7548 windows_or_buffers_changed anyway. */
7549 if (windows_or_buffers_changed
7550 /* This used to test w->update_mode_line, but we believe
7551 there is no need to recompute the menu in that case. */
7552 || update_mode_lines
7553 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7554 < BUF_MODIFF (XBUFFER (w->buffer)))
7555 != !NILP (w->last_had_star))
7556 || ((!NILP (Vtransient_mark_mode)
7557 && !NILP (XBUFFER (w->buffer)->mark_active))
7558 != !NILP (w->region_showing)))
7559 {
7560 struct buffer *prev = current_buffer;
7561 int count = SPECPDL_INDEX ();
7562
7563 specbind (Qinhibit_menubar_update, Qt);
7564
7565 set_buffer_internal_1 (XBUFFER (w->buffer));
7566 if (save_match_data)
7567 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7568 if (NILP (Voverriding_local_map_menu_flag))
7569 {
7570 specbind (Qoverriding_terminal_local_map, Qnil);
7571 specbind (Qoverriding_local_map, Qnil);
7572 }
7573
7574 /* Run the Lucid hook. */
7575 safe_run_hooks (Qactivate_menubar_hook);
7576
7577 /* If it has changed current-menubar from previous value,
7578 really recompute the menu-bar from the value. */
7579 if (! NILP (Vlucid_menu_bar_dirty_flag))
7580 call0 (Qrecompute_lucid_menubar);
7581
7582 safe_run_hooks (Qmenu_bar_update_hook);
7583 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7584
7585 /* Redisplay the menu bar in case we changed it. */
7586 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
7587 || defined (USE_GTK)
7588 if (FRAME_WINDOW_P (f)
7589 #if defined (MAC_OS)
7590 /* All frames on Mac OS share the same menubar. So only the
7591 selected frame should be allowed to set it. */
7592 && f == SELECTED_FRAME ()
7593 #endif
7594 )
7595 set_frame_menubar (f, 0, 0);
7596 else
7597 /* On a terminal screen, the menu bar is an ordinary screen
7598 line, and this makes it get updated. */
7599 w->update_mode_line = Qt;
7600 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
7601 /* In the non-toolkit version, the menu bar is an ordinary screen
7602 line, and this makes it get updated. */
7603 w->update_mode_line = Qt;
7604 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
7605
7606 unbind_to (count, Qnil);
7607 set_buffer_internal_1 (prev);
7608 }
7609 }
7610 }
7611
7612
7613 \f
7614 /***********************************************************************
7615 Tool-bars
7616 ***********************************************************************/
7617
7618 #ifdef HAVE_WINDOW_SYSTEM
7619
7620 /* Update the tool-bar item list for frame F. This has to be done
7621 before we start to fill in any display lines. Called from
7622 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7623 and restore it here. */
7624
7625 static void
7626 update_tool_bar (f, save_match_data)
7627 struct frame *f;
7628 int save_match_data;
7629 {
7630 #ifdef USE_GTK
7631 int do_update = FRAME_EXTERNAL_TOOL_BAR(f);
7632 #else
7633 int do_update = WINDOWP (f->tool_bar_window)
7634 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0;
7635 #endif
7636
7637 if (do_update)
7638 {
7639 Lisp_Object window;
7640 struct window *w;
7641
7642 window = FRAME_SELECTED_WINDOW (f);
7643 w = XWINDOW (window);
7644
7645 /* If the user has switched buffers or windows, we need to
7646 recompute to reflect the new bindings. But we'll
7647 recompute when update_mode_lines is set too; that means
7648 that people can use force-mode-line-update to request
7649 that the menu bar be recomputed. The adverse effect on
7650 the rest of the redisplay algorithm is about the same as
7651 windows_or_buffers_changed anyway. */
7652 if (windows_or_buffers_changed
7653 || !NILP (w->update_mode_line)
7654 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7655 < BUF_MODIFF (XBUFFER (w->buffer)))
7656 != !NILP (w->last_had_star))
7657 || ((!NILP (Vtransient_mark_mode)
7658 && !NILP (XBUFFER (w->buffer)->mark_active))
7659 != !NILP (w->region_showing)))
7660 {
7661 struct buffer *prev = current_buffer;
7662 int count = SPECPDL_INDEX ();
7663
7664 /* Set current_buffer to the buffer of the selected
7665 window of the frame, so that we get the right local
7666 keymaps. */
7667 set_buffer_internal_1 (XBUFFER (w->buffer));
7668
7669 /* Save match data, if we must. */
7670 if (save_match_data)
7671 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7672
7673 /* Make sure that we don't accidentally use bogus keymaps. */
7674 if (NILP (Voverriding_local_map_menu_flag))
7675 {
7676 specbind (Qoverriding_terminal_local_map, Qnil);
7677 specbind (Qoverriding_local_map, Qnil);
7678 }
7679
7680 /* Build desired tool-bar items from keymaps. */
7681 f->tool_bar_items
7682 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7683
7684 /* Redisplay the tool-bar in case we changed it. */
7685 w->update_mode_line = Qt;
7686
7687 unbind_to (count, Qnil);
7688 set_buffer_internal_1 (prev);
7689 }
7690 }
7691 }
7692
7693
7694 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7695 F's desired tool-bar contents. F->tool_bar_items must have
7696 been set up previously by calling prepare_menu_bars. */
7697
7698 static void
7699 build_desired_tool_bar_string (f)
7700 struct frame *f;
7701 {
7702 int i, size, size_needed;
7703 struct gcpro gcpro1, gcpro2, gcpro3;
7704 Lisp_Object image, plist, props;
7705
7706 image = plist = props = Qnil;
7707 GCPRO3 (image, plist, props);
7708
7709 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7710 Otherwise, make a new string. */
7711
7712 /* The size of the string we might be able to reuse. */
7713 size = (STRINGP (f->desired_tool_bar_string)
7714 ? SCHARS (f->desired_tool_bar_string)
7715 : 0);
7716
7717 /* We need one space in the string for each image. */
7718 size_needed = f->n_tool_bar_items;
7719
7720 /* Reuse f->desired_tool_bar_string, if possible. */
7721 if (size < size_needed || NILP (f->desired_tool_bar_string))
7722 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7723 make_number (' '));
7724 else
7725 {
7726 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7727 Fremove_text_properties (make_number (0), make_number (size),
7728 props, f->desired_tool_bar_string);
7729 }
7730
7731 /* Put a `display' property on the string for the images to display,
7732 put a `menu_item' property on tool-bar items with a value that
7733 is the index of the item in F's tool-bar item vector. */
7734 for (i = 0; i < f->n_tool_bar_items; ++i)
7735 {
7736 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7737
7738 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7739 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7740 int hmargin, vmargin, relief, idx, end;
7741 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7742
7743 /* If image is a vector, choose the image according to the
7744 button state. */
7745 image = PROP (TOOL_BAR_ITEM_IMAGES);
7746 if (VECTORP (image))
7747 {
7748 if (enabled_p)
7749 idx = (selected_p
7750 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7751 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7752 else
7753 idx = (selected_p
7754 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7755 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7756
7757 xassert (ASIZE (image) >= idx);
7758 image = AREF (image, idx);
7759 }
7760 else
7761 idx = -1;
7762
7763 /* Ignore invalid image specifications. */
7764 if (!valid_image_p (image))
7765 continue;
7766
7767 /* Display the tool-bar button pressed, or depressed. */
7768 plist = Fcopy_sequence (XCDR (image));
7769
7770 /* Compute margin and relief to draw. */
7771 relief = (tool_bar_button_relief >= 0
7772 ? tool_bar_button_relief
7773 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7774 hmargin = vmargin = relief;
7775
7776 if (INTEGERP (Vtool_bar_button_margin)
7777 && XINT (Vtool_bar_button_margin) > 0)
7778 {
7779 hmargin += XFASTINT (Vtool_bar_button_margin);
7780 vmargin += XFASTINT (Vtool_bar_button_margin);
7781 }
7782 else if (CONSP (Vtool_bar_button_margin))
7783 {
7784 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7785 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7786 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7787
7788 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7789 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7790 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7791 }
7792
7793 if (auto_raise_tool_bar_buttons_p)
7794 {
7795 /* Add a `:relief' property to the image spec if the item is
7796 selected. */
7797 if (selected_p)
7798 {
7799 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7800 hmargin -= relief;
7801 vmargin -= relief;
7802 }
7803 }
7804 else
7805 {
7806 /* If image is selected, display it pressed, i.e. with a
7807 negative relief. If it's not selected, display it with a
7808 raised relief. */
7809 plist = Fplist_put (plist, QCrelief,
7810 (selected_p
7811 ? make_number (-relief)
7812 : make_number (relief)));
7813 hmargin -= relief;
7814 vmargin -= relief;
7815 }
7816
7817 /* Put a margin around the image. */
7818 if (hmargin || vmargin)
7819 {
7820 if (hmargin == vmargin)
7821 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7822 else
7823 plist = Fplist_put (plist, QCmargin,
7824 Fcons (make_number (hmargin),
7825 make_number (vmargin)));
7826 }
7827
7828 /* If button is not enabled, and we don't have special images
7829 for the disabled state, make the image appear disabled by
7830 applying an appropriate algorithm to it. */
7831 if (!enabled_p && idx < 0)
7832 plist = Fplist_put (plist, QCconversion, Qdisabled);
7833
7834 /* Put a `display' text property on the string for the image to
7835 display. Put a `menu-item' property on the string that gives
7836 the start of this item's properties in the tool-bar items
7837 vector. */
7838 image = Fcons (Qimage, plist);
7839 props = list4 (Qdisplay, image,
7840 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7841
7842 /* Let the last image hide all remaining spaces in the tool bar
7843 string. The string can be longer than needed when we reuse a
7844 previous string. */
7845 if (i + 1 == f->n_tool_bar_items)
7846 end = SCHARS (f->desired_tool_bar_string);
7847 else
7848 end = i + 1;
7849 Fadd_text_properties (make_number (i), make_number (end),
7850 props, f->desired_tool_bar_string);
7851 #undef PROP
7852 }
7853
7854 UNGCPRO;
7855 }
7856
7857
7858 /* Display one line of the tool-bar of frame IT->f. */
7859
7860 static void
7861 display_tool_bar_line (it)
7862 struct it *it;
7863 {
7864 struct glyph_row *row = it->glyph_row;
7865 int max_x = it->last_visible_x;
7866 struct glyph *last;
7867
7868 prepare_desired_row (row);
7869 row->y = it->current_y;
7870
7871 /* Note that this isn't made use of if the face hasn't a box,
7872 so there's no need to check the face here. */
7873 it->start_of_box_run_p = 1;
7874
7875 while (it->current_x < max_x)
7876 {
7877 int x_before, x, n_glyphs_before, i, nglyphs;
7878
7879 /* Get the next display element. */
7880 if (!get_next_display_element (it))
7881 break;
7882
7883 /* Produce glyphs. */
7884 x_before = it->current_x;
7885 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7886 PRODUCE_GLYPHS (it);
7887
7888 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7889 i = 0;
7890 x = x_before;
7891 while (i < nglyphs)
7892 {
7893 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7894
7895 if (x + glyph->pixel_width > max_x)
7896 {
7897 /* Glyph doesn't fit on line. */
7898 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7899 it->current_x = x;
7900 goto out;
7901 }
7902
7903 ++it->hpos;
7904 x += glyph->pixel_width;
7905 ++i;
7906 }
7907
7908 /* Stop at line ends. */
7909 if (ITERATOR_AT_END_OF_LINE_P (it))
7910 break;
7911
7912 set_iterator_to_next (it, 1);
7913 }
7914
7915 out:;
7916
7917 row->displays_text_p = row->used[TEXT_AREA] != 0;
7918 extend_face_to_end_of_line (it);
7919 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7920 last->right_box_line_p = 1;
7921 if (last == row->glyphs[TEXT_AREA])
7922 last->left_box_line_p = 1;
7923 compute_line_metrics (it);
7924
7925 /* If line is empty, make it occupy the rest of the tool-bar. */
7926 if (!row->displays_text_p)
7927 {
7928 row->height = row->phys_height = it->last_visible_y - row->y;
7929 row->ascent = row->phys_ascent = 0;
7930 }
7931
7932 row->full_width_p = 1;
7933 row->continued_p = 0;
7934 row->truncated_on_left_p = 0;
7935 row->truncated_on_right_p = 0;
7936
7937 it->current_x = it->hpos = 0;
7938 it->current_y += row->height;
7939 ++it->vpos;
7940 ++it->glyph_row;
7941 }
7942
7943
7944 /* Value is the number of screen lines needed to make all tool-bar
7945 items of frame F visible. */
7946
7947 static int
7948 tool_bar_lines_needed (f)
7949 struct frame *f;
7950 {
7951 struct window *w = XWINDOW (f->tool_bar_window);
7952 struct it it;
7953
7954 /* Initialize an iterator for iteration over
7955 F->desired_tool_bar_string in the tool-bar window of frame F. */
7956 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7957 it.first_visible_x = 0;
7958 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7959 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7960
7961 while (!ITERATOR_AT_END_P (&it))
7962 {
7963 it.glyph_row = w->desired_matrix->rows;
7964 clear_glyph_row (it.glyph_row);
7965 display_tool_bar_line (&it);
7966 }
7967
7968 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7969 }
7970
7971
7972 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7973 0, 1, 0,
7974 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7975 (frame)
7976 Lisp_Object frame;
7977 {
7978 struct frame *f;
7979 struct window *w;
7980 int nlines = 0;
7981
7982 if (NILP (frame))
7983 frame = selected_frame;
7984 else
7985 CHECK_FRAME (frame);
7986 f = XFRAME (frame);
7987
7988 if (WINDOWP (f->tool_bar_window)
7989 || (w = XWINDOW (f->tool_bar_window),
7990 XFASTINT (w->height) > 0))
7991 {
7992 update_tool_bar (f, 1);
7993 if (f->n_tool_bar_items)
7994 {
7995 build_desired_tool_bar_string (f);
7996 nlines = tool_bar_lines_needed (f);
7997 }
7998 }
7999
8000 return make_number (nlines);
8001 }
8002
8003
8004 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8005 height should be changed. */
8006
8007 static int
8008 redisplay_tool_bar (f)
8009 struct frame *f;
8010 {
8011 struct window *w;
8012 struct it it;
8013 struct glyph_row *row;
8014 int change_height_p = 0;
8015
8016 #ifdef USE_GTK
8017 if (FRAME_EXTERNAL_TOOL_BAR(f))
8018 update_frame_tool_bar (f);
8019 return 0;
8020 #endif
8021
8022 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8023 do anything. This means you must start with tool-bar-lines
8024 non-zero to get the auto-sizing effect. Or in other words, you
8025 can turn off tool-bars by specifying tool-bar-lines zero. */
8026 if (!WINDOWP (f->tool_bar_window)
8027 || (w = XWINDOW (f->tool_bar_window),
8028 XFASTINT (w->height) == 0))
8029 return 0;
8030
8031 /* Set up an iterator for the tool-bar window. */
8032 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8033 it.first_visible_x = 0;
8034 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
8035 row = it.glyph_row;
8036
8037 /* Build a string that represents the contents of the tool-bar. */
8038 build_desired_tool_bar_string (f);
8039 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8040
8041 /* Display as many lines as needed to display all tool-bar items. */
8042 while (it.current_y < it.last_visible_y)
8043 display_tool_bar_line (&it);
8044
8045 /* It doesn't make much sense to try scrolling in the tool-bar
8046 window, so don't do it. */
8047 w->desired_matrix->no_scrolling_p = 1;
8048 w->must_be_updated_p = 1;
8049
8050 if (auto_resize_tool_bars_p)
8051 {
8052 int nlines;
8053
8054 /* If we couldn't display everything, change the tool-bar's
8055 height. */
8056 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8057 change_height_p = 1;
8058
8059 /* If there are blank lines at the end, except for a partially
8060 visible blank line at the end that is smaller than
8061 CANON_Y_UNIT, change the tool-bar's height. */
8062 row = it.glyph_row - 1;
8063 if (!row->displays_text_p
8064 && row->height >= CANON_Y_UNIT (f))
8065 change_height_p = 1;
8066
8067 /* If row displays tool-bar items, but is partially visible,
8068 change the tool-bar's height. */
8069 if (row->displays_text_p
8070 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8071 change_height_p = 1;
8072
8073 /* Resize windows as needed by changing the `tool-bar-lines'
8074 frame parameter. */
8075 if (change_height_p
8076 && (nlines = tool_bar_lines_needed (f),
8077 nlines != XFASTINT (w->height)))
8078 {
8079 extern Lisp_Object Qtool_bar_lines;
8080 Lisp_Object frame;
8081 int old_height = XFASTINT (w->height);
8082
8083 XSETFRAME (frame, f);
8084 clear_glyph_matrix (w->desired_matrix);
8085 Fmodify_frame_parameters (frame,
8086 Fcons (Fcons (Qtool_bar_lines,
8087 make_number (nlines)),
8088 Qnil));
8089 if (XFASTINT (w->height) != old_height)
8090 fonts_changed_p = 1;
8091 }
8092 }
8093
8094 return change_height_p;
8095 }
8096
8097
8098 /* Get information about the tool-bar item which is displayed in GLYPH
8099 on frame F. Return in *PROP_IDX the index where tool-bar item
8100 properties start in F->tool_bar_items. Value is zero if
8101 GLYPH doesn't display a tool-bar item. */
8102
8103 int
8104 tool_bar_item_info (f, glyph, prop_idx)
8105 struct frame *f;
8106 struct glyph *glyph;
8107 int *prop_idx;
8108 {
8109 Lisp_Object prop;
8110 int success_p;
8111 int charpos;
8112
8113 /* This function can be called asynchronously, which means we must
8114 exclude any possibility that Fget_text_property signals an
8115 error. */
8116 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8117 charpos = max (0, charpos);
8118
8119 /* Get the text property `menu-item' at pos. The value of that
8120 property is the start index of this item's properties in
8121 F->tool_bar_items. */
8122 prop = Fget_text_property (make_number (charpos),
8123 Qmenu_item, f->current_tool_bar_string);
8124 if (INTEGERP (prop))
8125 {
8126 *prop_idx = XINT (prop);
8127 success_p = 1;
8128 }
8129 else
8130 success_p = 0;
8131
8132 return success_p;
8133 }
8134
8135 #endif /* HAVE_WINDOW_SYSTEM */
8136
8137
8138 \f
8139 /************************************************************************
8140 Horizontal scrolling
8141 ************************************************************************/
8142
8143 static int hscroll_window_tree P_ ((Lisp_Object));
8144 static int hscroll_windows P_ ((Lisp_Object));
8145
8146 /* For all leaf windows in the window tree rooted at WINDOW, set their
8147 hscroll value so that PT is (i) visible in the window, and (ii) so
8148 that it is not within a certain margin at the window's left and
8149 right border. Value is non-zero if any window's hscroll has been
8150 changed. */
8151
8152 static int
8153 hscroll_window_tree (window)
8154 Lisp_Object window;
8155 {
8156 int hscrolled_p = 0;
8157 int hscroll_relative_p = FLOATP (Vhscroll_step);
8158 int hscroll_step_abs = 0;
8159 double hscroll_step_rel = 0;
8160
8161 if (hscroll_relative_p)
8162 {
8163 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
8164 if (hscroll_step_rel < 0)
8165 {
8166 hscroll_relative_p = 0;
8167 hscroll_step_abs = 0;
8168 }
8169 }
8170 else if (INTEGERP (Vhscroll_step))
8171 {
8172 hscroll_step_abs = XINT (Vhscroll_step);
8173 if (hscroll_step_abs < 0)
8174 hscroll_step_abs = 0;
8175 }
8176 else
8177 hscroll_step_abs = 0;
8178
8179 while (WINDOWP (window))
8180 {
8181 struct window *w = XWINDOW (window);
8182
8183 if (WINDOWP (w->hchild))
8184 hscrolled_p |= hscroll_window_tree (w->hchild);
8185 else if (WINDOWP (w->vchild))
8186 hscrolled_p |= hscroll_window_tree (w->vchild);
8187 else if (w->cursor.vpos >= 0)
8188 {
8189 int h_margin, text_area_x, text_area_y;
8190 int text_area_width, text_area_height;
8191 struct glyph_row *current_cursor_row
8192 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8193 struct glyph_row *desired_cursor_row
8194 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8195 struct glyph_row *cursor_row
8196 = (desired_cursor_row->enabled_p
8197 ? desired_cursor_row
8198 : current_cursor_row);
8199
8200 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8201 &text_area_width, &text_area_height);
8202
8203 /* Scroll when cursor is inside this scroll margin. */
8204 h_margin = hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
8205
8206 if ((XFASTINT (w->hscroll)
8207 && w->cursor.x <= h_margin)
8208 || (cursor_row->enabled_p
8209 && cursor_row->truncated_on_right_p
8210 && (w->cursor.x >= text_area_width - h_margin)))
8211 {
8212 struct it it;
8213 int hscroll;
8214 struct buffer *saved_current_buffer;
8215 int pt;
8216 int wanted_x;
8217
8218 /* Find point in a display of infinite width. */
8219 saved_current_buffer = current_buffer;
8220 current_buffer = XBUFFER (w->buffer);
8221
8222 if (w == XWINDOW (selected_window))
8223 pt = BUF_PT (current_buffer);
8224 else
8225 {
8226 pt = marker_position (w->pointm);
8227 pt = max (BEGV, pt);
8228 pt = min (ZV, pt);
8229 }
8230
8231 /* Move iterator to pt starting at cursor_row->start in
8232 a line with infinite width. */
8233 init_to_row_start (&it, w, cursor_row);
8234 it.last_visible_x = INFINITY;
8235 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8236 current_buffer = saved_current_buffer;
8237
8238 /* Position cursor in window. */
8239 if (!hscroll_relative_p && hscroll_step_abs == 0)
8240 hscroll = max (0, it.current_x - text_area_width / 2)
8241 / CANON_X_UNIT (it.f);
8242 else if (w->cursor.x >= text_area_width - h_margin)
8243 {
8244 if (hscroll_relative_p)
8245 wanted_x = text_area_width * (1 - hscroll_step_rel)
8246 - h_margin;
8247 else
8248 wanted_x = text_area_width
8249 - hscroll_step_abs * CANON_X_UNIT (it.f)
8250 - h_margin;
8251 hscroll
8252 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8253 }
8254 else
8255 {
8256 if (hscroll_relative_p)
8257 wanted_x = text_area_width * hscroll_step_rel
8258 + h_margin;
8259 else
8260 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
8261 + h_margin;
8262 hscroll
8263 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8264 }
8265 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8266
8267 /* Don't call Fset_window_hscroll if value hasn't
8268 changed because it will prevent redisplay
8269 optimizations. */
8270 if (XFASTINT (w->hscroll) != hscroll)
8271 {
8272 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8273 w->hscroll = make_number (hscroll);
8274 hscrolled_p = 1;
8275 }
8276 }
8277 }
8278
8279 window = w->next;
8280 }
8281
8282 /* Value is non-zero if hscroll of any leaf window has been changed. */
8283 return hscrolled_p;
8284 }
8285
8286
8287 /* Set hscroll so that cursor is visible and not inside horizontal
8288 scroll margins for all windows in the tree rooted at WINDOW. See
8289 also hscroll_window_tree above. Value is non-zero if any window's
8290 hscroll has been changed. If it has, desired matrices on the frame
8291 of WINDOW are cleared. */
8292
8293 static int
8294 hscroll_windows (window)
8295 Lisp_Object window;
8296 {
8297 int hscrolled_p;
8298
8299 if (automatic_hscrolling_p)
8300 {
8301 hscrolled_p = hscroll_window_tree (window);
8302 if (hscrolled_p)
8303 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8304 }
8305 else
8306 hscrolled_p = 0;
8307 return hscrolled_p;
8308 }
8309
8310
8311 \f
8312 /************************************************************************
8313 Redisplay
8314 ************************************************************************/
8315
8316 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8317 to a non-zero value. This is sometimes handy to have in a debugger
8318 session. */
8319
8320 #if GLYPH_DEBUG
8321
8322 /* First and last unchanged row for try_window_id. */
8323
8324 int debug_first_unchanged_at_end_vpos;
8325 int debug_last_unchanged_at_beg_vpos;
8326
8327 /* Delta vpos and y. */
8328
8329 int debug_dvpos, debug_dy;
8330
8331 /* Delta in characters and bytes for try_window_id. */
8332
8333 int debug_delta, debug_delta_bytes;
8334
8335 /* Values of window_end_pos and window_end_vpos at the end of
8336 try_window_id. */
8337
8338 EMACS_INT debug_end_pos, debug_end_vpos;
8339
8340 /* Append a string to W->desired_matrix->method. FMT is a printf
8341 format string. A1...A9 are a supplement for a variable-length
8342 argument list. If trace_redisplay_p is non-zero also printf the
8343 resulting string to stderr. */
8344
8345 static void
8346 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8347 struct window *w;
8348 char *fmt;
8349 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8350 {
8351 char buffer[512];
8352 char *method = w->desired_matrix->method;
8353 int len = strlen (method);
8354 int size = sizeof w->desired_matrix->method;
8355 int remaining = size - len - 1;
8356
8357 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8358 if (len && remaining)
8359 {
8360 method[len] = '|';
8361 --remaining, ++len;
8362 }
8363
8364 strncpy (method + len, buffer, remaining);
8365
8366 if (trace_redisplay_p)
8367 fprintf (stderr, "%p (%s): %s\n",
8368 w,
8369 ((BUFFERP (w->buffer)
8370 && STRINGP (XBUFFER (w->buffer)->name))
8371 ? (char *) SDATA (XBUFFER (w->buffer)->name)
8372 : "no buffer"),
8373 buffer);
8374 }
8375
8376 #endif /* GLYPH_DEBUG */
8377
8378
8379 /* Value is non-zero if all changes in window W, which displays
8380 current_buffer, are in the text between START and END. START is a
8381 buffer position, END is given as a distance from Z. Used in
8382 redisplay_internal for display optimization. */
8383
8384 static INLINE int
8385 text_outside_line_unchanged_p (w, start, end)
8386 struct window *w;
8387 int start, end;
8388 {
8389 int unchanged_p = 1;
8390
8391 /* If text or overlays have changed, see where. */
8392 if (XFASTINT (w->last_modified) < MODIFF
8393 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8394 {
8395 /* Gap in the line? */
8396 if (GPT < start || Z - GPT < end)
8397 unchanged_p = 0;
8398
8399 /* Changes start in front of the line, or end after it? */
8400 if (unchanged_p
8401 && (BEG_UNCHANGED < start - 1
8402 || END_UNCHANGED < end))
8403 unchanged_p = 0;
8404
8405 /* If selective display, can't optimize if changes start at the
8406 beginning of the line. */
8407 if (unchanged_p
8408 && INTEGERP (current_buffer->selective_display)
8409 && XINT (current_buffer->selective_display) > 0
8410 && (BEG_UNCHANGED < start || GPT <= start))
8411 unchanged_p = 0;
8412
8413 /* If there are overlays at the start or end of the line, these
8414 may have overlay strings with newlines in them. A change at
8415 START, for instance, may actually concern the display of such
8416 overlay strings as well, and they are displayed on different
8417 lines. So, quickly rule out this case. (For the future, it
8418 might be desirable to implement something more telling than
8419 just BEG/END_UNCHANGED.) */
8420 if (unchanged_p)
8421 {
8422 if (BEG + BEG_UNCHANGED == start
8423 && overlay_touches_p (start))
8424 unchanged_p = 0;
8425 if (END_UNCHANGED == end
8426 && overlay_touches_p (Z - end))
8427 unchanged_p = 0;
8428 }
8429 }
8430
8431 return unchanged_p;
8432 }
8433
8434
8435 /* Do a frame update, taking possible shortcuts into account. This is
8436 the main external entry point for redisplay.
8437
8438 If the last redisplay displayed an echo area message and that message
8439 is no longer requested, we clear the echo area or bring back the
8440 mini-buffer if that is in use. */
8441
8442 void
8443 redisplay ()
8444 {
8445 redisplay_internal (0);
8446 }
8447
8448
8449 /* Return 1 if point moved out of or into a composition. Otherwise
8450 return 0. PREV_BUF and PREV_PT are the last point buffer and
8451 position. BUF and PT are the current point buffer and position. */
8452
8453 int
8454 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8455 struct buffer *prev_buf, *buf;
8456 int prev_pt, pt;
8457 {
8458 int start, end;
8459 Lisp_Object prop;
8460 Lisp_Object buffer;
8461
8462 XSETBUFFER (buffer, buf);
8463 /* Check a composition at the last point if point moved within the
8464 same buffer. */
8465 if (prev_buf == buf)
8466 {
8467 if (prev_pt == pt)
8468 /* Point didn't move. */
8469 return 0;
8470
8471 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8472 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8473 && COMPOSITION_VALID_P (start, end, prop)
8474 && start < prev_pt && end > prev_pt)
8475 /* The last point was within the composition. Return 1 iff
8476 point moved out of the composition. */
8477 return (pt <= start || pt >= end);
8478 }
8479
8480 /* Check a composition at the current point. */
8481 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8482 && find_composition (pt, -1, &start, &end, &prop, buffer)
8483 && COMPOSITION_VALID_P (start, end, prop)
8484 && start < pt && end > pt);
8485 }
8486
8487
8488 /* Reconsider the setting of B->clip_changed which is displayed
8489 in window W. */
8490
8491 static INLINE void
8492 reconsider_clip_changes (w, b)
8493 struct window *w;
8494 struct buffer *b;
8495 {
8496 if (b->clip_changed
8497 && !NILP (w->window_end_valid)
8498 && w->current_matrix->buffer == b
8499 && w->current_matrix->zv == BUF_ZV (b)
8500 && w->current_matrix->begv == BUF_BEGV (b))
8501 b->clip_changed = 0;
8502
8503 /* If display wasn't paused, and W is not a tool bar window, see if
8504 point has been moved into or out of a composition. In that case,
8505 we set b->clip_changed to 1 to force updating the screen. If
8506 b->clip_changed has already been set to 1, we can skip this
8507 check. */
8508 if (!b->clip_changed
8509 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8510 {
8511 int pt;
8512
8513 if (w == XWINDOW (selected_window))
8514 pt = BUF_PT (current_buffer);
8515 else
8516 pt = marker_position (w->pointm);
8517
8518 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8519 || pt != XINT (w->last_point))
8520 && check_point_in_composition (w->current_matrix->buffer,
8521 XINT (w->last_point),
8522 XBUFFER (w->buffer), pt))
8523 b->clip_changed = 1;
8524 }
8525 }
8526 \f
8527 #define STOP_POLLING \
8528 do { if (! polling_stopped_here) stop_polling (); \
8529 polling_stopped_here = 1; } while (0)
8530
8531 #define RESUME_POLLING \
8532 do { if (polling_stopped_here) start_polling (); \
8533 polling_stopped_here = 0; } while (0)
8534
8535
8536 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8537 response to any user action; therefore, we should preserve the echo
8538 area. (Actually, our caller does that job.) Perhaps in the future
8539 avoid recentering windows if it is not necessary; currently that
8540 causes some problems. */
8541
8542 static void
8543 redisplay_internal (preserve_echo_area)
8544 int preserve_echo_area;
8545 {
8546 struct window *w = XWINDOW (selected_window);
8547 struct frame *f = XFRAME (w->frame);
8548 int pause;
8549 int must_finish = 0;
8550 struct text_pos tlbufpos, tlendpos;
8551 int number_of_visible_frames;
8552 int count;
8553 struct frame *sf = SELECTED_FRAME ();
8554 int polling_stopped_here = 0;
8555
8556 /* Non-zero means redisplay has to consider all windows on all
8557 frames. Zero means, only selected_window is considered. */
8558 int consider_all_windows_p;
8559
8560 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8561
8562 /* No redisplay if running in batch mode or frame is not yet fully
8563 initialized, or redisplay is explicitly turned off by setting
8564 Vinhibit_redisplay. */
8565 if (noninteractive
8566 || !NILP (Vinhibit_redisplay)
8567 || !f->glyphs_initialized_p)
8568 return;
8569
8570 /* The flag redisplay_performed_directly_p is set by
8571 direct_output_for_insert when it already did the whole screen
8572 update necessary. */
8573 if (redisplay_performed_directly_p)
8574 {
8575 redisplay_performed_directly_p = 0;
8576 if (!hscroll_windows (selected_window))
8577 return;
8578 }
8579
8580 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8581 if (popup_activated ())
8582 return;
8583 #endif
8584
8585 /* I don't think this happens but let's be paranoid. */
8586 if (redisplaying_p)
8587 return;
8588
8589 /* Record a function that resets redisplaying_p to its old value
8590 when we leave this function. */
8591 count = SPECPDL_INDEX ();
8592 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8593 ++redisplaying_p;
8594 specbind (Qinhibit_free_realized_faces, Qnil);
8595
8596 retry:
8597 pause = 0;
8598 reconsider_clip_changes (w, current_buffer);
8599
8600 /* If new fonts have been loaded that make a glyph matrix adjustment
8601 necessary, do it. */
8602 if (fonts_changed_p)
8603 {
8604 adjust_glyphs (NULL);
8605 ++windows_or_buffers_changed;
8606 fonts_changed_p = 0;
8607 }
8608
8609 /* If face_change_count is non-zero, init_iterator will free all
8610 realized faces, which includes the faces referenced from current
8611 matrices. So, we can't reuse current matrices in this case. */
8612 if (face_change_count)
8613 ++windows_or_buffers_changed;
8614
8615 if (! FRAME_WINDOW_P (sf)
8616 && previous_terminal_frame != sf)
8617 {
8618 /* Since frames on an ASCII terminal share the same display
8619 area, displaying a different frame means redisplay the whole
8620 thing. */
8621 windows_or_buffers_changed++;
8622 SET_FRAME_GARBAGED (sf);
8623 XSETFRAME (Vterminal_frame, sf);
8624 }
8625 previous_terminal_frame = sf;
8626
8627 /* Set the visible flags for all frames. Do this before checking
8628 for resized or garbaged frames; they want to know if their frames
8629 are visible. See the comment in frame.h for
8630 FRAME_SAMPLE_VISIBILITY. */
8631 {
8632 Lisp_Object tail, frame;
8633
8634 number_of_visible_frames = 0;
8635
8636 FOR_EACH_FRAME (tail, frame)
8637 {
8638 struct frame *f = XFRAME (frame);
8639
8640 FRAME_SAMPLE_VISIBILITY (f);
8641 if (FRAME_VISIBLE_P (f))
8642 ++number_of_visible_frames;
8643 clear_desired_matrices (f);
8644 }
8645 }
8646
8647 /* Notice any pending interrupt request to change frame size. */
8648 do_pending_window_change (1);
8649
8650 /* Clear frames marked as garbaged. */
8651 if (frame_garbaged)
8652 clear_garbaged_frames ();
8653
8654 /* Build menubar and tool-bar items. */
8655 prepare_menu_bars ();
8656
8657 if (windows_or_buffers_changed)
8658 update_mode_lines++;
8659
8660 /* Detect case that we need to write or remove a star in the mode line. */
8661 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8662 {
8663 w->update_mode_line = Qt;
8664 if (buffer_shared > 1)
8665 update_mode_lines++;
8666 }
8667
8668 /* If %c is in the mode line, update it if needed. */
8669 if (!NILP (w->column_number_displayed)
8670 /* This alternative quickly identifies a common case
8671 where no change is needed. */
8672 && !(PT == XFASTINT (w->last_point)
8673 && XFASTINT (w->last_modified) >= MODIFF
8674 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8675 && (XFASTINT (w->column_number_displayed)
8676 != (int) current_column ())) /* iftc */
8677 w->update_mode_line = Qt;
8678
8679 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8680
8681 /* The variable buffer_shared is set in redisplay_window and
8682 indicates that we redisplay a buffer in different windows. See
8683 there. */
8684 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
8685 || cursor_type_changed);
8686
8687 /* If specs for an arrow have changed, do thorough redisplay
8688 to ensure we remove any arrow that should no longer exist. */
8689 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8690 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8691 consider_all_windows_p = windows_or_buffers_changed = 1;
8692
8693 /* Normally the message* functions will have already displayed and
8694 updated the echo area, but the frame may have been trashed, or
8695 the update may have been preempted, so display the echo area
8696 again here. Checking message_cleared_p captures the case that
8697 the echo area should be cleared. */
8698 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8699 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8700 || (message_cleared_p
8701 && minibuf_level == 0
8702 /* If the mini-window is currently selected, this means the
8703 echo-area doesn't show through. */
8704 && !MINI_WINDOW_P (XWINDOW (selected_window))))
8705 {
8706 int window_height_changed_p = echo_area_display (0);
8707 must_finish = 1;
8708
8709 /* If we don't display the current message, don't clear the
8710 message_cleared_p flag, because, if we did, we wouldn't clear
8711 the echo area in the next redisplay which doesn't preserve
8712 the echo area. */
8713 if (!display_last_displayed_message_p)
8714 message_cleared_p = 0;
8715
8716 if (fonts_changed_p)
8717 goto retry;
8718 else if (window_height_changed_p)
8719 {
8720 consider_all_windows_p = 1;
8721 ++update_mode_lines;
8722 ++windows_or_buffers_changed;
8723
8724 /* If window configuration was changed, frames may have been
8725 marked garbaged. Clear them or we will experience
8726 surprises wrt scrolling. */
8727 if (frame_garbaged)
8728 clear_garbaged_frames ();
8729 }
8730 }
8731 else if (EQ (selected_window, minibuf_window)
8732 && (current_buffer->clip_changed
8733 || XFASTINT (w->last_modified) < MODIFF
8734 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8735 && resize_mini_window (w, 0))
8736 {
8737 /* Resized active mini-window to fit the size of what it is
8738 showing if its contents might have changed. */
8739 must_finish = 1;
8740 consider_all_windows_p = 1;
8741 ++windows_or_buffers_changed;
8742 ++update_mode_lines;
8743
8744 /* If window configuration was changed, frames may have been
8745 marked garbaged. Clear them or we will experience
8746 surprises wrt scrolling. */
8747 if (frame_garbaged)
8748 clear_garbaged_frames ();
8749 }
8750
8751
8752 /* If showing the region, and mark has changed, we must redisplay
8753 the whole window. The assignment to this_line_start_pos prevents
8754 the optimization directly below this if-statement. */
8755 if (((!NILP (Vtransient_mark_mode)
8756 && !NILP (XBUFFER (w->buffer)->mark_active))
8757 != !NILP (w->region_showing))
8758 || (!NILP (w->region_showing)
8759 && !EQ (w->region_showing,
8760 Fmarker_position (XBUFFER (w->buffer)->mark))))
8761 CHARPOS (this_line_start_pos) = 0;
8762
8763 /* Optimize the case that only the line containing the cursor in the
8764 selected window has changed. Variables starting with this_ are
8765 set in display_line and record information about the line
8766 containing the cursor. */
8767 tlbufpos = this_line_start_pos;
8768 tlendpos = this_line_end_pos;
8769 if (!consider_all_windows_p
8770 && CHARPOS (tlbufpos) > 0
8771 && NILP (w->update_mode_line)
8772 && !current_buffer->clip_changed
8773 && !current_buffer->prevent_redisplay_optimizations_p
8774 && FRAME_VISIBLE_P (XFRAME (w->frame))
8775 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8776 /* Make sure recorded data applies to current buffer, etc. */
8777 && this_line_buffer == current_buffer
8778 && current_buffer == XBUFFER (w->buffer)
8779 && NILP (w->force_start)
8780 && NILP (w->optional_new_start)
8781 /* Point must be on the line that we have info recorded about. */
8782 && PT >= CHARPOS (tlbufpos)
8783 && PT <= Z - CHARPOS (tlendpos)
8784 /* All text outside that line, including its final newline,
8785 must be unchanged */
8786 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8787 CHARPOS (tlendpos)))
8788 {
8789 if (CHARPOS (tlbufpos) > BEGV
8790 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8791 && (CHARPOS (tlbufpos) == ZV
8792 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8793 /* Former continuation line has disappeared by becoming empty */
8794 goto cancel;
8795 else if (XFASTINT (w->last_modified) < MODIFF
8796 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8797 || MINI_WINDOW_P (w))
8798 {
8799 /* We have to handle the case of continuation around a
8800 wide-column character (See the comment in indent.c around
8801 line 885).
8802
8803 For instance, in the following case:
8804
8805 -------- Insert --------
8806 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8807 J_I_ ==> J_I_ `^^' are cursors.
8808 ^^ ^^
8809 -------- --------
8810
8811 As we have to redraw the line above, we should goto cancel. */
8812
8813 struct it it;
8814 int line_height_before = this_line_pixel_height;
8815
8816 /* Note that start_display will handle the case that the
8817 line starting at tlbufpos is a continuation lines. */
8818 start_display (&it, w, tlbufpos);
8819
8820 /* Implementation note: It this still necessary? */
8821 if (it.current_x != this_line_start_x)
8822 goto cancel;
8823
8824 TRACE ((stderr, "trying display optimization 1\n"));
8825 w->cursor.vpos = -1;
8826 overlay_arrow_seen = 0;
8827 it.vpos = this_line_vpos;
8828 it.current_y = this_line_y;
8829 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8830 display_line (&it);
8831
8832 /* If line contains point, is not continued,
8833 and ends at same distance from eob as before, we win */
8834 if (w->cursor.vpos >= 0
8835 /* Line is not continued, otherwise this_line_start_pos
8836 would have been set to 0 in display_line. */
8837 && CHARPOS (this_line_start_pos)
8838 /* Line ends as before. */
8839 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8840 /* Line has same height as before. Otherwise other lines
8841 would have to be shifted up or down. */
8842 && this_line_pixel_height == line_height_before)
8843 {
8844 /* If this is not the window's last line, we must adjust
8845 the charstarts of the lines below. */
8846 if (it.current_y < it.last_visible_y)
8847 {
8848 struct glyph_row *row
8849 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8850 int delta, delta_bytes;
8851
8852 if (Z - CHARPOS (tlendpos) == ZV)
8853 {
8854 /* This line ends at end of (accessible part of)
8855 buffer. There is no newline to count. */
8856 delta = (Z
8857 - CHARPOS (tlendpos)
8858 - MATRIX_ROW_START_CHARPOS (row));
8859 delta_bytes = (Z_BYTE
8860 - BYTEPOS (tlendpos)
8861 - MATRIX_ROW_START_BYTEPOS (row));
8862 }
8863 else
8864 {
8865 /* This line ends in a newline. Must take
8866 account of the newline and the rest of the
8867 text that follows. */
8868 delta = (Z
8869 - CHARPOS (tlendpos)
8870 - MATRIX_ROW_START_CHARPOS (row));
8871 delta_bytes = (Z_BYTE
8872 - BYTEPOS (tlendpos)
8873 - MATRIX_ROW_START_BYTEPOS (row));
8874 }
8875
8876 increment_matrix_positions (w->current_matrix,
8877 this_line_vpos + 1,
8878 w->current_matrix->nrows,
8879 delta, delta_bytes);
8880 }
8881
8882 /* If this row displays text now but previously didn't,
8883 or vice versa, w->window_end_vpos may have to be
8884 adjusted. */
8885 if ((it.glyph_row - 1)->displays_text_p)
8886 {
8887 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8888 XSETINT (w->window_end_vpos, this_line_vpos);
8889 }
8890 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8891 && this_line_vpos > 0)
8892 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8893 w->window_end_valid = Qnil;
8894
8895 /* Update hint: No need to try to scroll in update_window. */
8896 w->desired_matrix->no_scrolling_p = 1;
8897
8898 #if GLYPH_DEBUG
8899 *w->desired_matrix->method = 0;
8900 debug_method_add (w, "optimization 1");
8901 #endif
8902 goto update;
8903 }
8904 else
8905 goto cancel;
8906 }
8907 else if (/* Cursor position hasn't changed. */
8908 PT == XFASTINT (w->last_point)
8909 /* Make sure the cursor was last displayed
8910 in this window. Otherwise we have to reposition it. */
8911 && 0 <= w->cursor.vpos
8912 && XINT (w->height) > w->cursor.vpos)
8913 {
8914 if (!must_finish)
8915 {
8916 do_pending_window_change (1);
8917
8918 /* We used to always goto end_of_redisplay here, but this
8919 isn't enough if we have a blinking cursor. */
8920 if (w->cursor_off_p == w->last_cursor_off_p)
8921 goto end_of_redisplay;
8922 }
8923 goto update;
8924 }
8925 /* If highlighting the region, or if the cursor is in the echo area,
8926 then we can't just move the cursor. */
8927 else if (! (!NILP (Vtransient_mark_mode)
8928 && !NILP (current_buffer->mark_active))
8929 && (EQ (selected_window, current_buffer->last_selected_window)
8930 || highlight_nonselected_windows)
8931 && NILP (w->region_showing)
8932 && NILP (Vshow_trailing_whitespace)
8933 && !cursor_in_echo_area)
8934 {
8935 struct it it;
8936 struct glyph_row *row;
8937
8938 /* Skip from tlbufpos to PT and see where it is. Note that
8939 PT may be in invisible text. If so, we will end at the
8940 next visible position. */
8941 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8942 NULL, DEFAULT_FACE_ID);
8943 it.current_x = this_line_start_x;
8944 it.current_y = this_line_y;
8945 it.vpos = this_line_vpos;
8946
8947 /* The call to move_it_to stops in front of PT, but
8948 moves over before-strings. */
8949 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8950
8951 if (it.vpos == this_line_vpos
8952 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8953 row->enabled_p))
8954 {
8955 xassert (this_line_vpos == it.vpos);
8956 xassert (this_line_y == it.current_y);
8957 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8958 #if GLYPH_DEBUG
8959 *w->desired_matrix->method = 0;
8960 debug_method_add (w, "optimization 3");
8961 #endif
8962 goto update;
8963 }
8964 else
8965 goto cancel;
8966 }
8967
8968 cancel:
8969 /* Text changed drastically or point moved off of line. */
8970 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8971 }
8972
8973 CHARPOS (this_line_start_pos) = 0;
8974 consider_all_windows_p |= buffer_shared > 1;
8975 ++clear_face_cache_count;
8976
8977
8978 /* Build desired matrices, and update the display. If
8979 consider_all_windows_p is non-zero, do it for all windows on all
8980 frames. Otherwise do it for selected_window, only. */
8981
8982 if (consider_all_windows_p)
8983 {
8984 Lisp_Object tail, frame;
8985 int i, n = 0, size = 50;
8986 struct frame **updated
8987 = (struct frame **) alloca (size * sizeof *updated);
8988
8989 /* Clear the face cache eventually. */
8990 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8991 {
8992 clear_face_cache (0);
8993 clear_face_cache_count = 0;
8994 }
8995
8996 /* Recompute # windows showing selected buffer. This will be
8997 incremented each time such a window is displayed. */
8998 buffer_shared = 0;
8999
9000 FOR_EACH_FRAME (tail, frame)
9001 {
9002 struct frame *f = XFRAME (frame);
9003
9004 if (FRAME_WINDOW_P (f) || f == sf)
9005 {
9006 #ifdef HAVE_WINDOW_SYSTEM
9007 if (clear_face_cache_count % 50 == 0
9008 && FRAME_WINDOW_P (f))
9009 clear_image_cache (f, 0);
9010 #endif /* HAVE_WINDOW_SYSTEM */
9011
9012 /* Mark all the scroll bars to be removed; we'll redeem
9013 the ones we want when we redisplay their windows. */
9014 if (condemn_scroll_bars_hook)
9015 condemn_scroll_bars_hook (f);
9016
9017 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
9018 redisplay_windows (FRAME_ROOT_WINDOW (f));
9019
9020 /* Any scroll bars which redisplay_windows should have
9021 nuked should now go away. */
9022 if (judge_scroll_bars_hook)
9023 judge_scroll_bars_hook (f);
9024
9025 /* If fonts changed, display again. */
9026 /* ??? rms: I suspect it is a mistake to jump all the way
9027 back to retry here. It should just retry this frame. */
9028 if (fonts_changed_p)
9029 goto retry;
9030
9031 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
9032 {
9033 /* See if we have to hscroll. */
9034 if (hscroll_windows (f->root_window))
9035 goto retry;
9036
9037 /* Prevent various kinds of signals during display
9038 update. stdio is not robust about handling
9039 signals, which can cause an apparent I/O
9040 error. */
9041 if (interrupt_input)
9042 unrequest_sigio ();
9043 STOP_POLLING;
9044
9045 /* Update the display. */
9046 set_window_update_flags (XWINDOW (f->root_window), 1);
9047 pause |= update_frame (f, 0, 0);
9048 if (pause)
9049 break;
9050
9051 if (n == size)
9052 {
9053 int nbytes = size * sizeof *updated;
9054 struct frame **p = (struct frame **) alloca (2 * nbytes);
9055 bcopy (updated, p, nbytes);
9056 size *= 2;
9057 }
9058
9059 updated[n++] = f;
9060 }
9061 }
9062 }
9063
9064 /* Do the mark_window_display_accurate after all windows have
9065 been redisplayed because this call resets flags in buffers
9066 which are needed for proper redisplay. */
9067 for (i = 0; i < n; ++i)
9068 {
9069 struct frame *f = updated[i];
9070 mark_window_display_accurate (f->root_window, 1);
9071 if (frame_up_to_date_hook)
9072 frame_up_to_date_hook (f);
9073 }
9074 }
9075 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
9076 {
9077 Lisp_Object mini_window;
9078 struct frame *mini_frame;
9079
9080 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
9081 /* Use list_of_error, not Qerror, so that
9082 we catch only errors and don't run the debugger. */
9083 internal_condition_case_1 (redisplay_window_1, selected_window,
9084 list_of_error,
9085 redisplay_window_error);
9086
9087 /* Compare desired and current matrices, perform output. */
9088
9089 update:
9090 /* If fonts changed, display again. */
9091 if (fonts_changed_p)
9092 goto retry;
9093
9094 /* Prevent various kinds of signals during display update.
9095 stdio is not robust about handling signals,
9096 which can cause an apparent I/O error. */
9097 if (interrupt_input)
9098 unrequest_sigio ();
9099 STOP_POLLING;
9100
9101 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
9102 {
9103 if (hscroll_windows (selected_window))
9104 goto retry;
9105
9106 XWINDOW (selected_window)->must_be_updated_p = 1;
9107 pause = update_frame (sf, 0, 0);
9108 }
9109
9110 /* We may have called echo_area_display at the top of this
9111 function. If the echo area is on another frame, that may
9112 have put text on a frame other than the selected one, so the
9113 above call to update_frame would not have caught it. Catch
9114 it here. */
9115 mini_window = FRAME_MINIBUF_WINDOW (sf);
9116 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9117
9118 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
9119 {
9120 XWINDOW (mini_window)->must_be_updated_p = 1;
9121 pause |= update_frame (mini_frame, 0, 0);
9122 if (!pause && hscroll_windows (mini_window))
9123 goto retry;
9124 }
9125 }
9126
9127 /* If display was paused because of pending input, make sure we do a
9128 thorough update the next time. */
9129 if (pause)
9130 {
9131 /* Prevent the optimization at the beginning of
9132 redisplay_internal that tries a single-line update of the
9133 line containing the cursor in the selected window. */
9134 CHARPOS (this_line_start_pos) = 0;
9135
9136 /* Let the overlay arrow be updated the next time. */
9137 if (!NILP (last_arrow_position))
9138 {
9139 last_arrow_position = Qt;
9140 last_arrow_string = Qt;
9141 }
9142
9143 /* If we pause after scrolling, some rows in the current
9144 matrices of some windows are not valid. */
9145 if (!WINDOW_FULL_WIDTH_P (w)
9146 && !FRAME_WINDOW_P (XFRAME (w->frame)))
9147 update_mode_lines = 1;
9148 }
9149 else
9150 {
9151 if (!consider_all_windows_p)
9152 {
9153 /* This has already been done above if
9154 consider_all_windows_p is set. */
9155 mark_window_display_accurate_1 (w, 1);
9156
9157 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9158 last_arrow_string = Voverlay_arrow_string;
9159
9160 if (frame_up_to_date_hook != 0)
9161 frame_up_to_date_hook (sf);
9162 }
9163
9164 update_mode_lines = 0;
9165 windows_or_buffers_changed = 0;
9166 cursor_type_changed = 0;
9167 }
9168
9169 /* Start SIGIO interrupts coming again. Having them off during the
9170 code above makes it less likely one will discard output, but not
9171 impossible, since there might be stuff in the system buffer here.
9172 But it is much hairier to try to do anything about that. */
9173 if (interrupt_input)
9174 request_sigio ();
9175 RESUME_POLLING;
9176
9177 /* If a frame has become visible which was not before, redisplay
9178 again, so that we display it. Expose events for such a frame
9179 (which it gets when becoming visible) don't call the parts of
9180 redisplay constructing glyphs, so simply exposing a frame won't
9181 display anything in this case. So, we have to display these
9182 frames here explicitly. */
9183 if (!pause)
9184 {
9185 Lisp_Object tail, frame;
9186 int new_count = 0;
9187
9188 FOR_EACH_FRAME (tail, frame)
9189 {
9190 int this_is_visible = 0;
9191
9192 if (XFRAME (frame)->visible)
9193 this_is_visible = 1;
9194 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9195 if (XFRAME (frame)->visible)
9196 this_is_visible = 1;
9197
9198 if (this_is_visible)
9199 new_count++;
9200 }
9201
9202 if (new_count != number_of_visible_frames)
9203 windows_or_buffers_changed++;
9204 }
9205
9206 /* Change frame size now if a change is pending. */
9207 do_pending_window_change (1);
9208
9209 /* If we just did a pending size change, or have additional
9210 visible frames, redisplay again. */
9211 if (windows_or_buffers_changed && !pause)
9212 goto retry;
9213
9214 end_of_redisplay:
9215 unbind_to (count, Qnil);
9216 RESUME_POLLING;
9217 }
9218
9219
9220 /* Redisplay, but leave alone any recent echo area message unless
9221 another message has been requested in its place.
9222
9223 This is useful in situations where you need to redisplay but no
9224 user action has occurred, making it inappropriate for the message
9225 area to be cleared. See tracking_off and
9226 wait_reading_process_input for examples of these situations.
9227
9228 FROM_WHERE is an integer saying from where this function was
9229 called. This is useful for debugging. */
9230
9231 void
9232 redisplay_preserve_echo_area (from_where)
9233 int from_where;
9234 {
9235 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9236
9237 if (!NILP (echo_area_buffer[1]))
9238 {
9239 /* We have a previously displayed message, but no current
9240 message. Redisplay the previous message. */
9241 display_last_displayed_message_p = 1;
9242 redisplay_internal (1);
9243 display_last_displayed_message_p = 0;
9244 }
9245 else
9246 redisplay_internal (1);
9247 }
9248
9249
9250 /* Function registered with record_unwind_protect in
9251 redisplay_internal. Reset redisplaying_p to the value it had
9252 before redisplay_internal was called, and clear
9253 prevent_freeing_realized_faces_p. */
9254
9255 static Lisp_Object
9256 unwind_redisplay (old_redisplaying_p)
9257 Lisp_Object old_redisplaying_p;
9258 {
9259 redisplaying_p = XFASTINT (old_redisplaying_p);
9260 return Qnil;
9261 }
9262
9263
9264 /* Mark the display of window W as accurate or inaccurate. If
9265 ACCURATE_P is non-zero mark display of W as accurate. If
9266 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9267 redisplay_internal is called. */
9268
9269 static void
9270 mark_window_display_accurate_1 (w, accurate_p)
9271 struct window *w;
9272 int accurate_p;
9273 {
9274 if (BUFFERP (w->buffer))
9275 {
9276 struct buffer *b = XBUFFER (w->buffer);
9277
9278 w->last_modified
9279 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9280 w->last_overlay_modified
9281 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9282 w->last_had_star
9283 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9284
9285 if (accurate_p)
9286 {
9287 b->clip_changed = 0;
9288 b->prevent_redisplay_optimizations_p = 0;
9289
9290 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9291 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9292 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9293 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9294
9295 w->current_matrix->buffer = b;
9296 w->current_matrix->begv = BUF_BEGV (b);
9297 w->current_matrix->zv = BUF_ZV (b);
9298
9299 w->last_cursor = w->cursor;
9300 w->last_cursor_off_p = w->cursor_off_p;
9301
9302 if (w == XWINDOW (selected_window))
9303 w->last_point = make_number (BUF_PT (b));
9304 else
9305 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9306 }
9307 }
9308
9309 if (accurate_p)
9310 {
9311 w->window_end_valid = w->buffer;
9312 #if 0 /* This is incorrect with variable-height lines. */
9313 xassert (XINT (w->window_end_vpos)
9314 < (XINT (w->height)
9315 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9316 #endif
9317 w->update_mode_line = Qnil;
9318 }
9319 }
9320
9321
9322 /* Mark the display of windows in the window tree rooted at WINDOW as
9323 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9324 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9325 be redisplayed the next time redisplay_internal is called. */
9326
9327 void
9328 mark_window_display_accurate (window, accurate_p)
9329 Lisp_Object window;
9330 int accurate_p;
9331 {
9332 struct window *w;
9333
9334 for (; !NILP (window); window = w->next)
9335 {
9336 w = XWINDOW (window);
9337 mark_window_display_accurate_1 (w, accurate_p);
9338
9339 if (!NILP (w->vchild))
9340 mark_window_display_accurate (w->vchild, accurate_p);
9341 if (!NILP (w->hchild))
9342 mark_window_display_accurate (w->hchild, accurate_p);
9343 }
9344
9345 if (accurate_p)
9346 {
9347 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9348 last_arrow_string = Voverlay_arrow_string;
9349 }
9350 else
9351 {
9352 /* Force a thorough redisplay the next time by setting
9353 last_arrow_position and last_arrow_string to t, which is
9354 unequal to any useful value of Voverlay_arrow_... */
9355 last_arrow_position = Qt;
9356 last_arrow_string = Qt;
9357 }
9358 }
9359
9360
9361 /* Return value in display table DP (Lisp_Char_Table *) for character
9362 C. Since a display table doesn't have any parent, we don't have to
9363 follow parent. Do not call this function directly but use the
9364 macro DISP_CHAR_VECTOR. */
9365
9366 Lisp_Object
9367 disp_char_vector (dp, c)
9368 struct Lisp_Char_Table *dp;
9369 int c;
9370 {
9371 int code[4], i;
9372 Lisp_Object val;
9373
9374 if (SINGLE_BYTE_CHAR_P (c))
9375 return (dp->contents[c]);
9376
9377 SPLIT_CHAR (c, code[0], code[1], code[2]);
9378 if (code[1] < 32)
9379 code[1] = -1;
9380 else if (code[2] < 32)
9381 code[2] = -1;
9382
9383 /* Here, the possible range of code[0] (== charset ID) is
9384 128..max_charset. Since the top level char table contains data
9385 for multibyte characters after 256th element, we must increment
9386 code[0] by 128 to get a correct index. */
9387 code[0] += 128;
9388 code[3] = -1; /* anchor */
9389
9390 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9391 {
9392 val = dp->contents[code[i]];
9393 if (!SUB_CHAR_TABLE_P (val))
9394 return (NILP (val) ? dp->defalt : val);
9395 }
9396
9397 /* Here, val is a sub char table. We return the default value of
9398 it. */
9399 return (dp->defalt);
9400 }
9401
9402
9403 \f
9404 /***********************************************************************
9405 Window Redisplay
9406 ***********************************************************************/
9407
9408 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9409
9410 static void
9411 redisplay_windows (window)
9412 Lisp_Object window;
9413 {
9414 while (!NILP (window))
9415 {
9416 struct window *w = XWINDOW (window);
9417
9418 if (!NILP (w->hchild))
9419 redisplay_windows (w->hchild);
9420 else if (!NILP (w->vchild))
9421 redisplay_windows (w->vchild);
9422 else
9423 {
9424 displayed_buffer = XBUFFER (w->buffer);
9425 /* Use list_of_error, not Qerror, so that
9426 we catch only errors and don't run the debugger. */
9427 internal_condition_case_1 (redisplay_window_0, window,
9428 list_of_error,
9429 redisplay_window_error);
9430 }
9431
9432 window = w->next;
9433 }
9434 }
9435
9436 static Lisp_Object
9437 redisplay_window_error ()
9438 {
9439 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9440 return Qnil;
9441 }
9442
9443 static Lisp_Object
9444 redisplay_window_0 (window)
9445 Lisp_Object window;
9446 {
9447 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9448 redisplay_window (window, 0);
9449 return Qnil;
9450 }
9451
9452 static Lisp_Object
9453 redisplay_window_1 (window)
9454 Lisp_Object window;
9455 {
9456 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9457 redisplay_window (window, 1);
9458 return Qnil;
9459 }
9460 \f
9461
9462 /* Increment GLYPH until it reaches END or CONDITION fails while
9463 adding (GLYPH)->pixel_width to X. */
9464
9465 #define SKIP_GLYPHS(glyph, end, x, condition) \
9466 do \
9467 { \
9468 (x) += (glyph)->pixel_width; \
9469 ++(glyph); \
9470 } \
9471 while ((glyph) < (end) && (condition))
9472
9473
9474 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9475 DELTA is the number of bytes by which positions recorded in ROW
9476 differ from current buffer positions. */
9477
9478 void
9479 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9480 struct window *w;
9481 struct glyph_row *row;
9482 struct glyph_matrix *matrix;
9483 int delta, delta_bytes, dy, dvpos;
9484 {
9485 struct glyph *glyph = row->glyphs[TEXT_AREA];
9486 struct glyph *end = glyph + row->used[TEXT_AREA];
9487 /* The first glyph that starts a sequence of glyphs from string. */
9488 struct glyph *string_start;
9489 /* The X coordinate of string_start. */
9490 int string_start_x;
9491 /* The last known character position. */
9492 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
9493 /* The last known character position before string_start. */
9494 int string_before_pos;
9495 int x = row->x;
9496 int pt_old = PT - delta;
9497
9498 /* Skip over glyphs not having an object at the start of the row.
9499 These are special glyphs like truncation marks on terminal
9500 frames. */
9501 if (row->displays_text_p)
9502 while (glyph < end
9503 && INTEGERP (glyph->object)
9504 && glyph->charpos < 0)
9505 {
9506 x += glyph->pixel_width;
9507 ++glyph;
9508 }
9509
9510 string_start = NULL;
9511 while (glyph < end
9512 && !INTEGERP (glyph->object)
9513 && (!BUFFERP (glyph->object)
9514 || (last_pos = glyph->charpos) < pt_old))
9515 {
9516 if (! STRINGP (glyph->object))
9517 {
9518 string_start = NULL;
9519 x += glyph->pixel_width;
9520 ++glyph;
9521 }
9522 else
9523 {
9524 string_before_pos = last_pos;
9525 string_start = glyph;
9526 string_start_x = x;
9527 /* Skip all glyphs from string. */
9528 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
9529 }
9530 }
9531
9532 if (string_start
9533 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
9534 {
9535 /* We may have skipped over point because the previous glyphs
9536 are from string. As there's no easy way to know the
9537 character position of the current glyph, find the correct
9538 glyph on point by scanning from string_start again. */
9539 Lisp_Object limit;
9540 Lisp_Object string;
9541 int pos;
9542
9543 limit = make_number (pt_old + 1);
9544 end = glyph;
9545 glyph = string_start;
9546 x = string_start_x;
9547 string = glyph->object;
9548 pos = string_buffer_position (w, string, string_before_pos);
9549 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
9550 because we always put cursor after overlay strings. */
9551 while (pos == 0 && glyph < end)
9552 {
9553 string = glyph->object;
9554 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
9555 if (glyph < end)
9556 pos = string_buffer_position (w, glyph->object, string_before_pos);
9557 }
9558
9559 while (glyph < end)
9560 {
9561 pos = XINT (Fnext_single_char_property_change
9562 (make_number (pos), Qdisplay, Qnil, limit));
9563 if (pos > pt_old)
9564 break;
9565 /* Skip glyphs from the same string. */
9566 string = glyph->object;
9567 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
9568 /* Skip glyphs from an overlay. */
9569 while (glyph < end
9570 && ! string_buffer_position (w, glyph->object, pos))
9571 {
9572 string = glyph->object;
9573 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
9574 }
9575 }
9576 }
9577
9578 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9579 w->cursor.x = x;
9580 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9581 w->cursor.y = row->y + dy;
9582
9583 if (w == XWINDOW (selected_window))
9584 {
9585 if (!row->continued_p
9586 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9587 && row->x == 0)
9588 {
9589 this_line_buffer = XBUFFER (w->buffer);
9590
9591 CHARPOS (this_line_start_pos)
9592 = MATRIX_ROW_START_CHARPOS (row) + delta;
9593 BYTEPOS (this_line_start_pos)
9594 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9595
9596 CHARPOS (this_line_end_pos)
9597 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9598 BYTEPOS (this_line_end_pos)
9599 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9600
9601 this_line_y = w->cursor.y;
9602 this_line_pixel_height = row->height;
9603 this_line_vpos = w->cursor.vpos;
9604 this_line_start_x = row->x;
9605 }
9606 else
9607 CHARPOS (this_line_start_pos) = 0;
9608 }
9609 }
9610
9611
9612 /* Run window scroll functions, if any, for WINDOW with new window
9613 start STARTP. Sets the window start of WINDOW to that position.
9614
9615 We assume that the window's buffer is really current. */
9616
9617 static INLINE struct text_pos
9618 run_window_scroll_functions (window, startp)
9619 Lisp_Object window;
9620 struct text_pos startp;
9621 {
9622 struct window *w = XWINDOW (window);
9623 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9624
9625 if (current_buffer != XBUFFER (w->buffer))
9626 abort ();
9627
9628 if (!NILP (Vwindow_scroll_functions))
9629 {
9630 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9631 make_number (CHARPOS (startp)));
9632 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9633 /* In case the hook functions switch buffers. */
9634 if (current_buffer != XBUFFER (w->buffer))
9635 set_buffer_internal_1 (XBUFFER (w->buffer));
9636 }
9637
9638 return startp;
9639 }
9640
9641
9642 /* Make sure the line containing the cursor is fully visible.
9643 A value of 1 means there is nothing to be done.
9644 (Either the line is fully visible, or it cannot be made so,
9645 or we cannot tell.)
9646 A value of 0 means the caller should do scrolling
9647 as if point had gone off the screen. */
9648
9649 static int
9650 make_cursor_line_fully_visible (w)
9651 struct window *w;
9652 {
9653 struct glyph_matrix *matrix;
9654 struct glyph_row *row;
9655 int window_height;
9656
9657 /* It's not always possible to find the cursor, e.g, when a window
9658 is full of overlay strings. Don't do anything in that case. */
9659 if (w->cursor.vpos < 0)
9660 return 1;
9661
9662 matrix = w->desired_matrix;
9663 row = MATRIX_ROW (matrix, w->cursor.vpos);
9664
9665 /* If the cursor row is not partially visible, there's nothing to do. */
9666 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9667 return 1;
9668
9669 /* If the row the cursor is in is taller than the window's height,
9670 it's not clear what to do, so do nothing. */
9671 window_height = window_box_height (w);
9672 if (row->height >= window_height)
9673 return 1;
9674
9675 return 0;
9676
9677 #if 0
9678 /* This code used to try to scroll the window just enough to make
9679 the line visible. It returned 0 to say that the caller should
9680 allocate larger glyph matrices. */
9681
9682 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9683 {
9684 int dy = row->height - row->visible_height;
9685 w->vscroll = 0;
9686 w->cursor.y += dy;
9687 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9688 }
9689 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9690 {
9691 int dy = - (row->height - row->visible_height);
9692 w->vscroll = dy;
9693 w->cursor.y += dy;
9694 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9695 }
9696
9697 /* When we change the cursor y-position of the selected window,
9698 change this_line_y as well so that the display optimization for
9699 the cursor line of the selected window in redisplay_internal uses
9700 the correct y-position. */
9701 if (w == XWINDOW (selected_window))
9702 this_line_y = w->cursor.y;
9703
9704 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9705 redisplay with larger matrices. */
9706 if (matrix->nrows < required_matrix_height (w))
9707 {
9708 fonts_changed_p = 1;
9709 return 0;
9710 }
9711
9712 return 1;
9713 #endif /* 0 */
9714 }
9715
9716
9717 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9718 non-zero means only WINDOW is redisplayed in redisplay_internal.
9719 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9720 in redisplay_window to bring a partially visible line into view in
9721 the case that only the cursor has moved.
9722
9723 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
9724 last screen line's vertical height extends past the end of the screen.
9725
9726 Value is
9727
9728 1 if scrolling succeeded
9729
9730 0 if scrolling didn't find point.
9731
9732 -1 if new fonts have been loaded so that we must interrupt
9733 redisplay, adjust glyph matrices, and try again. */
9734
9735 enum
9736 {
9737 SCROLLING_SUCCESS,
9738 SCROLLING_FAILED,
9739 SCROLLING_NEED_LARGER_MATRICES
9740 };
9741
9742 static int
9743 try_scrolling (window, just_this_one_p, scroll_conservatively,
9744 scroll_step, temp_scroll_step, last_line_misfit)
9745 Lisp_Object window;
9746 int just_this_one_p;
9747 EMACS_INT scroll_conservatively, scroll_step;
9748 int temp_scroll_step;
9749 int last_line_misfit;
9750 {
9751 struct window *w = XWINDOW (window);
9752 struct frame *f = XFRAME (w->frame);
9753 struct text_pos scroll_margin_pos;
9754 struct text_pos pos;
9755 struct text_pos startp;
9756 struct it it;
9757 Lisp_Object window_end;
9758 int this_scroll_margin;
9759 int dy = 0;
9760 int scroll_max;
9761 int rc;
9762 int amount_to_scroll = 0;
9763 Lisp_Object aggressive;
9764 int height;
9765 int end_scroll_margin;
9766
9767 #if GLYPH_DEBUG
9768 debug_method_add (w, "try_scrolling");
9769 #endif
9770
9771 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9772
9773 /* Compute scroll margin height in pixels. We scroll when point is
9774 within this distance from the top or bottom of the window. */
9775 if (scroll_margin > 0)
9776 {
9777 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9778 this_scroll_margin *= CANON_Y_UNIT (f);
9779 }
9780 else
9781 this_scroll_margin = 0;
9782
9783 /* Compute how much we should try to scroll maximally to bring point
9784 into view. */
9785 if (scroll_step || scroll_conservatively || temp_scroll_step)
9786 scroll_max = max (scroll_step,
9787 max (scroll_conservatively, temp_scroll_step));
9788 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9789 || NUMBERP (current_buffer->scroll_up_aggressively))
9790 /* We're trying to scroll because of aggressive scrolling
9791 but no scroll_step is set. Choose an arbitrary one. Maybe
9792 there should be a variable for this. */
9793 scroll_max = 10;
9794 else
9795 scroll_max = 0;
9796 scroll_max *= CANON_Y_UNIT (f);
9797
9798 /* Decide whether we have to scroll down. Start at the window end
9799 and move this_scroll_margin up to find the position of the scroll
9800 margin. */
9801 window_end = Fwindow_end (window, Qt);
9802
9803 too_near_end:
9804
9805 CHARPOS (scroll_margin_pos) = XINT (window_end);
9806 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9807
9808 end_scroll_margin = this_scroll_margin + !!last_line_misfit;
9809 if (end_scroll_margin)
9810 {
9811 start_display (&it, w, scroll_margin_pos);
9812 move_it_vertically (&it, - end_scroll_margin);
9813 scroll_margin_pos = it.current.pos;
9814 }
9815
9816 if (PT >= CHARPOS (scroll_margin_pos))
9817 {
9818 int y0;
9819
9820 /* Point is in the scroll margin at the bottom of the window, or
9821 below. Compute a new window start that makes point visible. */
9822
9823 /* Compute the distance from the scroll margin to PT.
9824 Give up if the distance is greater than scroll_max. */
9825 start_display (&it, w, scroll_margin_pos);
9826 y0 = it.current_y;
9827 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9828 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9829
9830 /* To make point visible, we have to move the window start
9831 down so that the line the cursor is in is visible, which
9832 means we have to add in the height of the cursor line. */
9833 dy = line_bottom_y (&it) - y0;
9834
9835 if (dy > scroll_max)
9836 return SCROLLING_FAILED;
9837
9838 /* Move the window start down. If scrolling conservatively,
9839 move it just enough down to make point visible. If
9840 scroll_step is set, move it down by scroll_step. */
9841 start_display (&it, w, startp);
9842
9843 if (scroll_conservatively)
9844 /* Set AMOUNT_TO_SCROLL to at least one line,
9845 and at most scroll_conservatively lines. */
9846 amount_to_scroll
9847 = min (max (dy, CANON_Y_UNIT (f)),
9848 CANON_Y_UNIT (f) * scroll_conservatively);
9849 else if (scroll_step || temp_scroll_step)
9850 amount_to_scroll = scroll_max;
9851 else
9852 {
9853 aggressive = current_buffer->scroll_up_aggressively;
9854 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9855 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9856 if (NUMBERP (aggressive))
9857 amount_to_scroll = XFLOATINT (aggressive) * height;
9858 }
9859
9860 if (amount_to_scroll <= 0)
9861 return SCROLLING_FAILED;
9862
9863 /* If moving by amount_to_scroll leaves STARTP unchanged,
9864 move it down one screen line. */
9865
9866 move_it_vertically (&it, amount_to_scroll);
9867 if (CHARPOS (it.current.pos) == CHARPOS (startp))
9868 move_it_by_lines (&it, 1, 1);
9869 startp = it.current.pos;
9870 }
9871 else
9872 {
9873 /* See if point is inside the scroll margin at the top of the
9874 window. */
9875 scroll_margin_pos = startp;
9876 if (this_scroll_margin)
9877 {
9878 start_display (&it, w, startp);
9879 move_it_vertically (&it, this_scroll_margin);
9880 scroll_margin_pos = it.current.pos;
9881 }
9882
9883 if (PT < CHARPOS (scroll_margin_pos))
9884 {
9885 /* Point is in the scroll margin at the top of the window or
9886 above what is displayed in the window. */
9887 int y0;
9888
9889 /* Compute the vertical distance from PT to the scroll
9890 margin position. Give up if distance is greater than
9891 scroll_max. */
9892 SET_TEXT_POS (pos, PT, PT_BYTE);
9893 start_display (&it, w, pos);
9894 y0 = it.current_y;
9895 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9896 it.last_visible_y, -1,
9897 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9898 dy = it.current_y - y0;
9899 if (dy > scroll_max)
9900 return SCROLLING_FAILED;
9901
9902 /* Compute new window start. */
9903 start_display (&it, w, startp);
9904
9905 if (scroll_conservatively)
9906 amount_to_scroll =
9907 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9908 else if (scroll_step || temp_scroll_step)
9909 amount_to_scroll = scroll_max;
9910 else
9911 {
9912 aggressive = current_buffer->scroll_down_aggressively;
9913 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9914 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9915 if (NUMBERP (aggressive))
9916 amount_to_scroll = XFLOATINT (aggressive) * height;
9917 }
9918
9919 if (amount_to_scroll <= 0)
9920 return SCROLLING_FAILED;
9921
9922 move_it_vertically (&it, - amount_to_scroll);
9923 startp = it.current.pos;
9924 }
9925 }
9926
9927 /* Run window scroll functions. */
9928 startp = run_window_scroll_functions (window, startp);
9929
9930 /* Display the window. Give up if new fonts are loaded, or if point
9931 doesn't appear. */
9932 if (!try_window (window, startp))
9933 rc = SCROLLING_NEED_LARGER_MATRICES;
9934 else if (w->cursor.vpos < 0)
9935 {
9936 clear_glyph_matrix (w->desired_matrix);
9937 rc = SCROLLING_FAILED;
9938 }
9939 else
9940 {
9941 /* Maybe forget recorded base line for line number display. */
9942 if (!just_this_one_p
9943 || current_buffer->clip_changed
9944 || BEG_UNCHANGED < CHARPOS (startp))
9945 w->base_line_number = Qnil;
9946
9947 /* If cursor ends up on a partially visible line,
9948 treat that as being off the bottom of the screen. */
9949 if (! make_cursor_line_fully_visible (w))
9950 {
9951 clear_glyph_matrix (w->desired_matrix);
9952 last_line_misfit = 1;
9953 goto too_near_end;
9954 }
9955 rc = SCROLLING_SUCCESS;
9956 }
9957
9958 return rc;
9959 }
9960
9961
9962 /* Compute a suitable window start for window W if display of W starts
9963 on a continuation line. Value is non-zero if a new window start
9964 was computed.
9965
9966 The new window start will be computed, based on W's width, starting
9967 from the start of the continued line. It is the start of the
9968 screen line with the minimum distance from the old start W->start. */
9969
9970 static int
9971 compute_window_start_on_continuation_line (w)
9972 struct window *w;
9973 {
9974 struct text_pos pos, start_pos;
9975 int window_start_changed_p = 0;
9976
9977 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9978
9979 /* If window start is on a continuation line... Window start may be
9980 < BEGV in case there's invisible text at the start of the
9981 buffer (M-x rmail, for example). */
9982 if (CHARPOS (start_pos) > BEGV
9983 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9984 {
9985 struct it it;
9986 struct glyph_row *row;
9987
9988 /* Handle the case that the window start is out of range. */
9989 if (CHARPOS (start_pos) < BEGV)
9990 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9991 else if (CHARPOS (start_pos) > ZV)
9992 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9993
9994 /* Find the start of the continued line. This should be fast
9995 because scan_buffer is fast (newline cache). */
9996 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9997 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9998 row, DEFAULT_FACE_ID);
9999 reseat_at_previous_visible_line_start (&it);
10000
10001 /* If the line start is "too far" away from the window start,
10002 say it takes too much time to compute a new window start. */
10003 if (CHARPOS (start_pos) - IT_CHARPOS (it)
10004 < XFASTINT (w->height) * XFASTINT (w->width))
10005 {
10006 int min_distance, distance;
10007
10008 /* Move forward by display lines to find the new window
10009 start. If window width was enlarged, the new start can
10010 be expected to be > the old start. If window width was
10011 decreased, the new window start will be < the old start.
10012 So, we're looking for the display line start with the
10013 minimum distance from the old window start. */
10014 pos = it.current.pos;
10015 min_distance = INFINITY;
10016 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
10017 distance < min_distance)
10018 {
10019 min_distance = distance;
10020 pos = it.current.pos;
10021 move_it_by_lines (&it, 1, 0);
10022 }
10023
10024 /* Set the window start there. */
10025 SET_MARKER_FROM_TEXT_POS (w->start, pos);
10026 window_start_changed_p = 1;
10027 }
10028 }
10029
10030 return window_start_changed_p;
10031 }
10032
10033
10034 /* Try cursor movement in case text has not changed in window WINDOW,
10035 with window start STARTP. Value is
10036
10037 CURSOR_MOVEMENT_SUCCESS if successful
10038
10039 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
10040
10041 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
10042 display. *SCROLL_STEP is set to 1, under certain circumstances, if
10043 we want to scroll as if scroll-step were set to 1. See the code.
10044
10045 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
10046 which case we have to abort this redisplay, and adjust matrices
10047 first. */
10048
10049 enum
10050 {
10051 CURSOR_MOVEMENT_SUCCESS,
10052 CURSOR_MOVEMENT_CANNOT_BE_USED,
10053 CURSOR_MOVEMENT_MUST_SCROLL,
10054 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
10055 };
10056
10057 static int
10058 try_cursor_movement (window, startp, scroll_step)
10059 Lisp_Object window;
10060 struct text_pos startp;
10061 int *scroll_step;
10062 {
10063 struct window *w = XWINDOW (window);
10064 struct frame *f = XFRAME (w->frame);
10065 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
10066
10067 #if GLYPH_DEBUG
10068 if (inhibit_try_cursor_movement)
10069 return rc;
10070 #endif
10071
10072 /* Handle case where text has not changed, only point, and it has
10073 not moved off the frame. */
10074 if (/* Point may be in this window. */
10075 PT >= CHARPOS (startp)
10076 /* Selective display hasn't changed. */
10077 && !current_buffer->clip_changed
10078 /* Function force-mode-line-update is used to force a thorough
10079 redisplay. It sets either windows_or_buffers_changed or
10080 update_mode_lines. So don't take a shortcut here for these
10081 cases. */
10082 && !update_mode_lines
10083 && !windows_or_buffers_changed
10084 && !cursor_type_changed
10085 /* Can't use this case if highlighting a region. When a
10086 region exists, cursor movement has to do more than just
10087 set the cursor. */
10088 && !(!NILP (Vtransient_mark_mode)
10089 && !NILP (current_buffer->mark_active))
10090 && NILP (w->region_showing)
10091 && NILP (Vshow_trailing_whitespace)
10092 /* Right after splitting windows, last_point may be nil. */
10093 && INTEGERP (w->last_point)
10094 /* This code is not used for mini-buffer for the sake of the case
10095 of redisplaying to replace an echo area message; since in
10096 that case the mini-buffer contents per se are usually
10097 unchanged. This code is of no real use in the mini-buffer
10098 since the handling of this_line_start_pos, etc., in redisplay
10099 handles the same cases. */
10100 && !EQ (window, minibuf_window)
10101 /* When splitting windows or for new windows, it happens that
10102 redisplay is called with a nil window_end_vpos or one being
10103 larger than the window. This should really be fixed in
10104 window.c. I don't have this on my list, now, so we do
10105 approximately the same as the old redisplay code. --gerd. */
10106 && INTEGERP (w->window_end_vpos)
10107 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
10108 && (FRAME_WINDOW_P (f)
10109 || !MARKERP (Voverlay_arrow_position)
10110 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
10111 {
10112 int this_scroll_margin;
10113 struct glyph_row *row = NULL;
10114
10115 #if GLYPH_DEBUG
10116 debug_method_add (w, "cursor movement");
10117 #endif
10118
10119 /* Scroll if point within this distance from the top or bottom
10120 of the window. This is a pixel value. */
10121 this_scroll_margin = max (0, scroll_margin);
10122 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
10123 this_scroll_margin *= CANON_Y_UNIT (f);
10124
10125 /* Start with the row the cursor was displayed during the last
10126 not paused redisplay. Give up if that row is not valid. */
10127 if (w->last_cursor.vpos < 0
10128 || w->last_cursor.vpos >= w->current_matrix->nrows)
10129 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10130 else
10131 {
10132 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
10133 if (row->mode_line_p)
10134 ++row;
10135 if (!row->enabled_p)
10136 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10137 }
10138
10139 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
10140 {
10141 int scroll_p = 0;
10142 int last_y = window_text_bottom_y (w) - this_scroll_margin;
10143
10144 if (PT > XFASTINT (w->last_point))
10145 {
10146 /* Point has moved forward. */
10147 while (MATRIX_ROW_END_CHARPOS (row) < PT
10148 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10149 {
10150 xassert (row->enabled_p);
10151 ++row;
10152 }
10153
10154 /* The end position of a row equals the start position
10155 of the next row. If PT is there, we would rather
10156 display it in the next line. */
10157 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10158 && MATRIX_ROW_END_CHARPOS (row) == PT
10159 && !cursor_row_p (w, row))
10160 ++row;
10161
10162 /* If within the scroll margin, scroll. Note that
10163 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
10164 the next line would be drawn, and that
10165 this_scroll_margin can be zero. */
10166 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
10167 || PT > MATRIX_ROW_END_CHARPOS (row)
10168 /* Line is completely visible last line in window
10169 and PT is to be set in the next line. */
10170 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
10171 && PT == MATRIX_ROW_END_CHARPOS (row)
10172 && !row->ends_at_zv_p
10173 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
10174 scroll_p = 1;
10175 }
10176 else if (PT < XFASTINT (w->last_point))
10177 {
10178 /* Cursor has to be moved backward. Note that PT >=
10179 CHARPOS (startp) because of the outer
10180 if-statement. */
10181 while (!row->mode_line_p
10182 && (MATRIX_ROW_START_CHARPOS (row) > PT
10183 || (MATRIX_ROW_START_CHARPOS (row) == PT
10184 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
10185 && (row->y > this_scroll_margin
10186 || CHARPOS (startp) == BEGV))
10187 {
10188 xassert (row->enabled_p);
10189 --row;
10190 }
10191
10192 /* Consider the following case: Window starts at BEGV,
10193 there is invisible, intangible text at BEGV, so that
10194 display starts at some point START > BEGV. It can
10195 happen that we are called with PT somewhere between
10196 BEGV and START. Try to handle that case. */
10197 if (row < w->current_matrix->rows
10198 || row->mode_line_p)
10199 {
10200 row = w->current_matrix->rows;
10201 if (row->mode_line_p)
10202 ++row;
10203 }
10204
10205 /* Due to newlines in overlay strings, we may have to
10206 skip forward over overlay strings. */
10207 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10208 && MATRIX_ROW_END_CHARPOS (row) == PT
10209 && !cursor_row_p (w, row))
10210 ++row;
10211
10212 /* If within the scroll margin, scroll. */
10213 if (row->y < this_scroll_margin
10214 && CHARPOS (startp) != BEGV)
10215 scroll_p = 1;
10216 }
10217
10218 if (PT < MATRIX_ROW_START_CHARPOS (row)
10219 || PT > MATRIX_ROW_END_CHARPOS (row))
10220 {
10221 /* if PT is not in the glyph row, give up. */
10222 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10223 }
10224 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10225 {
10226 if (PT == MATRIX_ROW_END_CHARPOS (row)
10227 && !row->ends_at_zv_p
10228 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
10229 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10230 else if (row->height > window_box_height (w))
10231 {
10232 /* If we end up in a partially visible line, let's
10233 make it fully visible, except when it's taller
10234 than the window, in which case we can't do much
10235 about it. */
10236 *scroll_step = 1;
10237 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10238 }
10239 else
10240 {
10241 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10242 if (!make_cursor_line_fully_visible (w))
10243 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10244 else
10245 rc = CURSOR_MOVEMENT_SUCCESS;
10246 }
10247 }
10248 else if (scroll_p)
10249 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10250 else
10251 {
10252 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10253 rc = CURSOR_MOVEMENT_SUCCESS;
10254 }
10255 }
10256 }
10257
10258 return rc;
10259 }
10260
10261
10262 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
10263 selected_window is redisplayed.
10264
10265 We can return without actually redisplaying the window if
10266 fonts_changed_p is nonzero. In that case, redisplay_internal will
10267 retry. */
10268
10269 static void
10270 redisplay_window (window, just_this_one_p)
10271 Lisp_Object window;
10272 int just_this_one_p;
10273 {
10274 struct window *w = XWINDOW (window);
10275 struct frame *f = XFRAME (w->frame);
10276 struct buffer *buffer = XBUFFER (w->buffer);
10277 struct buffer *old = current_buffer;
10278 struct text_pos lpoint, opoint, startp;
10279 int update_mode_line;
10280 int tem;
10281 struct it it;
10282 /* Record it now because it's overwritten. */
10283 int current_matrix_up_to_date_p = 0;
10284 /* This is less strict than current_matrix_up_to_date_p.
10285 It indictes that the buffer contents and narrowing are unchanged. */
10286 int buffer_unchanged_p = 0;
10287 int temp_scroll_step = 0;
10288 int count = SPECPDL_INDEX ();
10289 int rc;
10290 int centering_position;
10291 int last_line_misfit = 0;
10292
10293 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10294 opoint = lpoint;
10295
10296 /* W must be a leaf window here. */
10297 xassert (!NILP (w->buffer));
10298 #if GLYPH_DEBUG
10299 *w->desired_matrix->method = 0;
10300 #endif
10301
10302 specbind (Qinhibit_point_motion_hooks, Qt);
10303
10304 reconsider_clip_changes (w, buffer);
10305
10306 /* Has the mode line to be updated? */
10307 update_mode_line = (!NILP (w->update_mode_line)
10308 || update_mode_lines
10309 || buffer->clip_changed
10310 || buffer->prevent_redisplay_optimizations_p);
10311
10312 if (MINI_WINDOW_P (w))
10313 {
10314 if (w == XWINDOW (echo_area_window)
10315 && !NILP (echo_area_buffer[0]))
10316 {
10317 if (update_mode_line)
10318 /* We may have to update a tty frame's menu bar or a
10319 tool-bar. Example `M-x C-h C-h C-g'. */
10320 goto finish_menu_bars;
10321 else
10322 /* We've already displayed the echo area glyphs in this window. */
10323 goto finish_scroll_bars;
10324 }
10325 else if ((w != XWINDOW (minibuf_window)
10326 || minibuf_level == 0)
10327 /* Quail displays non-mini buffers in minibuffer window.
10328 In that case, redisplay the window normally. */
10329 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
10330 {
10331 /* W is a mini-buffer window, but it's not active, so clear
10332 it. */
10333 int yb = window_text_bottom_y (w);
10334 struct glyph_row *row;
10335 int y;
10336
10337 for (y = 0, row = w->desired_matrix->rows;
10338 y < yb;
10339 y += row->height, ++row)
10340 blank_row (w, row, y);
10341 goto finish_scroll_bars;
10342 }
10343
10344 clear_glyph_matrix (w->desired_matrix);
10345 }
10346
10347 /* Otherwise set up data on this window; select its buffer and point
10348 value. */
10349 /* Really select the buffer, for the sake of buffer-local
10350 variables. */
10351 set_buffer_internal_1 (XBUFFER (w->buffer));
10352 SET_TEXT_POS (opoint, PT, PT_BYTE);
10353
10354 current_matrix_up_to_date_p
10355 = (!NILP (w->window_end_valid)
10356 && !current_buffer->clip_changed
10357 && !current_buffer->prevent_redisplay_optimizations_p
10358 && XFASTINT (w->last_modified) >= MODIFF
10359 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10360
10361 buffer_unchanged_p
10362 = (!NILP (w->window_end_valid)
10363 && !current_buffer->clip_changed
10364 && XFASTINT (w->last_modified) >= MODIFF
10365 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10366
10367 /* When windows_or_buffers_changed is non-zero, we can't rely on
10368 the window end being valid, so set it to nil there. */
10369 if (windows_or_buffers_changed)
10370 {
10371 /* If window starts on a continuation line, maybe adjust the
10372 window start in case the window's width changed. */
10373 if (XMARKER (w->start)->buffer == current_buffer)
10374 compute_window_start_on_continuation_line (w);
10375
10376 w->window_end_valid = Qnil;
10377 }
10378
10379 /* Some sanity checks. */
10380 CHECK_WINDOW_END (w);
10381 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
10382 abort ();
10383 if (BYTEPOS (opoint) < CHARPOS (opoint))
10384 abort ();
10385
10386 /* If %c is in mode line, update it if needed. */
10387 if (!NILP (w->column_number_displayed)
10388 /* This alternative quickly identifies a common case
10389 where no change is needed. */
10390 && !(PT == XFASTINT (w->last_point)
10391 && XFASTINT (w->last_modified) >= MODIFF
10392 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10393 && (XFASTINT (w->column_number_displayed)
10394 != (int) current_column ())) /* iftc */
10395 update_mode_line = 1;
10396
10397 /* Count number of windows showing the selected buffer. An indirect
10398 buffer counts as its base buffer. */
10399 if (!just_this_one_p)
10400 {
10401 struct buffer *current_base, *window_base;
10402 current_base = current_buffer;
10403 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10404 if (current_base->base_buffer)
10405 current_base = current_base->base_buffer;
10406 if (window_base->base_buffer)
10407 window_base = window_base->base_buffer;
10408 if (current_base == window_base)
10409 buffer_shared++;
10410 }
10411
10412 /* Point refers normally to the selected window. For any other
10413 window, set up appropriate value. */
10414 if (!EQ (window, selected_window))
10415 {
10416 int new_pt = XMARKER (w->pointm)->charpos;
10417 int new_pt_byte = marker_byte_position (w->pointm);
10418 if (new_pt < BEGV)
10419 {
10420 new_pt = BEGV;
10421 new_pt_byte = BEGV_BYTE;
10422 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10423 }
10424 else if (new_pt > (ZV - 1))
10425 {
10426 new_pt = ZV;
10427 new_pt_byte = ZV_BYTE;
10428 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10429 }
10430
10431 /* We don't use SET_PT so that the point-motion hooks don't run. */
10432 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10433 }
10434
10435 /* If any of the character widths specified in the display table
10436 have changed, invalidate the width run cache. It's true that
10437 this may be a bit late to catch such changes, but the rest of
10438 redisplay goes (non-fatally) haywire when the display table is
10439 changed, so why should we worry about doing any better? */
10440 if (current_buffer->width_run_cache)
10441 {
10442 struct Lisp_Char_Table *disptab = buffer_display_table ();
10443
10444 if (! disptab_matches_widthtab (disptab,
10445 XVECTOR (current_buffer->width_table)))
10446 {
10447 invalidate_region_cache (current_buffer,
10448 current_buffer->width_run_cache,
10449 BEG, Z);
10450 recompute_width_table (current_buffer, disptab);
10451 }
10452 }
10453
10454 /* If window-start is screwed up, choose a new one. */
10455 if (XMARKER (w->start)->buffer != current_buffer)
10456 goto recenter;
10457
10458 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10459
10460 /* If someone specified a new starting point but did not insist,
10461 check whether it can be used. */
10462 if (!NILP (w->optional_new_start)
10463 && CHARPOS (startp) >= BEGV
10464 && CHARPOS (startp) <= ZV)
10465 {
10466 w->optional_new_start = Qnil;
10467 start_display (&it, w, startp);
10468 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10469 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10470 if (IT_CHARPOS (it) == PT)
10471 w->force_start = Qt;
10472 }
10473
10474 /* Handle case where place to start displaying has been specified,
10475 unless the specified location is outside the accessible range. */
10476 if (!NILP (w->force_start)
10477 || w->frozen_window_start_p)
10478 {
10479 /* We set this later on if we have to adjust point. */
10480 int new_vpos = -1;
10481
10482 w->force_start = Qnil;
10483 w->vscroll = 0;
10484 w->window_end_valid = Qnil;
10485
10486 /* Forget any recorded base line for line number display. */
10487 if (!buffer_unchanged_p)
10488 w->base_line_number = Qnil;
10489
10490 /* Redisplay the mode line. Select the buffer properly for that.
10491 Also, run the hook window-scroll-functions
10492 because we have scrolled. */
10493 /* Note, we do this after clearing force_start because
10494 if there's an error, it is better to forget about force_start
10495 than to get into an infinite loop calling the hook functions
10496 and having them get more errors. */
10497 if (!update_mode_line
10498 || ! NILP (Vwindow_scroll_functions))
10499 {
10500 update_mode_line = 1;
10501 w->update_mode_line = Qt;
10502 startp = run_window_scroll_functions (window, startp);
10503 }
10504
10505 w->last_modified = make_number (0);
10506 w->last_overlay_modified = make_number (0);
10507 if (CHARPOS (startp) < BEGV)
10508 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10509 else if (CHARPOS (startp) > ZV)
10510 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10511
10512 /* Redisplay, then check if cursor has been set during the
10513 redisplay. Give up if new fonts were loaded. */
10514 if (!try_window (window, startp))
10515 {
10516 w->force_start = Qt;
10517 clear_glyph_matrix (w->desired_matrix);
10518 goto need_larger_matrices;
10519 }
10520
10521 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10522 {
10523 /* If point does not appear, try to move point so it does
10524 appear. The desired matrix has been built above, so we
10525 can use it here. */
10526 new_vpos = window_box_height (w) / 2;
10527 }
10528
10529 if (!make_cursor_line_fully_visible (w))
10530 {
10531 /* Point does appear, but on a line partly visible at end of window.
10532 Move it back to a fully-visible line. */
10533 new_vpos = window_box_height (w);
10534 }
10535
10536 /* If we need to move point for either of the above reasons,
10537 now actually do it. */
10538 if (new_vpos >= 0)
10539 {
10540 struct glyph_row *row;
10541
10542 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10543 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
10544 ++row;
10545
10546 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10547 MATRIX_ROW_START_BYTEPOS (row));
10548
10549 if (w != XWINDOW (selected_window))
10550 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10551 else if (current_buffer == old)
10552 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10553
10554 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10555
10556 /* If we are highlighting the region, then we just changed
10557 the region, so redisplay to show it. */
10558 if (!NILP (Vtransient_mark_mode)
10559 && !NILP (current_buffer->mark_active))
10560 {
10561 clear_glyph_matrix (w->desired_matrix);
10562 if (!try_window (window, startp))
10563 goto need_larger_matrices;
10564 }
10565 }
10566
10567 #if GLYPH_DEBUG
10568 debug_method_add (w, "forced window start");
10569 #endif
10570 goto done;
10571 }
10572
10573 /* Handle case where text has not changed, only point, and it has
10574 not moved off the frame, and we are not retrying after hscroll.
10575 (current_matrix_up_to_date_p is nonzero when retrying.) */
10576 if (current_matrix_up_to_date_p
10577 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10578 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10579 {
10580 switch (rc)
10581 {
10582 case CURSOR_MOVEMENT_SUCCESS:
10583 goto done;
10584
10585 #if 0 /* try_cursor_movement never returns this value. */
10586 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10587 goto need_larger_matrices;
10588 #endif
10589
10590 case CURSOR_MOVEMENT_MUST_SCROLL:
10591 goto try_to_scroll;
10592
10593 default:
10594 abort ();
10595 }
10596 }
10597 /* If current starting point was originally the beginning of a line
10598 but no longer is, find a new starting point. */
10599 else if (!NILP (w->start_at_line_beg)
10600 && !(CHARPOS (startp) <= BEGV
10601 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10602 {
10603 #if GLYPH_DEBUG
10604 debug_method_add (w, "recenter 1");
10605 #endif
10606 goto recenter;
10607 }
10608
10609 /* Try scrolling with try_window_id. Value is > 0 if update has
10610 been done, it is -1 if we know that the same window start will
10611 not work. It is 0 if unsuccessful for some other reason. */
10612 else if ((tem = try_window_id (w)) != 0)
10613 {
10614 #if GLYPH_DEBUG
10615 debug_method_add (w, "try_window_id %d", tem);
10616 #endif
10617
10618 if (fonts_changed_p)
10619 goto need_larger_matrices;
10620 if (tem > 0)
10621 goto done;
10622
10623 /* Otherwise try_window_id has returned -1 which means that we
10624 don't want the alternative below this comment to execute. */
10625 }
10626 else if (CHARPOS (startp) >= BEGV
10627 && CHARPOS (startp) <= ZV
10628 && PT >= CHARPOS (startp)
10629 && (CHARPOS (startp) < ZV
10630 /* Avoid starting at end of buffer. */
10631 || CHARPOS (startp) == BEGV
10632 || (XFASTINT (w->last_modified) >= MODIFF
10633 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10634 {
10635 #if GLYPH_DEBUG
10636 debug_method_add (w, "same window start");
10637 #endif
10638
10639 /* Try to redisplay starting at same place as before.
10640 If point has not moved off frame, accept the results. */
10641 if (!current_matrix_up_to_date_p
10642 /* Don't use try_window_reusing_current_matrix in this case
10643 because a window scroll function can have changed the
10644 buffer. */
10645 || !NILP (Vwindow_scroll_functions)
10646 || MINI_WINDOW_P (w)
10647 || !try_window_reusing_current_matrix (w))
10648 {
10649 IF_DEBUG (debug_method_add (w, "1"));
10650 try_window (window, startp);
10651 }
10652
10653 if (fonts_changed_p)
10654 goto need_larger_matrices;
10655
10656 if (w->cursor.vpos >= 0)
10657 {
10658 if (!just_this_one_p
10659 || current_buffer->clip_changed
10660 || BEG_UNCHANGED < CHARPOS (startp))
10661 /* Forget any recorded base line for line number display. */
10662 w->base_line_number = Qnil;
10663
10664 if (!make_cursor_line_fully_visible (w))
10665 {
10666 clear_glyph_matrix (w->desired_matrix);
10667 last_line_misfit = 1;
10668 }
10669 /* Drop through and scroll. */
10670 else
10671 goto done;
10672 }
10673 else
10674 clear_glyph_matrix (w->desired_matrix);
10675 }
10676
10677 try_to_scroll:
10678
10679 w->last_modified = make_number (0);
10680 w->last_overlay_modified = make_number (0);
10681
10682 /* Redisplay the mode line. Select the buffer properly for that. */
10683 if (!update_mode_line)
10684 {
10685 update_mode_line = 1;
10686 w->update_mode_line = Qt;
10687 }
10688
10689 /* Try to scroll by specified few lines. */
10690 if ((scroll_conservatively
10691 || scroll_step
10692 || temp_scroll_step
10693 || NUMBERP (current_buffer->scroll_up_aggressively)
10694 || NUMBERP (current_buffer->scroll_down_aggressively))
10695 && !current_buffer->clip_changed
10696 && CHARPOS (startp) >= BEGV
10697 && CHARPOS (startp) <= ZV)
10698 {
10699 /* The function returns -1 if new fonts were loaded, 1 if
10700 successful, 0 if not successful. */
10701 int rc = try_scrolling (window, just_this_one_p,
10702 scroll_conservatively,
10703 scroll_step,
10704 temp_scroll_step, last_line_misfit);
10705 switch (rc)
10706 {
10707 case SCROLLING_SUCCESS:
10708 goto done;
10709
10710 case SCROLLING_NEED_LARGER_MATRICES:
10711 goto need_larger_matrices;
10712
10713 case SCROLLING_FAILED:
10714 break;
10715
10716 default:
10717 abort ();
10718 }
10719 }
10720
10721 /* Finally, just choose place to start which centers point */
10722
10723 recenter:
10724 centering_position = window_box_height (w) / 2;
10725
10726 point_at_top:
10727 /* Jump here with centering_position already set to 0. */
10728
10729 #if GLYPH_DEBUG
10730 debug_method_add (w, "recenter");
10731 #endif
10732
10733 /* w->vscroll = 0; */
10734
10735 /* Forget any previously recorded base line for line number display. */
10736 if (!buffer_unchanged_p)
10737 w->base_line_number = Qnil;
10738
10739 /* Move backward half the height of the window. */
10740 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10741 it.current_y = it.last_visible_y;
10742 move_it_vertically_backward (&it, centering_position);
10743 xassert (IT_CHARPOS (it) >= BEGV);
10744
10745 /* The function move_it_vertically_backward may move over more
10746 than the specified y-distance. If it->w is small, e.g. a
10747 mini-buffer window, we may end up in front of the window's
10748 display area. Start displaying at the start of the line
10749 containing PT in this case. */
10750 if (it.current_y <= 0)
10751 {
10752 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10753 move_it_vertically (&it, 0);
10754 xassert (IT_CHARPOS (it) <= PT);
10755 it.current_y = 0;
10756 }
10757
10758 it.current_x = it.hpos = 0;
10759
10760 /* Set startp here explicitly in case that helps avoid an infinite loop
10761 in case the window-scroll-functions functions get errors. */
10762 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10763
10764 /* Run scroll hooks. */
10765 startp = run_window_scroll_functions (window, it.current.pos);
10766
10767 /* Redisplay the window. */
10768 if (!current_matrix_up_to_date_p
10769 || windows_or_buffers_changed
10770 || cursor_type_changed
10771 /* Don't use try_window_reusing_current_matrix in this case
10772 because it can have changed the buffer. */
10773 || !NILP (Vwindow_scroll_functions)
10774 || !just_this_one_p
10775 || MINI_WINDOW_P (w)
10776 || !try_window_reusing_current_matrix (w))
10777 try_window (window, startp);
10778
10779 /* If new fonts have been loaded (due to fontsets), give up. We
10780 have to start a new redisplay since we need to re-adjust glyph
10781 matrices. */
10782 if (fonts_changed_p)
10783 goto need_larger_matrices;
10784
10785 /* If cursor did not appear assume that the middle of the window is
10786 in the first line of the window. Do it again with the next line.
10787 (Imagine a window of height 100, displaying two lines of height
10788 60. Moving back 50 from it->last_visible_y will end in the first
10789 line.) */
10790 if (w->cursor.vpos < 0)
10791 {
10792 if (!NILP (w->window_end_valid)
10793 && PT >= Z - XFASTINT (w->window_end_pos))
10794 {
10795 clear_glyph_matrix (w->desired_matrix);
10796 move_it_by_lines (&it, 1, 0);
10797 try_window (window, it.current.pos);
10798 }
10799 else if (PT < IT_CHARPOS (it))
10800 {
10801 clear_glyph_matrix (w->desired_matrix);
10802 move_it_by_lines (&it, -1, 0);
10803 try_window (window, it.current.pos);
10804 }
10805 else
10806 {
10807 /* Not much we can do about it. */
10808 }
10809 }
10810
10811 /* Consider the following case: Window starts at BEGV, there is
10812 invisible, intangible text at BEGV, so that display starts at
10813 some point START > BEGV. It can happen that we are called with
10814 PT somewhere between BEGV and START. Try to handle that case. */
10815 if (w->cursor.vpos < 0)
10816 {
10817 struct glyph_row *row = w->current_matrix->rows;
10818 if (row->mode_line_p)
10819 ++row;
10820 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10821 }
10822
10823 if (!make_cursor_line_fully_visible (w))
10824 {
10825 /* If vscroll is enabled, disable it and try again. */
10826 if (w->vscroll)
10827 {
10828 w->vscroll = 0;
10829 clear_glyph_matrix (w->desired_matrix);
10830 goto recenter;
10831 }
10832
10833 /* If centering point failed to make the whole line visible,
10834 put point at the top instead. That has to make the whole line
10835 visible, if it can be done. */
10836 centering_position = 0;
10837 goto point_at_top;
10838 }
10839
10840 done:
10841
10842 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10843 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10844 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10845 ? Qt : Qnil);
10846
10847 /* Display the mode line, if we must. */
10848 if ((update_mode_line
10849 /* If window not full width, must redo its mode line
10850 if (a) the window to its side is being redone and
10851 (b) we do a frame-based redisplay. This is a consequence
10852 of how inverted lines are drawn in frame-based redisplay. */
10853 || (!just_this_one_p
10854 && !FRAME_WINDOW_P (f)
10855 && !WINDOW_FULL_WIDTH_P (w))
10856 /* Line number to display. */
10857 || INTEGERP (w->base_line_pos)
10858 /* Column number is displayed and different from the one displayed. */
10859 || (!NILP (w->column_number_displayed)
10860 && (XFASTINT (w->column_number_displayed)
10861 != (int) current_column ()))) /* iftc */
10862 /* This means that the window has a mode line. */
10863 && (WINDOW_WANTS_MODELINE_P (w)
10864 || WINDOW_WANTS_HEADER_LINE_P (w)))
10865 {
10866 display_mode_lines (w);
10867
10868 /* If mode line height has changed, arrange for a thorough
10869 immediate redisplay using the correct mode line height. */
10870 if (WINDOW_WANTS_MODELINE_P (w)
10871 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10872 {
10873 fonts_changed_p = 1;
10874 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10875 = DESIRED_MODE_LINE_HEIGHT (w);
10876 }
10877
10878 /* If top line height has changed, arrange for a thorough
10879 immediate redisplay using the correct mode line height. */
10880 if (WINDOW_WANTS_HEADER_LINE_P (w)
10881 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10882 {
10883 fonts_changed_p = 1;
10884 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10885 = DESIRED_HEADER_LINE_HEIGHT (w);
10886 }
10887
10888 if (fonts_changed_p)
10889 goto need_larger_matrices;
10890 }
10891
10892 if (!line_number_displayed
10893 && !BUFFERP (w->base_line_pos))
10894 {
10895 w->base_line_pos = Qnil;
10896 w->base_line_number = Qnil;
10897 }
10898
10899 finish_menu_bars:
10900
10901 /* When we reach a frame's selected window, redo the frame's menu bar. */
10902 if (update_mode_line
10903 && EQ (FRAME_SELECTED_WINDOW (f), window))
10904 {
10905 int redisplay_menu_p = 0;
10906 int redisplay_tool_bar_p = 0;
10907
10908 if (FRAME_WINDOW_P (f))
10909 {
10910 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
10911 || defined (USE_GTK)
10912 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10913 #else
10914 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10915 #endif
10916 }
10917 else
10918 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10919
10920 if (redisplay_menu_p)
10921 display_menu_bar (w);
10922
10923 #ifdef HAVE_WINDOW_SYSTEM
10924 #ifdef USE_GTK
10925 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
10926 #else
10927 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
10928 && (FRAME_TOOL_BAR_LINES (f) > 0
10929 || auto_resize_tool_bars_p);
10930
10931 #endif
10932
10933 if (redisplay_tool_bar_p)
10934 redisplay_tool_bar (f);
10935 #endif
10936 }
10937
10938 /* We go to this label, with fonts_changed_p nonzero,
10939 if it is necessary to try again using larger glyph matrices.
10940 We have to redeem the scroll bar even in this case,
10941 because the loop in redisplay_internal expects that. */
10942 need_larger_matrices:
10943 ;
10944 finish_scroll_bars:
10945
10946 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10947 {
10948 int start, end, whole;
10949
10950 /* Calculate the start and end positions for the current window.
10951 At some point, it would be nice to choose between scrollbars
10952 which reflect the whole buffer size, with special markers
10953 indicating narrowing, and scrollbars which reflect only the
10954 visible region.
10955
10956 Note that mini-buffers sometimes aren't displaying any text. */
10957 if (!MINI_WINDOW_P (w)
10958 || (w == XWINDOW (minibuf_window)
10959 && NILP (echo_area_buffer[0])))
10960 {
10961 whole = ZV - BEGV;
10962 start = marker_position (w->start) - BEGV;
10963 /* I don't think this is guaranteed to be right. For the
10964 moment, we'll pretend it is. */
10965 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10966
10967 if (end < start)
10968 end = start;
10969 if (whole < (end - start))
10970 whole = end - start;
10971 }
10972 else
10973 start = end = whole = 0;
10974
10975 /* Indicate what this scroll bar ought to be displaying now. */
10976 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10977
10978 /* Note that we actually used the scroll bar attached to this
10979 window, so it shouldn't be deleted at the end of redisplay. */
10980 redeem_scroll_bar_hook (w);
10981 }
10982
10983 /* Restore current_buffer and value of point in it. */
10984 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10985 set_buffer_internal_1 (old);
10986 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10987
10988 unbind_to (count, Qnil);
10989 }
10990
10991
10992 /* Build the complete desired matrix of WINDOW with a window start
10993 buffer position POS. Value is non-zero if successful. It is zero
10994 if fonts were loaded during redisplay which makes re-adjusting
10995 glyph matrices necessary. */
10996
10997 int
10998 try_window (window, pos)
10999 Lisp_Object window;
11000 struct text_pos pos;
11001 {
11002 struct window *w = XWINDOW (window);
11003 struct it it;
11004 struct glyph_row *last_text_row = NULL;
11005
11006 /* Make POS the new window start. */
11007 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
11008
11009 /* Mark cursor position as unknown. No overlay arrow seen. */
11010 w->cursor.vpos = -1;
11011 overlay_arrow_seen = 0;
11012
11013 /* Initialize iterator and info to start at POS. */
11014 start_display (&it, w, pos);
11015
11016 /* Display all lines of W. */
11017 while (it.current_y < it.last_visible_y)
11018 {
11019 if (display_line (&it))
11020 last_text_row = it.glyph_row - 1;
11021 if (fonts_changed_p)
11022 return 0;
11023 }
11024
11025 /* If bottom moved off end of frame, change mode line percentage. */
11026 if (XFASTINT (w->window_end_pos) <= 0
11027 && Z != IT_CHARPOS (it))
11028 w->update_mode_line = Qt;
11029
11030 /* Set window_end_pos to the offset of the last character displayed
11031 on the window from the end of current_buffer. Set
11032 window_end_vpos to its row number. */
11033 if (last_text_row)
11034 {
11035 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
11036 w->window_end_bytepos
11037 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11038 w->window_end_pos
11039 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11040 w->window_end_vpos
11041 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11042 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
11043 ->displays_text_p);
11044 }
11045 else
11046 {
11047 w->window_end_bytepos = 0;
11048 w->window_end_pos = w->window_end_vpos = make_number (0);
11049 }
11050
11051 /* But that is not valid info until redisplay finishes. */
11052 w->window_end_valid = Qnil;
11053 return 1;
11054 }
11055
11056
11057 \f
11058 /************************************************************************
11059 Window redisplay reusing current matrix when buffer has not changed
11060 ************************************************************************/
11061
11062 /* Try redisplay of window W showing an unchanged buffer with a
11063 different window start than the last time it was displayed by
11064 reusing its current matrix. Value is non-zero if successful.
11065 W->start is the new window start. */
11066
11067 static int
11068 try_window_reusing_current_matrix (w)
11069 struct window *w;
11070 {
11071 struct frame *f = XFRAME (w->frame);
11072 struct glyph_row *row, *bottom_row;
11073 struct it it;
11074 struct run run;
11075 struct text_pos start, new_start;
11076 int nrows_scrolled, i;
11077 struct glyph_row *last_text_row;
11078 struct glyph_row *last_reused_text_row;
11079 struct glyph_row *start_row;
11080 int start_vpos, min_y, max_y;
11081
11082 #if GLYPH_DEBUG
11083 if (inhibit_try_window_reusing)
11084 return 0;
11085 #endif
11086
11087 if (/* This function doesn't handle terminal frames. */
11088 !FRAME_WINDOW_P (f)
11089 /* Don't try to reuse the display if windows have been split
11090 or such. */
11091 || windows_or_buffers_changed
11092 || cursor_type_changed)
11093 return 0;
11094
11095 /* Can't do this if region may have changed. */
11096 if ((!NILP (Vtransient_mark_mode)
11097 && !NILP (current_buffer->mark_active))
11098 || !NILP (w->region_showing)
11099 || !NILP (Vshow_trailing_whitespace))
11100 return 0;
11101
11102 /* If top-line visibility has changed, give up. */
11103 if (WINDOW_WANTS_HEADER_LINE_P (w)
11104 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
11105 return 0;
11106
11107 /* Give up if old or new display is scrolled vertically. We could
11108 make this function handle this, but right now it doesn't. */
11109 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11110 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
11111 return 0;
11112
11113 /* The variable new_start now holds the new window start. The old
11114 start `start' can be determined from the current matrix. */
11115 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
11116 start = start_row->start.pos;
11117 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
11118
11119 /* Clear the desired matrix for the display below. */
11120 clear_glyph_matrix (w->desired_matrix);
11121
11122 if (CHARPOS (new_start) <= CHARPOS (start))
11123 {
11124 int first_row_y;
11125
11126 /* Don't use this method if the display starts with an ellipsis
11127 displayed for invisible text. It's not easy to handle that case
11128 below, and it's certainly not worth the effort since this is
11129 not a frequent case. */
11130 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
11131 return 0;
11132
11133 IF_DEBUG (debug_method_add (w, "twu1"));
11134
11135 /* Display up to a row that can be reused. The variable
11136 last_text_row is set to the last row displayed that displays
11137 text. Note that it.vpos == 0 if or if not there is a
11138 header-line; it's not the same as the MATRIX_ROW_VPOS! */
11139 start_display (&it, w, new_start);
11140 first_row_y = it.current_y;
11141 w->cursor.vpos = -1;
11142 last_text_row = last_reused_text_row = NULL;
11143
11144 while (it.current_y < it.last_visible_y
11145 && IT_CHARPOS (it) < CHARPOS (start)
11146 && !fonts_changed_p)
11147 if (display_line (&it))
11148 last_text_row = it.glyph_row - 1;
11149
11150 /* A value of current_y < last_visible_y means that we stopped
11151 at the previous window start, which in turn means that we
11152 have at least one reusable row. */
11153 if (it.current_y < it.last_visible_y)
11154 {
11155 /* IT.vpos always starts from 0; it counts text lines. */
11156 nrows_scrolled = it.vpos;
11157
11158 /* Find PT if not already found in the lines displayed. */
11159 if (w->cursor.vpos < 0)
11160 {
11161 int dy = it.current_y - first_row_y;
11162
11163 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11164 row = row_containing_pos (w, PT, row, NULL, dy);
11165 if (row)
11166 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
11167 dy, nrows_scrolled);
11168 else
11169 {
11170 clear_glyph_matrix (w->desired_matrix);
11171 return 0;
11172 }
11173 }
11174
11175 /* Scroll the display. Do it before the current matrix is
11176 changed. The problem here is that update has not yet
11177 run, i.e. part of the current matrix is not up to date.
11178 scroll_run_hook will clear the cursor, and use the
11179 current matrix to get the height of the row the cursor is
11180 in. */
11181 run.current_y = first_row_y;
11182 run.desired_y = it.current_y;
11183 run.height = it.last_visible_y - it.current_y;
11184
11185 if (run.height > 0 && run.current_y != run.desired_y)
11186 {
11187 update_begin (f);
11188 rif->update_window_begin_hook (w);
11189 rif->clear_mouse_face (w);
11190 rif->scroll_run_hook (w, &run);
11191 rif->update_window_end_hook (w, 0, 0);
11192 update_end (f);
11193 }
11194
11195 /* Shift current matrix down by nrows_scrolled lines. */
11196 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11197 rotate_matrix (w->current_matrix,
11198 start_vpos,
11199 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11200 nrows_scrolled);
11201
11202 /* Disable lines that must be updated. */
11203 for (i = 0; i < it.vpos; ++i)
11204 (start_row + i)->enabled_p = 0;
11205
11206 /* Re-compute Y positions. */
11207 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11208 max_y = it.last_visible_y;
11209 for (row = start_row + nrows_scrolled;
11210 row < bottom_row;
11211 ++row)
11212 {
11213 row->y = it.current_y;
11214 row->visible_height = row->height;
11215
11216 if (row->y < min_y)
11217 row->visible_height -= min_y - row->y;
11218 if (row->y + row->height > max_y)
11219 row->visible_height -= row->y + row->height - max_y;
11220
11221 it.current_y += row->height;
11222
11223 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11224 last_reused_text_row = row;
11225 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
11226 break;
11227 }
11228
11229 /* Disable lines in the current matrix which are now
11230 below the window. */
11231 for (++row; row < bottom_row; ++row)
11232 row->enabled_p = 0;
11233 }
11234
11235 /* Update window_end_pos etc.; last_reused_text_row is the last
11236 reused row from the current matrix containing text, if any.
11237 The value of last_text_row is the last displayed line
11238 containing text. */
11239 if (last_reused_text_row)
11240 {
11241 w->window_end_bytepos
11242 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
11243 w->window_end_pos
11244 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
11245 w->window_end_vpos
11246 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
11247 w->current_matrix));
11248 }
11249 else if (last_text_row)
11250 {
11251 w->window_end_bytepos
11252 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11253 w->window_end_pos
11254 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11255 w->window_end_vpos
11256 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11257 }
11258 else
11259 {
11260 /* This window must be completely empty. */
11261 w->window_end_bytepos = 0;
11262 w->window_end_pos = w->window_end_vpos = make_number (0);
11263 }
11264 w->window_end_valid = Qnil;
11265
11266 /* Update hint: don't try scrolling again in update_window. */
11267 w->desired_matrix->no_scrolling_p = 1;
11268
11269 #if GLYPH_DEBUG
11270 debug_method_add (w, "try_window_reusing_current_matrix 1");
11271 #endif
11272 return 1;
11273 }
11274 else if (CHARPOS (new_start) > CHARPOS (start))
11275 {
11276 struct glyph_row *pt_row, *row;
11277 struct glyph_row *first_reusable_row;
11278 struct glyph_row *first_row_to_display;
11279 int dy;
11280 int yb = window_text_bottom_y (w);
11281
11282 /* Find the row starting at new_start, if there is one. Don't
11283 reuse a partially visible line at the end. */
11284 first_reusable_row = start_row;
11285 while (first_reusable_row->enabled_p
11286 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
11287 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11288 < CHARPOS (new_start)))
11289 ++first_reusable_row;
11290
11291 /* Give up if there is no row to reuse. */
11292 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
11293 || !first_reusable_row->enabled_p
11294 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11295 != CHARPOS (new_start)))
11296 return 0;
11297
11298 /* We can reuse fully visible rows beginning with
11299 first_reusable_row to the end of the window. Set
11300 first_row_to_display to the first row that cannot be reused.
11301 Set pt_row to the row containing point, if there is any. */
11302 pt_row = NULL;
11303 for (first_row_to_display = first_reusable_row;
11304 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
11305 ++first_row_to_display)
11306 {
11307 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
11308 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
11309 pt_row = first_row_to_display;
11310 }
11311
11312 /* Start displaying at the start of first_row_to_display. */
11313 xassert (first_row_to_display->y < yb);
11314 init_to_row_start (&it, w, first_row_to_display);
11315
11316 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
11317 - start_vpos);
11318 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
11319 - nrows_scrolled);
11320 it.current_y = (first_row_to_display->y - first_reusable_row->y
11321 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
11322
11323 /* Display lines beginning with first_row_to_display in the
11324 desired matrix. Set last_text_row to the last row displayed
11325 that displays text. */
11326 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
11327 if (pt_row == NULL)
11328 w->cursor.vpos = -1;
11329 last_text_row = NULL;
11330 while (it.current_y < it.last_visible_y && !fonts_changed_p)
11331 if (display_line (&it))
11332 last_text_row = it.glyph_row - 1;
11333
11334 /* Give up If point isn't in a row displayed or reused. */
11335 if (w->cursor.vpos < 0)
11336 {
11337 clear_glyph_matrix (w->desired_matrix);
11338 return 0;
11339 }
11340
11341 /* If point is in a reused row, adjust y and vpos of the cursor
11342 position. */
11343 if (pt_row)
11344 {
11345 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
11346 w->current_matrix);
11347 w->cursor.y -= first_reusable_row->y;
11348 }
11349
11350 /* Scroll the display. */
11351 run.current_y = first_reusable_row->y;
11352 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11353 run.height = it.last_visible_y - run.current_y;
11354 dy = run.current_y - run.desired_y;
11355
11356 if (run.height)
11357 {
11358 struct frame *f = XFRAME (WINDOW_FRAME (w));
11359 update_begin (f);
11360 rif->update_window_begin_hook (w);
11361 rif->clear_mouse_face (w);
11362 rif->scroll_run_hook (w, &run);
11363 rif->update_window_end_hook (w, 0, 0);
11364 update_end (f);
11365 }
11366
11367 /* Adjust Y positions of reused rows. */
11368 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11369 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11370 max_y = it.last_visible_y;
11371 for (row = first_reusable_row; row < first_row_to_display; ++row)
11372 {
11373 row->y -= dy;
11374 row->visible_height = row->height;
11375 if (row->y < min_y)
11376 row->visible_height -= min_y - row->y;
11377 if (row->y + row->height > max_y)
11378 row->visible_height -= row->y + row->height - max_y;
11379 }
11380
11381 /* Scroll the current matrix. */
11382 xassert (nrows_scrolled > 0);
11383 rotate_matrix (w->current_matrix,
11384 start_vpos,
11385 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11386 -nrows_scrolled);
11387
11388 /* Disable rows not reused. */
11389 for (row -= nrows_scrolled; row < bottom_row; ++row)
11390 row->enabled_p = 0;
11391
11392 /* Adjust window end. A null value of last_text_row means that
11393 the window end is in reused rows which in turn means that
11394 only its vpos can have changed. */
11395 if (last_text_row)
11396 {
11397 w->window_end_bytepos
11398 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11399 w->window_end_pos
11400 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11401 w->window_end_vpos
11402 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11403 }
11404 else
11405 {
11406 w->window_end_vpos
11407 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
11408 }
11409
11410 w->window_end_valid = Qnil;
11411 w->desired_matrix->no_scrolling_p = 1;
11412
11413 #if GLYPH_DEBUG
11414 debug_method_add (w, "try_window_reusing_current_matrix 2");
11415 #endif
11416 return 1;
11417 }
11418
11419 return 0;
11420 }
11421
11422
11423 \f
11424 /************************************************************************
11425 Window redisplay reusing current matrix when buffer has changed
11426 ************************************************************************/
11427
11428 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11429 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
11430 int *, int *));
11431 static struct glyph_row *
11432 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11433 struct glyph_row *));
11434
11435
11436 /* Return the last row in MATRIX displaying text. If row START is
11437 non-null, start searching with that row. IT gives the dimensions
11438 of the display. Value is null if matrix is empty; otherwise it is
11439 a pointer to the row found. */
11440
11441 static struct glyph_row *
11442 find_last_row_displaying_text (matrix, it, start)
11443 struct glyph_matrix *matrix;
11444 struct it *it;
11445 struct glyph_row *start;
11446 {
11447 struct glyph_row *row, *row_found;
11448
11449 /* Set row_found to the last row in IT->w's current matrix
11450 displaying text. The loop looks funny but think of partially
11451 visible lines. */
11452 row_found = NULL;
11453 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11454 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11455 {
11456 xassert (row->enabled_p);
11457 row_found = row;
11458 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11459 break;
11460 ++row;
11461 }
11462
11463 return row_found;
11464 }
11465
11466
11467 /* Return the last row in the current matrix of W that is not affected
11468 by changes at the start of current_buffer that occurred since W's
11469 current matrix was built. Value is null if no such row exists.
11470
11471 BEG_UNCHANGED us the number of characters unchanged at the start of
11472 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11473 first changed character in current_buffer. Characters at positions <
11474 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11475 when the current matrix was built. */
11476
11477 static struct glyph_row *
11478 find_last_unchanged_at_beg_row (w)
11479 struct window *w;
11480 {
11481 int first_changed_pos = BEG + BEG_UNCHANGED;
11482 struct glyph_row *row;
11483 struct glyph_row *row_found = NULL;
11484 int yb = window_text_bottom_y (w);
11485
11486 /* Find the last row displaying unchanged text. */
11487 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11488 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11489 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11490 {
11491 if (/* If row ends before first_changed_pos, it is unchanged,
11492 except in some case. */
11493 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11494 /* When row ends in ZV and we write at ZV it is not
11495 unchanged. */
11496 && !row->ends_at_zv_p
11497 /* When first_changed_pos is the end of a continued line,
11498 row is not unchanged because it may be no longer
11499 continued. */
11500 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11501 && row->continued_p))
11502 row_found = row;
11503
11504 /* Stop if last visible row. */
11505 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11506 break;
11507
11508 ++row;
11509 }
11510
11511 return row_found;
11512 }
11513
11514
11515 /* Find the first glyph row in the current matrix of W that is not
11516 affected by changes at the end of current_buffer since the
11517 time W's current matrix was built.
11518
11519 Return in *DELTA the number of chars by which buffer positions in
11520 unchanged text at the end of current_buffer must be adjusted.
11521
11522 Return in *DELTA_BYTES the corresponding number of bytes.
11523
11524 Value is null if no such row exists, i.e. all rows are affected by
11525 changes. */
11526
11527 static struct glyph_row *
11528 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11529 struct window *w;
11530 int *delta, *delta_bytes;
11531 {
11532 struct glyph_row *row;
11533 struct glyph_row *row_found = NULL;
11534
11535 *delta = *delta_bytes = 0;
11536
11537 /* Display must not have been paused, otherwise the current matrix
11538 is not up to date. */
11539 if (NILP (w->window_end_valid))
11540 abort ();
11541
11542 /* A value of window_end_pos >= END_UNCHANGED means that the window
11543 end is in the range of changed text. If so, there is no
11544 unchanged row at the end of W's current matrix. */
11545 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11546 return NULL;
11547
11548 /* Set row to the last row in W's current matrix displaying text. */
11549 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11550
11551 /* If matrix is entirely empty, no unchanged row exists. */
11552 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11553 {
11554 /* The value of row is the last glyph row in the matrix having a
11555 meaningful buffer position in it. The end position of row
11556 corresponds to window_end_pos. This allows us to translate
11557 buffer positions in the current matrix to current buffer
11558 positions for characters not in changed text. */
11559 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11560 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11561 int last_unchanged_pos, last_unchanged_pos_old;
11562 struct glyph_row *first_text_row
11563 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11564
11565 *delta = Z - Z_old;
11566 *delta_bytes = Z_BYTE - Z_BYTE_old;
11567
11568 /* Set last_unchanged_pos to the buffer position of the last
11569 character in the buffer that has not been changed. Z is the
11570 index + 1 of the last character in current_buffer, i.e. by
11571 subtracting END_UNCHANGED we get the index of the last
11572 unchanged character, and we have to add BEG to get its buffer
11573 position. */
11574 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11575 last_unchanged_pos_old = last_unchanged_pos - *delta;
11576
11577 /* Search backward from ROW for a row displaying a line that
11578 starts at a minimum position >= last_unchanged_pos_old. */
11579 for (; row > first_text_row; --row)
11580 {
11581 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11582 abort ();
11583
11584 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11585 row_found = row;
11586 }
11587 }
11588
11589 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11590 abort ();
11591
11592 return row_found;
11593 }
11594
11595
11596 /* Make sure that glyph rows in the current matrix of window W
11597 reference the same glyph memory as corresponding rows in the
11598 frame's frame matrix. This function is called after scrolling W's
11599 current matrix on a terminal frame in try_window_id and
11600 try_window_reusing_current_matrix. */
11601
11602 static void
11603 sync_frame_with_window_matrix_rows (w)
11604 struct window *w;
11605 {
11606 struct frame *f = XFRAME (w->frame);
11607 struct glyph_row *window_row, *window_row_end, *frame_row;
11608
11609 /* Preconditions: W must be a leaf window and full-width. Its frame
11610 must have a frame matrix. */
11611 xassert (NILP (w->hchild) && NILP (w->vchild));
11612 xassert (WINDOW_FULL_WIDTH_P (w));
11613 xassert (!FRAME_WINDOW_P (f));
11614
11615 /* If W is a full-width window, glyph pointers in W's current matrix
11616 have, by definition, to be the same as glyph pointers in the
11617 corresponding frame matrix. Note that frame matrices have no
11618 marginal areas (see build_frame_matrix). */
11619 window_row = w->current_matrix->rows;
11620 window_row_end = window_row + w->current_matrix->nrows;
11621 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11622 while (window_row < window_row_end)
11623 {
11624 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
11625 struct glyph *end = window_row->glyphs[LAST_AREA];
11626
11627 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
11628 frame_row->glyphs[TEXT_AREA] = start;
11629 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
11630 frame_row->glyphs[LAST_AREA] = end;
11631
11632 /* Disable frame rows whose corresponding window rows have
11633 been disabled in try_window_id. */
11634 if (!window_row->enabled_p)
11635 frame_row->enabled_p = 0;
11636
11637 ++window_row, ++frame_row;
11638 }
11639 }
11640
11641
11642 /* Find the glyph row in window W containing CHARPOS. Consider all
11643 rows between START and END (not inclusive). END null means search
11644 all rows to the end of the display area of W. Value is the row
11645 containing CHARPOS or null. */
11646
11647 struct glyph_row *
11648 row_containing_pos (w, charpos, start, end, dy)
11649 struct window *w;
11650 int charpos;
11651 struct glyph_row *start, *end;
11652 int dy;
11653 {
11654 struct glyph_row *row = start;
11655 int last_y;
11656
11657 /* If we happen to start on a header-line, skip that. */
11658 if (row->mode_line_p)
11659 ++row;
11660
11661 if ((end && row >= end) || !row->enabled_p)
11662 return NULL;
11663
11664 last_y = window_text_bottom_y (w) - dy;
11665
11666 while (1)
11667 {
11668 /* Give up if we have gone too far. */
11669 if (end && row >= end)
11670 return NULL;
11671 /* This formerly returned if they were equal.
11672 I think that both quantities are of a "last plus one" type;
11673 if so, when they are equal, the row is within the screen. -- rms. */
11674 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
11675 return NULL;
11676
11677 /* If it is in this row, return this row. */
11678 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
11679 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11680 /* The end position of a row equals the start
11681 position of the next row. If CHARPOS is there, we
11682 would rather display it in the next line, except
11683 when this line ends in ZV. */
11684 && !row->ends_at_zv_p
11685 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11686 && charpos >= MATRIX_ROW_START_CHARPOS (row))
11687 return row;
11688 ++row;
11689 }
11690 }
11691
11692
11693 /* Try to redisplay window W by reusing its existing display. W's
11694 current matrix must be up to date when this function is called,
11695 i.e. window_end_valid must not be nil.
11696
11697 Value is
11698
11699 1 if display has been updated
11700 0 if otherwise unsuccessful
11701 -1 if redisplay with same window start is known not to succeed
11702
11703 The following steps are performed:
11704
11705 1. Find the last row in the current matrix of W that is not
11706 affected by changes at the start of current_buffer. If no such row
11707 is found, give up.
11708
11709 2. Find the first row in W's current matrix that is not affected by
11710 changes at the end of current_buffer. Maybe there is no such row.
11711
11712 3. Display lines beginning with the row + 1 found in step 1 to the
11713 row found in step 2 or, if step 2 didn't find a row, to the end of
11714 the window.
11715
11716 4. If cursor is not known to appear on the window, give up.
11717
11718 5. If display stopped at the row found in step 2, scroll the
11719 display and current matrix as needed.
11720
11721 6. Maybe display some lines at the end of W, if we must. This can
11722 happen under various circumstances, like a partially visible line
11723 becoming fully visible, or because newly displayed lines are displayed
11724 in smaller font sizes.
11725
11726 7. Update W's window end information. */
11727
11728 static int
11729 try_window_id (w)
11730 struct window *w;
11731 {
11732 struct frame *f = XFRAME (w->frame);
11733 struct glyph_matrix *current_matrix = w->current_matrix;
11734 struct glyph_matrix *desired_matrix = w->desired_matrix;
11735 struct glyph_row *last_unchanged_at_beg_row;
11736 struct glyph_row *first_unchanged_at_end_row;
11737 struct glyph_row *row;
11738 struct glyph_row *bottom_row;
11739 int bottom_vpos;
11740 struct it it;
11741 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11742 struct text_pos start_pos;
11743 struct run run;
11744 int first_unchanged_at_end_vpos = 0;
11745 struct glyph_row *last_text_row, *last_text_row_at_end;
11746 struct text_pos start;
11747 int first_changed_charpos, last_changed_charpos;
11748
11749 #if GLYPH_DEBUG
11750 if (inhibit_try_window_id)
11751 return 0;
11752 #endif
11753
11754 /* This is handy for debugging. */
11755 #if 0
11756 #define GIVE_UP(X) \
11757 do { \
11758 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11759 return 0; \
11760 } while (0)
11761 #else
11762 #define GIVE_UP(X) return 0
11763 #endif
11764
11765 SET_TEXT_POS_FROM_MARKER (start, w->start);
11766
11767 /* Don't use this for mini-windows because these can show
11768 messages and mini-buffers, and we don't handle that here. */
11769 if (MINI_WINDOW_P (w))
11770 GIVE_UP (1);
11771
11772 /* This flag is used to prevent redisplay optimizations. */
11773 if (windows_or_buffers_changed || cursor_type_changed)
11774 GIVE_UP (2);
11775
11776 /* Verify that narrowing has not changed.
11777 Also verify that we were not told to prevent redisplay optimizations.
11778 It would be nice to further
11779 reduce the number of cases where this prevents try_window_id. */
11780 if (current_buffer->clip_changed
11781 || current_buffer->prevent_redisplay_optimizations_p)
11782 GIVE_UP (3);
11783
11784 /* Window must either use window-based redisplay or be full width. */
11785 if (!FRAME_WINDOW_P (f)
11786 && (!line_ins_del_ok
11787 || !WINDOW_FULL_WIDTH_P (w)))
11788 GIVE_UP (4);
11789
11790 /* Give up if point is not known NOT to appear in W. */
11791 if (PT < CHARPOS (start))
11792 GIVE_UP (5);
11793
11794 /* Another way to prevent redisplay optimizations. */
11795 if (XFASTINT (w->last_modified) == 0)
11796 GIVE_UP (6);
11797
11798 /* Verify that window is not hscrolled. */
11799 if (XFASTINT (w->hscroll) != 0)
11800 GIVE_UP (7);
11801
11802 /* Verify that display wasn't paused. */
11803 if (NILP (w->window_end_valid))
11804 GIVE_UP (8);
11805
11806 /* Can't use this if highlighting a region because a cursor movement
11807 will do more than just set the cursor. */
11808 if (!NILP (Vtransient_mark_mode)
11809 && !NILP (current_buffer->mark_active))
11810 GIVE_UP (9);
11811
11812 /* Likewise if highlighting trailing whitespace. */
11813 if (!NILP (Vshow_trailing_whitespace))
11814 GIVE_UP (11);
11815
11816 /* Likewise if showing a region. */
11817 if (!NILP (w->region_showing))
11818 GIVE_UP (10);
11819
11820 /* Can use this if overlay arrow position and or string have changed. */
11821 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11822 || !EQ (last_arrow_string, Voverlay_arrow_string))
11823 GIVE_UP (12);
11824
11825
11826 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11827 only if buffer has really changed. The reason is that the gap is
11828 initially at Z for freshly visited files. The code below would
11829 set end_unchanged to 0 in that case. */
11830 if (MODIFF > SAVE_MODIFF
11831 /* This seems to happen sometimes after saving a buffer. */
11832 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11833 {
11834 if (GPT - BEG < BEG_UNCHANGED)
11835 BEG_UNCHANGED = GPT - BEG;
11836 if (Z - GPT < END_UNCHANGED)
11837 END_UNCHANGED = Z - GPT;
11838 }
11839
11840 /* The position of the first and last character that has been changed. */
11841 first_changed_charpos = BEG + BEG_UNCHANGED;
11842 last_changed_charpos = Z - END_UNCHANGED;
11843
11844 /* If window starts after a line end, and the last change is in
11845 front of that newline, then changes don't affect the display.
11846 This case happens with stealth-fontification. Note that although
11847 the display is unchanged, glyph positions in the matrix have to
11848 be adjusted, of course. */
11849 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11850 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11851 && ((last_changed_charpos < CHARPOS (start)
11852 && CHARPOS (start) == BEGV)
11853 || (last_changed_charpos < CHARPOS (start) - 1
11854 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11855 {
11856 int Z_old, delta, Z_BYTE_old, delta_bytes;
11857 struct glyph_row *r0;
11858
11859 /* Compute how many chars/bytes have been added to or removed
11860 from the buffer. */
11861 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11862 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11863 delta = Z - Z_old;
11864 delta_bytes = Z_BYTE - Z_BYTE_old;
11865
11866 /* Give up if PT is not in the window. Note that it already has
11867 been checked at the start of try_window_id that PT is not in
11868 front of the window start. */
11869 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11870 GIVE_UP (13);
11871
11872 /* If window start is unchanged, we can reuse the whole matrix
11873 as is, after adjusting glyph positions. No need to compute
11874 the window end again, since its offset from Z hasn't changed. */
11875 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11876 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11877 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11878 {
11879 /* Adjust positions in the glyph matrix. */
11880 if (delta || delta_bytes)
11881 {
11882 struct glyph_row *r1
11883 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11884 increment_matrix_positions (w->current_matrix,
11885 MATRIX_ROW_VPOS (r0, current_matrix),
11886 MATRIX_ROW_VPOS (r1, current_matrix),
11887 delta, delta_bytes);
11888 }
11889
11890 /* Set the cursor. */
11891 row = row_containing_pos (w, PT, r0, NULL, 0);
11892 if (row)
11893 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11894 else
11895 abort ();
11896 return 1;
11897 }
11898 }
11899
11900 /* Handle the case that changes are all below what is displayed in
11901 the window, and that PT is in the window. This shortcut cannot
11902 be taken if ZV is visible in the window, and text has been added
11903 there that is visible in the window. */
11904 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11905 /* ZV is not visible in the window, or there are no
11906 changes at ZV, actually. */
11907 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11908 || first_changed_charpos == last_changed_charpos))
11909 {
11910 struct glyph_row *r0;
11911
11912 /* Give up if PT is not in the window. Note that it already has
11913 been checked at the start of try_window_id that PT is not in
11914 front of the window start. */
11915 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11916 GIVE_UP (14);
11917
11918 /* If window start is unchanged, we can reuse the whole matrix
11919 as is, without changing glyph positions since no text has
11920 been added/removed in front of the window end. */
11921 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11922 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11923 {
11924 /* We have to compute the window end anew since text
11925 can have been added/removed after it. */
11926 w->window_end_pos
11927 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11928 w->window_end_bytepos
11929 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11930
11931 /* Set the cursor. */
11932 row = row_containing_pos (w, PT, r0, NULL, 0);
11933 if (row)
11934 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11935 else
11936 abort ();
11937 return 2;
11938 }
11939 }
11940
11941 /* Give up if window start is in the changed area.
11942
11943 The condition used to read
11944
11945 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11946
11947 but why that was tested escapes me at the moment. */
11948 if (CHARPOS (start) >= first_changed_charpos
11949 && CHARPOS (start) <= last_changed_charpos)
11950 GIVE_UP (15);
11951
11952 /* Check that window start agrees with the start of the first glyph
11953 row in its current matrix. Check this after we know the window
11954 start is not in changed text, otherwise positions would not be
11955 comparable. */
11956 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11957 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11958 GIVE_UP (16);
11959
11960 /* Give up if the window ends in strings. Overlay strings
11961 at the end are difficult to handle, so don't try. */
11962 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11963 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11964 GIVE_UP (20);
11965
11966 /* Compute the position at which we have to start displaying new
11967 lines. Some of the lines at the top of the window might be
11968 reusable because they are not displaying changed text. Find the
11969 last row in W's current matrix not affected by changes at the
11970 start of current_buffer. Value is null if changes start in the
11971 first line of window. */
11972 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11973 if (last_unchanged_at_beg_row)
11974 {
11975 /* Avoid starting to display in the moddle of a character, a TAB
11976 for instance. This is easier than to set up the iterator
11977 exactly, and it's not a frequent case, so the additional
11978 effort wouldn't really pay off. */
11979 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11980 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
11981 && last_unchanged_at_beg_row > w->current_matrix->rows)
11982 --last_unchanged_at_beg_row;
11983
11984 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11985 GIVE_UP (17);
11986
11987 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11988 GIVE_UP (18);
11989 start_pos = it.current.pos;
11990
11991 /* Start displaying new lines in the desired matrix at the same
11992 vpos we would use in the current matrix, i.e. below
11993 last_unchanged_at_beg_row. */
11994 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11995 current_matrix);
11996 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11997 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11998
11999 xassert (it.hpos == 0 && it.current_x == 0);
12000 }
12001 else
12002 {
12003 /* There are no reusable lines at the start of the window.
12004 Start displaying in the first line. */
12005 start_display (&it, w, start);
12006 start_pos = it.current.pos;
12007 }
12008
12009 /* Find the first row that is not affected by changes at the end of
12010 the buffer. Value will be null if there is no unchanged row, in
12011 which case we must redisplay to the end of the window. delta
12012 will be set to the value by which buffer positions beginning with
12013 first_unchanged_at_end_row have to be adjusted due to text
12014 changes. */
12015 first_unchanged_at_end_row
12016 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
12017 IF_DEBUG (debug_delta = delta);
12018 IF_DEBUG (debug_delta_bytes = delta_bytes);
12019
12020 /* Set stop_pos to the buffer position up to which we will have to
12021 display new lines. If first_unchanged_at_end_row != NULL, this
12022 is the buffer position of the start of the line displayed in that
12023 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
12024 that we don't stop at a buffer position. */
12025 stop_pos = 0;
12026 if (first_unchanged_at_end_row)
12027 {
12028 xassert (last_unchanged_at_beg_row == NULL
12029 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
12030
12031 /* If this is a continuation line, move forward to the next one
12032 that isn't. Changes in lines above affect this line.
12033 Caution: this may move first_unchanged_at_end_row to a row
12034 not displaying text. */
12035 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
12036 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
12037 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
12038 < it.last_visible_y))
12039 ++first_unchanged_at_end_row;
12040
12041 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
12042 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
12043 >= it.last_visible_y))
12044 first_unchanged_at_end_row = NULL;
12045 else
12046 {
12047 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
12048 + delta);
12049 first_unchanged_at_end_vpos
12050 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
12051 xassert (stop_pos >= Z - END_UNCHANGED);
12052 }
12053 }
12054 else if (last_unchanged_at_beg_row == NULL)
12055 GIVE_UP (19);
12056
12057
12058 #if GLYPH_DEBUG
12059
12060 /* Either there is no unchanged row at the end, or the one we have
12061 now displays text. This is a necessary condition for the window
12062 end pos calculation at the end of this function. */
12063 xassert (first_unchanged_at_end_row == NULL
12064 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
12065
12066 debug_last_unchanged_at_beg_vpos
12067 = (last_unchanged_at_beg_row
12068 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
12069 : -1);
12070 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
12071
12072 #endif /* GLYPH_DEBUG != 0 */
12073
12074
12075 /* Display new lines. Set last_text_row to the last new line
12076 displayed which has text on it, i.e. might end up as being the
12077 line where the window_end_vpos is. */
12078 w->cursor.vpos = -1;
12079 last_text_row = NULL;
12080 overlay_arrow_seen = 0;
12081 while (it.current_y < it.last_visible_y
12082 && !fonts_changed_p
12083 && (first_unchanged_at_end_row == NULL
12084 || IT_CHARPOS (it) < stop_pos))
12085 {
12086 if (display_line (&it))
12087 last_text_row = it.glyph_row - 1;
12088 }
12089
12090 if (fonts_changed_p)
12091 return -1;
12092
12093
12094 /* Compute differences in buffer positions, y-positions etc. for
12095 lines reused at the bottom of the window. Compute what we can
12096 scroll. */
12097 if (first_unchanged_at_end_row
12098 /* No lines reused because we displayed everything up to the
12099 bottom of the window. */
12100 && it.current_y < it.last_visible_y)
12101 {
12102 dvpos = (it.vpos
12103 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
12104 current_matrix));
12105 dy = it.current_y - first_unchanged_at_end_row->y;
12106 run.current_y = first_unchanged_at_end_row->y;
12107 run.desired_y = run.current_y + dy;
12108 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
12109 }
12110 else
12111 {
12112 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
12113 first_unchanged_at_end_row = NULL;
12114 }
12115 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
12116
12117
12118 /* Find the cursor if not already found. We have to decide whether
12119 PT will appear on this window (it sometimes doesn't, but this is
12120 not a very frequent case.) This decision has to be made before
12121 the current matrix is altered. A value of cursor.vpos < 0 means
12122 that PT is either in one of the lines beginning at
12123 first_unchanged_at_end_row or below the window. Don't care for
12124 lines that might be displayed later at the window end; as
12125 mentioned, this is not a frequent case. */
12126 if (w->cursor.vpos < 0)
12127 {
12128 /* Cursor in unchanged rows at the top? */
12129 if (PT < CHARPOS (start_pos)
12130 && last_unchanged_at_beg_row)
12131 {
12132 row = row_containing_pos (w, PT,
12133 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
12134 last_unchanged_at_beg_row + 1, 0);
12135 if (row)
12136 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12137 }
12138
12139 /* Start from first_unchanged_at_end_row looking for PT. */
12140 else if (first_unchanged_at_end_row)
12141 {
12142 row = row_containing_pos (w, PT - delta,
12143 first_unchanged_at_end_row, NULL, 0);
12144 if (row)
12145 set_cursor_from_row (w, row, w->current_matrix, delta,
12146 delta_bytes, dy, dvpos);
12147 }
12148
12149 /* Give up if cursor was not found. */
12150 if (w->cursor.vpos < 0)
12151 {
12152 clear_glyph_matrix (w->desired_matrix);
12153 return -1;
12154 }
12155 }
12156
12157 /* Don't let the cursor end in the scroll margins. */
12158 {
12159 int this_scroll_margin, cursor_height;
12160
12161 this_scroll_margin = max (0, scroll_margin);
12162 this_scroll_margin = min (this_scroll_margin,
12163 XFASTINT (w->height) / 4);
12164 this_scroll_margin *= CANON_Y_UNIT (it.f);
12165 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
12166
12167 if ((w->cursor.y < this_scroll_margin
12168 && CHARPOS (start) > BEGV)
12169 /* Don't take scroll margin into account at the bottom because
12170 old redisplay didn't do it either. */
12171 || w->cursor.y + cursor_height > it.last_visible_y)
12172 {
12173 w->cursor.vpos = -1;
12174 clear_glyph_matrix (w->desired_matrix);
12175 return -1;
12176 }
12177 }
12178
12179 /* Scroll the display. Do it before changing the current matrix so
12180 that xterm.c doesn't get confused about where the cursor glyph is
12181 found. */
12182 if (dy && run.height)
12183 {
12184 update_begin (f);
12185
12186 if (FRAME_WINDOW_P (f))
12187 {
12188 rif->update_window_begin_hook (w);
12189 rif->clear_mouse_face (w);
12190 rif->scroll_run_hook (w, &run);
12191 rif->update_window_end_hook (w, 0, 0);
12192 }
12193 else
12194 {
12195 /* Terminal frame. In this case, dvpos gives the number of
12196 lines to scroll by; dvpos < 0 means scroll up. */
12197 int first_unchanged_at_end_vpos
12198 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
12199 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
12200 int end = (XFASTINT (w->top)
12201 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
12202 + window_internal_height (w));
12203
12204 /* Perform the operation on the screen. */
12205 if (dvpos > 0)
12206 {
12207 /* Scroll last_unchanged_at_beg_row to the end of the
12208 window down dvpos lines. */
12209 set_terminal_window (end);
12210
12211 /* On dumb terminals delete dvpos lines at the end
12212 before inserting dvpos empty lines. */
12213 if (!scroll_region_ok)
12214 ins_del_lines (end - dvpos, -dvpos);
12215
12216 /* Insert dvpos empty lines in front of
12217 last_unchanged_at_beg_row. */
12218 ins_del_lines (from, dvpos);
12219 }
12220 else if (dvpos < 0)
12221 {
12222 /* Scroll up last_unchanged_at_beg_vpos to the end of
12223 the window to last_unchanged_at_beg_vpos - |dvpos|. */
12224 set_terminal_window (end);
12225
12226 /* Delete dvpos lines in front of
12227 last_unchanged_at_beg_vpos. ins_del_lines will set
12228 the cursor to the given vpos and emit |dvpos| delete
12229 line sequences. */
12230 ins_del_lines (from + dvpos, dvpos);
12231
12232 /* On a dumb terminal insert dvpos empty lines at the
12233 end. */
12234 if (!scroll_region_ok)
12235 ins_del_lines (end + dvpos, -dvpos);
12236 }
12237
12238 set_terminal_window (0);
12239 }
12240
12241 update_end (f);
12242 }
12243
12244 /* Shift reused rows of the current matrix to the right position.
12245 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
12246 text. */
12247 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12248 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
12249 if (dvpos < 0)
12250 {
12251 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
12252 bottom_vpos, dvpos);
12253 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
12254 bottom_vpos, 0);
12255 }
12256 else if (dvpos > 0)
12257 {
12258 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
12259 bottom_vpos, dvpos);
12260 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
12261 first_unchanged_at_end_vpos + dvpos, 0);
12262 }
12263
12264 /* For frame-based redisplay, make sure that current frame and window
12265 matrix are in sync with respect to glyph memory. */
12266 if (!FRAME_WINDOW_P (f))
12267 sync_frame_with_window_matrix_rows (w);
12268
12269 /* Adjust buffer positions in reused rows. */
12270 if (delta)
12271 increment_matrix_positions (current_matrix,
12272 first_unchanged_at_end_vpos + dvpos,
12273 bottom_vpos, delta, delta_bytes);
12274
12275 /* Adjust Y positions. */
12276 if (dy)
12277 shift_glyph_matrix (w, current_matrix,
12278 first_unchanged_at_end_vpos + dvpos,
12279 bottom_vpos, dy);
12280
12281 if (first_unchanged_at_end_row)
12282 first_unchanged_at_end_row += dvpos;
12283
12284 /* If scrolling up, there may be some lines to display at the end of
12285 the window. */
12286 last_text_row_at_end = NULL;
12287 if (dy < 0)
12288 {
12289 /* Scrolling up can leave for example a partially visible line
12290 at the end of the window to be redisplayed. */
12291 /* Set last_row to the glyph row in the current matrix where the
12292 window end line is found. It has been moved up or down in
12293 the matrix by dvpos. */
12294 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
12295 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
12296
12297 /* If last_row is the window end line, it should display text. */
12298 xassert (last_row->displays_text_p);
12299
12300 /* If window end line was partially visible before, begin
12301 displaying at that line. Otherwise begin displaying with the
12302 line following it. */
12303 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
12304 {
12305 init_to_row_start (&it, w, last_row);
12306 it.vpos = last_vpos;
12307 it.current_y = last_row->y;
12308 }
12309 else
12310 {
12311 init_to_row_end (&it, w, last_row);
12312 it.vpos = 1 + last_vpos;
12313 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
12314 ++last_row;
12315 }
12316
12317 /* We may start in a continuation line. If so, we have to
12318 get the right continuation_lines_width and current_x. */
12319 it.continuation_lines_width = last_row->continuation_lines_width;
12320 it.hpos = it.current_x = 0;
12321
12322 /* Display the rest of the lines at the window end. */
12323 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
12324 while (it.current_y < it.last_visible_y
12325 && !fonts_changed_p)
12326 {
12327 /* Is it always sure that the display agrees with lines in
12328 the current matrix? I don't think so, so we mark rows
12329 displayed invalid in the current matrix by setting their
12330 enabled_p flag to zero. */
12331 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
12332 if (display_line (&it))
12333 last_text_row_at_end = it.glyph_row - 1;
12334 }
12335 }
12336
12337 /* Update window_end_pos and window_end_vpos. */
12338 if (first_unchanged_at_end_row
12339 && first_unchanged_at_end_row->y < it.last_visible_y
12340 && !last_text_row_at_end)
12341 {
12342 /* Window end line if one of the preserved rows from the current
12343 matrix. Set row to the last row displaying text in current
12344 matrix starting at first_unchanged_at_end_row, after
12345 scrolling. */
12346 xassert (first_unchanged_at_end_row->displays_text_p);
12347 row = find_last_row_displaying_text (w->current_matrix, &it,
12348 first_unchanged_at_end_row);
12349 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
12350
12351 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12352 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12353 w->window_end_vpos
12354 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
12355 xassert (w->window_end_bytepos >= 0);
12356 IF_DEBUG (debug_method_add (w, "A"));
12357 }
12358 else if (last_text_row_at_end)
12359 {
12360 w->window_end_pos
12361 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
12362 w->window_end_bytepos
12363 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
12364 w->window_end_vpos
12365 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
12366 xassert (w->window_end_bytepos >= 0);
12367 IF_DEBUG (debug_method_add (w, "B"));
12368 }
12369 else if (last_text_row)
12370 {
12371 /* We have displayed either to the end of the window or at the
12372 end of the window, i.e. the last row with text is to be found
12373 in the desired matrix. */
12374 w->window_end_pos
12375 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12376 w->window_end_bytepos
12377 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12378 w->window_end_vpos
12379 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
12380 xassert (w->window_end_bytepos >= 0);
12381 }
12382 else if (first_unchanged_at_end_row == NULL
12383 && last_text_row == NULL
12384 && last_text_row_at_end == NULL)
12385 {
12386 /* Displayed to end of window, but no line containing text was
12387 displayed. Lines were deleted at the end of the window. */
12388 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12389 int vpos = XFASTINT (w->window_end_vpos);
12390 struct glyph_row *current_row = current_matrix->rows + vpos;
12391 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12392
12393 for (row = NULL;
12394 row == NULL && vpos >= first_vpos;
12395 --vpos, --current_row, --desired_row)
12396 {
12397 if (desired_row->enabled_p)
12398 {
12399 if (desired_row->displays_text_p)
12400 row = desired_row;
12401 }
12402 else if (current_row->displays_text_p)
12403 row = current_row;
12404 }
12405
12406 xassert (row != NULL);
12407 w->window_end_vpos = make_number (vpos + 1);
12408 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12409 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12410 xassert (w->window_end_bytepos >= 0);
12411 IF_DEBUG (debug_method_add (w, "C"));
12412 }
12413 else
12414 abort ();
12415
12416 #if 0 /* This leads to problems, for instance when the cursor is
12417 at ZV, and the cursor line displays no text. */
12418 /* Disable rows below what's displayed in the window. This makes
12419 debugging easier. */
12420 enable_glyph_matrix_rows (current_matrix,
12421 XFASTINT (w->window_end_vpos) + 1,
12422 bottom_vpos, 0);
12423 #endif
12424
12425 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12426 debug_end_vpos = XFASTINT (w->window_end_vpos));
12427
12428 /* Record that display has not been completed. */
12429 w->window_end_valid = Qnil;
12430 w->desired_matrix->no_scrolling_p = 1;
12431 return 3;
12432
12433 #undef GIVE_UP
12434 }
12435
12436
12437 \f
12438 /***********************************************************************
12439 More debugging support
12440 ***********************************************************************/
12441
12442 #if GLYPH_DEBUG
12443
12444 void dump_glyph_row P_ ((struct glyph_row *, int, int));
12445 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
12446 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
12447
12448
12449 /* Dump the contents of glyph matrix MATRIX on stderr.
12450
12451 GLYPHS 0 means don't show glyph contents.
12452 GLYPHS 1 means show glyphs in short form
12453 GLYPHS > 1 means show glyphs in long form. */
12454
12455 void
12456 dump_glyph_matrix (matrix, glyphs)
12457 struct glyph_matrix *matrix;
12458 int glyphs;
12459 {
12460 int i;
12461 for (i = 0; i < matrix->nrows; ++i)
12462 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
12463 }
12464
12465
12466 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12467 the glyph row and area where the glyph comes from. */
12468
12469 void
12470 dump_glyph (row, glyph, area)
12471 struct glyph_row *row;
12472 struct glyph *glyph;
12473 int area;
12474 {
12475 if (glyph->type == CHAR_GLYPH)
12476 {
12477 fprintf (stderr,
12478 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12479 glyph - row->glyphs[TEXT_AREA],
12480 'C',
12481 glyph->charpos,
12482 (BUFFERP (glyph->object)
12483 ? 'B'
12484 : (STRINGP (glyph->object)
12485 ? 'S'
12486 : '-')),
12487 glyph->pixel_width,
12488 glyph->u.ch,
12489 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12490 ? glyph->u.ch
12491 : '.'),
12492 glyph->face_id,
12493 glyph->left_box_line_p,
12494 glyph->right_box_line_p);
12495 }
12496 else if (glyph->type == STRETCH_GLYPH)
12497 {
12498 fprintf (stderr,
12499 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12500 glyph - row->glyphs[TEXT_AREA],
12501 'S',
12502 glyph->charpos,
12503 (BUFFERP (glyph->object)
12504 ? 'B'
12505 : (STRINGP (glyph->object)
12506 ? 'S'
12507 : '-')),
12508 glyph->pixel_width,
12509 0,
12510 '.',
12511 glyph->face_id,
12512 glyph->left_box_line_p,
12513 glyph->right_box_line_p);
12514 }
12515 else if (glyph->type == IMAGE_GLYPH)
12516 {
12517 fprintf (stderr,
12518 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12519 glyph - row->glyphs[TEXT_AREA],
12520 'I',
12521 glyph->charpos,
12522 (BUFFERP (glyph->object)
12523 ? 'B'
12524 : (STRINGP (glyph->object)
12525 ? 'S'
12526 : '-')),
12527 glyph->pixel_width,
12528 glyph->u.img_id,
12529 '.',
12530 glyph->face_id,
12531 glyph->left_box_line_p,
12532 glyph->right_box_line_p);
12533 }
12534 }
12535
12536
12537 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12538 GLYPHS 0 means don't show glyph contents.
12539 GLYPHS 1 means show glyphs in short form
12540 GLYPHS > 1 means show glyphs in long form. */
12541
12542 void
12543 dump_glyph_row (row, vpos, glyphs)
12544 struct glyph_row *row;
12545 int vpos, glyphs;
12546 {
12547 if (glyphs != 1)
12548 {
12549 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12550 fprintf (stderr, "=======================================================================\n");
12551
12552 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
12553 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12554 vpos,
12555 MATRIX_ROW_START_CHARPOS (row),
12556 MATRIX_ROW_END_CHARPOS (row),
12557 row->used[TEXT_AREA],
12558 row->contains_overlapping_glyphs_p,
12559 row->enabled_p,
12560 row->truncated_on_left_p,
12561 row->truncated_on_right_p,
12562 row->overlay_arrow_p,
12563 row->continued_p,
12564 MATRIX_ROW_CONTINUATION_LINE_P (row),
12565 row->displays_text_p,
12566 row->ends_at_zv_p,
12567 row->fill_line_p,
12568 row->ends_in_middle_of_char_p,
12569 row->starts_in_middle_of_char_p,
12570 row->mouse_face_p,
12571 row->x,
12572 row->y,
12573 row->pixel_width,
12574 row->height,
12575 row->visible_height,
12576 row->ascent,
12577 row->phys_ascent);
12578 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12579 row->end.overlay_string_index,
12580 row->continuation_lines_width);
12581 fprintf (stderr, "%9d %5d\n",
12582 CHARPOS (row->start.string_pos),
12583 CHARPOS (row->end.string_pos));
12584 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12585 row->end.dpvec_index);
12586 }
12587
12588 if (glyphs > 1)
12589 {
12590 int area;
12591
12592 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12593 {
12594 struct glyph *glyph = row->glyphs[area];
12595 struct glyph *glyph_end = glyph + row->used[area];
12596
12597 /* Glyph for a line end in text. */
12598 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12599 ++glyph_end;
12600
12601 if (glyph < glyph_end)
12602 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12603
12604 for (; glyph < glyph_end; ++glyph)
12605 dump_glyph (row, glyph, area);
12606 }
12607 }
12608 else if (glyphs == 1)
12609 {
12610 int area;
12611
12612 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12613 {
12614 char *s = (char *) alloca (row->used[area] + 1);
12615 int i;
12616
12617 for (i = 0; i < row->used[area]; ++i)
12618 {
12619 struct glyph *glyph = row->glyphs[area] + i;
12620 if (glyph->type == CHAR_GLYPH
12621 && glyph->u.ch < 0x80
12622 && glyph->u.ch >= ' ')
12623 s[i] = glyph->u.ch;
12624 else
12625 s[i] = '.';
12626 }
12627
12628 s[i] = '\0';
12629 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12630 }
12631 }
12632 }
12633
12634
12635 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12636 Sdump_glyph_matrix, 0, 1, "p",
12637 doc: /* Dump the current matrix of the selected window to stderr.
12638 Shows contents of glyph row structures. With non-nil
12639 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12640 glyphs in short form, otherwise show glyphs in long form. */)
12641 (glyphs)
12642 Lisp_Object glyphs;
12643 {
12644 struct window *w = XWINDOW (selected_window);
12645 struct buffer *buffer = XBUFFER (w->buffer);
12646
12647 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12648 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12649 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12650 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12651 fprintf (stderr, "=============================================\n");
12652 dump_glyph_matrix (w->current_matrix,
12653 NILP (glyphs) ? 0 : XINT (glyphs));
12654 return Qnil;
12655 }
12656
12657
12658 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
12659 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
12660 ()
12661 {
12662 struct frame *f = XFRAME (selected_frame);
12663 dump_glyph_matrix (f->current_matrix, 1);
12664 return Qnil;
12665 }
12666
12667
12668 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12669 doc: /* Dump glyph row ROW to stderr.
12670 GLYPH 0 means don't dump glyphs.
12671 GLYPH 1 means dump glyphs in short form.
12672 GLYPH > 1 or omitted means dump glyphs in long form. */)
12673 (row, glyphs)
12674 Lisp_Object row, glyphs;
12675 {
12676 struct glyph_matrix *matrix;
12677 int vpos;
12678
12679 CHECK_NUMBER (row);
12680 matrix = XWINDOW (selected_window)->current_matrix;
12681 vpos = XINT (row);
12682 if (vpos >= 0 && vpos < matrix->nrows)
12683 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12684 vpos,
12685 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12686 return Qnil;
12687 }
12688
12689
12690 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12691 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12692 GLYPH 0 means don't dump glyphs.
12693 GLYPH 1 means dump glyphs in short form.
12694 GLYPH > 1 or omitted means dump glyphs in long form. */)
12695 (row, glyphs)
12696 Lisp_Object row, glyphs;
12697 {
12698 struct frame *sf = SELECTED_FRAME ();
12699 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12700 int vpos;
12701
12702 CHECK_NUMBER (row);
12703 vpos = XINT (row);
12704 if (vpos >= 0 && vpos < m->nrows)
12705 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12706 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12707 return Qnil;
12708 }
12709
12710
12711 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12712 doc: /* Toggle tracing of redisplay.
12713 With ARG, turn tracing on if and only if ARG is positive. */)
12714 (arg)
12715 Lisp_Object arg;
12716 {
12717 if (NILP (arg))
12718 trace_redisplay_p = !trace_redisplay_p;
12719 else
12720 {
12721 arg = Fprefix_numeric_value (arg);
12722 trace_redisplay_p = XINT (arg) > 0;
12723 }
12724
12725 return Qnil;
12726 }
12727
12728
12729 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12730 doc: /* Like `format', but print result to stderr.
12731 usage: (trace-to-stderr STRING &rest OBJECTS) */)
12732 (nargs, args)
12733 int nargs;
12734 Lisp_Object *args;
12735 {
12736 Lisp_Object s = Fformat (nargs, args);
12737 fprintf (stderr, "%s", SDATA (s));
12738 return Qnil;
12739 }
12740
12741 #endif /* GLYPH_DEBUG */
12742
12743
12744 \f
12745 /***********************************************************************
12746 Building Desired Matrix Rows
12747 ***********************************************************************/
12748
12749 /* Return a temporary glyph row holding the glyphs of an overlay
12750 arrow. Only used for non-window-redisplay windows. */
12751
12752 static struct glyph_row *
12753 get_overlay_arrow_glyph_row (w)
12754 struct window *w;
12755 {
12756 struct frame *f = XFRAME (WINDOW_FRAME (w));
12757 struct buffer *buffer = XBUFFER (w->buffer);
12758 struct buffer *old = current_buffer;
12759 const unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
12760 int arrow_len = SCHARS (Voverlay_arrow_string);
12761 const unsigned char *arrow_end = arrow_string + arrow_len;
12762 const unsigned char *p;
12763 struct it it;
12764 int multibyte_p;
12765 int n_glyphs_before;
12766
12767 set_buffer_temp (buffer);
12768 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12769 it.glyph_row->used[TEXT_AREA] = 0;
12770 SET_TEXT_POS (it.position, 0, 0);
12771
12772 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12773 p = arrow_string;
12774 while (p < arrow_end)
12775 {
12776 Lisp_Object face, ilisp;
12777
12778 /* Get the next character. */
12779 if (multibyte_p)
12780 it.c = string_char_and_length (p, arrow_len, &it.len);
12781 else
12782 it.c = *p, it.len = 1;
12783 p += it.len;
12784
12785 /* Get its face. */
12786 ilisp = make_number (p - arrow_string);
12787 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12788 it.face_id = compute_char_face (f, it.c, face);
12789
12790 /* Compute its width, get its glyphs. */
12791 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12792 SET_TEXT_POS (it.position, -1, -1);
12793 PRODUCE_GLYPHS (&it);
12794
12795 /* If this character doesn't fit any more in the line, we have
12796 to remove some glyphs. */
12797 if (it.current_x > it.last_visible_x)
12798 {
12799 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12800 break;
12801 }
12802 }
12803
12804 set_buffer_temp (old);
12805 return it.glyph_row;
12806 }
12807
12808
12809 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12810 glyphs are only inserted for terminal frames since we can't really
12811 win with truncation glyphs when partially visible glyphs are
12812 involved. Which glyphs to insert is determined by
12813 produce_special_glyphs. */
12814
12815 static void
12816 insert_left_trunc_glyphs (it)
12817 struct it *it;
12818 {
12819 struct it truncate_it;
12820 struct glyph *from, *end, *to, *toend;
12821
12822 xassert (!FRAME_WINDOW_P (it->f));
12823
12824 /* Get the truncation glyphs. */
12825 truncate_it = *it;
12826 truncate_it.current_x = 0;
12827 truncate_it.face_id = DEFAULT_FACE_ID;
12828 truncate_it.glyph_row = &scratch_glyph_row;
12829 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12830 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12831 truncate_it.object = make_number (0);
12832 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12833
12834 /* Overwrite glyphs from IT with truncation glyphs. */
12835 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12836 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12837 to = it->glyph_row->glyphs[TEXT_AREA];
12838 toend = to + it->glyph_row->used[TEXT_AREA];
12839
12840 while (from < end)
12841 *to++ = *from++;
12842
12843 /* There may be padding glyphs left over. Overwrite them too. */
12844 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12845 {
12846 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12847 while (from < end)
12848 *to++ = *from++;
12849 }
12850
12851 if (to > toend)
12852 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12853 }
12854
12855
12856 /* Compute the pixel height and width of IT->glyph_row.
12857
12858 Most of the time, ascent and height of a display line will be equal
12859 to the max_ascent and max_height values of the display iterator
12860 structure. This is not the case if
12861
12862 1. We hit ZV without displaying anything. In this case, max_ascent
12863 and max_height will be zero.
12864
12865 2. We have some glyphs that don't contribute to the line height.
12866 (The glyph row flag contributes_to_line_height_p is for future
12867 pixmap extensions).
12868
12869 The first case is easily covered by using default values because in
12870 these cases, the line height does not really matter, except that it
12871 must not be zero. */
12872
12873 static void
12874 compute_line_metrics (it)
12875 struct it *it;
12876 {
12877 struct glyph_row *row = it->glyph_row;
12878 int area, i;
12879
12880 if (FRAME_WINDOW_P (it->f))
12881 {
12882 int i, min_y, max_y;
12883
12884 /* The line may consist of one space only, that was added to
12885 place the cursor on it. If so, the row's height hasn't been
12886 computed yet. */
12887 if (row->height == 0)
12888 {
12889 if (it->max_ascent + it->max_descent == 0)
12890 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12891 row->ascent = it->max_ascent;
12892 row->height = it->max_ascent + it->max_descent;
12893 row->phys_ascent = it->max_phys_ascent;
12894 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12895 }
12896
12897 /* Compute the width of this line. */
12898 row->pixel_width = row->x;
12899 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12900 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12901
12902 xassert (row->pixel_width >= 0);
12903 xassert (row->ascent >= 0 && row->height > 0);
12904
12905 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12906 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12907
12908 /* If first line's physical ascent is larger than its logical
12909 ascent, use the physical ascent, and make the row taller.
12910 This makes accented characters fully visible. */
12911 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12912 && row->phys_ascent > row->ascent)
12913 {
12914 row->height += row->phys_ascent - row->ascent;
12915 row->ascent = row->phys_ascent;
12916 }
12917
12918 /* Compute how much of the line is visible. */
12919 row->visible_height = row->height;
12920
12921 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12922 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12923
12924 if (row->y < min_y)
12925 row->visible_height -= min_y - row->y;
12926 if (row->y + row->height > max_y)
12927 row->visible_height -= row->y + row->height - max_y;
12928 }
12929 else
12930 {
12931 row->pixel_width = row->used[TEXT_AREA];
12932 if (row->continued_p)
12933 row->pixel_width -= it->continuation_pixel_width;
12934 else if (row->truncated_on_right_p)
12935 row->pixel_width -= it->truncation_pixel_width;
12936 row->ascent = row->phys_ascent = 0;
12937 row->height = row->phys_height = row->visible_height = 1;
12938 }
12939
12940 /* Compute a hash code for this row. */
12941 row->hash = 0;
12942 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12943 for (i = 0; i < row->used[area]; ++i)
12944 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12945 + row->glyphs[area][i].u.val
12946 + row->glyphs[area][i].face_id
12947 + row->glyphs[area][i].padding_p
12948 + (row->glyphs[area][i].type << 2));
12949
12950 it->max_ascent = it->max_descent = 0;
12951 it->max_phys_ascent = it->max_phys_descent = 0;
12952 }
12953
12954
12955 /* Append one space to the glyph row of iterator IT if doing a
12956 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12957 space have the default face, otherwise let it have the same face as
12958 IT->face_id. Value is non-zero if a space was added.
12959
12960 This function is called to make sure that there is always one glyph
12961 at the end of a glyph row that the cursor can be set on under
12962 window-systems. (If there weren't such a glyph we would not know
12963 how wide and tall a box cursor should be displayed).
12964
12965 At the same time this space let's a nicely handle clearing to the
12966 end of the line if the row ends in italic text. */
12967
12968 static int
12969 append_space (it, default_face_p)
12970 struct it *it;
12971 int default_face_p;
12972 {
12973 if (FRAME_WINDOW_P (it->f))
12974 {
12975 int n = it->glyph_row->used[TEXT_AREA];
12976
12977 if (it->glyph_row->glyphs[TEXT_AREA] + n
12978 < it->glyph_row->glyphs[1 + TEXT_AREA])
12979 {
12980 /* Save some values that must not be changed.
12981 Must save IT->c and IT->len because otherwise
12982 ITERATOR_AT_END_P wouldn't work anymore after
12983 append_space has been called. */
12984 enum display_element_type saved_what = it->what;
12985 int saved_c = it->c, saved_len = it->len;
12986 int saved_x = it->current_x;
12987 int saved_face_id = it->face_id;
12988 struct text_pos saved_pos;
12989 Lisp_Object saved_object;
12990 struct face *face;
12991
12992 saved_object = it->object;
12993 saved_pos = it->position;
12994
12995 it->what = IT_CHARACTER;
12996 bzero (&it->position, sizeof it->position);
12997 it->object = make_number (0);
12998 it->c = ' ';
12999 it->len = 1;
13000
13001 if (default_face_p)
13002 it->face_id = DEFAULT_FACE_ID;
13003 else if (it->face_before_selective_p)
13004 it->face_id = it->saved_face_id;
13005 face = FACE_FROM_ID (it->f, it->face_id);
13006 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
13007
13008 PRODUCE_GLYPHS (it);
13009
13010 it->current_x = saved_x;
13011 it->object = saved_object;
13012 it->position = saved_pos;
13013 it->what = saved_what;
13014 it->face_id = saved_face_id;
13015 it->len = saved_len;
13016 it->c = saved_c;
13017 return 1;
13018 }
13019 }
13020
13021 return 0;
13022 }
13023
13024
13025 /* Extend the face of the last glyph in the text area of IT->glyph_row
13026 to the end of the display line. Called from display_line.
13027 If the glyph row is empty, add a space glyph to it so that we
13028 know the face to draw. Set the glyph row flag fill_line_p. */
13029
13030 static void
13031 extend_face_to_end_of_line (it)
13032 struct it *it;
13033 {
13034 struct face *face;
13035 struct frame *f = it->f;
13036
13037 /* If line is already filled, do nothing. */
13038 if (it->current_x >= it->last_visible_x)
13039 return;
13040
13041 /* Face extension extends the background and box of IT->face_id
13042 to the end of the line. If the background equals the background
13043 of the frame, we don't have to do anything. */
13044 if (it->face_before_selective_p)
13045 face = FACE_FROM_ID (it->f, it->saved_face_id);
13046 else
13047 face = FACE_FROM_ID (f, it->face_id);
13048
13049 if (FRAME_WINDOW_P (f)
13050 && face->box == FACE_NO_BOX
13051 && face->background == FRAME_BACKGROUND_PIXEL (f)
13052 && !face->stipple)
13053 return;
13054
13055 /* Set the glyph row flag indicating that the face of the last glyph
13056 in the text area has to be drawn to the end of the text area. */
13057 it->glyph_row->fill_line_p = 1;
13058
13059 /* If current character of IT is not ASCII, make sure we have the
13060 ASCII face. This will be automatically undone the next time
13061 get_next_display_element returns a multibyte character. Note
13062 that the character will always be single byte in unibyte text. */
13063 if (!SINGLE_BYTE_CHAR_P (it->c))
13064 {
13065 it->face_id = FACE_FOR_CHAR (f, face, 0);
13066 }
13067
13068 if (FRAME_WINDOW_P (f))
13069 {
13070 /* If the row is empty, add a space with the current face of IT,
13071 so that we know which face to draw. */
13072 if (it->glyph_row->used[TEXT_AREA] == 0)
13073 {
13074 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
13075 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
13076 it->glyph_row->used[TEXT_AREA] = 1;
13077 }
13078 }
13079 else
13080 {
13081 /* Save some values that must not be changed. */
13082 int saved_x = it->current_x;
13083 struct text_pos saved_pos;
13084 Lisp_Object saved_object;
13085 enum display_element_type saved_what = it->what;
13086 int saved_face_id = it->face_id;
13087
13088 saved_object = it->object;
13089 saved_pos = it->position;
13090
13091 it->what = IT_CHARACTER;
13092 bzero (&it->position, sizeof it->position);
13093 it->object = make_number (0);
13094 it->c = ' ';
13095 it->len = 1;
13096 it->face_id = face->id;
13097
13098 PRODUCE_GLYPHS (it);
13099
13100 while (it->current_x <= it->last_visible_x)
13101 PRODUCE_GLYPHS (it);
13102
13103 /* Don't count these blanks really. It would let us insert a left
13104 truncation glyph below and make us set the cursor on them, maybe. */
13105 it->current_x = saved_x;
13106 it->object = saved_object;
13107 it->position = saved_pos;
13108 it->what = saved_what;
13109 it->face_id = saved_face_id;
13110 }
13111 }
13112
13113
13114 /* Value is non-zero if text starting at CHARPOS in current_buffer is
13115 trailing whitespace. */
13116
13117 static int
13118 trailing_whitespace_p (charpos)
13119 int charpos;
13120 {
13121 int bytepos = CHAR_TO_BYTE (charpos);
13122 int c = 0;
13123
13124 while (bytepos < ZV_BYTE
13125 && (c = FETCH_CHAR (bytepos),
13126 c == ' ' || c == '\t'))
13127 ++bytepos;
13128
13129 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
13130 {
13131 if (bytepos != PT_BYTE)
13132 return 1;
13133 }
13134 return 0;
13135 }
13136
13137
13138 /* Highlight trailing whitespace, if any, in ROW. */
13139
13140 void
13141 highlight_trailing_whitespace (f, row)
13142 struct frame *f;
13143 struct glyph_row *row;
13144 {
13145 int used = row->used[TEXT_AREA];
13146
13147 if (used)
13148 {
13149 struct glyph *start = row->glyphs[TEXT_AREA];
13150 struct glyph *glyph = start + used - 1;
13151
13152 /* Skip over glyphs inserted to display the cursor at the
13153 end of a line, for extending the face of the last glyph
13154 to the end of the line on terminals, and for truncation
13155 and continuation glyphs. */
13156 while (glyph >= start
13157 && glyph->type == CHAR_GLYPH
13158 && INTEGERP (glyph->object))
13159 --glyph;
13160
13161 /* If last glyph is a space or stretch, and it's trailing
13162 whitespace, set the face of all trailing whitespace glyphs in
13163 IT->glyph_row to `trailing-whitespace'. */
13164 if (glyph >= start
13165 && BUFFERP (glyph->object)
13166 && (glyph->type == STRETCH_GLYPH
13167 || (glyph->type == CHAR_GLYPH
13168 && glyph->u.ch == ' '))
13169 && trailing_whitespace_p (glyph->charpos))
13170 {
13171 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
13172
13173 while (glyph >= start
13174 && BUFFERP (glyph->object)
13175 && (glyph->type == STRETCH_GLYPH
13176 || (glyph->type == CHAR_GLYPH
13177 && glyph->u.ch == ' ')))
13178 (glyph--)->face_id = face_id;
13179 }
13180 }
13181 }
13182
13183
13184 /* Value is non-zero if glyph row ROW in window W should be
13185 used to hold the cursor. */
13186
13187 static int
13188 cursor_row_p (w, row)
13189 struct window *w;
13190 struct glyph_row *row;
13191 {
13192 int cursor_row_p = 1;
13193
13194 if (PT == MATRIX_ROW_END_CHARPOS (row))
13195 {
13196 /* If the row ends with a newline from a string, we don't want
13197 the cursor there (if the row is continued it doesn't end in a
13198 newline). */
13199 if (CHARPOS (row->end.string_pos) >= 0
13200 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13201 cursor_row_p = row->continued_p;
13202
13203 /* If the row ends at ZV, display the cursor at the end of that
13204 row instead of at the start of the row below. */
13205 else if (row->ends_at_zv_p)
13206 cursor_row_p = 1;
13207 else
13208 cursor_row_p = 0;
13209 }
13210
13211 return cursor_row_p;
13212 }
13213
13214
13215 /* Construct the glyph row IT->glyph_row in the desired matrix of
13216 IT->w from text at the current position of IT. See dispextern.h
13217 for an overview of struct it. Value is non-zero if
13218 IT->glyph_row displays text, as opposed to a line displaying ZV
13219 only. */
13220
13221 static int
13222 display_line (it)
13223 struct it *it;
13224 {
13225 struct glyph_row *row = it->glyph_row;
13226
13227 /* We always start displaying at hpos zero even if hscrolled. */
13228 xassert (it->hpos == 0 && it->current_x == 0);
13229
13230 /* We must not display in a row that's not a text row. */
13231 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
13232 < it->w->desired_matrix->nrows);
13233
13234 /* Is IT->w showing the region? */
13235 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
13236
13237 /* Clear the result glyph row and enable it. */
13238 prepare_desired_row (row);
13239
13240 row->y = it->current_y;
13241 row->start = it->current;
13242 row->continuation_lines_width = it->continuation_lines_width;
13243 row->displays_text_p = 1;
13244 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
13245 it->starts_in_middle_of_char_p = 0;
13246
13247 /* Arrange the overlays nicely for our purposes. Usually, we call
13248 display_line on only one line at a time, in which case this
13249 can't really hurt too much, or we call it on lines which appear
13250 one after another in the buffer, in which case all calls to
13251 recenter_overlay_lists but the first will be pretty cheap. */
13252 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
13253
13254 /* Move over display elements that are not visible because we are
13255 hscrolled. This may stop at an x-position < IT->first_visible_x
13256 if the first glyph is partially visible or if we hit a line end. */
13257 if (it->current_x < it->first_visible_x)
13258 move_it_in_display_line_to (it, ZV, it->first_visible_x,
13259 MOVE_TO_POS | MOVE_TO_X);
13260
13261 /* Get the initial row height. This is either the height of the
13262 text hscrolled, if there is any, or zero. */
13263 row->ascent = it->max_ascent;
13264 row->height = it->max_ascent + it->max_descent;
13265 row->phys_ascent = it->max_phys_ascent;
13266 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13267
13268 /* Loop generating characters. The loop is left with IT on the next
13269 character to display. */
13270 while (1)
13271 {
13272 int n_glyphs_before, hpos_before, x_before;
13273 int x, i, nglyphs;
13274 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
13275
13276 /* Retrieve the next thing to display. Value is zero if end of
13277 buffer reached. */
13278 if (!get_next_display_element (it))
13279 {
13280 /* Maybe add a space at the end of this line that is used to
13281 display the cursor there under X. Set the charpos of the
13282 first glyph of blank lines not corresponding to any text
13283 to -1. */
13284 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
13285 || row->used[TEXT_AREA] == 0)
13286 {
13287 row->glyphs[TEXT_AREA]->charpos = -1;
13288 row->displays_text_p = 0;
13289
13290 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
13291 && (!MINI_WINDOW_P (it->w)
13292 || (minibuf_level && EQ (it->window, minibuf_window))))
13293 row->indicate_empty_line_p = 1;
13294 }
13295
13296 it->continuation_lines_width = 0;
13297 row->ends_at_zv_p = 1;
13298 break;
13299 }
13300
13301 /* Now, get the metrics of what we want to display. This also
13302 generates glyphs in `row' (which is IT->glyph_row). */
13303 n_glyphs_before = row->used[TEXT_AREA];
13304 x = it->current_x;
13305
13306 /* Remember the line height so far in case the next element doesn't
13307 fit on the line. */
13308 if (!it->truncate_lines_p)
13309 {
13310 ascent = it->max_ascent;
13311 descent = it->max_descent;
13312 phys_ascent = it->max_phys_ascent;
13313 phys_descent = it->max_phys_descent;
13314 }
13315
13316 PRODUCE_GLYPHS (it);
13317
13318 /* If this display element was in marginal areas, continue with
13319 the next one. */
13320 if (it->area != TEXT_AREA)
13321 {
13322 row->ascent = max (row->ascent, it->max_ascent);
13323 row->height = max (row->height, it->max_ascent + it->max_descent);
13324 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13325 row->phys_height = max (row->phys_height,
13326 it->max_phys_ascent + it->max_phys_descent);
13327 set_iterator_to_next (it, 1);
13328 continue;
13329 }
13330
13331 /* Does the display element fit on the line? If we truncate
13332 lines, we should draw past the right edge of the window. If
13333 we don't truncate, we want to stop so that we can display the
13334 continuation glyph before the right margin. If lines are
13335 continued, there are two possible strategies for characters
13336 resulting in more than 1 glyph (e.g. tabs): Display as many
13337 glyphs as possible in this line and leave the rest for the
13338 continuation line, or display the whole element in the next
13339 line. Original redisplay did the former, so we do it also. */
13340 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
13341 hpos_before = it->hpos;
13342 x_before = x;
13343
13344 if (/* Not a newline. */
13345 nglyphs > 0
13346 /* Glyphs produced fit entirely in the line. */
13347 && it->current_x < it->last_visible_x)
13348 {
13349 it->hpos += nglyphs;
13350 row->ascent = max (row->ascent, it->max_ascent);
13351 row->height = max (row->height, it->max_ascent + it->max_descent);
13352 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13353 row->phys_height = max (row->phys_height,
13354 it->max_phys_ascent + it->max_phys_descent);
13355 if (it->current_x - it->pixel_width < it->first_visible_x)
13356 row->x = x - it->first_visible_x;
13357 }
13358 else
13359 {
13360 int new_x;
13361 struct glyph *glyph;
13362
13363 for (i = 0; i < nglyphs; ++i, x = new_x)
13364 {
13365 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13366 new_x = x + glyph->pixel_width;
13367
13368 if (/* Lines are continued. */
13369 !it->truncate_lines_p
13370 && (/* Glyph doesn't fit on the line. */
13371 new_x > it->last_visible_x
13372 /* Or it fits exactly on a window system frame. */
13373 || (new_x == it->last_visible_x
13374 && FRAME_WINDOW_P (it->f))))
13375 {
13376 /* End of a continued line. */
13377
13378 if (it->hpos == 0
13379 || (new_x == it->last_visible_x
13380 && FRAME_WINDOW_P (it->f)))
13381 {
13382 /* Current glyph is the only one on the line or
13383 fits exactly on the line. We must continue
13384 the line because we can't draw the cursor
13385 after the glyph. */
13386 row->continued_p = 1;
13387 it->current_x = new_x;
13388 it->continuation_lines_width += new_x;
13389 ++it->hpos;
13390 if (i == nglyphs - 1)
13391 set_iterator_to_next (it, 1);
13392 }
13393 else if (CHAR_GLYPH_PADDING_P (*glyph)
13394 && !FRAME_WINDOW_P (it->f))
13395 {
13396 /* A padding glyph that doesn't fit on this line.
13397 This means the whole character doesn't fit
13398 on the line. */
13399 row->used[TEXT_AREA] = n_glyphs_before;
13400
13401 /* Fill the rest of the row with continuation
13402 glyphs like in 20.x. */
13403 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13404 < row->glyphs[1 + TEXT_AREA])
13405 produce_special_glyphs (it, IT_CONTINUATION);
13406
13407 row->continued_p = 1;
13408 it->current_x = x_before;
13409 it->continuation_lines_width += x_before;
13410
13411 /* Restore the height to what it was before the
13412 element not fitting on the line. */
13413 it->max_ascent = ascent;
13414 it->max_descent = descent;
13415 it->max_phys_ascent = phys_ascent;
13416 it->max_phys_descent = phys_descent;
13417 }
13418 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13419 {
13420 /* A TAB that extends past the right edge of the
13421 window. This produces a single glyph on
13422 window system frames. We leave the glyph in
13423 this row and let it fill the row, but don't
13424 consume the TAB. */
13425 it->continuation_lines_width += it->last_visible_x;
13426 row->ends_in_middle_of_char_p = 1;
13427 row->continued_p = 1;
13428 glyph->pixel_width = it->last_visible_x - x;
13429 it->starts_in_middle_of_char_p = 1;
13430 }
13431 else
13432 {
13433 /* Something other than a TAB that draws past
13434 the right edge of the window. Restore
13435 positions to values before the element. */
13436 row->used[TEXT_AREA] = n_glyphs_before + i;
13437
13438 /* Display continuation glyphs. */
13439 if (!FRAME_WINDOW_P (it->f))
13440 produce_special_glyphs (it, IT_CONTINUATION);
13441 row->continued_p = 1;
13442
13443 it->continuation_lines_width += x;
13444
13445 if (nglyphs > 1 && i > 0)
13446 {
13447 row->ends_in_middle_of_char_p = 1;
13448 it->starts_in_middle_of_char_p = 1;
13449 }
13450
13451 /* Restore the height to what it was before the
13452 element not fitting on the line. */
13453 it->max_ascent = ascent;
13454 it->max_descent = descent;
13455 it->max_phys_ascent = phys_ascent;
13456 it->max_phys_descent = phys_descent;
13457 }
13458
13459 break;
13460 }
13461 else if (new_x > it->first_visible_x)
13462 {
13463 /* Increment number of glyphs actually displayed. */
13464 ++it->hpos;
13465
13466 if (x < it->first_visible_x)
13467 /* Glyph is partially visible, i.e. row starts at
13468 negative X position. */
13469 row->x = x - it->first_visible_x;
13470 }
13471 else
13472 {
13473 /* Glyph is completely off the left margin of the
13474 window. This should not happen because of the
13475 move_it_in_display_line at the start of this
13476 function, unless the text display area of the
13477 window is empty. */
13478 xassert (it->first_visible_x <= it->last_visible_x);
13479 }
13480 }
13481
13482 row->ascent = max (row->ascent, it->max_ascent);
13483 row->height = max (row->height, it->max_ascent + it->max_descent);
13484 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13485 row->phys_height = max (row->phys_height,
13486 it->max_phys_ascent + it->max_phys_descent);
13487
13488 /* End of this display line if row is continued. */
13489 if (row->continued_p)
13490 break;
13491 }
13492
13493 /* Is this a line end? If yes, we're also done, after making
13494 sure that a non-default face is extended up to the right
13495 margin of the window. */
13496 if (ITERATOR_AT_END_OF_LINE_P (it))
13497 {
13498 int used_before = row->used[TEXT_AREA];
13499
13500 row->ends_in_newline_from_string_p = STRINGP (it->object);
13501
13502 /* Add a space at the end of the line that is used to
13503 display the cursor there. */
13504 append_space (it, 0);
13505
13506 /* Extend the face to the end of the line. */
13507 extend_face_to_end_of_line (it);
13508
13509 /* Make sure we have the position. */
13510 if (used_before == 0)
13511 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13512
13513 /* Consume the line end. This skips over invisible lines. */
13514 set_iterator_to_next (it, 1);
13515 it->continuation_lines_width = 0;
13516 break;
13517 }
13518
13519 /* Proceed with next display element. Note that this skips
13520 over lines invisible because of selective display. */
13521 set_iterator_to_next (it, 1);
13522
13523 /* If we truncate lines, we are done when the last displayed
13524 glyphs reach past the right margin of the window. */
13525 if (it->truncate_lines_p
13526 && (FRAME_WINDOW_P (it->f)
13527 ? (it->current_x >= it->last_visible_x)
13528 : (it->current_x > it->last_visible_x)))
13529 {
13530 /* Maybe add truncation glyphs. */
13531 if (!FRAME_WINDOW_P (it->f))
13532 {
13533 int i, n;
13534
13535 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13536 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13537 break;
13538
13539 for (n = row->used[TEXT_AREA]; i < n; ++i)
13540 {
13541 row->used[TEXT_AREA] = i;
13542 produce_special_glyphs (it, IT_TRUNCATION);
13543 }
13544 }
13545
13546 row->truncated_on_right_p = 1;
13547 it->continuation_lines_width = 0;
13548 reseat_at_next_visible_line_start (it, 0);
13549 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13550 it->hpos = hpos_before;
13551 it->current_x = x_before;
13552 break;
13553 }
13554 }
13555
13556 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13557 at the left window margin. */
13558 if (it->first_visible_x
13559 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13560 {
13561 if (!FRAME_WINDOW_P (it->f))
13562 insert_left_trunc_glyphs (it);
13563 row->truncated_on_left_p = 1;
13564 }
13565
13566 /* If the start of this line is the overlay arrow-position, then
13567 mark this glyph row as the one containing the overlay arrow.
13568 This is clearly a mess with variable size fonts. It would be
13569 better to let it be displayed like cursors under X. */
13570 if (MARKERP (Voverlay_arrow_position)
13571 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13572 && (MATRIX_ROW_START_CHARPOS (row)
13573 == marker_position (Voverlay_arrow_position))
13574 && STRINGP (Voverlay_arrow_string)
13575 && ! overlay_arrow_seen)
13576 {
13577 /* Overlay arrow in window redisplay is a fringe bitmap. */
13578 if (!FRAME_WINDOW_P (it->f))
13579 {
13580 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13581 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13582 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13583 struct glyph *p = row->glyphs[TEXT_AREA];
13584 struct glyph *p2, *end;
13585
13586 /* Copy the arrow glyphs. */
13587 while (glyph < arrow_end)
13588 *p++ = *glyph++;
13589
13590 /* Throw away padding glyphs. */
13591 p2 = p;
13592 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13593 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13594 ++p2;
13595 if (p2 > p)
13596 {
13597 while (p2 < end)
13598 *p++ = *p2++;
13599 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13600 }
13601 }
13602
13603 overlay_arrow_seen = 1;
13604 row->overlay_arrow_p = 1;
13605 }
13606
13607 /* Compute pixel dimensions of this line. */
13608 compute_line_metrics (it);
13609
13610 /* Remember the position at which this line ends. */
13611 row->end = it->current;
13612
13613 /* Maybe set the cursor. */
13614 if (it->w->cursor.vpos < 0
13615 && PT >= MATRIX_ROW_START_CHARPOS (row)
13616 && PT <= MATRIX_ROW_END_CHARPOS (row)
13617 && cursor_row_p (it->w, row))
13618 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13619
13620 /* Highlight trailing whitespace. */
13621 if (!NILP (Vshow_trailing_whitespace))
13622 highlight_trailing_whitespace (it->f, it->glyph_row);
13623
13624 /* Prepare for the next line. This line starts horizontally at (X
13625 HPOS) = (0 0). Vertical positions are incremented. As a
13626 convenience for the caller, IT->glyph_row is set to the next
13627 row to be used. */
13628 it->current_x = it->hpos = 0;
13629 it->current_y += row->height;
13630 ++it->vpos;
13631 ++it->glyph_row;
13632 return row->displays_text_p;
13633 }
13634
13635
13636 \f
13637 /***********************************************************************
13638 Menu Bar
13639 ***********************************************************************/
13640
13641 /* Redisplay the menu bar in the frame for window W.
13642
13643 The menu bar of X frames that don't have X toolkit support is
13644 displayed in a special window W->frame->menu_bar_window.
13645
13646 The menu bar of terminal frames is treated specially as far as
13647 glyph matrices are concerned. Menu bar lines are not part of
13648 windows, so the update is done directly on the frame matrix rows
13649 for the menu bar. */
13650
13651 static void
13652 display_menu_bar (w)
13653 struct window *w;
13654 {
13655 struct frame *f = XFRAME (WINDOW_FRAME (w));
13656 struct it it;
13657 Lisp_Object items;
13658 int i;
13659
13660 /* Don't do all this for graphical frames. */
13661 #ifdef HAVE_NTGUI
13662 if (!NILP (Vwindow_system))
13663 return;
13664 #endif
13665 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
13666 if (FRAME_X_P (f))
13667 return;
13668 #endif
13669 #ifdef MAC_OS
13670 if (FRAME_MAC_P (f))
13671 return;
13672 #endif
13673
13674 #ifdef USE_X_TOOLKIT
13675 xassert (!FRAME_WINDOW_P (f));
13676 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13677 it.first_visible_x = 0;
13678 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13679 #else /* not USE_X_TOOLKIT */
13680 if (FRAME_WINDOW_P (f))
13681 {
13682 /* Menu bar lines are displayed in the desired matrix of the
13683 dummy window menu_bar_window. */
13684 struct window *menu_w;
13685 xassert (WINDOWP (f->menu_bar_window));
13686 menu_w = XWINDOW (f->menu_bar_window);
13687 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13688 MENU_FACE_ID);
13689 it.first_visible_x = 0;
13690 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13691 }
13692 else
13693 {
13694 /* This is a TTY frame, i.e. character hpos/vpos are used as
13695 pixel x/y. */
13696 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13697 MENU_FACE_ID);
13698 it.first_visible_x = 0;
13699 it.last_visible_x = FRAME_WIDTH (f);
13700 }
13701 #endif /* not USE_X_TOOLKIT */
13702
13703 if (! mode_line_inverse_video)
13704 /* Force the menu-bar to be displayed in the default face. */
13705 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13706
13707 /* Clear all rows of the menu bar. */
13708 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13709 {
13710 struct glyph_row *row = it.glyph_row + i;
13711 clear_glyph_row (row);
13712 row->enabled_p = 1;
13713 row->full_width_p = 1;
13714 }
13715
13716 /* Display all items of the menu bar. */
13717 items = FRAME_MENU_BAR_ITEMS (it.f);
13718 for (i = 0; i < XVECTOR (items)->size; i += 4)
13719 {
13720 Lisp_Object string;
13721
13722 /* Stop at nil string. */
13723 string = AREF (items, i + 1);
13724 if (NILP (string))
13725 break;
13726
13727 /* Remember where item was displayed. */
13728 AREF (items, i + 3) = make_number (it.hpos);
13729
13730 /* Display the item, pad with one space. */
13731 if (it.current_x < it.last_visible_x)
13732 display_string (NULL, string, Qnil, 0, 0, &it,
13733 SCHARS (string) + 1, 0, 0, -1);
13734 }
13735
13736 /* Fill out the line with spaces. */
13737 if (it.current_x < it.last_visible_x)
13738 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13739
13740 /* Compute the total height of the lines. */
13741 compute_line_metrics (&it);
13742 }
13743
13744
13745 \f
13746 /***********************************************************************
13747 Mode Line
13748 ***********************************************************************/
13749
13750 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13751 FORCE is non-zero, redisplay mode lines unconditionally.
13752 Otherwise, redisplay only mode lines that are garbaged. Value is
13753 the number of windows whose mode lines were redisplayed. */
13754
13755 static int
13756 redisplay_mode_lines (window, force)
13757 Lisp_Object window;
13758 int force;
13759 {
13760 int nwindows = 0;
13761
13762 while (!NILP (window))
13763 {
13764 struct window *w = XWINDOW (window);
13765
13766 if (WINDOWP (w->hchild))
13767 nwindows += redisplay_mode_lines (w->hchild, force);
13768 else if (WINDOWP (w->vchild))
13769 nwindows += redisplay_mode_lines (w->vchild, force);
13770 else if (force
13771 || FRAME_GARBAGED_P (XFRAME (w->frame))
13772 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13773 {
13774 struct text_pos lpoint;
13775 struct buffer *old = current_buffer;
13776
13777 /* Set the window's buffer for the mode line display. */
13778 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13779 set_buffer_internal_1 (XBUFFER (w->buffer));
13780
13781 /* Point refers normally to the selected window. For any
13782 other window, set up appropriate value. */
13783 if (!EQ (window, selected_window))
13784 {
13785 struct text_pos pt;
13786
13787 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13788 if (CHARPOS (pt) < BEGV)
13789 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13790 else if (CHARPOS (pt) > (ZV - 1))
13791 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13792 else
13793 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13794 }
13795
13796 /* Display mode lines. */
13797 clear_glyph_matrix (w->desired_matrix);
13798 if (display_mode_lines (w))
13799 {
13800 ++nwindows;
13801 w->must_be_updated_p = 1;
13802 }
13803
13804 /* Restore old settings. */
13805 set_buffer_internal_1 (old);
13806 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13807 }
13808
13809 window = w->next;
13810 }
13811
13812 return nwindows;
13813 }
13814
13815
13816 /* Display the mode and/or top line of window W. Value is the number
13817 of mode lines displayed. */
13818
13819 static int
13820 display_mode_lines (w)
13821 struct window *w;
13822 {
13823 Lisp_Object old_selected_window, old_selected_frame;
13824 int n = 0;
13825
13826 old_selected_frame = selected_frame;
13827 selected_frame = w->frame;
13828 old_selected_window = selected_window;
13829 XSETWINDOW (selected_window, w);
13830
13831 /* These will be set while the mode line specs are processed. */
13832 line_number_displayed = 0;
13833 w->column_number_displayed = Qnil;
13834
13835 if (WINDOW_WANTS_MODELINE_P (w))
13836 {
13837 struct window *sel_w = XWINDOW (old_selected_window);
13838
13839 /* Select mode line face based on the real selected window. */
13840 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
13841 current_buffer->mode_line_format);
13842 ++n;
13843 }
13844
13845 if (WINDOW_WANTS_HEADER_LINE_P (w))
13846 {
13847 display_mode_line (w, HEADER_LINE_FACE_ID,
13848 current_buffer->header_line_format);
13849 ++n;
13850 }
13851
13852 selected_frame = old_selected_frame;
13853 selected_window = old_selected_window;
13854 return n;
13855 }
13856
13857
13858 /* Display mode or top line of window W. FACE_ID specifies which line
13859 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13860 FORMAT is the mode line format to display. Value is the pixel
13861 height of the mode line displayed. */
13862
13863 static int
13864 display_mode_line (w, face_id, format)
13865 struct window *w;
13866 enum face_id face_id;
13867 Lisp_Object format;
13868 {
13869 struct it it;
13870 struct face *face;
13871
13872 init_iterator (&it, w, -1, -1, NULL, face_id);
13873 prepare_desired_row (it.glyph_row);
13874
13875 if (! mode_line_inverse_video)
13876 /* Force the mode-line to be displayed in the default face. */
13877 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13878
13879 /* Temporarily make frame's keyboard the current kboard so that
13880 kboard-local variables in the mode_line_format will get the right
13881 values. */
13882 push_frame_kboard (it.f);
13883 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
13884 pop_frame_kboard ();
13885
13886 /* Fill up with spaces. */
13887 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13888
13889 compute_line_metrics (&it);
13890 it.glyph_row->full_width_p = 1;
13891 it.glyph_row->mode_line_p = 1;
13892 it.glyph_row->continued_p = 0;
13893 it.glyph_row->truncated_on_left_p = 0;
13894 it.glyph_row->truncated_on_right_p = 0;
13895
13896 /* Make a 3D mode-line have a shadow at its right end. */
13897 face = FACE_FROM_ID (it.f, face_id);
13898 extend_face_to_end_of_line (&it);
13899 if (face->box != FACE_NO_BOX)
13900 {
13901 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13902 + it.glyph_row->used[TEXT_AREA] - 1);
13903 last->right_box_line_p = 1;
13904 }
13905
13906 return it.glyph_row->height;
13907 }
13908
13909 /* Alist that caches the results of :propertize.
13910 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
13911 Lisp_Object mode_line_proptrans_alist;
13912
13913 /* List of strings making up the mode-line. */
13914 Lisp_Object mode_line_string_list;
13915
13916 /* Base face property when building propertized mode line string. */
13917 static Lisp_Object mode_line_string_face;
13918 static Lisp_Object mode_line_string_face_prop;
13919
13920
13921 /* Contribute ELT to the mode line for window IT->w. How it
13922 translates into text depends on its data type.
13923
13924 IT describes the display environment in which we display, as usual.
13925
13926 DEPTH is the depth in recursion. It is used to prevent
13927 infinite recursion here.
13928
13929 FIELD_WIDTH is the number of characters the display of ELT should
13930 occupy in the mode line, and PRECISION is the maximum number of
13931 characters to display from ELT's representation. See
13932 display_string for details.
13933
13934 Returns the hpos of the end of the text generated by ELT.
13935
13936 PROPS is a property list to add to any string we encounter.
13937
13938 If RISKY is nonzero, remove (disregard) any properties in any string
13939 we encounter, and ignore :eval and :propertize.
13940
13941 If the global variable `frame_title_ptr' is non-NULL, then the output
13942 is passed to `store_frame_title' instead of `display_string'. */
13943
13944 static int
13945 display_mode_element (it, depth, field_width, precision, elt, props, risky)
13946 struct it *it;
13947 int depth;
13948 int field_width, precision;
13949 Lisp_Object elt, props;
13950 int risky;
13951 {
13952 int n = 0, field, prec;
13953 int literal = 0;
13954
13955 tail_recurse:
13956 if (depth > 10)
13957 goto invalid;
13958
13959 depth++;
13960
13961 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13962 {
13963 case Lisp_String:
13964 {
13965 /* A string: output it and check for %-constructs within it. */
13966 unsigned char c;
13967 const unsigned char *this, *lisp_string;
13968
13969 if (!NILP (props) || risky)
13970 {
13971 Lisp_Object oprops, aelt;
13972 oprops = Ftext_properties_at (make_number (0), elt);
13973
13974 if (NILP (Fequal (props, oprops)) || risky)
13975 {
13976 /* If the starting string has properties,
13977 merge the specified ones onto the existing ones. */
13978 if (! NILP (oprops) && !risky)
13979 {
13980 Lisp_Object tem;
13981
13982 oprops = Fcopy_sequence (oprops);
13983 tem = props;
13984 while (CONSP (tem))
13985 {
13986 oprops = Fplist_put (oprops, XCAR (tem),
13987 XCAR (XCDR (tem)));
13988 tem = XCDR (XCDR (tem));
13989 }
13990 props = oprops;
13991 }
13992
13993 aelt = Fassoc (elt, mode_line_proptrans_alist);
13994 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
13995 {
13996 mode_line_proptrans_alist
13997 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
13998 elt = XCAR (aelt);
13999 }
14000 else
14001 {
14002 Lisp_Object tem;
14003
14004 elt = Fcopy_sequence (elt);
14005 Fset_text_properties (make_number (0), Flength (elt),
14006 props, elt);
14007 /* Add this item to mode_line_proptrans_alist. */
14008 mode_line_proptrans_alist
14009 = Fcons (Fcons (elt, props),
14010 mode_line_proptrans_alist);
14011 /* Truncate mode_line_proptrans_alist
14012 to at most 50 elements. */
14013 tem = Fnthcdr (make_number (50),
14014 mode_line_proptrans_alist);
14015 if (! NILP (tem))
14016 XSETCDR (tem, Qnil);
14017 }
14018 }
14019 }
14020
14021 this = SDATA (elt);
14022 lisp_string = this;
14023
14024 if (literal)
14025 {
14026 prec = precision - n;
14027 if (frame_title_ptr)
14028 n += store_frame_title (SDATA (elt), -1, prec);
14029 else if (!NILP (mode_line_string_list))
14030 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
14031 else
14032 n += display_string (NULL, elt, Qnil, 0, 0, it,
14033 0, prec, 0, STRING_MULTIBYTE (elt));
14034
14035 break;
14036 }
14037
14038 while ((precision <= 0 || n < precision)
14039 && *this
14040 && (frame_title_ptr
14041 || !NILP (mode_line_string_list)
14042 || it->current_x < it->last_visible_x))
14043 {
14044 const unsigned char *last = this;
14045
14046 /* Advance to end of string or next format specifier. */
14047 while ((c = *this++) != '\0' && c != '%')
14048 ;
14049
14050 if (this - 1 != last)
14051 {
14052 /* Output to end of string or up to '%'. Field width
14053 is length of string. Don't output more than
14054 PRECISION allows us. */
14055 --this;
14056
14057 prec = chars_in_text (last, this - last);
14058 if (precision > 0 && prec > precision - n)
14059 prec = precision - n;
14060
14061 if (frame_title_ptr)
14062 n += store_frame_title (last, 0, prec);
14063 else if (!NILP (mode_line_string_list))
14064 {
14065 int bytepos = last - lisp_string;
14066 int charpos = string_byte_to_char (elt, bytepos);
14067 n += store_mode_line_string (NULL,
14068 Fsubstring (elt, make_number (charpos),
14069 make_number (charpos + prec)),
14070 0, 0, 0, Qnil);
14071 }
14072 else
14073 {
14074 int bytepos = last - lisp_string;
14075 int charpos = string_byte_to_char (elt, bytepos);
14076 n += display_string (NULL, elt, Qnil, 0, charpos,
14077 it, 0, prec, 0,
14078 STRING_MULTIBYTE (elt));
14079 }
14080 }
14081 else /* c == '%' */
14082 {
14083 const unsigned char *percent_position = this;
14084
14085 /* Get the specified minimum width. Zero means
14086 don't pad. */
14087 field = 0;
14088 while ((c = *this++) >= '0' && c <= '9')
14089 field = field * 10 + c - '0';
14090
14091 /* Don't pad beyond the total padding allowed. */
14092 if (field_width - n > 0 && field > field_width - n)
14093 field = field_width - n;
14094
14095 /* Note that either PRECISION <= 0 or N < PRECISION. */
14096 prec = precision - n;
14097
14098 if (c == 'M')
14099 n += display_mode_element (it, depth, field, prec,
14100 Vglobal_mode_string, props,
14101 risky);
14102 else if (c != 0)
14103 {
14104 int multibyte;
14105 int bytepos, charpos;
14106 unsigned char *spec;
14107
14108 bytepos = percent_position - lisp_string;
14109 charpos = (STRING_MULTIBYTE (elt)
14110 ? string_byte_to_char (elt, bytepos)
14111 : bytepos);
14112
14113 spec
14114 = decode_mode_spec (it->w, c, field, prec, &multibyte);
14115
14116 if (frame_title_ptr)
14117 n += store_frame_title (spec, field, prec);
14118 else if (!NILP (mode_line_string_list))
14119 {
14120 int len = strlen (spec);
14121 Lisp_Object tem = make_string (spec, len);
14122 props = Ftext_properties_at (make_number (charpos), elt);
14123 /* Should only keep face property in props */
14124 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
14125 }
14126 else
14127 {
14128 int nglyphs_before, nwritten;
14129
14130 nglyphs_before = it->glyph_row->used[TEXT_AREA];
14131 nwritten = display_string (spec, Qnil, elt,
14132 charpos, 0, it,
14133 field, prec, 0,
14134 multibyte);
14135
14136 /* Assign to the glyphs written above the
14137 string where the `%x' came from, position
14138 of the `%'. */
14139 if (nwritten > 0)
14140 {
14141 struct glyph *glyph
14142 = (it->glyph_row->glyphs[TEXT_AREA]
14143 + nglyphs_before);
14144 int i;
14145
14146 for (i = 0; i < nwritten; ++i)
14147 {
14148 glyph[i].object = elt;
14149 glyph[i].charpos = charpos;
14150 }
14151
14152 n += nwritten;
14153 }
14154 }
14155 }
14156 else /* c == 0 */
14157 break;
14158 }
14159 }
14160 }
14161 break;
14162
14163 case Lisp_Symbol:
14164 /* A symbol: process the value of the symbol recursively
14165 as if it appeared here directly. Avoid error if symbol void.
14166 Special case: if value of symbol is a string, output the string
14167 literally. */
14168 {
14169 register Lisp_Object tem;
14170
14171 /* If the variable is not marked as risky to set
14172 then its contents are risky to use. */
14173 if (NILP (Fget (elt, Qrisky_local_variable)))
14174 risky = 1;
14175
14176 tem = Fboundp (elt);
14177 if (!NILP (tem))
14178 {
14179 tem = Fsymbol_value (elt);
14180 /* If value is a string, output that string literally:
14181 don't check for % within it. */
14182 if (STRINGP (tem))
14183 literal = 1;
14184
14185 if (!EQ (tem, elt))
14186 {
14187 /* Give up right away for nil or t. */
14188 elt = tem;
14189 goto tail_recurse;
14190 }
14191 }
14192 }
14193 break;
14194
14195 case Lisp_Cons:
14196 {
14197 register Lisp_Object car, tem;
14198
14199 /* A cons cell: five distinct cases.
14200 If first element is :eval or :propertize, do something special.
14201 If first element is a string or a cons, process all the elements
14202 and effectively concatenate them.
14203 If first element is a negative number, truncate displaying cdr to
14204 at most that many characters. If positive, pad (with spaces)
14205 to at least that many characters.
14206 If first element is a symbol, process the cadr or caddr recursively
14207 according to whether the symbol's value is non-nil or nil. */
14208 car = XCAR (elt);
14209 if (EQ (car, QCeval))
14210 {
14211 /* An element of the form (:eval FORM) means evaluate FORM
14212 and use the result as mode line elements. */
14213
14214 if (risky)
14215 break;
14216
14217 if (CONSP (XCDR (elt)))
14218 {
14219 Lisp_Object spec;
14220 spec = safe_eval (XCAR (XCDR (elt)));
14221 n += display_mode_element (it, depth, field_width - n,
14222 precision - n, spec, props,
14223 risky);
14224 }
14225 }
14226 else if (EQ (car, QCpropertize))
14227 {
14228 /* An element of the form (:propertize ELT PROPS...)
14229 means display ELT but applying properties PROPS. */
14230
14231 if (risky)
14232 break;
14233
14234 if (CONSP (XCDR (elt)))
14235 n += display_mode_element (it, depth, field_width - n,
14236 precision - n, XCAR (XCDR (elt)),
14237 XCDR (XCDR (elt)), risky);
14238 }
14239 else if (SYMBOLP (car))
14240 {
14241 tem = Fboundp (car);
14242 elt = XCDR (elt);
14243 if (!CONSP (elt))
14244 goto invalid;
14245 /* elt is now the cdr, and we know it is a cons cell.
14246 Use its car if CAR has a non-nil value. */
14247 if (!NILP (tem))
14248 {
14249 tem = Fsymbol_value (car);
14250 if (!NILP (tem))
14251 {
14252 elt = XCAR (elt);
14253 goto tail_recurse;
14254 }
14255 }
14256 /* Symbol's value is nil (or symbol is unbound)
14257 Get the cddr of the original list
14258 and if possible find the caddr and use that. */
14259 elt = XCDR (elt);
14260 if (NILP (elt))
14261 break;
14262 else if (!CONSP (elt))
14263 goto invalid;
14264 elt = XCAR (elt);
14265 goto tail_recurse;
14266 }
14267 else if (INTEGERP (car))
14268 {
14269 register int lim = XINT (car);
14270 elt = XCDR (elt);
14271 if (lim < 0)
14272 {
14273 /* Negative int means reduce maximum width. */
14274 if (precision <= 0)
14275 precision = -lim;
14276 else
14277 precision = min (precision, -lim);
14278 }
14279 else if (lim > 0)
14280 {
14281 /* Padding specified. Don't let it be more than
14282 current maximum. */
14283 if (precision > 0)
14284 lim = min (precision, lim);
14285
14286 /* If that's more padding than already wanted, queue it.
14287 But don't reduce padding already specified even if
14288 that is beyond the current truncation point. */
14289 field_width = max (lim, field_width);
14290 }
14291 goto tail_recurse;
14292 }
14293 else if (STRINGP (car) || CONSP (car))
14294 {
14295 register int limit = 50;
14296 /* Limit is to protect against circular lists. */
14297 while (CONSP (elt)
14298 && --limit > 0
14299 && (precision <= 0 || n < precision))
14300 {
14301 n += display_mode_element (it, depth, field_width - n,
14302 precision - n, XCAR (elt),
14303 props, risky);
14304 elt = XCDR (elt);
14305 }
14306 }
14307 }
14308 break;
14309
14310 default:
14311 invalid:
14312 if (frame_title_ptr)
14313 n += store_frame_title ("*invalid*", 0, precision - n);
14314 else if (!NILP (mode_line_string_list))
14315 n += store_mode_line_string ("*invalid*", Qnil, 0, 0, precision - n, Qnil);
14316 else
14317 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
14318 precision - n, 0, 0);
14319 return n;
14320 }
14321
14322 /* Pad to FIELD_WIDTH. */
14323 if (field_width > 0 && n < field_width)
14324 {
14325 if (frame_title_ptr)
14326 n += store_frame_title ("", field_width - n, 0);
14327 else if (!NILP (mode_line_string_list))
14328 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
14329 else
14330 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
14331 0, 0, 0);
14332 }
14333
14334 return n;
14335 }
14336
14337 /* Store a mode-line string element in mode_line_string_list.
14338
14339 If STRING is non-null, display that C string. Otherwise, the Lisp
14340 string LISP_STRING is displayed.
14341
14342 FIELD_WIDTH is the minimum number of output glyphs to produce.
14343 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14344 with spaces. FIELD_WIDTH <= 0 means don't pad.
14345
14346 PRECISION is the maximum number of characters to output from
14347 STRING. PRECISION <= 0 means don't truncate the string.
14348
14349 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
14350 properties to the string.
14351
14352 PROPS are the properties to add to the string.
14353 The mode_line_string_face face property is always added to the string.
14354 */
14355
14356 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
14357 char *string;
14358 Lisp_Object lisp_string;
14359 int copy_string;
14360 int field_width;
14361 int precision;
14362 Lisp_Object props;
14363 {
14364 int len;
14365 int n = 0;
14366
14367 if (string != NULL)
14368 {
14369 len = strlen (string);
14370 if (precision > 0 && len > precision)
14371 len = precision;
14372 lisp_string = make_string (string, len);
14373 if (NILP (props))
14374 props = mode_line_string_face_prop;
14375 else if (!NILP (mode_line_string_face))
14376 {
14377 Lisp_Object face = Fplist_get (props, Qface);
14378 props = Fcopy_sequence (props);
14379 if (NILP (face))
14380 face = mode_line_string_face;
14381 else
14382 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14383 props = Fplist_put (props, Qface, face);
14384 }
14385 Fadd_text_properties (make_number (0), make_number (len),
14386 props, lisp_string);
14387 }
14388 else
14389 {
14390 len = XFASTINT (Flength (lisp_string));
14391 if (precision > 0 && len > precision)
14392 {
14393 len = precision;
14394 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
14395 precision = -1;
14396 }
14397 if (!NILP (mode_line_string_face))
14398 {
14399 Lisp_Object face;
14400 if (NILP (props))
14401 props = Ftext_properties_at (make_number (0), lisp_string);
14402 face = Fplist_get (props, Qface);
14403 if (NILP (face))
14404 face = mode_line_string_face;
14405 else
14406 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14407 props = Fcons (Qface, Fcons (face, Qnil));
14408 if (copy_string)
14409 lisp_string = Fcopy_sequence (lisp_string);
14410 }
14411 if (!NILP (props))
14412 Fadd_text_properties (make_number (0), make_number (len),
14413 props, lisp_string);
14414 }
14415
14416 if (len > 0)
14417 {
14418 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
14419 n += len;
14420 }
14421
14422 if (field_width > len)
14423 {
14424 field_width -= len;
14425 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
14426 if (!NILP (props))
14427 Fadd_text_properties (make_number (0), make_number (field_width),
14428 props, lisp_string);
14429 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
14430 n += field_width;
14431 }
14432
14433 return n;
14434 }
14435
14436
14437 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
14438 0, 3, 0,
14439 doc: /* Return the mode-line of selected window as a string.
14440 First optional arg FORMAT specifies a different format string (see
14441 `mode-line-format' for details) to use. If FORMAT is t, return
14442 the buffer's header-line. Second optional arg WINDOW specifies a
14443 different window to use as the context for the formatting.
14444 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
14445 (format, window, no_props)
14446 Lisp_Object format, window, no_props;
14447 {
14448 struct it it;
14449 int len;
14450 struct window *w;
14451 struct buffer *old_buffer = NULL;
14452 enum face_id face_id = DEFAULT_FACE_ID;
14453
14454 if (NILP (window))
14455 window = selected_window;
14456 CHECK_WINDOW (window);
14457 w = XWINDOW (window);
14458 CHECK_BUFFER (w->buffer);
14459
14460 if (XBUFFER (w->buffer) != current_buffer)
14461 {
14462 old_buffer = current_buffer;
14463 set_buffer_internal_1 (XBUFFER (w->buffer));
14464 }
14465
14466 if (NILP (format) || EQ (format, Qt))
14467 {
14468 face_id = NILP (format)
14469 ? CURRENT_MODE_LINE_FACE_ID (w) :
14470 HEADER_LINE_FACE_ID;
14471 format = NILP (format)
14472 ? current_buffer->mode_line_format
14473 : current_buffer->header_line_format;
14474 }
14475
14476 init_iterator (&it, w, -1, -1, NULL, face_id);
14477
14478 if (NILP (no_props))
14479 {
14480 mode_line_string_face =
14481 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
14482 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
14483 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
14484
14485 mode_line_string_face_prop =
14486 NILP (mode_line_string_face) ? Qnil :
14487 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
14488
14489 /* We need a dummy last element in mode_line_string_list to
14490 indicate we are building the propertized mode-line string.
14491 Using mode_line_string_face_prop here GC protects it. */
14492 mode_line_string_list =
14493 Fcons (mode_line_string_face_prop, Qnil);
14494 frame_title_ptr = NULL;
14495 }
14496 else
14497 {
14498 mode_line_string_face_prop = Qnil;
14499 mode_line_string_list = Qnil;
14500 frame_title_ptr = frame_title_buf;
14501 }
14502
14503 push_frame_kboard (it.f);
14504 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
14505 pop_frame_kboard ();
14506
14507 if (old_buffer)
14508 set_buffer_internal_1 (old_buffer);
14509
14510 if (NILP (no_props))
14511 {
14512 Lisp_Object str;
14513 mode_line_string_list = Fnreverse (mode_line_string_list);
14514 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
14515 make_string ("", 0));
14516 mode_line_string_face_prop = Qnil;
14517 mode_line_string_list = Qnil;
14518 return str;
14519 }
14520
14521 len = frame_title_ptr - frame_title_buf;
14522 if (len > 0 && frame_title_ptr[-1] == '-')
14523 {
14524 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
14525 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
14526 ;
14527 frame_title_ptr += 3; /* restore last non-dash + two dashes */
14528 if (len > frame_title_ptr - frame_title_buf)
14529 len = frame_title_ptr - frame_title_buf;
14530 }
14531
14532 frame_title_ptr = NULL;
14533 return make_string (frame_title_buf, len);
14534 }
14535
14536 /* Write a null-terminated, right justified decimal representation of
14537 the positive integer D to BUF using a minimal field width WIDTH. */
14538
14539 static void
14540 pint2str (buf, width, d)
14541 register char *buf;
14542 register int width;
14543 register int d;
14544 {
14545 register char *p = buf;
14546
14547 if (d <= 0)
14548 *p++ = '0';
14549 else
14550 {
14551 while (d > 0)
14552 {
14553 *p++ = d % 10 + '0';
14554 d /= 10;
14555 }
14556 }
14557
14558 for (width -= (int) (p - buf); width > 0; --width)
14559 *p++ = ' ';
14560 *p-- = '\0';
14561 while (p > buf)
14562 {
14563 d = *buf;
14564 *buf++ = *p;
14565 *p-- = d;
14566 }
14567 }
14568
14569 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
14570 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
14571 type of CODING_SYSTEM. Return updated pointer into BUF. */
14572
14573 static unsigned char invalid_eol_type[] = "(*invalid*)";
14574
14575 static char *
14576 decode_mode_spec_coding (coding_system, buf, eol_flag)
14577 Lisp_Object coding_system;
14578 register char *buf;
14579 int eol_flag;
14580 {
14581 Lisp_Object val;
14582 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
14583 const unsigned char *eol_str;
14584 int eol_str_len;
14585 /* The EOL conversion we are using. */
14586 Lisp_Object eoltype;
14587
14588 val = Fget (coding_system, Qcoding_system);
14589 eoltype = Qnil;
14590
14591 if (!VECTORP (val)) /* Not yet decided. */
14592 {
14593 if (multibyte)
14594 *buf++ = '-';
14595 if (eol_flag)
14596 eoltype = eol_mnemonic_undecided;
14597 /* Don't mention EOL conversion if it isn't decided. */
14598 }
14599 else
14600 {
14601 Lisp_Object eolvalue;
14602
14603 eolvalue = Fget (coding_system, Qeol_type);
14604
14605 if (multibyte)
14606 *buf++ = XFASTINT (AREF (val, 1));
14607
14608 if (eol_flag)
14609 {
14610 /* The EOL conversion that is normal on this system. */
14611
14612 if (NILP (eolvalue)) /* Not yet decided. */
14613 eoltype = eol_mnemonic_undecided;
14614 else if (VECTORP (eolvalue)) /* Not yet decided. */
14615 eoltype = eol_mnemonic_undecided;
14616 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
14617 eoltype = (XFASTINT (eolvalue) == 0
14618 ? eol_mnemonic_unix
14619 : (XFASTINT (eolvalue) == 1
14620 ? eol_mnemonic_dos : eol_mnemonic_mac));
14621 }
14622 }
14623
14624 if (eol_flag)
14625 {
14626 /* Mention the EOL conversion if it is not the usual one. */
14627 if (STRINGP (eoltype))
14628 {
14629 eol_str = SDATA (eoltype);
14630 eol_str_len = SBYTES (eoltype);
14631 }
14632 else if (INTEGERP (eoltype)
14633 && CHAR_VALID_P (XINT (eoltype), 0))
14634 {
14635 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
14636 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
14637 eol_str = tmp;
14638 }
14639 else
14640 {
14641 eol_str = invalid_eol_type;
14642 eol_str_len = sizeof (invalid_eol_type) - 1;
14643 }
14644 bcopy (eol_str, buf, eol_str_len);
14645 buf += eol_str_len;
14646 }
14647
14648 return buf;
14649 }
14650
14651 /* Return a string for the output of a mode line %-spec for window W,
14652 generated by character C. PRECISION >= 0 means don't return a
14653 string longer than that value. FIELD_WIDTH > 0 means pad the
14654 string returned with spaces to that value. Return 1 in *MULTIBYTE
14655 if the result is multibyte text. */
14656
14657 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
14658
14659 static char *
14660 decode_mode_spec (w, c, field_width, precision, multibyte)
14661 struct window *w;
14662 register int c;
14663 int field_width, precision;
14664 int *multibyte;
14665 {
14666 Lisp_Object obj;
14667 struct frame *f = XFRAME (WINDOW_FRAME (w));
14668 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
14669 struct buffer *b = XBUFFER (w->buffer);
14670
14671 obj = Qnil;
14672 *multibyte = 0;
14673
14674 switch (c)
14675 {
14676 case '*':
14677 if (!NILP (b->read_only))
14678 return "%";
14679 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14680 return "*";
14681 return "-";
14682
14683 case '+':
14684 /* This differs from %* only for a modified read-only buffer. */
14685 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14686 return "*";
14687 if (!NILP (b->read_only))
14688 return "%";
14689 return "-";
14690
14691 case '&':
14692 /* This differs from %* in ignoring read-only-ness. */
14693 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14694 return "*";
14695 return "-";
14696
14697 case '%':
14698 return "%";
14699
14700 case '[':
14701 {
14702 int i;
14703 char *p;
14704
14705 if (command_loop_level > 5)
14706 return "[[[... ";
14707 p = decode_mode_spec_buf;
14708 for (i = 0; i < command_loop_level; i++)
14709 *p++ = '[';
14710 *p = 0;
14711 return decode_mode_spec_buf;
14712 }
14713
14714 case ']':
14715 {
14716 int i;
14717 char *p;
14718
14719 if (command_loop_level > 5)
14720 return " ...]]]";
14721 p = decode_mode_spec_buf;
14722 for (i = 0; i < command_loop_level; i++)
14723 *p++ = ']';
14724 *p = 0;
14725 return decode_mode_spec_buf;
14726 }
14727
14728 case '-':
14729 {
14730 register int i;
14731
14732 /* Let lots_of_dashes be a string of infinite length. */
14733 if (!NILP (mode_line_string_list))
14734 return "--";
14735 if (field_width <= 0
14736 || field_width > sizeof (lots_of_dashes))
14737 {
14738 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
14739 decode_mode_spec_buf[i] = '-';
14740 decode_mode_spec_buf[i] = '\0';
14741 return decode_mode_spec_buf;
14742 }
14743 else
14744 return lots_of_dashes;
14745 }
14746
14747 case 'b':
14748 obj = b->name;
14749 break;
14750
14751 case 'c':
14752 {
14753 int col = (int) current_column (); /* iftc */
14754 w->column_number_displayed = make_number (col);
14755 pint2str (decode_mode_spec_buf, field_width, col);
14756 return decode_mode_spec_buf;
14757 }
14758
14759 case 'F':
14760 /* %F displays the frame name. */
14761 if (!NILP (f->title))
14762 return (char *) SDATA (f->title);
14763 if (f->explicit_name || ! FRAME_WINDOW_P (f))
14764 return (char *) SDATA (f->name);
14765 return "Emacs";
14766
14767 case 'f':
14768 obj = b->filename;
14769 break;
14770
14771 case 'l':
14772 {
14773 int startpos = XMARKER (w->start)->charpos;
14774 int startpos_byte = marker_byte_position (w->start);
14775 int line, linepos, linepos_byte, topline;
14776 int nlines, junk;
14777 int height = XFASTINT (w->height);
14778
14779 /* If we decided that this buffer isn't suitable for line numbers,
14780 don't forget that too fast. */
14781 if (EQ (w->base_line_pos, w->buffer))
14782 goto no_value;
14783 /* But do forget it, if the window shows a different buffer now. */
14784 else if (BUFFERP (w->base_line_pos))
14785 w->base_line_pos = Qnil;
14786
14787 /* If the buffer is very big, don't waste time. */
14788 if (INTEGERP (Vline_number_display_limit)
14789 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
14790 {
14791 w->base_line_pos = Qnil;
14792 w->base_line_number = Qnil;
14793 goto no_value;
14794 }
14795
14796 if (!NILP (w->base_line_number)
14797 && !NILP (w->base_line_pos)
14798 && XFASTINT (w->base_line_pos) <= startpos)
14799 {
14800 line = XFASTINT (w->base_line_number);
14801 linepos = XFASTINT (w->base_line_pos);
14802 linepos_byte = buf_charpos_to_bytepos (b, linepos);
14803 }
14804 else
14805 {
14806 line = 1;
14807 linepos = BUF_BEGV (b);
14808 linepos_byte = BUF_BEGV_BYTE (b);
14809 }
14810
14811 /* Count lines from base line to window start position. */
14812 nlines = display_count_lines (linepos, linepos_byte,
14813 startpos_byte,
14814 startpos, &junk);
14815
14816 topline = nlines + line;
14817
14818 /* Determine a new base line, if the old one is too close
14819 or too far away, or if we did not have one.
14820 "Too close" means it's plausible a scroll-down would
14821 go back past it. */
14822 if (startpos == BUF_BEGV (b))
14823 {
14824 w->base_line_number = make_number (topline);
14825 w->base_line_pos = make_number (BUF_BEGV (b));
14826 }
14827 else if (nlines < height + 25 || nlines > height * 3 + 50
14828 || linepos == BUF_BEGV (b))
14829 {
14830 int limit = BUF_BEGV (b);
14831 int limit_byte = BUF_BEGV_BYTE (b);
14832 int position;
14833 int distance = (height * 2 + 30) * line_number_display_limit_width;
14834
14835 if (startpos - distance > limit)
14836 {
14837 limit = startpos - distance;
14838 limit_byte = CHAR_TO_BYTE (limit);
14839 }
14840
14841 nlines = display_count_lines (startpos, startpos_byte,
14842 limit_byte,
14843 - (height * 2 + 30),
14844 &position);
14845 /* If we couldn't find the lines we wanted within
14846 line_number_display_limit_width chars per line,
14847 give up on line numbers for this window. */
14848 if (position == limit_byte && limit == startpos - distance)
14849 {
14850 w->base_line_pos = w->buffer;
14851 w->base_line_number = Qnil;
14852 goto no_value;
14853 }
14854
14855 w->base_line_number = make_number (topline - nlines);
14856 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
14857 }
14858
14859 /* Now count lines from the start pos to point. */
14860 nlines = display_count_lines (startpos, startpos_byte,
14861 PT_BYTE, PT, &junk);
14862
14863 /* Record that we did display the line number. */
14864 line_number_displayed = 1;
14865
14866 /* Make the string to show. */
14867 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
14868 return decode_mode_spec_buf;
14869 no_value:
14870 {
14871 char* p = decode_mode_spec_buf;
14872 int pad = field_width - 2;
14873 while (pad-- > 0)
14874 *p++ = ' ';
14875 *p++ = '?';
14876 *p++ = '?';
14877 *p = '\0';
14878 return decode_mode_spec_buf;
14879 }
14880 }
14881 break;
14882
14883 case 'm':
14884 obj = b->mode_name;
14885 break;
14886
14887 case 'n':
14888 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
14889 return " Narrow";
14890 break;
14891
14892 case 'p':
14893 {
14894 int pos = marker_position (w->start);
14895 int total = BUF_ZV (b) - BUF_BEGV (b);
14896
14897 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
14898 {
14899 if (pos <= BUF_BEGV (b))
14900 return "All";
14901 else
14902 return "Bottom";
14903 }
14904 else if (pos <= BUF_BEGV (b))
14905 return "Top";
14906 else
14907 {
14908 if (total > 1000000)
14909 /* Do it differently for a large value, to avoid overflow. */
14910 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14911 else
14912 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14913 /* We can't normally display a 3-digit number,
14914 so get us a 2-digit number that is close. */
14915 if (total == 100)
14916 total = 99;
14917 sprintf (decode_mode_spec_buf, "%2d%%", total);
14918 return decode_mode_spec_buf;
14919 }
14920 }
14921
14922 /* Display percentage of size above the bottom of the screen. */
14923 case 'P':
14924 {
14925 int toppos = marker_position (w->start);
14926 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14927 int total = BUF_ZV (b) - BUF_BEGV (b);
14928
14929 if (botpos >= BUF_ZV (b))
14930 {
14931 if (toppos <= BUF_BEGV (b))
14932 return "All";
14933 else
14934 return "Bottom";
14935 }
14936 else
14937 {
14938 if (total > 1000000)
14939 /* Do it differently for a large value, to avoid overflow. */
14940 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14941 else
14942 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14943 /* We can't normally display a 3-digit number,
14944 so get us a 2-digit number that is close. */
14945 if (total == 100)
14946 total = 99;
14947 if (toppos <= BUF_BEGV (b))
14948 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14949 else
14950 sprintf (decode_mode_spec_buf, "%2d%%", total);
14951 return decode_mode_spec_buf;
14952 }
14953 }
14954
14955 case 's':
14956 /* status of process */
14957 obj = Fget_buffer_process (w->buffer);
14958 if (NILP (obj))
14959 return "no process";
14960 #ifdef subprocesses
14961 obj = Fsymbol_name (Fprocess_status (obj));
14962 #endif
14963 break;
14964
14965 case 't': /* indicate TEXT or BINARY */
14966 #ifdef MODE_LINE_BINARY_TEXT
14967 return MODE_LINE_BINARY_TEXT (b);
14968 #else
14969 return "T";
14970 #endif
14971
14972 case 'z':
14973 /* coding-system (not including end-of-line format) */
14974 case 'Z':
14975 /* coding-system (including end-of-line type) */
14976 {
14977 int eol_flag = (c == 'Z');
14978 char *p = decode_mode_spec_buf;
14979
14980 if (! FRAME_WINDOW_P (f))
14981 {
14982 /* No need to mention EOL here--the terminal never needs
14983 to do EOL conversion. */
14984 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14985 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
14986 }
14987 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14988 p, eol_flag);
14989
14990 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14991 #ifdef subprocesses
14992 obj = Fget_buffer_process (Fcurrent_buffer ());
14993 if (PROCESSP (obj))
14994 {
14995 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14996 p, eol_flag);
14997 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14998 p, eol_flag);
14999 }
15000 #endif /* subprocesses */
15001 #endif /* 0 */
15002 *p = 0;
15003 return decode_mode_spec_buf;
15004 }
15005 }
15006
15007 if (STRINGP (obj))
15008 {
15009 *multibyte = STRING_MULTIBYTE (obj);
15010 return (char *) SDATA (obj);
15011 }
15012 else
15013 return "";
15014 }
15015
15016
15017 /* Count up to COUNT lines starting from START / START_BYTE.
15018 But don't go beyond LIMIT_BYTE.
15019 Return the number of lines thus found (always nonnegative).
15020
15021 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
15022
15023 static int
15024 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
15025 int start, start_byte, limit_byte, count;
15026 int *byte_pos_ptr;
15027 {
15028 register unsigned char *cursor;
15029 unsigned char *base;
15030
15031 register int ceiling;
15032 register unsigned char *ceiling_addr;
15033 int orig_count = count;
15034
15035 /* If we are not in selective display mode,
15036 check only for newlines. */
15037 int selective_display = (!NILP (current_buffer->selective_display)
15038 && !INTEGERP (current_buffer->selective_display));
15039
15040 if (count > 0)
15041 {
15042 while (start_byte < limit_byte)
15043 {
15044 ceiling = BUFFER_CEILING_OF (start_byte);
15045 ceiling = min (limit_byte - 1, ceiling);
15046 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
15047 base = (cursor = BYTE_POS_ADDR (start_byte));
15048 while (1)
15049 {
15050 if (selective_display)
15051 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
15052 ;
15053 else
15054 while (*cursor != '\n' && ++cursor != ceiling_addr)
15055 ;
15056
15057 if (cursor != ceiling_addr)
15058 {
15059 if (--count == 0)
15060 {
15061 start_byte += cursor - base + 1;
15062 *byte_pos_ptr = start_byte;
15063 return orig_count;
15064 }
15065 else
15066 if (++cursor == ceiling_addr)
15067 break;
15068 }
15069 else
15070 break;
15071 }
15072 start_byte += cursor - base;
15073 }
15074 }
15075 else
15076 {
15077 while (start_byte > limit_byte)
15078 {
15079 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
15080 ceiling = max (limit_byte, ceiling);
15081 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
15082 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
15083 while (1)
15084 {
15085 if (selective_display)
15086 while (--cursor != ceiling_addr
15087 && *cursor != '\n' && *cursor != 015)
15088 ;
15089 else
15090 while (--cursor != ceiling_addr && *cursor != '\n')
15091 ;
15092
15093 if (cursor != ceiling_addr)
15094 {
15095 if (++count == 0)
15096 {
15097 start_byte += cursor - base + 1;
15098 *byte_pos_ptr = start_byte;
15099 /* When scanning backwards, we should
15100 not count the newline posterior to which we stop. */
15101 return - orig_count - 1;
15102 }
15103 }
15104 else
15105 break;
15106 }
15107 /* Here we add 1 to compensate for the last decrement
15108 of CURSOR, which took it past the valid range. */
15109 start_byte += cursor - base + 1;
15110 }
15111 }
15112
15113 *byte_pos_ptr = limit_byte;
15114
15115 if (count < 0)
15116 return - orig_count + count;
15117 return orig_count - count;
15118
15119 }
15120
15121
15122 \f
15123 /***********************************************************************
15124 Displaying strings
15125 ***********************************************************************/
15126
15127 /* Display a NUL-terminated string, starting with index START.
15128
15129 If STRING is non-null, display that C string. Otherwise, the Lisp
15130 string LISP_STRING is displayed.
15131
15132 If FACE_STRING is not nil, FACE_STRING_POS is a position in
15133 FACE_STRING. Display STRING or LISP_STRING with the face at
15134 FACE_STRING_POS in FACE_STRING:
15135
15136 Display the string in the environment given by IT, but use the
15137 standard display table, temporarily.
15138
15139 FIELD_WIDTH is the minimum number of output glyphs to produce.
15140 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15141 with spaces. If STRING has more characters, more than FIELD_WIDTH
15142 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
15143
15144 PRECISION is the maximum number of characters to output from
15145 STRING. PRECISION < 0 means don't truncate the string.
15146
15147 This is roughly equivalent to printf format specifiers:
15148
15149 FIELD_WIDTH PRECISION PRINTF
15150 ----------------------------------------
15151 -1 -1 %s
15152 -1 10 %.10s
15153 10 -1 %10s
15154 20 10 %20.10s
15155
15156 MULTIBYTE zero means do not display multibyte chars, > 0 means do
15157 display them, and < 0 means obey the current buffer's value of
15158 enable_multibyte_characters.
15159
15160 Value is the number of glyphs produced. */
15161
15162 static int
15163 display_string (string, lisp_string, face_string, face_string_pos,
15164 start, it, field_width, precision, max_x, multibyte)
15165 unsigned char *string;
15166 Lisp_Object lisp_string;
15167 Lisp_Object face_string;
15168 int face_string_pos;
15169 int start;
15170 struct it *it;
15171 int field_width, precision, max_x;
15172 int multibyte;
15173 {
15174 int hpos_at_start = it->hpos;
15175 int saved_face_id = it->face_id;
15176 struct glyph_row *row = it->glyph_row;
15177
15178 /* Initialize the iterator IT for iteration over STRING beginning
15179 with index START. */
15180 reseat_to_string (it, string, lisp_string, start,
15181 precision, field_width, multibyte);
15182
15183 /* If displaying STRING, set up the face of the iterator
15184 from LISP_STRING, if that's given. */
15185 if (STRINGP (face_string))
15186 {
15187 int endptr;
15188 struct face *face;
15189
15190 it->face_id
15191 = face_at_string_position (it->w, face_string, face_string_pos,
15192 0, it->region_beg_charpos,
15193 it->region_end_charpos,
15194 &endptr, it->base_face_id, 0);
15195 face = FACE_FROM_ID (it->f, it->face_id);
15196 it->face_box_p = face->box != FACE_NO_BOX;
15197 }
15198
15199 /* Set max_x to the maximum allowed X position. Don't let it go
15200 beyond the right edge of the window. */
15201 if (max_x <= 0)
15202 max_x = it->last_visible_x;
15203 else
15204 max_x = min (max_x, it->last_visible_x);
15205
15206 /* Skip over display elements that are not visible. because IT->w is
15207 hscrolled. */
15208 if (it->current_x < it->first_visible_x)
15209 move_it_in_display_line_to (it, 100000, it->first_visible_x,
15210 MOVE_TO_POS | MOVE_TO_X);
15211
15212 row->ascent = it->max_ascent;
15213 row->height = it->max_ascent + it->max_descent;
15214 row->phys_ascent = it->max_phys_ascent;
15215 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15216
15217 /* This condition is for the case that we are called with current_x
15218 past last_visible_x. */
15219 while (it->current_x < max_x)
15220 {
15221 int x_before, x, n_glyphs_before, i, nglyphs;
15222
15223 /* Get the next display element. */
15224 if (!get_next_display_element (it))
15225 break;
15226
15227 /* Produce glyphs. */
15228 x_before = it->current_x;
15229 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
15230 PRODUCE_GLYPHS (it);
15231
15232 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
15233 i = 0;
15234 x = x_before;
15235 while (i < nglyphs)
15236 {
15237 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15238
15239 if (!it->truncate_lines_p
15240 && x + glyph->pixel_width > max_x)
15241 {
15242 /* End of continued line or max_x reached. */
15243 if (CHAR_GLYPH_PADDING_P (*glyph))
15244 {
15245 /* A wide character is unbreakable. */
15246 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
15247 it->current_x = x_before;
15248 }
15249 else
15250 {
15251 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
15252 it->current_x = x;
15253 }
15254 break;
15255 }
15256 else if (x + glyph->pixel_width > it->first_visible_x)
15257 {
15258 /* Glyph is at least partially visible. */
15259 ++it->hpos;
15260 if (x < it->first_visible_x)
15261 it->glyph_row->x = x - it->first_visible_x;
15262 }
15263 else
15264 {
15265 /* Glyph is off the left margin of the display area.
15266 Should not happen. */
15267 abort ();
15268 }
15269
15270 row->ascent = max (row->ascent, it->max_ascent);
15271 row->height = max (row->height, it->max_ascent + it->max_descent);
15272 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15273 row->phys_height = max (row->phys_height,
15274 it->max_phys_ascent + it->max_phys_descent);
15275 x += glyph->pixel_width;
15276 ++i;
15277 }
15278
15279 /* Stop if max_x reached. */
15280 if (i < nglyphs)
15281 break;
15282
15283 /* Stop at line ends. */
15284 if (ITERATOR_AT_END_OF_LINE_P (it))
15285 {
15286 it->continuation_lines_width = 0;
15287 break;
15288 }
15289
15290 set_iterator_to_next (it, 1);
15291
15292 /* Stop if truncating at the right edge. */
15293 if (it->truncate_lines_p
15294 && it->current_x >= it->last_visible_x)
15295 {
15296 /* Add truncation mark, but don't do it if the line is
15297 truncated at a padding space. */
15298 if (IT_CHARPOS (*it) < it->string_nchars)
15299 {
15300 if (!FRAME_WINDOW_P (it->f))
15301 {
15302 int i, n;
15303
15304 if (it->current_x > it->last_visible_x)
15305 {
15306 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15307 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15308 break;
15309 for (n = row->used[TEXT_AREA]; i < n; ++i)
15310 {
15311 row->used[TEXT_AREA] = i;
15312 produce_special_glyphs (it, IT_TRUNCATION);
15313 }
15314 }
15315 produce_special_glyphs (it, IT_TRUNCATION);
15316 }
15317 it->glyph_row->truncated_on_right_p = 1;
15318 }
15319 break;
15320 }
15321 }
15322
15323 /* Maybe insert a truncation at the left. */
15324 if (it->first_visible_x
15325 && IT_CHARPOS (*it) > 0)
15326 {
15327 if (!FRAME_WINDOW_P (it->f))
15328 insert_left_trunc_glyphs (it);
15329 it->glyph_row->truncated_on_left_p = 1;
15330 }
15331
15332 it->face_id = saved_face_id;
15333
15334 /* Value is number of columns displayed. */
15335 return it->hpos - hpos_at_start;
15336 }
15337
15338
15339 \f
15340 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
15341 appears as an element of LIST or as the car of an element of LIST.
15342 If PROPVAL is a list, compare each element against LIST in that
15343 way, and return 1/2 if any element of PROPVAL is found in LIST.
15344 Otherwise return 0. This function cannot quit.
15345 The return value is 2 if the text is invisible but with an ellipsis
15346 and 1 if it's invisible and without an ellipsis. */
15347
15348 int
15349 invisible_p (propval, list)
15350 register Lisp_Object propval;
15351 Lisp_Object list;
15352 {
15353 register Lisp_Object tail, proptail;
15354
15355 for (tail = list; CONSP (tail); tail = XCDR (tail))
15356 {
15357 register Lisp_Object tem;
15358 tem = XCAR (tail);
15359 if (EQ (propval, tem))
15360 return 1;
15361 if (CONSP (tem) && EQ (propval, XCAR (tem)))
15362 return NILP (XCDR (tem)) ? 1 : 2;
15363 }
15364
15365 if (CONSP (propval))
15366 {
15367 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
15368 {
15369 Lisp_Object propelt;
15370 propelt = XCAR (proptail);
15371 for (tail = list; CONSP (tail); tail = XCDR (tail))
15372 {
15373 register Lisp_Object tem;
15374 tem = XCAR (tail);
15375 if (EQ (propelt, tem))
15376 return 1;
15377 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
15378 return NILP (XCDR (tem)) ? 1 : 2;
15379 }
15380 }
15381 }
15382
15383 return 0;
15384 }
15385
15386 \f
15387 /***********************************************************************
15388 Cursor types
15389 ***********************************************************************/
15390
15391 /* Value is the internal representation of the specified cursor type
15392 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
15393 of the bar cursor. */
15394
15395 enum text_cursor_kinds
15396 get_specified_cursor_type (arg, width)
15397 Lisp_Object arg;
15398 int *width;
15399 {
15400 enum text_cursor_kinds type;
15401
15402 if (NILP (arg))
15403 return NO_CURSOR;
15404
15405 if (EQ (arg, Qbox))
15406 return FILLED_BOX_CURSOR;
15407
15408 if (EQ (arg, Qhollow))
15409 return HOLLOW_BOX_CURSOR;
15410
15411 if (EQ (arg, Qbar))
15412 {
15413 *width = 2;
15414 return BAR_CURSOR;
15415 }
15416
15417 if (CONSP (arg)
15418 && EQ (XCAR (arg), Qbar)
15419 && INTEGERP (XCDR (arg))
15420 && XINT (XCDR (arg)) >= 0)
15421 {
15422 *width = XINT (XCDR (arg));
15423 return BAR_CURSOR;
15424 }
15425
15426 if (EQ (arg, Qhbar))
15427 {
15428 *width = 2;
15429 return HBAR_CURSOR;
15430 }
15431
15432 if (CONSP (arg)
15433 && EQ (XCAR (arg), Qhbar)
15434 && INTEGERP (XCDR (arg))
15435 && XINT (XCDR (arg)) >= 0)
15436 {
15437 *width = XINT (XCDR (arg));
15438 return HBAR_CURSOR;
15439 }
15440
15441 /* Treat anything unknown as "hollow box cursor".
15442 It was bad to signal an error; people have trouble fixing
15443 .Xdefaults with Emacs, when it has something bad in it. */
15444 type = HOLLOW_BOX_CURSOR;
15445
15446 return type;
15447 }
15448
15449 /* Set the default cursor types for specified frame. */
15450 void
15451 set_frame_cursor_types (f, arg)
15452 struct frame *f;
15453 Lisp_Object arg;
15454 {
15455 int width;
15456 Lisp_Object tem;
15457
15458 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
15459 FRAME_CURSOR_WIDTH (f) = width;
15460
15461 /* By default, set up the blink-off state depending on the on-state. */
15462
15463 tem = Fassoc (arg, Vblink_cursor_alist);
15464 if (!NILP (tem))
15465 {
15466 FRAME_BLINK_OFF_CURSOR (f)
15467 = get_specified_cursor_type (XCDR (tem), &width);
15468 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
15469 }
15470 else
15471 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
15472 }
15473
15474
15475 /* Return the cursor we want to be displayed in window W. Return
15476 width of bar/hbar cursor through WIDTH arg. Return with
15477 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
15478 (i.e. if the `system caret' should track this cursor).
15479
15480 In a mini-buffer window, we want the cursor only to appear if we
15481 are reading input from this window. For the selected window, we
15482 want the cursor type given by the frame parameter or buffer local
15483 setting of cursor-type. If explicitly marked off, draw no cursor.
15484 In all other cases, we want a hollow box cursor. */
15485
15486 enum text_cursor_kinds
15487 get_window_cursor_type (w, width, active_cursor)
15488 struct window *w;
15489 int *width;
15490 int *active_cursor;
15491 {
15492 struct frame *f = XFRAME (w->frame);
15493 struct buffer *b = XBUFFER (w->buffer);
15494 int cursor_type = DEFAULT_CURSOR;
15495 Lisp_Object alt_cursor;
15496 int non_selected = 0;
15497
15498 *active_cursor = 1;
15499
15500 /* Echo area */
15501 if (cursor_in_echo_area
15502 && FRAME_HAS_MINIBUF_P (f)
15503 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
15504 {
15505 if (w == XWINDOW (echo_area_window))
15506 {
15507 *width = FRAME_CURSOR_WIDTH (f);
15508 return FRAME_DESIRED_CURSOR (f);
15509 }
15510
15511 *active_cursor = 0;
15512 non_selected = 1;
15513 }
15514
15515 /* Nonselected window or nonselected frame. */
15516 else if (w != XWINDOW (f->selected_window)
15517 #ifdef HAVE_WINDOW_SYSTEM
15518 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
15519 #endif
15520 )
15521 {
15522 *active_cursor = 0;
15523
15524 if (MINI_WINDOW_P (w) && minibuf_level == 0)
15525 return NO_CURSOR;
15526
15527 non_selected = 1;
15528 }
15529
15530 /* Never display a cursor in a window in which cursor-type is nil. */
15531 if (NILP (b->cursor_type))
15532 return NO_CURSOR;
15533
15534 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
15535 if (non_selected)
15536 {
15537 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
15538 return get_specified_cursor_type (alt_cursor, width);
15539 }
15540
15541 /* Get the normal cursor type for this window. */
15542 if (EQ (b->cursor_type, Qt))
15543 {
15544 cursor_type = FRAME_DESIRED_CURSOR (f);
15545 *width = FRAME_CURSOR_WIDTH (f);
15546 }
15547 else
15548 cursor_type = get_specified_cursor_type (b->cursor_type, width);
15549
15550 /* Use normal cursor if not blinked off. */
15551 if (!w->cursor_off_p)
15552 return cursor_type;
15553
15554 /* Cursor is blinked off, so determine how to "toggle" it. */
15555
15556 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
15557 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
15558 return get_specified_cursor_type (XCDR (alt_cursor), width);
15559
15560 /* Then see if frame has specified a specific blink off cursor type. */
15561 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
15562 {
15563 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
15564 return FRAME_BLINK_OFF_CURSOR (f);
15565 }
15566
15567 /* Finally perform built-in cursor blinking:
15568 filled box <-> hollow box
15569 wide [h]bar <-> narrow [h]bar
15570 narrow [h]bar <-> no cursor
15571 other type <-> no cursor */
15572
15573 if (cursor_type == FILLED_BOX_CURSOR)
15574 return HOLLOW_BOX_CURSOR;
15575
15576 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
15577 {
15578 *width = 1;
15579 return cursor_type;
15580 }
15581
15582 return NO_CURSOR;
15583 }
15584
15585 \f
15586 /***********************************************************************
15587 Initialization
15588 ***********************************************************************/
15589
15590 void
15591 syms_of_xdisp ()
15592 {
15593 Vwith_echo_area_save_vector = Qnil;
15594 staticpro (&Vwith_echo_area_save_vector);
15595
15596 Vmessage_stack = Qnil;
15597 staticpro (&Vmessage_stack);
15598
15599 Qinhibit_redisplay = intern ("inhibit-redisplay");
15600 staticpro (&Qinhibit_redisplay);
15601
15602 message_dolog_marker1 = Fmake_marker ();
15603 staticpro (&message_dolog_marker1);
15604 message_dolog_marker2 = Fmake_marker ();
15605 staticpro (&message_dolog_marker2);
15606 message_dolog_marker3 = Fmake_marker ();
15607 staticpro (&message_dolog_marker3);
15608
15609 #if GLYPH_DEBUG
15610 defsubr (&Sdump_frame_glyph_matrix);
15611 defsubr (&Sdump_glyph_matrix);
15612 defsubr (&Sdump_glyph_row);
15613 defsubr (&Sdump_tool_bar_row);
15614 defsubr (&Strace_redisplay);
15615 defsubr (&Strace_to_stderr);
15616 #endif
15617 #ifdef HAVE_WINDOW_SYSTEM
15618 defsubr (&Stool_bar_lines_needed);
15619 #endif
15620 defsubr (&Sformat_mode_line);
15621
15622 staticpro (&Qmenu_bar_update_hook);
15623 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
15624
15625 staticpro (&Qoverriding_terminal_local_map);
15626 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
15627
15628 staticpro (&Qoverriding_local_map);
15629 Qoverriding_local_map = intern ("overriding-local-map");
15630
15631 staticpro (&Qwindow_scroll_functions);
15632 Qwindow_scroll_functions = intern ("window-scroll-functions");
15633
15634 staticpro (&Qredisplay_end_trigger_functions);
15635 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
15636
15637 staticpro (&Qinhibit_point_motion_hooks);
15638 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
15639
15640 QCdata = intern (":data");
15641 staticpro (&QCdata);
15642 Qdisplay = intern ("display");
15643 staticpro (&Qdisplay);
15644 Qspace_width = intern ("space-width");
15645 staticpro (&Qspace_width);
15646 Qraise = intern ("raise");
15647 staticpro (&Qraise);
15648 Qspace = intern ("space");
15649 staticpro (&Qspace);
15650 Qmargin = intern ("margin");
15651 staticpro (&Qmargin);
15652 Qleft_margin = intern ("left-margin");
15653 staticpro (&Qleft_margin);
15654 Qright_margin = intern ("right-margin");
15655 staticpro (&Qright_margin);
15656 Qalign_to = intern ("align-to");
15657 staticpro (&Qalign_to);
15658 QCalign_to = intern (":align-to");
15659 staticpro (&QCalign_to);
15660 Qrelative_width = intern ("relative-width");
15661 staticpro (&Qrelative_width);
15662 QCrelative_width = intern (":relative-width");
15663 staticpro (&QCrelative_width);
15664 QCrelative_height = intern (":relative-height");
15665 staticpro (&QCrelative_height);
15666 QCeval = intern (":eval");
15667 staticpro (&QCeval);
15668 QCpropertize = intern (":propertize");
15669 staticpro (&QCpropertize);
15670 QCfile = intern (":file");
15671 staticpro (&QCfile);
15672 Qfontified = intern ("fontified");
15673 staticpro (&Qfontified);
15674 Qfontification_functions = intern ("fontification-functions");
15675 staticpro (&Qfontification_functions);
15676 Qtrailing_whitespace = intern ("trailing-whitespace");
15677 staticpro (&Qtrailing_whitespace);
15678 Qimage = intern ("image");
15679 staticpro (&Qimage);
15680 Qmessage_truncate_lines = intern ("message-truncate-lines");
15681 staticpro (&Qmessage_truncate_lines);
15682 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
15683 staticpro (&Qcursor_in_non_selected_windows);
15684 Qgrow_only = intern ("grow-only");
15685 staticpro (&Qgrow_only);
15686 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
15687 staticpro (&Qinhibit_menubar_update);
15688 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
15689 staticpro (&Qinhibit_eval_during_redisplay);
15690 Qposition = intern ("position");
15691 staticpro (&Qposition);
15692 Qbuffer_position = intern ("buffer-position");
15693 staticpro (&Qbuffer_position);
15694 Qobject = intern ("object");
15695 staticpro (&Qobject);
15696 Qbar = intern ("bar");
15697 staticpro (&Qbar);
15698 Qhbar = intern ("hbar");
15699 staticpro (&Qhbar);
15700 Qbox = intern ("box");
15701 staticpro (&Qbox);
15702 Qhollow = intern ("hollow");
15703 staticpro (&Qhollow);
15704 Qrisky_local_variable = intern ("risky-local-variable");
15705 staticpro (&Qrisky_local_variable);
15706 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
15707 staticpro (&Qinhibit_free_realized_faces);
15708
15709 list_of_error = Fcons (intern ("error"), Qnil);
15710 staticpro (&list_of_error);
15711
15712 last_arrow_position = Qnil;
15713 last_arrow_string = Qnil;
15714 staticpro (&last_arrow_position);
15715 staticpro (&last_arrow_string);
15716
15717 echo_buffer[0] = echo_buffer[1] = Qnil;
15718 staticpro (&echo_buffer[0]);
15719 staticpro (&echo_buffer[1]);
15720
15721 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
15722 staticpro (&echo_area_buffer[0]);
15723 staticpro (&echo_area_buffer[1]);
15724
15725 Vmessages_buffer_name = build_string ("*Messages*");
15726 staticpro (&Vmessages_buffer_name);
15727
15728 mode_line_proptrans_alist = Qnil;
15729 staticpro (&mode_line_proptrans_alist);
15730
15731 mode_line_string_list = Qnil;
15732 staticpro (&mode_line_string_list);
15733
15734 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
15735 doc: /* Non-nil means highlight trailing whitespace.
15736 The face used for trailing whitespace is `trailing-whitespace'. */);
15737 Vshow_trailing_whitespace = Qnil;
15738
15739 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
15740 doc: /* Non-nil means don't actually do any redisplay.
15741 This is used for internal purposes. */);
15742 Vinhibit_redisplay = Qnil;
15743
15744 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
15745 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
15746 Vglobal_mode_string = Qnil;
15747
15748 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
15749 doc: /* Marker for where to display an arrow on top of the buffer text.
15750 This must be the beginning of a line in order to work.
15751 See also `overlay-arrow-string'. */);
15752 Voverlay_arrow_position = Qnil;
15753
15754 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
15755 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
15756 Voverlay_arrow_string = Qnil;
15757
15758 DEFVAR_INT ("scroll-step", &scroll_step,
15759 doc: /* *The number of lines to try scrolling a window by when point moves out.
15760 If that fails to bring point back on frame, point is centered instead.
15761 If this is zero, point is always centered after it moves off frame.
15762 If you want scrolling to always be a line at a time, you should set
15763 `scroll-conservatively' to a large value rather than set this to 1. */);
15764
15765 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
15766 doc: /* *Scroll up to this many lines, to bring point back on screen.
15767 A value of zero means to scroll the text to center point vertically
15768 in the window. */);
15769 scroll_conservatively = 0;
15770
15771 DEFVAR_INT ("scroll-margin", &scroll_margin,
15772 doc: /* *Number of lines of margin at the top and bottom of a window.
15773 Recenter the window whenever point gets within this many lines
15774 of the top or bottom of the window. */);
15775 scroll_margin = 0;
15776
15777 #if GLYPH_DEBUG
15778 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
15779 #endif
15780
15781 DEFVAR_BOOL ("truncate-partial-width-windows",
15782 &truncate_partial_width_windows,
15783 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
15784 truncate_partial_width_windows = 1;
15785
15786 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
15787 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
15788 Any other value means to use the appropriate face, `mode-line',
15789 `header-line', or `menu' respectively. */);
15790 mode_line_inverse_video = 1;
15791
15792 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
15793 doc: /* *Maximum buffer size for which line number should be displayed.
15794 If the buffer is bigger than this, the line number does not appear
15795 in the mode line. A value of nil means no limit. */);
15796 Vline_number_display_limit = Qnil;
15797
15798 DEFVAR_INT ("line-number-display-limit-width",
15799 &line_number_display_limit_width,
15800 doc: /* *Maximum line width (in characters) for line number display.
15801 If the average length of the lines near point is bigger than this, then the
15802 line number may be omitted from the mode line. */);
15803 line_number_display_limit_width = 200;
15804
15805 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
15806 doc: /* *Non-nil means highlight region even in nonselected windows. */);
15807 highlight_nonselected_windows = 0;
15808
15809 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
15810 doc: /* Non-nil if more than one frame is visible on this display.
15811 Minibuffer-only frames don't count, but iconified frames do.
15812 This variable is not guaranteed to be accurate except while processing
15813 `frame-title-format' and `icon-title-format'. */);
15814
15815 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
15816 doc: /* Template for displaying the title bar of visible frames.
15817 \(Assuming the window manager supports this feature.)
15818 This variable has the same structure as `mode-line-format' (which see),
15819 and is used only on frames for which no explicit name has been set
15820 \(see `modify-frame-parameters'). */);
15821 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
15822 doc: /* Template for displaying the title bar of an iconified frame.
15823 \(Assuming the window manager supports this feature.)
15824 This variable has the same structure as `mode-line-format' (which see),
15825 and is used only on frames for which no explicit name has been set
15826 \(see `modify-frame-parameters'). */);
15827 Vicon_title_format
15828 = Vframe_title_format
15829 = Fcons (intern ("multiple-frames"),
15830 Fcons (build_string ("%b"),
15831 Fcons (Fcons (empty_string,
15832 Fcons (intern ("invocation-name"),
15833 Fcons (build_string ("@"),
15834 Fcons (intern ("system-name"),
15835 Qnil)))),
15836 Qnil)));
15837
15838 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
15839 doc: /* Maximum number of lines to keep in the message log buffer.
15840 If nil, disable message logging. If t, log messages but don't truncate
15841 the buffer when it becomes large. */);
15842 Vmessage_log_max = make_number (50);
15843
15844 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
15845 doc: /* Functions called before redisplay, if window sizes have changed.
15846 The value should be a list of functions that take one argument.
15847 Just before redisplay, for each frame, if any of its windows have changed
15848 size since the last redisplay, or have been split or deleted,
15849 all the functions in the list are called, with the frame as argument. */);
15850 Vwindow_size_change_functions = Qnil;
15851
15852 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
15853 doc: /* List of Functions to call before redisplaying a window with scrolling.
15854 Each function is called with two arguments, the window
15855 and its new display-start position. Note that the value of `window-end'
15856 is not valid when these functions are called. */);
15857 Vwindow_scroll_functions = Qnil;
15858
15859 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
15860 doc: /* *Non-nil means automatically resize tool-bars.
15861 This increases a tool-bar's height if not all tool-bar items are visible.
15862 It decreases a tool-bar's height when it would display blank lines
15863 otherwise. */);
15864 auto_resize_tool_bars_p = 1;
15865
15866 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
15867 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
15868 auto_raise_tool_bar_buttons_p = 1;
15869
15870 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
15871 doc: /* *Margin around tool-bar buttons in pixels.
15872 If an integer, use that for both horizontal and vertical margins.
15873 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
15874 HORZ specifying the horizontal margin, and VERT specifying the
15875 vertical margin. */);
15876 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
15877
15878 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
15879 doc: /* *Relief thickness of tool-bar buttons. */);
15880 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
15881
15882 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
15883 doc: /* List of functions to call to fontify regions of text.
15884 Each function is called with one argument POS. Functions must
15885 fontify a region starting at POS in the current buffer, and give
15886 fontified regions the property `fontified'. */);
15887 Vfontification_functions = Qnil;
15888 Fmake_variable_buffer_local (Qfontification_functions);
15889
15890 DEFVAR_BOOL ("unibyte-display-via-language-environment",
15891 &unibyte_display_via_language_environment,
15892 doc: /* *Non-nil means display unibyte text according to language environment.
15893 Specifically this means that unibyte non-ASCII characters
15894 are displayed by converting them to the equivalent multibyte characters
15895 according to the current language environment. As a result, they are
15896 displayed according to the current fontset. */);
15897 unibyte_display_via_language_environment = 0;
15898
15899 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
15900 doc: /* *Maximum height for resizing mini-windows.
15901 If a float, it specifies a fraction of the mini-window frame's height.
15902 If an integer, it specifies a number of lines. */);
15903 Vmax_mini_window_height = make_float (0.25);
15904
15905 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
15906 doc: /* *How to resize mini-windows.
15907 A value of nil means don't automatically resize mini-windows.
15908 A value of t means resize them to fit the text displayed in them.
15909 A value of `grow-only', the default, means let mini-windows grow
15910 only, until their display becomes empty, at which point the windows
15911 go back to their normal size. */);
15912 Vresize_mini_windows = Qgrow_only;
15913
15914 DEFVAR_LISP ("cursor-in-non-selected-windows",
15915 &Vcursor_in_non_selected_windows,
15916 doc: /* *Cursor type to display in non-selected windows.
15917 t means to use hollow box cursor. See `cursor-type' for other values. */);
15918 Vcursor_in_non_selected_windows = Qt;
15919
15920 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
15921 doc: /* Alist specifying how to blink the cursor off.
15922 Each element has the form (ON-STATE . OFF-STATE). Whenever the
15923 `cursor-type' frame-parameter or variable equals ON-STATE,
15924 comparing using `equal', Emacs uses OFF-STATE to specify
15925 how to blink it off. */);
15926 Vblink_cursor_alist = Qnil;
15927
15928 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
15929 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
15930 automatic_hscrolling_p = 1;
15931
15932 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
15933 doc: /* *How many columns away from the window edge point is allowed to get
15934 before automatic hscrolling will horizontally scroll the window. */);
15935 hscroll_margin = 5;
15936
15937 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
15938 doc: /* *How many columns to scroll the window when point gets too close to the edge.
15939 When point is less than `automatic-hscroll-margin' columns from the window
15940 edge, automatic hscrolling will scroll the window by the amount of columns
15941 determined by this variable. If its value is a positive integer, scroll that
15942 many columns. If it's a positive floating-point number, it specifies the
15943 fraction of the window's width to scroll. If it's nil or zero, point will be
15944 centered horizontally after the scroll. Any other value, including negative
15945 numbers, are treated as if the value were zero.
15946
15947 Automatic hscrolling always moves point outside the scroll margin, so if
15948 point was more than scroll step columns inside the margin, the window will
15949 scroll more than the value given by the scroll step.
15950
15951 Note that the lower bound for automatic hscrolling specified by `scroll-left'
15952 and `scroll-right' overrides this variable's effect. */);
15953 Vhscroll_step = make_number (0);
15954
15955 DEFVAR_LISP ("image-types", &Vimage_types,
15956 doc: /* List of supported image types.
15957 Each element of the list is a symbol for a supported image type. */);
15958 Vimage_types = Qnil;
15959
15960 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
15961 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
15962 Bind this around calls to `message' to let it take effect. */);
15963 message_truncate_lines = 0;
15964
15965 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
15966 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
15967 Can be used to update submenus whose contents should vary. */);
15968 Vmenu_bar_update_hook = Qnil;
15969
15970 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
15971 doc: /* Non-nil means don't update menu bars. Internal use only. */);
15972 inhibit_menubar_update = 0;
15973
15974 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
15975 doc: /* Non-nil means don't eval Lisp during redisplay. */);
15976 inhibit_eval_during_redisplay = 0;
15977
15978 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
15979 doc: /* Non-nil means don't free realized faces. Internal use only. */);
15980 inhibit_free_realized_faces = 0;
15981
15982 #if GLYPH_DEBUG
15983 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
15984 doc: /* Inhibit try_window_id display optimization. */);
15985 inhibit_try_window_id = 0;
15986
15987 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
15988 doc: /* Inhibit try_window_reusing display optimization. */);
15989 inhibit_try_window_reusing = 0;
15990
15991 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
15992 doc: /* Inhibit try_cursor_movement display optimization. */);
15993 inhibit_try_cursor_movement = 0;
15994 #endif /* GLYPH_DEBUG */
15995 }
15996
15997
15998 /* Initialize this module when Emacs starts. */
15999
16000 void
16001 init_xdisp ()
16002 {
16003 Lisp_Object root_window;
16004 struct window *mini_w;
16005
16006 current_header_line_height = current_mode_line_height = -1;
16007
16008 CHARPOS (this_line_start_pos) = 0;
16009
16010 mini_w = XWINDOW (minibuf_window);
16011 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
16012
16013 if (!noninteractive)
16014 {
16015 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
16016 int i;
16017
16018 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
16019 set_window_height (root_window,
16020 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
16021 0);
16022 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
16023 set_window_height (minibuf_window, 1, 0);
16024
16025 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
16026 mini_w->width = make_number (FRAME_WIDTH (f));
16027
16028 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
16029 scratch_glyph_row.glyphs[TEXT_AREA + 1]
16030 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
16031
16032 /* The default ellipsis glyphs `...'. */
16033 for (i = 0; i < 3; ++i)
16034 default_invis_vector[i] = make_number ('.');
16035 }
16036
16037 {
16038 /* Allocate the buffer for frame titles.
16039 Also used for `format-mode-line'. */
16040 int size = 100;
16041 frame_title_buf = (char *) xmalloc (size);
16042 frame_title_buf_end = frame_title_buf + size;
16043 frame_title_ptr = NULL;
16044 }
16045
16046 help_echo_showing_p = 0;
16047 }
16048
16049