]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(start_of_data): Don't define the function if a macro
[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
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "macros.h"
183 #include "disptab.h"
184 #include "termhooks.h"
185 #include "intervals.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
189 #include "fontset.h"
190
191 #ifdef HAVE_X_WINDOWS
192 #include "xterm.h"
193 #endif
194 #ifdef WINDOWSNT
195 #include "w32term.h"
196 #endif
197 #ifdef 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 extern void set_frame_menubar P_ ((struct frame *f, int, int));
205 extern int pending_menu_activation;
206 #endif
207
208 extern int interrupt_input;
209 extern int command_loop_level;
210
211 extern int minibuffer_auto_raise;
212
213 extern Lisp_Object Qface;
214 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
215
216 extern Lisp_Object Voverriding_local_map;
217 extern Lisp_Object Voverriding_local_map_menu_flag;
218 extern Lisp_Object Qmenu_item;
219
220 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
221 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
222 Lisp_Object Qredisplay_end_trigger_functions;
223 Lisp_Object Qinhibit_point_motion_hooks;
224 Lisp_Object QCeval, Qwhen, QCfile, QCdata, QCpropertize;
225 Lisp_Object Qfontified;
226 Lisp_Object Qgrow_only;
227 Lisp_Object Qinhibit_eval_during_redisplay;
228 Lisp_Object Qbuffer_position, Qposition, Qobject;
229
230 Lisp_Object Qrisky_local_variable;
231
232 /* Holds the list (error). */
233 Lisp_Object list_of_error;
234
235 /* Functions called to fontify regions of text. */
236
237 Lisp_Object Vfontification_functions;
238 Lisp_Object Qfontification_functions;
239
240 /* Non-zero means draw tool bar buttons raised when the mouse moves
241 over them. */
242
243 int auto_raise_tool_bar_buttons_p;
244
245 /* Margin around tool bar buttons in pixels. */
246
247 Lisp_Object Vtool_bar_button_margin;
248
249 /* Thickness of shadow to draw around tool bar buttons. */
250
251 EMACS_INT tool_bar_button_relief;
252
253 /* Non-zero means automatically resize tool-bars so that all tool-bar
254 items are visible, and no blank lines remain. */
255
256 int auto_resize_tool_bars_p;
257
258 /* Non-nil means don't actually do any redisplay. */
259
260 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
261
262 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
263
264 int inhibit_eval_during_redisplay;
265
266 /* Names of text properties relevant for redisplay. */
267
268 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
269 extern Lisp_Object Qface, Qinvisible, Qwidth;
270
271 /* Symbols used in text property values. */
272
273 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
274 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
275 Lisp_Object Qmargin;
276 extern Lisp_Object Qheight;
277
278 /* Non-nil means highlight trailing whitespace. */
279
280 Lisp_Object Vshow_trailing_whitespace;
281
282 /* Name of the face used to highlight trailing whitespace. */
283
284 Lisp_Object Qtrailing_whitespace;
285
286 /* The symbol `image' which is the car of the lists used to represent
287 images in Lisp. */
288
289 Lisp_Object Qimage;
290
291 /* Non-zero means print newline to stdout before next mini-buffer
292 message. */
293
294 int noninteractive_need_newline;
295
296 /* Non-zero means print newline to message log before next message. */
297
298 static int message_log_need_newline;
299
300 /* Three markers that message_dolog uses.
301 It could allocate them itself, but that causes trouble
302 in handling memory-full errors. */
303 static Lisp_Object message_dolog_marker1;
304 static Lisp_Object message_dolog_marker2;
305 static Lisp_Object message_dolog_marker3;
306 \f
307 /* The buffer position of the first character appearing entirely or
308 partially on the line of the selected window which contains the
309 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
310 redisplay optimization in redisplay_internal. */
311
312 static struct text_pos this_line_start_pos;
313
314 /* Number of characters past the end of the line above, including the
315 terminating newline. */
316
317 static struct text_pos this_line_end_pos;
318
319 /* The vertical positions and the height of this line. */
320
321 static int this_line_vpos;
322 static int this_line_y;
323 static int this_line_pixel_height;
324
325 /* X position at which this display line starts. Usually zero;
326 negative if first character is partially visible. */
327
328 static int this_line_start_x;
329
330 /* Buffer that this_line_.* variables are referring to. */
331
332 static struct buffer *this_line_buffer;
333
334 /* Nonzero means truncate lines in all windows less wide than the
335 frame. */
336
337 int truncate_partial_width_windows;
338
339 /* A flag to control how to display unibyte 8-bit character. */
340
341 int unibyte_display_via_language_environment;
342
343 /* Nonzero means we have more than one non-mini-buffer-only frame.
344 Not guaranteed to be accurate except while parsing
345 frame-title-format. */
346
347 int multiple_frames;
348
349 Lisp_Object Vglobal_mode_string;
350
351 /* Marker for where to display an arrow on top of the buffer text. */
352
353 Lisp_Object Voverlay_arrow_position;
354
355 /* String to display for the arrow. Only used on terminal frames. */
356
357 Lisp_Object Voverlay_arrow_string;
358
359 /* Values of those variables at last redisplay. However, if
360 Voverlay_arrow_position is a marker, last_arrow_position is its
361 numerical position. */
362
363 static Lisp_Object last_arrow_position, last_arrow_string;
364
365 /* Like mode-line-format, but for the title bar on a visible frame. */
366
367 Lisp_Object Vframe_title_format;
368
369 /* Like mode-line-format, but for the title bar on an iconified frame. */
370
371 Lisp_Object Vicon_title_format;
372
373 /* List of functions to call when a window's size changes. These
374 functions get one arg, a frame on which one or more windows' sizes
375 have changed. */
376
377 static Lisp_Object Vwindow_size_change_functions;
378
379 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
380
381 /* Nonzero if overlay arrow has been displayed once in this window. */
382
383 static int overlay_arrow_seen;
384
385 /* Nonzero means highlight the region even in nonselected windows. */
386
387 int highlight_nonselected_windows;
388
389 /* If cursor motion alone moves point off frame, try scrolling this
390 many lines up or down if that will bring it back. */
391
392 static EMACS_INT scroll_step;
393
394 /* Nonzero means scroll just far enough to bring point back on the
395 screen, when appropriate. */
396
397 static EMACS_INT scroll_conservatively;
398
399 /* Recenter the window whenever point gets within this many lines of
400 the top or bottom of the window. This value is translated into a
401 pixel value by multiplying it with CANON_Y_UNIT, which means that
402 there is really a fixed pixel height scroll margin. */
403
404 EMACS_INT scroll_margin;
405
406 /* Number of windows showing the buffer of the selected window (or
407 another buffer with the same base buffer). keyboard.c refers to
408 this. */
409
410 int buffer_shared;
411
412 /* Vector containing glyphs for an ellipsis `...'. */
413
414 static Lisp_Object default_invis_vector[3];
415
416 /* Zero means display the mode-line/header-line/menu-bar in the default face
417 (this slightly odd definition is for compatibility with previous versions
418 of emacs), non-zero means display them using their respective faces.
419
420 This variable is deprecated. */
421
422 int mode_line_inverse_video;
423
424 /* Prompt to display in front of the mini-buffer contents. */
425
426 Lisp_Object minibuf_prompt;
427
428 /* Width of current mini-buffer prompt. Only set after display_line
429 of the line that contains the prompt. */
430
431 int minibuf_prompt_width;
432
433 /* This is the window where the echo area message was displayed. It
434 is always a mini-buffer window, but it may not be the same window
435 currently active as a mini-buffer. */
436
437 Lisp_Object echo_area_window;
438
439 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
440 pushes the current message and the value of
441 message_enable_multibyte on the stack, the function restore_message
442 pops the stack and displays MESSAGE again. */
443
444 Lisp_Object Vmessage_stack;
445
446 /* Nonzero means multibyte characters were enabled when the echo area
447 message was specified. */
448
449 int message_enable_multibyte;
450
451 /* Nonzero if we should redraw the mode lines on the next redisplay. */
452
453 int update_mode_lines;
454
455 /* Nonzero if window sizes or contents have changed since last
456 redisplay that finished. */
457
458 int windows_or_buffers_changed;
459
460 /* Nonzero means a frame's cursor type has been changed. */
461
462 int cursor_type_changed;
463
464 /* Nonzero after display_mode_line if %l was used and it displayed a
465 line number. */
466
467 int line_number_displayed;
468
469 /* Maximum buffer size for which to display line numbers. */
470
471 Lisp_Object Vline_number_display_limit;
472
473 /* Line width to consider when repositioning for line number display. */
474
475 static EMACS_INT line_number_display_limit_width;
476
477 /* Number of lines to keep in the message log buffer. t means
478 infinite. nil means don't log at all. */
479
480 Lisp_Object Vmessage_log_max;
481
482 /* The name of the *Messages* buffer, a string. */
483
484 static Lisp_Object Vmessages_buffer_name;
485
486 /* Current, index 0, and last displayed echo area message. Either
487 buffers from echo_buffers, or nil to indicate no message. */
488
489 Lisp_Object echo_area_buffer[2];
490
491 /* The buffers referenced from echo_area_buffer. */
492
493 static Lisp_Object echo_buffer[2];
494
495 /* A vector saved used in with_area_buffer to reduce consing. */
496
497 static Lisp_Object Vwith_echo_area_save_vector;
498
499 /* Non-zero means display_echo_area should display the last echo area
500 message again. Set by redisplay_preserve_echo_area. */
501
502 static int display_last_displayed_message_p;
503
504 /* Nonzero if echo area is being used by print; zero if being used by
505 message. */
506
507 int message_buf_print;
508
509 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
510
511 Lisp_Object Qinhibit_menubar_update;
512 int inhibit_menubar_update;
513
514 /* Maximum height for resizing mini-windows. Either a float
515 specifying a fraction of the available height, or an integer
516 specifying a number of lines. */
517
518 Lisp_Object Vmax_mini_window_height;
519
520 /* Non-zero means messages should be displayed with truncated
521 lines instead of being continued. */
522
523 int message_truncate_lines;
524 Lisp_Object Qmessage_truncate_lines;
525
526 /* Set to 1 in clear_message to make redisplay_internal aware
527 of an emptied echo area. */
528
529 static int message_cleared_p;
530
531 /* Non-zero means we want a hollow cursor in windows that are not
532 selected. Zero means there's no cursor in such windows. */
533
534 int cursor_in_non_selected_windows;
535 Lisp_Object Qcursor_in_non_selected_windows;
536
537 /* A scratch glyph row with contents used for generating truncation
538 glyphs. Also used in direct_output_for_insert. */
539
540 #define MAX_SCRATCH_GLYPHS 100
541 struct glyph_row scratch_glyph_row;
542 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
543
544 /* Ascent and height of the last line processed by move_it_to. */
545
546 static int last_max_ascent, last_height;
547
548 /* Non-zero if there's a help-echo in the echo area. */
549
550 int help_echo_showing_p;
551
552 /* If >= 0, computed, exact values of mode-line and header-line height
553 to use in the macros CURRENT_MODE_LINE_HEIGHT and
554 CURRENT_HEADER_LINE_HEIGHT. */
555
556 int current_mode_line_height, current_header_line_height;
557
558 /* The maximum distance to look ahead for text properties. Values
559 that are too small let us call compute_char_face and similar
560 functions too often which is expensive. Values that are too large
561 let us call compute_char_face and alike too often because we
562 might not be interested in text properties that far away. */
563
564 #define TEXT_PROP_DISTANCE_LIMIT 100
565
566 #if GLYPH_DEBUG
567
568 /* Variables to turn off display optimizations from Lisp. */
569
570 int inhibit_try_window_id, inhibit_try_window_reusing;
571 int inhibit_try_cursor_movement;
572
573 /* Non-zero means print traces of redisplay if compiled with
574 GLYPH_DEBUG != 0. */
575
576 int trace_redisplay_p;
577
578 #endif /* GLYPH_DEBUG */
579
580 #ifdef DEBUG_TRACE_MOVE
581 /* Non-zero means trace with TRACE_MOVE to stderr. */
582 int trace_move;
583
584 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
585 #else
586 #define TRACE_MOVE(x) (void) 0
587 #endif
588
589 /* Non-zero means automatically scroll windows horizontally to make
590 point visible. */
591
592 int automatic_hscrolling_p;
593
594 /* How close to the margin can point get before the window is scrolled
595 horizontally. */
596 EMACS_INT hscroll_margin;
597
598 /* How much to scroll horizontally when point is inside the above margin. */
599 Lisp_Object Vhscroll_step;
600
601 /* A list of symbols, one for each supported image type. */
602
603 Lisp_Object Vimage_types;
604
605 /* The variable `resize-mini-windows'. If nil, don't resize
606 mini-windows. If t, always resize them to fit the text they
607 display. If `grow-only', let mini-windows grow only until they
608 become empty. */
609
610 Lisp_Object Vresize_mini_windows;
611
612 /* Buffer being redisplayed -- for redisplay_window_error. */
613
614 struct buffer *displayed_buffer;
615
616 /* Value returned from text property handlers (see below). */
617
618 enum prop_handled
619 {
620 HANDLED_NORMALLY,
621 HANDLED_RECOMPUTE_PROPS,
622 HANDLED_OVERLAY_STRING_CONSUMED,
623 HANDLED_RETURN
624 };
625
626 /* A description of text properties that redisplay is interested
627 in. */
628
629 struct props
630 {
631 /* The name of the property. */
632 Lisp_Object *name;
633
634 /* A unique index for the property. */
635 enum prop_idx idx;
636
637 /* A handler function called to set up iterator IT from the property
638 at IT's current position. Value is used to steer handle_stop. */
639 enum prop_handled (*handler) P_ ((struct it *it));
640 };
641
642 static enum prop_handled handle_face_prop P_ ((struct it *));
643 static enum prop_handled handle_invisible_prop P_ ((struct it *));
644 static enum prop_handled handle_display_prop P_ ((struct it *));
645 static enum prop_handled handle_composition_prop P_ ((struct it *));
646 static enum prop_handled handle_overlay_change P_ ((struct it *));
647 static enum prop_handled handle_fontified_prop P_ ((struct it *));
648
649 /* Properties handled by iterators. */
650
651 static struct props it_props[] =
652 {
653 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
654 /* Handle `face' before `display' because some sub-properties of
655 `display' need to know the face. */
656 {&Qface, FACE_PROP_IDX, handle_face_prop},
657 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
658 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
659 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
660 {NULL, 0, NULL}
661 };
662
663 /* Value is the position described by X. If X is a marker, value is
664 the marker_position of X. Otherwise, value is X. */
665
666 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
667
668 /* Enumeration returned by some move_it_.* functions internally. */
669
670 enum move_it_result
671 {
672 /* Not used. Undefined value. */
673 MOVE_UNDEFINED,
674
675 /* Move ended at the requested buffer position or ZV. */
676 MOVE_POS_MATCH_OR_ZV,
677
678 /* Move ended at the requested X pixel position. */
679 MOVE_X_REACHED,
680
681 /* Move within a line ended at the end of a line that must be
682 continued. */
683 MOVE_LINE_CONTINUED,
684
685 /* Move within a line ended at the end of a line that would
686 be displayed truncated. */
687 MOVE_LINE_TRUNCATED,
688
689 /* Move within a line ended at a line end. */
690 MOVE_NEWLINE_OR_CR
691 };
692
693 /* This counter is used to clear the face cache every once in a while
694 in redisplay_internal. It is incremented for each redisplay.
695 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
696 cleared. */
697
698 #define CLEAR_FACE_CACHE_COUNT 500
699 static int clear_face_cache_count;
700
701 /* Record the previous terminal frame we displayed. */
702
703 static struct frame *previous_terminal_frame;
704
705 /* Non-zero while redisplay_internal is in progress. */
706
707 int redisplaying_p;
708
709 /* Non-zero while redisplay is updating the display. */
710
711 int redisplay_updating_p;
712
713 \f
714 /* Function prototypes. */
715
716 static void setup_for_ellipsis P_ ((struct it *));
717 static void mark_window_display_accurate_1 P_ ((struct window *, int));
718 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
719 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
720 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
721 static int redisplay_mode_lines P_ ((Lisp_Object, int));
722 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
723
724 #if 0
725 static int invisible_text_between_p P_ ((struct it *, int, int));
726 #endif
727
728 static int next_element_from_ellipsis P_ ((struct it *));
729 static void pint2str P_ ((char *, int, int));
730 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
731 struct text_pos));
732 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
733 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
734 static void store_frame_title_char P_ ((char));
735 static int store_frame_title P_ ((const unsigned char *, int, int));
736 static void x_consider_frame_title P_ ((Lisp_Object));
737 static void handle_stop P_ ((struct it *));
738 static int tool_bar_lines_needed P_ ((struct frame *));
739 static int single_display_prop_intangible_p P_ ((Lisp_Object));
740 static void ensure_echo_area_buffers P_ ((void));
741 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
742 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
743 static int with_echo_area_buffer P_ ((struct window *, int,
744 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
745 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
746 static void clear_garbaged_frames P_ ((void));
747 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
748 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
749 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
750 static int display_echo_area P_ ((struct window *));
751 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
752 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
753 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
754 static int string_char_and_length P_ ((const unsigned char *, int, int *));
755 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
756 struct text_pos));
757 static int compute_window_start_on_continuation_line P_ ((struct window *));
758 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
759 static void insert_left_trunc_glyphs P_ ((struct it *));
760 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
761 static void extend_face_to_end_of_line P_ ((struct it *));
762 static int append_space P_ ((struct it *, int));
763 static int make_cursor_line_fully_visible P_ ((struct window *));
764 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int));
765 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
766 static int trailing_whitespace_p P_ ((int));
767 static int message_log_check_duplicate P_ ((int, int, int, int));
768 static void push_it P_ ((struct it *));
769 static void pop_it P_ ((struct it *));
770 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
771 static void redisplay_internal P_ ((int));
772 static int echo_area_display P_ ((int));
773 static void redisplay_windows P_ ((Lisp_Object));
774 static void redisplay_window P_ ((Lisp_Object, int));
775 static Lisp_Object redisplay_window_error ();
776 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
777 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
778 static void update_menu_bar P_ ((struct frame *, int));
779 static int try_window_reusing_current_matrix P_ ((struct window *));
780 static int try_window_id P_ ((struct window *));
781 static int display_line P_ ((struct it *));
782 static int display_mode_lines P_ ((struct window *));
783 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
784 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
785 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
786 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
787 static void display_menu_bar P_ ((struct window *));
788 static int display_count_lines P_ ((int, int, int, int, int *));
789 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
790 int, int, struct it *, int, int, int, int));
791 static void compute_line_metrics P_ ((struct it *));
792 static void run_redisplay_end_trigger_hook P_ ((struct it *));
793 static int get_overlay_strings P_ ((struct it *, int));
794 static void next_overlay_string P_ ((struct it *));
795 static void reseat P_ ((struct it *, struct text_pos, int));
796 static void reseat_1 P_ ((struct it *, struct text_pos, int));
797 static void back_to_previous_visible_line_start P_ ((struct it *));
798 static void reseat_at_previous_visible_line_start P_ ((struct it *));
799 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
800 static int next_element_from_display_vector P_ ((struct it *));
801 static int next_element_from_string P_ ((struct it *));
802 static int next_element_from_c_string P_ ((struct it *));
803 static int next_element_from_buffer P_ ((struct it *));
804 static int next_element_from_composition P_ ((struct it *));
805 static int next_element_from_image P_ ((struct it *));
806 static int next_element_from_stretch P_ ((struct it *));
807 static void load_overlay_strings P_ ((struct it *, int));
808 static int init_from_display_pos P_ ((struct it *, struct window *,
809 struct display_pos *));
810 static void reseat_to_string P_ ((struct it *, unsigned char *,
811 Lisp_Object, int, int, int, int));
812 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
813 int, int, int));
814 void move_it_vertically_backward P_ ((struct it *, int));
815 static void init_to_row_start P_ ((struct it *, struct window *,
816 struct glyph_row *));
817 static int init_to_row_end P_ ((struct it *, struct window *,
818 struct glyph_row *));
819 static void back_to_previous_line_start P_ ((struct it *));
820 static int forward_to_next_line_start P_ ((struct it *, int *));
821 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
822 Lisp_Object, int));
823 static struct text_pos string_pos P_ ((int, Lisp_Object));
824 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
825 static int number_of_chars P_ ((unsigned char *, int));
826 static void compute_stop_pos P_ ((struct it *));
827 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
828 Lisp_Object));
829 static int face_before_or_after_it_pos P_ ((struct it *, int));
830 static int next_overlay_change P_ ((int));
831 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
832 Lisp_Object, struct text_pos *,
833 int));
834 static int underlying_face_id P_ ((struct it *));
835 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
836 struct window *));
837
838 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
839 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
840
841 #ifdef HAVE_WINDOW_SYSTEM
842
843 static void update_tool_bar P_ ((struct frame *, int));
844 static void build_desired_tool_bar_string P_ ((struct frame *f));
845 static int redisplay_tool_bar P_ ((struct frame *));
846 static void display_tool_bar_line P_ ((struct it *));
847
848 #endif /* HAVE_WINDOW_SYSTEM */
849
850 \f
851 /***********************************************************************
852 Window display dimensions
853 ***********************************************************************/
854
855 /* Return the window-relative maximum y + 1 for glyph rows displaying
856 text in window W. This is the height of W minus the height of a
857 mode line, if any. */
858
859 INLINE int
860 window_text_bottom_y (w)
861 struct window *w;
862 {
863 struct frame *f = XFRAME (w->frame);
864 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
865
866 if (WINDOW_WANTS_MODELINE_P (w))
867 height -= CURRENT_MODE_LINE_HEIGHT (w);
868 return height;
869 }
870
871
872 /* Return the pixel width of display area AREA of window W. AREA < 0
873 means return the total width of W, not including fringes to
874 the left and right of the window. */
875
876 INLINE int
877 window_box_width (w, area)
878 struct window *w;
879 int area;
880 {
881 struct frame *f = XFRAME (w->frame);
882 int width = XFASTINT (w->width);
883
884 if (!w->pseudo_window_p)
885 {
886 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
887
888 if (area == TEXT_AREA)
889 {
890 if (INTEGERP (w->left_margin_width))
891 width -= XFASTINT (w->left_margin_width);
892 if (INTEGERP (w->right_margin_width))
893 width -= XFASTINT (w->right_margin_width);
894 }
895 else if (area == LEFT_MARGIN_AREA)
896 width = (INTEGERP (w->left_margin_width)
897 ? XFASTINT (w->left_margin_width) : 0);
898 else if (area == RIGHT_MARGIN_AREA)
899 width = (INTEGERP (w->right_margin_width)
900 ? XFASTINT (w->right_margin_width) : 0);
901 }
902
903 return width * CANON_X_UNIT (f);
904 }
905
906
907 /* Return the pixel height of the display area of window W, not
908 including mode lines of W, if any. */
909
910 INLINE int
911 window_box_height (w)
912 struct window *w;
913 {
914 struct frame *f = XFRAME (w->frame);
915 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
916
917 xassert (height >= 0);
918
919 /* Note: the code below that determines the mode-line/header-line
920 height is essentially the same as that contained in the macro
921 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
922 the appropriate glyph row has its `mode_line_p' flag set,
923 and if it doesn't, uses estimate_mode_line_height instead. */
924
925 if (WINDOW_WANTS_MODELINE_P (w))
926 {
927 struct glyph_row *ml_row
928 = (w->current_matrix && w->current_matrix->rows
929 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
930 : 0);
931 if (ml_row && ml_row->mode_line_p)
932 height -= ml_row->height;
933 else
934 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
935 }
936
937 if (WINDOW_WANTS_HEADER_LINE_P (w))
938 {
939 struct glyph_row *hl_row
940 = (w->current_matrix && w->current_matrix->rows
941 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
942 : 0);
943 if (hl_row && hl_row->mode_line_p)
944 height -= hl_row->height;
945 else
946 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
947 }
948
949 /* With a very small font and a mode-line that's taller than
950 default, we might end up with a negative height. */
951 return max (0, height);
952 }
953
954
955 /* Return the frame-relative coordinate of the left edge of display
956 area AREA of window W. AREA < 0 means return the left edge of the
957 whole window, to the right of the left fringe of W. */
958
959 INLINE int
960 window_box_left (w, area)
961 struct window *w;
962 int area;
963 {
964 struct frame *f = XFRAME (w->frame);
965 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
966
967 if (!w->pseudo_window_p)
968 {
969 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
970 + FRAME_LEFT_FRINGE_WIDTH (f));
971
972 if (area == TEXT_AREA)
973 x += window_box_width (w, LEFT_MARGIN_AREA);
974 else if (area == RIGHT_MARGIN_AREA)
975 x += (window_box_width (w, LEFT_MARGIN_AREA)
976 + window_box_width (w, TEXT_AREA));
977 }
978
979 return x;
980 }
981
982
983 /* Return the frame-relative coordinate of the right edge of display
984 area AREA of window W. AREA < 0 means return the left edge of the
985 whole window, to the left of the right fringe of W. */
986
987 INLINE int
988 window_box_right (w, area)
989 struct window *w;
990 int area;
991 {
992 return window_box_left (w, area) + window_box_width (w, area);
993 }
994
995
996 /* Get the bounding box of the display area AREA of window W, without
997 mode lines, in frame-relative coordinates. AREA < 0 means the
998 whole window, not including the left and right fringes of
999 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1000 coordinates of the upper-left corner of the box. Return in
1001 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1002
1003 INLINE void
1004 window_box (w, area, box_x, box_y, box_width, box_height)
1005 struct window *w;
1006 int area;
1007 int *box_x, *box_y, *box_width, *box_height;
1008 {
1009 struct frame *f = XFRAME (w->frame);
1010
1011 *box_width = window_box_width (w, area);
1012 *box_height = window_box_height (w);
1013 *box_x = window_box_left (w, area);
1014 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
1015 + XFASTINT (w->top) * CANON_Y_UNIT (f));
1016 if (WINDOW_WANTS_HEADER_LINE_P (w))
1017 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1018 }
1019
1020
1021 /* Get the bounding box of the display area AREA of window W, without
1022 mode lines. AREA < 0 means the whole window, not including the
1023 left and right fringe of the window. Return in *TOP_LEFT_X
1024 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1025 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1026 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1027 box. */
1028
1029 INLINE void
1030 window_box_edges (w, area, top_left_x, top_left_y,
1031 bottom_right_x, bottom_right_y)
1032 struct window *w;
1033 int area;
1034 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1035 {
1036 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1037 bottom_right_y);
1038 *bottom_right_x += *top_left_x;
1039 *bottom_right_y += *top_left_y;
1040 }
1041
1042
1043 \f
1044 /***********************************************************************
1045 Utilities
1046 ***********************************************************************/
1047
1048 /* Return the bottom y-position of the line the iterator IT is in.
1049 This can modify IT's settings. */
1050
1051 int
1052 line_bottom_y (it)
1053 struct it *it;
1054 {
1055 int line_height = it->max_ascent + it->max_descent;
1056 int line_top_y = it->current_y;
1057
1058 if (line_height == 0)
1059 {
1060 if (last_height)
1061 line_height = last_height;
1062 else if (IT_CHARPOS (*it) < ZV)
1063 {
1064 move_it_by_lines (it, 1, 1);
1065 line_height = (it->max_ascent || it->max_descent
1066 ? it->max_ascent + it->max_descent
1067 : last_height);
1068 }
1069 else
1070 {
1071 struct glyph_row *row = it->glyph_row;
1072
1073 /* Use the default character height. */
1074 it->glyph_row = NULL;
1075 it->what = IT_CHARACTER;
1076 it->c = ' ';
1077 it->len = 1;
1078 PRODUCE_GLYPHS (it);
1079 line_height = it->ascent + it->descent;
1080 it->glyph_row = row;
1081 }
1082 }
1083
1084 return line_top_y + line_height;
1085 }
1086
1087
1088 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1089 1 if POS is visible and the line containing POS is fully visible.
1090 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1091 and header-lines heights. */
1092
1093 int
1094 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1095 struct window *w;
1096 int charpos, *fully, exact_mode_line_heights_p;
1097 {
1098 struct it it;
1099 struct text_pos top;
1100 int visible_p;
1101 struct buffer *old_buffer = NULL;
1102
1103 if (XBUFFER (w->buffer) != current_buffer)
1104 {
1105 old_buffer = current_buffer;
1106 set_buffer_internal_1 (XBUFFER (w->buffer));
1107 }
1108
1109 *fully = visible_p = 0;
1110 SET_TEXT_POS_FROM_MARKER (top, w->start);
1111
1112 /* Compute exact mode line heights, if requested. */
1113 if (exact_mode_line_heights_p)
1114 {
1115 if (WINDOW_WANTS_MODELINE_P (w))
1116 current_mode_line_height
1117 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1118 current_buffer->mode_line_format);
1119
1120 if (WINDOW_WANTS_HEADER_LINE_P (w))
1121 current_header_line_height
1122 = display_mode_line (w, HEADER_LINE_FACE_ID,
1123 current_buffer->header_line_format);
1124 }
1125
1126 start_display (&it, w, top);
1127 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1128 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1129
1130 /* Note that we may overshoot because of invisible text. */
1131 if (IT_CHARPOS (it) >= charpos)
1132 {
1133 int top_y = it.current_y;
1134 int bottom_y = line_bottom_y (&it);
1135 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1136
1137 if (top_y < window_top_y)
1138 visible_p = bottom_y > window_top_y;
1139 else if (top_y < it.last_visible_y)
1140 {
1141 visible_p = 1;
1142 *fully = bottom_y <= it.last_visible_y;
1143 }
1144 }
1145 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1146 {
1147 move_it_by_lines (&it, 1, 0);
1148 if (charpos < IT_CHARPOS (it))
1149 {
1150 visible_p = 1;
1151 *fully = 0;
1152 }
1153 }
1154
1155 if (old_buffer)
1156 set_buffer_internal_1 (old_buffer);
1157
1158 current_header_line_height = current_mode_line_height = -1;
1159 return visible_p;
1160 }
1161
1162
1163 /* Return the next character from STR which is MAXLEN bytes long.
1164 Return in *LEN the length of the character. This is like
1165 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1166 we find one, we return a `?', but with the length of the invalid
1167 character. */
1168
1169 static INLINE int
1170 string_char_and_length (str, maxlen, len)
1171 const unsigned char *str;
1172 int maxlen, *len;
1173 {
1174 int c;
1175
1176 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1177 if (!CHAR_VALID_P (c, 1))
1178 /* We may not change the length here because other places in Emacs
1179 don't use this function, i.e. they silently accept invalid
1180 characters. */
1181 c = '?';
1182
1183 return c;
1184 }
1185
1186
1187
1188 /* Given a position POS containing a valid character and byte position
1189 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1190
1191 static struct text_pos
1192 string_pos_nchars_ahead (pos, string, nchars)
1193 struct text_pos pos;
1194 Lisp_Object string;
1195 int nchars;
1196 {
1197 xassert (STRINGP (string) && nchars >= 0);
1198
1199 if (STRING_MULTIBYTE (string))
1200 {
1201 int rest = SBYTES (string) - BYTEPOS (pos);
1202 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1203 int len;
1204
1205 while (nchars--)
1206 {
1207 string_char_and_length (p, rest, &len);
1208 p += len, rest -= len;
1209 xassert (rest >= 0);
1210 CHARPOS (pos) += 1;
1211 BYTEPOS (pos) += len;
1212 }
1213 }
1214 else
1215 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1216
1217 return pos;
1218 }
1219
1220
1221 /* Value is the text position, i.e. character and byte position,
1222 for character position CHARPOS in STRING. */
1223
1224 static INLINE struct text_pos
1225 string_pos (charpos, string)
1226 int charpos;
1227 Lisp_Object string;
1228 {
1229 struct text_pos pos;
1230 xassert (STRINGP (string));
1231 xassert (charpos >= 0);
1232 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1233 return pos;
1234 }
1235
1236
1237 /* Value is a text position, i.e. character and byte position, for
1238 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1239 means recognize multibyte characters. */
1240
1241 static struct text_pos
1242 c_string_pos (charpos, s, multibyte_p)
1243 int charpos;
1244 unsigned char *s;
1245 int multibyte_p;
1246 {
1247 struct text_pos pos;
1248
1249 xassert (s != NULL);
1250 xassert (charpos >= 0);
1251
1252 if (multibyte_p)
1253 {
1254 int rest = strlen (s), len;
1255
1256 SET_TEXT_POS (pos, 0, 0);
1257 while (charpos--)
1258 {
1259 string_char_and_length (s, rest, &len);
1260 s += len, rest -= len;
1261 xassert (rest >= 0);
1262 CHARPOS (pos) += 1;
1263 BYTEPOS (pos) += len;
1264 }
1265 }
1266 else
1267 SET_TEXT_POS (pos, charpos, charpos);
1268
1269 return pos;
1270 }
1271
1272
1273 /* Value is the number of characters in C string S. MULTIBYTE_P
1274 non-zero means recognize multibyte characters. */
1275
1276 static int
1277 number_of_chars (s, multibyte_p)
1278 unsigned char *s;
1279 int multibyte_p;
1280 {
1281 int nchars;
1282
1283 if (multibyte_p)
1284 {
1285 int rest = strlen (s), len;
1286 unsigned char *p = (unsigned char *) s;
1287
1288 for (nchars = 0; rest > 0; ++nchars)
1289 {
1290 string_char_and_length (p, rest, &len);
1291 rest -= len, p += len;
1292 }
1293 }
1294 else
1295 nchars = strlen (s);
1296
1297 return nchars;
1298 }
1299
1300
1301 /* Compute byte position NEWPOS->bytepos corresponding to
1302 NEWPOS->charpos. POS is a known position in string STRING.
1303 NEWPOS->charpos must be >= POS.charpos. */
1304
1305 static void
1306 compute_string_pos (newpos, pos, string)
1307 struct text_pos *newpos, pos;
1308 Lisp_Object string;
1309 {
1310 xassert (STRINGP (string));
1311 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1312
1313 if (STRING_MULTIBYTE (string))
1314 *newpos = string_pos_nchars_ahead (pos, string,
1315 CHARPOS (*newpos) - CHARPOS (pos));
1316 else
1317 BYTEPOS (*newpos) = CHARPOS (*newpos);
1318 }
1319
1320
1321 \f
1322 /***********************************************************************
1323 Lisp form evaluation
1324 ***********************************************************************/
1325
1326 /* Error handler for safe_eval and safe_call. */
1327
1328 static Lisp_Object
1329 safe_eval_handler (arg)
1330 Lisp_Object arg;
1331 {
1332 add_to_log ("Error during redisplay: %s", arg, Qnil);
1333 return Qnil;
1334 }
1335
1336
1337 /* Evaluate SEXPR and return the result, or nil if something went
1338 wrong. Prevent redisplay during the evaluation. */
1339
1340 Lisp_Object
1341 safe_eval (sexpr)
1342 Lisp_Object sexpr;
1343 {
1344 Lisp_Object val;
1345
1346 if (inhibit_eval_during_redisplay)
1347 val = Qnil;
1348 else
1349 {
1350 int count = SPECPDL_INDEX ();
1351 struct gcpro gcpro1;
1352
1353 GCPRO1 (sexpr);
1354 specbind (Qinhibit_redisplay, Qt);
1355 /* Use Qt to ensure debugger does not run,
1356 so there is no possibility of wanting to redisplay. */
1357 val = internal_condition_case_1 (Feval, sexpr, Qt,
1358 safe_eval_handler);
1359 UNGCPRO;
1360 val = unbind_to (count, val);
1361 }
1362
1363 return val;
1364 }
1365
1366
1367 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1368 Return the result, or nil if something went wrong. Prevent
1369 redisplay during the evaluation. */
1370
1371 Lisp_Object
1372 safe_call (nargs, args)
1373 int nargs;
1374 Lisp_Object *args;
1375 {
1376 Lisp_Object val;
1377
1378 if (inhibit_eval_during_redisplay)
1379 val = Qnil;
1380 else
1381 {
1382 int count = SPECPDL_INDEX ();
1383 struct gcpro gcpro1;
1384
1385 GCPRO1 (args[0]);
1386 gcpro1.nvars = nargs;
1387 specbind (Qinhibit_redisplay, Qt);
1388 /* Use Qt to ensure debugger does not run,
1389 so there is no possibility of wanting to redisplay. */
1390 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1391 safe_eval_handler);
1392 UNGCPRO;
1393 val = unbind_to (count, val);
1394 }
1395
1396 return val;
1397 }
1398
1399
1400 /* Call function FN with one argument ARG.
1401 Return the result, or nil if something went wrong. */
1402
1403 Lisp_Object
1404 safe_call1 (fn, arg)
1405 Lisp_Object fn, arg;
1406 {
1407 Lisp_Object args[2];
1408 args[0] = fn;
1409 args[1] = arg;
1410 return safe_call (2, args);
1411 }
1412
1413
1414 \f
1415 /***********************************************************************
1416 Debugging
1417 ***********************************************************************/
1418
1419 #if 0
1420
1421 /* Define CHECK_IT to perform sanity checks on iterators.
1422 This is for debugging. It is too slow to do unconditionally. */
1423
1424 static void
1425 check_it (it)
1426 struct it *it;
1427 {
1428 if (it->method == next_element_from_string)
1429 {
1430 xassert (STRINGP (it->string));
1431 xassert (IT_STRING_CHARPOS (*it) >= 0);
1432 }
1433 else if (it->method == next_element_from_buffer)
1434 {
1435 /* Check that character and byte positions agree. */
1436 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1437 }
1438
1439 if (it->dpvec)
1440 xassert (it->current.dpvec_index >= 0);
1441 else
1442 xassert (it->current.dpvec_index < 0);
1443 }
1444
1445 #define CHECK_IT(IT) check_it ((IT))
1446
1447 #else /* not 0 */
1448
1449 #define CHECK_IT(IT) (void) 0
1450
1451 #endif /* not 0 */
1452
1453
1454 #if GLYPH_DEBUG
1455
1456 /* Check that the window end of window W is what we expect it
1457 to be---the last row in the current matrix displaying text. */
1458
1459 static void
1460 check_window_end (w)
1461 struct window *w;
1462 {
1463 if (!MINI_WINDOW_P (w)
1464 && !NILP (w->window_end_valid))
1465 {
1466 struct glyph_row *row;
1467 xassert ((row = MATRIX_ROW (w->current_matrix,
1468 XFASTINT (w->window_end_vpos)),
1469 !row->enabled_p
1470 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1471 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1472 }
1473 }
1474
1475 #define CHECK_WINDOW_END(W) check_window_end ((W))
1476
1477 #else /* not GLYPH_DEBUG */
1478
1479 #define CHECK_WINDOW_END(W) (void) 0
1480
1481 #endif /* not GLYPH_DEBUG */
1482
1483
1484 \f
1485 /***********************************************************************
1486 Iterator initialization
1487 ***********************************************************************/
1488
1489 /* Initialize IT for displaying current_buffer in window W, starting
1490 at character position CHARPOS. CHARPOS < 0 means that no buffer
1491 position is specified which is useful when the iterator is assigned
1492 a position later. BYTEPOS is the byte position corresponding to
1493 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1494
1495 If ROW is not null, calls to produce_glyphs with IT as parameter
1496 will produce glyphs in that row.
1497
1498 BASE_FACE_ID is the id of a base face to use. It must be one of
1499 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1500 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1501 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1502
1503 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1504 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1505 will be initialized to use the corresponding mode line glyph row of
1506 the desired matrix of W. */
1507
1508 void
1509 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1510 struct it *it;
1511 struct window *w;
1512 int charpos, bytepos;
1513 struct glyph_row *row;
1514 enum face_id base_face_id;
1515 {
1516 int highlight_region_p;
1517
1518 /* Some precondition checks. */
1519 xassert (w != NULL && it != NULL);
1520 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1521 && charpos <= ZV));
1522
1523 /* If face attributes have been changed since the last redisplay,
1524 free realized faces now because they depend on face definitions
1525 that might have changed. Don't free faces while there might be
1526 desired matrices pending which reference these faces. */
1527 if (face_change_count && !redisplay_updating_p)
1528 {
1529 face_change_count = 0;
1530 free_all_realized_faces (Qnil);
1531 }
1532
1533 /* Use one of the mode line rows of W's desired matrix if
1534 appropriate. */
1535 if (row == NULL)
1536 {
1537 if (base_face_id == MODE_LINE_FACE_ID
1538 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1539 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1540 else if (base_face_id == HEADER_LINE_FACE_ID)
1541 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1542 }
1543
1544 /* Clear IT. */
1545 bzero (it, sizeof *it);
1546 it->current.overlay_string_index = -1;
1547 it->current.dpvec_index = -1;
1548 it->base_face_id = base_face_id;
1549
1550 /* The window in which we iterate over current_buffer: */
1551 XSETWINDOW (it->window, w);
1552 it->w = w;
1553 it->f = XFRAME (w->frame);
1554
1555 /* Extra space between lines (on window systems only). */
1556 if (base_face_id == DEFAULT_FACE_ID
1557 && FRAME_WINDOW_P (it->f))
1558 {
1559 if (NATNUMP (current_buffer->extra_line_spacing))
1560 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1561 else if (it->f->extra_line_spacing > 0)
1562 it->extra_line_spacing = it->f->extra_line_spacing;
1563 }
1564
1565 /* If realized faces have been removed, e.g. because of face
1566 attribute changes of named faces, recompute them. When running
1567 in batch mode, the face cache of Vterminal_frame is null. If
1568 we happen to get called, make a dummy face cache. */
1569 if (
1570 #ifndef WINDOWSNT
1571 noninteractive &&
1572 #endif
1573 FRAME_FACE_CACHE (it->f) == NULL)
1574 init_frame_faces (it->f);
1575 if (FRAME_FACE_CACHE (it->f)->used == 0)
1576 recompute_basic_faces (it->f);
1577
1578 /* Current value of the `space-width', and 'height' properties. */
1579 it->space_width = Qnil;
1580 it->font_height = Qnil;
1581
1582 /* Are control characters displayed as `^C'? */
1583 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1584
1585 /* -1 means everything between a CR and the following line end
1586 is invisible. >0 means lines indented more than this value are
1587 invisible. */
1588 it->selective = (INTEGERP (current_buffer->selective_display)
1589 ? XFASTINT (current_buffer->selective_display)
1590 : (!NILP (current_buffer->selective_display)
1591 ? -1 : 0));
1592 it->selective_display_ellipsis_p
1593 = !NILP (current_buffer->selective_display_ellipses);
1594
1595 /* Display table to use. */
1596 it->dp = window_display_table (w);
1597
1598 /* Are multibyte characters enabled in current_buffer? */
1599 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1600
1601 /* Non-zero if we should highlight the region. */
1602 highlight_region_p
1603 = (!NILP (Vtransient_mark_mode)
1604 && !NILP (current_buffer->mark_active)
1605 && XMARKER (current_buffer->mark)->buffer != 0);
1606
1607 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1608 start and end of a visible region in window IT->w. Set both to
1609 -1 to indicate no region. */
1610 if (highlight_region_p
1611 /* Maybe highlight only in selected window. */
1612 && (/* Either show region everywhere. */
1613 highlight_nonselected_windows
1614 /* Or show region in the selected window. */
1615 || w == XWINDOW (selected_window)
1616 /* Or show the region if we are in the mini-buffer and W is
1617 the window the mini-buffer refers to. */
1618 || (MINI_WINDOW_P (XWINDOW (selected_window))
1619 && WINDOWP (minibuf_selected_window)
1620 && w == XWINDOW (minibuf_selected_window))))
1621 {
1622 int charpos = marker_position (current_buffer->mark);
1623 it->region_beg_charpos = min (PT, charpos);
1624 it->region_end_charpos = max (PT, charpos);
1625 }
1626 else
1627 it->region_beg_charpos = it->region_end_charpos = -1;
1628
1629 /* Get the position at which the redisplay_end_trigger hook should
1630 be run, if it is to be run at all. */
1631 if (MARKERP (w->redisplay_end_trigger)
1632 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1633 it->redisplay_end_trigger_charpos
1634 = marker_position (w->redisplay_end_trigger);
1635 else if (INTEGERP (w->redisplay_end_trigger))
1636 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1637
1638 /* Correct bogus values of tab_width. */
1639 it->tab_width = XINT (current_buffer->tab_width);
1640 if (it->tab_width <= 0 || it->tab_width > 1000)
1641 it->tab_width = 8;
1642
1643 /* Are lines in the display truncated? */
1644 it->truncate_lines_p
1645 = (base_face_id != DEFAULT_FACE_ID
1646 || XINT (it->w->hscroll)
1647 || (truncate_partial_width_windows
1648 && !WINDOW_FULL_WIDTH_P (it->w))
1649 || !NILP (current_buffer->truncate_lines));
1650
1651 /* Get dimensions of truncation and continuation glyphs. These are
1652 displayed as fringe bitmaps under X, so we don't need them for such
1653 frames. */
1654 if (!FRAME_WINDOW_P (it->f))
1655 {
1656 if (it->truncate_lines_p)
1657 {
1658 /* We will need the truncation glyph. */
1659 xassert (it->glyph_row == NULL);
1660 produce_special_glyphs (it, IT_TRUNCATION);
1661 it->truncation_pixel_width = it->pixel_width;
1662 }
1663 else
1664 {
1665 /* We will need the continuation glyph. */
1666 xassert (it->glyph_row == NULL);
1667 produce_special_glyphs (it, IT_CONTINUATION);
1668 it->continuation_pixel_width = it->pixel_width;
1669 }
1670
1671 /* Reset these values to zero because the produce_special_glyphs
1672 above has changed them. */
1673 it->pixel_width = it->ascent = it->descent = 0;
1674 it->phys_ascent = it->phys_descent = 0;
1675 }
1676
1677 /* Set this after getting the dimensions of truncation and
1678 continuation glyphs, so that we don't produce glyphs when calling
1679 produce_special_glyphs, above. */
1680 it->glyph_row = row;
1681 it->area = TEXT_AREA;
1682
1683 /* Get the dimensions of the display area. The display area
1684 consists of the visible window area plus a horizontally scrolled
1685 part to the left of the window. All x-values are relative to the
1686 start of this total display area. */
1687 if (base_face_id != DEFAULT_FACE_ID)
1688 {
1689 /* Mode lines, menu bar in terminal frames. */
1690 it->first_visible_x = 0;
1691 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1692 }
1693 else
1694 {
1695 it->first_visible_x
1696 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1697 it->last_visible_x = (it->first_visible_x
1698 + window_box_width (w, TEXT_AREA));
1699
1700 /* If we truncate lines, leave room for the truncator glyph(s) at
1701 the right margin. Otherwise, leave room for the continuation
1702 glyph(s). Truncation and continuation glyphs are not inserted
1703 for window-based redisplay. */
1704 if (!FRAME_WINDOW_P (it->f))
1705 {
1706 if (it->truncate_lines_p)
1707 it->last_visible_x -= it->truncation_pixel_width;
1708 else
1709 it->last_visible_x -= it->continuation_pixel_width;
1710 }
1711
1712 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1713 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1714 }
1715
1716 /* Leave room for a border glyph. */
1717 if (!FRAME_WINDOW_P (it->f)
1718 && !WINDOW_RIGHTMOST_P (it->w))
1719 it->last_visible_x -= 1;
1720
1721 it->last_visible_y = window_text_bottom_y (w);
1722
1723 /* For mode lines and alike, arrange for the first glyph having a
1724 left box line if the face specifies a box. */
1725 if (base_face_id != DEFAULT_FACE_ID)
1726 {
1727 struct face *face;
1728
1729 it->face_id = base_face_id;
1730
1731 /* If we have a boxed mode line, make the first character appear
1732 with a left box line. */
1733 face = FACE_FROM_ID (it->f, base_face_id);
1734 if (face->box != FACE_NO_BOX)
1735 it->start_of_box_run_p = 1;
1736 }
1737
1738 /* If a buffer position was specified, set the iterator there,
1739 getting overlays and face properties from that position. */
1740 if (charpos >= BUF_BEG (current_buffer))
1741 {
1742 it->end_charpos = ZV;
1743 it->face_id = -1;
1744 IT_CHARPOS (*it) = charpos;
1745
1746 /* Compute byte position if not specified. */
1747 if (bytepos < charpos)
1748 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1749 else
1750 IT_BYTEPOS (*it) = bytepos;
1751
1752 /* Compute faces etc. */
1753 reseat (it, it->current.pos, 1);
1754 }
1755
1756 CHECK_IT (it);
1757 }
1758
1759
1760 /* Initialize IT for the display of window W with window start POS. */
1761
1762 void
1763 start_display (it, w, pos)
1764 struct it *it;
1765 struct window *w;
1766 struct text_pos pos;
1767 {
1768 struct glyph_row *row;
1769 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1770
1771 row = w->desired_matrix->rows + first_vpos;
1772 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1773
1774 if (!it->truncate_lines_p)
1775 {
1776 int start_at_line_beg_p;
1777 int first_y = it->current_y;
1778
1779 /* If window start is not at a line start, skip forward to POS to
1780 get the correct continuation lines width. */
1781 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1782 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1783 if (!start_at_line_beg_p)
1784 {
1785 reseat_at_previous_visible_line_start (it);
1786 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1787
1788 /* If lines are continued, this line may end in the middle
1789 of a multi-glyph character (e.g. a control character
1790 displayed as \003, or in the middle of an overlay
1791 string). In this case move_it_to above will not have
1792 taken us to the start of the continuation line but to the
1793 end of the continued line. */
1794 if (it->current_x > 0)
1795 {
1796 if (it->current.dpvec_index >= 0
1797 || it->current.overlay_string_index >= 0)
1798 {
1799 set_iterator_to_next (it, 1);
1800 move_it_in_display_line_to (it, -1, -1, 0);
1801 }
1802
1803 it->continuation_lines_width += it->current_x;
1804 }
1805
1806 /* We're starting a new display line, not affected by the
1807 height of the continued line, so clear the appropriate
1808 fields in the iterator structure. */
1809 it->max_ascent = it->max_descent = 0;
1810 it->max_phys_ascent = it->max_phys_descent = 0;
1811
1812 it->current_y = first_y;
1813 it->vpos = 0;
1814 it->current_x = it->hpos = 0;
1815 }
1816 }
1817
1818 #if 0 /* Don't assert the following because start_display is sometimes
1819 called intentionally with a window start that is not at a
1820 line start. Please leave this code in as a comment. */
1821
1822 /* Window start should be on a line start, now. */
1823 xassert (it->continuation_lines_width
1824 || IT_CHARPOS (it) == BEGV
1825 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1826 #endif /* 0 */
1827 }
1828
1829
1830 /* Return 1 if POS is a position in ellipses displayed for invisible
1831 text. W is the window we display, for text property lookup. */
1832
1833 static int
1834 in_ellipses_for_invisible_text_p (pos, w)
1835 struct display_pos *pos;
1836 struct window *w;
1837 {
1838 Lisp_Object prop, window;
1839 int ellipses_p = 0;
1840 int charpos = CHARPOS (pos->pos);
1841
1842 /* If POS specifies a position in a display vector, this might
1843 be for an ellipsis displayed for invisible text. We won't
1844 get the iterator set up for delivering that ellipsis unless
1845 we make sure that it gets aware of the invisible text. */
1846 if (pos->dpvec_index >= 0
1847 && pos->overlay_string_index < 0
1848 && CHARPOS (pos->string_pos) < 0
1849 && charpos > BEGV
1850 && (XSETWINDOW (window, w),
1851 prop = Fget_char_property (make_number (charpos),
1852 Qinvisible, window),
1853 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1854 {
1855 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1856 window);
1857 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
1858 }
1859
1860 return ellipses_p;
1861 }
1862
1863
1864 /* Initialize IT for stepping through current_buffer in window W,
1865 starting at position POS that includes overlay string and display
1866 vector/ control character translation position information. Value
1867 is zero if there are overlay strings with newlines at POS. */
1868
1869 static int
1870 init_from_display_pos (it, w, pos)
1871 struct it *it;
1872 struct window *w;
1873 struct display_pos *pos;
1874 {
1875 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1876 int i, overlay_strings_with_newlines = 0;
1877
1878 /* If POS specifies a position in a display vector, this might
1879 be for an ellipsis displayed for invisible text. We won't
1880 get the iterator set up for delivering that ellipsis unless
1881 we make sure that it gets aware of the invisible text. */
1882 if (in_ellipses_for_invisible_text_p (pos, w))
1883 {
1884 --charpos;
1885 bytepos = 0;
1886 }
1887
1888 /* Keep in mind: the call to reseat in init_iterator skips invisible
1889 text, so we might end up at a position different from POS. This
1890 is only a problem when POS is a row start after a newline and an
1891 overlay starts there with an after-string, and the overlay has an
1892 invisible property. Since we don't skip invisible text in
1893 display_line and elsewhere immediately after consuming the
1894 newline before the row start, such a POS will not be in a string,
1895 but the call to init_iterator below will move us to the
1896 after-string. */
1897 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1898
1899 for (i = 0; i < it->n_overlay_strings; ++i)
1900 {
1901 const char *s = SDATA (it->overlay_strings[i]);
1902 const char *e = s + SBYTES (it->overlay_strings[i]);
1903
1904 while (s < e && *s != '\n')
1905 ++s;
1906
1907 if (s < e)
1908 {
1909 overlay_strings_with_newlines = 1;
1910 break;
1911 }
1912 }
1913
1914 /* If position is within an overlay string, set up IT to the right
1915 overlay string. */
1916 if (pos->overlay_string_index >= 0)
1917 {
1918 int relative_index;
1919
1920 /* If the first overlay string happens to have a `display'
1921 property for an image, the iterator will be set up for that
1922 image, and we have to undo that setup first before we can
1923 correct the overlay string index. */
1924 if (it->method == next_element_from_image)
1925 pop_it (it);
1926
1927 /* We already have the first chunk of overlay strings in
1928 IT->overlay_strings. Load more until the one for
1929 pos->overlay_string_index is in IT->overlay_strings. */
1930 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1931 {
1932 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1933 it->current.overlay_string_index = 0;
1934 while (n--)
1935 {
1936 load_overlay_strings (it, 0);
1937 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1938 }
1939 }
1940
1941 it->current.overlay_string_index = pos->overlay_string_index;
1942 relative_index = (it->current.overlay_string_index
1943 % OVERLAY_STRING_CHUNK_SIZE);
1944 it->string = it->overlay_strings[relative_index];
1945 xassert (STRINGP (it->string));
1946 it->current.string_pos = pos->string_pos;
1947 it->method = next_element_from_string;
1948 }
1949
1950 #if 0 /* This is bogus because POS not having an overlay string
1951 position does not mean it's after the string. Example: A
1952 line starting with a before-string and initialization of IT
1953 to the previous row's end position. */
1954 else if (it->current.overlay_string_index >= 0)
1955 {
1956 /* If POS says we're already after an overlay string ending at
1957 POS, make sure to pop the iterator because it will be in
1958 front of that overlay string. When POS is ZV, we've thereby
1959 also ``processed'' overlay strings at ZV. */
1960 while (it->sp)
1961 pop_it (it);
1962 it->current.overlay_string_index = -1;
1963 it->method = next_element_from_buffer;
1964 if (CHARPOS (pos->pos) == ZV)
1965 it->overlay_strings_at_end_processed_p = 1;
1966 }
1967 #endif /* 0 */
1968
1969 if (CHARPOS (pos->string_pos) >= 0)
1970 {
1971 /* Recorded position is not in an overlay string, but in another
1972 string. This can only be a string from a `display' property.
1973 IT should already be filled with that string. */
1974 it->current.string_pos = pos->string_pos;
1975 xassert (STRINGP (it->string));
1976 }
1977
1978 /* Restore position in display vector translations, control
1979 character translations or ellipses. */
1980 if (pos->dpvec_index >= 0)
1981 {
1982 if (it->dpvec == NULL)
1983 get_next_display_element (it);
1984 xassert (it->dpvec && it->current.dpvec_index == 0);
1985 it->current.dpvec_index = pos->dpvec_index;
1986 }
1987
1988 CHECK_IT (it);
1989 return !overlay_strings_with_newlines;
1990 }
1991
1992
1993 /* Initialize IT for stepping through current_buffer in window W
1994 starting at ROW->start. */
1995
1996 static void
1997 init_to_row_start (it, w, row)
1998 struct it *it;
1999 struct window *w;
2000 struct glyph_row *row;
2001 {
2002 init_from_display_pos (it, w, &row->start);
2003 it->continuation_lines_width = row->continuation_lines_width;
2004 CHECK_IT (it);
2005 }
2006
2007
2008 /* Initialize IT for stepping through current_buffer in window W
2009 starting in the line following ROW, i.e. starting at ROW->end.
2010 Value is zero if there are overlay strings with newlines at ROW's
2011 end position. */
2012
2013 static int
2014 init_to_row_end (it, w, row)
2015 struct it *it;
2016 struct window *w;
2017 struct glyph_row *row;
2018 {
2019 int success = 0;
2020
2021 if (init_from_display_pos (it, w, &row->end))
2022 {
2023 if (row->continued_p)
2024 it->continuation_lines_width
2025 = row->continuation_lines_width + row->pixel_width;
2026 CHECK_IT (it);
2027 success = 1;
2028 }
2029
2030 return success;
2031 }
2032
2033
2034
2035 \f
2036 /***********************************************************************
2037 Text properties
2038 ***********************************************************************/
2039
2040 /* Called when IT reaches IT->stop_charpos. Handle text property and
2041 overlay changes. Set IT->stop_charpos to the next position where
2042 to stop. */
2043
2044 static void
2045 handle_stop (it)
2046 struct it *it;
2047 {
2048 enum prop_handled handled;
2049 int handle_overlay_change_p = 1;
2050 struct props *p;
2051
2052 it->dpvec = NULL;
2053 it->current.dpvec_index = -1;
2054
2055 do
2056 {
2057 handled = HANDLED_NORMALLY;
2058
2059 /* Call text property handlers. */
2060 for (p = it_props; p->handler; ++p)
2061 {
2062 handled = p->handler (it);
2063
2064 if (handled == HANDLED_RECOMPUTE_PROPS)
2065 break;
2066 else if (handled == HANDLED_RETURN)
2067 return;
2068 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2069 handle_overlay_change_p = 0;
2070 }
2071
2072 if (handled != HANDLED_RECOMPUTE_PROPS)
2073 {
2074 /* Don't check for overlay strings below when set to deliver
2075 characters from a display vector. */
2076 if (it->method == next_element_from_display_vector)
2077 handle_overlay_change_p = 0;
2078
2079 /* Handle overlay changes. */
2080 if (handle_overlay_change_p)
2081 handled = handle_overlay_change (it);
2082
2083 /* Determine where to stop next. */
2084 if (handled == HANDLED_NORMALLY)
2085 compute_stop_pos (it);
2086 }
2087 }
2088 while (handled == HANDLED_RECOMPUTE_PROPS);
2089 }
2090
2091
2092 /* Compute IT->stop_charpos from text property and overlay change
2093 information for IT's current position. */
2094
2095 static void
2096 compute_stop_pos (it)
2097 struct it *it;
2098 {
2099 register INTERVAL iv, next_iv;
2100 Lisp_Object object, limit, position;
2101
2102 /* If nowhere else, stop at the end. */
2103 it->stop_charpos = it->end_charpos;
2104
2105 if (STRINGP (it->string))
2106 {
2107 /* Strings are usually short, so don't limit the search for
2108 properties. */
2109 object = it->string;
2110 limit = Qnil;
2111 position = make_number (IT_STRING_CHARPOS (*it));
2112 }
2113 else
2114 {
2115 int charpos;
2116
2117 /* If next overlay change is in front of the current stop pos
2118 (which is IT->end_charpos), stop there. Note: value of
2119 next_overlay_change is point-max if no overlay change
2120 follows. */
2121 charpos = next_overlay_change (IT_CHARPOS (*it));
2122 if (charpos < it->stop_charpos)
2123 it->stop_charpos = charpos;
2124
2125 /* If showing the region, we have to stop at the region
2126 start or end because the face might change there. */
2127 if (it->region_beg_charpos > 0)
2128 {
2129 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2130 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2131 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2132 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2133 }
2134
2135 /* Set up variables for computing the stop position from text
2136 property changes. */
2137 XSETBUFFER (object, current_buffer);
2138 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2139 position = make_number (IT_CHARPOS (*it));
2140
2141 }
2142
2143 /* Get the interval containing IT's position. Value is a null
2144 interval if there isn't such an interval. */
2145 iv = validate_interval_range (object, &position, &position, 0);
2146 if (!NULL_INTERVAL_P (iv))
2147 {
2148 Lisp_Object values_here[LAST_PROP_IDX];
2149 struct props *p;
2150
2151 /* Get properties here. */
2152 for (p = it_props; p->handler; ++p)
2153 values_here[p->idx] = textget (iv->plist, *p->name);
2154
2155 /* Look for an interval following iv that has different
2156 properties. */
2157 for (next_iv = next_interval (iv);
2158 (!NULL_INTERVAL_P (next_iv)
2159 && (NILP (limit)
2160 || XFASTINT (limit) > next_iv->position));
2161 next_iv = next_interval (next_iv))
2162 {
2163 for (p = it_props; p->handler; ++p)
2164 {
2165 Lisp_Object new_value;
2166
2167 new_value = textget (next_iv->plist, *p->name);
2168 if (!EQ (values_here[p->idx], new_value))
2169 break;
2170 }
2171
2172 if (p->handler)
2173 break;
2174 }
2175
2176 if (!NULL_INTERVAL_P (next_iv))
2177 {
2178 if (INTEGERP (limit)
2179 && next_iv->position >= XFASTINT (limit))
2180 /* No text property change up to limit. */
2181 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2182 else
2183 /* Text properties change in next_iv. */
2184 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2185 }
2186 }
2187
2188 xassert (STRINGP (it->string)
2189 || (it->stop_charpos >= BEGV
2190 && it->stop_charpos >= IT_CHARPOS (*it)));
2191 }
2192
2193
2194 /* Return the position of the next overlay change after POS in
2195 current_buffer. Value is point-max if no overlay change
2196 follows. This is like `next-overlay-change' but doesn't use
2197 xmalloc. */
2198
2199 static int
2200 next_overlay_change (pos)
2201 int pos;
2202 {
2203 int noverlays;
2204 int endpos;
2205 Lisp_Object *overlays;
2206 int len;
2207 int i;
2208
2209 /* Get all overlays at the given position. */
2210 len = 10;
2211 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2212 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2213 if (noverlays > len)
2214 {
2215 len = noverlays;
2216 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2217 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2218 }
2219
2220 /* If any of these overlays ends before endpos,
2221 use its ending point instead. */
2222 for (i = 0; i < noverlays; ++i)
2223 {
2224 Lisp_Object oend;
2225 int oendpos;
2226
2227 oend = OVERLAY_END (overlays[i]);
2228 oendpos = OVERLAY_POSITION (oend);
2229 endpos = min (endpos, oendpos);
2230 }
2231
2232 return endpos;
2233 }
2234
2235
2236 \f
2237 /***********************************************************************
2238 Fontification
2239 ***********************************************************************/
2240
2241 /* Handle changes in the `fontified' property of the current buffer by
2242 calling hook functions from Qfontification_functions to fontify
2243 regions of text. */
2244
2245 static enum prop_handled
2246 handle_fontified_prop (it)
2247 struct it *it;
2248 {
2249 Lisp_Object prop, pos;
2250 enum prop_handled handled = HANDLED_NORMALLY;
2251
2252 /* Get the value of the `fontified' property at IT's current buffer
2253 position. (The `fontified' property doesn't have a special
2254 meaning in strings.) If the value is nil, call functions from
2255 Qfontification_functions. */
2256 if (!STRINGP (it->string)
2257 && it->s == NULL
2258 && !NILP (Vfontification_functions)
2259 && !NILP (Vrun_hooks)
2260 && (pos = make_number (IT_CHARPOS (*it)),
2261 prop = Fget_char_property (pos, Qfontified, Qnil),
2262 NILP (prop)))
2263 {
2264 int count = SPECPDL_INDEX ();
2265 Lisp_Object val;
2266
2267 val = Vfontification_functions;
2268 specbind (Qfontification_functions, Qnil);
2269
2270 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2271 safe_call1 (val, pos);
2272 else
2273 {
2274 Lisp_Object globals, fn;
2275 struct gcpro gcpro1, gcpro2;
2276
2277 globals = Qnil;
2278 GCPRO2 (val, globals);
2279
2280 for (; CONSP (val); val = XCDR (val))
2281 {
2282 fn = XCAR (val);
2283
2284 if (EQ (fn, Qt))
2285 {
2286 /* A value of t indicates this hook has a local
2287 binding; it means to run the global binding too.
2288 In a global value, t should not occur. If it
2289 does, we must ignore it to avoid an endless
2290 loop. */
2291 for (globals = Fdefault_value (Qfontification_functions);
2292 CONSP (globals);
2293 globals = XCDR (globals))
2294 {
2295 fn = XCAR (globals);
2296 if (!EQ (fn, Qt))
2297 safe_call1 (fn, pos);
2298 }
2299 }
2300 else
2301 safe_call1 (fn, pos);
2302 }
2303
2304 UNGCPRO;
2305 }
2306
2307 unbind_to (count, Qnil);
2308
2309 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2310 something. This avoids an endless loop if they failed to
2311 fontify the text for which reason ever. */
2312 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2313 handled = HANDLED_RECOMPUTE_PROPS;
2314 }
2315
2316 return handled;
2317 }
2318
2319
2320 \f
2321 /***********************************************************************
2322 Faces
2323 ***********************************************************************/
2324
2325 /* Set up iterator IT from face properties at its current position.
2326 Called from handle_stop. */
2327
2328 static enum prop_handled
2329 handle_face_prop (it)
2330 struct it *it;
2331 {
2332 int new_face_id, next_stop;
2333
2334 if (!STRINGP (it->string))
2335 {
2336 new_face_id
2337 = face_at_buffer_position (it->w,
2338 IT_CHARPOS (*it),
2339 it->region_beg_charpos,
2340 it->region_end_charpos,
2341 &next_stop,
2342 (IT_CHARPOS (*it)
2343 + TEXT_PROP_DISTANCE_LIMIT),
2344 0);
2345
2346 /* Is this a start of a run of characters with box face?
2347 Caveat: this can be called for a freshly initialized
2348 iterator; face_id is -1 in this case. We know that the new
2349 face will not change until limit, i.e. if the new face has a
2350 box, all characters up to limit will have one. But, as
2351 usual, we don't know whether limit is really the end. */
2352 if (new_face_id != it->face_id)
2353 {
2354 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2355
2356 /* If new face has a box but old face has not, this is
2357 the start of a run of characters with box, i.e. it has
2358 a shadow on the left side. The value of face_id of the
2359 iterator will be -1 if this is the initial call that gets
2360 the face. In this case, we have to look in front of IT's
2361 position and see whether there is a face != new_face_id. */
2362 it->start_of_box_run_p
2363 = (new_face->box != FACE_NO_BOX
2364 && (it->face_id >= 0
2365 || IT_CHARPOS (*it) == BEG
2366 || new_face_id != face_before_it_pos (it)));
2367 it->face_box_p = new_face->box != FACE_NO_BOX;
2368 }
2369 }
2370 else
2371 {
2372 int base_face_id, bufpos;
2373
2374 if (it->current.overlay_string_index >= 0)
2375 bufpos = IT_CHARPOS (*it);
2376 else
2377 bufpos = 0;
2378
2379 /* For strings from a buffer, i.e. overlay strings or strings
2380 from a `display' property, use the face at IT's current
2381 buffer position as the base face to merge with, so that
2382 overlay strings appear in the same face as surrounding
2383 text, unless they specify their own faces. */
2384 base_face_id = underlying_face_id (it);
2385
2386 new_face_id = face_at_string_position (it->w,
2387 it->string,
2388 IT_STRING_CHARPOS (*it),
2389 bufpos,
2390 it->region_beg_charpos,
2391 it->region_end_charpos,
2392 &next_stop,
2393 base_face_id, 0);
2394
2395 #if 0 /* This shouldn't be neccessary. Let's check it. */
2396 /* If IT is used to display a mode line we would really like to
2397 use the mode line face instead of the frame's default face. */
2398 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2399 && new_face_id == DEFAULT_FACE_ID)
2400 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2401 #endif
2402
2403 /* Is this a start of a run of characters with box? Caveat:
2404 this can be called for a freshly allocated iterator; face_id
2405 is -1 is this case. We know that the new face will not
2406 change until the next check pos, i.e. if the new face has a
2407 box, all characters up to that position will have a
2408 box. But, as usual, we don't know whether that position
2409 is really the end. */
2410 if (new_face_id != it->face_id)
2411 {
2412 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2413 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2414
2415 /* If new face has a box but old face hasn't, this is the
2416 start of a run of characters with box, i.e. it has a
2417 shadow on the left side. */
2418 it->start_of_box_run_p
2419 = new_face->box && (old_face == NULL || !old_face->box);
2420 it->face_box_p = new_face->box != FACE_NO_BOX;
2421 }
2422 }
2423
2424 it->face_id = new_face_id;
2425 return HANDLED_NORMALLY;
2426 }
2427
2428
2429 /* Return the ID of the face ``underlying'' IT's current position,
2430 which is in a string. If the iterator is associated with a
2431 buffer, return the face at IT's current buffer position.
2432 Otherwise, use the iterator's base_face_id. */
2433
2434 static int
2435 underlying_face_id (it)
2436 struct it *it;
2437 {
2438 int face_id = it->base_face_id, i;
2439
2440 xassert (STRINGP (it->string));
2441
2442 for (i = it->sp - 1; i >= 0; --i)
2443 if (NILP (it->stack[i].string))
2444 face_id = it->stack[i].face_id;
2445
2446 return face_id;
2447 }
2448
2449
2450 /* Compute the face one character before or after the current position
2451 of IT. BEFORE_P non-zero means get the face in front of IT's
2452 position. Value is the id of the face. */
2453
2454 static int
2455 face_before_or_after_it_pos (it, before_p)
2456 struct it *it;
2457 int before_p;
2458 {
2459 int face_id, limit;
2460 int next_check_charpos;
2461 struct text_pos pos;
2462
2463 xassert (it->s == NULL);
2464
2465 if (STRINGP (it->string))
2466 {
2467 int bufpos, base_face_id;
2468
2469 /* No face change past the end of the string (for the case
2470 we are padding with spaces). No face change before the
2471 string start. */
2472 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2473 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2474 return it->face_id;
2475
2476 /* Set pos to the position before or after IT's current position. */
2477 if (before_p)
2478 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2479 else
2480 /* For composition, we must check the character after the
2481 composition. */
2482 pos = (it->what == IT_COMPOSITION
2483 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2484 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2485
2486 if (it->current.overlay_string_index >= 0)
2487 bufpos = IT_CHARPOS (*it);
2488 else
2489 bufpos = 0;
2490
2491 base_face_id = underlying_face_id (it);
2492
2493 /* Get the face for ASCII, or unibyte. */
2494 face_id = face_at_string_position (it->w,
2495 it->string,
2496 CHARPOS (pos),
2497 bufpos,
2498 it->region_beg_charpos,
2499 it->region_end_charpos,
2500 &next_check_charpos,
2501 base_face_id, 0);
2502
2503 /* Correct the face for charsets different from ASCII. Do it
2504 for the multibyte case only. The face returned above is
2505 suitable for unibyte text if IT->string is unibyte. */
2506 if (STRING_MULTIBYTE (it->string))
2507 {
2508 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2509 int rest = SBYTES (it->string) - BYTEPOS (pos);
2510 int c, len;
2511 struct face *face = FACE_FROM_ID (it->f, face_id);
2512
2513 c = string_char_and_length (p, rest, &len);
2514 face_id = FACE_FOR_CHAR (it->f, face, c);
2515 }
2516 }
2517 else
2518 {
2519 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2520 || (IT_CHARPOS (*it) <= BEGV && before_p))
2521 return it->face_id;
2522
2523 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2524 pos = it->current.pos;
2525
2526 if (before_p)
2527 DEC_TEXT_POS (pos, it->multibyte_p);
2528 else
2529 {
2530 if (it->what == IT_COMPOSITION)
2531 /* For composition, we must check the position after the
2532 composition. */
2533 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2534 else
2535 INC_TEXT_POS (pos, it->multibyte_p);
2536 }
2537
2538 /* Determine face for CHARSET_ASCII, or unibyte. */
2539 face_id = face_at_buffer_position (it->w,
2540 CHARPOS (pos),
2541 it->region_beg_charpos,
2542 it->region_end_charpos,
2543 &next_check_charpos,
2544 limit, 0);
2545
2546 /* Correct the face for charsets different from ASCII. Do it
2547 for the multibyte case only. The face returned above is
2548 suitable for unibyte text if current_buffer is unibyte. */
2549 if (it->multibyte_p)
2550 {
2551 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
2552 struct face *face = FACE_FROM_ID (it->f, face_id);
2553 face_id = FACE_FOR_CHAR (it->f, face, c);
2554 }
2555 }
2556
2557 return face_id;
2558 }
2559
2560
2561 \f
2562 /***********************************************************************
2563 Invisible text
2564 ***********************************************************************/
2565
2566 /* Set up iterator IT from invisible properties at its current
2567 position. Called from handle_stop. */
2568
2569 static enum prop_handled
2570 handle_invisible_prop (it)
2571 struct it *it;
2572 {
2573 enum prop_handled handled = HANDLED_NORMALLY;
2574
2575 if (STRINGP (it->string))
2576 {
2577 extern Lisp_Object Qinvisible;
2578 Lisp_Object prop, end_charpos, limit, charpos;
2579
2580 /* Get the value of the invisible text property at the
2581 current position. Value will be nil if there is no such
2582 property. */
2583 charpos = make_number (IT_STRING_CHARPOS (*it));
2584 prop = Fget_text_property (charpos, Qinvisible, it->string);
2585
2586 if (!NILP (prop)
2587 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2588 {
2589 handled = HANDLED_RECOMPUTE_PROPS;
2590
2591 /* Get the position at which the next change of the
2592 invisible text property can be found in IT->string.
2593 Value will be nil if the property value is the same for
2594 all the rest of IT->string. */
2595 XSETINT (limit, SCHARS (it->string));
2596 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2597 it->string, limit);
2598
2599 /* Text at current position is invisible. The next
2600 change in the property is at position end_charpos.
2601 Move IT's current position to that position. */
2602 if (INTEGERP (end_charpos)
2603 && XFASTINT (end_charpos) < XFASTINT (limit))
2604 {
2605 struct text_pos old;
2606 old = it->current.string_pos;
2607 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2608 compute_string_pos (&it->current.string_pos, old, it->string);
2609 }
2610 else
2611 {
2612 /* The rest of the string is invisible. If this is an
2613 overlay string, proceed with the next overlay string
2614 or whatever comes and return a character from there. */
2615 if (it->current.overlay_string_index >= 0)
2616 {
2617 next_overlay_string (it);
2618 /* Don't check for overlay strings when we just
2619 finished processing them. */
2620 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2621 }
2622 else
2623 {
2624 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
2625 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
2626 }
2627 }
2628 }
2629 }
2630 else
2631 {
2632 int invis_p, newpos, next_stop, start_charpos;
2633 Lisp_Object pos, prop, overlay;
2634
2635 /* First of all, is there invisible text at this position? */
2636 start_charpos = IT_CHARPOS (*it);
2637 pos = make_number (IT_CHARPOS (*it));
2638 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2639 &overlay);
2640 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2641
2642 /* If we are on invisible text, skip over it. */
2643 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
2644 {
2645 /* Record whether we have to display an ellipsis for the
2646 invisible text. */
2647 int display_ellipsis_p = invis_p == 2;
2648
2649 handled = HANDLED_RECOMPUTE_PROPS;
2650
2651 /* Loop skipping over invisible text. The loop is left at
2652 ZV or with IT on the first char being visible again. */
2653 do
2654 {
2655 /* Try to skip some invisible text. Return value is the
2656 position reached which can be equal to IT's position
2657 if there is nothing invisible here. This skips both
2658 over invisible text properties and overlays with
2659 invisible property. */
2660 newpos = skip_invisible (IT_CHARPOS (*it),
2661 &next_stop, ZV, it->window);
2662
2663 /* If we skipped nothing at all we weren't at invisible
2664 text in the first place. If everything to the end of
2665 the buffer was skipped, end the loop. */
2666 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2667 invis_p = 0;
2668 else
2669 {
2670 /* We skipped some characters but not necessarily
2671 all there are. Check if we ended up on visible
2672 text. Fget_char_property returns the property of
2673 the char before the given position, i.e. if we
2674 get invis_p = 0, this means that the char at
2675 newpos is visible. */
2676 pos = make_number (newpos);
2677 prop = Fget_char_property (pos, Qinvisible, it->window);
2678 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2679 }
2680
2681 /* If we ended up on invisible text, proceed to
2682 skip starting with next_stop. */
2683 if (invis_p)
2684 IT_CHARPOS (*it) = next_stop;
2685 }
2686 while (invis_p);
2687
2688 /* The position newpos is now either ZV or on visible text. */
2689 IT_CHARPOS (*it) = newpos;
2690 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2691
2692 /* If there are before-strings at the start of invisible
2693 text, and the text is invisible because of a text
2694 property, arrange to show before-strings because 20.x did
2695 it that way. (If the text is invisible because of an
2696 overlay property instead of a text property, this is
2697 already handled in the overlay code.) */
2698 if (NILP (overlay)
2699 && get_overlay_strings (it, start_charpos))
2700 {
2701 handled = HANDLED_RECOMPUTE_PROPS;
2702 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2703 }
2704 else if (display_ellipsis_p)
2705 setup_for_ellipsis (it);
2706 }
2707 }
2708
2709 return handled;
2710 }
2711
2712
2713 /* Make iterator IT return `...' next. */
2714
2715 static void
2716 setup_for_ellipsis (it)
2717 struct it *it;
2718 {
2719 if (it->dp
2720 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2721 {
2722 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2723 it->dpvec = v->contents;
2724 it->dpend = v->contents + v->size;
2725 }
2726 else
2727 {
2728 /* Default `...'. */
2729 it->dpvec = default_invis_vector;
2730 it->dpend = default_invis_vector + 3;
2731 }
2732
2733 /* The ellipsis display does not replace the display of the
2734 character at the new position. Indicate this by setting
2735 IT->dpvec_char_len to zero. */
2736 it->dpvec_char_len = 0;
2737
2738 it->current.dpvec_index = 0;
2739 it->method = next_element_from_display_vector;
2740 }
2741
2742
2743 \f
2744 /***********************************************************************
2745 'display' property
2746 ***********************************************************************/
2747
2748 /* Set up iterator IT from `display' property at its current position.
2749 Called from handle_stop. */
2750
2751 static enum prop_handled
2752 handle_display_prop (it)
2753 struct it *it;
2754 {
2755 Lisp_Object prop, object;
2756 struct text_pos *position;
2757 int display_replaced_p = 0;
2758
2759 if (STRINGP (it->string))
2760 {
2761 object = it->string;
2762 position = &it->current.string_pos;
2763 }
2764 else
2765 {
2766 object = it->w->buffer;
2767 position = &it->current.pos;
2768 }
2769
2770 /* Reset those iterator values set from display property values. */
2771 it->font_height = Qnil;
2772 it->space_width = Qnil;
2773 it->voffset = 0;
2774
2775 /* We don't support recursive `display' properties, i.e. string
2776 values that have a string `display' property, that have a string
2777 `display' property etc. */
2778 if (!it->string_from_display_prop_p)
2779 it->area = TEXT_AREA;
2780
2781 prop = Fget_char_property (make_number (position->charpos),
2782 Qdisplay, object);
2783 if (NILP (prop))
2784 return HANDLED_NORMALLY;
2785
2786 if (CONSP (prop)
2787 /* Simple properties. */
2788 && !EQ (XCAR (prop), Qimage)
2789 && !EQ (XCAR (prop), Qspace)
2790 && !EQ (XCAR (prop), Qwhen)
2791 && !EQ (XCAR (prop), Qspace_width)
2792 && !EQ (XCAR (prop), Qheight)
2793 && !EQ (XCAR (prop), Qraise)
2794 /* Marginal area specifications. */
2795 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2796 && !NILP (XCAR (prop)))
2797 {
2798 for (; CONSP (prop); prop = XCDR (prop))
2799 {
2800 if (handle_single_display_prop (it, XCAR (prop), object,
2801 position, display_replaced_p))
2802 display_replaced_p = 1;
2803 }
2804 }
2805 else if (VECTORP (prop))
2806 {
2807 int i;
2808 for (i = 0; i < ASIZE (prop); ++i)
2809 if (handle_single_display_prop (it, AREF (prop, i), object,
2810 position, display_replaced_p))
2811 display_replaced_p = 1;
2812 }
2813 else
2814 {
2815 if (handle_single_display_prop (it, prop, object, position, 0))
2816 display_replaced_p = 1;
2817 }
2818
2819 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2820 }
2821
2822
2823 /* Value is the position of the end of the `display' property starting
2824 at START_POS in OBJECT. */
2825
2826 static struct text_pos
2827 display_prop_end (it, object, start_pos)
2828 struct it *it;
2829 Lisp_Object object;
2830 struct text_pos start_pos;
2831 {
2832 Lisp_Object end;
2833 struct text_pos end_pos;
2834
2835 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2836 Qdisplay, object, Qnil);
2837 CHARPOS (end_pos) = XFASTINT (end);
2838 if (STRINGP (object))
2839 compute_string_pos (&end_pos, start_pos, it->string);
2840 else
2841 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2842
2843 return end_pos;
2844 }
2845
2846
2847 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2848 is the object in which the `display' property was found. *POSITION
2849 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2850 means that we previously saw a display sub-property which already
2851 replaced text display with something else, for example an image;
2852 ignore such properties after the first one has been processed.
2853
2854 If PROP is a `space' or `image' sub-property, set *POSITION to the
2855 end position of the `display' property.
2856
2857 Value is non-zero if something was found which replaces the display
2858 of buffer or string text. */
2859
2860 static int
2861 handle_single_display_prop (it, prop, object, position,
2862 display_replaced_before_p)
2863 struct it *it;
2864 Lisp_Object prop;
2865 Lisp_Object object;
2866 struct text_pos *position;
2867 int display_replaced_before_p;
2868 {
2869 Lisp_Object value;
2870 int replaces_text_display_p = 0;
2871 Lisp_Object form;
2872
2873 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2874 evaluated. If the result is nil, VALUE is ignored. */
2875 form = Qt;
2876 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2877 {
2878 prop = XCDR (prop);
2879 if (!CONSP (prop))
2880 return 0;
2881 form = XCAR (prop);
2882 prop = XCDR (prop);
2883 }
2884
2885 if (!NILP (form) && !EQ (form, Qt))
2886 {
2887 int count = SPECPDL_INDEX ();
2888 struct gcpro gcpro1;
2889
2890 /* Bind `object' to the object having the `display' property, a
2891 buffer or string. Bind `position' to the position in the
2892 object where the property was found, and `buffer-position'
2893 to the current position in the buffer. */
2894 specbind (Qobject, object);
2895 specbind (Qposition, make_number (CHARPOS (*position)));
2896 specbind (Qbuffer_position,
2897 make_number (STRINGP (object)
2898 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2899 GCPRO1 (form);
2900 form = safe_eval (form);
2901 UNGCPRO;
2902 unbind_to (count, Qnil);
2903 }
2904
2905 if (NILP (form))
2906 return 0;
2907
2908 if (CONSP (prop)
2909 && EQ (XCAR (prop), Qheight)
2910 && CONSP (XCDR (prop)))
2911 {
2912 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2913 return 0;
2914
2915 /* `(height HEIGHT)'. */
2916 it->font_height = XCAR (XCDR (prop));
2917 if (!NILP (it->font_height))
2918 {
2919 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2920 int new_height = -1;
2921
2922 if (CONSP (it->font_height)
2923 && (EQ (XCAR (it->font_height), Qplus)
2924 || EQ (XCAR (it->font_height), Qminus))
2925 && CONSP (XCDR (it->font_height))
2926 && INTEGERP (XCAR (XCDR (it->font_height))))
2927 {
2928 /* `(+ N)' or `(- N)' where N is an integer. */
2929 int steps = XINT (XCAR (XCDR (it->font_height)));
2930 if (EQ (XCAR (it->font_height), Qplus))
2931 steps = - steps;
2932 it->face_id = smaller_face (it->f, it->face_id, steps);
2933 }
2934 else if (FUNCTIONP (it->font_height))
2935 {
2936 /* Call function with current height as argument.
2937 Value is the new height. */
2938 Lisp_Object height;
2939 height = safe_call1 (it->font_height,
2940 face->lface[LFACE_HEIGHT_INDEX]);
2941 if (NUMBERP (height))
2942 new_height = XFLOATINT (height);
2943 }
2944 else if (NUMBERP (it->font_height))
2945 {
2946 /* Value is a multiple of the canonical char height. */
2947 struct face *face;
2948
2949 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2950 new_height = (XFLOATINT (it->font_height)
2951 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2952 }
2953 else
2954 {
2955 /* Evaluate IT->font_height with `height' bound to the
2956 current specified height to get the new height. */
2957 Lisp_Object value;
2958 int count = SPECPDL_INDEX ();
2959
2960 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2961 value = safe_eval (it->font_height);
2962 unbind_to (count, Qnil);
2963
2964 if (NUMBERP (value))
2965 new_height = XFLOATINT (value);
2966 }
2967
2968 if (new_height > 0)
2969 it->face_id = face_with_height (it->f, it->face_id, new_height);
2970 }
2971 }
2972 else if (CONSP (prop)
2973 && EQ (XCAR (prop), Qspace_width)
2974 && CONSP (XCDR (prop)))
2975 {
2976 /* `(space_width WIDTH)'. */
2977 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2978 return 0;
2979
2980 value = XCAR (XCDR (prop));
2981 if (NUMBERP (value) && XFLOATINT (value) > 0)
2982 it->space_width = value;
2983 }
2984 else if (CONSP (prop)
2985 && EQ (XCAR (prop), Qraise)
2986 && CONSP (XCDR (prop)))
2987 {
2988 /* `(raise FACTOR)'. */
2989 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2990 return 0;
2991
2992 #ifdef HAVE_WINDOW_SYSTEM
2993 value = XCAR (XCDR (prop));
2994 if (NUMBERP (value))
2995 {
2996 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2997 it->voffset = - (XFLOATINT (value)
2998 * (FONT_HEIGHT (face->font)));
2999 }
3000 #endif /* HAVE_WINDOW_SYSTEM */
3001 }
3002 else if (!it->string_from_display_prop_p)
3003 {
3004 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3005 VALUE) or `((margin nil) VALUE)' or VALUE. */
3006 Lisp_Object location, value;
3007 struct text_pos start_pos;
3008 int valid_p;
3009
3010 /* Characters having this form of property are not displayed, so
3011 we have to find the end of the property. */
3012 start_pos = *position;
3013 *position = display_prop_end (it, object, start_pos);
3014 value = Qnil;
3015
3016 /* Let's stop at the new position and assume that all
3017 text properties change there. */
3018 it->stop_charpos = position->charpos;
3019
3020 location = Qunbound;
3021 if (CONSP (prop) && CONSP (XCAR (prop)))
3022 {
3023 Lisp_Object tem;
3024
3025 value = XCDR (prop);
3026 if (CONSP (value))
3027 value = XCAR (value);
3028
3029 tem = XCAR (prop);
3030 if (EQ (XCAR (tem), Qmargin)
3031 && (tem = XCDR (tem),
3032 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3033 (NILP (tem)
3034 || EQ (tem, Qleft_margin)
3035 || EQ (tem, Qright_margin))))
3036 location = tem;
3037 }
3038
3039 if (EQ (location, Qunbound))
3040 {
3041 location = Qnil;
3042 value = prop;
3043 }
3044
3045 #ifdef HAVE_WINDOW_SYSTEM
3046 if (FRAME_TERMCAP_P (it->f))
3047 valid_p = STRINGP (value);
3048 else
3049 valid_p = (STRINGP (value)
3050 || (CONSP (value) && EQ (XCAR (value), Qspace))
3051 || valid_image_p (value));
3052 #else /* not HAVE_WINDOW_SYSTEM */
3053 valid_p = STRINGP (value);
3054 #endif /* not HAVE_WINDOW_SYSTEM */
3055
3056 if ((EQ (location, Qleft_margin)
3057 || EQ (location, Qright_margin)
3058 || NILP (location))
3059 && valid_p
3060 && !display_replaced_before_p)
3061 {
3062 replaces_text_display_p = 1;
3063
3064 /* Save current settings of IT so that we can restore them
3065 when we are finished with the glyph property value. */
3066 push_it (it);
3067
3068 if (NILP (location))
3069 it->area = TEXT_AREA;
3070 else if (EQ (location, Qleft_margin))
3071 it->area = LEFT_MARGIN_AREA;
3072 else
3073 it->area = RIGHT_MARGIN_AREA;
3074
3075 if (STRINGP (value))
3076 {
3077 it->string = value;
3078 it->multibyte_p = STRING_MULTIBYTE (it->string);
3079 it->current.overlay_string_index = -1;
3080 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3081 it->end_charpos = it->string_nchars = SCHARS (it->string);
3082 it->method = next_element_from_string;
3083 it->stop_charpos = 0;
3084 it->string_from_display_prop_p = 1;
3085 /* Say that we haven't consumed the characters with
3086 `display' property yet. The call to pop_it in
3087 set_iterator_to_next will clean this up. */
3088 *position = start_pos;
3089 }
3090 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3091 {
3092 it->method = next_element_from_stretch;
3093 it->object = value;
3094 it->current.pos = it->position = start_pos;
3095 }
3096 #ifdef HAVE_WINDOW_SYSTEM
3097 else
3098 {
3099 it->what = IT_IMAGE;
3100 it->image_id = lookup_image (it->f, value);
3101 it->position = start_pos;
3102 it->object = NILP (object) ? it->w->buffer : object;
3103 it->method = next_element_from_image;
3104
3105 /* Say that we haven't consumed the characters with
3106 `display' property yet. The call to pop_it in
3107 set_iterator_to_next will clean this up. */
3108 *position = start_pos;
3109 }
3110 #endif /* HAVE_WINDOW_SYSTEM */
3111 }
3112 else
3113 /* Invalid property or property not supported. Restore
3114 the position to what it was before. */
3115 *position = start_pos;
3116 }
3117
3118 return replaces_text_display_p;
3119 }
3120
3121
3122 /* Check if PROP is a display sub-property value whose text should be
3123 treated as intangible. */
3124
3125 static int
3126 single_display_prop_intangible_p (prop)
3127 Lisp_Object prop;
3128 {
3129 /* Skip over `when FORM'. */
3130 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3131 {
3132 prop = XCDR (prop);
3133 if (!CONSP (prop))
3134 return 0;
3135 prop = XCDR (prop);
3136 }
3137
3138 if (!CONSP (prop))
3139 return 0;
3140
3141 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3142 we don't need to treat text as intangible. */
3143 if (EQ (XCAR (prop), Qmargin))
3144 {
3145 prop = XCDR (prop);
3146 if (!CONSP (prop))
3147 return 0;
3148
3149 prop = XCDR (prop);
3150 if (!CONSP (prop)
3151 || EQ (XCAR (prop), Qleft_margin)
3152 || EQ (XCAR (prop), Qright_margin))
3153 return 0;
3154 }
3155
3156 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3157 }
3158
3159
3160 /* Check if PROP is a display property value whose text should be
3161 treated as intangible. */
3162
3163 int
3164 display_prop_intangible_p (prop)
3165 Lisp_Object prop;
3166 {
3167 if (CONSP (prop)
3168 && CONSP (XCAR (prop))
3169 && !EQ (Qmargin, XCAR (XCAR (prop))))
3170 {
3171 /* A list of sub-properties. */
3172 while (CONSP (prop))
3173 {
3174 if (single_display_prop_intangible_p (XCAR (prop)))
3175 return 1;
3176 prop = XCDR (prop);
3177 }
3178 }
3179 else if (VECTORP (prop))
3180 {
3181 /* A vector of sub-properties. */
3182 int i;
3183 for (i = 0; i < ASIZE (prop); ++i)
3184 if (single_display_prop_intangible_p (AREF (prop, i)))
3185 return 1;
3186 }
3187 else
3188 return single_display_prop_intangible_p (prop);
3189
3190 return 0;
3191 }
3192
3193
3194 /* Return 1 if PROP is a display sub-property value containing STRING. */
3195
3196 static int
3197 single_display_prop_string_p (prop, string)
3198 Lisp_Object prop, string;
3199 {
3200 if (EQ (string, prop))
3201 return 1;
3202
3203 /* Skip over `when FORM'. */
3204 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3205 {
3206 prop = XCDR (prop);
3207 if (!CONSP (prop))
3208 return 0;
3209 prop = XCDR (prop);
3210 }
3211
3212 if (CONSP (prop))
3213 /* Skip over `margin LOCATION'. */
3214 if (EQ (XCAR (prop), Qmargin))
3215 {
3216 prop = XCDR (prop);
3217 if (!CONSP (prop))
3218 return 0;
3219
3220 prop = XCDR (prop);
3221 if (!CONSP (prop))
3222 return 0;
3223 }
3224
3225 return CONSP (prop) && EQ (XCAR (prop), string);
3226 }
3227
3228
3229 /* Return 1 if STRING appears in the `display' property PROP. */
3230
3231 static int
3232 display_prop_string_p (prop, string)
3233 Lisp_Object prop, string;
3234 {
3235 if (CONSP (prop)
3236 && CONSP (XCAR (prop))
3237 && !EQ (Qmargin, XCAR (XCAR (prop))))
3238 {
3239 /* A list of sub-properties. */
3240 while (CONSP (prop))
3241 {
3242 if (single_display_prop_string_p (XCAR (prop), string))
3243 return 1;
3244 prop = XCDR (prop);
3245 }
3246 }
3247 else if (VECTORP (prop))
3248 {
3249 /* A vector of sub-properties. */
3250 int i;
3251 for (i = 0; i < ASIZE (prop); ++i)
3252 if (single_display_prop_string_p (AREF (prop, i), string))
3253 return 1;
3254 }
3255 else
3256 return single_display_prop_string_p (prop, string);
3257
3258 return 0;
3259 }
3260
3261
3262 /* Determine from which buffer position in W's buffer STRING comes
3263 from. AROUND_CHARPOS is an approximate position where it could
3264 be from. Value is the buffer position or 0 if it couldn't be
3265 determined.
3266
3267 W's buffer must be current.
3268
3269 This function is necessary because we don't record buffer positions
3270 in glyphs generated from strings (to keep struct glyph small).
3271 This function may only use code that doesn't eval because it is
3272 called asynchronously from note_mouse_highlight. */
3273
3274 int
3275 string_buffer_position (w, string, around_charpos)
3276 struct window *w;
3277 Lisp_Object string;
3278 int around_charpos;
3279 {
3280 Lisp_Object limit, prop, pos;
3281 const int MAX_DISTANCE = 1000;
3282 int found = 0;
3283
3284 pos = make_number (around_charpos);
3285 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3286 while (!found && !EQ (pos, limit))
3287 {
3288 prop = Fget_char_property (pos, Qdisplay, Qnil);
3289 if (!NILP (prop) && display_prop_string_p (prop, string))
3290 found = 1;
3291 else
3292 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3293 }
3294
3295 if (!found)
3296 {
3297 pos = make_number (around_charpos);
3298 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3299 while (!found && !EQ (pos, limit))
3300 {
3301 prop = Fget_char_property (pos, Qdisplay, Qnil);
3302 if (!NILP (prop) && display_prop_string_p (prop, string))
3303 found = 1;
3304 else
3305 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3306 limit);
3307 }
3308 }
3309
3310 return found ? XINT (pos) : 0;
3311 }
3312
3313
3314 \f
3315 /***********************************************************************
3316 `composition' property
3317 ***********************************************************************/
3318
3319 /* Set up iterator IT from `composition' property at its current
3320 position. Called from handle_stop. */
3321
3322 static enum prop_handled
3323 handle_composition_prop (it)
3324 struct it *it;
3325 {
3326 Lisp_Object prop, string;
3327 int pos, pos_byte, end;
3328 enum prop_handled handled = HANDLED_NORMALLY;
3329
3330 if (STRINGP (it->string))
3331 {
3332 pos = IT_STRING_CHARPOS (*it);
3333 pos_byte = IT_STRING_BYTEPOS (*it);
3334 string = it->string;
3335 }
3336 else
3337 {
3338 pos = IT_CHARPOS (*it);
3339 pos_byte = IT_BYTEPOS (*it);
3340 string = Qnil;
3341 }
3342
3343 /* If there's a valid composition and point is not inside of the
3344 composition (in the case that the composition is from the current
3345 buffer), draw a glyph composed from the composition components. */
3346 if (find_composition (pos, -1, &pos, &end, &prop, string)
3347 && COMPOSITION_VALID_P (pos, end, prop)
3348 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3349 {
3350 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3351
3352 if (id >= 0)
3353 {
3354 it->method = next_element_from_composition;
3355 it->cmp_id = id;
3356 it->cmp_len = COMPOSITION_LENGTH (prop);
3357 /* For a terminal, draw only the first character of the
3358 components. */
3359 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3360 it->len = (STRINGP (it->string)
3361 ? string_char_to_byte (it->string, end)
3362 : CHAR_TO_BYTE (end)) - pos_byte;
3363 it->stop_charpos = end;
3364 handled = HANDLED_RETURN;
3365 }
3366 }
3367
3368 return handled;
3369 }
3370
3371
3372 \f
3373 /***********************************************************************
3374 Overlay strings
3375 ***********************************************************************/
3376
3377 /* The following structure is used to record overlay strings for
3378 later sorting in load_overlay_strings. */
3379
3380 struct overlay_entry
3381 {
3382 Lisp_Object overlay;
3383 Lisp_Object string;
3384 int priority;
3385 int after_string_p;
3386 };
3387
3388
3389 /* Set up iterator IT from overlay strings at its current position.
3390 Called from handle_stop. */
3391
3392 static enum prop_handled
3393 handle_overlay_change (it)
3394 struct it *it;
3395 {
3396 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3397 return HANDLED_RECOMPUTE_PROPS;
3398 else
3399 return HANDLED_NORMALLY;
3400 }
3401
3402
3403 /* Set up the next overlay string for delivery by IT, if there is an
3404 overlay string to deliver. Called by set_iterator_to_next when the
3405 end of the current overlay string is reached. If there are more
3406 overlay strings to display, IT->string and
3407 IT->current.overlay_string_index are set appropriately here.
3408 Otherwise IT->string is set to nil. */
3409
3410 static void
3411 next_overlay_string (it)
3412 struct it *it;
3413 {
3414 ++it->current.overlay_string_index;
3415 if (it->current.overlay_string_index == it->n_overlay_strings)
3416 {
3417 /* No more overlay strings. Restore IT's settings to what
3418 they were before overlay strings were processed, and
3419 continue to deliver from current_buffer. */
3420 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3421
3422 pop_it (it);
3423 xassert (it->stop_charpos >= BEGV
3424 && it->stop_charpos <= it->end_charpos);
3425 it->string = Qnil;
3426 it->current.overlay_string_index = -1;
3427 SET_TEXT_POS (it->current.string_pos, -1, -1);
3428 it->n_overlay_strings = 0;
3429 it->method = next_element_from_buffer;
3430
3431 /* If we're at the end of the buffer, record that we have
3432 processed the overlay strings there already, so that
3433 next_element_from_buffer doesn't try it again. */
3434 if (IT_CHARPOS (*it) >= it->end_charpos)
3435 it->overlay_strings_at_end_processed_p = 1;
3436
3437 /* If we have to display `...' for invisible text, set
3438 the iterator up for that. */
3439 if (display_ellipsis_p)
3440 setup_for_ellipsis (it);
3441 }
3442 else
3443 {
3444 /* There are more overlay strings to process. If
3445 IT->current.overlay_string_index has advanced to a position
3446 where we must load IT->overlay_strings with more strings, do
3447 it. */
3448 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3449
3450 if (it->current.overlay_string_index && i == 0)
3451 load_overlay_strings (it, 0);
3452
3453 /* Initialize IT to deliver display elements from the overlay
3454 string. */
3455 it->string = it->overlay_strings[i];
3456 it->multibyte_p = STRING_MULTIBYTE (it->string);
3457 SET_TEXT_POS (it->current.string_pos, 0, 0);
3458 it->method = next_element_from_string;
3459 it->stop_charpos = 0;
3460 }
3461
3462 CHECK_IT (it);
3463 }
3464
3465
3466 /* Compare two overlay_entry structures E1 and E2. Used as a
3467 comparison function for qsort in load_overlay_strings. Overlay
3468 strings for the same position are sorted so that
3469
3470 1. All after-strings come in front of before-strings, except
3471 when they come from the same overlay.
3472
3473 2. Within after-strings, strings are sorted so that overlay strings
3474 from overlays with higher priorities come first.
3475
3476 2. Within before-strings, strings are sorted so that overlay
3477 strings from overlays with higher priorities come last.
3478
3479 Value is analogous to strcmp. */
3480
3481
3482 static int
3483 compare_overlay_entries (e1, e2)
3484 void *e1, *e2;
3485 {
3486 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3487 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3488 int result;
3489
3490 if (entry1->after_string_p != entry2->after_string_p)
3491 {
3492 /* Let after-strings appear in front of before-strings if
3493 they come from different overlays. */
3494 if (EQ (entry1->overlay, entry2->overlay))
3495 result = entry1->after_string_p ? 1 : -1;
3496 else
3497 result = entry1->after_string_p ? -1 : 1;
3498 }
3499 else if (entry1->after_string_p)
3500 /* After-strings sorted in order of decreasing priority. */
3501 result = entry2->priority - entry1->priority;
3502 else
3503 /* Before-strings sorted in order of increasing priority. */
3504 result = entry1->priority - entry2->priority;
3505
3506 return result;
3507 }
3508
3509
3510 /* Load the vector IT->overlay_strings with overlay strings from IT's
3511 current buffer position, or from CHARPOS if that is > 0. Set
3512 IT->n_overlays to the total number of overlay strings found.
3513
3514 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3515 a time. On entry into load_overlay_strings,
3516 IT->current.overlay_string_index gives the number of overlay
3517 strings that have already been loaded by previous calls to this
3518 function.
3519
3520 IT->add_overlay_start contains an additional overlay start
3521 position to consider for taking overlay strings from, if non-zero.
3522 This position comes into play when the overlay has an `invisible'
3523 property, and both before and after-strings. When we've skipped to
3524 the end of the overlay, because of its `invisible' property, we
3525 nevertheless want its before-string to appear.
3526 IT->add_overlay_start will contain the overlay start position
3527 in this case.
3528
3529 Overlay strings are sorted so that after-string strings come in
3530 front of before-string strings. Within before and after-strings,
3531 strings are sorted by overlay priority. See also function
3532 compare_overlay_entries. */
3533
3534 static void
3535 load_overlay_strings (it, charpos)
3536 struct it *it;
3537 int charpos;
3538 {
3539 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3540 Lisp_Object ov, overlay, window, str, invisible;
3541 int start, end;
3542 int size = 20;
3543 int n = 0, i, j, invis_p;
3544 struct overlay_entry *entries
3545 = (struct overlay_entry *) alloca (size * sizeof *entries);
3546
3547 if (charpos <= 0)
3548 charpos = IT_CHARPOS (*it);
3549
3550 /* Append the overlay string STRING of overlay OVERLAY to vector
3551 `entries' which has size `size' and currently contains `n'
3552 elements. AFTER_P non-zero means STRING is an after-string of
3553 OVERLAY. */
3554 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3555 do \
3556 { \
3557 Lisp_Object priority; \
3558 \
3559 if (n == size) \
3560 { \
3561 int new_size = 2 * size; \
3562 struct overlay_entry *old = entries; \
3563 entries = \
3564 (struct overlay_entry *) alloca (new_size \
3565 * sizeof *entries); \
3566 bcopy (old, entries, size * sizeof *entries); \
3567 size = new_size; \
3568 } \
3569 \
3570 entries[n].string = (STRING); \
3571 entries[n].overlay = (OVERLAY); \
3572 priority = Foverlay_get ((OVERLAY), Qpriority); \
3573 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3574 entries[n].after_string_p = (AFTER_P); \
3575 ++n; \
3576 } \
3577 while (0)
3578
3579 /* Process overlay before the overlay center. */
3580 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3581 {
3582 overlay = XCAR (ov);
3583 xassert (OVERLAYP (overlay));
3584 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3585 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3586
3587 if (end < charpos)
3588 break;
3589
3590 /* Skip this overlay if it doesn't start or end at IT's current
3591 position. */
3592 if (end != charpos && start != charpos)
3593 continue;
3594
3595 /* Skip this overlay if it doesn't apply to IT->w. */
3596 window = Foverlay_get (overlay, Qwindow);
3597 if (WINDOWP (window) && XWINDOW (window) != it->w)
3598 continue;
3599
3600 /* If the text ``under'' the overlay is invisible, both before-
3601 and after-strings from this overlay are visible; start and
3602 end position are indistinguishable. */
3603 invisible = Foverlay_get (overlay, Qinvisible);
3604 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3605
3606 /* If overlay has a non-empty before-string, record it. */
3607 if ((start == charpos || (end == charpos && invis_p))
3608 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3609 && SCHARS (str))
3610 RECORD_OVERLAY_STRING (overlay, str, 0);
3611
3612 /* If overlay has a non-empty after-string, record it. */
3613 if ((end == charpos || (start == charpos && invis_p))
3614 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3615 && SCHARS (str))
3616 RECORD_OVERLAY_STRING (overlay, str, 1);
3617 }
3618
3619 /* Process overlays after the overlay center. */
3620 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3621 {
3622 overlay = XCAR (ov);
3623 xassert (OVERLAYP (overlay));
3624 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3625 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3626
3627 if (start > charpos)
3628 break;
3629
3630 /* Skip this overlay if it doesn't start or end at IT's current
3631 position. */
3632 if (end != charpos && start != charpos)
3633 continue;
3634
3635 /* Skip this overlay if it doesn't apply to IT->w. */
3636 window = Foverlay_get (overlay, Qwindow);
3637 if (WINDOWP (window) && XWINDOW (window) != it->w)
3638 continue;
3639
3640 /* If the text ``under'' the overlay is invisible, it has a zero
3641 dimension, and both before- and after-strings apply. */
3642 invisible = Foverlay_get (overlay, Qinvisible);
3643 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3644
3645 /* If overlay has a non-empty before-string, record it. */
3646 if ((start == charpos || (end == charpos && invis_p))
3647 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3648 && SCHARS (str))
3649 RECORD_OVERLAY_STRING (overlay, str, 0);
3650
3651 /* If overlay has a non-empty after-string, record it. */
3652 if ((end == charpos || (start == charpos && invis_p))
3653 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3654 && SCHARS (str))
3655 RECORD_OVERLAY_STRING (overlay, str, 1);
3656 }
3657
3658 #undef RECORD_OVERLAY_STRING
3659
3660 /* Sort entries. */
3661 if (n > 1)
3662 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3663
3664 /* Record the total number of strings to process. */
3665 it->n_overlay_strings = n;
3666
3667 /* IT->current.overlay_string_index is the number of overlay strings
3668 that have already been consumed by IT. Copy some of the
3669 remaining overlay strings to IT->overlay_strings. */
3670 i = 0;
3671 j = it->current.overlay_string_index;
3672 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3673 it->overlay_strings[i++] = entries[j++].string;
3674
3675 CHECK_IT (it);
3676 }
3677
3678
3679 /* Get the first chunk of overlay strings at IT's current buffer
3680 position, or at CHARPOS if that is > 0. Value is non-zero if at
3681 least one overlay string was found. */
3682
3683 static int
3684 get_overlay_strings (it, charpos)
3685 struct it *it;
3686 int charpos;
3687 {
3688 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3689 process. This fills IT->overlay_strings with strings, and sets
3690 IT->n_overlay_strings to the total number of strings to process.
3691 IT->pos.overlay_string_index has to be set temporarily to zero
3692 because load_overlay_strings needs this; it must be set to -1
3693 when no overlay strings are found because a zero value would
3694 indicate a position in the first overlay string. */
3695 it->current.overlay_string_index = 0;
3696 load_overlay_strings (it, charpos);
3697
3698 /* If we found overlay strings, set up IT to deliver display
3699 elements from the first one. Otherwise set up IT to deliver
3700 from current_buffer. */
3701 if (it->n_overlay_strings)
3702 {
3703 /* Make sure we know settings in current_buffer, so that we can
3704 restore meaningful values when we're done with the overlay
3705 strings. */
3706 compute_stop_pos (it);
3707 xassert (it->face_id >= 0);
3708
3709 /* Save IT's settings. They are restored after all overlay
3710 strings have been processed. */
3711 xassert (it->sp == 0);
3712 push_it (it);
3713
3714 /* Set up IT to deliver display elements from the first overlay
3715 string. */
3716 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3717 it->string = it->overlay_strings[0];
3718 it->stop_charpos = 0;
3719 xassert (STRINGP (it->string));
3720 it->end_charpos = SCHARS (it->string);
3721 it->multibyte_p = STRING_MULTIBYTE (it->string);
3722 it->method = next_element_from_string;
3723 }
3724 else
3725 {
3726 it->string = Qnil;
3727 it->current.overlay_string_index = -1;
3728 it->method = next_element_from_buffer;
3729 }
3730
3731 CHECK_IT (it);
3732
3733 /* Value is non-zero if we found at least one overlay string. */
3734 return STRINGP (it->string);
3735 }
3736
3737
3738 \f
3739 /***********************************************************************
3740 Saving and restoring state
3741 ***********************************************************************/
3742
3743 /* Save current settings of IT on IT->stack. Called, for example,
3744 before setting up IT for an overlay string, to be able to restore
3745 IT's settings to what they were after the overlay string has been
3746 processed. */
3747
3748 static void
3749 push_it (it)
3750 struct it *it;
3751 {
3752 struct iterator_stack_entry *p;
3753
3754 xassert (it->sp < 2);
3755 p = it->stack + it->sp;
3756
3757 p->stop_charpos = it->stop_charpos;
3758 xassert (it->face_id >= 0);
3759 p->face_id = it->face_id;
3760 p->string = it->string;
3761 p->pos = it->current;
3762 p->end_charpos = it->end_charpos;
3763 p->string_nchars = it->string_nchars;
3764 p->area = it->area;
3765 p->multibyte_p = it->multibyte_p;
3766 p->space_width = it->space_width;
3767 p->font_height = it->font_height;
3768 p->voffset = it->voffset;
3769 p->string_from_display_prop_p = it->string_from_display_prop_p;
3770 p->display_ellipsis_p = 0;
3771 ++it->sp;
3772 }
3773
3774
3775 /* Restore IT's settings from IT->stack. Called, for example, when no
3776 more overlay strings must be processed, and we return to delivering
3777 display elements from a buffer, or when the end of a string from a
3778 `display' property is reached and we return to delivering display
3779 elements from an overlay string, or from a buffer. */
3780
3781 static void
3782 pop_it (it)
3783 struct it *it;
3784 {
3785 struct iterator_stack_entry *p;
3786
3787 xassert (it->sp > 0);
3788 --it->sp;
3789 p = it->stack + it->sp;
3790 it->stop_charpos = p->stop_charpos;
3791 it->face_id = p->face_id;
3792 it->string = p->string;
3793 it->current = p->pos;
3794 it->end_charpos = p->end_charpos;
3795 it->string_nchars = p->string_nchars;
3796 it->area = p->area;
3797 it->multibyte_p = p->multibyte_p;
3798 it->space_width = p->space_width;
3799 it->font_height = p->font_height;
3800 it->voffset = p->voffset;
3801 it->string_from_display_prop_p = p->string_from_display_prop_p;
3802 }
3803
3804
3805 \f
3806 /***********************************************************************
3807 Moving over lines
3808 ***********************************************************************/
3809
3810 /* Set IT's current position to the previous line start. */
3811
3812 static void
3813 back_to_previous_line_start (it)
3814 struct it *it;
3815 {
3816 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3817 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3818 }
3819
3820
3821 /* Move IT to the next line start.
3822
3823 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3824 we skipped over part of the text (as opposed to moving the iterator
3825 continuously over the text). Otherwise, don't change the value
3826 of *SKIPPED_P.
3827
3828 Newlines may come from buffer text, overlay strings, or strings
3829 displayed via the `display' property. That's the reason we can't
3830 simply use find_next_newline_no_quit.
3831
3832 Note that this function may not skip over invisible text that is so
3833 because of text properties and immediately follows a newline. If
3834 it would, function reseat_at_next_visible_line_start, when called
3835 from set_iterator_to_next, would effectively make invisible
3836 characters following a newline part of the wrong glyph row, which
3837 leads to wrong cursor motion. */
3838
3839 static int
3840 forward_to_next_line_start (it, skipped_p)
3841 struct it *it;
3842 int *skipped_p;
3843 {
3844 int old_selective, newline_found_p, n;
3845 const int MAX_NEWLINE_DISTANCE = 500;
3846
3847 /* If already on a newline, just consume it to avoid unintended
3848 skipping over invisible text below. */
3849 if (it->what == IT_CHARACTER
3850 && it->c == '\n'
3851 && CHARPOS (it->position) == IT_CHARPOS (*it))
3852 {
3853 set_iterator_to_next (it, 0);
3854 it->c = 0;
3855 return 1;
3856 }
3857
3858 /* Don't handle selective display in the following. It's (a)
3859 unnecessary because it's done by the caller, and (b) leads to an
3860 infinite recursion because next_element_from_ellipsis indirectly
3861 calls this function. */
3862 old_selective = it->selective;
3863 it->selective = 0;
3864
3865 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3866 from buffer text. */
3867 for (n = newline_found_p = 0;
3868 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3869 n += STRINGP (it->string) ? 0 : 1)
3870 {
3871 if (!get_next_display_element (it))
3872 break;
3873 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3874 set_iterator_to_next (it, 0);
3875 }
3876
3877 /* If we didn't find a newline near enough, see if we can use a
3878 short-cut. */
3879 if (n == MAX_NEWLINE_DISTANCE)
3880 {
3881 int start = IT_CHARPOS (*it);
3882 int limit = find_next_newline_no_quit (start, 1);
3883 Lisp_Object pos;
3884
3885 xassert (!STRINGP (it->string));
3886
3887 /* If there isn't any `display' property in sight, and no
3888 overlays, we can just use the position of the newline in
3889 buffer text. */
3890 if (it->stop_charpos >= limit
3891 || ((pos = Fnext_single_property_change (make_number (start),
3892 Qdisplay,
3893 Qnil, make_number (limit)),
3894 NILP (pos))
3895 && next_overlay_change (start) == ZV))
3896 {
3897 IT_CHARPOS (*it) = limit;
3898 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3899 *skipped_p = newline_found_p = 1;
3900 }
3901 else
3902 {
3903 while (get_next_display_element (it)
3904 && !newline_found_p)
3905 {
3906 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3907 set_iterator_to_next (it, 0);
3908 }
3909 }
3910 }
3911
3912 it->selective = old_selective;
3913 return newline_found_p;
3914 }
3915
3916
3917 /* Set IT's current position to the previous visible line start. Skip
3918 invisible text that is so either due to text properties or due to
3919 selective display. Caution: this does not change IT->current_x and
3920 IT->hpos. */
3921
3922 static void
3923 back_to_previous_visible_line_start (it)
3924 struct it *it;
3925 {
3926 int visible_p = 0;
3927
3928 /* Go back one newline if not on BEGV already. */
3929 if (IT_CHARPOS (*it) > BEGV)
3930 back_to_previous_line_start (it);
3931
3932 /* Move over lines that are invisible because of selective display
3933 or text properties. */
3934 while (IT_CHARPOS (*it) > BEGV
3935 && !visible_p)
3936 {
3937 visible_p = 1;
3938
3939 /* If selective > 0, then lines indented more than that values
3940 are invisible. */
3941 if (it->selective > 0
3942 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3943 (double) it->selective)) /* iftc */
3944 visible_p = 0;
3945 else
3946 {
3947 Lisp_Object prop;
3948
3949 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3950 Qinvisible, it->window);
3951 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3952 visible_p = 0;
3953 }
3954
3955 /* Back one more newline if the current one is invisible. */
3956 if (!visible_p)
3957 back_to_previous_line_start (it);
3958 }
3959
3960 xassert (IT_CHARPOS (*it) >= BEGV);
3961 xassert (IT_CHARPOS (*it) == BEGV
3962 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3963 CHECK_IT (it);
3964 }
3965
3966
3967 /* Reseat iterator IT at the previous visible line start. Skip
3968 invisible text that is so either due to text properties or due to
3969 selective display. At the end, update IT's overlay information,
3970 face information etc. */
3971
3972 static void
3973 reseat_at_previous_visible_line_start (it)
3974 struct it *it;
3975 {
3976 back_to_previous_visible_line_start (it);
3977 reseat (it, it->current.pos, 1);
3978 CHECK_IT (it);
3979 }
3980
3981
3982 /* Reseat iterator IT on the next visible line start in the current
3983 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3984 preceding the line start. Skip over invisible text that is so
3985 because of selective display. Compute faces, overlays etc at the
3986 new position. Note that this function does not skip over text that
3987 is invisible because of text properties. */
3988
3989 static void
3990 reseat_at_next_visible_line_start (it, on_newline_p)
3991 struct it *it;
3992 int on_newline_p;
3993 {
3994 int newline_found_p, skipped_p = 0;
3995
3996 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3997
3998 /* Skip over lines that are invisible because they are indented
3999 more than the value of IT->selective. */
4000 if (it->selective > 0)
4001 while (IT_CHARPOS (*it) < ZV
4002 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4003 (double) it->selective)) /* iftc */
4004 {
4005 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4006 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4007 }
4008
4009 /* Position on the newline if that's what's requested. */
4010 if (on_newline_p && newline_found_p)
4011 {
4012 if (STRINGP (it->string))
4013 {
4014 if (IT_STRING_CHARPOS (*it) > 0)
4015 {
4016 --IT_STRING_CHARPOS (*it);
4017 --IT_STRING_BYTEPOS (*it);
4018 }
4019 }
4020 else if (IT_CHARPOS (*it) > BEGV)
4021 {
4022 --IT_CHARPOS (*it);
4023 --IT_BYTEPOS (*it);
4024 reseat (it, it->current.pos, 0);
4025 }
4026 }
4027 else if (skipped_p)
4028 reseat (it, it->current.pos, 0);
4029
4030 CHECK_IT (it);
4031 }
4032
4033
4034 \f
4035 /***********************************************************************
4036 Changing an iterator's position
4037 ***********************************************************************/
4038
4039 /* Change IT's current position to POS in current_buffer. If FORCE_P
4040 is non-zero, always check for text properties at the new position.
4041 Otherwise, text properties are only looked up if POS >=
4042 IT->check_charpos of a property. */
4043
4044 static void
4045 reseat (it, pos, force_p)
4046 struct it *it;
4047 struct text_pos pos;
4048 int force_p;
4049 {
4050 int original_pos = IT_CHARPOS (*it);
4051
4052 reseat_1 (it, pos, 0);
4053
4054 /* Determine where to check text properties. Avoid doing it
4055 where possible because text property lookup is very expensive. */
4056 if (force_p
4057 || CHARPOS (pos) > it->stop_charpos
4058 || CHARPOS (pos) < original_pos)
4059 handle_stop (it);
4060
4061 CHECK_IT (it);
4062 }
4063
4064
4065 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4066 IT->stop_pos to POS, also. */
4067
4068 static void
4069 reseat_1 (it, pos, set_stop_p)
4070 struct it *it;
4071 struct text_pos pos;
4072 int set_stop_p;
4073 {
4074 /* Don't call this function when scanning a C string. */
4075 xassert (it->s == NULL);
4076
4077 /* POS must be a reasonable value. */
4078 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4079
4080 it->current.pos = it->position = pos;
4081 XSETBUFFER (it->object, current_buffer);
4082 it->end_charpos = ZV;
4083 it->dpvec = NULL;
4084 it->current.dpvec_index = -1;
4085 it->current.overlay_string_index = -1;
4086 IT_STRING_CHARPOS (*it) = -1;
4087 IT_STRING_BYTEPOS (*it) = -1;
4088 it->string = Qnil;
4089 it->method = next_element_from_buffer;
4090 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4091 it->sp = 0;
4092 it->face_before_selective_p = 0;
4093
4094 if (set_stop_p)
4095 it->stop_charpos = CHARPOS (pos);
4096 }
4097
4098
4099 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4100 If S is non-null, it is a C string to iterate over. Otherwise,
4101 STRING gives a Lisp string to iterate over.
4102
4103 If PRECISION > 0, don't return more then PRECISION number of
4104 characters from the string.
4105
4106 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4107 characters have been returned. FIELD_WIDTH < 0 means an infinite
4108 field width.
4109
4110 MULTIBYTE = 0 means disable processing of multibyte characters,
4111 MULTIBYTE > 0 means enable it,
4112 MULTIBYTE < 0 means use IT->multibyte_p.
4113
4114 IT must be initialized via a prior call to init_iterator before
4115 calling this function. */
4116
4117 static void
4118 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4119 struct it *it;
4120 unsigned char *s;
4121 Lisp_Object string;
4122 int charpos;
4123 int precision, field_width, multibyte;
4124 {
4125 /* No region in strings. */
4126 it->region_beg_charpos = it->region_end_charpos = -1;
4127
4128 /* No text property checks performed by default, but see below. */
4129 it->stop_charpos = -1;
4130
4131 /* Set iterator position and end position. */
4132 bzero (&it->current, sizeof it->current);
4133 it->current.overlay_string_index = -1;
4134 it->current.dpvec_index = -1;
4135 xassert (charpos >= 0);
4136
4137 /* If STRING is specified, use its multibyteness, otherwise use the
4138 setting of MULTIBYTE, if specified. */
4139 if (multibyte >= 0)
4140 it->multibyte_p = multibyte > 0;
4141
4142 if (s == NULL)
4143 {
4144 xassert (STRINGP (string));
4145 it->string = string;
4146 it->s = NULL;
4147 it->end_charpos = it->string_nchars = SCHARS (string);
4148 it->method = next_element_from_string;
4149 it->current.string_pos = string_pos (charpos, string);
4150 }
4151 else
4152 {
4153 it->s = s;
4154 it->string = Qnil;
4155
4156 /* Note that we use IT->current.pos, not it->current.string_pos,
4157 for displaying C strings. */
4158 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4159 if (it->multibyte_p)
4160 {
4161 it->current.pos = c_string_pos (charpos, s, 1);
4162 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4163 }
4164 else
4165 {
4166 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4167 it->end_charpos = it->string_nchars = strlen (s);
4168 }
4169
4170 it->method = next_element_from_c_string;
4171 }
4172
4173 /* PRECISION > 0 means don't return more than PRECISION characters
4174 from the string. */
4175 if (precision > 0 && it->end_charpos - charpos > precision)
4176 it->end_charpos = it->string_nchars = charpos + precision;
4177
4178 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4179 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4180 FIELD_WIDTH < 0 means infinite field width. This is useful for
4181 padding with `-' at the end of a mode line. */
4182 if (field_width < 0)
4183 field_width = INFINITY;
4184 if (field_width > it->end_charpos - charpos)
4185 it->end_charpos = charpos + field_width;
4186
4187 /* Use the standard display table for displaying strings. */
4188 if (DISP_TABLE_P (Vstandard_display_table))
4189 it->dp = XCHAR_TABLE (Vstandard_display_table);
4190
4191 it->stop_charpos = charpos;
4192 CHECK_IT (it);
4193 }
4194
4195
4196 \f
4197 /***********************************************************************
4198 Iteration
4199 ***********************************************************************/
4200
4201 /* Load IT's display element fields with information about the next
4202 display element from the current position of IT. Value is zero if
4203 end of buffer (or C string) is reached. */
4204
4205 int
4206 get_next_display_element (it)
4207 struct it *it;
4208 {
4209 /* Non-zero means that we found an display element. Zero means that
4210 we hit the end of what we iterate over. Performance note: the
4211 function pointer `method' used here turns out to be faster than
4212 using a sequence of if-statements. */
4213 int success_p = (*it->method) (it);
4214
4215 if (it->what == IT_CHARACTER)
4216 {
4217 /* Map via display table or translate control characters.
4218 IT->c, IT->len etc. have been set to the next character by
4219 the function call above. If we have a display table, and it
4220 contains an entry for IT->c, translate it. Don't do this if
4221 IT->c itself comes from a display table, otherwise we could
4222 end up in an infinite recursion. (An alternative could be to
4223 count the recursion depth of this function and signal an
4224 error when a certain maximum depth is reached.) Is it worth
4225 it? */
4226 if (success_p && it->dpvec == NULL)
4227 {
4228 Lisp_Object dv;
4229
4230 if (it->dp
4231 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4232 VECTORP (dv)))
4233 {
4234 struct Lisp_Vector *v = XVECTOR (dv);
4235
4236 /* Return the first character from the display table
4237 entry, if not empty. If empty, don't display the
4238 current character. */
4239 if (v->size)
4240 {
4241 it->dpvec_char_len = it->len;
4242 it->dpvec = v->contents;
4243 it->dpend = v->contents + v->size;
4244 it->current.dpvec_index = 0;
4245 it->method = next_element_from_display_vector;
4246 success_p = get_next_display_element (it);
4247 }
4248 else
4249 {
4250 set_iterator_to_next (it, 0);
4251 success_p = get_next_display_element (it);
4252 }
4253 }
4254
4255 /* Translate control characters into `\003' or `^C' form.
4256 Control characters coming from a display table entry are
4257 currently not translated because we use IT->dpvec to hold
4258 the translation. This could easily be changed but I
4259 don't believe that it is worth doing.
4260
4261 Non-printable multibyte characters are also translated
4262 octal form. */
4263 else if ((it->c < ' '
4264 && (it->area != TEXT_AREA
4265 || (it->c != '\n' && it->c != '\t')))
4266 || (it->c >= 127
4267 && it->len == 1)
4268 || !CHAR_PRINTABLE_P (it->c))
4269 {
4270 /* IT->c is a control character which must be displayed
4271 either as '\003' or as `^C' where the '\\' and '^'
4272 can be defined in the display table. Fill
4273 IT->ctl_chars with glyphs for what we have to
4274 display. Then, set IT->dpvec to these glyphs. */
4275 GLYPH g;
4276
4277 if (it->c < 128 && it->ctl_arrow_p)
4278 {
4279 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4280 if (it->dp
4281 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4282 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4283 g = XINT (DISP_CTRL_GLYPH (it->dp));
4284 else
4285 g = FAST_MAKE_GLYPH ('^', 0);
4286 XSETINT (it->ctl_chars[0], g);
4287
4288 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4289 XSETINT (it->ctl_chars[1], g);
4290
4291 /* Set up IT->dpvec and return first character from it. */
4292 it->dpvec_char_len = it->len;
4293 it->dpvec = it->ctl_chars;
4294 it->dpend = it->dpvec + 2;
4295 it->current.dpvec_index = 0;
4296 it->method = next_element_from_display_vector;
4297 get_next_display_element (it);
4298 }
4299 else
4300 {
4301 unsigned char str[MAX_MULTIBYTE_LENGTH];
4302 int len;
4303 int i;
4304 GLYPH escape_glyph;
4305
4306 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4307 if (it->dp
4308 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4309 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4310 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4311 else
4312 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4313
4314 if (SINGLE_BYTE_CHAR_P (it->c))
4315 str[0] = it->c, len = 1;
4316 else
4317 {
4318 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4319 if (len < 0)
4320 {
4321 /* It's an invalid character, which
4322 shouldn't happen actually, but due to
4323 bugs it may happen. Let's print the char
4324 as is, there's not much meaningful we can
4325 do with it. */
4326 str[0] = it->c;
4327 str[1] = it->c >> 8;
4328 str[2] = it->c >> 16;
4329 str[3] = it->c >> 24;
4330 len = 4;
4331 }
4332 }
4333
4334 for (i = 0; i < len; i++)
4335 {
4336 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4337 /* Insert three more glyphs into IT->ctl_chars for
4338 the octal display of the character. */
4339 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4340 XSETINT (it->ctl_chars[i * 4 + 1], g);
4341 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4342 XSETINT (it->ctl_chars[i * 4 + 2], g);
4343 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4344 XSETINT (it->ctl_chars[i * 4 + 3], g);
4345 }
4346
4347 /* Set up IT->dpvec and return the first character
4348 from it. */
4349 it->dpvec_char_len = it->len;
4350 it->dpvec = it->ctl_chars;
4351 it->dpend = it->dpvec + len * 4;
4352 it->current.dpvec_index = 0;
4353 it->method = next_element_from_display_vector;
4354 get_next_display_element (it);
4355 }
4356 }
4357 }
4358
4359 /* Adjust face id for a multibyte character. There are no
4360 multibyte character in unibyte text. */
4361 if (it->multibyte_p
4362 && success_p
4363 && FRAME_WINDOW_P (it->f))
4364 {
4365 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4366 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4367 }
4368 }
4369
4370 /* Is this character the last one of a run of characters with
4371 box? If yes, set IT->end_of_box_run_p to 1. */
4372 if (it->face_box_p
4373 && it->s == NULL)
4374 {
4375 int face_id;
4376 struct face *face;
4377
4378 it->end_of_box_run_p
4379 = ((face_id = face_after_it_pos (it),
4380 face_id != it->face_id)
4381 && (face = FACE_FROM_ID (it->f, face_id),
4382 face->box == FACE_NO_BOX));
4383 }
4384
4385 /* Value is 0 if end of buffer or string reached. */
4386 return success_p;
4387 }
4388
4389
4390 /* Move IT to the next display element.
4391
4392 RESEAT_P non-zero means if called on a newline in buffer text,
4393 skip to the next visible line start.
4394
4395 Functions get_next_display_element and set_iterator_to_next are
4396 separate because I find this arrangement easier to handle than a
4397 get_next_display_element function that also increments IT's
4398 position. The way it is we can first look at an iterator's current
4399 display element, decide whether it fits on a line, and if it does,
4400 increment the iterator position. The other way around we probably
4401 would either need a flag indicating whether the iterator has to be
4402 incremented the next time, or we would have to implement a
4403 decrement position function which would not be easy to write. */
4404
4405 void
4406 set_iterator_to_next (it, reseat_p)
4407 struct it *it;
4408 int reseat_p;
4409 {
4410 /* Reset flags indicating start and end of a sequence of characters
4411 with box. Reset them at the start of this function because
4412 moving the iterator to a new position might set them. */
4413 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4414
4415 if (it->method == next_element_from_buffer)
4416 {
4417 /* The current display element of IT is a character from
4418 current_buffer. Advance in the buffer, and maybe skip over
4419 invisible lines that are so because of selective display. */
4420 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4421 reseat_at_next_visible_line_start (it, 0);
4422 else
4423 {
4424 xassert (it->len != 0);
4425 IT_BYTEPOS (*it) += it->len;
4426 IT_CHARPOS (*it) += 1;
4427 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4428 }
4429 }
4430 else if (it->method == next_element_from_composition)
4431 {
4432 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4433 if (STRINGP (it->string))
4434 {
4435 IT_STRING_BYTEPOS (*it) += it->len;
4436 IT_STRING_CHARPOS (*it) += it->cmp_len;
4437 it->method = next_element_from_string;
4438 goto consider_string_end;
4439 }
4440 else
4441 {
4442 IT_BYTEPOS (*it) += it->len;
4443 IT_CHARPOS (*it) += it->cmp_len;
4444 it->method = next_element_from_buffer;
4445 }
4446 }
4447 else if (it->method == next_element_from_c_string)
4448 {
4449 /* Current display element of IT is from a C string. */
4450 IT_BYTEPOS (*it) += it->len;
4451 IT_CHARPOS (*it) += 1;
4452 }
4453 else if (it->method == next_element_from_display_vector)
4454 {
4455 /* Current display element of IT is from a display table entry.
4456 Advance in the display table definition. Reset it to null if
4457 end reached, and continue with characters from buffers/
4458 strings. */
4459 ++it->current.dpvec_index;
4460
4461 /* Restore face of the iterator to what they were before the
4462 display vector entry (these entries may contain faces). */
4463 it->face_id = it->saved_face_id;
4464
4465 if (it->dpvec + it->current.dpvec_index == it->dpend)
4466 {
4467 if (it->s)
4468 it->method = next_element_from_c_string;
4469 else if (STRINGP (it->string))
4470 it->method = next_element_from_string;
4471 else
4472 it->method = next_element_from_buffer;
4473
4474 it->dpvec = NULL;
4475 it->current.dpvec_index = -1;
4476
4477 /* Skip over characters which were displayed via IT->dpvec. */
4478 if (it->dpvec_char_len < 0)
4479 reseat_at_next_visible_line_start (it, 1);
4480 else if (it->dpvec_char_len > 0)
4481 {
4482 it->len = it->dpvec_char_len;
4483 set_iterator_to_next (it, reseat_p);
4484 }
4485 }
4486 }
4487 else if (it->method == next_element_from_string)
4488 {
4489 /* Current display element is a character from a Lisp string. */
4490 xassert (it->s == NULL && STRINGP (it->string));
4491 IT_STRING_BYTEPOS (*it) += it->len;
4492 IT_STRING_CHARPOS (*it) += 1;
4493
4494 consider_string_end:
4495
4496 if (it->current.overlay_string_index >= 0)
4497 {
4498 /* IT->string is an overlay string. Advance to the
4499 next, if there is one. */
4500 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4501 next_overlay_string (it);
4502 }
4503 else
4504 {
4505 /* IT->string is not an overlay string. If we reached
4506 its end, and there is something on IT->stack, proceed
4507 with what is on the stack. This can be either another
4508 string, this time an overlay string, or a buffer. */
4509 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
4510 && it->sp > 0)
4511 {
4512 pop_it (it);
4513 if (!STRINGP (it->string))
4514 it->method = next_element_from_buffer;
4515 else
4516 goto consider_string_end;
4517 }
4518 }
4519 }
4520 else if (it->method == next_element_from_image
4521 || it->method == next_element_from_stretch)
4522 {
4523 /* The position etc with which we have to proceed are on
4524 the stack. The position may be at the end of a string,
4525 if the `display' property takes up the whole string. */
4526 pop_it (it);
4527 it->image_id = 0;
4528 if (STRINGP (it->string))
4529 {
4530 it->method = next_element_from_string;
4531 goto consider_string_end;
4532 }
4533 else
4534 it->method = next_element_from_buffer;
4535 }
4536 else
4537 /* There are no other methods defined, so this should be a bug. */
4538 abort ();
4539
4540 xassert (it->method != next_element_from_string
4541 || (STRINGP (it->string)
4542 && IT_STRING_CHARPOS (*it) >= 0));
4543 }
4544
4545
4546 /* Load IT's display element fields with information about the next
4547 display element which comes from a display table entry or from the
4548 result of translating a control character to one of the forms `^C'
4549 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4550
4551 static int
4552 next_element_from_display_vector (it)
4553 struct it *it;
4554 {
4555 /* Precondition. */
4556 xassert (it->dpvec && it->current.dpvec_index >= 0);
4557
4558 /* Remember the current face id in case glyphs specify faces.
4559 IT's face is restored in set_iterator_to_next. */
4560 it->saved_face_id = it->face_id;
4561
4562 if (INTEGERP (*it->dpvec)
4563 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4564 {
4565 int lface_id;
4566 GLYPH g;
4567
4568 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4569 it->c = FAST_GLYPH_CHAR (g);
4570 it->len = CHAR_BYTES (it->c);
4571
4572 /* The entry may contain a face id to use. Such a face id is
4573 the id of a Lisp face, not a realized face. A face id of
4574 zero means no face is specified. */
4575 lface_id = FAST_GLYPH_FACE (g);
4576 if (lface_id)
4577 {
4578 /* The function returns -1 if lface_id is invalid. */
4579 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4580 if (face_id >= 0)
4581 it->face_id = face_id;
4582 }
4583 }
4584 else
4585 /* Display table entry is invalid. Return a space. */
4586 it->c = ' ', it->len = 1;
4587
4588 /* Don't change position and object of the iterator here. They are
4589 still the values of the character that had this display table
4590 entry or was translated, and that's what we want. */
4591 it->what = IT_CHARACTER;
4592 return 1;
4593 }
4594
4595
4596 /* Load IT with the next display element from Lisp string IT->string.
4597 IT->current.string_pos is the current position within the string.
4598 If IT->current.overlay_string_index >= 0, the Lisp string is an
4599 overlay string. */
4600
4601 static int
4602 next_element_from_string (it)
4603 struct it *it;
4604 {
4605 struct text_pos position;
4606
4607 xassert (STRINGP (it->string));
4608 xassert (IT_STRING_CHARPOS (*it) >= 0);
4609 position = it->current.string_pos;
4610
4611 /* Time to check for invisible text? */
4612 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4613 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4614 {
4615 handle_stop (it);
4616
4617 /* Since a handler may have changed IT->method, we must
4618 recurse here. */
4619 return get_next_display_element (it);
4620 }
4621
4622 if (it->current.overlay_string_index >= 0)
4623 {
4624 /* Get the next character from an overlay string. In overlay
4625 strings, There is no field width or padding with spaces to
4626 do. */
4627 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4628 {
4629 it->what = IT_EOB;
4630 return 0;
4631 }
4632 else if (STRING_MULTIBYTE (it->string))
4633 {
4634 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4635 const unsigned char *s = (SDATA (it->string)
4636 + IT_STRING_BYTEPOS (*it));
4637 it->c = string_char_and_length (s, remaining, &it->len);
4638 }
4639 else
4640 {
4641 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4642 it->len = 1;
4643 }
4644 }
4645 else
4646 {
4647 /* Get the next character from a Lisp string that is not an
4648 overlay string. Such strings come from the mode line, for
4649 example. We may have to pad with spaces, or truncate the
4650 string. See also next_element_from_c_string. */
4651 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4652 {
4653 it->what = IT_EOB;
4654 return 0;
4655 }
4656 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4657 {
4658 /* Pad with spaces. */
4659 it->c = ' ', it->len = 1;
4660 CHARPOS (position) = BYTEPOS (position) = -1;
4661 }
4662 else if (STRING_MULTIBYTE (it->string))
4663 {
4664 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4665 const unsigned char *s = (SDATA (it->string)
4666 + IT_STRING_BYTEPOS (*it));
4667 it->c = string_char_and_length (s, maxlen, &it->len);
4668 }
4669 else
4670 {
4671 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4672 it->len = 1;
4673 }
4674 }
4675
4676 /* Record what we have and where it came from. Note that we store a
4677 buffer position in IT->position although it could arguably be a
4678 string position. */
4679 it->what = IT_CHARACTER;
4680 it->object = it->string;
4681 it->position = position;
4682 return 1;
4683 }
4684
4685
4686 /* Load IT with next display element from C string IT->s.
4687 IT->string_nchars is the maximum number of characters to return
4688 from the string. IT->end_charpos may be greater than
4689 IT->string_nchars when this function is called, in which case we
4690 may have to return padding spaces. Value is zero if end of string
4691 reached, including padding spaces. */
4692
4693 static int
4694 next_element_from_c_string (it)
4695 struct it *it;
4696 {
4697 int success_p = 1;
4698
4699 xassert (it->s);
4700 it->what = IT_CHARACTER;
4701 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4702 it->object = Qnil;
4703
4704 /* IT's position can be greater IT->string_nchars in case a field
4705 width or precision has been specified when the iterator was
4706 initialized. */
4707 if (IT_CHARPOS (*it) >= it->end_charpos)
4708 {
4709 /* End of the game. */
4710 it->what = IT_EOB;
4711 success_p = 0;
4712 }
4713 else if (IT_CHARPOS (*it) >= it->string_nchars)
4714 {
4715 /* Pad with spaces. */
4716 it->c = ' ', it->len = 1;
4717 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4718 }
4719 else if (it->multibyte_p)
4720 {
4721 /* Implementation note: The calls to strlen apparently aren't a
4722 performance problem because there is no noticeable performance
4723 difference between Emacs running in unibyte or multibyte mode. */
4724 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4725 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4726 maxlen, &it->len);
4727 }
4728 else
4729 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4730
4731 return success_p;
4732 }
4733
4734
4735 /* Set up IT to return characters from an ellipsis, if appropriate.
4736 The definition of the ellipsis glyphs may come from a display table
4737 entry. This function Fills IT with the first glyph from the
4738 ellipsis if an ellipsis is to be displayed. */
4739
4740 static int
4741 next_element_from_ellipsis (it)
4742 struct it *it;
4743 {
4744 if (it->selective_display_ellipsis_p)
4745 {
4746 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4747 {
4748 /* Use the display table definition for `...'. Invalid glyphs
4749 will be handled by the method returning elements from dpvec. */
4750 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4751 it->dpvec_char_len = it->len;
4752 it->dpvec = v->contents;
4753 it->dpend = v->contents + v->size;
4754 it->current.dpvec_index = 0;
4755 it->method = next_element_from_display_vector;
4756 }
4757 else
4758 {
4759 /* Use default `...' which is stored in default_invis_vector. */
4760 it->dpvec_char_len = it->len;
4761 it->dpvec = default_invis_vector;
4762 it->dpend = default_invis_vector + 3;
4763 it->current.dpvec_index = 0;
4764 it->method = next_element_from_display_vector;
4765 }
4766 }
4767 else
4768 {
4769 /* The face at the current position may be different from the
4770 face we find after the invisible text. Remember what it
4771 was in IT->saved_face_id, and signal that it's there by
4772 setting face_before_selective_p. */
4773 it->saved_face_id = it->face_id;
4774 it->method = next_element_from_buffer;
4775 reseat_at_next_visible_line_start (it, 1);
4776 it->face_before_selective_p = 1;
4777 }
4778
4779 return get_next_display_element (it);
4780 }
4781
4782
4783 /* Deliver an image display element. The iterator IT is already
4784 filled with image information (done in handle_display_prop). Value
4785 is always 1. */
4786
4787
4788 static int
4789 next_element_from_image (it)
4790 struct it *it;
4791 {
4792 it->what = IT_IMAGE;
4793 return 1;
4794 }
4795
4796
4797 /* Fill iterator IT with next display element from a stretch glyph
4798 property. IT->object is the value of the text property. Value is
4799 always 1. */
4800
4801 static int
4802 next_element_from_stretch (it)
4803 struct it *it;
4804 {
4805 it->what = IT_STRETCH;
4806 return 1;
4807 }
4808
4809
4810 /* Load IT with the next display element from current_buffer. Value
4811 is zero if end of buffer reached. IT->stop_charpos is the next
4812 position at which to stop and check for text properties or buffer
4813 end. */
4814
4815 static int
4816 next_element_from_buffer (it)
4817 struct it *it;
4818 {
4819 int success_p = 1;
4820
4821 /* Check this assumption, otherwise, we would never enter the
4822 if-statement, below. */
4823 xassert (IT_CHARPOS (*it) >= BEGV
4824 && IT_CHARPOS (*it) <= it->stop_charpos);
4825
4826 if (IT_CHARPOS (*it) >= it->stop_charpos)
4827 {
4828 if (IT_CHARPOS (*it) >= it->end_charpos)
4829 {
4830 int overlay_strings_follow_p;
4831
4832 /* End of the game, except when overlay strings follow that
4833 haven't been returned yet. */
4834 if (it->overlay_strings_at_end_processed_p)
4835 overlay_strings_follow_p = 0;
4836 else
4837 {
4838 it->overlay_strings_at_end_processed_p = 1;
4839 overlay_strings_follow_p = get_overlay_strings (it, 0);
4840 }
4841
4842 if (overlay_strings_follow_p)
4843 success_p = get_next_display_element (it);
4844 else
4845 {
4846 it->what = IT_EOB;
4847 it->position = it->current.pos;
4848 success_p = 0;
4849 }
4850 }
4851 else
4852 {
4853 handle_stop (it);
4854 return get_next_display_element (it);
4855 }
4856 }
4857 else
4858 {
4859 /* No face changes, overlays etc. in sight, so just return a
4860 character from current_buffer. */
4861 unsigned char *p;
4862
4863 /* Maybe run the redisplay end trigger hook. Performance note:
4864 This doesn't seem to cost measurable time. */
4865 if (it->redisplay_end_trigger_charpos
4866 && it->glyph_row
4867 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4868 run_redisplay_end_trigger_hook (it);
4869
4870 /* Get the next character, maybe multibyte. */
4871 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4872 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4873 {
4874 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4875 - IT_BYTEPOS (*it));
4876 it->c = string_char_and_length (p, maxlen, &it->len);
4877 }
4878 else
4879 it->c = *p, it->len = 1;
4880
4881 /* Record what we have and where it came from. */
4882 it->what = IT_CHARACTER;;
4883 it->object = it->w->buffer;
4884 it->position = it->current.pos;
4885
4886 /* Normally we return the character found above, except when we
4887 really want to return an ellipsis for selective display. */
4888 if (it->selective)
4889 {
4890 if (it->c == '\n')
4891 {
4892 /* A value of selective > 0 means hide lines indented more
4893 than that number of columns. */
4894 if (it->selective > 0
4895 && IT_CHARPOS (*it) + 1 < ZV
4896 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4897 IT_BYTEPOS (*it) + 1,
4898 (double) it->selective)) /* iftc */
4899 {
4900 success_p = next_element_from_ellipsis (it);
4901 it->dpvec_char_len = -1;
4902 }
4903 }
4904 else if (it->c == '\r' && it->selective == -1)
4905 {
4906 /* A value of selective == -1 means that everything from the
4907 CR to the end of the line is invisible, with maybe an
4908 ellipsis displayed for it. */
4909 success_p = next_element_from_ellipsis (it);
4910 it->dpvec_char_len = -1;
4911 }
4912 }
4913 }
4914
4915 /* Value is zero if end of buffer reached. */
4916 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4917 return success_p;
4918 }
4919
4920
4921 /* Run the redisplay end trigger hook for IT. */
4922
4923 static void
4924 run_redisplay_end_trigger_hook (it)
4925 struct it *it;
4926 {
4927 Lisp_Object args[3];
4928
4929 /* IT->glyph_row should be non-null, i.e. we should be actually
4930 displaying something, or otherwise we should not run the hook. */
4931 xassert (it->glyph_row);
4932
4933 /* Set up hook arguments. */
4934 args[0] = Qredisplay_end_trigger_functions;
4935 args[1] = it->window;
4936 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4937 it->redisplay_end_trigger_charpos = 0;
4938
4939 /* Since we are *trying* to run these functions, don't try to run
4940 them again, even if they get an error. */
4941 it->w->redisplay_end_trigger = Qnil;
4942 Frun_hook_with_args (3, args);
4943
4944 /* Notice if it changed the face of the character we are on. */
4945 handle_face_prop (it);
4946 }
4947
4948
4949 /* Deliver a composition display element. The iterator IT is already
4950 filled with composition information (done in
4951 handle_composition_prop). Value is always 1. */
4952
4953 static int
4954 next_element_from_composition (it)
4955 struct it *it;
4956 {
4957 it->what = IT_COMPOSITION;
4958 it->position = (STRINGP (it->string)
4959 ? it->current.string_pos
4960 : it->current.pos);
4961 return 1;
4962 }
4963
4964
4965 \f
4966 /***********************************************************************
4967 Moving an iterator without producing glyphs
4968 ***********************************************************************/
4969
4970 /* Move iterator IT to a specified buffer or X position within one
4971 line on the display without producing glyphs.
4972
4973 OP should be a bit mask including some or all of these bits:
4974 MOVE_TO_X: Stop on reaching x-position TO_X.
4975 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
4976 Regardless of OP's value, stop in reaching the end of the display line.
4977
4978 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
4979 This means, in particular, that TO_X includes window's horizontal
4980 scroll amount.
4981
4982 The return value has several possible values that
4983 say what condition caused the scan to stop:
4984
4985 MOVE_POS_MATCH_OR_ZV
4986 - when TO_POS or ZV was reached.
4987
4988 MOVE_X_REACHED
4989 -when TO_X was reached before TO_POS or ZV were reached.
4990
4991 MOVE_LINE_CONTINUED
4992 - when we reached the end of the display area and the line must
4993 be continued.
4994
4995 MOVE_LINE_TRUNCATED
4996 - when we reached the end of the display area and the line is
4997 truncated.
4998
4999 MOVE_NEWLINE_OR_CR
5000 - when we stopped at a line end, i.e. a newline or a CR and selective
5001 display is on. */
5002
5003 static enum move_it_result
5004 move_it_in_display_line_to (it, to_charpos, to_x, op)
5005 struct it *it;
5006 int to_charpos, to_x, op;
5007 {
5008 enum move_it_result result = MOVE_UNDEFINED;
5009 struct glyph_row *saved_glyph_row;
5010
5011 /* Don't produce glyphs in produce_glyphs. */
5012 saved_glyph_row = it->glyph_row;
5013 it->glyph_row = NULL;
5014
5015 while (1)
5016 {
5017 int x, i, ascent = 0, descent = 0;
5018
5019 /* Stop when ZV or TO_CHARPOS reached. */
5020 if (!get_next_display_element (it)
5021 || ((op & MOVE_TO_POS) != 0
5022 && BUFFERP (it->object)
5023 && IT_CHARPOS (*it) >= to_charpos))
5024 {
5025 result = MOVE_POS_MATCH_OR_ZV;
5026 break;
5027 }
5028
5029 /* The call to produce_glyphs will get the metrics of the
5030 display element IT is loaded with. We record in x the
5031 x-position before this display element in case it does not
5032 fit on the line. */
5033 x = it->current_x;
5034
5035 /* Remember the line height so far in case the next element doesn't
5036 fit on the line. */
5037 if (!it->truncate_lines_p)
5038 {
5039 ascent = it->max_ascent;
5040 descent = it->max_descent;
5041 }
5042
5043 PRODUCE_GLYPHS (it);
5044
5045 if (it->area != TEXT_AREA)
5046 {
5047 set_iterator_to_next (it, 1);
5048 continue;
5049 }
5050
5051 /* The number of glyphs we get back in IT->nglyphs will normally
5052 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5053 character on a terminal frame, or (iii) a line end. For the
5054 second case, IT->nglyphs - 1 padding glyphs will be present
5055 (on X frames, there is only one glyph produced for a
5056 composite character.
5057
5058 The behavior implemented below means, for continuation lines,
5059 that as many spaces of a TAB as fit on the current line are
5060 displayed there. For terminal frames, as many glyphs of a
5061 multi-glyph character are displayed in the current line, too.
5062 This is what the old redisplay code did, and we keep it that
5063 way. Under X, the whole shape of a complex character must
5064 fit on the line or it will be completely displayed in the
5065 next line.
5066
5067 Note that both for tabs and padding glyphs, all glyphs have
5068 the same width. */
5069 if (it->nglyphs)
5070 {
5071 /* More than one glyph or glyph doesn't fit on line. All
5072 glyphs have the same width. */
5073 int single_glyph_width = it->pixel_width / it->nglyphs;
5074 int new_x;
5075
5076 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5077 {
5078 new_x = x + single_glyph_width;
5079
5080 /* We want to leave anything reaching TO_X to the caller. */
5081 if ((op & MOVE_TO_X) && new_x > to_x)
5082 {
5083 it->current_x = x;
5084 result = MOVE_X_REACHED;
5085 break;
5086 }
5087 else if (/* Lines are continued. */
5088 !it->truncate_lines_p
5089 && (/* And glyph doesn't fit on the line. */
5090 new_x > it->last_visible_x
5091 /* Or it fits exactly and we're on a window
5092 system frame. */
5093 || (new_x == it->last_visible_x
5094 && FRAME_WINDOW_P (it->f))))
5095 {
5096 if (/* IT->hpos == 0 means the very first glyph
5097 doesn't fit on the line, e.g. a wide image. */
5098 it->hpos == 0
5099 || (new_x == it->last_visible_x
5100 && FRAME_WINDOW_P (it->f)))
5101 {
5102 ++it->hpos;
5103 it->current_x = new_x;
5104 if (i == it->nglyphs - 1)
5105 set_iterator_to_next (it, 1);
5106 }
5107 else
5108 {
5109 it->current_x = x;
5110 it->max_ascent = ascent;
5111 it->max_descent = descent;
5112 }
5113
5114 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5115 IT_CHARPOS (*it)));
5116 result = MOVE_LINE_CONTINUED;
5117 break;
5118 }
5119 else if (new_x > it->first_visible_x)
5120 {
5121 /* Glyph is visible. Increment number of glyphs that
5122 would be displayed. */
5123 ++it->hpos;
5124 }
5125 else
5126 {
5127 /* Glyph is completely off the left margin of the display
5128 area. Nothing to do. */
5129 }
5130 }
5131
5132 if (result != MOVE_UNDEFINED)
5133 break;
5134 }
5135 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5136 {
5137 /* Stop when TO_X specified and reached. This check is
5138 necessary here because of lines consisting of a line end,
5139 only. The line end will not produce any glyphs and we
5140 would never get MOVE_X_REACHED. */
5141 xassert (it->nglyphs == 0);
5142 result = MOVE_X_REACHED;
5143 break;
5144 }
5145
5146 /* Is this a line end? If yes, we're done. */
5147 if (ITERATOR_AT_END_OF_LINE_P (it))
5148 {
5149 result = MOVE_NEWLINE_OR_CR;
5150 break;
5151 }
5152
5153 /* The current display element has been consumed. Advance
5154 to the next. */
5155 set_iterator_to_next (it, 1);
5156
5157 /* Stop if lines are truncated and IT's current x-position is
5158 past the right edge of the window now. */
5159 if (it->truncate_lines_p
5160 && it->current_x >= it->last_visible_x)
5161 {
5162 result = MOVE_LINE_TRUNCATED;
5163 break;
5164 }
5165 }
5166
5167 /* Restore the iterator settings altered at the beginning of this
5168 function. */
5169 it->glyph_row = saved_glyph_row;
5170 return result;
5171 }
5172
5173
5174 /* Move IT forward until it satisfies one or more of the criteria in
5175 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5176
5177 OP is a bit-mask that specifies where to stop, and in particular,
5178 which of those four position arguments makes a difference. See the
5179 description of enum move_operation_enum.
5180
5181 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5182 screen line, this function will set IT to the next position >
5183 TO_CHARPOS. */
5184
5185 void
5186 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5187 struct it *it;
5188 int to_charpos, to_x, to_y, to_vpos;
5189 int op;
5190 {
5191 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5192 int line_height;
5193 int reached = 0;
5194
5195 for (;;)
5196 {
5197 if (op & MOVE_TO_VPOS)
5198 {
5199 /* If no TO_CHARPOS and no TO_X specified, stop at the
5200 start of the line TO_VPOS. */
5201 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5202 {
5203 if (it->vpos == to_vpos)
5204 {
5205 reached = 1;
5206 break;
5207 }
5208 else
5209 skip = move_it_in_display_line_to (it, -1, -1, 0);
5210 }
5211 else
5212 {
5213 /* TO_VPOS >= 0 means stop at TO_X in the line at
5214 TO_VPOS, or at TO_POS, whichever comes first. */
5215 if (it->vpos == to_vpos)
5216 {
5217 reached = 2;
5218 break;
5219 }
5220
5221 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5222
5223 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5224 {
5225 reached = 3;
5226 break;
5227 }
5228 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5229 {
5230 /* We have reached TO_X but not in the line we want. */
5231 skip = move_it_in_display_line_to (it, to_charpos,
5232 -1, MOVE_TO_POS);
5233 if (skip == MOVE_POS_MATCH_OR_ZV)
5234 {
5235 reached = 4;
5236 break;
5237 }
5238 }
5239 }
5240 }
5241 else if (op & MOVE_TO_Y)
5242 {
5243 struct it it_backup;
5244
5245 /* TO_Y specified means stop at TO_X in the line containing
5246 TO_Y---or at TO_CHARPOS if this is reached first. The
5247 problem is that we can't really tell whether the line
5248 contains TO_Y before we have completely scanned it, and
5249 this may skip past TO_X. What we do is to first scan to
5250 TO_X.
5251
5252 If TO_X is not specified, use a TO_X of zero. The reason
5253 is to make the outcome of this function more predictable.
5254 If we didn't use TO_X == 0, we would stop at the end of
5255 the line which is probably not what a caller would expect
5256 to happen. */
5257 skip = move_it_in_display_line_to (it, to_charpos,
5258 ((op & MOVE_TO_X)
5259 ? to_x : 0),
5260 (MOVE_TO_X
5261 | (op & MOVE_TO_POS)));
5262
5263 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5264 if (skip == MOVE_POS_MATCH_OR_ZV)
5265 {
5266 reached = 5;
5267 break;
5268 }
5269
5270 /* If TO_X was reached, we would like to know whether TO_Y
5271 is in the line. This can only be said if we know the
5272 total line height which requires us to scan the rest of
5273 the line. */
5274 if (skip == MOVE_X_REACHED)
5275 {
5276 it_backup = *it;
5277 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5278 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5279 op & MOVE_TO_POS);
5280 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5281 }
5282
5283 /* Now, decide whether TO_Y is in this line. */
5284 line_height = it->max_ascent + it->max_descent;
5285 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5286
5287 if (to_y >= it->current_y
5288 && to_y < it->current_y + line_height)
5289 {
5290 if (skip == MOVE_X_REACHED)
5291 /* If TO_Y is in this line and TO_X was reached above,
5292 we scanned too far. We have to restore IT's settings
5293 to the ones before skipping. */
5294 *it = it_backup;
5295 reached = 6;
5296 }
5297 else if (skip == MOVE_X_REACHED)
5298 {
5299 skip = skip2;
5300 if (skip == MOVE_POS_MATCH_OR_ZV)
5301 reached = 7;
5302 }
5303
5304 if (reached)
5305 break;
5306 }
5307 else
5308 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5309
5310 switch (skip)
5311 {
5312 case MOVE_POS_MATCH_OR_ZV:
5313 reached = 8;
5314 goto out;
5315
5316 case MOVE_NEWLINE_OR_CR:
5317 set_iterator_to_next (it, 1);
5318 it->continuation_lines_width = 0;
5319 break;
5320
5321 case MOVE_LINE_TRUNCATED:
5322 it->continuation_lines_width = 0;
5323 reseat_at_next_visible_line_start (it, 0);
5324 if ((op & MOVE_TO_POS) != 0
5325 && IT_CHARPOS (*it) > to_charpos)
5326 {
5327 reached = 9;
5328 goto out;
5329 }
5330 break;
5331
5332 case MOVE_LINE_CONTINUED:
5333 it->continuation_lines_width += it->current_x;
5334 break;
5335
5336 default:
5337 abort ();
5338 }
5339
5340 /* Reset/increment for the next run. */
5341 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5342 it->current_x = it->hpos = 0;
5343 it->current_y += it->max_ascent + it->max_descent;
5344 ++it->vpos;
5345 last_height = it->max_ascent + it->max_descent;
5346 last_max_ascent = it->max_ascent;
5347 it->max_ascent = it->max_descent = 0;
5348 }
5349
5350 out:
5351
5352 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5353 }
5354
5355
5356 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5357
5358 If DY > 0, move IT backward at least that many pixels. DY = 0
5359 means move IT backward to the preceding line start or BEGV. This
5360 function may move over more than DY pixels if IT->current_y - DY
5361 ends up in the middle of a line; in this case IT->current_y will be
5362 set to the top of the line moved to. */
5363
5364 void
5365 move_it_vertically_backward (it, dy)
5366 struct it *it;
5367 int dy;
5368 {
5369 int nlines, h;
5370 struct it it2, it3;
5371 int start_pos = IT_CHARPOS (*it);
5372
5373 xassert (dy >= 0);
5374
5375 /* Estimate how many newlines we must move back. */
5376 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5377
5378 /* Set the iterator's position that many lines back. */
5379 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5380 back_to_previous_visible_line_start (it);
5381
5382 /* Reseat the iterator here. When moving backward, we don't want
5383 reseat to skip forward over invisible text, set up the iterator
5384 to deliver from overlay strings at the new position etc. So,
5385 use reseat_1 here. */
5386 reseat_1 (it, it->current.pos, 1);
5387
5388 /* We are now surely at a line start. */
5389 it->current_x = it->hpos = 0;
5390
5391 /* Move forward and see what y-distance we moved. First move to the
5392 start of the next line so that we get its height. We need this
5393 height to be able to tell whether we reached the specified
5394 y-distance. */
5395 it2 = *it;
5396 it2.max_ascent = it2.max_descent = 0;
5397 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5398 MOVE_TO_POS | MOVE_TO_VPOS);
5399 xassert (IT_CHARPOS (*it) >= BEGV);
5400 it3 = it2;
5401
5402 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5403 xassert (IT_CHARPOS (*it) >= BEGV);
5404 h = it2.current_y - it->current_y;
5405 nlines = it2.vpos - it->vpos;
5406
5407 /* Correct IT's y and vpos position. */
5408 it->vpos -= nlines;
5409 it->current_y -= h;
5410
5411 if (dy == 0)
5412 {
5413 /* DY == 0 means move to the start of the screen line. The
5414 value of nlines is > 0 if continuation lines were involved. */
5415 if (nlines > 0)
5416 move_it_by_lines (it, nlines, 1);
5417 xassert (IT_CHARPOS (*it) <= start_pos);
5418 }
5419 else if (nlines)
5420 {
5421 /* The y-position we try to reach. Note that h has been
5422 subtracted in front of the if-statement. */
5423 int target_y = it->current_y + h - dy;
5424 int y0 = it3.current_y;
5425 int y1 = line_bottom_y (&it3);
5426 int line_height = y1 - y0;
5427
5428 /* If we did not reach target_y, try to move further backward if
5429 we can. If we moved too far backward, try to move forward. */
5430 if (target_y < it->current_y
5431 /* This is heuristic. In a window that's 3 lines high, with
5432 a line height of 13 pixels each, recentering with point
5433 on the bottom line will try to move -39/2 = 19 pixels
5434 backward. Try to avoid moving into the first line. */
5435 && it->current_y - target_y > line_height / 3 * 2
5436 && IT_CHARPOS (*it) > BEGV)
5437 {
5438 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5439 target_y - it->current_y));
5440 move_it_vertically (it, target_y - it->current_y);
5441 xassert (IT_CHARPOS (*it) >= BEGV);
5442 }
5443 else if (target_y >= it->current_y + line_height
5444 && IT_CHARPOS (*it) < ZV)
5445 {
5446 /* Should move forward by at least one line, maybe more.
5447
5448 Note: Calling move_it_by_lines can be expensive on
5449 terminal frames, where compute_motion is used (via
5450 vmotion) to do the job, when there are very long lines
5451 and truncate-lines is nil. That's the reason for
5452 treating terminal frames specially here. */
5453
5454 if (!FRAME_WINDOW_P (it->f))
5455 move_it_vertically (it, target_y - (it->current_y + line_height));
5456 else
5457 {
5458 do
5459 {
5460 move_it_by_lines (it, 1, 1);
5461 }
5462 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5463 }
5464
5465 xassert (IT_CHARPOS (*it) >= BEGV);
5466 }
5467 }
5468 }
5469
5470
5471 /* Move IT by a specified amount of pixel lines DY. DY negative means
5472 move backwards. DY = 0 means move to start of screen line. At the
5473 end, IT will be on the start of a screen line. */
5474
5475 void
5476 move_it_vertically (it, dy)
5477 struct it *it;
5478 int dy;
5479 {
5480 if (dy <= 0)
5481 move_it_vertically_backward (it, -dy);
5482 else if (dy > 0)
5483 {
5484 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5485 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5486 MOVE_TO_POS | MOVE_TO_Y);
5487 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5488
5489 /* If buffer ends in ZV without a newline, move to the start of
5490 the line to satisfy the post-condition. */
5491 if (IT_CHARPOS (*it) == ZV
5492 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5493 move_it_by_lines (it, 0, 0);
5494 }
5495 }
5496
5497
5498 /* Move iterator IT past the end of the text line it is in. */
5499
5500 void
5501 move_it_past_eol (it)
5502 struct it *it;
5503 {
5504 enum move_it_result rc;
5505
5506 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5507 if (rc == MOVE_NEWLINE_OR_CR)
5508 set_iterator_to_next (it, 0);
5509 }
5510
5511
5512 #if 0 /* Currently not used. */
5513
5514 /* Return non-zero if some text between buffer positions START_CHARPOS
5515 and END_CHARPOS is invisible. IT->window is the window for text
5516 property lookup. */
5517
5518 static int
5519 invisible_text_between_p (it, start_charpos, end_charpos)
5520 struct it *it;
5521 int start_charpos, end_charpos;
5522 {
5523 Lisp_Object prop, limit;
5524 int invisible_found_p;
5525
5526 xassert (it != NULL && start_charpos <= end_charpos);
5527
5528 /* Is text at START invisible? */
5529 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5530 it->window);
5531 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5532 invisible_found_p = 1;
5533 else
5534 {
5535 limit = Fnext_single_char_property_change (make_number (start_charpos),
5536 Qinvisible, Qnil,
5537 make_number (end_charpos));
5538 invisible_found_p = XFASTINT (limit) < end_charpos;
5539 }
5540
5541 return invisible_found_p;
5542 }
5543
5544 #endif /* 0 */
5545
5546
5547 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5548 negative means move up. DVPOS == 0 means move to the start of the
5549 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5550 NEED_Y_P is zero, IT->current_y will be left unchanged.
5551
5552 Further optimization ideas: If we would know that IT->f doesn't use
5553 a face with proportional font, we could be faster for
5554 truncate-lines nil. */
5555
5556 void
5557 move_it_by_lines (it, dvpos, need_y_p)
5558 struct it *it;
5559 int dvpos, need_y_p;
5560 {
5561 struct position pos;
5562
5563 if (!FRAME_WINDOW_P (it->f))
5564 {
5565 struct text_pos textpos;
5566
5567 /* We can use vmotion on frames without proportional fonts. */
5568 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5569 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5570 reseat (it, textpos, 1);
5571 it->vpos += pos.vpos;
5572 it->current_y += pos.vpos;
5573 }
5574 else if (dvpos == 0)
5575 {
5576 /* DVPOS == 0 means move to the start of the screen line. */
5577 move_it_vertically_backward (it, 0);
5578 xassert (it->current_x == 0 && it->hpos == 0);
5579 }
5580 else if (dvpos > 0)
5581 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5582 else
5583 {
5584 struct it it2;
5585 int start_charpos, i;
5586
5587 /* Start at the beginning of the screen line containing IT's
5588 position. */
5589 move_it_vertically_backward (it, 0);
5590
5591 /* Go back -DVPOS visible lines and reseat the iterator there. */
5592 start_charpos = IT_CHARPOS (*it);
5593 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5594 back_to_previous_visible_line_start (it);
5595 reseat (it, it->current.pos, 1);
5596 it->current_x = it->hpos = 0;
5597
5598 /* Above call may have moved too far if continuation lines
5599 are involved. Scan forward and see if it did. */
5600 it2 = *it;
5601 it2.vpos = it2.current_y = 0;
5602 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5603 it->vpos -= it2.vpos;
5604 it->current_y -= it2.current_y;
5605 it->current_x = it->hpos = 0;
5606
5607 /* If we moved too far, move IT some lines forward. */
5608 if (it2.vpos > -dvpos)
5609 {
5610 int delta = it2.vpos + dvpos;
5611 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5612 }
5613 }
5614 }
5615
5616
5617 \f
5618 /***********************************************************************
5619 Messages
5620 ***********************************************************************/
5621
5622
5623 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5624 to *Messages*. */
5625
5626 void
5627 add_to_log (format, arg1, arg2)
5628 char *format;
5629 Lisp_Object arg1, arg2;
5630 {
5631 Lisp_Object args[3];
5632 Lisp_Object msg, fmt;
5633 char *buffer;
5634 int len;
5635 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5636
5637 /* Do nothing if called asynchronously. Inserting text into
5638 a buffer may call after-change-functions and alike and
5639 that would means running Lisp asynchronously. */
5640 if (handling_signal)
5641 return;
5642
5643 fmt = msg = Qnil;
5644 GCPRO4 (fmt, msg, arg1, arg2);
5645
5646 args[0] = fmt = build_string (format);
5647 args[1] = arg1;
5648 args[2] = arg2;
5649 msg = Fformat (3, args);
5650
5651 len = SBYTES (msg) + 1;
5652 buffer = (char *) alloca (len);
5653 bcopy (SDATA (msg), buffer, len);
5654
5655 message_dolog (buffer, len - 1, 1, 0);
5656 UNGCPRO;
5657 }
5658
5659
5660 /* Output a newline in the *Messages* buffer if "needs" one. */
5661
5662 void
5663 message_log_maybe_newline ()
5664 {
5665 if (message_log_need_newline)
5666 message_dolog ("", 0, 1, 0);
5667 }
5668
5669
5670 /* Add a string M of length NBYTES to the message log, optionally
5671 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5672 nonzero, means interpret the contents of M as multibyte. This
5673 function calls low-level routines in order to bypass text property
5674 hooks, etc. which might not be safe to run. */
5675
5676 void
5677 message_dolog (m, nbytes, nlflag, multibyte)
5678 const char *m;
5679 int nbytes, nlflag, multibyte;
5680 {
5681 if (!NILP (Vmemory_full))
5682 return;
5683
5684 if (!NILP (Vmessage_log_max))
5685 {
5686 struct buffer *oldbuf;
5687 Lisp_Object oldpoint, oldbegv, oldzv;
5688 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5689 int point_at_end = 0;
5690 int zv_at_end = 0;
5691 Lisp_Object old_deactivate_mark, tem;
5692 struct gcpro gcpro1;
5693
5694 old_deactivate_mark = Vdeactivate_mark;
5695 oldbuf = current_buffer;
5696 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5697 current_buffer->undo_list = Qt;
5698
5699 oldpoint = message_dolog_marker1;
5700 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5701 oldbegv = message_dolog_marker2;
5702 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5703 oldzv = message_dolog_marker3;
5704 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5705 GCPRO1 (old_deactivate_mark);
5706
5707 if (PT == Z)
5708 point_at_end = 1;
5709 if (ZV == Z)
5710 zv_at_end = 1;
5711
5712 BEGV = BEG;
5713 BEGV_BYTE = BEG_BYTE;
5714 ZV = Z;
5715 ZV_BYTE = Z_BYTE;
5716 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5717
5718 /* Insert the string--maybe converting multibyte to single byte
5719 or vice versa, so that all the text fits the buffer. */
5720 if (multibyte
5721 && NILP (current_buffer->enable_multibyte_characters))
5722 {
5723 int i, c, char_bytes;
5724 unsigned char work[1];
5725
5726 /* Convert a multibyte string to single-byte
5727 for the *Message* buffer. */
5728 for (i = 0; i < nbytes; i += nbytes)
5729 {
5730 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5731 work[0] = (SINGLE_BYTE_CHAR_P (c)
5732 ? c
5733 : multibyte_char_to_unibyte (c, Qnil));
5734 insert_1_both (work, 1, 1, 1, 0, 0);
5735 }
5736 }
5737 else if (! multibyte
5738 && ! NILP (current_buffer->enable_multibyte_characters))
5739 {
5740 int i, c, char_bytes;
5741 unsigned char *msg = (unsigned char *) m;
5742 unsigned char str[MAX_MULTIBYTE_LENGTH];
5743 /* Convert a single-byte string to multibyte
5744 for the *Message* buffer. */
5745 for (i = 0; i < nbytes; i++)
5746 {
5747 c = unibyte_char_to_multibyte (msg[i]);
5748 char_bytes = CHAR_STRING (c, str);
5749 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5750 }
5751 }
5752 else if (nbytes)
5753 insert_1 (m, nbytes, 1, 0, 0);
5754
5755 if (nlflag)
5756 {
5757 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5758 insert_1 ("\n", 1, 1, 0, 0);
5759
5760 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5761 this_bol = PT;
5762 this_bol_byte = PT_BYTE;
5763
5764 /* See if this line duplicates the previous one.
5765 If so, combine duplicates. */
5766 if (this_bol > BEG)
5767 {
5768 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5769 prev_bol = PT;
5770 prev_bol_byte = PT_BYTE;
5771
5772 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5773 this_bol, this_bol_byte);
5774 if (dup)
5775 {
5776 del_range_both (prev_bol, prev_bol_byte,
5777 this_bol, this_bol_byte, 0);
5778 if (dup > 1)
5779 {
5780 char dupstr[40];
5781 int duplen;
5782
5783 /* If you change this format, don't forget to also
5784 change message_log_check_duplicate. */
5785 sprintf (dupstr, " [%d times]", dup);
5786 duplen = strlen (dupstr);
5787 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5788 insert_1 (dupstr, duplen, 1, 0, 1);
5789 }
5790 }
5791 }
5792
5793 /* If we have more than the desired maximum number of lines
5794 in the *Messages* buffer now, delete the oldest ones.
5795 This is safe because we don't have undo in this buffer. */
5796
5797 if (NATNUMP (Vmessage_log_max))
5798 {
5799 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5800 -XFASTINT (Vmessage_log_max) - 1, 0);
5801 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5802 }
5803 }
5804 BEGV = XMARKER (oldbegv)->charpos;
5805 BEGV_BYTE = marker_byte_position (oldbegv);
5806
5807 if (zv_at_end)
5808 {
5809 ZV = Z;
5810 ZV_BYTE = Z_BYTE;
5811 }
5812 else
5813 {
5814 ZV = XMARKER (oldzv)->charpos;
5815 ZV_BYTE = marker_byte_position (oldzv);
5816 }
5817
5818 if (point_at_end)
5819 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5820 else
5821 /* We can't do Fgoto_char (oldpoint) because it will run some
5822 Lisp code. */
5823 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5824 XMARKER (oldpoint)->bytepos);
5825
5826 UNGCPRO;
5827 unchain_marker (oldpoint);
5828 unchain_marker (oldbegv);
5829 unchain_marker (oldzv);
5830
5831 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5832 set_buffer_internal (oldbuf);
5833 if (NILP (tem))
5834 windows_or_buffers_changed = old_windows_or_buffers_changed;
5835 message_log_need_newline = !nlflag;
5836 Vdeactivate_mark = old_deactivate_mark;
5837 }
5838 }
5839
5840
5841 /* We are at the end of the buffer after just having inserted a newline.
5842 (Note: We depend on the fact we won't be crossing the gap.)
5843 Check to see if the most recent message looks a lot like the previous one.
5844 Return 0 if different, 1 if the new one should just replace it, or a
5845 value N > 1 if we should also append " [N times]". */
5846
5847 static int
5848 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5849 int prev_bol, this_bol;
5850 int prev_bol_byte, this_bol_byte;
5851 {
5852 int i;
5853 int len = Z_BYTE - 1 - this_bol_byte;
5854 int seen_dots = 0;
5855 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5856 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5857
5858 for (i = 0; i < len; i++)
5859 {
5860 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5861 seen_dots = 1;
5862 if (p1[i] != p2[i])
5863 return seen_dots;
5864 }
5865 p1 += len;
5866 if (*p1 == '\n')
5867 return 2;
5868 if (*p1++ == ' ' && *p1++ == '[')
5869 {
5870 int n = 0;
5871 while (*p1 >= '0' && *p1 <= '9')
5872 n = n * 10 + *p1++ - '0';
5873 if (strncmp (p1, " times]\n", 8) == 0)
5874 return n+1;
5875 }
5876 return 0;
5877 }
5878
5879
5880 /* Display an echo area message M with a specified length of NBYTES
5881 bytes. The string may include null characters. If M is 0, clear
5882 out any existing message, and let the mini-buffer text show
5883 through.
5884
5885 The buffer M must continue to exist until after the echo area gets
5886 cleared or some other message gets displayed there. This means do
5887 not pass text that is stored in a Lisp string; do not pass text in
5888 a buffer that was alloca'd. */
5889
5890 void
5891 message2 (m, nbytes, multibyte)
5892 const char *m;
5893 int nbytes;
5894 int multibyte;
5895 {
5896 /* First flush out any partial line written with print. */
5897 message_log_maybe_newline ();
5898 if (m)
5899 message_dolog (m, nbytes, 1, multibyte);
5900 message2_nolog (m, nbytes, multibyte);
5901 }
5902
5903
5904 /* The non-logging counterpart of message2. */
5905
5906 void
5907 message2_nolog (m, nbytes, multibyte)
5908 const char *m;
5909 int nbytes;
5910 {
5911 struct frame *sf = SELECTED_FRAME ();
5912 message_enable_multibyte = multibyte;
5913
5914 if (noninteractive)
5915 {
5916 if (noninteractive_need_newline)
5917 putc ('\n', stderr);
5918 noninteractive_need_newline = 0;
5919 if (m)
5920 fwrite (m, nbytes, 1, stderr);
5921 if (cursor_in_echo_area == 0)
5922 fprintf (stderr, "\n");
5923 fflush (stderr);
5924 }
5925 /* A null message buffer means that the frame hasn't really been
5926 initialized yet. Error messages get reported properly by
5927 cmd_error, so this must be just an informative message; toss it. */
5928 else if (INTERACTIVE
5929 && sf->glyphs_initialized_p
5930 && FRAME_MESSAGE_BUF (sf))
5931 {
5932 Lisp_Object mini_window;
5933 struct frame *f;
5934
5935 /* Get the frame containing the mini-buffer
5936 that the selected frame is using. */
5937 mini_window = FRAME_MINIBUF_WINDOW (sf);
5938 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5939
5940 FRAME_SAMPLE_VISIBILITY (f);
5941 if (FRAME_VISIBLE_P (sf)
5942 && ! FRAME_VISIBLE_P (f))
5943 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5944
5945 if (m)
5946 {
5947 set_message (m, Qnil, nbytes, multibyte);
5948 if (minibuffer_auto_raise)
5949 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5950 }
5951 else
5952 clear_message (1, 1);
5953
5954 do_pending_window_change (0);
5955 echo_area_display (1);
5956 do_pending_window_change (0);
5957 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5958 (*frame_up_to_date_hook) (f);
5959 }
5960 }
5961
5962
5963 /* Display an echo area message M with a specified length of NBYTES
5964 bytes. The string may include null characters. If M is not a
5965 string, clear out any existing message, and let the mini-buffer
5966 text show through. */
5967
5968 void
5969 message3 (m, nbytes, multibyte)
5970 Lisp_Object m;
5971 int nbytes;
5972 int multibyte;
5973 {
5974 struct gcpro gcpro1;
5975
5976 GCPRO1 (m);
5977
5978 /* First flush out any partial line written with print. */
5979 message_log_maybe_newline ();
5980 if (STRINGP (m))
5981 message_dolog (SDATA (m), nbytes, 1, multibyte);
5982 message3_nolog (m, nbytes, multibyte);
5983
5984 UNGCPRO;
5985 }
5986
5987
5988 /* The non-logging version of message3. */
5989
5990 void
5991 message3_nolog (m, nbytes, multibyte)
5992 Lisp_Object m;
5993 int nbytes, multibyte;
5994 {
5995 struct frame *sf = SELECTED_FRAME ();
5996 message_enable_multibyte = multibyte;
5997
5998 if (noninteractive)
5999 {
6000 if (noninteractive_need_newline)
6001 putc ('\n', stderr);
6002 noninteractive_need_newline = 0;
6003 if (STRINGP (m))
6004 fwrite (SDATA (m), nbytes, 1, stderr);
6005 if (cursor_in_echo_area == 0)
6006 fprintf (stderr, "\n");
6007 fflush (stderr);
6008 }
6009 /* A null message buffer means that the frame hasn't really been
6010 initialized yet. Error messages get reported properly by
6011 cmd_error, so this must be just an informative message; toss it. */
6012 else if (INTERACTIVE
6013 && sf->glyphs_initialized_p
6014 && FRAME_MESSAGE_BUF (sf))
6015 {
6016 Lisp_Object mini_window;
6017 Lisp_Object frame;
6018 struct frame *f;
6019
6020 /* Get the frame containing the mini-buffer
6021 that the selected frame is using. */
6022 mini_window = FRAME_MINIBUF_WINDOW (sf);
6023 frame = XWINDOW (mini_window)->frame;
6024 f = XFRAME (frame);
6025
6026 FRAME_SAMPLE_VISIBILITY (f);
6027 if (FRAME_VISIBLE_P (sf)
6028 && !FRAME_VISIBLE_P (f))
6029 Fmake_frame_visible (frame);
6030
6031 if (STRINGP (m) && SCHARS (m) > 0)
6032 {
6033 set_message (NULL, m, nbytes, multibyte);
6034 if (minibuffer_auto_raise)
6035 Fraise_frame (frame);
6036 }
6037 else
6038 clear_message (1, 1);
6039
6040 do_pending_window_change (0);
6041 echo_area_display (1);
6042 do_pending_window_change (0);
6043 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6044 (*frame_up_to_date_hook) (f);
6045 }
6046 }
6047
6048
6049 /* Display a null-terminated echo area message M. If M is 0, clear
6050 out any existing message, and let the mini-buffer text show through.
6051
6052 The buffer M must continue to exist until after the echo area gets
6053 cleared or some other message gets displayed there. Do not pass
6054 text that is stored in a Lisp string. Do not pass text in a buffer
6055 that was alloca'd. */
6056
6057 void
6058 message1 (m)
6059 char *m;
6060 {
6061 message2 (m, (m ? strlen (m) : 0), 0);
6062 }
6063
6064
6065 /* The non-logging counterpart of message1. */
6066
6067 void
6068 message1_nolog (m)
6069 char *m;
6070 {
6071 message2_nolog (m, (m ? strlen (m) : 0), 0);
6072 }
6073
6074 /* Display a message M which contains a single %s
6075 which gets replaced with STRING. */
6076
6077 void
6078 message_with_string (m, string, log)
6079 char *m;
6080 Lisp_Object string;
6081 int log;
6082 {
6083 CHECK_STRING (string);
6084
6085 if (noninteractive)
6086 {
6087 if (m)
6088 {
6089 if (noninteractive_need_newline)
6090 putc ('\n', stderr);
6091 noninteractive_need_newline = 0;
6092 fprintf (stderr, m, SDATA (string));
6093 if (cursor_in_echo_area == 0)
6094 fprintf (stderr, "\n");
6095 fflush (stderr);
6096 }
6097 }
6098 else if (INTERACTIVE)
6099 {
6100 /* The frame whose minibuffer we're going to display the message on.
6101 It may be larger than the selected frame, so we need
6102 to use its buffer, not the selected frame's buffer. */
6103 Lisp_Object mini_window;
6104 struct frame *f, *sf = SELECTED_FRAME ();
6105
6106 /* Get the frame containing the minibuffer
6107 that the selected frame is using. */
6108 mini_window = FRAME_MINIBUF_WINDOW (sf);
6109 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6110
6111 /* A null message buffer means that the frame hasn't really been
6112 initialized yet. Error messages get reported properly by
6113 cmd_error, so this must be just an informative message; toss it. */
6114 if (FRAME_MESSAGE_BUF (f))
6115 {
6116 Lisp_Object args[2], message;
6117 struct gcpro gcpro1, gcpro2;
6118
6119 args[0] = build_string (m);
6120 args[1] = message = string;
6121 GCPRO2 (args[0], message);
6122 gcpro1.nvars = 2;
6123
6124 message = Fformat (2, args);
6125
6126 if (log)
6127 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6128 else
6129 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6130
6131 UNGCPRO;
6132
6133 /* Print should start at the beginning of the message
6134 buffer next time. */
6135 message_buf_print = 0;
6136 }
6137 }
6138 }
6139
6140
6141 /* Dump an informative message to the minibuf. If M is 0, clear out
6142 any existing message, and let the mini-buffer text show through. */
6143
6144 /* VARARGS 1 */
6145 void
6146 message (m, a1, a2, a3)
6147 char *m;
6148 EMACS_INT a1, a2, a3;
6149 {
6150 if (noninteractive)
6151 {
6152 if (m)
6153 {
6154 if (noninteractive_need_newline)
6155 putc ('\n', stderr);
6156 noninteractive_need_newline = 0;
6157 fprintf (stderr, m, a1, a2, a3);
6158 if (cursor_in_echo_area == 0)
6159 fprintf (stderr, "\n");
6160 fflush (stderr);
6161 }
6162 }
6163 else if (INTERACTIVE)
6164 {
6165 /* The frame whose mini-buffer we're going to display the message
6166 on. It may be larger than the selected frame, so we need to
6167 use its buffer, not the selected frame's buffer. */
6168 Lisp_Object mini_window;
6169 struct frame *f, *sf = SELECTED_FRAME ();
6170
6171 /* Get the frame containing the mini-buffer
6172 that the selected frame is using. */
6173 mini_window = FRAME_MINIBUF_WINDOW (sf);
6174 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6175
6176 /* A null message buffer means that the frame hasn't really been
6177 initialized yet. Error messages get reported properly by
6178 cmd_error, so this must be just an informative message; toss
6179 it. */
6180 if (FRAME_MESSAGE_BUF (f))
6181 {
6182 if (m)
6183 {
6184 int len;
6185 #ifdef NO_ARG_ARRAY
6186 char *a[3];
6187 a[0] = (char *) a1;
6188 a[1] = (char *) a2;
6189 a[2] = (char *) a3;
6190
6191 len = doprnt (FRAME_MESSAGE_BUF (f),
6192 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6193 #else
6194 len = doprnt (FRAME_MESSAGE_BUF (f),
6195 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6196 (char **) &a1);
6197 #endif /* NO_ARG_ARRAY */
6198
6199 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6200 }
6201 else
6202 message1 (0);
6203
6204 /* Print should start at the beginning of the message
6205 buffer next time. */
6206 message_buf_print = 0;
6207 }
6208 }
6209 }
6210
6211
6212 /* The non-logging version of message. */
6213
6214 void
6215 message_nolog (m, a1, a2, a3)
6216 char *m;
6217 EMACS_INT a1, a2, a3;
6218 {
6219 Lisp_Object old_log_max;
6220 old_log_max = Vmessage_log_max;
6221 Vmessage_log_max = Qnil;
6222 message (m, a1, a2, a3);
6223 Vmessage_log_max = old_log_max;
6224 }
6225
6226
6227 /* Display the current message in the current mini-buffer. This is
6228 only called from error handlers in process.c, and is not time
6229 critical. */
6230
6231 void
6232 update_echo_area ()
6233 {
6234 if (!NILP (echo_area_buffer[0]))
6235 {
6236 Lisp_Object string;
6237 string = Fcurrent_message ();
6238 message3 (string, SBYTES (string),
6239 !NILP (current_buffer->enable_multibyte_characters));
6240 }
6241 }
6242
6243
6244 /* Make sure echo area buffers in `echo_buffers' are live.
6245 If they aren't, make new ones. */
6246
6247 static void
6248 ensure_echo_area_buffers ()
6249 {
6250 int i;
6251
6252 for (i = 0; i < 2; ++i)
6253 if (!BUFFERP (echo_buffer[i])
6254 || NILP (XBUFFER (echo_buffer[i])->name))
6255 {
6256 char name[30];
6257 Lisp_Object old_buffer;
6258 int j;
6259
6260 old_buffer = echo_buffer[i];
6261 sprintf (name, " *Echo Area %d*", i);
6262 echo_buffer[i] = Fget_buffer_create (build_string (name));
6263 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6264
6265 for (j = 0; j < 2; ++j)
6266 if (EQ (old_buffer, echo_area_buffer[j]))
6267 echo_area_buffer[j] = echo_buffer[i];
6268 }
6269 }
6270
6271
6272 /* Call FN with args A1..A4 with either the current or last displayed
6273 echo_area_buffer as current buffer.
6274
6275 WHICH zero means use the current message buffer
6276 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6277 from echo_buffer[] and clear it.
6278
6279 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6280 suitable buffer from echo_buffer[] and clear it.
6281
6282 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6283 that the current message becomes the last displayed one, make
6284 choose a suitable buffer for echo_area_buffer[0], and clear it.
6285
6286 Value is what FN returns. */
6287
6288 static int
6289 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6290 struct window *w;
6291 int which;
6292 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6293 EMACS_INT a1;
6294 Lisp_Object a2;
6295 EMACS_INT a3, a4;
6296 {
6297 Lisp_Object buffer;
6298 int this_one, the_other, clear_buffer_p, rc;
6299 int count = SPECPDL_INDEX ();
6300
6301 /* If buffers aren't live, make new ones. */
6302 ensure_echo_area_buffers ();
6303
6304 clear_buffer_p = 0;
6305
6306 if (which == 0)
6307 this_one = 0, the_other = 1;
6308 else if (which > 0)
6309 this_one = 1, the_other = 0;
6310 else
6311 {
6312 this_one = 0, the_other = 1;
6313 clear_buffer_p = 1;
6314
6315 /* We need a fresh one in case the current echo buffer equals
6316 the one containing the last displayed echo area message. */
6317 if (!NILP (echo_area_buffer[this_one])
6318 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6319 echo_area_buffer[this_one] = Qnil;
6320 }
6321
6322 /* Choose a suitable buffer from echo_buffer[] is we don't
6323 have one. */
6324 if (NILP (echo_area_buffer[this_one]))
6325 {
6326 echo_area_buffer[this_one]
6327 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6328 ? echo_buffer[the_other]
6329 : echo_buffer[this_one]);
6330 clear_buffer_p = 1;
6331 }
6332
6333 buffer = echo_area_buffer[this_one];
6334
6335 /* Don't get confused by reusing the buffer used for echoing
6336 for a different purpose. */
6337 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6338 cancel_echoing ();
6339
6340 record_unwind_protect (unwind_with_echo_area_buffer,
6341 with_echo_area_buffer_unwind_data (w));
6342
6343 /* Make the echo area buffer current. Note that for display
6344 purposes, it is not necessary that the displayed window's buffer
6345 == current_buffer, except for text property lookup. So, let's
6346 only set that buffer temporarily here without doing a full
6347 Fset_window_buffer. We must also change w->pointm, though,
6348 because otherwise an assertions in unshow_buffer fails, and Emacs
6349 aborts. */
6350 set_buffer_internal_1 (XBUFFER (buffer));
6351 if (w)
6352 {
6353 w->buffer = buffer;
6354 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6355 }
6356
6357 current_buffer->undo_list = Qt;
6358 current_buffer->read_only = Qnil;
6359 specbind (Qinhibit_read_only, Qt);
6360 specbind (Qinhibit_modification_hooks, Qt);
6361
6362 if (clear_buffer_p && Z > BEG)
6363 del_range (BEG, Z);
6364
6365 xassert (BEGV >= BEG);
6366 xassert (ZV <= Z && ZV >= BEGV);
6367
6368 rc = fn (a1, a2, a3, a4);
6369
6370 xassert (BEGV >= BEG);
6371 xassert (ZV <= Z && ZV >= BEGV);
6372
6373 unbind_to (count, Qnil);
6374 return rc;
6375 }
6376
6377
6378 /* Save state that should be preserved around the call to the function
6379 FN called in with_echo_area_buffer. */
6380
6381 static Lisp_Object
6382 with_echo_area_buffer_unwind_data (w)
6383 struct window *w;
6384 {
6385 int i = 0;
6386 Lisp_Object vector;
6387
6388 /* Reduce consing by keeping one vector in
6389 Vwith_echo_area_save_vector. */
6390 vector = Vwith_echo_area_save_vector;
6391 Vwith_echo_area_save_vector = Qnil;
6392
6393 if (NILP (vector))
6394 vector = Fmake_vector (make_number (7), Qnil);
6395
6396 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6397 AREF (vector, i) = Vdeactivate_mark, ++i;
6398 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6399
6400 if (w)
6401 {
6402 XSETWINDOW (AREF (vector, i), w); ++i;
6403 AREF (vector, i) = w->buffer; ++i;
6404 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6405 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6406 }
6407 else
6408 {
6409 int end = i + 4;
6410 for (; i < end; ++i)
6411 AREF (vector, i) = Qnil;
6412 }
6413
6414 xassert (i == ASIZE (vector));
6415 return vector;
6416 }
6417
6418
6419 /* Restore global state from VECTOR which was created by
6420 with_echo_area_buffer_unwind_data. */
6421
6422 static Lisp_Object
6423 unwind_with_echo_area_buffer (vector)
6424 Lisp_Object vector;
6425 {
6426 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6427 Vdeactivate_mark = AREF (vector, 1);
6428 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6429
6430 if (WINDOWP (AREF (vector, 3)))
6431 {
6432 struct window *w;
6433 Lisp_Object buffer, charpos, bytepos;
6434
6435 w = XWINDOW (AREF (vector, 3));
6436 buffer = AREF (vector, 4);
6437 charpos = AREF (vector, 5);
6438 bytepos = AREF (vector, 6);
6439
6440 w->buffer = buffer;
6441 set_marker_both (w->pointm, buffer,
6442 XFASTINT (charpos), XFASTINT (bytepos));
6443 }
6444
6445 Vwith_echo_area_save_vector = vector;
6446 return Qnil;
6447 }
6448
6449
6450 /* Set up the echo area for use by print functions. MULTIBYTE_P
6451 non-zero means we will print multibyte. */
6452
6453 void
6454 setup_echo_area_for_printing (multibyte_p)
6455 int multibyte_p;
6456 {
6457 ensure_echo_area_buffers ();
6458
6459 if (!message_buf_print)
6460 {
6461 /* A message has been output since the last time we printed.
6462 Choose a fresh echo area buffer. */
6463 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6464 echo_area_buffer[0] = echo_buffer[1];
6465 else
6466 echo_area_buffer[0] = echo_buffer[0];
6467
6468 /* Switch to that buffer and clear it. */
6469 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6470 current_buffer->truncate_lines = Qnil;
6471
6472 if (Z > BEG)
6473 {
6474 int count = SPECPDL_INDEX ();
6475 specbind (Qinhibit_read_only, Qt);
6476 /* Note that undo recording is always disabled. */
6477 del_range (BEG, Z);
6478 unbind_to (count, Qnil);
6479 }
6480 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6481
6482 /* Set up the buffer for the multibyteness we need. */
6483 if (multibyte_p
6484 != !NILP (current_buffer->enable_multibyte_characters))
6485 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6486
6487 /* Raise the frame containing the echo area. */
6488 if (minibuffer_auto_raise)
6489 {
6490 struct frame *sf = SELECTED_FRAME ();
6491 Lisp_Object mini_window;
6492 mini_window = FRAME_MINIBUF_WINDOW (sf);
6493 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6494 }
6495
6496 message_log_maybe_newline ();
6497 message_buf_print = 1;
6498 }
6499 else
6500 {
6501 if (NILP (echo_area_buffer[0]))
6502 {
6503 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6504 echo_area_buffer[0] = echo_buffer[1];
6505 else
6506 echo_area_buffer[0] = echo_buffer[0];
6507 }
6508
6509 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6510 {
6511 /* Someone switched buffers between print requests. */
6512 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6513 current_buffer->truncate_lines = Qnil;
6514 }
6515 }
6516 }
6517
6518
6519 /* Display an echo area message in window W. Value is non-zero if W's
6520 height is changed. If display_last_displayed_message_p is
6521 non-zero, display the message that was last displayed, otherwise
6522 display the current message. */
6523
6524 static int
6525 display_echo_area (w)
6526 struct window *w;
6527 {
6528 int i, no_message_p, window_height_changed_p, count;
6529
6530 /* Temporarily disable garbage collections while displaying the echo
6531 area. This is done because a GC can print a message itself.
6532 That message would modify the echo area buffer's contents while a
6533 redisplay of the buffer is going on, and seriously confuse
6534 redisplay. */
6535 count = inhibit_garbage_collection ();
6536
6537 /* If there is no message, we must call display_echo_area_1
6538 nevertheless because it resizes the window. But we will have to
6539 reset the echo_area_buffer in question to nil at the end because
6540 with_echo_area_buffer will sets it to an empty buffer. */
6541 i = display_last_displayed_message_p ? 1 : 0;
6542 no_message_p = NILP (echo_area_buffer[i]);
6543
6544 window_height_changed_p
6545 = with_echo_area_buffer (w, display_last_displayed_message_p,
6546 display_echo_area_1,
6547 (EMACS_INT) w, Qnil, 0, 0);
6548
6549 if (no_message_p)
6550 echo_area_buffer[i] = Qnil;
6551
6552 unbind_to (count, Qnil);
6553 return window_height_changed_p;
6554 }
6555
6556
6557 /* Helper for display_echo_area. Display the current buffer which
6558 contains the current echo area message in window W, a mini-window,
6559 a pointer to which is passed in A1. A2..A4 are currently not used.
6560 Change the height of W so that all of the message is displayed.
6561 Value is non-zero if height of W was changed. */
6562
6563 static int
6564 display_echo_area_1 (a1, a2, a3, a4)
6565 EMACS_INT a1;
6566 Lisp_Object a2;
6567 EMACS_INT a3, a4;
6568 {
6569 struct window *w = (struct window *) a1;
6570 Lisp_Object window;
6571 struct text_pos start;
6572 int window_height_changed_p = 0;
6573
6574 /* Do this before displaying, so that we have a large enough glyph
6575 matrix for the display. */
6576 window_height_changed_p = resize_mini_window (w, 0);
6577
6578 /* Display. */
6579 clear_glyph_matrix (w->desired_matrix);
6580 XSETWINDOW (window, w);
6581 SET_TEXT_POS (start, BEG, BEG_BYTE);
6582 try_window (window, start);
6583
6584 return window_height_changed_p;
6585 }
6586
6587
6588 /* Resize the echo area window to exactly the size needed for the
6589 currently displayed message, if there is one. If a mini-buffer
6590 is active, don't shrink it. */
6591
6592 void
6593 resize_echo_area_exactly ()
6594 {
6595 if (BUFFERP (echo_area_buffer[0])
6596 && WINDOWP (echo_area_window))
6597 {
6598 struct window *w = XWINDOW (echo_area_window);
6599 int resized_p;
6600 Lisp_Object resize_exactly;
6601
6602 if (minibuf_level == 0)
6603 resize_exactly = Qt;
6604 else
6605 resize_exactly = Qnil;
6606
6607 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6608 (EMACS_INT) w, resize_exactly, 0, 0);
6609 if (resized_p)
6610 {
6611 ++windows_or_buffers_changed;
6612 ++update_mode_lines;
6613 redisplay_internal (0);
6614 }
6615 }
6616 }
6617
6618
6619 /* Callback function for with_echo_area_buffer, when used from
6620 resize_echo_area_exactly. A1 contains a pointer to the window to
6621 resize, EXACTLY non-nil means resize the mini-window exactly to the
6622 size of the text displayed. A3 and A4 are not used. Value is what
6623 resize_mini_window returns. */
6624
6625 static int
6626 resize_mini_window_1 (a1, exactly, a3, a4)
6627 EMACS_INT a1;
6628 Lisp_Object exactly;
6629 EMACS_INT a3, a4;
6630 {
6631 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6632 }
6633
6634
6635 /* Resize mini-window W to fit the size of its contents. EXACT:P
6636 means size the window exactly to the size needed. Otherwise, it's
6637 only enlarged until W's buffer is empty. Value is non-zero if
6638 the window height has been changed. */
6639
6640 int
6641 resize_mini_window (w, exact_p)
6642 struct window *w;
6643 int exact_p;
6644 {
6645 struct frame *f = XFRAME (w->frame);
6646 int window_height_changed_p = 0;
6647
6648 xassert (MINI_WINDOW_P (w));
6649
6650 /* Don't resize windows while redisplaying a window; it would
6651 confuse redisplay functions when the size of the window they are
6652 displaying changes from under them. Such a resizing can happen,
6653 for instance, when which-func prints a long message while
6654 we are running fontification-functions. We're running these
6655 functions with safe_call which binds inhibit-redisplay to t. */
6656 if (!NILP (Vinhibit_redisplay))
6657 return 0;
6658
6659 /* Nil means don't try to resize. */
6660 if (NILP (Vresize_mini_windows)
6661 || (FRAME_X_P (f) && f->output_data.x == NULL))
6662 return 0;
6663
6664 if (!FRAME_MINIBUF_ONLY_P (f))
6665 {
6666 struct it it;
6667 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6668 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6669 int height, max_height;
6670 int unit = CANON_Y_UNIT (f);
6671 struct text_pos start;
6672 struct buffer *old_current_buffer = NULL;
6673
6674 if (current_buffer != XBUFFER (w->buffer))
6675 {
6676 old_current_buffer = current_buffer;
6677 set_buffer_internal (XBUFFER (w->buffer));
6678 }
6679
6680 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6681
6682 /* Compute the max. number of lines specified by the user. */
6683 if (FLOATP (Vmax_mini_window_height))
6684 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6685 else if (INTEGERP (Vmax_mini_window_height))
6686 max_height = XINT (Vmax_mini_window_height);
6687 else
6688 max_height = total_height / 4;
6689
6690 /* Correct that max. height if it's bogus. */
6691 max_height = max (1, max_height);
6692 max_height = min (total_height, max_height);
6693
6694 /* Find out the height of the text in the window. */
6695 if (it.truncate_lines_p)
6696 height = 1;
6697 else
6698 {
6699 last_height = 0;
6700 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6701 if (it.max_ascent == 0 && it.max_descent == 0)
6702 height = it.current_y + last_height;
6703 else
6704 height = it.current_y + it.max_ascent + it.max_descent;
6705 height -= it.extra_line_spacing;
6706 height = (height + unit - 1) / unit;
6707 }
6708
6709 /* Compute a suitable window start. */
6710 if (height > max_height)
6711 {
6712 height = max_height;
6713 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6714 move_it_vertically_backward (&it, (height - 1) * unit);
6715 start = it.current.pos;
6716 }
6717 else
6718 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6719 SET_MARKER_FROM_TEXT_POS (w->start, start);
6720
6721 if (EQ (Vresize_mini_windows, Qgrow_only))
6722 {
6723 /* Let it grow only, until we display an empty message, in which
6724 case the window shrinks again. */
6725 if (height > XFASTINT (w->height))
6726 {
6727 int old_height = XFASTINT (w->height);
6728 freeze_window_starts (f, 1);
6729 grow_mini_window (w, height - XFASTINT (w->height));
6730 window_height_changed_p = XFASTINT (w->height) != old_height;
6731 }
6732 else if (height < XFASTINT (w->height)
6733 && (exact_p || BEGV == ZV))
6734 {
6735 int old_height = XFASTINT (w->height);
6736 freeze_window_starts (f, 0);
6737 shrink_mini_window (w);
6738 window_height_changed_p = XFASTINT (w->height) != old_height;
6739 }
6740 }
6741 else
6742 {
6743 /* Always resize to exact size needed. */
6744 if (height > XFASTINT (w->height))
6745 {
6746 int old_height = XFASTINT (w->height);
6747 freeze_window_starts (f, 1);
6748 grow_mini_window (w, height - XFASTINT (w->height));
6749 window_height_changed_p = XFASTINT (w->height) != old_height;
6750 }
6751 else if (height < XFASTINT (w->height))
6752 {
6753 int old_height = XFASTINT (w->height);
6754 freeze_window_starts (f, 0);
6755 shrink_mini_window (w);
6756
6757 if (height)
6758 {
6759 freeze_window_starts (f, 1);
6760 grow_mini_window (w, height - XFASTINT (w->height));
6761 }
6762
6763 window_height_changed_p = XFASTINT (w->height) != old_height;
6764 }
6765 }
6766
6767 if (old_current_buffer)
6768 set_buffer_internal (old_current_buffer);
6769 }
6770
6771 return window_height_changed_p;
6772 }
6773
6774
6775 /* Value is the current message, a string, or nil if there is no
6776 current message. */
6777
6778 Lisp_Object
6779 current_message ()
6780 {
6781 Lisp_Object msg;
6782
6783 if (NILP (echo_area_buffer[0]))
6784 msg = Qnil;
6785 else
6786 {
6787 with_echo_area_buffer (0, 0, current_message_1,
6788 (EMACS_INT) &msg, Qnil, 0, 0);
6789 if (NILP (msg))
6790 echo_area_buffer[0] = Qnil;
6791 }
6792
6793 return msg;
6794 }
6795
6796
6797 static int
6798 current_message_1 (a1, a2, a3, a4)
6799 EMACS_INT a1;
6800 Lisp_Object a2;
6801 EMACS_INT a3, a4;
6802 {
6803 Lisp_Object *msg = (Lisp_Object *) a1;
6804
6805 if (Z > BEG)
6806 *msg = make_buffer_string (BEG, Z, 1);
6807 else
6808 *msg = Qnil;
6809 return 0;
6810 }
6811
6812
6813 /* Push the current message on Vmessage_stack for later restauration
6814 by restore_message. Value is non-zero if the current message isn't
6815 empty. This is a relatively infrequent operation, so it's not
6816 worth optimizing. */
6817
6818 int
6819 push_message ()
6820 {
6821 Lisp_Object msg;
6822 msg = current_message ();
6823 Vmessage_stack = Fcons (msg, Vmessage_stack);
6824 return STRINGP (msg);
6825 }
6826
6827
6828 /* Handler for record_unwind_protect calling pop_message. */
6829
6830 Lisp_Object
6831 push_message_unwind (dummy)
6832 Lisp_Object dummy;
6833 {
6834 pop_message ();
6835 return Qnil;
6836 }
6837
6838
6839 /* Restore message display from the top of Vmessage_stack. */
6840
6841 void
6842 restore_message ()
6843 {
6844 Lisp_Object msg;
6845
6846 xassert (CONSP (Vmessage_stack));
6847 msg = XCAR (Vmessage_stack);
6848 if (STRINGP (msg))
6849 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
6850 else
6851 message3_nolog (msg, 0, 0);
6852 }
6853
6854
6855 /* Pop the top-most entry off Vmessage_stack. */
6856
6857 void
6858 pop_message ()
6859 {
6860 xassert (CONSP (Vmessage_stack));
6861 Vmessage_stack = XCDR (Vmessage_stack);
6862 }
6863
6864
6865 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6866 exits. If the stack is not empty, we have a missing pop_message
6867 somewhere. */
6868
6869 void
6870 check_message_stack ()
6871 {
6872 if (!NILP (Vmessage_stack))
6873 abort ();
6874 }
6875
6876
6877 /* Truncate to NCHARS what will be displayed in the echo area the next
6878 time we display it---but don't redisplay it now. */
6879
6880 void
6881 truncate_echo_area (nchars)
6882 int nchars;
6883 {
6884 if (nchars == 0)
6885 echo_area_buffer[0] = Qnil;
6886 /* A null message buffer means that the frame hasn't really been
6887 initialized yet. Error messages get reported properly by
6888 cmd_error, so this must be just an informative message; toss it. */
6889 else if (!noninteractive
6890 && INTERACTIVE
6891 && !NILP (echo_area_buffer[0]))
6892 {
6893 struct frame *sf = SELECTED_FRAME ();
6894 if (FRAME_MESSAGE_BUF (sf))
6895 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6896 }
6897 }
6898
6899
6900 /* Helper function for truncate_echo_area. Truncate the current
6901 message to at most NCHARS characters. */
6902
6903 static int
6904 truncate_message_1 (nchars, a2, a3, a4)
6905 EMACS_INT nchars;
6906 Lisp_Object a2;
6907 EMACS_INT a3, a4;
6908 {
6909 if (BEG + nchars < Z)
6910 del_range (BEG + nchars, Z);
6911 if (Z == BEG)
6912 echo_area_buffer[0] = Qnil;
6913 return 0;
6914 }
6915
6916
6917 /* Set the current message to a substring of S or STRING.
6918
6919 If STRING is a Lisp string, set the message to the first NBYTES
6920 bytes from STRING. NBYTES zero means use the whole string. If
6921 STRING is multibyte, the message will be displayed multibyte.
6922
6923 If S is not null, set the message to the first LEN bytes of S. LEN
6924 zero means use the whole string. MULTIBYTE_P non-zero means S is
6925 multibyte. Display the message multibyte in that case. */
6926
6927 void
6928 set_message (s, string, nbytes, multibyte_p)
6929 const char *s;
6930 Lisp_Object string;
6931 int nbytes;
6932 {
6933 message_enable_multibyte
6934 = ((s && multibyte_p)
6935 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6936
6937 with_echo_area_buffer (0, -1, set_message_1,
6938 (EMACS_INT) s, string, nbytes, multibyte_p);
6939 message_buf_print = 0;
6940 help_echo_showing_p = 0;
6941 }
6942
6943
6944 /* Helper function for set_message. Arguments have the same meaning
6945 as there, with A1 corresponding to S and A2 corresponding to STRING
6946 This function is called with the echo area buffer being
6947 current. */
6948
6949 static int
6950 set_message_1 (a1, a2, nbytes, multibyte_p)
6951 EMACS_INT a1;
6952 Lisp_Object a2;
6953 EMACS_INT nbytes, multibyte_p;
6954 {
6955 const char *s = (const char *) a1;
6956 Lisp_Object string = a2;
6957
6958 xassert (BEG == Z);
6959
6960 /* Change multibyteness of the echo buffer appropriately. */
6961 if (message_enable_multibyte
6962 != !NILP (current_buffer->enable_multibyte_characters))
6963 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6964
6965 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6966
6967 /* Insert new message at BEG. */
6968 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6969
6970 if (STRINGP (string))
6971 {
6972 int nchars;
6973
6974 if (nbytes == 0)
6975 nbytes = SBYTES (string);
6976 nchars = string_byte_to_char (string, nbytes);
6977
6978 /* This function takes care of single/multibyte conversion. We
6979 just have to ensure that the echo area buffer has the right
6980 setting of enable_multibyte_characters. */
6981 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6982 }
6983 else if (s)
6984 {
6985 if (nbytes == 0)
6986 nbytes = strlen (s);
6987
6988 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6989 {
6990 /* Convert from multi-byte to single-byte. */
6991 int i, c, n;
6992 unsigned char work[1];
6993
6994 /* Convert a multibyte string to single-byte. */
6995 for (i = 0; i < nbytes; i += n)
6996 {
6997 c = string_char_and_length (s + i, nbytes - i, &n);
6998 work[0] = (SINGLE_BYTE_CHAR_P (c)
6999 ? c
7000 : multibyte_char_to_unibyte (c, Qnil));
7001 insert_1_both (work, 1, 1, 1, 0, 0);
7002 }
7003 }
7004 else if (!multibyte_p
7005 && !NILP (current_buffer->enable_multibyte_characters))
7006 {
7007 /* Convert from single-byte to multi-byte. */
7008 int i, c, n;
7009 const unsigned char *msg = (const unsigned char *) s;
7010 unsigned char str[MAX_MULTIBYTE_LENGTH];
7011
7012 /* Convert a single-byte string to multibyte. */
7013 for (i = 0; i < nbytes; i++)
7014 {
7015 c = unibyte_char_to_multibyte (msg[i]);
7016 n = CHAR_STRING (c, str);
7017 insert_1_both (str, 1, n, 1, 0, 0);
7018 }
7019 }
7020 else
7021 insert_1 (s, nbytes, 1, 0, 0);
7022 }
7023
7024 return 0;
7025 }
7026
7027
7028 /* Clear messages. CURRENT_P non-zero means clear the current
7029 message. LAST_DISPLAYED_P non-zero means clear the message
7030 last displayed. */
7031
7032 void
7033 clear_message (current_p, last_displayed_p)
7034 int current_p, last_displayed_p;
7035 {
7036 if (current_p)
7037 {
7038 echo_area_buffer[0] = Qnil;
7039 message_cleared_p = 1;
7040 }
7041
7042 if (last_displayed_p)
7043 echo_area_buffer[1] = Qnil;
7044
7045 message_buf_print = 0;
7046 }
7047
7048 /* Clear garbaged frames.
7049
7050 This function is used where the old redisplay called
7051 redraw_garbaged_frames which in turn called redraw_frame which in
7052 turn called clear_frame. The call to clear_frame was a source of
7053 flickering. I believe a clear_frame is not necessary. It should
7054 suffice in the new redisplay to invalidate all current matrices,
7055 and ensure a complete redisplay of all windows. */
7056
7057 static void
7058 clear_garbaged_frames ()
7059 {
7060 if (frame_garbaged)
7061 {
7062 Lisp_Object tail, frame;
7063 int changed_count = 0;
7064
7065 FOR_EACH_FRAME (tail, frame)
7066 {
7067 struct frame *f = XFRAME (frame);
7068
7069 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7070 {
7071 if (f->resized_p)
7072 Fredraw_frame (frame);
7073 clear_current_matrices (f);
7074 changed_count++;
7075 f->garbaged = 0;
7076 f->resized_p = 0;
7077 }
7078 }
7079
7080 frame_garbaged = 0;
7081 if (changed_count)
7082 ++windows_or_buffers_changed;
7083 }
7084 }
7085
7086
7087 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7088 is non-zero update selected_frame. Value is non-zero if the
7089 mini-windows height has been changed. */
7090
7091 static int
7092 echo_area_display (update_frame_p)
7093 int update_frame_p;
7094 {
7095 Lisp_Object mini_window;
7096 struct window *w;
7097 struct frame *f;
7098 int window_height_changed_p = 0;
7099 struct frame *sf = SELECTED_FRAME ();
7100
7101 mini_window = FRAME_MINIBUF_WINDOW (sf);
7102 w = XWINDOW (mini_window);
7103 f = XFRAME (WINDOW_FRAME (w));
7104
7105 /* Don't display if frame is invisible or not yet initialized. */
7106 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7107 return 0;
7108
7109 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7110 #ifndef MAC_OS8
7111 #ifdef HAVE_WINDOW_SYSTEM
7112 /* When Emacs starts, selected_frame may be a visible terminal
7113 frame, even if we run under a window system. If we let this
7114 through, a message would be displayed on the terminal. */
7115 if (EQ (selected_frame, Vterminal_frame)
7116 && !NILP (Vwindow_system))
7117 return 0;
7118 #endif /* HAVE_WINDOW_SYSTEM */
7119 #endif
7120
7121 /* Redraw garbaged frames. */
7122 if (frame_garbaged)
7123 clear_garbaged_frames ();
7124
7125 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7126 {
7127 echo_area_window = mini_window;
7128 window_height_changed_p = display_echo_area (w);
7129 w->must_be_updated_p = 1;
7130
7131 /* Update the display, unless called from redisplay_internal.
7132 Also don't update the screen during redisplay itself. The
7133 update will happen at the end of redisplay, and an update
7134 here could cause confusion. */
7135 if (update_frame_p && !redisplaying_p)
7136 {
7137 int n = 0;
7138
7139 /* If the display update has been interrupted by pending
7140 input, update mode lines in the frame. Due to the
7141 pending input, it might have been that redisplay hasn't
7142 been called, so that mode lines above the echo area are
7143 garbaged. This looks odd, so we prevent it here. */
7144 if (!display_completed)
7145 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7146
7147 if (window_height_changed_p
7148 /* Don't do this if Emacs is shutting down. Redisplay
7149 needs to run hooks. */
7150 && !NILP (Vrun_hooks))
7151 {
7152 /* Must update other windows. Likewise as in other
7153 cases, don't let this update be interrupted by
7154 pending input. */
7155 int count = SPECPDL_INDEX ();
7156 specbind (Qredisplay_dont_pause, Qt);
7157 windows_or_buffers_changed = 1;
7158 redisplay_internal (0);
7159 unbind_to (count, Qnil);
7160 }
7161 else if (FRAME_WINDOW_P (f) && n == 0)
7162 {
7163 /* Window configuration is the same as before.
7164 Can do with a display update of the echo area,
7165 unless we displayed some mode lines. */
7166 update_single_window (w, 1);
7167 rif->flush_display (f);
7168 }
7169 else
7170 update_frame (f, 1, 1);
7171
7172 /* If cursor is in the echo area, make sure that the next
7173 redisplay displays the minibuffer, so that the cursor will
7174 be replaced with what the minibuffer wants. */
7175 if (cursor_in_echo_area)
7176 ++windows_or_buffers_changed;
7177 }
7178 }
7179 else if (!EQ (mini_window, selected_window))
7180 windows_or_buffers_changed++;
7181
7182 /* Last displayed message is now the current message. */
7183 echo_area_buffer[1] = echo_area_buffer[0];
7184
7185 /* Prevent redisplay optimization in redisplay_internal by resetting
7186 this_line_start_pos. This is done because the mini-buffer now
7187 displays the message instead of its buffer text. */
7188 if (EQ (mini_window, selected_window))
7189 CHARPOS (this_line_start_pos) = 0;
7190
7191 return window_height_changed_p;
7192 }
7193
7194
7195 \f
7196 /***********************************************************************
7197 Frame Titles
7198 ***********************************************************************/
7199
7200
7201 /* The frame title buffering code is also used by Fformat_mode_line.
7202 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7203
7204 /* A buffer for constructing frame titles in it; allocated from the
7205 heap in init_xdisp and resized as needed in store_frame_title_char. */
7206
7207 static char *frame_title_buf;
7208
7209 /* The buffer's end, and a current output position in it. */
7210
7211 static char *frame_title_buf_end;
7212 static char *frame_title_ptr;
7213
7214
7215 /* Store a single character C for the frame title in frame_title_buf.
7216 Re-allocate frame_title_buf if necessary. */
7217
7218 static void
7219 store_frame_title_char (c)
7220 char c;
7221 {
7222 /* If output position has reached the end of the allocated buffer,
7223 double the buffer's size. */
7224 if (frame_title_ptr == frame_title_buf_end)
7225 {
7226 int len = frame_title_ptr - frame_title_buf;
7227 int new_size = 2 * len * sizeof *frame_title_buf;
7228 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7229 frame_title_buf_end = frame_title_buf + new_size;
7230 frame_title_ptr = frame_title_buf + len;
7231 }
7232
7233 *frame_title_ptr++ = c;
7234 }
7235
7236
7237 /* Store part of a frame title in frame_title_buf, beginning at
7238 frame_title_ptr. STR is the string to store. Do not copy
7239 characters that yield more columns than PRECISION; PRECISION <= 0
7240 means copy the whole string. Pad with spaces until FIELD_WIDTH
7241 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7242 pad. Called from display_mode_element when it is used to build a
7243 frame title. */
7244
7245 static int
7246 store_frame_title (str, field_width, precision)
7247 const unsigned char *str;
7248 int field_width, precision;
7249 {
7250 int n = 0;
7251 int dummy, nbytes;
7252
7253 /* Copy at most PRECISION chars from STR. */
7254 nbytes = strlen (str);
7255 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7256 while (nbytes--)
7257 store_frame_title_char (*str++);
7258
7259 /* Fill up with spaces until FIELD_WIDTH reached. */
7260 while (field_width > 0
7261 && n < field_width)
7262 {
7263 store_frame_title_char (' ');
7264 ++n;
7265 }
7266
7267 return n;
7268 }
7269
7270 #ifdef HAVE_WINDOW_SYSTEM
7271
7272 /* Set the title of FRAME, if it has changed. The title format is
7273 Vicon_title_format if FRAME is iconified, otherwise it is
7274 frame_title_format. */
7275
7276 static void
7277 x_consider_frame_title (frame)
7278 Lisp_Object frame;
7279 {
7280 struct frame *f = XFRAME (frame);
7281
7282 if (FRAME_WINDOW_P (f)
7283 || FRAME_MINIBUF_ONLY_P (f)
7284 || f->explicit_name)
7285 {
7286 /* Do we have more than one visible frame on this X display? */
7287 Lisp_Object tail;
7288 Lisp_Object fmt;
7289 struct buffer *obuf;
7290 int len;
7291 struct it it;
7292
7293 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7294 {
7295 Lisp_Object other_frame = XCAR (tail);
7296 struct frame *tf = XFRAME (other_frame);
7297
7298 if (tf != f
7299 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7300 && !FRAME_MINIBUF_ONLY_P (tf)
7301 && !EQ (other_frame, tip_frame)
7302 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7303 break;
7304 }
7305
7306 /* Set global variable indicating that multiple frames exist. */
7307 multiple_frames = CONSP (tail);
7308
7309 /* Switch to the buffer of selected window of the frame. Set up
7310 frame_title_ptr so that display_mode_element will output into it;
7311 then display the title. */
7312 obuf = current_buffer;
7313 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7314 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7315 frame_title_ptr = frame_title_buf;
7316 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7317 NULL, DEFAULT_FACE_ID);
7318 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7319 len = frame_title_ptr - frame_title_buf;
7320 frame_title_ptr = NULL;
7321 set_buffer_internal_1 (obuf);
7322
7323 /* Set the title only if it's changed. This avoids consing in
7324 the common case where it hasn't. (If it turns out that we've
7325 already wasted too much time by walking through the list with
7326 display_mode_element, then we might need to optimize at a
7327 higher level than this.) */
7328 if (! STRINGP (f->name)
7329 || SBYTES (f->name) != len
7330 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7331 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7332 }
7333 }
7334
7335 #endif /* not HAVE_WINDOW_SYSTEM */
7336
7337
7338
7339 \f
7340 /***********************************************************************
7341 Menu Bars
7342 ***********************************************************************/
7343
7344
7345 /* Prepare for redisplay by updating menu-bar item lists when
7346 appropriate. This can call eval. */
7347
7348 void
7349 prepare_menu_bars ()
7350 {
7351 int all_windows;
7352 struct gcpro gcpro1, gcpro2;
7353 struct frame *f;
7354 Lisp_Object tooltip_frame;
7355
7356 #ifdef HAVE_WINDOW_SYSTEM
7357 tooltip_frame = tip_frame;
7358 #else
7359 tooltip_frame = Qnil;
7360 #endif
7361
7362 /* Update all frame titles based on their buffer names, etc. We do
7363 this before the menu bars so that the buffer-menu will show the
7364 up-to-date frame titles. */
7365 #ifdef HAVE_WINDOW_SYSTEM
7366 if (windows_or_buffers_changed || update_mode_lines)
7367 {
7368 Lisp_Object tail, frame;
7369
7370 FOR_EACH_FRAME (tail, frame)
7371 {
7372 f = XFRAME (frame);
7373 if (!EQ (frame, tooltip_frame)
7374 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7375 x_consider_frame_title (frame);
7376 }
7377 }
7378 #endif /* HAVE_WINDOW_SYSTEM */
7379
7380 /* Update the menu bar item lists, if appropriate. This has to be
7381 done before any actual redisplay or generation of display lines. */
7382 all_windows = (update_mode_lines
7383 || buffer_shared > 1
7384 || windows_or_buffers_changed);
7385 if (all_windows)
7386 {
7387 Lisp_Object tail, frame;
7388 int count = SPECPDL_INDEX ();
7389
7390 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7391
7392 FOR_EACH_FRAME (tail, frame)
7393 {
7394 f = XFRAME (frame);
7395
7396 /* Ignore tooltip frame. */
7397 if (EQ (frame, tooltip_frame))
7398 continue;
7399
7400 /* If a window on this frame changed size, report that to
7401 the user and clear the size-change flag. */
7402 if (FRAME_WINDOW_SIZES_CHANGED (f))
7403 {
7404 Lisp_Object functions;
7405
7406 /* Clear flag first in case we get an error below. */
7407 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7408 functions = Vwindow_size_change_functions;
7409 GCPRO2 (tail, functions);
7410
7411 while (CONSP (functions))
7412 {
7413 call1 (XCAR (functions), frame);
7414 functions = XCDR (functions);
7415 }
7416 UNGCPRO;
7417 }
7418
7419 GCPRO1 (tail);
7420 update_menu_bar (f, 0);
7421 #ifdef HAVE_WINDOW_SYSTEM
7422 update_tool_bar (f, 0);
7423 #endif
7424 UNGCPRO;
7425 }
7426
7427 unbind_to (count, Qnil);
7428 }
7429 else
7430 {
7431 struct frame *sf = SELECTED_FRAME ();
7432 update_menu_bar (sf, 1);
7433 #ifdef HAVE_WINDOW_SYSTEM
7434 update_tool_bar (sf, 1);
7435 #endif
7436 }
7437
7438 /* Motif needs this. See comment in xmenu.c. Turn it off when
7439 pending_menu_activation is not defined. */
7440 #ifdef USE_X_TOOLKIT
7441 pending_menu_activation = 0;
7442 #endif
7443 }
7444
7445
7446 /* Update the menu bar item list for frame F. This has to be done
7447 before we start to fill in any display lines, because it can call
7448 eval.
7449
7450 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7451
7452 static void
7453 update_menu_bar (f, save_match_data)
7454 struct frame *f;
7455 int save_match_data;
7456 {
7457 Lisp_Object window;
7458 register struct window *w;
7459
7460 /* If called recursively during a menu update, do nothing. This can
7461 happen when, for instance, an activate-menubar-hook causes a
7462 redisplay. */
7463 if (inhibit_menubar_update)
7464 return;
7465
7466 window = FRAME_SELECTED_WINDOW (f);
7467 w = XWINDOW (window);
7468
7469 #if 0 /* The if statement below this if statement used to include the
7470 condition !NILP (w->update_mode_line), rather than using
7471 update_mode_lines directly, and this if statement may have
7472 been added to make that condition work. Now the if
7473 statement below matches its comment, this isn't needed. */
7474 if (update_mode_lines)
7475 w->update_mode_line = Qt;
7476 #endif
7477
7478 if (FRAME_WINDOW_P (f)
7479 ?
7480 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
7481 FRAME_EXTERNAL_MENU_BAR (f)
7482 #else
7483 FRAME_MENU_BAR_LINES (f) > 0
7484 #endif
7485 : FRAME_MENU_BAR_LINES (f) > 0)
7486 {
7487 /* If the user has switched buffers or windows, we need to
7488 recompute to reflect the new bindings. But we'll
7489 recompute when update_mode_lines is set too; that means
7490 that people can use force-mode-line-update to request
7491 that the menu bar be recomputed. The adverse effect on
7492 the rest of the redisplay algorithm is about the same as
7493 windows_or_buffers_changed anyway. */
7494 if (windows_or_buffers_changed
7495 /* This used to test w->update_mode_line, but we believe
7496 there is no need to recompute the menu in that case. */
7497 || update_mode_lines
7498 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7499 < BUF_MODIFF (XBUFFER (w->buffer)))
7500 != !NILP (w->last_had_star))
7501 || ((!NILP (Vtransient_mark_mode)
7502 && !NILP (XBUFFER (w->buffer)->mark_active))
7503 != !NILP (w->region_showing)))
7504 {
7505 struct buffer *prev = current_buffer;
7506 int count = SPECPDL_INDEX ();
7507
7508 specbind (Qinhibit_menubar_update, Qt);
7509
7510 set_buffer_internal_1 (XBUFFER (w->buffer));
7511 if (save_match_data)
7512 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7513 if (NILP (Voverriding_local_map_menu_flag))
7514 {
7515 specbind (Qoverriding_terminal_local_map, Qnil);
7516 specbind (Qoverriding_local_map, Qnil);
7517 }
7518
7519 /* Run the Lucid hook. */
7520 safe_run_hooks (Qactivate_menubar_hook);
7521
7522 /* If it has changed current-menubar from previous value,
7523 really recompute the menu-bar from the value. */
7524 if (! NILP (Vlucid_menu_bar_dirty_flag))
7525 call0 (Qrecompute_lucid_menubar);
7526
7527 safe_run_hooks (Qmenu_bar_update_hook);
7528 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7529
7530 /* Redisplay the menu bar in case we changed it. */
7531 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
7532 if (FRAME_WINDOW_P (f)
7533 #if defined (MAC_OS)
7534 /* All frames on Mac OS share the same menubar. So only the
7535 selected frame should be allowed to set it. */
7536 && f == SELECTED_FRAME ()
7537 #endif
7538 )
7539 set_frame_menubar (f, 0, 0);
7540 else
7541 /* On a terminal screen, the menu bar is an ordinary screen
7542 line, and this makes it get updated. */
7543 w->update_mode_line = Qt;
7544 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7545 /* In the non-toolkit version, the menu bar is an ordinary screen
7546 line, and this makes it get updated. */
7547 w->update_mode_line = Qt;
7548 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7549
7550 unbind_to (count, Qnil);
7551 set_buffer_internal_1 (prev);
7552 }
7553 }
7554 }
7555
7556
7557 \f
7558 /***********************************************************************
7559 Tool-bars
7560 ***********************************************************************/
7561
7562 #ifdef HAVE_WINDOW_SYSTEM
7563
7564 /* Update the tool-bar item list for frame F. This has to be done
7565 before we start to fill in any display lines. Called from
7566 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7567 and restore it here. */
7568
7569 static void
7570 update_tool_bar (f, save_match_data)
7571 struct frame *f;
7572 int save_match_data;
7573 {
7574 if (WINDOWP (f->tool_bar_window)
7575 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7576 {
7577 Lisp_Object window;
7578 struct window *w;
7579
7580 window = FRAME_SELECTED_WINDOW (f);
7581 w = XWINDOW (window);
7582
7583 /* If the user has switched buffers or windows, we need to
7584 recompute to reflect the new bindings. But we'll
7585 recompute when update_mode_lines is set too; that means
7586 that people can use force-mode-line-update to request
7587 that the menu bar be recomputed. The adverse effect on
7588 the rest of the redisplay algorithm is about the same as
7589 windows_or_buffers_changed anyway. */
7590 if (windows_or_buffers_changed
7591 || !NILP (w->update_mode_line)
7592 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7593 < BUF_MODIFF (XBUFFER (w->buffer)))
7594 != !NILP (w->last_had_star))
7595 || ((!NILP (Vtransient_mark_mode)
7596 && !NILP (XBUFFER (w->buffer)->mark_active))
7597 != !NILP (w->region_showing)))
7598 {
7599 struct buffer *prev = current_buffer;
7600 int count = SPECPDL_INDEX ();
7601
7602 /* Set current_buffer to the buffer of the selected
7603 window of the frame, so that we get the right local
7604 keymaps. */
7605 set_buffer_internal_1 (XBUFFER (w->buffer));
7606
7607 /* Save match data, if we must. */
7608 if (save_match_data)
7609 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7610
7611 /* Make sure that we don't accidentally use bogus keymaps. */
7612 if (NILP (Voverriding_local_map_menu_flag))
7613 {
7614 specbind (Qoverriding_terminal_local_map, Qnil);
7615 specbind (Qoverriding_local_map, Qnil);
7616 }
7617
7618 /* Build desired tool-bar items from keymaps. */
7619 f->tool_bar_items
7620 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7621
7622 /* Redisplay the tool-bar in case we changed it. */
7623 w->update_mode_line = Qt;
7624
7625 unbind_to (count, Qnil);
7626 set_buffer_internal_1 (prev);
7627 }
7628 }
7629 }
7630
7631
7632 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7633 F's desired tool-bar contents. F->tool_bar_items must have
7634 been set up previously by calling prepare_menu_bars. */
7635
7636 static void
7637 build_desired_tool_bar_string (f)
7638 struct frame *f;
7639 {
7640 int i, size, size_needed;
7641 struct gcpro gcpro1, gcpro2, gcpro3;
7642 Lisp_Object image, plist, props;
7643
7644 image = plist = props = Qnil;
7645 GCPRO3 (image, plist, props);
7646
7647 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7648 Otherwise, make a new string. */
7649
7650 /* The size of the string we might be able to reuse. */
7651 size = (STRINGP (f->desired_tool_bar_string)
7652 ? SCHARS (f->desired_tool_bar_string)
7653 : 0);
7654
7655 /* We need one space in the string for each image. */
7656 size_needed = f->n_tool_bar_items;
7657
7658 /* Reuse f->desired_tool_bar_string, if possible. */
7659 if (size < size_needed || NILP (f->desired_tool_bar_string))
7660 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7661 make_number (' '));
7662 else
7663 {
7664 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7665 Fremove_text_properties (make_number (0), make_number (size),
7666 props, f->desired_tool_bar_string);
7667 }
7668
7669 /* Put a `display' property on the string for the images to display,
7670 put a `menu_item' property on tool-bar items with a value that
7671 is the index of the item in F's tool-bar item vector. */
7672 for (i = 0; i < f->n_tool_bar_items; ++i)
7673 {
7674 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7675
7676 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7677 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7678 int hmargin, vmargin, relief, idx, end;
7679 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7680
7681 /* If image is a vector, choose the image according to the
7682 button state. */
7683 image = PROP (TOOL_BAR_ITEM_IMAGES);
7684 if (VECTORP (image))
7685 {
7686 if (enabled_p)
7687 idx = (selected_p
7688 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7689 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7690 else
7691 idx = (selected_p
7692 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7693 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7694
7695 xassert (ASIZE (image) >= idx);
7696 image = AREF (image, idx);
7697 }
7698 else
7699 idx = -1;
7700
7701 /* Ignore invalid image specifications. */
7702 if (!valid_image_p (image))
7703 continue;
7704
7705 /* Display the tool-bar button pressed, or depressed. */
7706 plist = Fcopy_sequence (XCDR (image));
7707
7708 /* Compute margin and relief to draw. */
7709 relief = (tool_bar_button_relief >= 0
7710 ? tool_bar_button_relief
7711 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7712 hmargin = vmargin = relief;
7713
7714 if (INTEGERP (Vtool_bar_button_margin)
7715 && XINT (Vtool_bar_button_margin) > 0)
7716 {
7717 hmargin += XFASTINT (Vtool_bar_button_margin);
7718 vmargin += XFASTINT (Vtool_bar_button_margin);
7719 }
7720 else if (CONSP (Vtool_bar_button_margin))
7721 {
7722 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7723 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7724 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7725
7726 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7727 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7728 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7729 }
7730
7731 if (auto_raise_tool_bar_buttons_p)
7732 {
7733 /* Add a `:relief' property to the image spec if the item is
7734 selected. */
7735 if (selected_p)
7736 {
7737 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7738 hmargin -= relief;
7739 vmargin -= relief;
7740 }
7741 }
7742 else
7743 {
7744 /* If image is selected, display it pressed, i.e. with a
7745 negative relief. If it's not selected, display it with a
7746 raised relief. */
7747 plist = Fplist_put (plist, QCrelief,
7748 (selected_p
7749 ? make_number (-relief)
7750 : make_number (relief)));
7751 hmargin -= relief;
7752 vmargin -= relief;
7753 }
7754
7755 /* Put a margin around the image. */
7756 if (hmargin || vmargin)
7757 {
7758 if (hmargin == vmargin)
7759 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7760 else
7761 plist = Fplist_put (plist, QCmargin,
7762 Fcons (make_number (hmargin),
7763 make_number (vmargin)));
7764 }
7765
7766 /* If button is not enabled, and we don't have special images
7767 for the disabled state, make the image appear disabled by
7768 applying an appropriate algorithm to it. */
7769 if (!enabled_p && idx < 0)
7770 plist = Fplist_put (plist, QCconversion, Qdisabled);
7771
7772 /* Put a `display' text property on the string for the image to
7773 display. Put a `menu-item' property on the string that gives
7774 the start of this item's properties in the tool-bar items
7775 vector. */
7776 image = Fcons (Qimage, plist);
7777 props = list4 (Qdisplay, image,
7778 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7779
7780 /* Let the last image hide all remaining spaces in the tool bar
7781 string. The string can be longer than needed when we reuse a
7782 previous string. */
7783 if (i + 1 == f->n_tool_bar_items)
7784 end = SCHARS (f->desired_tool_bar_string);
7785 else
7786 end = i + 1;
7787 Fadd_text_properties (make_number (i), make_number (end),
7788 props, f->desired_tool_bar_string);
7789 #undef PROP
7790 }
7791
7792 UNGCPRO;
7793 }
7794
7795
7796 /* Display one line of the tool-bar of frame IT->f. */
7797
7798 static void
7799 display_tool_bar_line (it)
7800 struct it *it;
7801 {
7802 struct glyph_row *row = it->glyph_row;
7803 int max_x = it->last_visible_x;
7804 struct glyph *last;
7805
7806 prepare_desired_row (row);
7807 row->y = it->current_y;
7808
7809 /* Note that this isn't made use of if the face hasn't a box,
7810 so there's no need to check the face here. */
7811 it->start_of_box_run_p = 1;
7812
7813 while (it->current_x < max_x)
7814 {
7815 int x_before, x, n_glyphs_before, i, nglyphs;
7816
7817 /* Get the next display element. */
7818 if (!get_next_display_element (it))
7819 break;
7820
7821 /* Produce glyphs. */
7822 x_before = it->current_x;
7823 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7824 PRODUCE_GLYPHS (it);
7825
7826 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7827 i = 0;
7828 x = x_before;
7829 while (i < nglyphs)
7830 {
7831 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7832
7833 if (x + glyph->pixel_width > max_x)
7834 {
7835 /* Glyph doesn't fit on line. */
7836 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7837 it->current_x = x;
7838 goto out;
7839 }
7840
7841 ++it->hpos;
7842 x += glyph->pixel_width;
7843 ++i;
7844 }
7845
7846 /* Stop at line ends. */
7847 if (ITERATOR_AT_END_OF_LINE_P (it))
7848 break;
7849
7850 set_iterator_to_next (it, 1);
7851 }
7852
7853 out:;
7854
7855 row->displays_text_p = row->used[TEXT_AREA] != 0;
7856 extend_face_to_end_of_line (it);
7857 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7858 last->right_box_line_p = 1;
7859 if (last == row->glyphs[TEXT_AREA])
7860 last->left_box_line_p = 1;
7861 compute_line_metrics (it);
7862
7863 /* If line is empty, make it occupy the rest of the tool-bar. */
7864 if (!row->displays_text_p)
7865 {
7866 row->height = row->phys_height = it->last_visible_y - row->y;
7867 row->ascent = row->phys_ascent = 0;
7868 }
7869
7870 row->full_width_p = 1;
7871 row->continued_p = 0;
7872 row->truncated_on_left_p = 0;
7873 row->truncated_on_right_p = 0;
7874
7875 it->current_x = it->hpos = 0;
7876 it->current_y += row->height;
7877 ++it->vpos;
7878 ++it->glyph_row;
7879 }
7880
7881
7882 /* Value is the number of screen lines needed to make all tool-bar
7883 items of frame F visible. */
7884
7885 static int
7886 tool_bar_lines_needed (f)
7887 struct frame *f;
7888 {
7889 struct window *w = XWINDOW (f->tool_bar_window);
7890 struct it it;
7891
7892 /* Initialize an iterator for iteration over
7893 F->desired_tool_bar_string in the tool-bar window of frame F. */
7894 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7895 it.first_visible_x = 0;
7896 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7897 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7898
7899 while (!ITERATOR_AT_END_P (&it))
7900 {
7901 it.glyph_row = w->desired_matrix->rows;
7902 clear_glyph_row (it.glyph_row);
7903 display_tool_bar_line (&it);
7904 }
7905
7906 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7907 }
7908
7909
7910 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7911 0, 1, 0,
7912 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7913 (frame)
7914 Lisp_Object frame;
7915 {
7916 struct frame *f;
7917 struct window *w;
7918 int nlines = 0;
7919
7920 if (NILP (frame))
7921 frame = selected_frame;
7922 else
7923 CHECK_FRAME (frame);
7924 f = XFRAME (frame);
7925
7926 if (WINDOWP (f->tool_bar_window)
7927 || (w = XWINDOW (f->tool_bar_window),
7928 XFASTINT (w->height) > 0))
7929 {
7930 update_tool_bar (f, 1);
7931 if (f->n_tool_bar_items)
7932 {
7933 build_desired_tool_bar_string (f);
7934 nlines = tool_bar_lines_needed (f);
7935 }
7936 }
7937
7938 return make_number (nlines);
7939 }
7940
7941
7942 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7943 height should be changed. */
7944
7945 static int
7946 redisplay_tool_bar (f)
7947 struct frame *f;
7948 {
7949 struct window *w;
7950 struct it it;
7951 struct glyph_row *row;
7952 int change_height_p = 0;
7953
7954 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7955 do anything. This means you must start with tool-bar-lines
7956 non-zero to get the auto-sizing effect. Or in other words, you
7957 can turn off tool-bars by specifying tool-bar-lines zero. */
7958 if (!WINDOWP (f->tool_bar_window)
7959 || (w = XWINDOW (f->tool_bar_window),
7960 XFASTINT (w->height) == 0))
7961 return 0;
7962
7963 /* Set up an iterator for the tool-bar window. */
7964 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7965 it.first_visible_x = 0;
7966 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7967 row = it.glyph_row;
7968
7969 /* Build a string that represents the contents of the tool-bar. */
7970 build_desired_tool_bar_string (f);
7971 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7972
7973 /* Display as many lines as needed to display all tool-bar items. */
7974 while (it.current_y < it.last_visible_y)
7975 display_tool_bar_line (&it);
7976
7977 /* It doesn't make much sense to try scrolling in the tool-bar
7978 window, so don't do it. */
7979 w->desired_matrix->no_scrolling_p = 1;
7980 w->must_be_updated_p = 1;
7981
7982 if (auto_resize_tool_bars_p)
7983 {
7984 int nlines;
7985
7986 /* If we couldn't display everything, change the tool-bar's
7987 height. */
7988 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7989 change_height_p = 1;
7990
7991 /* If there are blank lines at the end, except for a partially
7992 visible blank line at the end that is smaller than
7993 CANON_Y_UNIT, change the tool-bar's height. */
7994 row = it.glyph_row - 1;
7995 if (!row->displays_text_p
7996 && row->height >= CANON_Y_UNIT (f))
7997 change_height_p = 1;
7998
7999 /* If row displays tool-bar items, but is partially visible,
8000 change the tool-bar's height. */
8001 if (row->displays_text_p
8002 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8003 change_height_p = 1;
8004
8005 /* Resize windows as needed by changing the `tool-bar-lines'
8006 frame parameter. */
8007 if (change_height_p
8008 && (nlines = tool_bar_lines_needed (f),
8009 nlines != XFASTINT (w->height)))
8010 {
8011 extern Lisp_Object Qtool_bar_lines;
8012 Lisp_Object frame;
8013 int old_height = XFASTINT (w->height);
8014
8015 XSETFRAME (frame, f);
8016 clear_glyph_matrix (w->desired_matrix);
8017 Fmodify_frame_parameters (frame,
8018 Fcons (Fcons (Qtool_bar_lines,
8019 make_number (nlines)),
8020 Qnil));
8021 if (XFASTINT (w->height) != old_height)
8022 fonts_changed_p = 1;
8023 }
8024 }
8025
8026 return change_height_p;
8027 }
8028
8029
8030 /* Get information about the tool-bar item which is displayed in GLYPH
8031 on frame F. Return in *PROP_IDX the index where tool-bar item
8032 properties start in F->tool_bar_items. Value is zero if
8033 GLYPH doesn't display a tool-bar item. */
8034
8035 int
8036 tool_bar_item_info (f, glyph, prop_idx)
8037 struct frame *f;
8038 struct glyph *glyph;
8039 int *prop_idx;
8040 {
8041 Lisp_Object prop;
8042 int success_p;
8043 int charpos;
8044
8045 /* This function can be called asynchronously, which means we must
8046 exclude any possibility that Fget_text_property signals an
8047 error. */
8048 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8049 charpos = max (0, charpos);
8050
8051 /* Get the text property `menu-item' at pos. The value of that
8052 property is the start index of this item's properties in
8053 F->tool_bar_items. */
8054 prop = Fget_text_property (make_number (charpos),
8055 Qmenu_item, f->current_tool_bar_string);
8056 if (INTEGERP (prop))
8057 {
8058 *prop_idx = XINT (prop);
8059 success_p = 1;
8060 }
8061 else
8062 success_p = 0;
8063
8064 return success_p;
8065 }
8066
8067 #endif /* HAVE_WINDOW_SYSTEM */
8068
8069
8070 \f
8071 /************************************************************************
8072 Horizontal scrolling
8073 ************************************************************************/
8074
8075 static int hscroll_window_tree P_ ((Lisp_Object));
8076 static int hscroll_windows P_ ((Lisp_Object));
8077
8078 /* For all leaf windows in the window tree rooted at WINDOW, set their
8079 hscroll value so that PT is (i) visible in the window, and (ii) so
8080 that it is not within a certain margin at the window's left and
8081 right border. Value is non-zero if any window's hscroll has been
8082 changed. */
8083
8084 static int
8085 hscroll_window_tree (window)
8086 Lisp_Object window;
8087 {
8088 int hscrolled_p = 0;
8089 int hscroll_relative_p = FLOATP (Vhscroll_step);
8090 int hscroll_step_abs = 0;
8091 double hscroll_step_rel = 0;
8092
8093 if (hscroll_relative_p)
8094 {
8095 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
8096 if (hscroll_step_rel < 0)
8097 {
8098 hscroll_relative_p = 0;
8099 hscroll_step_abs = 0;
8100 }
8101 }
8102 else if (INTEGERP (Vhscroll_step))
8103 {
8104 hscroll_step_abs = XINT (Vhscroll_step);
8105 if (hscroll_step_abs < 0)
8106 hscroll_step_abs = 0;
8107 }
8108 else
8109 hscroll_step_abs = 0;
8110
8111 while (WINDOWP (window))
8112 {
8113 struct window *w = XWINDOW (window);
8114
8115 if (WINDOWP (w->hchild))
8116 hscrolled_p |= hscroll_window_tree (w->hchild);
8117 else if (WINDOWP (w->vchild))
8118 hscrolled_p |= hscroll_window_tree (w->vchild);
8119 else if (w->cursor.vpos >= 0)
8120 {
8121 int h_margin, text_area_x, text_area_y;
8122 int text_area_width, text_area_height;
8123 struct glyph_row *current_cursor_row
8124 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8125 struct glyph_row *desired_cursor_row
8126 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8127 struct glyph_row *cursor_row
8128 = (desired_cursor_row->enabled_p
8129 ? desired_cursor_row
8130 : current_cursor_row);
8131
8132 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8133 &text_area_width, &text_area_height);
8134
8135 /* Scroll when cursor is inside this scroll margin. */
8136 h_margin = hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
8137
8138 if ((XFASTINT (w->hscroll)
8139 && w->cursor.x <= h_margin)
8140 || (cursor_row->enabled_p
8141 && cursor_row->truncated_on_right_p
8142 && (w->cursor.x >= text_area_width - h_margin)))
8143 {
8144 struct it it;
8145 int hscroll;
8146 struct buffer *saved_current_buffer;
8147 int pt;
8148 int wanted_x;
8149
8150 /* Find point in a display of infinite width. */
8151 saved_current_buffer = current_buffer;
8152 current_buffer = XBUFFER (w->buffer);
8153
8154 if (w == XWINDOW (selected_window))
8155 pt = BUF_PT (current_buffer);
8156 else
8157 {
8158 pt = marker_position (w->pointm);
8159 pt = max (BEGV, pt);
8160 pt = min (ZV, pt);
8161 }
8162
8163 /* Move iterator to pt starting at cursor_row->start in
8164 a line with infinite width. */
8165 init_to_row_start (&it, w, cursor_row);
8166 it.last_visible_x = INFINITY;
8167 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8168 current_buffer = saved_current_buffer;
8169
8170 /* Position cursor in window. */
8171 if (!hscroll_relative_p && hscroll_step_abs == 0)
8172 hscroll = max (0, it.current_x - text_area_width / 2)
8173 / CANON_X_UNIT (it.f);
8174 else if (w->cursor.x >= text_area_width - h_margin)
8175 {
8176 if (hscroll_relative_p)
8177 wanted_x = text_area_width * (1 - hscroll_step_rel)
8178 - h_margin;
8179 else
8180 wanted_x = text_area_width
8181 - hscroll_step_abs * CANON_X_UNIT (it.f)
8182 - h_margin;
8183 hscroll
8184 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8185 }
8186 else
8187 {
8188 if (hscroll_relative_p)
8189 wanted_x = text_area_width * hscroll_step_rel
8190 + h_margin;
8191 else
8192 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
8193 + h_margin;
8194 hscroll
8195 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8196 }
8197 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8198
8199 /* Don't call Fset_window_hscroll if value hasn't
8200 changed because it will prevent redisplay
8201 optimizations. */
8202 if (XFASTINT (w->hscroll) != hscroll)
8203 {
8204 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8205 w->hscroll = make_number (hscroll);
8206 hscrolled_p = 1;
8207 }
8208 }
8209 }
8210
8211 window = w->next;
8212 }
8213
8214 /* Value is non-zero if hscroll of any leaf window has been changed. */
8215 return hscrolled_p;
8216 }
8217
8218
8219 /* Set hscroll so that cursor is visible and not inside horizontal
8220 scroll margins for all windows in the tree rooted at WINDOW. See
8221 also hscroll_window_tree above. Value is non-zero if any window's
8222 hscroll has been changed. If it has, desired matrices on the frame
8223 of WINDOW are cleared. */
8224
8225 static int
8226 hscroll_windows (window)
8227 Lisp_Object window;
8228 {
8229 int hscrolled_p;
8230
8231 if (automatic_hscrolling_p)
8232 {
8233 hscrolled_p = hscroll_window_tree (window);
8234 if (hscrolled_p)
8235 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8236 }
8237 else
8238 hscrolled_p = 0;
8239 return hscrolled_p;
8240 }
8241
8242
8243 \f
8244 /************************************************************************
8245 Redisplay
8246 ************************************************************************/
8247
8248 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8249 to a non-zero value. This is sometimes handy to have in a debugger
8250 session. */
8251
8252 #if GLYPH_DEBUG
8253
8254 /* First and last unchanged row for try_window_id. */
8255
8256 int debug_first_unchanged_at_end_vpos;
8257 int debug_last_unchanged_at_beg_vpos;
8258
8259 /* Delta vpos and y. */
8260
8261 int debug_dvpos, debug_dy;
8262
8263 /* Delta in characters and bytes for try_window_id. */
8264
8265 int debug_delta, debug_delta_bytes;
8266
8267 /* Values of window_end_pos and window_end_vpos at the end of
8268 try_window_id. */
8269
8270 EMACS_INT debug_end_pos, debug_end_vpos;
8271
8272 /* Append a string to W->desired_matrix->method. FMT is a printf
8273 format string. A1...A9 are a supplement for a variable-length
8274 argument list. If trace_redisplay_p is non-zero also printf the
8275 resulting string to stderr. */
8276
8277 static void
8278 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8279 struct window *w;
8280 char *fmt;
8281 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8282 {
8283 char buffer[512];
8284 char *method = w->desired_matrix->method;
8285 int len = strlen (method);
8286 int size = sizeof w->desired_matrix->method;
8287 int remaining = size - len - 1;
8288
8289 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8290 if (len && remaining)
8291 {
8292 method[len] = '|';
8293 --remaining, ++len;
8294 }
8295
8296 strncpy (method + len, buffer, remaining);
8297
8298 if (trace_redisplay_p)
8299 fprintf (stderr, "%p (%s): %s\n",
8300 w,
8301 ((BUFFERP (w->buffer)
8302 && STRINGP (XBUFFER (w->buffer)->name))
8303 ? (char *) SDATA (XBUFFER (w->buffer)->name)
8304 : "no buffer"),
8305 buffer);
8306 }
8307
8308 #endif /* GLYPH_DEBUG */
8309
8310
8311 /* Value is non-zero if all changes in window W, which displays
8312 current_buffer, are in the text between START and END. START is a
8313 buffer position, END is given as a distance from Z. Used in
8314 redisplay_internal for display optimization. */
8315
8316 static INLINE int
8317 text_outside_line_unchanged_p (w, start, end)
8318 struct window *w;
8319 int start, end;
8320 {
8321 int unchanged_p = 1;
8322
8323 /* If text or overlays have changed, see where. */
8324 if (XFASTINT (w->last_modified) < MODIFF
8325 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8326 {
8327 /* Gap in the line? */
8328 if (GPT < start || Z - GPT < end)
8329 unchanged_p = 0;
8330
8331 /* Changes start in front of the line, or end after it? */
8332 if (unchanged_p
8333 && (BEG_UNCHANGED < start - 1
8334 || END_UNCHANGED < end))
8335 unchanged_p = 0;
8336
8337 /* If selective display, can't optimize if changes start at the
8338 beginning of the line. */
8339 if (unchanged_p
8340 && INTEGERP (current_buffer->selective_display)
8341 && XINT (current_buffer->selective_display) > 0
8342 && (BEG_UNCHANGED < start || GPT <= start))
8343 unchanged_p = 0;
8344
8345 /* If there are overlays at the start or end of the line, these
8346 may have overlay strings with newlines in them. A change at
8347 START, for instance, may actually concern the display of such
8348 overlay strings as well, and they are displayed on different
8349 lines. So, quickly rule out this case. (For the future, it
8350 might be desirable to implement something more telling than
8351 just BEG/END_UNCHANGED.) */
8352 if (unchanged_p)
8353 {
8354 if (BEG + BEG_UNCHANGED == start
8355 && overlay_touches_p (start))
8356 unchanged_p = 0;
8357 if (END_UNCHANGED == end
8358 && overlay_touches_p (Z - end))
8359 unchanged_p = 0;
8360 }
8361 }
8362
8363 return unchanged_p;
8364 }
8365
8366
8367 /* Do a frame update, taking possible shortcuts into account. This is
8368 the main external entry point for redisplay.
8369
8370 If the last redisplay displayed an echo area message and that message
8371 is no longer requested, we clear the echo area or bring back the
8372 mini-buffer if that is in use. */
8373
8374 void
8375 redisplay ()
8376 {
8377 redisplay_internal (0);
8378 }
8379
8380
8381 /* Return 1 if point moved out of or into a composition. Otherwise
8382 return 0. PREV_BUF and PREV_PT are the last point buffer and
8383 position. BUF and PT are the current point buffer and position. */
8384
8385 int
8386 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8387 struct buffer *prev_buf, *buf;
8388 int prev_pt, pt;
8389 {
8390 int start, end;
8391 Lisp_Object prop;
8392 Lisp_Object buffer;
8393
8394 XSETBUFFER (buffer, buf);
8395 /* Check a composition at the last point if point moved within the
8396 same buffer. */
8397 if (prev_buf == buf)
8398 {
8399 if (prev_pt == pt)
8400 /* Point didn't move. */
8401 return 0;
8402
8403 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8404 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8405 && COMPOSITION_VALID_P (start, end, prop)
8406 && start < prev_pt && end > prev_pt)
8407 /* The last point was within the composition. Return 1 iff
8408 point moved out of the composition. */
8409 return (pt <= start || pt >= end);
8410 }
8411
8412 /* Check a composition at the current point. */
8413 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8414 && find_composition (pt, -1, &start, &end, &prop, buffer)
8415 && COMPOSITION_VALID_P (start, end, prop)
8416 && start < pt && end > pt);
8417 }
8418
8419
8420 /* Reconsider the setting of B->clip_changed which is displayed
8421 in window W. */
8422
8423 static INLINE void
8424 reconsider_clip_changes (w, b)
8425 struct window *w;
8426 struct buffer *b;
8427 {
8428 if (b->clip_changed
8429 && !NILP (w->window_end_valid)
8430 && w->current_matrix->buffer == b
8431 && w->current_matrix->zv == BUF_ZV (b)
8432 && w->current_matrix->begv == BUF_BEGV (b))
8433 b->clip_changed = 0;
8434
8435 /* If display wasn't paused, and W is not a tool bar window, see if
8436 point has been moved into or out of a composition. In that case,
8437 we set b->clip_changed to 1 to force updating the screen. If
8438 b->clip_changed has already been set to 1, we can skip this
8439 check. */
8440 if (!b->clip_changed
8441 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8442 {
8443 int pt;
8444
8445 if (w == XWINDOW (selected_window))
8446 pt = BUF_PT (current_buffer);
8447 else
8448 pt = marker_position (w->pointm);
8449
8450 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8451 || pt != XINT (w->last_point))
8452 && check_point_in_composition (w->current_matrix->buffer,
8453 XINT (w->last_point),
8454 XBUFFER (w->buffer), pt))
8455 b->clip_changed = 1;
8456 }
8457 }
8458
8459
8460 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8461 response to any user action; therefore, we should preserve the echo
8462 area. (Actually, our caller does that job.) Perhaps in the future
8463 avoid recentering windows if it is not necessary; currently that
8464 causes some problems. */
8465
8466 static void
8467 redisplay_internal (preserve_echo_area)
8468 int preserve_echo_area;
8469 {
8470 struct window *w = XWINDOW (selected_window);
8471 struct frame *f = XFRAME (w->frame);
8472 int pause;
8473 int must_finish = 0;
8474 struct text_pos tlbufpos, tlendpos;
8475 int number_of_visible_frames;
8476 int count;
8477 struct frame *sf = SELECTED_FRAME ();
8478
8479 /* Non-zero means redisplay has to consider all windows on all
8480 frames. Zero means, only selected_window is considered. */
8481 int consider_all_windows_p;
8482
8483 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8484
8485 /* No redisplay if running in batch mode or frame is not yet fully
8486 initialized, or redisplay is explicitly turned off by setting
8487 Vinhibit_redisplay. */
8488 if (noninteractive
8489 || !NILP (Vinhibit_redisplay)
8490 || !f->glyphs_initialized_p)
8491 return;
8492
8493 /* The flag redisplay_performed_directly_p is set by
8494 direct_output_for_insert when it already did the whole screen
8495 update necessary. */
8496 if (redisplay_performed_directly_p)
8497 {
8498 redisplay_performed_directly_p = 0;
8499 if (!hscroll_windows (selected_window))
8500 return;
8501 }
8502
8503 #ifdef USE_X_TOOLKIT
8504 if (popup_activated ())
8505 return;
8506 #endif
8507
8508 /* I don't think this happens but let's be paranoid. */
8509 if (redisplaying_p)
8510 return;
8511
8512 /* Record a function that resets redisplaying_p to its old value
8513 when we leave this function. */
8514 count = SPECPDL_INDEX ();
8515 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8516 ++redisplaying_p;
8517
8518 retry:
8519 pause = 0;
8520 reconsider_clip_changes (w, current_buffer);
8521 redisplay_updating_p = 0;
8522
8523 /* If new fonts have been loaded that make a glyph matrix adjustment
8524 necessary, do it. */
8525 if (fonts_changed_p)
8526 {
8527 adjust_glyphs (NULL);
8528 ++windows_or_buffers_changed;
8529 fonts_changed_p = 0;
8530 }
8531
8532 /* If face_change_count is non-zero, init_iterator will free all
8533 realized faces, which includes the faces referenced from current
8534 matrices. So, we can't reuse current matrices in this case. */
8535 if (face_change_count)
8536 ++windows_or_buffers_changed;
8537
8538 if (! FRAME_WINDOW_P (sf)
8539 && previous_terminal_frame != sf)
8540 {
8541 /* Since frames on an ASCII terminal share the same display
8542 area, displaying a different frame means redisplay the whole
8543 thing. */
8544 windows_or_buffers_changed++;
8545 SET_FRAME_GARBAGED (sf);
8546 XSETFRAME (Vterminal_frame, sf);
8547 }
8548 previous_terminal_frame = sf;
8549
8550 /* Set the visible flags for all frames. Do this before checking
8551 for resized or garbaged frames; they want to know if their frames
8552 are visible. See the comment in frame.h for
8553 FRAME_SAMPLE_VISIBILITY. */
8554 {
8555 Lisp_Object tail, frame;
8556
8557 number_of_visible_frames = 0;
8558
8559 FOR_EACH_FRAME (tail, frame)
8560 {
8561 struct frame *f = XFRAME (frame);
8562
8563 FRAME_SAMPLE_VISIBILITY (f);
8564 if (FRAME_VISIBLE_P (f))
8565 ++number_of_visible_frames;
8566 clear_desired_matrices (f);
8567 }
8568 }
8569
8570 /* Notice any pending interrupt request to change frame size. */
8571 do_pending_window_change (1);
8572
8573 /* Clear frames marked as garbaged. */
8574 if (frame_garbaged)
8575 clear_garbaged_frames ();
8576
8577 /* Build menubar and tool-bar items. */
8578 prepare_menu_bars ();
8579
8580 if (windows_or_buffers_changed)
8581 update_mode_lines++;
8582
8583 /* Detect case that we need to write or remove a star in the mode line. */
8584 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8585 {
8586 w->update_mode_line = Qt;
8587 if (buffer_shared > 1)
8588 update_mode_lines++;
8589 }
8590
8591 /* If %c is in the mode line, update it if needed. */
8592 if (!NILP (w->column_number_displayed)
8593 /* This alternative quickly identifies a common case
8594 where no change is needed. */
8595 && !(PT == XFASTINT (w->last_point)
8596 && XFASTINT (w->last_modified) >= MODIFF
8597 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8598 && (XFASTINT (w->column_number_displayed)
8599 != (int) current_column ())) /* iftc */
8600 w->update_mode_line = Qt;
8601
8602 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8603
8604 /* The variable buffer_shared is set in redisplay_window and
8605 indicates that we redisplay a buffer in different windows. See
8606 there. */
8607 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
8608 || cursor_type_changed);
8609
8610 /* If specs for an arrow have changed, do thorough redisplay
8611 to ensure we remove any arrow that should no longer exist. */
8612 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8613 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8614 consider_all_windows_p = windows_or_buffers_changed = 1;
8615
8616 /* Normally the message* functions will have already displayed and
8617 updated the echo area, but the frame may have been trashed, or
8618 the update may have been preempted, so display the echo area
8619 again here. Checking message_cleared_p captures the case that
8620 the echo area should be cleared. */
8621 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8622 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8623 || (message_cleared_p
8624 && minibuf_level == 0
8625 /* If the mini-window is currently selected, this means the
8626 echo-area doesn't show through. */
8627 && !MINI_WINDOW_P (XWINDOW (selected_window))))
8628 {
8629 int window_height_changed_p = echo_area_display (0);
8630 must_finish = 1;
8631
8632 /* If we don't display the current message, don't clear the
8633 message_cleared_p flag, because, if we did, we wouldn't clear
8634 the echo area in the next redisplay which doesn't preserve
8635 the echo area. */
8636 if (!display_last_displayed_message_p)
8637 message_cleared_p = 0;
8638
8639 if (fonts_changed_p)
8640 goto retry;
8641 else if (window_height_changed_p)
8642 {
8643 consider_all_windows_p = 1;
8644 ++update_mode_lines;
8645 ++windows_or_buffers_changed;
8646
8647 /* If window configuration was changed, frames may have been
8648 marked garbaged. Clear them or we will experience
8649 surprises wrt scrolling. */
8650 if (frame_garbaged)
8651 clear_garbaged_frames ();
8652 }
8653 }
8654 else if (EQ (selected_window, minibuf_window)
8655 && (current_buffer->clip_changed
8656 || XFASTINT (w->last_modified) < MODIFF
8657 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8658 && resize_mini_window (w, 0))
8659 {
8660 /* Resized active mini-window to fit the size of what it is
8661 showing if its contents might have changed. */
8662 must_finish = 1;
8663 consider_all_windows_p = 1;
8664 ++windows_or_buffers_changed;
8665 ++update_mode_lines;
8666
8667 /* If window configuration was changed, frames may have been
8668 marked garbaged. Clear them or we will experience
8669 surprises wrt scrolling. */
8670 if (frame_garbaged)
8671 clear_garbaged_frames ();
8672 }
8673
8674
8675 /* If showing the region, and mark has changed, we must redisplay
8676 the whole window. The assignment to this_line_start_pos prevents
8677 the optimization directly below this if-statement. */
8678 if (((!NILP (Vtransient_mark_mode)
8679 && !NILP (XBUFFER (w->buffer)->mark_active))
8680 != !NILP (w->region_showing))
8681 || (!NILP (w->region_showing)
8682 && !EQ (w->region_showing,
8683 Fmarker_position (XBUFFER (w->buffer)->mark))))
8684 CHARPOS (this_line_start_pos) = 0;
8685
8686 /* Optimize the case that only the line containing the cursor in the
8687 selected window has changed. Variables starting with this_ are
8688 set in display_line and record information about the line
8689 containing the cursor. */
8690 tlbufpos = this_line_start_pos;
8691 tlendpos = this_line_end_pos;
8692 if (!consider_all_windows_p
8693 && CHARPOS (tlbufpos) > 0
8694 && NILP (w->update_mode_line)
8695 && !current_buffer->clip_changed
8696 && !current_buffer->prevent_redisplay_optimizations_p
8697 && FRAME_VISIBLE_P (XFRAME (w->frame))
8698 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8699 /* Make sure recorded data applies to current buffer, etc. */
8700 && this_line_buffer == current_buffer
8701 && current_buffer == XBUFFER (w->buffer)
8702 && NILP (w->force_start)
8703 && NILP (w->optional_new_start)
8704 /* Point must be on the line that we have info recorded about. */
8705 && PT >= CHARPOS (tlbufpos)
8706 && PT <= Z - CHARPOS (tlendpos)
8707 /* All text outside that line, including its final newline,
8708 must be unchanged */
8709 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8710 CHARPOS (tlendpos)))
8711 {
8712 if (CHARPOS (tlbufpos) > BEGV
8713 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8714 && (CHARPOS (tlbufpos) == ZV
8715 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8716 /* Former continuation line has disappeared by becoming empty */
8717 goto cancel;
8718 else if (XFASTINT (w->last_modified) < MODIFF
8719 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8720 || MINI_WINDOW_P (w))
8721 {
8722 /* We have to handle the case of continuation around a
8723 wide-column character (See the comment in indent.c around
8724 line 885).
8725
8726 For instance, in the following case:
8727
8728 -------- Insert --------
8729 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8730 J_I_ ==> J_I_ `^^' are cursors.
8731 ^^ ^^
8732 -------- --------
8733
8734 As we have to redraw the line above, we should goto cancel. */
8735
8736 struct it it;
8737 int line_height_before = this_line_pixel_height;
8738
8739 /* Note that start_display will handle the case that the
8740 line starting at tlbufpos is a continuation lines. */
8741 start_display (&it, w, tlbufpos);
8742
8743 /* Implementation note: It this still necessary? */
8744 if (it.current_x != this_line_start_x)
8745 goto cancel;
8746
8747 TRACE ((stderr, "trying display optimization 1\n"));
8748 w->cursor.vpos = -1;
8749 overlay_arrow_seen = 0;
8750 it.vpos = this_line_vpos;
8751 it.current_y = this_line_y;
8752 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8753 display_line (&it);
8754
8755 /* If line contains point, is not continued,
8756 and ends at same distance from eob as before, we win */
8757 if (w->cursor.vpos >= 0
8758 /* Line is not continued, otherwise this_line_start_pos
8759 would have been set to 0 in display_line. */
8760 && CHARPOS (this_line_start_pos)
8761 /* Line ends as before. */
8762 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8763 /* Line has same height as before. Otherwise other lines
8764 would have to be shifted up or down. */
8765 && this_line_pixel_height == line_height_before)
8766 {
8767 /* If this is not the window's last line, we must adjust
8768 the charstarts of the lines below. */
8769 if (it.current_y < it.last_visible_y)
8770 {
8771 struct glyph_row *row
8772 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8773 int delta, delta_bytes;
8774
8775 if (Z - CHARPOS (tlendpos) == ZV)
8776 {
8777 /* This line ends at end of (accessible part of)
8778 buffer. There is no newline to count. */
8779 delta = (Z
8780 - CHARPOS (tlendpos)
8781 - MATRIX_ROW_START_CHARPOS (row));
8782 delta_bytes = (Z_BYTE
8783 - BYTEPOS (tlendpos)
8784 - MATRIX_ROW_START_BYTEPOS (row));
8785 }
8786 else
8787 {
8788 /* This line ends in a newline. Must take
8789 account of the newline and the rest of the
8790 text that follows. */
8791 delta = (Z
8792 - CHARPOS (tlendpos)
8793 - MATRIX_ROW_START_CHARPOS (row));
8794 delta_bytes = (Z_BYTE
8795 - BYTEPOS (tlendpos)
8796 - MATRIX_ROW_START_BYTEPOS (row));
8797 }
8798
8799 increment_matrix_positions (w->current_matrix,
8800 this_line_vpos + 1,
8801 w->current_matrix->nrows,
8802 delta, delta_bytes);
8803 }
8804
8805 /* If this row displays text now but previously didn't,
8806 or vice versa, w->window_end_vpos may have to be
8807 adjusted. */
8808 if ((it.glyph_row - 1)->displays_text_p)
8809 {
8810 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8811 XSETINT (w->window_end_vpos, this_line_vpos);
8812 }
8813 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8814 && this_line_vpos > 0)
8815 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8816 w->window_end_valid = Qnil;
8817
8818 /* Update hint: No need to try to scroll in update_window. */
8819 w->desired_matrix->no_scrolling_p = 1;
8820
8821 #if GLYPH_DEBUG
8822 *w->desired_matrix->method = 0;
8823 debug_method_add (w, "optimization 1");
8824 #endif
8825 goto update;
8826 }
8827 else
8828 goto cancel;
8829 }
8830 else if (/* Cursor position hasn't changed. */
8831 PT == XFASTINT (w->last_point)
8832 /* Make sure the cursor was last displayed
8833 in this window. Otherwise we have to reposition it. */
8834 && 0 <= w->cursor.vpos
8835 && XINT (w->height) > w->cursor.vpos)
8836 {
8837 if (!must_finish)
8838 {
8839 do_pending_window_change (1);
8840
8841 /* We used to always goto end_of_redisplay here, but this
8842 isn't enough if we have a blinking cursor. */
8843 if (w->cursor_off_p == w->last_cursor_off_p)
8844 goto end_of_redisplay;
8845 }
8846 goto update;
8847 }
8848 /* If highlighting the region, or if the cursor is in the echo area,
8849 then we can't just move the cursor. */
8850 else if (! (!NILP (Vtransient_mark_mode)
8851 && !NILP (current_buffer->mark_active))
8852 && (EQ (selected_window, current_buffer->last_selected_window)
8853 || highlight_nonselected_windows)
8854 && NILP (w->region_showing)
8855 && NILP (Vshow_trailing_whitespace)
8856 && !cursor_in_echo_area)
8857 {
8858 struct it it;
8859 struct glyph_row *row;
8860
8861 /* Skip from tlbufpos to PT and see where it is. Note that
8862 PT may be in invisible text. If so, we will end at the
8863 next visible position. */
8864 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8865 NULL, DEFAULT_FACE_ID);
8866 it.current_x = this_line_start_x;
8867 it.current_y = this_line_y;
8868 it.vpos = this_line_vpos;
8869
8870 /* The call to move_it_to stops in front of PT, but
8871 moves over before-strings. */
8872 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8873
8874 if (it.vpos == this_line_vpos
8875 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8876 row->enabled_p))
8877 {
8878 xassert (this_line_vpos == it.vpos);
8879 xassert (this_line_y == it.current_y);
8880 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8881 #if GLYPH_DEBUG
8882 *w->desired_matrix->method = 0;
8883 debug_method_add (w, "optimization 3");
8884 #endif
8885 goto update;
8886 }
8887 else
8888 goto cancel;
8889 }
8890
8891 cancel:
8892 /* Text changed drastically or point moved off of line. */
8893 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8894 }
8895
8896 CHARPOS (this_line_start_pos) = 0;
8897 consider_all_windows_p |= buffer_shared > 1;
8898 ++clear_face_cache_count;
8899
8900
8901 /* Build desired matrices, and update the display. If
8902 consider_all_windows_p is non-zero, do it for all windows on all
8903 frames. Otherwise do it for selected_window, only. */
8904
8905 if (consider_all_windows_p)
8906 {
8907 Lisp_Object tail, frame;
8908 int i, n = 0, size = 50;
8909 struct frame **updated
8910 = (struct frame **) alloca (size * sizeof *updated);
8911
8912 /* Clear the face cache eventually. */
8913 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8914 {
8915 clear_face_cache (0);
8916 clear_face_cache_count = 0;
8917 }
8918
8919 /* Recompute # windows showing selected buffer. This will be
8920 incremented each time such a window is displayed. */
8921 buffer_shared = 0;
8922
8923 FOR_EACH_FRAME (tail, frame)
8924 {
8925 struct frame *f = XFRAME (frame);
8926
8927 if (FRAME_WINDOW_P (f) || f == sf)
8928 {
8929 #ifdef HAVE_WINDOW_SYSTEM
8930 if (clear_face_cache_count % 50 == 0
8931 && FRAME_WINDOW_P (f))
8932 clear_image_cache (f, 0);
8933 #endif /* HAVE_WINDOW_SYSTEM */
8934
8935 /* Mark all the scroll bars to be removed; we'll redeem
8936 the ones we want when we redisplay their windows. */
8937 if (condemn_scroll_bars_hook)
8938 condemn_scroll_bars_hook (f);
8939
8940 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8941 redisplay_windows (FRAME_ROOT_WINDOW (f));
8942
8943 /* Any scroll bars which redisplay_windows should have
8944 nuked should now go away. */
8945 if (judge_scroll_bars_hook)
8946 judge_scroll_bars_hook (f);
8947
8948 /* If fonts changed, display again. */
8949 if (fonts_changed_p)
8950 goto retry;
8951
8952 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8953 {
8954 /* See if we have to hscroll. */
8955 if (hscroll_windows (f->root_window))
8956 goto retry;
8957
8958 /* Prevent various kinds of signals during display
8959 update. stdio is not robust about handling
8960 signals, which can cause an apparent I/O
8961 error. */
8962 if (interrupt_input)
8963 unrequest_sigio ();
8964 stop_polling ();
8965
8966 /* Update the display. */
8967 set_window_update_flags (XWINDOW (f->root_window), 1);
8968 pause |= update_frame (f, 0, 0);
8969 if (pause)
8970 break;
8971
8972 if (n == size)
8973 {
8974 int nbytes = size * sizeof *updated;
8975 struct frame **p = (struct frame **) alloca (2 * nbytes);
8976 bcopy (updated, p, nbytes);
8977 size *= 2;
8978 }
8979
8980 updated[n++] = f;
8981 }
8982 }
8983 }
8984
8985 /* Do the mark_window_display_accurate after all windows have
8986 been redisplayed because this call resets flags in buffers
8987 which are needed for proper redisplay. */
8988 for (i = 0; i < n; ++i)
8989 {
8990 struct frame *f = updated[i];
8991 mark_window_display_accurate (f->root_window, 1);
8992 if (frame_up_to_date_hook)
8993 frame_up_to_date_hook (f);
8994 }
8995 }
8996 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8997 {
8998 Lisp_Object mini_window;
8999 struct frame *mini_frame;
9000
9001 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
9002 /* Use list_of_error, not Qerror, so that
9003 we catch only errors and don't run the debugger. */
9004 internal_condition_case_1 (redisplay_window_1, selected_window,
9005 list_of_error,
9006 redisplay_window_error);
9007
9008 /* Compare desired and current matrices, perform output. */
9009 update:
9010 redisplay_updating_p = 1;
9011
9012 /* If fonts changed, display again. */
9013 if (fonts_changed_p)
9014 goto retry;
9015
9016 /* Prevent various kinds of signals during display update.
9017 stdio is not robust about handling signals,
9018 which can cause an apparent I/O error. */
9019 if (interrupt_input)
9020 unrequest_sigio ();
9021 stop_polling ();
9022
9023 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
9024 {
9025 if (hscroll_windows (selected_window))
9026 goto retry;
9027
9028 XWINDOW (selected_window)->must_be_updated_p = 1;
9029 pause = update_frame (sf, 0, 0);
9030 }
9031
9032 /* We may have called echo_area_display at the top of this
9033 function. If the echo area is on another frame, that may
9034 have put text on a frame other than the selected one, so the
9035 above call to update_frame would not have caught it. Catch
9036 it here. */
9037 mini_window = FRAME_MINIBUF_WINDOW (sf);
9038 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9039
9040 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
9041 {
9042 XWINDOW (mini_window)->must_be_updated_p = 1;
9043 pause |= update_frame (mini_frame, 0, 0);
9044 if (!pause && hscroll_windows (mini_window))
9045 goto retry;
9046 }
9047 }
9048
9049 /* If display was paused because of pending input, make sure we do a
9050 thorough update the next time. */
9051 if (pause)
9052 {
9053 /* Prevent the optimization at the beginning of
9054 redisplay_internal that tries a single-line update of the
9055 line containing the cursor in the selected window. */
9056 CHARPOS (this_line_start_pos) = 0;
9057
9058 /* Let the overlay arrow be updated the next time. */
9059 if (!NILP (last_arrow_position))
9060 {
9061 last_arrow_position = Qt;
9062 last_arrow_string = Qt;
9063 }
9064
9065 /* If we pause after scrolling, some rows in the current
9066 matrices of some windows are not valid. */
9067 if (!WINDOW_FULL_WIDTH_P (w)
9068 && !FRAME_WINDOW_P (XFRAME (w->frame)))
9069 update_mode_lines = 1;
9070 }
9071 else
9072 {
9073 if (!consider_all_windows_p)
9074 {
9075 /* This has already been done above if
9076 consider_all_windows_p is set. */
9077 mark_window_display_accurate_1 (w, 1);
9078
9079 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9080 last_arrow_string = Voverlay_arrow_string;
9081
9082 if (frame_up_to_date_hook != 0)
9083 frame_up_to_date_hook (sf);
9084 }
9085
9086 update_mode_lines = 0;
9087 windows_or_buffers_changed = 0;
9088 cursor_type_changed = 0;
9089 }
9090
9091 /* Start SIGIO interrupts coming again. Having them off during the
9092 code above makes it less likely one will discard output, but not
9093 impossible, since there might be stuff in the system buffer here.
9094 But it is much hairier to try to do anything about that. */
9095 if (interrupt_input)
9096 request_sigio ();
9097 start_polling ();
9098
9099 /* If a frame has become visible which was not before, redisplay
9100 again, so that we display it. Expose events for such a frame
9101 (which it gets when becoming visible) don't call the parts of
9102 redisplay constructing glyphs, so simply exposing a frame won't
9103 display anything in this case. So, we have to display these
9104 frames here explicitly. */
9105 if (!pause)
9106 {
9107 Lisp_Object tail, frame;
9108 int new_count = 0;
9109
9110 FOR_EACH_FRAME (tail, frame)
9111 {
9112 int this_is_visible = 0;
9113
9114 if (XFRAME (frame)->visible)
9115 this_is_visible = 1;
9116 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9117 if (XFRAME (frame)->visible)
9118 this_is_visible = 1;
9119
9120 if (this_is_visible)
9121 new_count++;
9122 }
9123
9124 if (new_count != number_of_visible_frames)
9125 windows_or_buffers_changed++;
9126 }
9127
9128 /* Change frame size now if a change is pending. */
9129 do_pending_window_change (1);
9130
9131 /* If we just did a pending size change, or have additional
9132 visible frames, redisplay again. */
9133 if (windows_or_buffers_changed && !pause)
9134 goto retry;
9135
9136 end_of_redisplay:
9137 redisplay_updating_p = 0;
9138 unbind_to (count, Qnil);
9139 }
9140
9141
9142 /* Redisplay, but leave alone any recent echo area message unless
9143 another message has been requested in its place.
9144
9145 This is useful in situations where you need to redisplay but no
9146 user action has occurred, making it inappropriate for the message
9147 area to be cleared. See tracking_off and
9148 wait_reading_process_input for examples of these situations.
9149
9150 FROM_WHERE is an integer saying from where this function was
9151 called. This is useful for debugging. */
9152
9153 void
9154 redisplay_preserve_echo_area (from_where)
9155 int from_where;
9156 {
9157 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9158
9159 if (!NILP (echo_area_buffer[1]))
9160 {
9161 /* We have a previously displayed message, but no current
9162 message. Redisplay the previous message. */
9163 display_last_displayed_message_p = 1;
9164 redisplay_internal (1);
9165 display_last_displayed_message_p = 0;
9166 }
9167 else
9168 redisplay_internal (1);
9169 }
9170
9171
9172 /* Function registered with record_unwind_protect in
9173 redisplay_internal. Clears the flag indicating that a redisplay is
9174 in progress. */
9175
9176 static Lisp_Object
9177 unwind_redisplay (old_redisplaying_p)
9178 Lisp_Object old_redisplaying_p;
9179 {
9180 redisplaying_p = XFASTINT (old_redisplaying_p);
9181 return Qnil;
9182 }
9183
9184
9185 /* Mark the display of window W as accurate or inaccurate. If
9186 ACCURATE_P is non-zero mark display of W as accurate. If
9187 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9188 redisplay_internal is called. */
9189
9190 static void
9191 mark_window_display_accurate_1 (w, accurate_p)
9192 struct window *w;
9193 int accurate_p;
9194 {
9195 if (BUFFERP (w->buffer))
9196 {
9197 struct buffer *b = XBUFFER (w->buffer);
9198
9199 w->last_modified
9200 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9201 w->last_overlay_modified
9202 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9203 w->last_had_star
9204 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9205
9206 if (accurate_p)
9207 {
9208 b->clip_changed = 0;
9209 b->prevent_redisplay_optimizations_p = 0;
9210
9211 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9212 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9213 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9214 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9215
9216 w->current_matrix->buffer = b;
9217 w->current_matrix->begv = BUF_BEGV (b);
9218 w->current_matrix->zv = BUF_ZV (b);
9219
9220 w->last_cursor = w->cursor;
9221 w->last_cursor_off_p = w->cursor_off_p;
9222
9223 if (w == XWINDOW (selected_window))
9224 w->last_point = make_number (BUF_PT (b));
9225 else
9226 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9227 }
9228 }
9229
9230 if (accurate_p)
9231 {
9232 w->window_end_valid = w->buffer;
9233 #if 0 /* This is incorrect with variable-height lines. */
9234 xassert (XINT (w->window_end_vpos)
9235 < (XINT (w->height)
9236 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9237 #endif
9238 w->update_mode_line = Qnil;
9239 }
9240 }
9241
9242
9243 /* Mark the display of windows in the window tree rooted at WINDOW as
9244 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9245 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9246 be redisplayed the next time redisplay_internal is called. */
9247
9248 void
9249 mark_window_display_accurate (window, accurate_p)
9250 Lisp_Object window;
9251 int accurate_p;
9252 {
9253 struct window *w;
9254
9255 for (; !NILP (window); window = w->next)
9256 {
9257 w = XWINDOW (window);
9258 mark_window_display_accurate_1 (w, accurate_p);
9259
9260 if (!NILP (w->vchild))
9261 mark_window_display_accurate (w->vchild, accurate_p);
9262 if (!NILP (w->hchild))
9263 mark_window_display_accurate (w->hchild, accurate_p);
9264 }
9265
9266 if (accurate_p)
9267 {
9268 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9269 last_arrow_string = Voverlay_arrow_string;
9270 }
9271 else
9272 {
9273 /* Force a thorough redisplay the next time by setting
9274 last_arrow_position and last_arrow_string to t, which is
9275 unequal to any useful value of Voverlay_arrow_... */
9276 last_arrow_position = Qt;
9277 last_arrow_string = Qt;
9278 }
9279 }
9280
9281
9282 /* Return value in display table DP (Lisp_Char_Table *) for character
9283 C. Since a display table doesn't have any parent, we don't have to
9284 follow parent. Do not call this function directly but use the
9285 macro DISP_CHAR_VECTOR. */
9286
9287 Lisp_Object
9288 disp_char_vector (dp, c)
9289 struct Lisp_Char_Table *dp;
9290 int c;
9291 {
9292 int code[4], i;
9293 Lisp_Object val;
9294
9295 if (SINGLE_BYTE_CHAR_P (c))
9296 return (dp->contents[c]);
9297
9298 SPLIT_CHAR (c, code[0], code[1], code[2]);
9299 if (code[1] < 32)
9300 code[1] = -1;
9301 else if (code[2] < 32)
9302 code[2] = -1;
9303
9304 /* Here, the possible range of code[0] (== charset ID) is
9305 128..max_charset. Since the top level char table contains data
9306 for multibyte characters after 256th element, we must increment
9307 code[0] by 128 to get a correct index. */
9308 code[0] += 128;
9309 code[3] = -1; /* anchor */
9310
9311 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9312 {
9313 val = dp->contents[code[i]];
9314 if (!SUB_CHAR_TABLE_P (val))
9315 return (NILP (val) ? dp->defalt : val);
9316 }
9317
9318 /* Here, val is a sub char table. We return the default value of
9319 it. */
9320 return (dp->defalt);
9321 }
9322
9323
9324 \f
9325 /***********************************************************************
9326 Window Redisplay
9327 ***********************************************************************/
9328
9329 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9330
9331 static void
9332 redisplay_windows (window)
9333 Lisp_Object window;
9334 {
9335 while (!NILP (window))
9336 {
9337 struct window *w = XWINDOW (window);
9338
9339 if (!NILP (w->hchild))
9340 redisplay_windows (w->hchild);
9341 else if (!NILP (w->vchild))
9342 redisplay_windows (w->vchild);
9343 else
9344 {
9345 displayed_buffer = XBUFFER (w->buffer);
9346 /* Use list_of_error, not Qerror, so that
9347 we catch only errors and don't run the debugger. */
9348 internal_condition_case_1 (redisplay_window_0, window,
9349 list_of_error,
9350 redisplay_window_error);
9351 }
9352
9353 window = w->next;
9354 }
9355 }
9356
9357 static Lisp_Object
9358 redisplay_window_error ()
9359 {
9360 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9361 return Qnil;
9362 }
9363
9364 static Lisp_Object
9365 redisplay_window_0 (window)
9366 Lisp_Object window;
9367 {
9368 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9369 redisplay_window (window, 0);
9370 return Qnil;
9371 }
9372
9373 static Lisp_Object
9374 redisplay_window_1 (window)
9375 Lisp_Object window;
9376 {
9377 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9378 redisplay_window (window, 1);
9379 return Qnil;
9380 }
9381 \f
9382 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9383 DELTA is the number of bytes by which positions recorded in ROW
9384 differ from current buffer positions. */
9385
9386 void
9387 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9388 struct window *w;
9389 struct glyph_row *row;
9390 struct glyph_matrix *matrix;
9391 int delta, delta_bytes, dy, dvpos;
9392 {
9393 struct glyph *glyph = row->glyphs[TEXT_AREA];
9394 struct glyph *end = glyph + row->used[TEXT_AREA];
9395 int x = row->x;
9396 int pt_old = PT - delta;
9397
9398 /* Skip over glyphs not having an object at the start of the row.
9399 These are special glyphs like truncation marks on terminal
9400 frames. */
9401 if (row->displays_text_p)
9402 while (glyph < end
9403 && INTEGERP (glyph->object)
9404 && glyph->charpos < 0)
9405 {
9406 x += glyph->pixel_width;
9407 ++glyph;
9408 }
9409
9410 while (glyph < end
9411 && !INTEGERP (glyph->object)
9412 && (!BUFFERP (glyph->object)
9413 || glyph->charpos < pt_old))
9414 {
9415 x += glyph->pixel_width;
9416 ++glyph;
9417 }
9418
9419 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9420 w->cursor.x = x;
9421 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9422 w->cursor.y = row->y + dy;
9423
9424 if (w == XWINDOW (selected_window))
9425 {
9426 if (!row->continued_p
9427 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9428 && row->x == 0)
9429 {
9430 this_line_buffer = XBUFFER (w->buffer);
9431
9432 CHARPOS (this_line_start_pos)
9433 = MATRIX_ROW_START_CHARPOS (row) + delta;
9434 BYTEPOS (this_line_start_pos)
9435 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9436
9437 CHARPOS (this_line_end_pos)
9438 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9439 BYTEPOS (this_line_end_pos)
9440 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9441
9442 this_line_y = w->cursor.y;
9443 this_line_pixel_height = row->height;
9444 this_line_vpos = w->cursor.vpos;
9445 this_line_start_x = row->x;
9446 }
9447 else
9448 CHARPOS (this_line_start_pos) = 0;
9449 }
9450 }
9451
9452
9453 /* Run window scroll functions, if any, for WINDOW with new window
9454 start STARTP. Sets the window start of WINDOW to that position.
9455
9456 We assume that the window's buffer is really current. */
9457
9458 static INLINE struct text_pos
9459 run_window_scroll_functions (window, startp)
9460 Lisp_Object window;
9461 struct text_pos startp;
9462 {
9463 struct window *w = XWINDOW (window);
9464 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9465
9466 if (current_buffer != XBUFFER (w->buffer))
9467 abort ();
9468
9469 if (!NILP (Vwindow_scroll_functions))
9470 {
9471 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9472 make_number (CHARPOS (startp)));
9473 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9474 /* In case the hook functions switch buffers. */
9475 if (current_buffer != XBUFFER (w->buffer))
9476 set_buffer_internal_1 (XBUFFER (w->buffer));
9477 }
9478
9479 return startp;
9480 }
9481
9482
9483 /* Make sure the line containing the cursor is fully visible.
9484 A value of 1 means there is nothing to be done.
9485 (Either the line is fully visible, or it cannot be made so,
9486 or we cannot tell.)
9487 A value of 0 means the caller should do scrolling
9488 as if point had gone off the screen. */
9489
9490 static int
9491 make_cursor_line_fully_visible (w)
9492 struct window *w;
9493 {
9494 struct glyph_matrix *matrix;
9495 struct glyph_row *row;
9496 int window_height;
9497
9498 /* It's not always possible to find the cursor, e.g, when a window
9499 is full of overlay strings. Don't do anything in that case. */
9500 if (w->cursor.vpos < 0)
9501 return 1;
9502
9503 matrix = w->desired_matrix;
9504 row = MATRIX_ROW (matrix, w->cursor.vpos);
9505
9506 /* If the cursor row is not partially visible, there's nothing to do. */
9507 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9508 return 1;
9509
9510 /* If the row the cursor is in is taller than the window's height,
9511 it's not clear what to do, so do nothing. */
9512 window_height = window_box_height (w);
9513 if (row->height >= window_height)
9514 return 1;
9515
9516 return 0;
9517
9518 #if 0
9519 /* This code used to try to scroll the window just enough to make
9520 the line visible. It returned 0 to say that the caller should
9521 allocate larger glyph matrices. */
9522
9523 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9524 {
9525 int dy = row->height - row->visible_height;
9526 w->vscroll = 0;
9527 w->cursor.y += dy;
9528 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9529 }
9530 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9531 {
9532 int dy = - (row->height - row->visible_height);
9533 w->vscroll = dy;
9534 w->cursor.y += dy;
9535 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9536 }
9537
9538 /* When we change the cursor y-position of the selected window,
9539 change this_line_y as well so that the display optimization for
9540 the cursor line of the selected window in redisplay_internal uses
9541 the correct y-position. */
9542 if (w == XWINDOW (selected_window))
9543 this_line_y = w->cursor.y;
9544
9545 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9546 redisplay with larger matrices. */
9547 if (matrix->nrows < required_matrix_height (w))
9548 {
9549 fonts_changed_p = 1;
9550 return 0;
9551 }
9552
9553 return 1;
9554 #endif /* 0 */
9555 }
9556
9557
9558 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9559 non-zero means only WINDOW is redisplayed in redisplay_internal.
9560 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9561 in redisplay_window to bring a partially visible line into view in
9562 the case that only the cursor has moved.
9563
9564 Value is
9565
9566 1 if scrolling succeeded
9567
9568 0 if scrolling didn't find point.
9569
9570 -1 if new fonts have been loaded so that we must interrupt
9571 redisplay, adjust glyph matrices, and try again. */
9572
9573 enum
9574 {
9575 SCROLLING_SUCCESS,
9576 SCROLLING_FAILED,
9577 SCROLLING_NEED_LARGER_MATRICES
9578 };
9579
9580 static int
9581 try_scrolling (window, just_this_one_p, scroll_conservatively,
9582 scroll_step, temp_scroll_step)
9583 Lisp_Object window;
9584 int just_this_one_p;
9585 EMACS_INT scroll_conservatively, scroll_step;
9586 int temp_scroll_step;
9587 {
9588 struct window *w = XWINDOW (window);
9589 struct frame *f = XFRAME (w->frame);
9590 struct text_pos scroll_margin_pos;
9591 struct text_pos pos;
9592 struct text_pos startp;
9593 struct it it;
9594 Lisp_Object window_end;
9595 int this_scroll_margin;
9596 int dy = 0;
9597 int scroll_max;
9598 int rc;
9599 int amount_to_scroll = 0;
9600 Lisp_Object aggressive;
9601 int height;
9602
9603 #if GLYPH_DEBUG
9604 debug_method_add (w, "try_scrolling");
9605 #endif
9606
9607 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9608
9609 /* Compute scroll margin height in pixels. We scroll when point is
9610 within this distance from the top or bottom of the window. */
9611 if (scroll_margin > 0)
9612 {
9613 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9614 this_scroll_margin *= CANON_Y_UNIT (f);
9615 }
9616 else
9617 this_scroll_margin = 0;
9618
9619 /* Compute how much we should try to scroll maximally to bring point
9620 into view. */
9621 if (scroll_step || scroll_conservatively || temp_scroll_step)
9622 scroll_max = max (scroll_step,
9623 max (scroll_conservatively, temp_scroll_step));
9624 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9625 || NUMBERP (current_buffer->scroll_up_aggressively))
9626 /* We're trying to scroll because of aggressive scrolling
9627 but no scroll_step is set. Choose an arbitrary one. Maybe
9628 there should be a variable for this. */
9629 scroll_max = 10;
9630 else
9631 scroll_max = 0;
9632 scroll_max *= CANON_Y_UNIT (f);
9633
9634 /* Decide whether we have to scroll down. Start at the window end
9635 and move this_scroll_margin up to find the position of the scroll
9636 margin. */
9637 window_end = Fwindow_end (window, Qt);
9638 CHARPOS (scroll_margin_pos) = XINT (window_end);
9639 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9640 if (this_scroll_margin)
9641 {
9642 start_display (&it, w, scroll_margin_pos);
9643 move_it_vertically (&it, - this_scroll_margin);
9644 scroll_margin_pos = it.current.pos;
9645 }
9646
9647 if (PT >= CHARPOS (scroll_margin_pos))
9648 {
9649 int y0;
9650
9651 too_near_end:
9652 /* Point is in the scroll margin at the bottom of the window, or
9653 below. Compute a new window start that makes point visible. */
9654
9655 /* Compute the distance from the scroll margin to PT.
9656 Give up if the distance is greater than scroll_max. */
9657 start_display (&it, w, scroll_margin_pos);
9658 y0 = it.current_y;
9659 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9660 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9661
9662 /* To make point visible, we have to move the window start
9663 down so that the line the cursor is in is visible, which
9664 means we have to add in the height of the cursor line. */
9665 dy = line_bottom_y (&it) - y0;
9666
9667 if (dy > scroll_max)
9668 return SCROLLING_FAILED;
9669
9670 /* Move the window start down. If scrolling conservatively,
9671 move it just enough down to make point visible. If
9672 scroll_step is set, move it down by scroll_step. */
9673 start_display (&it, w, startp);
9674
9675 if (scroll_conservatively)
9676 amount_to_scroll
9677 = max (max (dy, CANON_Y_UNIT (f)),
9678 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9679 else if (scroll_step || temp_scroll_step)
9680 amount_to_scroll = scroll_max;
9681 else
9682 {
9683 aggressive = current_buffer->scroll_up_aggressively;
9684 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9685 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9686 if (NUMBERP (aggressive))
9687 amount_to_scroll = XFLOATINT (aggressive) * height;
9688 }
9689
9690 if (amount_to_scroll <= 0)
9691 return SCROLLING_FAILED;
9692
9693 move_it_vertically (&it, amount_to_scroll);
9694 startp = it.current.pos;
9695 }
9696 else
9697 {
9698 /* See if point is inside the scroll margin at the top of the
9699 window. */
9700 scroll_margin_pos = startp;
9701 if (this_scroll_margin)
9702 {
9703 start_display (&it, w, startp);
9704 move_it_vertically (&it, this_scroll_margin);
9705 scroll_margin_pos = it.current.pos;
9706 }
9707
9708 if (PT < CHARPOS (scroll_margin_pos))
9709 {
9710 /* Point is in the scroll margin at the top of the window or
9711 above what is displayed in the window. */
9712 int y0;
9713
9714 /* Compute the vertical distance from PT to the scroll
9715 margin position. Give up if distance is greater than
9716 scroll_max. */
9717 SET_TEXT_POS (pos, PT, PT_BYTE);
9718 start_display (&it, w, pos);
9719 y0 = it.current_y;
9720 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9721 it.last_visible_y, -1,
9722 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9723 dy = it.current_y - y0;
9724 if (dy > scroll_max)
9725 return SCROLLING_FAILED;
9726
9727 /* Compute new window start. */
9728 start_display (&it, w, startp);
9729
9730 if (scroll_conservatively)
9731 amount_to_scroll =
9732 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9733 else if (scroll_step || temp_scroll_step)
9734 amount_to_scroll = scroll_max;
9735 else
9736 {
9737 aggressive = current_buffer->scroll_down_aggressively;
9738 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9739 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9740 if (NUMBERP (aggressive))
9741 amount_to_scroll = XFLOATINT (aggressive) * height;
9742 }
9743
9744 if (amount_to_scroll <= 0)
9745 return SCROLLING_FAILED;
9746
9747 move_it_vertically (&it, - amount_to_scroll);
9748 startp = it.current.pos;
9749 }
9750 }
9751
9752 /* Run window scroll functions. */
9753 startp = run_window_scroll_functions (window, startp);
9754
9755 /* Display the window. Give up if new fonts are loaded, or if point
9756 doesn't appear. */
9757 if (!try_window (window, startp))
9758 rc = SCROLLING_NEED_LARGER_MATRICES;
9759 else if (w->cursor.vpos < 0)
9760 {
9761 clear_glyph_matrix (w->desired_matrix);
9762 rc = SCROLLING_FAILED;
9763 }
9764 else
9765 {
9766 /* Maybe forget recorded base line for line number display. */
9767 if (!just_this_one_p
9768 || current_buffer->clip_changed
9769 || BEG_UNCHANGED < CHARPOS (startp))
9770 w->base_line_number = Qnil;
9771
9772 /* If cursor ends up on a partially visible line,
9773 treat that as being off the bottom of the screen. */
9774 if (! make_cursor_line_fully_visible (w))
9775 goto too_near_end;
9776 rc = SCROLLING_SUCCESS;
9777 }
9778
9779 return rc;
9780 }
9781
9782
9783 /* Compute a suitable window start for window W if display of W starts
9784 on a continuation line. Value is non-zero if a new window start
9785 was computed.
9786
9787 The new window start will be computed, based on W's width, starting
9788 from the start of the continued line. It is the start of the
9789 screen line with the minimum distance from the old start W->start. */
9790
9791 static int
9792 compute_window_start_on_continuation_line (w)
9793 struct window *w;
9794 {
9795 struct text_pos pos, start_pos;
9796 int window_start_changed_p = 0;
9797
9798 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9799
9800 /* If window start is on a continuation line... Window start may be
9801 < BEGV in case there's invisible text at the start of the
9802 buffer (M-x rmail, for example). */
9803 if (CHARPOS (start_pos) > BEGV
9804 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9805 {
9806 struct it it;
9807 struct glyph_row *row;
9808
9809 /* Handle the case that the window start is out of range. */
9810 if (CHARPOS (start_pos) < BEGV)
9811 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9812 else if (CHARPOS (start_pos) > ZV)
9813 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9814
9815 /* Find the start of the continued line. This should be fast
9816 because scan_buffer is fast (newline cache). */
9817 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9818 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9819 row, DEFAULT_FACE_ID);
9820 reseat_at_previous_visible_line_start (&it);
9821
9822 /* If the line start is "too far" away from the window start,
9823 say it takes too much time to compute a new window start. */
9824 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9825 < XFASTINT (w->height) * XFASTINT (w->width))
9826 {
9827 int min_distance, distance;
9828
9829 /* Move forward by display lines to find the new window
9830 start. If window width was enlarged, the new start can
9831 be expected to be > the old start. If window width was
9832 decreased, the new window start will be < the old start.
9833 So, we're looking for the display line start with the
9834 minimum distance from the old window start. */
9835 pos = it.current.pos;
9836 min_distance = INFINITY;
9837 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9838 distance < min_distance)
9839 {
9840 min_distance = distance;
9841 pos = it.current.pos;
9842 move_it_by_lines (&it, 1, 0);
9843 }
9844
9845 /* Set the window start there. */
9846 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9847 window_start_changed_p = 1;
9848 }
9849 }
9850
9851 return window_start_changed_p;
9852 }
9853
9854
9855 /* Try cursor movement in case text has not changed in window WINDOW,
9856 with window start STARTP. Value is
9857
9858 CURSOR_MOVEMENT_SUCCESS if successful
9859
9860 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9861
9862 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9863 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9864 we want to scroll as if scroll-step were set to 1. See the code.
9865
9866 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9867 which case we have to abort this redisplay, and adjust matrices
9868 first. */
9869
9870 enum
9871 {
9872 CURSOR_MOVEMENT_SUCCESS,
9873 CURSOR_MOVEMENT_CANNOT_BE_USED,
9874 CURSOR_MOVEMENT_MUST_SCROLL,
9875 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9876 };
9877
9878 static int
9879 try_cursor_movement (window, startp, scroll_step)
9880 Lisp_Object window;
9881 struct text_pos startp;
9882 int *scroll_step;
9883 {
9884 struct window *w = XWINDOW (window);
9885 struct frame *f = XFRAME (w->frame);
9886 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
9887
9888 #if GLYPH_DEBUG
9889 if (inhibit_try_cursor_movement)
9890 return rc;
9891 #endif
9892
9893 /* Handle case where text has not changed, only point, and it has
9894 not moved off the frame. */
9895 if (/* Point may be in this window. */
9896 PT >= CHARPOS (startp)
9897 /* Selective display hasn't changed. */
9898 && !current_buffer->clip_changed
9899 /* Function force-mode-line-update is used to force a thorough
9900 redisplay. It sets either windows_or_buffers_changed or
9901 update_mode_lines. So don't take a shortcut here for these
9902 cases. */
9903 && !update_mode_lines
9904 && !windows_or_buffers_changed
9905 && !cursor_type_changed
9906 /* Can't use this case if highlighting a region. When a
9907 region exists, cursor movement has to do more than just
9908 set the cursor. */
9909 && !(!NILP (Vtransient_mark_mode)
9910 && !NILP (current_buffer->mark_active))
9911 && NILP (w->region_showing)
9912 && NILP (Vshow_trailing_whitespace)
9913 /* Right after splitting windows, last_point may be nil. */
9914 && INTEGERP (w->last_point)
9915 /* This code is not used for mini-buffer for the sake of the case
9916 of redisplaying to replace an echo area message; since in
9917 that case the mini-buffer contents per se are usually
9918 unchanged. This code is of no real use in the mini-buffer
9919 since the handling of this_line_start_pos, etc., in redisplay
9920 handles the same cases. */
9921 && !EQ (window, minibuf_window)
9922 /* When splitting windows or for new windows, it happens that
9923 redisplay is called with a nil window_end_vpos or one being
9924 larger than the window. This should really be fixed in
9925 window.c. I don't have this on my list, now, so we do
9926 approximately the same as the old redisplay code. --gerd. */
9927 && INTEGERP (w->window_end_vpos)
9928 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9929 && (FRAME_WINDOW_P (f)
9930 || !MARKERP (Voverlay_arrow_position)
9931 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9932 {
9933 int this_scroll_margin;
9934 struct glyph_row *row = NULL;
9935
9936 #if GLYPH_DEBUG
9937 debug_method_add (w, "cursor movement");
9938 #endif
9939
9940 /* Scroll if point within this distance from the top or bottom
9941 of the window. This is a pixel value. */
9942 this_scroll_margin = max (0, scroll_margin);
9943 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9944 this_scroll_margin *= CANON_Y_UNIT (f);
9945
9946 /* Start with the row the cursor was displayed during the last
9947 not paused redisplay. Give up if that row is not valid. */
9948 if (w->last_cursor.vpos < 0
9949 || w->last_cursor.vpos >= w->current_matrix->nrows)
9950 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9951 else
9952 {
9953 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9954 if (row->mode_line_p)
9955 ++row;
9956 if (!row->enabled_p)
9957 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9958 }
9959
9960 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
9961 {
9962 int scroll_p = 0;
9963 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9964
9965 if (PT > XFASTINT (w->last_point))
9966 {
9967 /* Point has moved forward. */
9968 while (MATRIX_ROW_END_CHARPOS (row) < PT
9969 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9970 {
9971 xassert (row->enabled_p);
9972 ++row;
9973 }
9974
9975 /* The end position of a row equals the start position
9976 of the next row. If PT is there, we would rather
9977 display it in the next line. */
9978 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9979 && MATRIX_ROW_END_CHARPOS (row) == PT
9980 && !cursor_row_p (w, row))
9981 ++row;
9982
9983 /* If within the scroll margin, scroll. Note that
9984 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9985 the next line would be drawn, and that
9986 this_scroll_margin can be zero. */
9987 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9988 || PT > MATRIX_ROW_END_CHARPOS (row)
9989 /* Line is completely visible last line in window
9990 and PT is to be set in the next line. */
9991 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9992 && PT == MATRIX_ROW_END_CHARPOS (row)
9993 && !row->ends_at_zv_p
9994 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9995 scroll_p = 1;
9996 }
9997 else if (PT < XFASTINT (w->last_point))
9998 {
9999 /* Cursor has to be moved backward. Note that PT >=
10000 CHARPOS (startp) because of the outer
10001 if-statement. */
10002 while (!row->mode_line_p
10003 && (MATRIX_ROW_START_CHARPOS (row) > PT
10004 || (MATRIX_ROW_START_CHARPOS (row) == PT
10005 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
10006 && (row->y > this_scroll_margin
10007 || CHARPOS (startp) == BEGV))
10008 {
10009 xassert (row->enabled_p);
10010 --row;
10011 }
10012
10013 /* Consider the following case: Window starts at BEGV,
10014 there is invisible, intangible text at BEGV, so that
10015 display starts at some point START > BEGV. It can
10016 happen that we are called with PT somewhere between
10017 BEGV and START. Try to handle that case. */
10018 if (row < w->current_matrix->rows
10019 || row->mode_line_p)
10020 {
10021 row = w->current_matrix->rows;
10022 if (row->mode_line_p)
10023 ++row;
10024 }
10025
10026 /* Due to newlines in overlay strings, we may have to
10027 skip forward over overlay strings. */
10028 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10029 && MATRIX_ROW_END_CHARPOS (row) == PT
10030 && !cursor_row_p (w, row))
10031 ++row;
10032
10033 /* If within the scroll margin, scroll. */
10034 if (row->y < this_scroll_margin
10035 && CHARPOS (startp) != BEGV)
10036 scroll_p = 1;
10037 }
10038
10039 if (PT < MATRIX_ROW_START_CHARPOS (row)
10040 || PT > MATRIX_ROW_END_CHARPOS (row))
10041 {
10042 /* if PT is not in the glyph row, give up. */
10043 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10044 }
10045 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10046 {
10047 if (PT == MATRIX_ROW_END_CHARPOS (row)
10048 && !row->ends_at_zv_p
10049 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
10050 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10051 else if (row->height > window_box_height (w))
10052 {
10053 /* If we end up in a partially visible line, let's
10054 make it fully visible, except when it's taller
10055 than the window, in which case we can't do much
10056 about it. */
10057 *scroll_step = 1;
10058 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10059 }
10060 else
10061 {
10062 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10063 try_window (window, startp);
10064 if (!make_cursor_line_fully_visible (w))
10065 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10066 else
10067 rc = CURSOR_MOVEMENT_SUCCESS;
10068 }
10069 }
10070 else if (scroll_p)
10071 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10072 else
10073 {
10074 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10075 rc = CURSOR_MOVEMENT_SUCCESS;
10076 }
10077 }
10078 }
10079
10080 return rc;
10081 }
10082
10083
10084 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
10085 selected_window is redisplayed. */
10086
10087 static void
10088 redisplay_window (window, just_this_one_p)
10089 Lisp_Object window;
10090 int just_this_one_p;
10091 {
10092 struct window *w = XWINDOW (window);
10093 struct frame *f = XFRAME (w->frame);
10094 struct buffer *buffer = XBUFFER (w->buffer);
10095 struct buffer *old = current_buffer;
10096 struct text_pos lpoint, opoint, startp;
10097 int update_mode_line;
10098 int tem;
10099 struct it it;
10100 /* Record it now because it's overwritten. */
10101 int current_matrix_up_to_date_p = 0;
10102 /* This is less strict than current_matrix_up_to_date_p.
10103 It indictes that the buffer contents and narrowing are unchanged. */
10104 int buffer_unchanged_p = 0;
10105 int temp_scroll_step = 0;
10106 int count = SPECPDL_INDEX ();
10107 int rc;
10108 int centering_position;
10109
10110 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10111 opoint = lpoint;
10112
10113 /* W must be a leaf window here. */
10114 xassert (!NILP (w->buffer));
10115 #if GLYPH_DEBUG
10116 *w->desired_matrix->method = 0;
10117 #endif
10118
10119 specbind (Qinhibit_point_motion_hooks, Qt);
10120
10121 reconsider_clip_changes (w, buffer);
10122
10123 /* Has the mode line to be updated? */
10124 update_mode_line = (!NILP (w->update_mode_line)
10125 || update_mode_lines
10126 || buffer->clip_changed
10127 || buffer->prevent_redisplay_optimizations_p);
10128
10129 if (MINI_WINDOW_P (w))
10130 {
10131 if (w == XWINDOW (echo_area_window)
10132 && !NILP (echo_area_buffer[0]))
10133 {
10134 if (update_mode_line)
10135 /* We may have to update a tty frame's menu bar or a
10136 tool-bar. Example `M-x C-h C-h C-g'. */
10137 goto finish_menu_bars;
10138 else
10139 /* We've already displayed the echo area glyphs in this window. */
10140 goto finish_scroll_bars;
10141 }
10142 else if (w != XWINDOW (minibuf_window))
10143 {
10144 /* W is a mini-buffer window, but it's not the currently
10145 active one, so clear it. */
10146 int yb = window_text_bottom_y (w);
10147 struct glyph_row *row;
10148 int y;
10149
10150 for (y = 0, row = w->desired_matrix->rows;
10151 y < yb;
10152 y += row->height, ++row)
10153 blank_row (w, row, y);
10154 goto finish_scroll_bars;
10155 }
10156
10157 clear_glyph_matrix (w->desired_matrix);
10158 }
10159
10160 /* Otherwise set up data on this window; select its buffer and point
10161 value. */
10162 /* Really select the buffer, for the sake of buffer-local
10163 variables. */
10164 set_buffer_internal_1 (XBUFFER (w->buffer));
10165 SET_TEXT_POS (opoint, PT, PT_BYTE);
10166
10167 current_matrix_up_to_date_p
10168 = (!NILP (w->window_end_valid)
10169 && !current_buffer->clip_changed
10170 && !current_buffer->prevent_redisplay_optimizations_p
10171 && XFASTINT (w->last_modified) >= MODIFF
10172 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10173
10174 buffer_unchanged_p
10175 = (!NILP (w->window_end_valid)
10176 && !current_buffer->clip_changed
10177 && XFASTINT (w->last_modified) >= MODIFF
10178 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10179
10180 /* When windows_or_buffers_changed is non-zero, we can't rely on
10181 the window end being valid, so set it to nil there. */
10182 if (windows_or_buffers_changed)
10183 {
10184 /* If window starts on a continuation line, maybe adjust the
10185 window start in case the window's width changed. */
10186 if (XMARKER (w->start)->buffer == current_buffer)
10187 compute_window_start_on_continuation_line (w);
10188
10189 w->window_end_valid = Qnil;
10190 }
10191
10192 /* Some sanity checks. */
10193 CHECK_WINDOW_END (w);
10194 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
10195 abort ();
10196 if (BYTEPOS (opoint) < CHARPOS (opoint))
10197 abort ();
10198
10199 /* If %c is in mode line, update it if needed. */
10200 if (!NILP (w->column_number_displayed)
10201 /* This alternative quickly identifies a common case
10202 where no change is needed. */
10203 && !(PT == XFASTINT (w->last_point)
10204 && XFASTINT (w->last_modified) >= MODIFF
10205 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10206 && (XFASTINT (w->column_number_displayed)
10207 != (int) current_column ())) /* iftc */
10208 update_mode_line = 1;
10209
10210 /* Count number of windows showing the selected buffer. An indirect
10211 buffer counts as its base buffer. */
10212 if (!just_this_one_p)
10213 {
10214 struct buffer *current_base, *window_base;
10215 current_base = current_buffer;
10216 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10217 if (current_base->base_buffer)
10218 current_base = current_base->base_buffer;
10219 if (window_base->base_buffer)
10220 window_base = window_base->base_buffer;
10221 if (current_base == window_base)
10222 buffer_shared++;
10223 }
10224
10225 /* Point refers normally to the selected window. For any other
10226 window, set up appropriate value. */
10227 if (!EQ (window, selected_window))
10228 {
10229 int new_pt = XMARKER (w->pointm)->charpos;
10230 int new_pt_byte = marker_byte_position (w->pointm);
10231 if (new_pt < BEGV)
10232 {
10233 new_pt = BEGV;
10234 new_pt_byte = BEGV_BYTE;
10235 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10236 }
10237 else if (new_pt > (ZV - 1))
10238 {
10239 new_pt = ZV;
10240 new_pt_byte = ZV_BYTE;
10241 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10242 }
10243
10244 /* We don't use SET_PT so that the point-motion hooks don't run. */
10245 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10246 }
10247
10248 /* If any of the character widths specified in the display table
10249 have changed, invalidate the width run cache. It's true that
10250 this may be a bit late to catch such changes, but the rest of
10251 redisplay goes (non-fatally) haywire when the display table is
10252 changed, so why should we worry about doing any better? */
10253 if (current_buffer->width_run_cache)
10254 {
10255 struct Lisp_Char_Table *disptab = buffer_display_table ();
10256
10257 if (! disptab_matches_widthtab (disptab,
10258 XVECTOR (current_buffer->width_table)))
10259 {
10260 invalidate_region_cache (current_buffer,
10261 current_buffer->width_run_cache,
10262 BEG, Z);
10263 recompute_width_table (current_buffer, disptab);
10264 }
10265 }
10266
10267 /* If window-start is screwed up, choose a new one. */
10268 if (XMARKER (w->start)->buffer != current_buffer)
10269 goto recenter;
10270
10271 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10272
10273 /* If someone specified a new starting point but did not insist,
10274 check whether it can be used. */
10275 if (!NILP (w->optional_new_start)
10276 && CHARPOS (startp) >= BEGV
10277 && CHARPOS (startp) <= ZV)
10278 {
10279 w->optional_new_start = Qnil;
10280 start_display (&it, w, startp);
10281 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10282 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10283 if (IT_CHARPOS (it) == PT)
10284 w->force_start = Qt;
10285 }
10286
10287 /* Handle case where place to start displaying has been specified,
10288 unless the specified location is outside the accessible range. */
10289 if (!NILP (w->force_start)
10290 || w->frozen_window_start_p)
10291 {
10292 w->force_start = Qnil;
10293 w->vscroll = 0;
10294 w->window_end_valid = Qnil;
10295
10296 /* Forget any recorded base line for line number display. */
10297 if (!buffer_unchanged_p)
10298 w->base_line_number = Qnil;
10299
10300 /* Redisplay the mode line. Select the buffer properly for that.
10301 Also, run the hook window-scroll-functions
10302 because we have scrolled. */
10303 /* Note, we do this after clearing force_start because
10304 if there's an error, it is better to forget about force_start
10305 than to get into an infinite loop calling the hook functions
10306 and having them get more errors. */
10307 if (!update_mode_line
10308 || ! NILP (Vwindow_scroll_functions))
10309 {
10310 update_mode_line = 1;
10311 w->update_mode_line = Qt;
10312 startp = run_window_scroll_functions (window, startp);
10313 }
10314
10315 w->last_modified = make_number (0);
10316 w->last_overlay_modified = make_number (0);
10317 if (CHARPOS (startp) < BEGV)
10318 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10319 else if (CHARPOS (startp) > ZV)
10320 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10321
10322 /* Redisplay, then check if cursor has been set during the
10323 redisplay. Give up if new fonts were loaded. */
10324 if (!try_window (window, startp))
10325 {
10326 w->force_start = Qt;
10327 clear_glyph_matrix (w->desired_matrix);
10328 goto finish_scroll_bars;
10329 }
10330
10331 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10332 {
10333 /* If point does not appear, try to move point so it does
10334 appear. The desired matrix has been built above, so we
10335 can use it here. */
10336 int window_height;
10337 struct glyph_row *row;
10338
10339 window_height = window_box_height (w) / 2;
10340 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10341 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
10342 ++row;
10343
10344 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10345 MATRIX_ROW_START_BYTEPOS (row));
10346
10347 if (w != XWINDOW (selected_window))
10348 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10349 else if (current_buffer == old)
10350 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10351
10352 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10353
10354 /* If we are highlighting the region, then we just changed
10355 the region, so redisplay to show it. */
10356 if (!NILP (Vtransient_mark_mode)
10357 && !NILP (current_buffer->mark_active))
10358 {
10359 clear_glyph_matrix (w->desired_matrix);
10360 if (!try_window (window, startp))
10361 goto need_larger_matrices;
10362 }
10363 }
10364
10365 if (!make_cursor_line_fully_visible (w))
10366 goto try_to_scroll;
10367 #if GLYPH_DEBUG
10368 debug_method_add (w, "forced window start");
10369 #endif
10370 goto done;
10371 }
10372
10373 /* Handle case where text has not changed, only point, and it has
10374 not moved off the frame, and we are not retrying after hscroll.
10375 (current_matrix_up_to_date_p is nonzero when retrying.) */
10376 if (current_matrix_up_to_date_p
10377 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10378 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10379 {
10380 switch (rc)
10381 {
10382 case CURSOR_MOVEMENT_SUCCESS:
10383 goto done;
10384
10385 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10386 goto need_larger_matrices;
10387
10388 case CURSOR_MOVEMENT_MUST_SCROLL:
10389 goto try_to_scroll;
10390
10391 default:
10392 abort ();
10393 }
10394 }
10395 /* If current starting point was originally the beginning of a line
10396 but no longer is, find a new starting point. */
10397 else if (!NILP (w->start_at_line_beg)
10398 && !(CHARPOS (startp) <= BEGV
10399 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10400 {
10401 #if GLYPH_DEBUG
10402 debug_method_add (w, "recenter 1");
10403 #endif
10404 goto recenter;
10405 }
10406
10407 /* Try scrolling with try_window_id. Value is > 0 if update has
10408 been done, it is -1 if we know that the same window start will
10409 not work. It is 0 if unsuccessful for some other reason. */
10410 else if ((tem = try_window_id (w)) != 0)
10411 {
10412 #if GLYPH_DEBUG
10413 debug_method_add (w, "try_window_id %d", tem);
10414 #endif
10415
10416 if (fonts_changed_p)
10417 goto need_larger_matrices;
10418 if (tem > 0)
10419 goto done;
10420
10421 /* Otherwise try_window_id has returned -1 which means that we
10422 don't want the alternative below this comment to execute. */
10423 }
10424 else if (CHARPOS (startp) >= BEGV
10425 && CHARPOS (startp) <= ZV
10426 && PT >= CHARPOS (startp)
10427 && (CHARPOS (startp) < ZV
10428 /* Avoid starting at end of buffer. */
10429 || CHARPOS (startp) == BEGV
10430 || (XFASTINT (w->last_modified) >= MODIFF
10431 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10432 {
10433 #if GLYPH_DEBUG
10434 debug_method_add (w, "same window start");
10435 #endif
10436
10437 /* Try to redisplay starting at same place as before.
10438 If point has not moved off frame, accept the results. */
10439 if (!current_matrix_up_to_date_p
10440 /* Don't use try_window_reusing_current_matrix in this case
10441 because a window scroll function can have changed the
10442 buffer. */
10443 || !NILP (Vwindow_scroll_functions)
10444 || MINI_WINDOW_P (w)
10445 || !try_window_reusing_current_matrix (w))
10446 {
10447 IF_DEBUG (debug_method_add (w, "1"));
10448 try_window (window, startp);
10449 }
10450
10451 if (fonts_changed_p)
10452 goto need_larger_matrices;
10453
10454 if (w->cursor.vpos >= 0)
10455 {
10456 if (!just_this_one_p
10457 || current_buffer->clip_changed
10458 || BEG_UNCHANGED < CHARPOS (startp))
10459 /* Forget any recorded base line for line number display. */
10460 w->base_line_number = Qnil;
10461
10462 if (!make_cursor_line_fully_visible (w))
10463 /* Drop through and scroll. */
10464 ;
10465 goto done;
10466 }
10467 else
10468 clear_glyph_matrix (w->desired_matrix);
10469 }
10470
10471 try_to_scroll:
10472
10473 w->last_modified = make_number (0);
10474 w->last_overlay_modified = make_number (0);
10475
10476 /* Redisplay the mode line. Select the buffer properly for that. */
10477 if (!update_mode_line)
10478 {
10479 update_mode_line = 1;
10480 w->update_mode_line = Qt;
10481 }
10482
10483 /* Try to scroll by specified few lines. */
10484 if ((scroll_conservatively
10485 || scroll_step
10486 || temp_scroll_step
10487 || NUMBERP (current_buffer->scroll_up_aggressively)
10488 || NUMBERP (current_buffer->scroll_down_aggressively))
10489 && !current_buffer->clip_changed
10490 && CHARPOS (startp) >= BEGV
10491 && CHARPOS (startp) <= ZV)
10492 {
10493 /* The function returns -1 if new fonts were loaded, 1 if
10494 successful, 0 if not successful. */
10495 int rc = try_scrolling (window, just_this_one_p,
10496 scroll_conservatively,
10497 scroll_step,
10498 temp_scroll_step);
10499 switch (rc)
10500 {
10501 case SCROLLING_SUCCESS:
10502 goto done;
10503
10504 case SCROLLING_NEED_LARGER_MATRICES:
10505 goto need_larger_matrices;
10506
10507 case SCROLLING_FAILED:
10508 break;
10509
10510 default:
10511 abort ();
10512 }
10513 }
10514
10515 /* Finally, just choose place to start which centers point */
10516
10517 recenter:
10518 centering_position = window_box_height (w) / 2;
10519
10520 point_at_top:
10521 /* Jump here with centering_position already set to 0. */
10522
10523 #if GLYPH_DEBUG
10524 debug_method_add (w, "recenter");
10525 #endif
10526
10527 /* w->vscroll = 0; */
10528
10529 /* Forget any previously recorded base line for line number display. */
10530 if (!buffer_unchanged_p)
10531 w->base_line_number = Qnil;
10532
10533 /* Move backward half the height of the window. */
10534 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10535 it.current_y = it.last_visible_y;
10536 move_it_vertically_backward (&it, centering_position);
10537 xassert (IT_CHARPOS (it) >= BEGV);
10538
10539 /* The function move_it_vertically_backward may move over more
10540 than the specified y-distance. If it->w is small, e.g. a
10541 mini-buffer window, we may end up in front of the window's
10542 display area. Start displaying at the start of the line
10543 containing PT in this case. */
10544 if (it.current_y <= 0)
10545 {
10546 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10547 move_it_vertically (&it, 0);
10548 xassert (IT_CHARPOS (it) <= PT);
10549 it.current_y = 0;
10550 }
10551
10552 it.current_x = it.hpos = 0;
10553
10554 /* Set startp here explicitly in case that helps avoid an infinite loop
10555 in case the window-scroll-functions functions get errors. */
10556 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10557
10558 /* Run scroll hooks. */
10559 startp = run_window_scroll_functions (window, it.current.pos);
10560
10561 /* Redisplay the window. */
10562 if (!current_matrix_up_to_date_p
10563 || windows_or_buffers_changed
10564 || cursor_type_changed
10565 /* Don't use try_window_reusing_current_matrix in this case
10566 because it can have changed the buffer. */
10567 || !NILP (Vwindow_scroll_functions)
10568 || !just_this_one_p
10569 || MINI_WINDOW_P (w)
10570 || !try_window_reusing_current_matrix (w))
10571 try_window (window, startp);
10572
10573 /* If new fonts have been loaded (due to fontsets), give up. We
10574 have to start a new redisplay since we need to re-adjust glyph
10575 matrices. */
10576 if (fonts_changed_p)
10577 goto need_larger_matrices;
10578
10579 /* If cursor did not appear assume that the middle of the window is
10580 in the first line of the window. Do it again with the next line.
10581 (Imagine a window of height 100, displaying two lines of height
10582 60. Moving back 50 from it->last_visible_y will end in the first
10583 line.) */
10584 if (w->cursor.vpos < 0)
10585 {
10586 if (!NILP (w->window_end_valid)
10587 && PT >= Z - XFASTINT (w->window_end_pos))
10588 {
10589 clear_glyph_matrix (w->desired_matrix);
10590 move_it_by_lines (&it, 1, 0);
10591 try_window (window, it.current.pos);
10592 }
10593 else if (PT < IT_CHARPOS (it))
10594 {
10595 clear_glyph_matrix (w->desired_matrix);
10596 move_it_by_lines (&it, -1, 0);
10597 try_window (window, it.current.pos);
10598 }
10599 else
10600 {
10601 /* Not much we can do about it. */
10602 }
10603 }
10604
10605 /* Consider the following case: Window starts at BEGV, there is
10606 invisible, intangible text at BEGV, so that display starts at
10607 some point START > BEGV. It can happen that we are called with
10608 PT somewhere between BEGV and START. Try to handle that case. */
10609 if (w->cursor.vpos < 0)
10610 {
10611 struct glyph_row *row = w->current_matrix->rows;
10612 if (row->mode_line_p)
10613 ++row;
10614 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10615 }
10616
10617 if (!make_cursor_line_fully_visible (w))
10618 {
10619 /* If centering point failed to make the whole line visible,
10620 put point at the top instead. That has to make the whole line
10621 visible, if it can be done. */
10622 centering_position = 0;
10623 goto point_at_top;
10624 }
10625
10626 done:
10627
10628 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10629 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10630 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10631 ? Qt : Qnil);
10632
10633 /* Display the mode line, if we must. */
10634 if ((update_mode_line
10635 /* If window not full width, must redo its mode line
10636 if (a) the window to its side is being redone and
10637 (b) we do a frame-based redisplay. This is a consequence
10638 of how inverted lines are drawn in frame-based redisplay. */
10639 || (!just_this_one_p
10640 && !FRAME_WINDOW_P (f)
10641 && !WINDOW_FULL_WIDTH_P (w))
10642 /* Line number to display. */
10643 || INTEGERP (w->base_line_pos)
10644 /* Column number is displayed and different from the one displayed. */
10645 || (!NILP (w->column_number_displayed)
10646 && (XFASTINT (w->column_number_displayed)
10647 != (int) current_column ()))) /* iftc */
10648 /* This means that the window has a mode line. */
10649 && (WINDOW_WANTS_MODELINE_P (w)
10650 || WINDOW_WANTS_HEADER_LINE_P (w)))
10651 {
10652 display_mode_lines (w);
10653
10654 /* If mode line height has changed, arrange for a thorough
10655 immediate redisplay using the correct mode line height. */
10656 if (WINDOW_WANTS_MODELINE_P (w)
10657 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10658 {
10659 fonts_changed_p = 1;
10660 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10661 = DESIRED_MODE_LINE_HEIGHT (w);
10662 }
10663
10664 /* If top line height has changed, arrange for a thorough
10665 immediate redisplay using the correct mode line height. */
10666 if (WINDOW_WANTS_HEADER_LINE_P (w)
10667 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10668 {
10669 fonts_changed_p = 1;
10670 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10671 = DESIRED_HEADER_LINE_HEIGHT (w);
10672 }
10673
10674 if (fonts_changed_p)
10675 goto need_larger_matrices;
10676 }
10677
10678 if (!line_number_displayed
10679 && !BUFFERP (w->base_line_pos))
10680 {
10681 w->base_line_pos = Qnil;
10682 w->base_line_number = Qnil;
10683 }
10684
10685 finish_menu_bars:
10686
10687 /* When we reach a frame's selected window, redo the frame's menu bar. */
10688 if (update_mode_line
10689 && EQ (FRAME_SELECTED_WINDOW (f), window))
10690 {
10691 int redisplay_menu_p = 0;
10692
10693 if (FRAME_WINDOW_P (f))
10694 {
10695 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS)
10696 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10697 #else
10698 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10699 #endif
10700 }
10701 else
10702 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10703
10704 if (redisplay_menu_p)
10705 display_menu_bar (w);
10706
10707 #ifdef HAVE_WINDOW_SYSTEM
10708 if (WINDOWP (f->tool_bar_window)
10709 && (FRAME_TOOL_BAR_LINES (f) > 0
10710 || auto_resize_tool_bars_p))
10711 redisplay_tool_bar (f);
10712 #endif
10713 }
10714
10715 need_larger_matrices:
10716 ;
10717 finish_scroll_bars:
10718
10719 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10720 {
10721 int start, end, whole;
10722
10723 /* Calculate the start and end positions for the current window.
10724 At some point, it would be nice to choose between scrollbars
10725 which reflect the whole buffer size, with special markers
10726 indicating narrowing, and scrollbars which reflect only the
10727 visible region.
10728
10729 Note that mini-buffers sometimes aren't displaying any text. */
10730 if (!MINI_WINDOW_P (w)
10731 || (w == XWINDOW (minibuf_window)
10732 && NILP (echo_area_buffer[0])))
10733 {
10734 whole = ZV - BEGV;
10735 start = marker_position (w->start) - BEGV;
10736 /* I don't think this is guaranteed to be right. For the
10737 moment, we'll pretend it is. */
10738 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10739
10740 if (end < start)
10741 end = start;
10742 if (whole < (end - start))
10743 whole = end - start;
10744 }
10745 else
10746 start = end = whole = 0;
10747
10748 /* Indicate what this scroll bar ought to be displaying now. */
10749 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10750
10751 /* Note that we actually used the scroll bar attached to this
10752 window, so it shouldn't be deleted at the end of redisplay. */
10753 redeem_scroll_bar_hook (w);
10754 }
10755
10756 /* Restore current_buffer and value of point in it. */
10757 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10758 set_buffer_internal_1 (old);
10759 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10760
10761 unbind_to (count, Qnil);
10762 }
10763
10764
10765 /* Build the complete desired matrix of WINDOW with a window start
10766 buffer position POS. Value is non-zero if successful. It is zero
10767 if fonts were loaded during redisplay which makes re-adjusting
10768 glyph matrices necessary. */
10769
10770 int
10771 try_window (window, pos)
10772 Lisp_Object window;
10773 struct text_pos pos;
10774 {
10775 struct window *w = XWINDOW (window);
10776 struct it it;
10777 struct glyph_row *last_text_row = NULL;
10778
10779 /* Make POS the new window start. */
10780 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10781
10782 /* Mark cursor position as unknown. No overlay arrow seen. */
10783 w->cursor.vpos = -1;
10784 overlay_arrow_seen = 0;
10785
10786 /* Initialize iterator and info to start at POS. */
10787 start_display (&it, w, pos);
10788
10789 /* Display all lines of W. */
10790 while (it.current_y < it.last_visible_y)
10791 {
10792 if (display_line (&it))
10793 last_text_row = it.glyph_row - 1;
10794 if (fonts_changed_p)
10795 return 0;
10796 }
10797
10798 /* If bottom moved off end of frame, change mode line percentage. */
10799 if (XFASTINT (w->window_end_pos) <= 0
10800 && Z != IT_CHARPOS (it))
10801 w->update_mode_line = Qt;
10802
10803 /* Set window_end_pos to the offset of the last character displayed
10804 on the window from the end of current_buffer. Set
10805 window_end_vpos to its row number. */
10806 if (last_text_row)
10807 {
10808 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10809 w->window_end_bytepos
10810 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10811 w->window_end_pos
10812 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10813 w->window_end_vpos
10814 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10815 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10816 ->displays_text_p);
10817 }
10818 else
10819 {
10820 w->window_end_bytepos = 0;
10821 w->window_end_pos = w->window_end_vpos = make_number (0);
10822 }
10823
10824 /* But that is not valid info until redisplay finishes. */
10825 w->window_end_valid = Qnil;
10826 return 1;
10827 }
10828
10829
10830 \f
10831 /************************************************************************
10832 Window redisplay reusing current matrix when buffer has not changed
10833 ************************************************************************/
10834
10835 /* Try redisplay of window W showing an unchanged buffer with a
10836 different window start than the last time it was displayed by
10837 reusing its current matrix. Value is non-zero if successful.
10838 W->start is the new window start. */
10839
10840 static int
10841 try_window_reusing_current_matrix (w)
10842 struct window *w;
10843 {
10844 struct frame *f = XFRAME (w->frame);
10845 struct glyph_row *row, *bottom_row;
10846 struct it it;
10847 struct run run;
10848 struct text_pos start, new_start;
10849 int nrows_scrolled, i;
10850 struct glyph_row *last_text_row;
10851 struct glyph_row *last_reused_text_row;
10852 struct glyph_row *start_row;
10853 int start_vpos, min_y, max_y;
10854
10855 #if GLYPH_DEBUG
10856 if (inhibit_try_window_reusing)
10857 return 0;
10858 #endif
10859
10860 if (/* This function doesn't handle terminal frames. */
10861 !FRAME_WINDOW_P (f)
10862 /* Don't try to reuse the display if windows have been split
10863 or such. */
10864 || windows_or_buffers_changed
10865 || cursor_type_changed)
10866 return 0;
10867
10868 /* Can't do this if region may have changed. */
10869 if ((!NILP (Vtransient_mark_mode)
10870 && !NILP (current_buffer->mark_active))
10871 || !NILP (w->region_showing)
10872 || !NILP (Vshow_trailing_whitespace))
10873 return 0;
10874
10875 /* If top-line visibility has changed, give up. */
10876 if (WINDOW_WANTS_HEADER_LINE_P (w)
10877 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10878 return 0;
10879
10880 /* Give up if old or new display is scrolled vertically. We could
10881 make this function handle this, but right now it doesn't. */
10882 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10883 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10884 return 0;
10885
10886 /* The variable new_start now holds the new window start. The old
10887 start `start' can be determined from the current matrix. */
10888 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10889 start = start_row->start.pos;
10890 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10891
10892 /* Clear the desired matrix for the display below. */
10893 clear_glyph_matrix (w->desired_matrix);
10894
10895 if (CHARPOS (new_start) <= CHARPOS (start))
10896 {
10897 int first_row_y;
10898
10899 /* Don't use this method if the display starts with an ellipsis
10900 displayed for invisible text. It's not easy to handle that case
10901 below, and it's certainly not worth the effort since this is
10902 not a frequent case. */
10903 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10904 return 0;
10905
10906 IF_DEBUG (debug_method_add (w, "twu1"));
10907
10908 /* Display up to a row that can be reused. The variable
10909 last_text_row is set to the last row displayed that displays
10910 text. Note that it.vpos == 0 if or if not there is a
10911 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10912 start_display (&it, w, new_start);
10913 first_row_y = it.current_y;
10914 w->cursor.vpos = -1;
10915 last_text_row = last_reused_text_row = NULL;
10916
10917 while (it.current_y < it.last_visible_y
10918 && IT_CHARPOS (it) < CHARPOS (start)
10919 && !fonts_changed_p)
10920 if (display_line (&it))
10921 last_text_row = it.glyph_row - 1;
10922
10923 /* A value of current_y < last_visible_y means that we stopped
10924 at the previous window start, which in turn means that we
10925 have at least one reusable row. */
10926 if (it.current_y < it.last_visible_y)
10927 {
10928 /* IT.vpos always starts from 0; it counts text lines. */
10929 nrows_scrolled = it.vpos;
10930
10931 /* Find PT if not already found in the lines displayed. */
10932 if (w->cursor.vpos < 0)
10933 {
10934 int dy = it.current_y - first_row_y;
10935
10936 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10937 row = row_containing_pos (w, PT, row, NULL, dy);
10938 if (row)
10939 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10940 dy, nrows_scrolled);
10941 else
10942 {
10943 clear_glyph_matrix (w->desired_matrix);
10944 return 0;
10945 }
10946 }
10947
10948 /* Scroll the display. Do it before the current matrix is
10949 changed. The problem here is that update has not yet
10950 run, i.e. part of the current matrix is not up to date.
10951 scroll_run_hook will clear the cursor, and use the
10952 current matrix to get the height of the row the cursor is
10953 in. */
10954 run.current_y = first_row_y;
10955 run.desired_y = it.current_y;
10956 run.height = it.last_visible_y - it.current_y;
10957
10958 if (run.height > 0 && run.current_y != run.desired_y)
10959 {
10960 update_begin (f);
10961 rif->update_window_begin_hook (w);
10962 rif->clear_mouse_face (w);
10963 rif->scroll_run_hook (w, &run);
10964 rif->update_window_end_hook (w, 0, 0);
10965 update_end (f);
10966 }
10967
10968 /* Shift current matrix down by nrows_scrolled lines. */
10969 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10970 rotate_matrix (w->current_matrix,
10971 start_vpos,
10972 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10973 nrows_scrolled);
10974
10975 /* Disable lines that must be updated. */
10976 for (i = 0; i < it.vpos; ++i)
10977 (start_row + i)->enabled_p = 0;
10978
10979 /* Re-compute Y positions. */
10980 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10981 max_y = it.last_visible_y;
10982 for (row = start_row + nrows_scrolled;
10983 row < bottom_row;
10984 ++row)
10985 {
10986 row->y = it.current_y;
10987 row->visible_height = row->height;
10988
10989 if (row->y < min_y)
10990 row->visible_height -= min_y - row->y;
10991 if (row->y + row->height > max_y)
10992 row->visible_height -= row->y + row->height - max_y;
10993
10994 it.current_y += row->height;
10995
10996 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10997 last_reused_text_row = row;
10998 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10999 break;
11000 }
11001
11002 /* Disable lines in the current matrix which are now
11003 below the window. */
11004 for (++row; row < bottom_row; ++row)
11005 row->enabled_p = 0;
11006 }
11007
11008 /* Update window_end_pos etc.; last_reused_text_row is the last
11009 reused row from the current matrix containing text, if any.
11010 The value of last_text_row is the last displayed line
11011 containing text. */
11012 if (last_reused_text_row)
11013 {
11014 w->window_end_bytepos
11015 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
11016 w->window_end_pos
11017 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
11018 w->window_end_vpos
11019 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
11020 w->current_matrix));
11021 }
11022 else if (last_text_row)
11023 {
11024 w->window_end_bytepos
11025 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11026 w->window_end_pos
11027 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11028 w->window_end_vpos
11029 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11030 }
11031 else
11032 {
11033 /* This window must be completely empty. */
11034 w->window_end_bytepos = 0;
11035 w->window_end_pos = w->window_end_vpos = make_number (0);
11036 }
11037 w->window_end_valid = Qnil;
11038
11039 /* Update hint: don't try scrolling again in update_window. */
11040 w->desired_matrix->no_scrolling_p = 1;
11041
11042 #if GLYPH_DEBUG
11043 debug_method_add (w, "try_window_reusing_current_matrix 1");
11044 #endif
11045 return 1;
11046 }
11047 else if (CHARPOS (new_start) > CHARPOS (start))
11048 {
11049 struct glyph_row *pt_row, *row;
11050 struct glyph_row *first_reusable_row;
11051 struct glyph_row *first_row_to_display;
11052 int dy;
11053 int yb = window_text_bottom_y (w);
11054
11055 /* Find the row starting at new_start, if there is one. Don't
11056 reuse a partially visible line at the end. */
11057 first_reusable_row = start_row;
11058 while (first_reusable_row->enabled_p
11059 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
11060 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11061 < CHARPOS (new_start)))
11062 ++first_reusable_row;
11063
11064 /* Give up if there is no row to reuse. */
11065 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
11066 || !first_reusable_row->enabled_p
11067 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11068 != CHARPOS (new_start)))
11069 return 0;
11070
11071 /* We can reuse fully visible rows beginning with
11072 first_reusable_row to the end of the window. Set
11073 first_row_to_display to the first row that cannot be reused.
11074 Set pt_row to the row containing point, if there is any. */
11075 pt_row = NULL;
11076 for (first_row_to_display = first_reusable_row;
11077 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
11078 ++first_row_to_display)
11079 {
11080 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
11081 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
11082 pt_row = first_row_to_display;
11083 }
11084
11085 /* Start displaying at the start of first_row_to_display. */
11086 xassert (first_row_to_display->y < yb);
11087 init_to_row_start (&it, w, first_row_to_display);
11088
11089 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
11090 - start_vpos);
11091 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
11092 - nrows_scrolled);
11093 it.current_y = (first_row_to_display->y - first_reusable_row->y
11094 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
11095
11096 /* Display lines beginning with first_row_to_display in the
11097 desired matrix. Set last_text_row to the last row displayed
11098 that displays text. */
11099 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
11100 if (pt_row == NULL)
11101 w->cursor.vpos = -1;
11102 last_text_row = NULL;
11103 while (it.current_y < it.last_visible_y && !fonts_changed_p)
11104 if (display_line (&it))
11105 last_text_row = it.glyph_row - 1;
11106
11107 /* Give up If point isn't in a row displayed or reused. */
11108 if (w->cursor.vpos < 0)
11109 {
11110 clear_glyph_matrix (w->desired_matrix);
11111 return 0;
11112 }
11113
11114 /* If point is in a reused row, adjust y and vpos of the cursor
11115 position. */
11116 if (pt_row)
11117 {
11118 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
11119 w->current_matrix);
11120 w->cursor.y -= first_reusable_row->y;
11121 }
11122
11123 /* Scroll the display. */
11124 run.current_y = first_reusable_row->y;
11125 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11126 run.height = it.last_visible_y - run.current_y;
11127 dy = run.current_y - run.desired_y;
11128
11129 if (run.height)
11130 {
11131 struct frame *f = XFRAME (WINDOW_FRAME (w));
11132 update_begin (f);
11133 rif->update_window_begin_hook (w);
11134 rif->clear_mouse_face (w);
11135 rif->scroll_run_hook (w, &run);
11136 rif->update_window_end_hook (w, 0, 0);
11137 update_end (f);
11138 }
11139
11140 /* Adjust Y positions of reused rows. */
11141 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11142 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11143 max_y = it.last_visible_y;
11144 for (row = first_reusable_row; row < first_row_to_display; ++row)
11145 {
11146 row->y -= dy;
11147 row->visible_height = row->height;
11148 if (row->y < min_y)
11149 row->visible_height -= min_y - row->y;
11150 if (row->y + row->height > max_y)
11151 row->visible_height -= row->y + row->height - max_y;
11152 }
11153
11154 /* Scroll the current matrix. */
11155 xassert (nrows_scrolled > 0);
11156 rotate_matrix (w->current_matrix,
11157 start_vpos,
11158 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11159 -nrows_scrolled);
11160
11161 /* Disable rows not reused. */
11162 for (row -= nrows_scrolled; row < bottom_row; ++row)
11163 row->enabled_p = 0;
11164
11165 /* Adjust window end. A null value of last_text_row means that
11166 the window end is in reused rows which in turn means that
11167 only its vpos can have changed. */
11168 if (last_text_row)
11169 {
11170 w->window_end_bytepos
11171 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11172 w->window_end_pos
11173 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11174 w->window_end_vpos
11175 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11176 }
11177 else
11178 {
11179 w->window_end_vpos
11180 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
11181 }
11182
11183 w->window_end_valid = Qnil;
11184 w->desired_matrix->no_scrolling_p = 1;
11185
11186 #if GLYPH_DEBUG
11187 debug_method_add (w, "try_window_reusing_current_matrix 2");
11188 #endif
11189 return 1;
11190 }
11191
11192 return 0;
11193 }
11194
11195
11196 \f
11197 /************************************************************************
11198 Window redisplay reusing current matrix when buffer has changed
11199 ************************************************************************/
11200
11201 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11202 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
11203 int *, int *));
11204 static struct glyph_row *
11205 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11206 struct glyph_row *));
11207
11208
11209 /* Return the last row in MATRIX displaying text. If row START is
11210 non-null, start searching with that row. IT gives the dimensions
11211 of the display. Value is null if matrix is empty; otherwise it is
11212 a pointer to the row found. */
11213
11214 static struct glyph_row *
11215 find_last_row_displaying_text (matrix, it, start)
11216 struct glyph_matrix *matrix;
11217 struct it *it;
11218 struct glyph_row *start;
11219 {
11220 struct glyph_row *row, *row_found;
11221
11222 /* Set row_found to the last row in IT->w's current matrix
11223 displaying text. The loop looks funny but think of partially
11224 visible lines. */
11225 row_found = NULL;
11226 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11227 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11228 {
11229 xassert (row->enabled_p);
11230 row_found = row;
11231 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11232 break;
11233 ++row;
11234 }
11235
11236 return row_found;
11237 }
11238
11239
11240 /* Return the last row in the current matrix of W that is not affected
11241 by changes at the start of current_buffer that occurred since W's
11242 current matrix was built. Value is null if no such row exists.
11243
11244 BEG_UNCHANGED us the number of characters unchanged at the start of
11245 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11246 first changed character in current_buffer. Characters at positions <
11247 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11248 when the current matrix was built. */
11249
11250 static struct glyph_row *
11251 find_last_unchanged_at_beg_row (w)
11252 struct window *w;
11253 {
11254 int first_changed_pos = BEG + BEG_UNCHANGED;
11255 struct glyph_row *row;
11256 struct glyph_row *row_found = NULL;
11257 int yb = window_text_bottom_y (w);
11258
11259 /* Find the last row displaying unchanged text. */
11260 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11261 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11262 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11263 {
11264 if (/* If row ends before first_changed_pos, it is unchanged,
11265 except in some case. */
11266 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11267 /* When row ends in ZV and we write at ZV it is not
11268 unchanged. */
11269 && !row->ends_at_zv_p
11270 /* When first_changed_pos is the end of a continued line,
11271 row is not unchanged because it may be no longer
11272 continued. */
11273 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11274 && row->continued_p))
11275 row_found = row;
11276
11277 /* Stop if last visible row. */
11278 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11279 break;
11280
11281 ++row;
11282 }
11283
11284 return row_found;
11285 }
11286
11287
11288 /* Find the first glyph row in the current matrix of W that is not
11289 affected by changes at the end of current_buffer since the
11290 time W's current matrix was built.
11291
11292 Return in *DELTA the number of chars by which buffer positions in
11293 unchanged text at the end of current_buffer must be adjusted.
11294
11295 Return in *DELTA_BYTES the corresponding number of bytes.
11296
11297 Value is null if no such row exists, i.e. all rows are affected by
11298 changes. */
11299
11300 static struct glyph_row *
11301 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11302 struct window *w;
11303 int *delta, *delta_bytes;
11304 {
11305 struct glyph_row *row;
11306 struct glyph_row *row_found = NULL;
11307
11308 *delta = *delta_bytes = 0;
11309
11310 /* Display must not have been paused, otherwise the current matrix
11311 is not up to date. */
11312 if (NILP (w->window_end_valid))
11313 abort ();
11314
11315 /* A value of window_end_pos >= END_UNCHANGED means that the window
11316 end is in the range of changed text. If so, there is no
11317 unchanged row at the end of W's current matrix. */
11318 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11319 return NULL;
11320
11321 /* Set row to the last row in W's current matrix displaying text. */
11322 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11323
11324 /* If matrix is entirely empty, no unchanged row exists. */
11325 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11326 {
11327 /* The value of row is the last glyph row in the matrix having a
11328 meaningful buffer position in it. The end position of row
11329 corresponds to window_end_pos. This allows us to translate
11330 buffer positions in the current matrix to current buffer
11331 positions for characters not in changed text. */
11332 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11333 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11334 int last_unchanged_pos, last_unchanged_pos_old;
11335 struct glyph_row *first_text_row
11336 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11337
11338 *delta = Z - Z_old;
11339 *delta_bytes = Z_BYTE - Z_BYTE_old;
11340
11341 /* Set last_unchanged_pos to the buffer position of the last
11342 character in the buffer that has not been changed. Z is the
11343 index + 1 of the last character in current_buffer, i.e. by
11344 subtracting END_UNCHANGED we get the index of the last
11345 unchanged character, and we have to add BEG to get its buffer
11346 position. */
11347 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11348 last_unchanged_pos_old = last_unchanged_pos - *delta;
11349
11350 /* Search backward from ROW for a row displaying a line that
11351 starts at a minimum position >= last_unchanged_pos_old. */
11352 for (; row > first_text_row; --row)
11353 {
11354 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11355 abort ();
11356
11357 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11358 row_found = row;
11359 }
11360 }
11361
11362 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11363 abort ();
11364
11365 return row_found;
11366 }
11367
11368
11369 /* Make sure that glyph rows in the current matrix of window W
11370 reference the same glyph memory as corresponding rows in the
11371 frame's frame matrix. This function is called after scrolling W's
11372 current matrix on a terminal frame in try_window_id and
11373 try_window_reusing_current_matrix. */
11374
11375 static void
11376 sync_frame_with_window_matrix_rows (w)
11377 struct window *w;
11378 {
11379 struct frame *f = XFRAME (w->frame);
11380 struct glyph_row *window_row, *window_row_end, *frame_row;
11381
11382 /* Preconditions: W must be a leaf window and full-width. Its frame
11383 must have a frame matrix. */
11384 xassert (NILP (w->hchild) && NILP (w->vchild));
11385 xassert (WINDOW_FULL_WIDTH_P (w));
11386 xassert (!FRAME_WINDOW_P (f));
11387
11388 /* If W is a full-width window, glyph pointers in W's current matrix
11389 have, by definition, to be the same as glyph pointers in the
11390 corresponding frame matrix. Note that frame matrices have no
11391 marginal areas (see build_frame_matrix). */
11392 window_row = w->current_matrix->rows;
11393 window_row_end = window_row + w->current_matrix->nrows;
11394 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11395 while (window_row < window_row_end)
11396 {
11397 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
11398 struct glyph *end = window_row->glyphs[LAST_AREA];
11399
11400 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
11401 frame_row->glyphs[TEXT_AREA] = start;
11402 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
11403 frame_row->glyphs[LAST_AREA] = end;
11404
11405 /* Disable frame rows whose corresponding window rows have
11406 been disabled in try_window_id. */
11407 if (!window_row->enabled_p)
11408 frame_row->enabled_p = 0;
11409
11410 ++window_row, ++frame_row;
11411 }
11412 }
11413
11414
11415 /* Find the glyph row in window W containing CHARPOS. Consider all
11416 rows between START and END (not inclusive). END null means search
11417 all rows to the end of the display area of W. Value is the row
11418 containing CHARPOS or null. */
11419
11420 struct glyph_row *
11421 row_containing_pos (w, charpos, start, end, dy)
11422 struct window *w;
11423 int charpos;
11424 struct glyph_row *start, *end;
11425 int dy;
11426 {
11427 struct glyph_row *row = start;
11428 int last_y;
11429
11430 /* If we happen to start on a header-line, skip that. */
11431 if (row->mode_line_p)
11432 ++row;
11433
11434 if ((end && row >= end) || !row->enabled_p)
11435 return NULL;
11436
11437 last_y = window_text_bottom_y (w) - dy;
11438
11439 while ((end == NULL || row < end)
11440 && MATRIX_ROW_BOTTOM_Y (row) < last_y
11441 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11442 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11443 /* The end position of a row equals the start
11444 position of the next row. If CHARPOS is there, we
11445 would rather display it in the next line, except
11446 when this line ends in ZV. */
11447 && !row->ends_at_zv_p
11448 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))
11449 ++row;
11450
11451 /* Give up if CHARPOS not found. */
11452 if ((end && row >= end)
11453 || charpos < MATRIX_ROW_START_CHARPOS (row)
11454 || charpos > MATRIX_ROW_END_CHARPOS (row))
11455 row = NULL;
11456
11457 return row;
11458 }
11459
11460
11461 /* Try to redisplay window W by reusing its existing display. W's
11462 current matrix must be up to date when this function is called,
11463 i.e. window_end_valid must not be nil.
11464
11465 Value is
11466
11467 1 if display has been updated
11468 0 if otherwise unsuccessful
11469 -1 if redisplay with same window start is known not to succeed
11470
11471 The following steps are performed:
11472
11473 1. Find the last row in the current matrix of W that is not
11474 affected by changes at the start of current_buffer. If no such row
11475 is found, give up.
11476
11477 2. Find the first row in W's current matrix that is not affected by
11478 changes at the end of current_buffer. Maybe there is no such row.
11479
11480 3. Display lines beginning with the row + 1 found in step 1 to the
11481 row found in step 2 or, if step 2 didn't find a row, to the end of
11482 the window.
11483
11484 4. If cursor is not known to appear on the window, give up.
11485
11486 5. If display stopped at the row found in step 2, scroll the
11487 display and current matrix as needed.
11488
11489 6. Maybe display some lines at the end of W, if we must. This can
11490 happen under various circumstances, like a partially visible line
11491 becoming fully visible, or because newly displayed lines are displayed
11492 in smaller font sizes.
11493
11494 7. Update W's window end information. */
11495
11496 static int
11497 try_window_id (w)
11498 struct window *w;
11499 {
11500 struct frame *f = XFRAME (w->frame);
11501 struct glyph_matrix *current_matrix = w->current_matrix;
11502 struct glyph_matrix *desired_matrix = w->desired_matrix;
11503 struct glyph_row *last_unchanged_at_beg_row;
11504 struct glyph_row *first_unchanged_at_end_row;
11505 struct glyph_row *row;
11506 struct glyph_row *bottom_row;
11507 int bottom_vpos;
11508 struct it it;
11509 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11510 struct text_pos start_pos;
11511 struct run run;
11512 int first_unchanged_at_end_vpos = 0;
11513 struct glyph_row *last_text_row, *last_text_row_at_end;
11514 struct text_pos start;
11515 int first_changed_charpos, last_changed_charpos;
11516
11517 #if GLYPH_DEBUG
11518 if (inhibit_try_window_id)
11519 return 0;
11520 #endif
11521
11522 /* This is handy for debugging. */
11523 #if 0
11524 #define GIVE_UP(X) \
11525 do { \
11526 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11527 return 0; \
11528 } while (0)
11529 #else
11530 #define GIVE_UP(X) return 0
11531 #endif
11532
11533 SET_TEXT_POS_FROM_MARKER (start, w->start);
11534
11535 /* Don't use this for mini-windows because these can show
11536 messages and mini-buffers, and we don't handle that here. */
11537 if (MINI_WINDOW_P (w))
11538 GIVE_UP (1);
11539
11540 /* This flag is used to prevent redisplay optimizations. */
11541 if (windows_or_buffers_changed || cursor_type_changed)
11542 GIVE_UP (2);
11543
11544 /* Verify that narrowing has not changed.
11545 Also verify that we were not told to prevent redisplay optimizations.
11546 It would be nice to further
11547 reduce the number of cases where this prevents try_window_id. */
11548 if (current_buffer->clip_changed
11549 || current_buffer->prevent_redisplay_optimizations_p)
11550 GIVE_UP (3);
11551
11552 /* Window must either use window-based redisplay or be full width. */
11553 if (!FRAME_WINDOW_P (f)
11554 && (!line_ins_del_ok
11555 || !WINDOW_FULL_WIDTH_P (w)))
11556 GIVE_UP (4);
11557
11558 /* Give up if point is not known NOT to appear in W. */
11559 if (PT < CHARPOS (start))
11560 GIVE_UP (5);
11561
11562 /* Another way to prevent redisplay optimizations. */
11563 if (XFASTINT (w->last_modified) == 0)
11564 GIVE_UP (6);
11565
11566 /* Verify that window is not hscrolled. */
11567 if (XFASTINT (w->hscroll) != 0)
11568 GIVE_UP (7);
11569
11570 /* Verify that display wasn't paused. */
11571 if (NILP (w->window_end_valid))
11572 GIVE_UP (8);
11573
11574 /* Can't use this if highlighting a region because a cursor movement
11575 will do more than just set the cursor. */
11576 if (!NILP (Vtransient_mark_mode)
11577 && !NILP (current_buffer->mark_active))
11578 GIVE_UP (9);
11579
11580 /* Likewise if highlighting trailing whitespace. */
11581 if (!NILP (Vshow_trailing_whitespace))
11582 GIVE_UP (11);
11583
11584 /* Likewise if showing a region. */
11585 if (!NILP (w->region_showing))
11586 GIVE_UP (10);
11587
11588 /* Can use this if overlay arrow position and or string have changed. */
11589 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11590 || !EQ (last_arrow_string, Voverlay_arrow_string))
11591 GIVE_UP (12);
11592
11593
11594 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11595 only if buffer has really changed. The reason is that the gap is
11596 initially at Z for freshly visited files. The code below would
11597 set end_unchanged to 0 in that case. */
11598 if (MODIFF > SAVE_MODIFF
11599 /* This seems to happen sometimes after saving a buffer. */
11600 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11601 {
11602 if (GPT - BEG < BEG_UNCHANGED)
11603 BEG_UNCHANGED = GPT - BEG;
11604 if (Z - GPT < END_UNCHANGED)
11605 END_UNCHANGED = Z - GPT;
11606 }
11607
11608 /* The position of the first and last character that has been changed. */
11609 first_changed_charpos = BEG + BEG_UNCHANGED;
11610 last_changed_charpos = Z - END_UNCHANGED;
11611
11612 /* If window starts after a line end, and the last change is in
11613 front of that newline, then changes don't affect the display.
11614 This case happens with stealth-fontification. Note that although
11615 the display is unchanged, glyph positions in the matrix have to
11616 be adjusted, of course. */
11617 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11618 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11619 && ((last_changed_charpos < CHARPOS (start)
11620 && CHARPOS (start) == BEGV)
11621 || (last_changed_charpos < CHARPOS (start) - 1
11622 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11623 {
11624 int Z_old, delta, Z_BYTE_old, delta_bytes;
11625 struct glyph_row *r0;
11626
11627 /* Compute how many chars/bytes have been added to or removed
11628 from the buffer. */
11629 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11630 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11631 delta = Z - Z_old;
11632 delta_bytes = Z_BYTE - Z_BYTE_old;
11633
11634 /* Give up if PT is not in the window. Note that it already has
11635 been checked at the start of try_window_id that PT is not in
11636 front of the window start. */
11637 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11638 GIVE_UP (13);
11639
11640 /* If window start is unchanged, we can reuse the whole matrix
11641 as is, after adjusting glyph positions. No need to compute
11642 the window end again, since its offset from Z hasn't changed. */
11643 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11644 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11645 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11646 {
11647 /* Adjust positions in the glyph matrix. */
11648 if (delta || delta_bytes)
11649 {
11650 struct glyph_row *r1
11651 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11652 increment_matrix_positions (w->current_matrix,
11653 MATRIX_ROW_VPOS (r0, current_matrix),
11654 MATRIX_ROW_VPOS (r1, current_matrix),
11655 delta, delta_bytes);
11656 }
11657
11658 /* Set the cursor. */
11659 row = row_containing_pos (w, PT, r0, NULL, 0);
11660 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11661 return 1;
11662 }
11663 }
11664
11665 /* Handle the case that changes are all below what is displayed in
11666 the window, and that PT is in the window. This shortcut cannot
11667 be taken if ZV is visible in the window, and text has been added
11668 there that is visible in the window. */
11669 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11670 /* ZV is not visible in the window, or there are no
11671 changes at ZV, actually. */
11672 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11673 || first_changed_charpos == last_changed_charpos))
11674 {
11675 struct glyph_row *r0;
11676
11677 /* Give up if PT is not in the window. Note that it already has
11678 been checked at the start of try_window_id that PT is not in
11679 front of the window start. */
11680 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11681 GIVE_UP (14);
11682
11683 /* If window start is unchanged, we can reuse the whole matrix
11684 as is, without changing glyph positions since no text has
11685 been added/removed in front of the window end. */
11686 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11687 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11688 {
11689 /* We have to compute the window end anew since text
11690 can have been added/removed after it. */
11691 w->window_end_pos
11692 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11693 w->window_end_bytepos
11694 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11695
11696 /* Set the cursor. */
11697 row = row_containing_pos (w, PT, r0, NULL, 0);
11698 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11699 return 2;
11700 }
11701 }
11702
11703 /* Give up if window start is in the changed area.
11704
11705 The condition used to read
11706
11707 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11708
11709 but why that was tested escapes me at the moment. */
11710 if (CHARPOS (start) >= first_changed_charpos
11711 && CHARPOS (start) <= last_changed_charpos)
11712 GIVE_UP (15);
11713
11714 /* Check that window start agrees with the start of the first glyph
11715 row in its current matrix. Check this after we know the window
11716 start is not in changed text, otherwise positions would not be
11717 comparable. */
11718 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11719 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11720 GIVE_UP (16);
11721
11722 /* Give up if the window ends in strings. Overlay strings
11723 at the end are difficult to handle, so don't try. */
11724 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
11725 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
11726 GIVE_UP (20);
11727
11728 /* Compute the position at which we have to start displaying new
11729 lines. Some of the lines at the top of the window might be
11730 reusable because they are not displaying changed text. Find the
11731 last row in W's current matrix not affected by changes at the
11732 start of current_buffer. Value is null if changes start in the
11733 first line of window. */
11734 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11735 if (last_unchanged_at_beg_row)
11736 {
11737 /* Avoid starting to display in the moddle of a character, a TAB
11738 for instance. This is easier than to set up the iterator
11739 exactly, and it's not a frequent case, so the additional
11740 effort wouldn't really pay off. */
11741 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11742 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
11743 && last_unchanged_at_beg_row > w->current_matrix->rows)
11744 --last_unchanged_at_beg_row;
11745
11746 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11747 GIVE_UP (17);
11748
11749 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11750 GIVE_UP (18);
11751 start_pos = it.current.pos;
11752
11753 /* Start displaying new lines in the desired matrix at the same
11754 vpos we would use in the current matrix, i.e. below
11755 last_unchanged_at_beg_row. */
11756 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11757 current_matrix);
11758 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11759 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11760
11761 xassert (it.hpos == 0 && it.current_x == 0);
11762 }
11763 else
11764 {
11765 /* There are no reusable lines at the start of the window.
11766 Start displaying in the first line. */
11767 start_display (&it, w, start);
11768 start_pos = it.current.pos;
11769 }
11770
11771 /* Find the first row that is not affected by changes at the end of
11772 the buffer. Value will be null if there is no unchanged row, in
11773 which case we must redisplay to the end of the window. delta
11774 will be set to the value by which buffer positions beginning with
11775 first_unchanged_at_end_row have to be adjusted due to text
11776 changes. */
11777 first_unchanged_at_end_row
11778 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11779 IF_DEBUG (debug_delta = delta);
11780 IF_DEBUG (debug_delta_bytes = delta_bytes);
11781
11782 /* Set stop_pos to the buffer position up to which we will have to
11783 display new lines. If first_unchanged_at_end_row != NULL, this
11784 is the buffer position of the start of the line displayed in that
11785 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11786 that we don't stop at a buffer position. */
11787 stop_pos = 0;
11788 if (first_unchanged_at_end_row)
11789 {
11790 xassert (last_unchanged_at_beg_row == NULL
11791 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11792
11793 /* If this is a continuation line, move forward to the next one
11794 that isn't. Changes in lines above affect this line.
11795 Caution: this may move first_unchanged_at_end_row to a row
11796 not displaying text. */
11797 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11798 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11799 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11800 < it.last_visible_y))
11801 ++first_unchanged_at_end_row;
11802
11803 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11804 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11805 >= it.last_visible_y))
11806 first_unchanged_at_end_row = NULL;
11807 else
11808 {
11809 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11810 + delta);
11811 first_unchanged_at_end_vpos
11812 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11813 xassert (stop_pos >= Z - END_UNCHANGED);
11814 }
11815 }
11816 else if (last_unchanged_at_beg_row == NULL)
11817 GIVE_UP (19);
11818
11819
11820 #if GLYPH_DEBUG
11821
11822 /* Either there is no unchanged row at the end, or the one we have
11823 now displays text. This is a necessary condition for the window
11824 end pos calculation at the end of this function. */
11825 xassert (first_unchanged_at_end_row == NULL
11826 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11827
11828 debug_last_unchanged_at_beg_vpos
11829 = (last_unchanged_at_beg_row
11830 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11831 : -1);
11832 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11833
11834 #endif /* GLYPH_DEBUG != 0 */
11835
11836
11837 /* Display new lines. Set last_text_row to the last new line
11838 displayed which has text on it, i.e. might end up as being the
11839 line where the window_end_vpos is. */
11840 w->cursor.vpos = -1;
11841 last_text_row = NULL;
11842 overlay_arrow_seen = 0;
11843 while (it.current_y < it.last_visible_y
11844 && !fonts_changed_p
11845 && (first_unchanged_at_end_row == NULL
11846 || IT_CHARPOS (it) < stop_pos))
11847 {
11848 if (display_line (&it))
11849 last_text_row = it.glyph_row - 1;
11850 }
11851
11852 if (fonts_changed_p)
11853 return -1;
11854
11855
11856 /* Compute differences in buffer positions, y-positions etc. for
11857 lines reused at the bottom of the window. Compute what we can
11858 scroll. */
11859 if (first_unchanged_at_end_row
11860 /* No lines reused because we displayed everything up to the
11861 bottom of the window. */
11862 && it.current_y < it.last_visible_y)
11863 {
11864 dvpos = (it.vpos
11865 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11866 current_matrix));
11867 dy = it.current_y - first_unchanged_at_end_row->y;
11868 run.current_y = first_unchanged_at_end_row->y;
11869 run.desired_y = run.current_y + dy;
11870 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11871 }
11872 else
11873 {
11874 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11875 first_unchanged_at_end_row = NULL;
11876 }
11877 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11878
11879
11880 /* Find the cursor if not already found. We have to decide whether
11881 PT will appear on this window (it sometimes doesn't, but this is
11882 not a very frequent case.) This decision has to be made before
11883 the current matrix is altered. A value of cursor.vpos < 0 means
11884 that PT is either in one of the lines beginning at
11885 first_unchanged_at_end_row or below the window. Don't care for
11886 lines that might be displayed later at the window end; as
11887 mentioned, this is not a frequent case. */
11888 if (w->cursor.vpos < 0)
11889 {
11890 /* Cursor in unchanged rows at the top? */
11891 if (PT < CHARPOS (start_pos)
11892 && last_unchanged_at_beg_row)
11893 {
11894 row = row_containing_pos (w, PT,
11895 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11896 last_unchanged_at_beg_row + 1, 0);
11897 if (row)
11898 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11899 }
11900
11901 /* Start from first_unchanged_at_end_row looking for PT. */
11902 else if (first_unchanged_at_end_row)
11903 {
11904 row = row_containing_pos (w, PT - delta,
11905 first_unchanged_at_end_row, NULL, 0);
11906 if (row)
11907 set_cursor_from_row (w, row, w->current_matrix, delta,
11908 delta_bytes, dy, dvpos);
11909 }
11910
11911 /* Give up if cursor was not found. */
11912 if (w->cursor.vpos < 0)
11913 {
11914 clear_glyph_matrix (w->desired_matrix);
11915 return -1;
11916 }
11917 }
11918
11919 /* Don't let the cursor end in the scroll margins. */
11920 {
11921 int this_scroll_margin, cursor_height;
11922
11923 this_scroll_margin = max (0, scroll_margin);
11924 this_scroll_margin = min (this_scroll_margin,
11925 XFASTINT (w->height) / 4);
11926 this_scroll_margin *= CANON_Y_UNIT (it.f);
11927 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11928
11929 if ((w->cursor.y < this_scroll_margin
11930 && CHARPOS (start) > BEGV)
11931 /* Don't take scroll margin into account at the bottom because
11932 old redisplay didn't do it either. */
11933 || w->cursor.y + cursor_height > it.last_visible_y)
11934 {
11935 w->cursor.vpos = -1;
11936 clear_glyph_matrix (w->desired_matrix);
11937 return -1;
11938 }
11939 }
11940
11941 /* Scroll the display. Do it before changing the current matrix so
11942 that xterm.c doesn't get confused about where the cursor glyph is
11943 found. */
11944 if (dy && run.height)
11945 {
11946 update_begin (f);
11947
11948 if (FRAME_WINDOW_P (f))
11949 {
11950 rif->update_window_begin_hook (w);
11951 rif->clear_mouse_face (w);
11952 rif->scroll_run_hook (w, &run);
11953 rif->update_window_end_hook (w, 0, 0);
11954 }
11955 else
11956 {
11957 /* Terminal frame. In this case, dvpos gives the number of
11958 lines to scroll by; dvpos < 0 means scroll up. */
11959 int first_unchanged_at_end_vpos
11960 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11961 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11962 int end = (XFASTINT (w->top)
11963 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11964 + window_internal_height (w));
11965
11966 /* Perform the operation on the screen. */
11967 if (dvpos > 0)
11968 {
11969 /* Scroll last_unchanged_at_beg_row to the end of the
11970 window down dvpos lines. */
11971 set_terminal_window (end);
11972
11973 /* On dumb terminals delete dvpos lines at the end
11974 before inserting dvpos empty lines. */
11975 if (!scroll_region_ok)
11976 ins_del_lines (end - dvpos, -dvpos);
11977
11978 /* Insert dvpos empty lines in front of
11979 last_unchanged_at_beg_row. */
11980 ins_del_lines (from, dvpos);
11981 }
11982 else if (dvpos < 0)
11983 {
11984 /* Scroll up last_unchanged_at_beg_vpos to the end of
11985 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11986 set_terminal_window (end);
11987
11988 /* Delete dvpos lines in front of
11989 last_unchanged_at_beg_vpos. ins_del_lines will set
11990 the cursor to the given vpos and emit |dvpos| delete
11991 line sequences. */
11992 ins_del_lines (from + dvpos, dvpos);
11993
11994 /* On a dumb terminal insert dvpos empty lines at the
11995 end. */
11996 if (!scroll_region_ok)
11997 ins_del_lines (end + dvpos, -dvpos);
11998 }
11999
12000 set_terminal_window (0);
12001 }
12002
12003 update_end (f);
12004 }
12005
12006 /* Shift reused rows of the current matrix to the right position.
12007 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
12008 text. */
12009 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12010 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
12011 if (dvpos < 0)
12012 {
12013 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
12014 bottom_vpos, dvpos);
12015 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
12016 bottom_vpos, 0);
12017 }
12018 else if (dvpos > 0)
12019 {
12020 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
12021 bottom_vpos, dvpos);
12022 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
12023 first_unchanged_at_end_vpos + dvpos, 0);
12024 }
12025
12026 /* For frame-based redisplay, make sure that current frame and window
12027 matrix are in sync with respect to glyph memory. */
12028 if (!FRAME_WINDOW_P (f))
12029 sync_frame_with_window_matrix_rows (w);
12030
12031 /* Adjust buffer positions in reused rows. */
12032 if (delta)
12033 increment_matrix_positions (current_matrix,
12034 first_unchanged_at_end_vpos + dvpos,
12035 bottom_vpos, delta, delta_bytes);
12036
12037 /* Adjust Y positions. */
12038 if (dy)
12039 shift_glyph_matrix (w, current_matrix,
12040 first_unchanged_at_end_vpos + dvpos,
12041 bottom_vpos, dy);
12042
12043 if (first_unchanged_at_end_row)
12044 first_unchanged_at_end_row += dvpos;
12045
12046 /* If scrolling up, there may be some lines to display at the end of
12047 the window. */
12048 last_text_row_at_end = NULL;
12049 if (dy < 0)
12050 {
12051 /* Scrolling up can leave for example a partially visible line
12052 at the end of the window to be redisplayed. */
12053 /* Set last_row to the glyph row in the current matrix where the
12054 window end line is found. It has been moved up or down in
12055 the matrix by dvpos. */
12056 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
12057 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
12058
12059 /* If last_row is the window end line, it should display text. */
12060 xassert (last_row->displays_text_p);
12061
12062 /* If window end line was partially visible before, begin
12063 displaying at that line. Otherwise begin displaying with the
12064 line following it. */
12065 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
12066 {
12067 init_to_row_start (&it, w, last_row);
12068 it.vpos = last_vpos;
12069 it.current_y = last_row->y;
12070 }
12071 else
12072 {
12073 init_to_row_end (&it, w, last_row);
12074 it.vpos = 1 + last_vpos;
12075 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
12076 ++last_row;
12077 }
12078
12079 /* We may start in a continuation line. If so, we have to
12080 get the right continuation_lines_width and current_x. */
12081 it.continuation_lines_width = last_row->continuation_lines_width;
12082 it.hpos = it.current_x = 0;
12083
12084 /* Display the rest of the lines at the window end. */
12085 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
12086 while (it.current_y < it.last_visible_y
12087 && !fonts_changed_p)
12088 {
12089 /* Is it always sure that the display agrees with lines in
12090 the current matrix? I don't think so, so we mark rows
12091 displayed invalid in the current matrix by setting their
12092 enabled_p flag to zero. */
12093 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
12094 if (display_line (&it))
12095 last_text_row_at_end = it.glyph_row - 1;
12096 }
12097 }
12098
12099 /* Update window_end_pos and window_end_vpos. */
12100 if (first_unchanged_at_end_row
12101 && first_unchanged_at_end_row->y < it.last_visible_y
12102 && !last_text_row_at_end)
12103 {
12104 /* Window end line if one of the preserved rows from the current
12105 matrix. Set row to the last row displaying text in current
12106 matrix starting at first_unchanged_at_end_row, after
12107 scrolling. */
12108 xassert (first_unchanged_at_end_row->displays_text_p);
12109 row = find_last_row_displaying_text (w->current_matrix, &it,
12110 first_unchanged_at_end_row);
12111 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
12112
12113 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12114 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12115 w->window_end_vpos
12116 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
12117 xassert (w->window_end_bytepos >= 0);
12118 IF_DEBUG (debug_method_add (w, "A"));
12119 }
12120 else if (last_text_row_at_end)
12121 {
12122 w->window_end_pos
12123 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
12124 w->window_end_bytepos
12125 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
12126 w->window_end_vpos
12127 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
12128 xassert (w->window_end_bytepos >= 0);
12129 IF_DEBUG (debug_method_add (w, "B"));
12130 }
12131 else if (last_text_row)
12132 {
12133 /* We have displayed either to the end of the window or at the
12134 end of the window, i.e. the last row with text is to be found
12135 in the desired matrix. */
12136 w->window_end_pos
12137 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12138 w->window_end_bytepos
12139 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12140 w->window_end_vpos
12141 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
12142 xassert (w->window_end_bytepos >= 0);
12143 }
12144 else if (first_unchanged_at_end_row == NULL
12145 && last_text_row == NULL
12146 && last_text_row_at_end == NULL)
12147 {
12148 /* Displayed to end of window, but no line containing text was
12149 displayed. Lines were deleted at the end of the window. */
12150 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12151 int vpos = XFASTINT (w->window_end_vpos);
12152 struct glyph_row *current_row = current_matrix->rows + vpos;
12153 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12154
12155 for (row = NULL;
12156 row == NULL && vpos >= first_vpos;
12157 --vpos, --current_row, --desired_row)
12158 {
12159 if (desired_row->enabled_p)
12160 {
12161 if (desired_row->displays_text_p)
12162 row = desired_row;
12163 }
12164 else if (current_row->displays_text_p)
12165 row = current_row;
12166 }
12167
12168 xassert (row != NULL);
12169 w->window_end_vpos = make_number (vpos + 1);
12170 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12171 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12172 xassert (w->window_end_bytepos >= 0);
12173 IF_DEBUG (debug_method_add (w, "C"));
12174 }
12175 else
12176 abort ();
12177
12178 #if 0 /* This leads to problems, for instance when the cursor is
12179 at ZV, and the cursor line displays no text. */
12180 /* Disable rows below what's displayed in the window. This makes
12181 debugging easier. */
12182 enable_glyph_matrix_rows (current_matrix,
12183 XFASTINT (w->window_end_vpos) + 1,
12184 bottom_vpos, 0);
12185 #endif
12186
12187 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12188 debug_end_vpos = XFASTINT (w->window_end_vpos));
12189
12190 /* Record that display has not been completed. */
12191 w->window_end_valid = Qnil;
12192 w->desired_matrix->no_scrolling_p = 1;
12193 return 3;
12194
12195 #undef GIVE_UP
12196 }
12197
12198
12199 \f
12200 /***********************************************************************
12201 More debugging support
12202 ***********************************************************************/
12203
12204 #if GLYPH_DEBUG
12205
12206 void dump_glyph_row P_ ((struct glyph_row *, int, int));
12207 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
12208 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
12209
12210
12211 /* Dump the contents of glyph matrix MATRIX on stderr.
12212
12213 GLYPHS 0 means don't show glyph contents.
12214 GLYPHS 1 means show glyphs in short form
12215 GLYPHS > 1 means show glyphs in long form. */
12216
12217 void
12218 dump_glyph_matrix (matrix, glyphs)
12219 struct glyph_matrix *matrix;
12220 int glyphs;
12221 {
12222 int i;
12223 for (i = 0; i < matrix->nrows; ++i)
12224 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
12225 }
12226
12227
12228 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12229 the glyph row and area where the glyph comes from. */
12230
12231 void
12232 dump_glyph (row, glyph, area)
12233 struct glyph_row *row;
12234 struct glyph *glyph;
12235 int area;
12236 {
12237 if (glyph->type == CHAR_GLYPH)
12238 {
12239 fprintf (stderr,
12240 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12241 glyph - row->glyphs[TEXT_AREA],
12242 'C',
12243 glyph->charpos,
12244 (BUFFERP (glyph->object)
12245 ? 'B'
12246 : (STRINGP (glyph->object)
12247 ? 'S'
12248 : '-')),
12249 glyph->pixel_width,
12250 glyph->u.ch,
12251 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12252 ? glyph->u.ch
12253 : '.'),
12254 glyph->face_id,
12255 glyph->left_box_line_p,
12256 glyph->right_box_line_p);
12257 }
12258 else if (glyph->type == STRETCH_GLYPH)
12259 {
12260 fprintf (stderr,
12261 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12262 glyph - row->glyphs[TEXT_AREA],
12263 'S',
12264 glyph->charpos,
12265 (BUFFERP (glyph->object)
12266 ? 'B'
12267 : (STRINGP (glyph->object)
12268 ? 'S'
12269 : '-')),
12270 glyph->pixel_width,
12271 0,
12272 '.',
12273 glyph->face_id,
12274 glyph->left_box_line_p,
12275 glyph->right_box_line_p);
12276 }
12277 else if (glyph->type == IMAGE_GLYPH)
12278 {
12279 fprintf (stderr,
12280 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12281 glyph - row->glyphs[TEXT_AREA],
12282 'I',
12283 glyph->charpos,
12284 (BUFFERP (glyph->object)
12285 ? 'B'
12286 : (STRINGP (glyph->object)
12287 ? 'S'
12288 : '-')),
12289 glyph->pixel_width,
12290 glyph->u.img_id,
12291 '.',
12292 glyph->face_id,
12293 glyph->left_box_line_p,
12294 glyph->right_box_line_p);
12295 }
12296 }
12297
12298
12299 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12300 GLYPHS 0 means don't show glyph contents.
12301 GLYPHS 1 means show glyphs in short form
12302 GLYPHS > 1 means show glyphs in long form. */
12303
12304 void
12305 dump_glyph_row (row, vpos, glyphs)
12306 struct glyph_row *row;
12307 int vpos, glyphs;
12308 {
12309 if (glyphs != 1)
12310 {
12311 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12312 fprintf (stderr, "=======================================================================\n");
12313
12314 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
12315 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12316 vpos,
12317 MATRIX_ROW_START_CHARPOS (row),
12318 MATRIX_ROW_END_CHARPOS (row),
12319 row->used[TEXT_AREA],
12320 row->contains_overlapping_glyphs_p,
12321 row->enabled_p,
12322 row->truncated_on_left_p,
12323 row->truncated_on_right_p,
12324 row->overlay_arrow_p,
12325 row->continued_p,
12326 MATRIX_ROW_CONTINUATION_LINE_P (row),
12327 row->displays_text_p,
12328 row->ends_at_zv_p,
12329 row->fill_line_p,
12330 row->ends_in_middle_of_char_p,
12331 row->starts_in_middle_of_char_p,
12332 row->mouse_face_p,
12333 row->x,
12334 row->y,
12335 row->pixel_width,
12336 row->height,
12337 row->visible_height,
12338 row->ascent,
12339 row->phys_ascent);
12340 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12341 row->end.overlay_string_index,
12342 row->continuation_lines_width);
12343 fprintf (stderr, "%9d %5d\n",
12344 CHARPOS (row->start.string_pos),
12345 CHARPOS (row->end.string_pos));
12346 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12347 row->end.dpvec_index);
12348 }
12349
12350 if (glyphs > 1)
12351 {
12352 int area;
12353
12354 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12355 {
12356 struct glyph *glyph = row->glyphs[area];
12357 struct glyph *glyph_end = glyph + row->used[area];
12358
12359 /* Glyph for a line end in text. */
12360 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12361 ++glyph_end;
12362
12363 if (glyph < glyph_end)
12364 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12365
12366 for (; glyph < glyph_end; ++glyph)
12367 dump_glyph (row, glyph, area);
12368 }
12369 }
12370 else if (glyphs == 1)
12371 {
12372 int area;
12373
12374 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12375 {
12376 char *s = (char *) alloca (row->used[area] + 1);
12377 int i;
12378
12379 for (i = 0; i < row->used[area]; ++i)
12380 {
12381 struct glyph *glyph = row->glyphs[area] + i;
12382 if (glyph->type == CHAR_GLYPH
12383 && glyph->u.ch < 0x80
12384 && glyph->u.ch >= ' ')
12385 s[i] = glyph->u.ch;
12386 else
12387 s[i] = '.';
12388 }
12389
12390 s[i] = '\0';
12391 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12392 }
12393 }
12394 }
12395
12396
12397 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12398 Sdump_glyph_matrix, 0, 1, "p",
12399 doc: /* Dump the current matrix of the selected window to stderr.
12400 Shows contents of glyph row structures. With non-nil
12401 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12402 glyphs in short form, otherwise show glyphs in long form. */)
12403 (glyphs)
12404 Lisp_Object glyphs;
12405 {
12406 struct window *w = XWINDOW (selected_window);
12407 struct buffer *buffer = XBUFFER (w->buffer);
12408
12409 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12410 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12411 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12412 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12413 fprintf (stderr, "=============================================\n");
12414 dump_glyph_matrix (w->current_matrix,
12415 NILP (glyphs) ? 0 : XINT (glyphs));
12416 return Qnil;
12417 }
12418
12419
12420 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
12421 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
12422 ()
12423 {
12424 struct frame *f = XFRAME (selected_frame);
12425 dump_glyph_matrix (f->current_matrix, 1);
12426 return Qnil;
12427 }
12428
12429
12430 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12431 doc: /* Dump glyph row ROW to stderr.
12432 GLYPH 0 means don't dump glyphs.
12433 GLYPH 1 means dump glyphs in short form.
12434 GLYPH > 1 or omitted means dump glyphs in long form. */)
12435 (row, glyphs)
12436 Lisp_Object row, glyphs;
12437 {
12438 struct glyph_matrix *matrix;
12439 int vpos;
12440
12441 CHECK_NUMBER (row);
12442 matrix = XWINDOW (selected_window)->current_matrix;
12443 vpos = XINT (row);
12444 if (vpos >= 0 && vpos < matrix->nrows)
12445 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12446 vpos,
12447 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12448 return Qnil;
12449 }
12450
12451
12452 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12453 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12454 GLYPH 0 means don't dump glyphs.
12455 GLYPH 1 means dump glyphs in short form.
12456 GLYPH > 1 or omitted means dump glyphs in long form. */)
12457 (row, glyphs)
12458 Lisp_Object row, glyphs;
12459 {
12460 struct frame *sf = SELECTED_FRAME ();
12461 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12462 int vpos;
12463
12464 CHECK_NUMBER (row);
12465 vpos = XINT (row);
12466 if (vpos >= 0 && vpos < m->nrows)
12467 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12468 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12469 return Qnil;
12470 }
12471
12472
12473 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12474 doc: /* Toggle tracing of redisplay.
12475 With ARG, turn tracing on if and only if ARG is positive. */)
12476 (arg)
12477 Lisp_Object arg;
12478 {
12479 if (NILP (arg))
12480 trace_redisplay_p = !trace_redisplay_p;
12481 else
12482 {
12483 arg = Fprefix_numeric_value (arg);
12484 trace_redisplay_p = XINT (arg) > 0;
12485 }
12486
12487 return Qnil;
12488 }
12489
12490
12491 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12492 doc: /* Like `format', but print result to stderr.
12493 usage: (trace-to-stderr STRING &rest OBJECTS) */)
12494 (nargs, args)
12495 int nargs;
12496 Lisp_Object *args;
12497 {
12498 Lisp_Object s = Fformat (nargs, args);
12499 fprintf (stderr, "%s", SDATA (s));
12500 return Qnil;
12501 }
12502
12503 #endif /* GLYPH_DEBUG */
12504
12505
12506 \f
12507 /***********************************************************************
12508 Building Desired Matrix Rows
12509 ***********************************************************************/
12510
12511 /* Return a temporary glyph row holding the glyphs of an overlay
12512 arrow. Only used for non-window-redisplay windows. */
12513
12514 static struct glyph_row *
12515 get_overlay_arrow_glyph_row (w)
12516 struct window *w;
12517 {
12518 struct frame *f = XFRAME (WINDOW_FRAME (w));
12519 struct buffer *buffer = XBUFFER (w->buffer);
12520 struct buffer *old = current_buffer;
12521 const unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
12522 int arrow_len = SCHARS (Voverlay_arrow_string);
12523 const unsigned char *arrow_end = arrow_string + arrow_len;
12524 const unsigned char *p;
12525 struct it it;
12526 int multibyte_p;
12527 int n_glyphs_before;
12528
12529 set_buffer_temp (buffer);
12530 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12531 it.glyph_row->used[TEXT_AREA] = 0;
12532 SET_TEXT_POS (it.position, 0, 0);
12533
12534 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12535 p = arrow_string;
12536 while (p < arrow_end)
12537 {
12538 Lisp_Object face, ilisp;
12539
12540 /* Get the next character. */
12541 if (multibyte_p)
12542 it.c = string_char_and_length (p, arrow_len, &it.len);
12543 else
12544 it.c = *p, it.len = 1;
12545 p += it.len;
12546
12547 /* Get its face. */
12548 ilisp = make_number (p - arrow_string);
12549 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12550 it.face_id = compute_char_face (f, it.c, face);
12551
12552 /* Compute its width, get its glyphs. */
12553 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12554 SET_TEXT_POS (it.position, -1, -1);
12555 PRODUCE_GLYPHS (&it);
12556
12557 /* If this character doesn't fit any more in the line, we have
12558 to remove some glyphs. */
12559 if (it.current_x > it.last_visible_x)
12560 {
12561 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12562 break;
12563 }
12564 }
12565
12566 set_buffer_temp (old);
12567 return it.glyph_row;
12568 }
12569
12570
12571 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12572 glyphs are only inserted for terminal frames since we can't really
12573 win with truncation glyphs when partially visible glyphs are
12574 involved. Which glyphs to insert is determined by
12575 produce_special_glyphs. */
12576
12577 static void
12578 insert_left_trunc_glyphs (it)
12579 struct it *it;
12580 {
12581 struct it truncate_it;
12582 struct glyph *from, *end, *to, *toend;
12583
12584 xassert (!FRAME_WINDOW_P (it->f));
12585
12586 /* Get the truncation glyphs. */
12587 truncate_it = *it;
12588 truncate_it.current_x = 0;
12589 truncate_it.face_id = DEFAULT_FACE_ID;
12590 truncate_it.glyph_row = &scratch_glyph_row;
12591 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12592 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12593 truncate_it.object = make_number (0);
12594 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12595
12596 /* Overwrite glyphs from IT with truncation glyphs. */
12597 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12598 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12599 to = it->glyph_row->glyphs[TEXT_AREA];
12600 toend = to + it->glyph_row->used[TEXT_AREA];
12601
12602 while (from < end)
12603 *to++ = *from++;
12604
12605 /* There may be padding glyphs left over. Overwrite them too. */
12606 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12607 {
12608 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12609 while (from < end)
12610 *to++ = *from++;
12611 }
12612
12613 if (to > toend)
12614 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12615 }
12616
12617
12618 /* Compute the pixel height and width of IT->glyph_row.
12619
12620 Most of the time, ascent and height of a display line will be equal
12621 to the max_ascent and max_height values of the display iterator
12622 structure. This is not the case if
12623
12624 1. We hit ZV without displaying anything. In this case, max_ascent
12625 and max_height will be zero.
12626
12627 2. We have some glyphs that don't contribute to the line height.
12628 (The glyph row flag contributes_to_line_height_p is for future
12629 pixmap extensions).
12630
12631 The first case is easily covered by using default values because in
12632 these cases, the line height does not really matter, except that it
12633 must not be zero. */
12634
12635 static void
12636 compute_line_metrics (it)
12637 struct it *it;
12638 {
12639 struct glyph_row *row = it->glyph_row;
12640 int area, i;
12641
12642 if (FRAME_WINDOW_P (it->f))
12643 {
12644 int i, min_y, max_y;
12645
12646 /* The line may consist of one space only, that was added to
12647 place the cursor on it. If so, the row's height hasn't been
12648 computed yet. */
12649 if (row->height == 0)
12650 {
12651 if (it->max_ascent + it->max_descent == 0)
12652 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12653 row->ascent = it->max_ascent;
12654 row->height = it->max_ascent + it->max_descent;
12655 row->phys_ascent = it->max_phys_ascent;
12656 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12657 }
12658
12659 /* Compute the width of this line. */
12660 row->pixel_width = row->x;
12661 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12662 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12663
12664 xassert (row->pixel_width >= 0);
12665 xassert (row->ascent >= 0 && row->height > 0);
12666
12667 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12668 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12669
12670 /* If first line's physical ascent is larger than its logical
12671 ascent, use the physical ascent, and make the row taller.
12672 This makes accented characters fully visible. */
12673 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12674 && row->phys_ascent > row->ascent)
12675 {
12676 row->height += row->phys_ascent - row->ascent;
12677 row->ascent = row->phys_ascent;
12678 }
12679
12680 /* Compute how much of the line is visible. */
12681 row->visible_height = row->height;
12682
12683 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12684 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12685
12686 if (row->y < min_y)
12687 row->visible_height -= min_y - row->y;
12688 if (row->y + row->height > max_y)
12689 row->visible_height -= row->y + row->height - max_y;
12690 }
12691 else
12692 {
12693 row->pixel_width = row->used[TEXT_AREA];
12694 if (row->continued_p)
12695 row->pixel_width -= it->continuation_pixel_width;
12696 else if (row->truncated_on_right_p)
12697 row->pixel_width -= it->truncation_pixel_width;
12698 row->ascent = row->phys_ascent = 0;
12699 row->height = row->phys_height = row->visible_height = 1;
12700 }
12701
12702 /* Compute a hash code for this row. */
12703 row->hash = 0;
12704 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12705 for (i = 0; i < row->used[area]; ++i)
12706 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12707 + row->glyphs[area][i].u.val
12708 + row->glyphs[area][i].face_id
12709 + row->glyphs[area][i].padding_p
12710 + (row->glyphs[area][i].type << 2));
12711
12712 it->max_ascent = it->max_descent = 0;
12713 it->max_phys_ascent = it->max_phys_descent = 0;
12714 }
12715
12716
12717 /* Append one space to the glyph row of iterator IT if doing a
12718 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12719 space have the default face, otherwise let it have the same face as
12720 IT->face_id. Value is non-zero if a space was added.
12721
12722 This function is called to make sure that there is always one glyph
12723 at the end of a glyph row that the cursor can be set on under
12724 window-systems. (If there weren't such a glyph we would not know
12725 how wide and tall a box cursor should be displayed).
12726
12727 At the same time this space let's a nicely handle clearing to the
12728 end of the line if the row ends in italic text. */
12729
12730 static int
12731 append_space (it, default_face_p)
12732 struct it *it;
12733 int default_face_p;
12734 {
12735 if (FRAME_WINDOW_P (it->f))
12736 {
12737 int n = it->glyph_row->used[TEXT_AREA];
12738
12739 if (it->glyph_row->glyphs[TEXT_AREA] + n
12740 < it->glyph_row->glyphs[1 + TEXT_AREA])
12741 {
12742 /* Save some values that must not be changed.
12743 Must save IT->c and IT->len because otherwise
12744 ITERATOR_AT_END_P wouldn't work anymore after
12745 append_space has been called. */
12746 enum display_element_type saved_what = it->what;
12747 int saved_c = it->c, saved_len = it->len;
12748 int saved_x = it->current_x;
12749 int saved_face_id = it->face_id;
12750 struct text_pos saved_pos;
12751 Lisp_Object saved_object;
12752 struct face *face;
12753
12754 saved_object = it->object;
12755 saved_pos = it->position;
12756
12757 it->what = IT_CHARACTER;
12758 bzero (&it->position, sizeof it->position);
12759 it->object = make_number (0);
12760 it->c = ' ';
12761 it->len = 1;
12762
12763 if (default_face_p)
12764 it->face_id = DEFAULT_FACE_ID;
12765 else if (it->face_before_selective_p)
12766 it->face_id = it->saved_face_id;
12767 face = FACE_FROM_ID (it->f, it->face_id);
12768 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12769
12770 PRODUCE_GLYPHS (it);
12771
12772 it->current_x = saved_x;
12773 it->object = saved_object;
12774 it->position = saved_pos;
12775 it->what = saved_what;
12776 it->face_id = saved_face_id;
12777 it->len = saved_len;
12778 it->c = saved_c;
12779 return 1;
12780 }
12781 }
12782
12783 return 0;
12784 }
12785
12786
12787 /* Extend the face of the last glyph in the text area of IT->glyph_row
12788 to the end of the display line. Called from display_line.
12789 If the glyph row is empty, add a space glyph to it so that we
12790 know the face to draw. Set the glyph row flag fill_line_p. */
12791
12792 static void
12793 extend_face_to_end_of_line (it)
12794 struct it *it;
12795 {
12796 struct face *face;
12797 struct frame *f = it->f;
12798
12799 /* If line is already filled, do nothing. */
12800 if (it->current_x >= it->last_visible_x)
12801 return;
12802
12803 /* Face extension extends the background and box of IT->face_id
12804 to the end of the line. If the background equals the background
12805 of the frame, we don't have to do anything. */
12806 if (it->face_before_selective_p)
12807 face = FACE_FROM_ID (it->f, it->saved_face_id);
12808 else
12809 face = FACE_FROM_ID (f, it->face_id);
12810
12811 if (FRAME_WINDOW_P (f)
12812 && face->box == FACE_NO_BOX
12813 && face->background == FRAME_BACKGROUND_PIXEL (f)
12814 && !face->stipple)
12815 return;
12816
12817 /* Set the glyph row flag indicating that the face of the last glyph
12818 in the text area has to be drawn to the end of the text area. */
12819 it->glyph_row->fill_line_p = 1;
12820
12821 /* If current character of IT is not ASCII, make sure we have the
12822 ASCII face. This will be automatically undone the next time
12823 get_next_display_element returns a multibyte character. Note
12824 that the character will always be single byte in unibyte text. */
12825 if (!SINGLE_BYTE_CHAR_P (it->c))
12826 {
12827 it->face_id = FACE_FOR_CHAR (f, face, 0);
12828 }
12829
12830 if (FRAME_WINDOW_P (f))
12831 {
12832 /* If the row is empty, add a space with the current face of IT,
12833 so that we know which face to draw. */
12834 if (it->glyph_row->used[TEXT_AREA] == 0)
12835 {
12836 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12837 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12838 it->glyph_row->used[TEXT_AREA] = 1;
12839 }
12840 }
12841 else
12842 {
12843 /* Save some values that must not be changed. */
12844 int saved_x = it->current_x;
12845 struct text_pos saved_pos;
12846 Lisp_Object saved_object;
12847 enum display_element_type saved_what = it->what;
12848 int saved_face_id = it->face_id;
12849
12850 saved_object = it->object;
12851 saved_pos = it->position;
12852
12853 it->what = IT_CHARACTER;
12854 bzero (&it->position, sizeof it->position);
12855 it->object = make_number (0);
12856 it->c = ' ';
12857 it->len = 1;
12858 it->face_id = face->id;
12859
12860 PRODUCE_GLYPHS (it);
12861
12862 while (it->current_x <= it->last_visible_x)
12863 PRODUCE_GLYPHS (it);
12864
12865 /* Don't count these blanks really. It would let us insert a left
12866 truncation glyph below and make us set the cursor on them, maybe. */
12867 it->current_x = saved_x;
12868 it->object = saved_object;
12869 it->position = saved_pos;
12870 it->what = saved_what;
12871 it->face_id = saved_face_id;
12872 }
12873 }
12874
12875
12876 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12877 trailing whitespace. */
12878
12879 static int
12880 trailing_whitespace_p (charpos)
12881 int charpos;
12882 {
12883 int bytepos = CHAR_TO_BYTE (charpos);
12884 int c = 0;
12885
12886 while (bytepos < ZV_BYTE
12887 && (c = FETCH_CHAR (bytepos),
12888 c == ' ' || c == '\t'))
12889 ++bytepos;
12890
12891 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12892 {
12893 if (bytepos != PT_BYTE)
12894 return 1;
12895 }
12896 return 0;
12897 }
12898
12899
12900 /* Highlight trailing whitespace, if any, in ROW. */
12901
12902 void
12903 highlight_trailing_whitespace (f, row)
12904 struct frame *f;
12905 struct glyph_row *row;
12906 {
12907 int used = row->used[TEXT_AREA];
12908
12909 if (used)
12910 {
12911 struct glyph *start = row->glyphs[TEXT_AREA];
12912 struct glyph *glyph = start + used - 1;
12913
12914 /* Skip over glyphs inserted to display the cursor at the
12915 end of a line, for extending the face of the last glyph
12916 to the end of the line on terminals, and for truncation
12917 and continuation glyphs. */
12918 while (glyph >= start
12919 && glyph->type == CHAR_GLYPH
12920 && INTEGERP (glyph->object))
12921 --glyph;
12922
12923 /* If last glyph is a space or stretch, and it's trailing
12924 whitespace, set the face of all trailing whitespace glyphs in
12925 IT->glyph_row to `trailing-whitespace'. */
12926 if (glyph >= start
12927 && BUFFERP (glyph->object)
12928 && (glyph->type == STRETCH_GLYPH
12929 || (glyph->type == CHAR_GLYPH
12930 && glyph->u.ch == ' '))
12931 && trailing_whitespace_p (glyph->charpos))
12932 {
12933 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12934
12935 while (glyph >= start
12936 && BUFFERP (glyph->object)
12937 && (glyph->type == STRETCH_GLYPH
12938 || (glyph->type == CHAR_GLYPH
12939 && glyph->u.ch == ' ')))
12940 (glyph--)->face_id = face_id;
12941 }
12942 }
12943 }
12944
12945
12946 /* Value is non-zero if glyph row ROW in window W should be
12947 used to hold the cursor. */
12948
12949 static int
12950 cursor_row_p (w, row)
12951 struct window *w;
12952 struct glyph_row *row;
12953 {
12954 int cursor_row_p = 1;
12955
12956 if (PT == MATRIX_ROW_END_CHARPOS (row))
12957 {
12958 /* If the row ends with a newline from a string, we don't want
12959 the cursor there (if the row is continued it doesn't end in a
12960 newline). */
12961 if (CHARPOS (row->end.string_pos) >= 0
12962 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12963 cursor_row_p = row->continued_p;
12964
12965 /* If the row ends at ZV, display the cursor at the end of that
12966 row instead of at the start of the row below. */
12967 else if (row->ends_at_zv_p)
12968 cursor_row_p = 1;
12969 else
12970 cursor_row_p = 0;
12971 }
12972
12973 return cursor_row_p;
12974 }
12975
12976
12977 /* Construct the glyph row IT->glyph_row in the desired matrix of
12978 IT->w from text at the current position of IT. See dispextern.h
12979 for an overview of struct it. Value is non-zero if
12980 IT->glyph_row displays text, as opposed to a line displaying ZV
12981 only. */
12982
12983 static int
12984 display_line (it)
12985 struct it *it;
12986 {
12987 struct glyph_row *row = it->glyph_row;
12988
12989 /* We always start displaying at hpos zero even if hscrolled. */
12990 xassert (it->hpos == 0 && it->current_x == 0);
12991
12992 /* We must not display in a row that's not a text row. */
12993 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12994 < it->w->desired_matrix->nrows);
12995
12996 /* Is IT->w showing the region? */
12997 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12998
12999 /* Clear the result glyph row and enable it. */
13000 prepare_desired_row (row);
13001
13002 row->y = it->current_y;
13003 row->start = it->current;
13004 row->continuation_lines_width = it->continuation_lines_width;
13005 row->displays_text_p = 1;
13006 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
13007 it->starts_in_middle_of_char_p = 0;
13008
13009 /* Arrange the overlays nicely for our purposes. Usually, we call
13010 display_line on only one line at a time, in which case this
13011 can't really hurt too much, or we call it on lines which appear
13012 one after another in the buffer, in which case all calls to
13013 recenter_overlay_lists but the first will be pretty cheap. */
13014 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
13015
13016 /* Move over display elements that are not visible because we are
13017 hscrolled. This may stop at an x-position < IT->first_visible_x
13018 if the first glyph is partially visible or if we hit a line end. */
13019 if (it->current_x < it->first_visible_x)
13020 move_it_in_display_line_to (it, ZV, it->first_visible_x,
13021 MOVE_TO_POS | MOVE_TO_X);
13022
13023 /* Get the initial row height. This is either the height of the
13024 text hscrolled, if there is any, or zero. */
13025 row->ascent = it->max_ascent;
13026 row->height = it->max_ascent + it->max_descent;
13027 row->phys_ascent = it->max_phys_ascent;
13028 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13029
13030 /* Loop generating characters. The loop is left with IT on the next
13031 character to display. */
13032 while (1)
13033 {
13034 int n_glyphs_before, hpos_before, x_before;
13035 int x, i, nglyphs;
13036 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
13037
13038 /* Retrieve the next thing to display. Value is zero if end of
13039 buffer reached. */
13040 if (!get_next_display_element (it))
13041 {
13042 /* Maybe add a space at the end of this line that is used to
13043 display the cursor there under X. Set the charpos of the
13044 first glyph of blank lines not corresponding to any text
13045 to -1. */
13046 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
13047 || row->used[TEXT_AREA] == 0)
13048 {
13049 row->glyphs[TEXT_AREA]->charpos = -1;
13050 row->displays_text_p = 0;
13051
13052 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
13053 && (!MINI_WINDOW_P (it->w)
13054 || (minibuf_level && EQ (it->window, minibuf_window))))
13055 row->indicate_empty_line_p = 1;
13056 }
13057
13058 it->continuation_lines_width = 0;
13059 row->ends_at_zv_p = 1;
13060 break;
13061 }
13062
13063 /* Now, get the metrics of what we want to display. This also
13064 generates glyphs in `row' (which is IT->glyph_row). */
13065 n_glyphs_before = row->used[TEXT_AREA];
13066 x = it->current_x;
13067
13068 /* Remember the line height so far in case the next element doesn't
13069 fit on the line. */
13070 if (!it->truncate_lines_p)
13071 {
13072 ascent = it->max_ascent;
13073 descent = it->max_descent;
13074 phys_ascent = it->max_phys_ascent;
13075 phys_descent = it->max_phys_descent;
13076 }
13077
13078 PRODUCE_GLYPHS (it);
13079
13080 /* If this display element was in marginal areas, continue with
13081 the next one. */
13082 if (it->area != TEXT_AREA)
13083 {
13084 row->ascent = max (row->ascent, it->max_ascent);
13085 row->height = max (row->height, it->max_ascent + it->max_descent);
13086 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13087 row->phys_height = max (row->phys_height,
13088 it->max_phys_ascent + it->max_phys_descent);
13089 set_iterator_to_next (it, 1);
13090 continue;
13091 }
13092
13093 /* Does the display element fit on the line? If we truncate
13094 lines, we should draw past the right edge of the window. If
13095 we don't truncate, we want to stop so that we can display the
13096 continuation glyph before the right margin. If lines are
13097 continued, there are two possible strategies for characters
13098 resulting in more than 1 glyph (e.g. tabs): Display as many
13099 glyphs as possible in this line and leave the rest for the
13100 continuation line, or display the whole element in the next
13101 line. Original redisplay did the former, so we do it also. */
13102 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
13103 hpos_before = it->hpos;
13104 x_before = x;
13105
13106 if (/* Not a newline. */
13107 nglyphs > 0
13108 /* Glyphs produced fit entirely in the line. */
13109 && it->current_x < it->last_visible_x)
13110 {
13111 it->hpos += nglyphs;
13112 row->ascent = max (row->ascent, it->max_ascent);
13113 row->height = max (row->height, it->max_ascent + it->max_descent);
13114 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13115 row->phys_height = max (row->phys_height,
13116 it->max_phys_ascent + it->max_phys_descent);
13117 if (it->current_x - it->pixel_width < it->first_visible_x)
13118 row->x = x - it->first_visible_x;
13119 }
13120 else
13121 {
13122 int new_x;
13123 struct glyph *glyph;
13124
13125 for (i = 0; i < nglyphs; ++i, x = new_x)
13126 {
13127 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13128 new_x = x + glyph->pixel_width;
13129
13130 if (/* Lines are continued. */
13131 !it->truncate_lines_p
13132 && (/* Glyph doesn't fit on the line. */
13133 new_x > it->last_visible_x
13134 /* Or it fits exactly on a window system frame. */
13135 || (new_x == it->last_visible_x
13136 && FRAME_WINDOW_P (it->f))))
13137 {
13138 /* End of a continued line. */
13139
13140 if (it->hpos == 0
13141 || (new_x == it->last_visible_x
13142 && FRAME_WINDOW_P (it->f)))
13143 {
13144 /* Current glyph is the only one on the line or
13145 fits exactly on the line. We must continue
13146 the line because we can't draw the cursor
13147 after the glyph. */
13148 row->continued_p = 1;
13149 it->current_x = new_x;
13150 it->continuation_lines_width += new_x;
13151 ++it->hpos;
13152 if (i == nglyphs - 1)
13153 set_iterator_to_next (it, 1);
13154 }
13155 else if (CHAR_GLYPH_PADDING_P (*glyph)
13156 && !FRAME_WINDOW_P (it->f))
13157 {
13158 /* A padding glyph that doesn't fit on this line.
13159 This means the whole character doesn't fit
13160 on the line. */
13161 row->used[TEXT_AREA] = n_glyphs_before;
13162
13163 /* Fill the rest of the row with continuation
13164 glyphs like in 20.x. */
13165 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13166 < row->glyphs[1 + TEXT_AREA])
13167 produce_special_glyphs (it, IT_CONTINUATION);
13168
13169 row->continued_p = 1;
13170 it->current_x = x_before;
13171 it->continuation_lines_width += x_before;
13172
13173 /* Restore the height to what it was before the
13174 element not fitting on the line. */
13175 it->max_ascent = ascent;
13176 it->max_descent = descent;
13177 it->max_phys_ascent = phys_ascent;
13178 it->max_phys_descent = phys_descent;
13179 }
13180 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13181 {
13182 /* A TAB that extends past the right edge of the
13183 window. This produces a single glyph on
13184 window system frames. We leave the glyph in
13185 this row and let it fill the row, but don't
13186 consume the TAB. */
13187 it->continuation_lines_width += it->last_visible_x;
13188 row->ends_in_middle_of_char_p = 1;
13189 row->continued_p = 1;
13190 glyph->pixel_width = it->last_visible_x - x;
13191 it->starts_in_middle_of_char_p = 1;
13192 }
13193 else
13194 {
13195 /* Something other than a TAB that draws past
13196 the right edge of the window. Restore
13197 positions to values before the element. */
13198 row->used[TEXT_AREA] = n_glyphs_before + i;
13199
13200 /* Display continuation glyphs. */
13201 if (!FRAME_WINDOW_P (it->f))
13202 produce_special_glyphs (it, IT_CONTINUATION);
13203 row->continued_p = 1;
13204
13205 it->continuation_lines_width += x;
13206
13207 if (nglyphs > 1 && i > 0)
13208 {
13209 row->ends_in_middle_of_char_p = 1;
13210 it->starts_in_middle_of_char_p = 1;
13211 }
13212
13213 /* Restore the height to what it was before the
13214 element not fitting on the line. */
13215 it->max_ascent = ascent;
13216 it->max_descent = descent;
13217 it->max_phys_ascent = phys_ascent;
13218 it->max_phys_descent = phys_descent;
13219 }
13220
13221 break;
13222 }
13223 else if (new_x > it->first_visible_x)
13224 {
13225 /* Increment number of glyphs actually displayed. */
13226 ++it->hpos;
13227
13228 if (x < it->first_visible_x)
13229 /* Glyph is partially visible, i.e. row starts at
13230 negative X position. */
13231 row->x = x - it->first_visible_x;
13232 }
13233 else
13234 {
13235 /* Glyph is completely off the left margin of the
13236 window. This should not happen because of the
13237 move_it_in_display_line at the start of this
13238 function, unless the text display area of the
13239 window is empty. */
13240 xassert (it->first_visible_x <= it->last_visible_x);
13241 }
13242 }
13243
13244 row->ascent = max (row->ascent, it->max_ascent);
13245 row->height = max (row->height, it->max_ascent + it->max_descent);
13246 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13247 row->phys_height = max (row->phys_height,
13248 it->max_phys_ascent + it->max_phys_descent);
13249
13250 /* End of this display line if row is continued. */
13251 if (row->continued_p)
13252 break;
13253 }
13254
13255 /* Is this a line end? If yes, we're also done, after making
13256 sure that a non-default face is extended up to the right
13257 margin of the window. */
13258 if (ITERATOR_AT_END_OF_LINE_P (it))
13259 {
13260 int used_before = row->used[TEXT_AREA];
13261
13262 row->ends_in_newline_from_string_p = STRINGP (it->object);
13263
13264 /* Add a space at the end of the line that is used to
13265 display the cursor there. */
13266 append_space (it, 0);
13267
13268 /* Extend the face to the end of the line. */
13269 extend_face_to_end_of_line (it);
13270
13271 /* Make sure we have the position. */
13272 if (used_before == 0)
13273 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13274
13275 /* Consume the line end. This skips over invisible lines. */
13276 set_iterator_to_next (it, 1);
13277 it->continuation_lines_width = 0;
13278 break;
13279 }
13280
13281 /* Proceed with next display element. Note that this skips
13282 over lines invisible because of selective display. */
13283 set_iterator_to_next (it, 1);
13284
13285 /* If we truncate lines, we are done when the last displayed
13286 glyphs reach past the right margin of the window. */
13287 if (it->truncate_lines_p
13288 && (FRAME_WINDOW_P (it->f)
13289 ? (it->current_x >= it->last_visible_x)
13290 : (it->current_x > it->last_visible_x)))
13291 {
13292 /* Maybe add truncation glyphs. */
13293 if (!FRAME_WINDOW_P (it->f))
13294 {
13295 int i, n;
13296
13297 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13298 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13299 break;
13300
13301 for (n = row->used[TEXT_AREA]; i < n; ++i)
13302 {
13303 row->used[TEXT_AREA] = i;
13304 produce_special_glyphs (it, IT_TRUNCATION);
13305 }
13306 }
13307
13308 row->truncated_on_right_p = 1;
13309 it->continuation_lines_width = 0;
13310 reseat_at_next_visible_line_start (it, 0);
13311 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13312 it->hpos = hpos_before;
13313 it->current_x = x_before;
13314 break;
13315 }
13316 }
13317
13318 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13319 at the left window margin. */
13320 if (it->first_visible_x
13321 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13322 {
13323 if (!FRAME_WINDOW_P (it->f))
13324 insert_left_trunc_glyphs (it);
13325 row->truncated_on_left_p = 1;
13326 }
13327
13328 /* If the start of this line is the overlay arrow-position, then
13329 mark this glyph row as the one containing the overlay arrow.
13330 This is clearly a mess with variable size fonts. It would be
13331 better to let it be displayed like cursors under X. */
13332 if (MARKERP (Voverlay_arrow_position)
13333 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13334 && (MATRIX_ROW_START_CHARPOS (row)
13335 == marker_position (Voverlay_arrow_position))
13336 && STRINGP (Voverlay_arrow_string)
13337 && ! overlay_arrow_seen)
13338 {
13339 /* Overlay arrow in window redisplay is a fringe bitmap. */
13340 if (!FRAME_WINDOW_P (it->f))
13341 {
13342 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13343 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13344 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13345 struct glyph *p = row->glyphs[TEXT_AREA];
13346 struct glyph *p2, *end;
13347
13348 /* Copy the arrow glyphs. */
13349 while (glyph < arrow_end)
13350 *p++ = *glyph++;
13351
13352 /* Throw away padding glyphs. */
13353 p2 = p;
13354 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13355 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13356 ++p2;
13357 if (p2 > p)
13358 {
13359 while (p2 < end)
13360 *p++ = *p2++;
13361 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13362 }
13363 }
13364
13365 overlay_arrow_seen = 1;
13366 row->overlay_arrow_p = 1;
13367 }
13368
13369 /* Compute pixel dimensions of this line. */
13370 compute_line_metrics (it);
13371
13372 /* Remember the position at which this line ends. */
13373 row->end = it->current;
13374
13375 /* Maybe set the cursor. */
13376 if (it->w->cursor.vpos < 0
13377 && PT >= MATRIX_ROW_START_CHARPOS (row)
13378 && PT <= MATRIX_ROW_END_CHARPOS (row)
13379 && cursor_row_p (it->w, row))
13380 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13381
13382 /* Highlight trailing whitespace. */
13383 if (!NILP (Vshow_trailing_whitespace))
13384 highlight_trailing_whitespace (it->f, it->glyph_row);
13385
13386 /* Prepare for the next line. This line starts horizontally at (X
13387 HPOS) = (0 0). Vertical positions are incremented. As a
13388 convenience for the caller, IT->glyph_row is set to the next
13389 row to be used. */
13390 it->current_x = it->hpos = 0;
13391 it->current_y += row->height;
13392 ++it->vpos;
13393 ++it->glyph_row;
13394 return row->displays_text_p;
13395 }
13396
13397
13398 \f
13399 /***********************************************************************
13400 Menu Bar
13401 ***********************************************************************/
13402
13403 /* Redisplay the menu bar in the frame for window W.
13404
13405 The menu bar of X frames that don't have X toolkit support is
13406 displayed in a special window W->frame->menu_bar_window.
13407
13408 The menu bar of terminal frames is treated specially as far as
13409 glyph matrices are concerned. Menu bar lines are not part of
13410 windows, so the update is done directly on the frame matrix rows
13411 for the menu bar. */
13412
13413 static void
13414 display_menu_bar (w)
13415 struct window *w;
13416 {
13417 struct frame *f = XFRAME (WINDOW_FRAME (w));
13418 struct it it;
13419 Lisp_Object items;
13420 int i;
13421
13422 /* Don't do all this for graphical frames. */
13423 #ifdef HAVE_NTGUI
13424 if (!NILP (Vwindow_system))
13425 return;
13426 #endif
13427 #ifdef USE_X_TOOLKIT
13428 if (FRAME_X_P (f))
13429 return;
13430 #endif
13431 #ifdef MAC_OS
13432 if (FRAME_MAC_P (f))
13433 return;
13434 #endif
13435
13436 #ifdef USE_X_TOOLKIT
13437 xassert (!FRAME_WINDOW_P (f));
13438 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13439 it.first_visible_x = 0;
13440 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13441 #else /* not USE_X_TOOLKIT */
13442 if (FRAME_WINDOW_P (f))
13443 {
13444 /* Menu bar lines are displayed in the desired matrix of the
13445 dummy window menu_bar_window. */
13446 struct window *menu_w;
13447 xassert (WINDOWP (f->menu_bar_window));
13448 menu_w = XWINDOW (f->menu_bar_window);
13449 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13450 MENU_FACE_ID);
13451 it.first_visible_x = 0;
13452 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13453 }
13454 else
13455 {
13456 /* This is a TTY frame, i.e. character hpos/vpos are used as
13457 pixel x/y. */
13458 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13459 MENU_FACE_ID);
13460 it.first_visible_x = 0;
13461 it.last_visible_x = FRAME_WIDTH (f);
13462 }
13463 #endif /* not USE_X_TOOLKIT */
13464
13465 if (! mode_line_inverse_video)
13466 /* Force the menu-bar to be displayed in the default face. */
13467 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13468
13469 /* Clear all rows of the menu bar. */
13470 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13471 {
13472 struct glyph_row *row = it.glyph_row + i;
13473 clear_glyph_row (row);
13474 row->enabled_p = 1;
13475 row->full_width_p = 1;
13476 }
13477
13478 /* Display all items of the menu bar. */
13479 items = FRAME_MENU_BAR_ITEMS (it.f);
13480 for (i = 0; i < XVECTOR (items)->size; i += 4)
13481 {
13482 Lisp_Object string;
13483
13484 /* Stop at nil string. */
13485 string = AREF (items, i + 1);
13486 if (NILP (string))
13487 break;
13488
13489 /* Remember where item was displayed. */
13490 AREF (items, i + 3) = make_number (it.hpos);
13491
13492 /* Display the item, pad with one space. */
13493 if (it.current_x < it.last_visible_x)
13494 display_string (NULL, string, Qnil, 0, 0, &it,
13495 SCHARS (string) + 1, 0, 0, -1);
13496 }
13497
13498 /* Fill out the line with spaces. */
13499 if (it.current_x < it.last_visible_x)
13500 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13501
13502 /* Compute the total height of the lines. */
13503 compute_line_metrics (&it);
13504 }
13505
13506
13507 \f
13508 /***********************************************************************
13509 Mode Line
13510 ***********************************************************************/
13511
13512 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13513 FORCE is non-zero, redisplay mode lines unconditionally.
13514 Otherwise, redisplay only mode lines that are garbaged. Value is
13515 the number of windows whose mode lines were redisplayed. */
13516
13517 static int
13518 redisplay_mode_lines (window, force)
13519 Lisp_Object window;
13520 int force;
13521 {
13522 int nwindows = 0;
13523
13524 while (!NILP (window))
13525 {
13526 struct window *w = XWINDOW (window);
13527
13528 if (WINDOWP (w->hchild))
13529 nwindows += redisplay_mode_lines (w->hchild, force);
13530 else if (WINDOWP (w->vchild))
13531 nwindows += redisplay_mode_lines (w->vchild, force);
13532 else if (force
13533 || FRAME_GARBAGED_P (XFRAME (w->frame))
13534 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13535 {
13536 struct text_pos lpoint;
13537 struct buffer *old = current_buffer;
13538
13539 /* Set the window's buffer for the mode line display. */
13540 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13541 set_buffer_internal_1 (XBUFFER (w->buffer));
13542
13543 /* Point refers normally to the selected window. For any
13544 other window, set up appropriate value. */
13545 if (!EQ (window, selected_window))
13546 {
13547 struct text_pos pt;
13548
13549 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13550 if (CHARPOS (pt) < BEGV)
13551 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13552 else if (CHARPOS (pt) > (ZV - 1))
13553 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13554 else
13555 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13556 }
13557
13558 /* Display mode lines. */
13559 clear_glyph_matrix (w->desired_matrix);
13560 if (display_mode_lines (w))
13561 {
13562 ++nwindows;
13563 w->must_be_updated_p = 1;
13564 }
13565
13566 /* Restore old settings. */
13567 set_buffer_internal_1 (old);
13568 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13569 }
13570
13571 window = w->next;
13572 }
13573
13574 return nwindows;
13575 }
13576
13577
13578 /* Display the mode and/or top line of window W. Value is the number
13579 of mode lines displayed. */
13580
13581 static int
13582 display_mode_lines (w)
13583 struct window *w;
13584 {
13585 Lisp_Object old_selected_window, old_selected_frame;
13586 int n = 0;
13587
13588 old_selected_frame = selected_frame;
13589 selected_frame = w->frame;
13590 old_selected_window = selected_window;
13591 XSETWINDOW (selected_window, w);
13592
13593 /* These will be set while the mode line specs are processed. */
13594 line_number_displayed = 0;
13595 w->column_number_displayed = Qnil;
13596
13597 if (WINDOW_WANTS_MODELINE_P (w))
13598 {
13599 struct window *sel_w = XWINDOW (old_selected_window);
13600
13601 /* Select mode line face based on the real selected window. */
13602 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
13603 current_buffer->mode_line_format);
13604 ++n;
13605 }
13606
13607 if (WINDOW_WANTS_HEADER_LINE_P (w))
13608 {
13609 display_mode_line (w, HEADER_LINE_FACE_ID,
13610 current_buffer->header_line_format);
13611 ++n;
13612 }
13613
13614 selected_frame = old_selected_frame;
13615 selected_window = old_selected_window;
13616 return n;
13617 }
13618
13619
13620 /* Display mode or top line of window W. FACE_ID specifies which line
13621 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13622 FORMAT is the mode line format to display. Value is the pixel
13623 height of the mode line displayed. */
13624
13625 static int
13626 display_mode_line (w, face_id, format)
13627 struct window *w;
13628 enum face_id face_id;
13629 Lisp_Object format;
13630 {
13631 struct it it;
13632 struct face *face;
13633
13634 init_iterator (&it, w, -1, -1, NULL, face_id);
13635 prepare_desired_row (it.glyph_row);
13636
13637 if (! mode_line_inverse_video)
13638 /* Force the mode-line to be displayed in the default face. */
13639 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13640
13641 /* Temporarily make frame's keyboard the current kboard so that
13642 kboard-local variables in the mode_line_format will get the right
13643 values. */
13644 push_frame_kboard (it.f);
13645 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
13646 pop_frame_kboard ();
13647
13648 /* Fill up with spaces. */
13649 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13650
13651 compute_line_metrics (&it);
13652 it.glyph_row->full_width_p = 1;
13653 it.glyph_row->mode_line_p = 1;
13654 it.glyph_row->continued_p = 0;
13655 it.glyph_row->truncated_on_left_p = 0;
13656 it.glyph_row->truncated_on_right_p = 0;
13657
13658 /* Make a 3D mode-line have a shadow at its right end. */
13659 face = FACE_FROM_ID (it.f, face_id);
13660 extend_face_to_end_of_line (&it);
13661 if (face->box != FACE_NO_BOX)
13662 {
13663 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13664 + it.glyph_row->used[TEXT_AREA] - 1);
13665 last->right_box_line_p = 1;
13666 }
13667
13668 return it.glyph_row->height;
13669 }
13670
13671 /* Alist that caches the results of :propertize.
13672 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
13673 Lisp_Object mode_line_proptrans_alist;
13674
13675 /* List of strings making up the mode-line. */
13676 Lisp_Object mode_line_string_list;
13677
13678 /* Base face property when building propertized mode line string. */
13679 static Lisp_Object mode_line_string_face;
13680 static Lisp_Object mode_line_string_face_prop;
13681
13682
13683 /* Contribute ELT to the mode line for window IT->w. How it
13684 translates into text depends on its data type.
13685
13686 IT describes the display environment in which we display, as usual.
13687
13688 DEPTH is the depth in recursion. It is used to prevent
13689 infinite recursion here.
13690
13691 FIELD_WIDTH is the number of characters the display of ELT should
13692 occupy in the mode line, and PRECISION is the maximum number of
13693 characters to display from ELT's representation. See
13694 display_string for details.
13695
13696 Returns the hpos of the end of the text generated by ELT.
13697
13698 PROPS is a property list to add to any string we encounter.
13699
13700 If RISKY is nonzero, remove (disregard) any properties in any string
13701 we encounter, and ignore :eval and :propertize.
13702
13703 If the global variable `frame_title_ptr' is non-NULL, then the output
13704 is passed to `store_frame_title' instead of `display_string'. */
13705
13706 static int
13707 display_mode_element (it, depth, field_width, precision, elt, props, risky)
13708 struct it *it;
13709 int depth;
13710 int field_width, precision;
13711 Lisp_Object elt, props;
13712 int risky;
13713 {
13714 int n = 0, field, prec;
13715 int literal = 0;
13716
13717 tail_recurse:
13718 if (depth > 10)
13719 goto invalid;
13720
13721 depth++;
13722
13723 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13724 {
13725 case Lisp_String:
13726 {
13727 /* A string: output it and check for %-constructs within it. */
13728 unsigned char c;
13729 const unsigned char *this, *lisp_string;
13730
13731 if (!NILP (props) || risky)
13732 {
13733 Lisp_Object oprops, aelt;
13734 oprops = Ftext_properties_at (make_number (0), elt);
13735
13736 if (NILP (Fequal (props, oprops)) || risky)
13737 {
13738 /* If the starting string has properties,
13739 merge the specified ones onto the existing ones. */
13740 if (! NILP (oprops) && !risky)
13741 {
13742 Lisp_Object tem;
13743
13744 oprops = Fcopy_sequence (oprops);
13745 tem = props;
13746 while (CONSP (tem))
13747 {
13748 oprops = Fplist_put (oprops, XCAR (tem),
13749 XCAR (XCDR (tem)));
13750 tem = XCDR (XCDR (tem));
13751 }
13752 props = oprops;
13753 }
13754
13755 aelt = Fassoc (elt, mode_line_proptrans_alist);
13756 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
13757 {
13758 mode_line_proptrans_alist
13759 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
13760 elt = XCAR (aelt);
13761 }
13762 else
13763 {
13764 Lisp_Object tem;
13765
13766 elt = Fcopy_sequence (elt);
13767 Fset_text_properties (make_number (0), Flength (elt),
13768 props, elt);
13769 /* Add this item to mode_line_proptrans_alist. */
13770 mode_line_proptrans_alist
13771 = Fcons (Fcons (elt, props),
13772 mode_line_proptrans_alist);
13773 /* Truncate mode_line_proptrans_alist
13774 to at most 50 elements. */
13775 tem = Fnthcdr (make_number (50),
13776 mode_line_proptrans_alist);
13777 if (! NILP (tem))
13778 XSETCDR (tem, Qnil);
13779 }
13780 }
13781 }
13782
13783 this = SDATA (elt);
13784 lisp_string = this;
13785
13786 if (literal)
13787 {
13788 prec = precision - n;
13789 if (frame_title_ptr)
13790 n += store_frame_title (SDATA (elt), -1, prec);
13791 else if (!NILP (mode_line_string_list))
13792 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
13793 else
13794 n += display_string (NULL, elt, Qnil, 0, 0, it,
13795 0, prec, 0, STRING_MULTIBYTE (elt));
13796
13797 break;
13798 }
13799
13800 while ((precision <= 0 || n < precision)
13801 && *this
13802 && (frame_title_ptr
13803 || !NILP (mode_line_string_list)
13804 || it->current_x < it->last_visible_x))
13805 {
13806 const unsigned char *last = this;
13807
13808 /* Advance to end of string or next format specifier. */
13809 while ((c = *this++) != '\0' && c != '%')
13810 ;
13811
13812 if (this - 1 != last)
13813 {
13814 /* Output to end of string or up to '%'. Field width
13815 is length of string. Don't output more than
13816 PRECISION allows us. */
13817 --this;
13818
13819 prec = chars_in_text (last, this - last);
13820 if (precision > 0 && prec > precision - n)
13821 prec = precision - n;
13822
13823 if (frame_title_ptr)
13824 n += store_frame_title (last, 0, prec);
13825 else if (!NILP (mode_line_string_list))
13826 {
13827 int bytepos = last - lisp_string;
13828 int charpos = string_byte_to_char (elt, bytepos);
13829 n += store_mode_line_string (NULL,
13830 Fsubstring (elt, make_number (charpos),
13831 make_number (charpos + prec)),
13832 0, 0, 0, Qnil);
13833 }
13834 else
13835 {
13836 int bytepos = last - lisp_string;
13837 int charpos = string_byte_to_char (elt, bytepos);
13838 n += display_string (NULL, elt, Qnil, 0, charpos,
13839 it, 0, prec, 0,
13840 STRING_MULTIBYTE (elt));
13841 }
13842 }
13843 else /* c == '%' */
13844 {
13845 const unsigned char *percent_position = this;
13846
13847 /* Get the specified minimum width. Zero means
13848 don't pad. */
13849 field = 0;
13850 while ((c = *this++) >= '0' && c <= '9')
13851 field = field * 10 + c - '0';
13852
13853 /* Don't pad beyond the total padding allowed. */
13854 if (field_width - n > 0 && field > field_width - n)
13855 field = field_width - n;
13856
13857 /* Note that either PRECISION <= 0 or N < PRECISION. */
13858 prec = precision - n;
13859
13860 if (c == 'M')
13861 n += display_mode_element (it, depth, field, prec,
13862 Vglobal_mode_string, props,
13863 risky);
13864 else if (c != 0)
13865 {
13866 int multibyte;
13867 int bytepos, charpos;
13868 unsigned char *spec;
13869
13870 bytepos = percent_position - lisp_string;
13871 charpos = (STRING_MULTIBYTE (elt)
13872 ? string_byte_to_char (elt, bytepos)
13873 : bytepos);
13874
13875 spec
13876 = decode_mode_spec (it->w, c, field, prec, &multibyte);
13877
13878 if (frame_title_ptr)
13879 n += store_frame_title (spec, field, prec);
13880 else if (!NILP (mode_line_string_list))
13881 {
13882 int len = strlen (spec);
13883 Lisp_Object tem = make_string (spec, len);
13884 props = Ftext_properties_at (make_number (charpos), elt);
13885 /* Should only keep face property in props */
13886 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
13887 }
13888 else
13889 {
13890 int nglyphs_before, nwritten;
13891
13892 nglyphs_before = it->glyph_row->used[TEXT_AREA];
13893 nwritten = display_string (spec, Qnil, elt,
13894 charpos, 0, it,
13895 field, prec, 0,
13896 multibyte);
13897
13898 /* Assign to the glyphs written above the
13899 string where the `%x' came from, position
13900 of the `%'. */
13901 if (nwritten > 0)
13902 {
13903 struct glyph *glyph
13904 = (it->glyph_row->glyphs[TEXT_AREA]
13905 + nglyphs_before);
13906 int i;
13907
13908 for (i = 0; i < nwritten; ++i)
13909 {
13910 glyph[i].object = elt;
13911 glyph[i].charpos = charpos;
13912 }
13913
13914 n += nwritten;
13915 }
13916 }
13917 }
13918 else /* c == 0 */
13919 break;
13920 }
13921 }
13922 }
13923 break;
13924
13925 case Lisp_Symbol:
13926 /* A symbol: process the value of the symbol recursively
13927 as if it appeared here directly. Avoid error if symbol void.
13928 Special case: if value of symbol is a string, output the string
13929 literally. */
13930 {
13931 register Lisp_Object tem;
13932
13933 /* If the variable is not marked as risky to set
13934 then its contents are risky to use. */
13935 if (NILP (Fget (elt, Qrisky_local_variable)))
13936 risky = 1;
13937
13938 tem = Fboundp (elt);
13939 if (!NILP (tem))
13940 {
13941 tem = Fsymbol_value (elt);
13942 /* If value is a string, output that string literally:
13943 don't check for % within it. */
13944 if (STRINGP (tem))
13945 literal = 1;
13946
13947 if (!EQ (tem, elt))
13948 {
13949 /* Give up right away for nil or t. */
13950 elt = tem;
13951 goto tail_recurse;
13952 }
13953 }
13954 }
13955 break;
13956
13957 case Lisp_Cons:
13958 {
13959 register Lisp_Object car, tem;
13960
13961 /* A cons cell: five distinct cases.
13962 If first element is :eval or :propertize, do something special.
13963 If first element is a string or a cons, process all the elements
13964 and effectively concatenate them.
13965 If first element is a negative number, truncate displaying cdr to
13966 at most that many characters. If positive, pad (with spaces)
13967 to at least that many characters.
13968 If first element is a symbol, process the cadr or caddr recursively
13969 according to whether the symbol's value is non-nil or nil. */
13970 car = XCAR (elt);
13971 if (EQ (car, QCeval))
13972 {
13973 /* An element of the form (:eval FORM) means evaluate FORM
13974 and use the result as mode line elements. */
13975
13976 if (risky)
13977 break;
13978
13979 if (CONSP (XCDR (elt)))
13980 {
13981 Lisp_Object spec;
13982 spec = safe_eval (XCAR (XCDR (elt)));
13983 n += display_mode_element (it, depth, field_width - n,
13984 precision - n, spec, props,
13985 risky);
13986 }
13987 }
13988 else if (EQ (car, QCpropertize))
13989 {
13990 /* An element of the form (:propertize ELT PROPS...)
13991 means display ELT but applying properties PROPS. */
13992
13993 if (risky)
13994 break;
13995
13996 if (CONSP (XCDR (elt)))
13997 n += display_mode_element (it, depth, field_width - n,
13998 precision - n, XCAR (XCDR (elt)),
13999 XCDR (XCDR (elt)), risky);
14000 }
14001 else if (SYMBOLP (car))
14002 {
14003 tem = Fboundp (car);
14004 elt = XCDR (elt);
14005 if (!CONSP (elt))
14006 goto invalid;
14007 /* elt is now the cdr, and we know it is a cons cell.
14008 Use its car if CAR has a non-nil value. */
14009 if (!NILP (tem))
14010 {
14011 tem = Fsymbol_value (car);
14012 if (!NILP (tem))
14013 {
14014 elt = XCAR (elt);
14015 goto tail_recurse;
14016 }
14017 }
14018 /* Symbol's value is nil (or symbol is unbound)
14019 Get the cddr of the original list
14020 and if possible find the caddr and use that. */
14021 elt = XCDR (elt);
14022 if (NILP (elt))
14023 break;
14024 else if (!CONSP (elt))
14025 goto invalid;
14026 elt = XCAR (elt);
14027 goto tail_recurse;
14028 }
14029 else if (INTEGERP (car))
14030 {
14031 register int lim = XINT (car);
14032 elt = XCDR (elt);
14033 if (lim < 0)
14034 {
14035 /* Negative int means reduce maximum width. */
14036 if (precision <= 0)
14037 precision = -lim;
14038 else
14039 precision = min (precision, -lim);
14040 }
14041 else if (lim > 0)
14042 {
14043 /* Padding specified. Don't let it be more than
14044 current maximum. */
14045 if (precision > 0)
14046 lim = min (precision, lim);
14047
14048 /* If that's more padding than already wanted, queue it.
14049 But don't reduce padding already specified even if
14050 that is beyond the current truncation point. */
14051 field_width = max (lim, field_width);
14052 }
14053 goto tail_recurse;
14054 }
14055 else if (STRINGP (car) || CONSP (car))
14056 {
14057 register int limit = 50;
14058 /* Limit is to protect against circular lists. */
14059 while (CONSP (elt)
14060 && --limit > 0
14061 && (precision <= 0 || n < precision))
14062 {
14063 n += display_mode_element (it, depth, field_width - n,
14064 precision - n, XCAR (elt),
14065 props, risky);
14066 elt = XCDR (elt);
14067 }
14068 }
14069 }
14070 break;
14071
14072 default:
14073 invalid:
14074 if (frame_title_ptr)
14075 n += store_frame_title ("*invalid*", 0, precision - n);
14076 else if (!NILP (mode_line_string_list))
14077 n += store_mode_line_string ("*invalid*", Qnil, 0, 0, precision - n, Qnil);
14078 else
14079 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
14080 precision - n, 0, 0);
14081 return n;
14082 }
14083
14084 /* Pad to FIELD_WIDTH. */
14085 if (field_width > 0 && n < field_width)
14086 {
14087 if (frame_title_ptr)
14088 n += store_frame_title ("", field_width - n, 0);
14089 else if (!NILP (mode_line_string_list))
14090 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
14091 else
14092 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
14093 0, 0, 0);
14094 }
14095
14096 return n;
14097 }
14098
14099 /* Store a mode-line string element in mode_line_string_list.
14100
14101 If STRING is non-null, display that C string. Otherwise, the Lisp
14102 string LISP_STRING is displayed.
14103
14104 FIELD_WIDTH is the minimum number of output glyphs to produce.
14105 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14106 with spaces. FIELD_WIDTH <= 0 means don't pad.
14107
14108 PRECISION is the maximum number of characters to output from
14109 STRING. PRECISION <= 0 means don't truncate the string.
14110
14111 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
14112 properties to the string.
14113
14114 PROPS are the properties to add to the string.
14115 The mode_line_string_face face property is always added to the string.
14116 */
14117
14118 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
14119 char *string;
14120 Lisp_Object lisp_string;
14121 int copy_string;
14122 int field_width;
14123 int precision;
14124 Lisp_Object props;
14125 {
14126 int len;
14127 int n = 0;
14128
14129 if (string != NULL)
14130 {
14131 len = strlen (string);
14132 if (precision > 0 && len > precision)
14133 len = precision;
14134 lisp_string = make_string (string, len);
14135 if (NILP (props))
14136 props = mode_line_string_face_prop;
14137 else if (!NILP (mode_line_string_face))
14138 {
14139 Lisp_Object face = Fplist_get (props, Qface);
14140 props = Fcopy_sequence (props);
14141 if (NILP (face))
14142 face = mode_line_string_face;
14143 else
14144 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14145 props = Fplist_put (props, Qface, face);
14146 }
14147 Fadd_text_properties (make_number (0), make_number (len),
14148 props, lisp_string);
14149 }
14150 else
14151 {
14152 len = XFASTINT (Flength (lisp_string));
14153 if (precision > 0 && len > precision)
14154 {
14155 len = precision;
14156 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
14157 precision = -1;
14158 }
14159 if (!NILP (mode_line_string_face))
14160 {
14161 Lisp_Object face;
14162 if (NILP (props))
14163 props = Ftext_properties_at (make_number (0), lisp_string);
14164 face = Fplist_get (props, Qface);
14165 if (NILP (face))
14166 face = mode_line_string_face;
14167 else
14168 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14169 props = Fcons (Qface, Fcons (face, Qnil));
14170 if (copy_string)
14171 lisp_string = Fcopy_sequence (lisp_string);
14172 }
14173 if (!NILP (props))
14174 Fadd_text_properties (make_number (0), make_number (len),
14175 props, lisp_string);
14176 }
14177
14178 if (len > 0)
14179 {
14180 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
14181 n += len;
14182 }
14183
14184 if (field_width > len)
14185 {
14186 field_width -= len;
14187 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
14188 if (!NILP (props))
14189 Fadd_text_properties (make_number (0), make_number (field_width),
14190 props, lisp_string);
14191 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
14192 n += field_width;
14193 }
14194
14195 return n;
14196 }
14197
14198
14199 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
14200 0, 3, 0,
14201 doc: /* Return the mode-line of selected window as a string.
14202 First optional arg FORMAT specifies a different format string (see
14203 `mode-line-format' for details) to use. If FORMAT is t, return
14204 the buffer's header-line. Second optional arg WINDOW specifies a
14205 different window to use as the context for the formatting.
14206 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
14207 (format, window, no_props)
14208 Lisp_Object format, window, no_props;
14209 {
14210 struct it it;
14211 int len;
14212 struct window *w;
14213 struct buffer *old_buffer = NULL;
14214 enum face_id face_id = DEFAULT_FACE_ID;
14215
14216 if (NILP (window))
14217 window = selected_window;
14218 CHECK_WINDOW (window);
14219 w = XWINDOW (window);
14220 CHECK_BUFFER (w->buffer);
14221
14222 if (XBUFFER (w->buffer) != current_buffer)
14223 {
14224 old_buffer = current_buffer;
14225 set_buffer_internal_1 (XBUFFER (w->buffer));
14226 }
14227
14228 if (NILP (format) || EQ (format, Qt))
14229 {
14230 face_id = NILP (format)
14231 ? CURRENT_MODE_LINE_FACE_ID (w) :
14232 HEADER_LINE_FACE_ID;
14233 format = NILP (format)
14234 ? current_buffer->mode_line_format
14235 : current_buffer->header_line_format;
14236 }
14237
14238 init_iterator (&it, w, -1, -1, NULL, face_id);
14239
14240 if (NILP (no_props))
14241 {
14242 mode_line_string_face =
14243 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
14244 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
14245 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
14246
14247 mode_line_string_face_prop =
14248 NILP (mode_line_string_face) ? Qnil :
14249 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
14250
14251 /* We need a dummy last element in mode_line_string_list to
14252 indicate we are building the propertized mode-line string.
14253 Using mode_line_string_face_prop here GC protects it. */
14254 mode_line_string_list =
14255 Fcons (mode_line_string_face_prop, Qnil);
14256 frame_title_ptr = NULL;
14257 }
14258 else
14259 {
14260 mode_line_string_face_prop = Qnil;
14261 mode_line_string_list = Qnil;
14262 frame_title_ptr = frame_title_buf;
14263 }
14264
14265 push_frame_kboard (it.f);
14266 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
14267 pop_frame_kboard ();
14268
14269 if (old_buffer)
14270 set_buffer_internal_1 (old_buffer);
14271
14272 if (NILP (no_props))
14273 {
14274 Lisp_Object str;
14275 mode_line_string_list = Fnreverse (mode_line_string_list);
14276 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
14277 make_string ("", 0));
14278 mode_line_string_face_prop = Qnil;
14279 mode_line_string_list = Qnil;
14280 return str;
14281 }
14282
14283 len = frame_title_ptr - frame_title_buf;
14284 if (len > 0 && frame_title_ptr[-1] == '-')
14285 {
14286 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
14287 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
14288 ;
14289 frame_title_ptr += 3; /* restore last non-dash + two dashes */
14290 if (len > frame_title_ptr - frame_title_buf)
14291 len = frame_title_ptr - frame_title_buf;
14292 }
14293
14294 frame_title_ptr = NULL;
14295 return make_string (frame_title_buf, len);
14296 }
14297
14298 /* Write a null-terminated, right justified decimal representation of
14299 the positive integer D to BUF using a minimal field width WIDTH. */
14300
14301 static void
14302 pint2str (buf, width, d)
14303 register char *buf;
14304 register int width;
14305 register int d;
14306 {
14307 register char *p = buf;
14308
14309 if (d <= 0)
14310 *p++ = '0';
14311 else
14312 {
14313 while (d > 0)
14314 {
14315 *p++ = d % 10 + '0';
14316 d /= 10;
14317 }
14318 }
14319
14320 for (width -= (int) (p - buf); width > 0; --width)
14321 *p++ = ' ';
14322 *p-- = '\0';
14323 while (p > buf)
14324 {
14325 d = *buf;
14326 *buf++ = *p;
14327 *p-- = d;
14328 }
14329 }
14330
14331 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
14332 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
14333 type of CODING_SYSTEM. Return updated pointer into BUF. */
14334
14335 static unsigned char invalid_eol_type[] = "(*invalid*)";
14336
14337 static char *
14338 decode_mode_spec_coding (coding_system, buf, eol_flag)
14339 Lisp_Object coding_system;
14340 register char *buf;
14341 int eol_flag;
14342 {
14343 Lisp_Object val;
14344 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
14345 const unsigned char *eol_str;
14346 int eol_str_len;
14347 /* The EOL conversion we are using. */
14348 Lisp_Object eoltype;
14349
14350 val = Fget (coding_system, Qcoding_system);
14351 eoltype = Qnil;
14352
14353 if (!VECTORP (val)) /* Not yet decided. */
14354 {
14355 if (multibyte)
14356 *buf++ = '-';
14357 if (eol_flag)
14358 eoltype = eol_mnemonic_undecided;
14359 /* Don't mention EOL conversion if it isn't decided. */
14360 }
14361 else
14362 {
14363 Lisp_Object eolvalue;
14364
14365 eolvalue = Fget (coding_system, Qeol_type);
14366
14367 if (multibyte)
14368 *buf++ = XFASTINT (AREF (val, 1));
14369
14370 if (eol_flag)
14371 {
14372 /* The EOL conversion that is normal on this system. */
14373
14374 if (NILP (eolvalue)) /* Not yet decided. */
14375 eoltype = eol_mnemonic_undecided;
14376 else if (VECTORP (eolvalue)) /* Not yet decided. */
14377 eoltype = eol_mnemonic_undecided;
14378 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
14379 eoltype = (XFASTINT (eolvalue) == 0
14380 ? eol_mnemonic_unix
14381 : (XFASTINT (eolvalue) == 1
14382 ? eol_mnemonic_dos : eol_mnemonic_mac));
14383 }
14384 }
14385
14386 if (eol_flag)
14387 {
14388 /* Mention the EOL conversion if it is not the usual one. */
14389 if (STRINGP (eoltype))
14390 {
14391 eol_str = SDATA (eoltype);
14392 eol_str_len = SBYTES (eoltype);
14393 }
14394 else if (INTEGERP (eoltype)
14395 && CHAR_VALID_P (XINT (eoltype), 0))
14396 {
14397 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
14398 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
14399 eol_str = tmp;
14400 }
14401 else
14402 {
14403 eol_str = invalid_eol_type;
14404 eol_str_len = sizeof (invalid_eol_type) - 1;
14405 }
14406 bcopy (eol_str, buf, eol_str_len);
14407 buf += eol_str_len;
14408 }
14409
14410 return buf;
14411 }
14412
14413 /* Return a string for the output of a mode line %-spec for window W,
14414 generated by character C. PRECISION >= 0 means don't return a
14415 string longer than that value. FIELD_WIDTH > 0 means pad the
14416 string returned with spaces to that value. Return 1 in *MULTIBYTE
14417 if the result is multibyte text. */
14418
14419 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
14420
14421 static char *
14422 decode_mode_spec (w, c, field_width, precision, multibyte)
14423 struct window *w;
14424 register int c;
14425 int field_width, precision;
14426 int *multibyte;
14427 {
14428 Lisp_Object obj;
14429 struct frame *f = XFRAME (WINDOW_FRAME (w));
14430 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
14431 struct buffer *b = XBUFFER (w->buffer);
14432
14433 obj = Qnil;
14434 *multibyte = 0;
14435
14436 switch (c)
14437 {
14438 case '*':
14439 if (!NILP (b->read_only))
14440 return "%";
14441 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14442 return "*";
14443 return "-";
14444
14445 case '+':
14446 /* This differs from %* only for a modified read-only buffer. */
14447 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14448 return "*";
14449 if (!NILP (b->read_only))
14450 return "%";
14451 return "-";
14452
14453 case '&':
14454 /* This differs from %* in ignoring read-only-ness. */
14455 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14456 return "*";
14457 return "-";
14458
14459 case '%':
14460 return "%";
14461
14462 case '[':
14463 {
14464 int i;
14465 char *p;
14466
14467 if (command_loop_level > 5)
14468 return "[[[... ";
14469 p = decode_mode_spec_buf;
14470 for (i = 0; i < command_loop_level; i++)
14471 *p++ = '[';
14472 *p = 0;
14473 return decode_mode_spec_buf;
14474 }
14475
14476 case ']':
14477 {
14478 int i;
14479 char *p;
14480
14481 if (command_loop_level > 5)
14482 return " ...]]]";
14483 p = decode_mode_spec_buf;
14484 for (i = 0; i < command_loop_level; i++)
14485 *p++ = ']';
14486 *p = 0;
14487 return decode_mode_spec_buf;
14488 }
14489
14490 case '-':
14491 {
14492 register int i;
14493
14494 /* Let lots_of_dashes be a string of infinite length. */
14495 if (!NILP (mode_line_string_list))
14496 return "--";
14497 if (field_width <= 0
14498 || field_width > sizeof (lots_of_dashes))
14499 {
14500 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
14501 decode_mode_spec_buf[i] = '-';
14502 decode_mode_spec_buf[i] = '\0';
14503 return decode_mode_spec_buf;
14504 }
14505 else
14506 return lots_of_dashes;
14507 }
14508
14509 case 'b':
14510 obj = b->name;
14511 break;
14512
14513 case 'c':
14514 {
14515 int col = (int) current_column (); /* iftc */
14516 w->column_number_displayed = make_number (col);
14517 pint2str (decode_mode_spec_buf, field_width, col);
14518 return decode_mode_spec_buf;
14519 }
14520
14521 case 'F':
14522 /* %F displays the frame name. */
14523 if (!NILP (f->title))
14524 return (char *) SDATA (f->title);
14525 if (f->explicit_name || ! FRAME_WINDOW_P (f))
14526 return (char *) SDATA (f->name);
14527 return "Emacs";
14528
14529 case 'f':
14530 obj = b->filename;
14531 break;
14532
14533 case 'l':
14534 {
14535 int startpos = XMARKER (w->start)->charpos;
14536 int startpos_byte = marker_byte_position (w->start);
14537 int line, linepos, linepos_byte, topline;
14538 int nlines, junk;
14539 int height = XFASTINT (w->height);
14540
14541 /* If we decided that this buffer isn't suitable for line numbers,
14542 don't forget that too fast. */
14543 if (EQ (w->base_line_pos, w->buffer))
14544 goto no_value;
14545 /* But do forget it, if the window shows a different buffer now. */
14546 else if (BUFFERP (w->base_line_pos))
14547 w->base_line_pos = Qnil;
14548
14549 /* If the buffer is very big, don't waste time. */
14550 if (INTEGERP (Vline_number_display_limit)
14551 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
14552 {
14553 w->base_line_pos = Qnil;
14554 w->base_line_number = Qnil;
14555 goto no_value;
14556 }
14557
14558 if (!NILP (w->base_line_number)
14559 && !NILP (w->base_line_pos)
14560 && XFASTINT (w->base_line_pos) <= startpos)
14561 {
14562 line = XFASTINT (w->base_line_number);
14563 linepos = XFASTINT (w->base_line_pos);
14564 linepos_byte = buf_charpos_to_bytepos (b, linepos);
14565 }
14566 else
14567 {
14568 line = 1;
14569 linepos = BUF_BEGV (b);
14570 linepos_byte = BUF_BEGV_BYTE (b);
14571 }
14572
14573 /* Count lines from base line to window start position. */
14574 nlines = display_count_lines (linepos, linepos_byte,
14575 startpos_byte,
14576 startpos, &junk);
14577
14578 topline = nlines + line;
14579
14580 /* Determine a new base line, if the old one is too close
14581 or too far away, or if we did not have one.
14582 "Too close" means it's plausible a scroll-down would
14583 go back past it. */
14584 if (startpos == BUF_BEGV (b))
14585 {
14586 w->base_line_number = make_number (topline);
14587 w->base_line_pos = make_number (BUF_BEGV (b));
14588 }
14589 else if (nlines < height + 25 || nlines > height * 3 + 50
14590 || linepos == BUF_BEGV (b))
14591 {
14592 int limit = BUF_BEGV (b);
14593 int limit_byte = BUF_BEGV_BYTE (b);
14594 int position;
14595 int distance = (height * 2 + 30) * line_number_display_limit_width;
14596
14597 if (startpos - distance > limit)
14598 {
14599 limit = startpos - distance;
14600 limit_byte = CHAR_TO_BYTE (limit);
14601 }
14602
14603 nlines = display_count_lines (startpos, startpos_byte,
14604 limit_byte,
14605 - (height * 2 + 30),
14606 &position);
14607 /* If we couldn't find the lines we wanted within
14608 line_number_display_limit_width chars per line,
14609 give up on line numbers for this window. */
14610 if (position == limit_byte && limit == startpos - distance)
14611 {
14612 w->base_line_pos = w->buffer;
14613 w->base_line_number = Qnil;
14614 goto no_value;
14615 }
14616
14617 w->base_line_number = make_number (topline - nlines);
14618 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
14619 }
14620
14621 /* Now count lines from the start pos to point. */
14622 nlines = display_count_lines (startpos, startpos_byte,
14623 PT_BYTE, PT, &junk);
14624
14625 /* Record that we did display the line number. */
14626 line_number_displayed = 1;
14627
14628 /* Make the string to show. */
14629 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
14630 return decode_mode_spec_buf;
14631 no_value:
14632 {
14633 char* p = decode_mode_spec_buf;
14634 int pad = field_width - 2;
14635 while (pad-- > 0)
14636 *p++ = ' ';
14637 *p++ = '?';
14638 *p++ = '?';
14639 *p = '\0';
14640 return decode_mode_spec_buf;
14641 }
14642 }
14643 break;
14644
14645 case 'm':
14646 obj = b->mode_name;
14647 break;
14648
14649 case 'n':
14650 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
14651 return " Narrow";
14652 break;
14653
14654 case 'p':
14655 {
14656 int pos = marker_position (w->start);
14657 int total = BUF_ZV (b) - BUF_BEGV (b);
14658
14659 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
14660 {
14661 if (pos <= BUF_BEGV (b))
14662 return "All";
14663 else
14664 return "Bottom";
14665 }
14666 else if (pos <= BUF_BEGV (b))
14667 return "Top";
14668 else
14669 {
14670 if (total > 1000000)
14671 /* Do it differently for a large value, to avoid overflow. */
14672 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14673 else
14674 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14675 /* We can't normally display a 3-digit number,
14676 so get us a 2-digit number that is close. */
14677 if (total == 100)
14678 total = 99;
14679 sprintf (decode_mode_spec_buf, "%2d%%", total);
14680 return decode_mode_spec_buf;
14681 }
14682 }
14683
14684 /* Display percentage of size above the bottom of the screen. */
14685 case 'P':
14686 {
14687 int toppos = marker_position (w->start);
14688 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14689 int total = BUF_ZV (b) - BUF_BEGV (b);
14690
14691 if (botpos >= BUF_ZV (b))
14692 {
14693 if (toppos <= BUF_BEGV (b))
14694 return "All";
14695 else
14696 return "Bottom";
14697 }
14698 else
14699 {
14700 if (total > 1000000)
14701 /* Do it differently for a large value, to avoid overflow. */
14702 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14703 else
14704 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14705 /* We can't normally display a 3-digit number,
14706 so get us a 2-digit number that is close. */
14707 if (total == 100)
14708 total = 99;
14709 if (toppos <= BUF_BEGV (b))
14710 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14711 else
14712 sprintf (decode_mode_spec_buf, "%2d%%", total);
14713 return decode_mode_spec_buf;
14714 }
14715 }
14716
14717 case 's':
14718 /* status of process */
14719 obj = Fget_buffer_process (w->buffer);
14720 if (NILP (obj))
14721 return "no process";
14722 #ifdef subprocesses
14723 obj = Fsymbol_name (Fprocess_status (obj));
14724 #endif
14725 break;
14726
14727 case 't': /* indicate TEXT or BINARY */
14728 #ifdef MODE_LINE_BINARY_TEXT
14729 return MODE_LINE_BINARY_TEXT (b);
14730 #else
14731 return "T";
14732 #endif
14733
14734 case 'z':
14735 /* coding-system (not including end-of-line format) */
14736 case 'Z':
14737 /* coding-system (including end-of-line type) */
14738 {
14739 int eol_flag = (c == 'Z');
14740 char *p = decode_mode_spec_buf;
14741
14742 if (! FRAME_WINDOW_P (f))
14743 {
14744 /* No need to mention EOL here--the terminal never needs
14745 to do EOL conversion. */
14746 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14747 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
14748 }
14749 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14750 p, eol_flag);
14751
14752 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14753 #ifdef subprocesses
14754 obj = Fget_buffer_process (Fcurrent_buffer ());
14755 if (PROCESSP (obj))
14756 {
14757 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14758 p, eol_flag);
14759 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14760 p, eol_flag);
14761 }
14762 #endif /* subprocesses */
14763 #endif /* 0 */
14764 *p = 0;
14765 return decode_mode_spec_buf;
14766 }
14767 }
14768
14769 if (STRINGP (obj))
14770 {
14771 *multibyte = STRING_MULTIBYTE (obj);
14772 return (char *) SDATA (obj);
14773 }
14774 else
14775 return "";
14776 }
14777
14778
14779 /* Count up to COUNT lines starting from START / START_BYTE.
14780 But don't go beyond LIMIT_BYTE.
14781 Return the number of lines thus found (always nonnegative).
14782
14783 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14784
14785 static int
14786 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14787 int start, start_byte, limit_byte, count;
14788 int *byte_pos_ptr;
14789 {
14790 register unsigned char *cursor;
14791 unsigned char *base;
14792
14793 register int ceiling;
14794 register unsigned char *ceiling_addr;
14795 int orig_count = count;
14796
14797 /* If we are not in selective display mode,
14798 check only for newlines. */
14799 int selective_display = (!NILP (current_buffer->selective_display)
14800 && !INTEGERP (current_buffer->selective_display));
14801
14802 if (count > 0)
14803 {
14804 while (start_byte < limit_byte)
14805 {
14806 ceiling = BUFFER_CEILING_OF (start_byte);
14807 ceiling = min (limit_byte - 1, ceiling);
14808 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14809 base = (cursor = BYTE_POS_ADDR (start_byte));
14810 while (1)
14811 {
14812 if (selective_display)
14813 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14814 ;
14815 else
14816 while (*cursor != '\n' && ++cursor != ceiling_addr)
14817 ;
14818
14819 if (cursor != ceiling_addr)
14820 {
14821 if (--count == 0)
14822 {
14823 start_byte += cursor - base + 1;
14824 *byte_pos_ptr = start_byte;
14825 return orig_count;
14826 }
14827 else
14828 if (++cursor == ceiling_addr)
14829 break;
14830 }
14831 else
14832 break;
14833 }
14834 start_byte += cursor - base;
14835 }
14836 }
14837 else
14838 {
14839 while (start_byte > limit_byte)
14840 {
14841 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14842 ceiling = max (limit_byte, ceiling);
14843 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14844 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14845 while (1)
14846 {
14847 if (selective_display)
14848 while (--cursor != ceiling_addr
14849 && *cursor != '\n' && *cursor != 015)
14850 ;
14851 else
14852 while (--cursor != ceiling_addr && *cursor != '\n')
14853 ;
14854
14855 if (cursor != ceiling_addr)
14856 {
14857 if (++count == 0)
14858 {
14859 start_byte += cursor - base + 1;
14860 *byte_pos_ptr = start_byte;
14861 /* When scanning backwards, we should
14862 not count the newline posterior to which we stop. */
14863 return - orig_count - 1;
14864 }
14865 }
14866 else
14867 break;
14868 }
14869 /* Here we add 1 to compensate for the last decrement
14870 of CURSOR, which took it past the valid range. */
14871 start_byte += cursor - base + 1;
14872 }
14873 }
14874
14875 *byte_pos_ptr = limit_byte;
14876
14877 if (count < 0)
14878 return - orig_count + count;
14879 return orig_count - count;
14880
14881 }
14882
14883
14884 \f
14885 /***********************************************************************
14886 Displaying strings
14887 ***********************************************************************/
14888
14889 /* Display a NUL-terminated string, starting with index START.
14890
14891 If STRING is non-null, display that C string. Otherwise, the Lisp
14892 string LISP_STRING is displayed.
14893
14894 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14895 FACE_STRING. Display STRING or LISP_STRING with the face at
14896 FACE_STRING_POS in FACE_STRING:
14897
14898 Display the string in the environment given by IT, but use the
14899 standard display table, temporarily.
14900
14901 FIELD_WIDTH is the minimum number of output glyphs to produce.
14902 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14903 with spaces. If STRING has more characters, more than FIELD_WIDTH
14904 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14905
14906 PRECISION is the maximum number of characters to output from
14907 STRING. PRECISION < 0 means don't truncate the string.
14908
14909 This is roughly equivalent to printf format specifiers:
14910
14911 FIELD_WIDTH PRECISION PRINTF
14912 ----------------------------------------
14913 -1 -1 %s
14914 -1 10 %.10s
14915 10 -1 %10s
14916 20 10 %20.10s
14917
14918 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14919 display them, and < 0 means obey the current buffer's value of
14920 enable_multibyte_characters.
14921
14922 Value is the number of glyphs produced. */
14923
14924 static int
14925 display_string (string, lisp_string, face_string, face_string_pos,
14926 start, it, field_width, precision, max_x, multibyte)
14927 unsigned char *string;
14928 Lisp_Object lisp_string;
14929 Lisp_Object face_string;
14930 int face_string_pos;
14931 int start;
14932 struct it *it;
14933 int field_width, precision, max_x;
14934 int multibyte;
14935 {
14936 int hpos_at_start = it->hpos;
14937 int saved_face_id = it->face_id;
14938 struct glyph_row *row = it->glyph_row;
14939
14940 /* Initialize the iterator IT for iteration over STRING beginning
14941 with index START. */
14942 reseat_to_string (it, string, lisp_string, start,
14943 precision, field_width, multibyte);
14944
14945 /* If displaying STRING, set up the face of the iterator
14946 from LISP_STRING, if that's given. */
14947 if (STRINGP (face_string))
14948 {
14949 int endptr;
14950 struct face *face;
14951
14952 it->face_id
14953 = face_at_string_position (it->w, face_string, face_string_pos,
14954 0, it->region_beg_charpos,
14955 it->region_end_charpos,
14956 &endptr, it->base_face_id, 0);
14957 face = FACE_FROM_ID (it->f, it->face_id);
14958 it->face_box_p = face->box != FACE_NO_BOX;
14959 }
14960
14961 /* Set max_x to the maximum allowed X position. Don't let it go
14962 beyond the right edge of the window. */
14963 if (max_x <= 0)
14964 max_x = it->last_visible_x;
14965 else
14966 max_x = min (max_x, it->last_visible_x);
14967
14968 /* Skip over display elements that are not visible. because IT->w is
14969 hscrolled. */
14970 if (it->current_x < it->first_visible_x)
14971 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14972 MOVE_TO_POS | MOVE_TO_X);
14973
14974 row->ascent = it->max_ascent;
14975 row->height = it->max_ascent + it->max_descent;
14976 row->phys_ascent = it->max_phys_ascent;
14977 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14978
14979 /* This condition is for the case that we are called with current_x
14980 past last_visible_x. */
14981 while (it->current_x < max_x)
14982 {
14983 int x_before, x, n_glyphs_before, i, nglyphs;
14984
14985 /* Get the next display element. */
14986 if (!get_next_display_element (it))
14987 break;
14988
14989 /* Produce glyphs. */
14990 x_before = it->current_x;
14991 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14992 PRODUCE_GLYPHS (it);
14993
14994 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14995 i = 0;
14996 x = x_before;
14997 while (i < nglyphs)
14998 {
14999 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15000
15001 if (!it->truncate_lines_p
15002 && x + glyph->pixel_width > max_x)
15003 {
15004 /* End of continued line or max_x reached. */
15005 if (CHAR_GLYPH_PADDING_P (*glyph))
15006 {
15007 /* A wide character is unbreakable. */
15008 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
15009 it->current_x = x_before;
15010 }
15011 else
15012 {
15013 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
15014 it->current_x = x;
15015 }
15016 break;
15017 }
15018 else if (x + glyph->pixel_width > it->first_visible_x)
15019 {
15020 /* Glyph is at least partially visible. */
15021 ++it->hpos;
15022 if (x < it->first_visible_x)
15023 it->glyph_row->x = x - it->first_visible_x;
15024 }
15025 else
15026 {
15027 /* Glyph is off the left margin of the display area.
15028 Should not happen. */
15029 abort ();
15030 }
15031
15032 row->ascent = max (row->ascent, it->max_ascent);
15033 row->height = max (row->height, it->max_ascent + it->max_descent);
15034 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15035 row->phys_height = max (row->phys_height,
15036 it->max_phys_ascent + it->max_phys_descent);
15037 x += glyph->pixel_width;
15038 ++i;
15039 }
15040
15041 /* Stop if max_x reached. */
15042 if (i < nglyphs)
15043 break;
15044
15045 /* Stop at line ends. */
15046 if (ITERATOR_AT_END_OF_LINE_P (it))
15047 {
15048 it->continuation_lines_width = 0;
15049 break;
15050 }
15051
15052 set_iterator_to_next (it, 1);
15053
15054 /* Stop if truncating at the right edge. */
15055 if (it->truncate_lines_p
15056 && it->current_x >= it->last_visible_x)
15057 {
15058 /* Add truncation mark, but don't do it if the line is
15059 truncated at a padding space. */
15060 if (IT_CHARPOS (*it) < it->string_nchars)
15061 {
15062 if (!FRAME_WINDOW_P (it->f))
15063 {
15064 int i, n;
15065
15066 if (it->current_x > it->last_visible_x)
15067 {
15068 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15069 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15070 break;
15071 for (n = row->used[TEXT_AREA]; i < n; ++i)
15072 {
15073 row->used[TEXT_AREA] = i;
15074 produce_special_glyphs (it, IT_TRUNCATION);
15075 }
15076 }
15077 produce_special_glyphs (it, IT_TRUNCATION);
15078 }
15079 it->glyph_row->truncated_on_right_p = 1;
15080 }
15081 break;
15082 }
15083 }
15084
15085 /* Maybe insert a truncation at the left. */
15086 if (it->first_visible_x
15087 && IT_CHARPOS (*it) > 0)
15088 {
15089 if (!FRAME_WINDOW_P (it->f))
15090 insert_left_trunc_glyphs (it);
15091 it->glyph_row->truncated_on_left_p = 1;
15092 }
15093
15094 it->face_id = saved_face_id;
15095
15096 /* Value is number of columns displayed. */
15097 return it->hpos - hpos_at_start;
15098 }
15099
15100
15101 \f
15102 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
15103 appears as an element of LIST or as the car of an element of LIST.
15104 If PROPVAL is a list, compare each element against LIST in that
15105 way, and return 1/2 if any element of PROPVAL is found in LIST.
15106 Otherwise return 0. This function cannot quit.
15107 The return value is 2 if the text is invisible but with an ellipsis
15108 and 1 if it's invisible and without an ellipsis. */
15109
15110 int
15111 invisible_p (propval, list)
15112 register Lisp_Object propval;
15113 Lisp_Object list;
15114 {
15115 register Lisp_Object tail, proptail;
15116
15117 for (tail = list; CONSP (tail); tail = XCDR (tail))
15118 {
15119 register Lisp_Object tem;
15120 tem = XCAR (tail);
15121 if (EQ (propval, tem))
15122 return 1;
15123 if (CONSP (tem) && EQ (propval, XCAR (tem)))
15124 return NILP (XCDR (tem)) ? 1 : 2;
15125 }
15126
15127 if (CONSP (propval))
15128 {
15129 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
15130 {
15131 Lisp_Object propelt;
15132 propelt = XCAR (proptail);
15133 for (tail = list; CONSP (tail); tail = XCDR (tail))
15134 {
15135 register Lisp_Object tem;
15136 tem = XCAR (tail);
15137 if (EQ (propelt, tem))
15138 return 1;
15139 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
15140 return NILP (XCDR (tem)) ? 1 : 2;
15141 }
15142 }
15143 }
15144
15145 return 0;
15146 }
15147
15148 \f
15149 /***********************************************************************
15150 Initialization
15151 ***********************************************************************/
15152
15153 void
15154 syms_of_xdisp ()
15155 {
15156 Vwith_echo_area_save_vector = Qnil;
15157 staticpro (&Vwith_echo_area_save_vector);
15158
15159 Vmessage_stack = Qnil;
15160 staticpro (&Vmessage_stack);
15161
15162 Qinhibit_redisplay = intern ("inhibit-redisplay");
15163 staticpro (&Qinhibit_redisplay);
15164
15165 message_dolog_marker1 = Fmake_marker ();
15166 staticpro (&message_dolog_marker1);
15167 message_dolog_marker2 = Fmake_marker ();
15168 staticpro (&message_dolog_marker2);
15169 message_dolog_marker3 = Fmake_marker ();
15170 staticpro (&message_dolog_marker3);
15171
15172 #if GLYPH_DEBUG
15173 defsubr (&Sdump_frame_glyph_matrix);
15174 defsubr (&Sdump_glyph_matrix);
15175 defsubr (&Sdump_glyph_row);
15176 defsubr (&Sdump_tool_bar_row);
15177 defsubr (&Strace_redisplay);
15178 defsubr (&Strace_to_stderr);
15179 #endif
15180 #ifdef HAVE_WINDOW_SYSTEM
15181 defsubr (&Stool_bar_lines_needed);
15182 #endif
15183 defsubr (&Sformat_mode_line);
15184
15185 staticpro (&Qmenu_bar_update_hook);
15186 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
15187
15188 staticpro (&Qoverriding_terminal_local_map);
15189 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
15190
15191 staticpro (&Qoverriding_local_map);
15192 Qoverriding_local_map = intern ("overriding-local-map");
15193
15194 staticpro (&Qwindow_scroll_functions);
15195 Qwindow_scroll_functions = intern ("window-scroll-functions");
15196
15197 staticpro (&Qredisplay_end_trigger_functions);
15198 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
15199
15200 staticpro (&Qinhibit_point_motion_hooks);
15201 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
15202
15203 QCdata = intern (":data");
15204 staticpro (&QCdata);
15205 Qdisplay = intern ("display");
15206 staticpro (&Qdisplay);
15207 Qspace_width = intern ("space-width");
15208 staticpro (&Qspace_width);
15209 Qraise = intern ("raise");
15210 staticpro (&Qraise);
15211 Qspace = intern ("space");
15212 staticpro (&Qspace);
15213 Qmargin = intern ("margin");
15214 staticpro (&Qmargin);
15215 Qleft_margin = intern ("left-margin");
15216 staticpro (&Qleft_margin);
15217 Qright_margin = intern ("right-margin");
15218 staticpro (&Qright_margin);
15219 Qalign_to = intern ("align-to");
15220 staticpro (&Qalign_to);
15221 QCalign_to = intern (":align-to");
15222 staticpro (&QCalign_to);
15223 Qrelative_width = intern ("relative-width");
15224 staticpro (&Qrelative_width);
15225 QCrelative_width = intern (":relative-width");
15226 staticpro (&QCrelative_width);
15227 QCrelative_height = intern (":relative-height");
15228 staticpro (&QCrelative_height);
15229 QCeval = intern (":eval");
15230 staticpro (&QCeval);
15231 QCpropertize = intern (":propertize");
15232 staticpro (&QCpropertize);
15233 Qwhen = intern ("when");
15234 staticpro (&Qwhen);
15235 QCfile = intern (":file");
15236 staticpro (&QCfile);
15237 Qfontified = intern ("fontified");
15238 staticpro (&Qfontified);
15239 Qfontification_functions = intern ("fontification-functions");
15240 staticpro (&Qfontification_functions);
15241 Qtrailing_whitespace = intern ("trailing-whitespace");
15242 staticpro (&Qtrailing_whitespace);
15243 Qimage = intern ("image");
15244 staticpro (&Qimage);
15245 Qmessage_truncate_lines = intern ("message-truncate-lines");
15246 staticpro (&Qmessage_truncate_lines);
15247 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
15248 staticpro (&Qcursor_in_non_selected_windows);
15249 Qgrow_only = intern ("grow-only");
15250 staticpro (&Qgrow_only);
15251 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
15252 staticpro (&Qinhibit_menubar_update);
15253 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
15254 staticpro (&Qinhibit_eval_during_redisplay);
15255 Qposition = intern ("position");
15256 staticpro (&Qposition);
15257 Qbuffer_position = intern ("buffer-position");
15258 staticpro (&Qbuffer_position);
15259 Qobject = intern ("object");
15260 staticpro (&Qobject);
15261 Qrisky_local_variable = intern ("risky-local-variable");
15262 staticpro (&Qrisky_local_variable);
15263
15264 list_of_error = Fcons (intern ("error"), Qnil);
15265 staticpro (&list_of_error);
15266
15267 last_arrow_position = Qnil;
15268 last_arrow_string = Qnil;
15269 staticpro (&last_arrow_position);
15270 staticpro (&last_arrow_string);
15271
15272 echo_buffer[0] = echo_buffer[1] = Qnil;
15273 staticpro (&echo_buffer[0]);
15274 staticpro (&echo_buffer[1]);
15275
15276 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
15277 staticpro (&echo_area_buffer[0]);
15278 staticpro (&echo_area_buffer[1]);
15279
15280 Vmessages_buffer_name = build_string ("*Messages*");
15281 staticpro (&Vmessages_buffer_name);
15282
15283 mode_line_proptrans_alist = Qnil;
15284 staticpro (&mode_line_proptrans_alist);
15285
15286 mode_line_string_list = Qnil;
15287 staticpro (&mode_line_string_list);
15288
15289 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
15290 doc: /* Non-nil means highlight trailing whitespace.
15291 The face used for trailing whitespace is `trailing-whitespace'. */);
15292 Vshow_trailing_whitespace = Qnil;
15293
15294 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
15295 doc: /* Non-nil means don't actually do any redisplay.
15296 This is used for internal purposes. */);
15297 Vinhibit_redisplay = Qnil;
15298
15299 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
15300 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
15301 Vglobal_mode_string = Qnil;
15302
15303 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
15304 doc: /* Marker for where to display an arrow on top of the buffer text.
15305 This must be the beginning of a line in order to work.
15306 See also `overlay-arrow-string'. */);
15307 Voverlay_arrow_position = Qnil;
15308
15309 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
15310 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
15311 Voverlay_arrow_string = Qnil;
15312
15313 DEFVAR_INT ("scroll-step", &scroll_step,
15314 doc: /* *The number of lines to try scrolling a window by when point moves out.
15315 If that fails to bring point back on frame, point is centered instead.
15316 If this is zero, point is always centered after it moves off frame.
15317 If you want scrolling to always be a line at a time, you should set
15318 `scroll-conservatively' to a large value rather than set this to 1. */);
15319
15320 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
15321 doc: /* *Scroll up to this many lines, to bring point back on screen.
15322 A value of zero means to scroll the text to center point vertically
15323 in the window. */);
15324 scroll_conservatively = 0;
15325
15326 DEFVAR_INT ("scroll-margin", &scroll_margin,
15327 doc: /* *Number of lines of margin at the top and bottom of a window.
15328 Recenter the window whenever point gets within this many lines
15329 of the top or bottom of the window. */);
15330 scroll_margin = 0;
15331
15332 #if GLYPH_DEBUG
15333 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
15334 #endif
15335
15336 DEFVAR_BOOL ("truncate-partial-width-windows",
15337 &truncate_partial_width_windows,
15338 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
15339 truncate_partial_width_windows = 1;
15340
15341 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
15342 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
15343 Any other value means to use the appropriate face, `mode-line',
15344 `header-line', or `menu' respectively. */);
15345 mode_line_inverse_video = 1;
15346
15347 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
15348 doc: /* *Maximum buffer size for which line number should be displayed.
15349 If the buffer is bigger than this, the line number does not appear
15350 in the mode line. A value of nil means no limit. */);
15351 Vline_number_display_limit = Qnil;
15352
15353 DEFVAR_INT ("line-number-display-limit-width",
15354 &line_number_display_limit_width,
15355 doc: /* *Maximum line width (in characters) for line number display.
15356 If the average length of the lines near point is bigger than this, then the
15357 line number may be omitted from the mode line. */);
15358 line_number_display_limit_width = 200;
15359
15360 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
15361 doc: /* *Non-nil means highlight region even in nonselected windows. */);
15362 highlight_nonselected_windows = 0;
15363
15364 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
15365 doc: /* Non-nil if more than one frame is visible on this display.
15366 Minibuffer-only frames don't count, but iconified frames do.
15367 This variable is not guaranteed to be accurate except while processing
15368 `frame-title-format' and `icon-title-format'. */);
15369
15370 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
15371 doc: /* Template for displaying the title bar of visible frames.
15372 \(Assuming the window manager supports this feature.)
15373 This variable has the same structure as `mode-line-format' (which see),
15374 and is used only on frames for which no explicit name has been set
15375 \(see `modify-frame-parameters'). */);
15376 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
15377 doc: /* Template for displaying the title bar of an iconified frame.
15378 \(Assuming the window manager supports this feature.)
15379 This variable has the same structure as `mode-line-format' (which see),
15380 and is used only on frames for which no explicit name has been set
15381 \(see `modify-frame-parameters'). */);
15382 Vicon_title_format
15383 = Vframe_title_format
15384 = Fcons (intern ("multiple-frames"),
15385 Fcons (build_string ("%b"),
15386 Fcons (Fcons (empty_string,
15387 Fcons (intern ("invocation-name"),
15388 Fcons (build_string ("@"),
15389 Fcons (intern ("system-name"),
15390 Qnil)))),
15391 Qnil)));
15392
15393 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
15394 doc: /* Maximum number of lines to keep in the message log buffer.
15395 If nil, disable message logging. If t, log messages but don't truncate
15396 the buffer when it becomes large. */);
15397 Vmessage_log_max = make_number (50);
15398
15399 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
15400 doc: /* Functions called before redisplay, if window sizes have changed.
15401 The value should be a list of functions that take one argument.
15402 Just before redisplay, for each frame, if any of its windows have changed
15403 size since the last redisplay, or have been split or deleted,
15404 all the functions in the list are called, with the frame as argument. */);
15405 Vwindow_size_change_functions = Qnil;
15406
15407 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
15408 doc: /* List of Functions to call before redisplaying a window with scrolling.
15409 Each function is called with two arguments, the window
15410 and its new display-start position. Note that the value of `window-end'
15411 is not valid when these functions are called. */);
15412 Vwindow_scroll_functions = Qnil;
15413
15414 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
15415 doc: /* *Non-nil means automatically resize tool-bars.
15416 This increases a tool-bar's height if not all tool-bar items are visible.
15417 It decreases a tool-bar's height when it would display blank lines
15418 otherwise. */);
15419 auto_resize_tool_bars_p = 1;
15420
15421 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
15422 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
15423 auto_raise_tool_bar_buttons_p = 1;
15424
15425 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
15426 doc: /* *Margin around tool-bar buttons in pixels.
15427 If an integer, use that for both horizontal and vertical margins.
15428 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
15429 HORZ specifying the horizontal margin, and VERT specifying the
15430 vertical margin. */);
15431 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
15432
15433 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
15434 doc: /* *Relief thickness of tool-bar buttons. */);
15435 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
15436
15437 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
15438 doc: /* List of functions to call to fontify regions of text.
15439 Each function is called with one argument POS. Functions must
15440 fontify a region starting at POS in the current buffer, and give
15441 fontified regions the property `fontified'. */);
15442 Vfontification_functions = Qnil;
15443 Fmake_variable_buffer_local (Qfontification_functions);
15444
15445 DEFVAR_BOOL ("unibyte-display-via-language-environment",
15446 &unibyte_display_via_language_environment,
15447 doc: /* *Non-nil means display unibyte text according to language environment.
15448 Specifically this means that unibyte non-ASCII characters
15449 are displayed by converting them to the equivalent multibyte characters
15450 according to the current language environment. As a result, they are
15451 displayed according to the current fontset. */);
15452 unibyte_display_via_language_environment = 0;
15453
15454 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
15455 doc: /* *Maximum height for resizing mini-windows.
15456 If a float, it specifies a fraction of the mini-window frame's height.
15457 If an integer, it specifies a number of lines. */);
15458 Vmax_mini_window_height = make_float (0.25);
15459
15460 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
15461 doc: /* *How to resize mini-windows.
15462 A value of nil means don't automatically resize mini-windows.
15463 A value of t means resize them to fit the text displayed in them.
15464 A value of `grow-only', the default, means let mini-windows grow
15465 only, until their display becomes empty, at which point the windows
15466 go back to their normal size. */);
15467 Vresize_mini_windows = Qgrow_only;
15468
15469 DEFVAR_BOOL ("cursor-in-non-selected-windows",
15470 &cursor_in_non_selected_windows,
15471 doc: /* *Non-nil means display a hollow cursor in non-selected windows.
15472 nil means don't display a cursor there. */);
15473 cursor_in_non_selected_windows = 1;
15474
15475 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
15476 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
15477 automatic_hscrolling_p = 1;
15478
15479 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
15480 doc: /* *How many columns away from the window edge point is allowed to get
15481 before automatic hscrolling will horizontally scroll the window. */);
15482 hscroll_margin = 5;
15483
15484 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
15485 doc: /* *How many columns to scroll the window when point gets too close to the edge.
15486 When point is less than `automatic-hscroll-margin' columns from the window
15487 edge, automatic hscrolling will scroll the window by the amount of columns
15488 determined by this variable. If its value is a positive integer, scroll that
15489 many columns. If it's a positive floating-point number, it specifies the
15490 fraction of the window's width to scroll. If it's nil or zero, point will be
15491 centered horizontally after the scroll. Any other value, including negative
15492 numbers, are treated as if the value were zero.
15493
15494 Automatic hscrolling always moves point outside the scroll margin, so if
15495 point was more than scroll step columns inside the margin, the window will
15496 scroll more than the value given by the scroll step.
15497
15498 Note that the lower bound for automatic hscrolling specified by `scroll-left'
15499 and `scroll-right' overrides this variable's effect. */);
15500 Vhscroll_step = make_number (0);
15501
15502 DEFVAR_LISP ("image-types", &Vimage_types,
15503 doc: /* List of supported image types.
15504 Each element of the list is a symbol for a supported image type. */);
15505 Vimage_types = Qnil;
15506
15507 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
15508 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
15509 Bind this around calls to `message' to let it take effect. */);
15510 message_truncate_lines = 0;
15511
15512 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
15513 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
15514 Can be used to update submenus whose contents should vary. */);
15515 Vmenu_bar_update_hook = Qnil;
15516
15517 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
15518 doc: /* Non-nil means don't update menu bars. Internal use only. */);
15519 inhibit_menubar_update = 0;
15520
15521 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
15522 doc: /* Non-nil means don't eval Lisp during redisplay. */);
15523 inhibit_eval_during_redisplay = 0;
15524
15525 #if GLYPH_DEBUG
15526 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
15527 doc: /* Inhibit try_window_id display optimization. */);
15528 inhibit_try_window_id = 0;
15529
15530 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
15531 doc: /* Inhibit try_window_reusing display optimization. */);
15532 inhibit_try_window_reusing = 0;
15533
15534 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
15535 doc: /* Inhibit try_cursor_movement display optimization. */);
15536 inhibit_try_cursor_movement = 0;
15537 #endif /* GLYPH_DEBUG */
15538 }
15539
15540
15541 /* Initialize this module when Emacs starts. */
15542
15543 void
15544 init_xdisp ()
15545 {
15546 Lisp_Object root_window;
15547 struct window *mini_w;
15548
15549 current_header_line_height = current_mode_line_height = -1;
15550
15551 CHARPOS (this_line_start_pos) = 0;
15552
15553 mini_w = XWINDOW (minibuf_window);
15554 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
15555
15556 if (!noninteractive)
15557 {
15558 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
15559 int i;
15560
15561 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
15562 set_window_height (root_window,
15563 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
15564 0);
15565 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
15566 set_window_height (minibuf_window, 1, 0);
15567
15568 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
15569 mini_w->width = make_number (FRAME_WIDTH (f));
15570
15571 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
15572 scratch_glyph_row.glyphs[TEXT_AREA + 1]
15573 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
15574
15575 /* The default ellipsis glyphs `...'. */
15576 for (i = 0; i < 3; ++i)
15577 default_invis_vector[i] = make_number ('.');
15578 }
15579
15580 {
15581 /* Allocate the buffer for frame titles.
15582 Also used for `format-mode-line'. */
15583 int size = 100;
15584 frame_title_buf = (char *) xmalloc (size);
15585 frame_title_buf_end = frame_title_buf + size;
15586 frame_title_ptr = NULL;
15587 }
15588
15589 help_echo_showing_p = 0;
15590 }
15591
15592